﻿// JScript File

var validationBehaviours =
{
	//behaviour attached to the validation function of an element with class mandatory
	".v-mandatory":function(el){
		//if it's an input button
		if(el.nodeName.toUpperCase() == "INPUT"){
			
			//check for previous validation
			try{
				if(el.validate.push){}
			}catch(e){
				el.validate = new Array();
			}
			//what type of validation
			switch(el.type){
				case "text":
				case "hidden":
				case "password": // for Text or Password field
					el.validate.push(function(element){
						if(element.value.length < 1){
								jQuery(el).ancestors("form").each(function(){
									this.errorStack.push({name:el.name.toString(),obj:el,label:jQuery("label[@for='"+el.name+"']")[0]});

								})
						}
					});
					break;		
				break;
				case "radio":
				case "checkbox": 
					el.validate.push(function(el){
							var valid = false;
							jQuery("input[@name='"+el.name+"']").each(function(){
								if(this.name == el.name){										
									if(this.checked){valid = true}
								}
							});
							if(!valid){
								jQuery(el).ancestors("form").each(function(){
									this.errorStack.push({name:el.name.toString(),obj:el,label:jQuery("label[@for='"+el.name+"']")[0]});

								})
							}
							jQuery(el).ancestors("form").each(function(){
								this.stopCheck.push(el.name);
							})
					})
				break;
				case "file":
				break;
				case "image":
				case "submit":
				case "button":
				default:
				
			}
		}
		//if it's a select box 
		if(el.nodeName.toUpperCase() == "SELECT"){
			//check for previous validation
			if(typeof el.validate == "undefined"){
				el.validate = new Array();
			}				
			el.validate.push(function(el){
				var valid = false;
				if(typeof validateConfig != "undefined" && typeof validateConfig["selectErrorOptions"] != "undefined" && typeof validateConfig["selectErrorOptions"][el.name] != "undefined"){
					if(el.value != validateConfig["selectErrorOptions"][el.name]){valid = true}
				}else{
					if(el.value != -1){valid = true}
				}
				
				if(!valid){
					jQuery(el).ancestors("form").each(function(){
						this.errorStack.push({name:el.name.toString(),obj:el,label:jQuery("label[@for='"+el.name+"']")[0]});

					})
				}
			})
			
		}
		// if it's a textarea
		if(el.nodeName.toUpperCase() == "TEXTAREA"){
			//check for previous validation
			if(typeof el.validate == "undefined"){
				el.validate = new Array();
			}				
			el.validate.push(function(element){
				if(!element.value.length){
					jQuery(el).ancestors("form").each(function(){
						this.errorStack.push({name:el.name.toString(),obj:el,label:jQuery("label[@for='"+el.name+"']")[0]});

					})
				}
			});			
		}
	},
	".v-postalCode":function(el){
		if(typeof el.validate == "undefined"){
			el.validate = new Array();
		}
		el.validate.push(function(el){
		
			/*if(!/^[a-zA-Z\d\s]*$/.test(el.value)){
					jQuery(el).ancestors("form").each(function(){
						this.errorStack.push({name:el.name.toString(),obj:el,label:jQuery("label[@for='"+el.name+"']")[0]});
					})
			}	*/	
		
			if(!/^([a-zA-Z]{1}\d{1}[a-zA-Z]{1}\s?\d{1}[a-zA-Z]{1}\d{1})?$/.test(el.value)){
			        if(!/^(\d{5}((-|\s){1}\d{4})?)?$/.test(el.value)){
					    jQuery(el).ancestors("form").each(function(){
						    this.errorStack.push({name:el.name.toString(),obj:el,label:jQuery("label[@for='"+el.name+"']")[0]});
					    })
					}
			}
		});			
	},
	".v-email":function(el){
		if(typeof el.validate == "undefined"){
			el.validate = new Array();
		}
		el.validate.push(function(el){
			if(!/^(\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+)?$/.test(el.value)){
					jQuery(el).ancestors("form").each(function(){
						this.errorStack.push({name:el.name.toString(),obj:el,label:jQuery("label[@for='"+el.name+"']")[0]});

					})
			}
		});
	},
	".v-numeric":function(el){
		if(typeof el.validate == "undefined"){
			el.validate = new Array();
		}
		el.validate.push(function(el){
			if(!/^(\d+([\.]?\d+)?)?$/.test(el.value)){
					jQuery(el).ancestors("form").each(function(){
						this.errorStack.push({name:el.name.toString(),obj:el,label:jQuery("label[@for='"+el.name+"']")[0]});

					})
			}
		});		
	},
	".v-currency":function(el){
		if(typeof el.validate == "undefined"){
			el.validate = new Array();
		}
		el.validate.push(function(el){
			if(!/^((\$)?(\s)?\d+([\.]?\d+)?(\s)?(\$)?)?$/.test(el.value)){
					jQuery(el).ancestors("form").each(function(){
						this.errorStack.push({name:el.name.toString(),obj:el,label:jQuery("label[@for='"+el.name+"']")[0]});

					})
			}
		});		
	},
	".v-alpha":function(el){
		if(typeof el.validate == "undefined"){
			el.validate = new Array();
		}
		el.validate.push(function(el){
			if(!/^([a-zA-Z\s]*)?$/.test(el.value)){
					jQuery(el).ancestors("form").each(function(){
						this.errorStack.push({name:el.name.toString(),obj:el,label:jQuery("label[@for='"+el.name+"']")[0]});

					})
			}
		});		
	},
	".v-password":function(el){
		if(typeof el.validate == "undefined"){
			el.validate = new Array();
		}		
		el.validate.push(function(){
			try{
				/*var valid = true
				for(validate in validateConfig["vPasswordSecurity"]){
					var checkPassword = new RegExp(validateConfig["vPasswordSecurity"][validate],"g");
					if(!checkPassword.exec(el.value)){valid=false;}
				}
				if(!valid){
					jQuery(el).ancestors("form").each(function(){
						this.errorStack.push({name:el.name.toString(),obj:el,label:jQuery("label[@for='"+el.name+"']")[0]});

					})
				}*/
			    if(!/^[a-zA-Z0-9]{2,}$/.test(el.value)){
					    jQuery(el).ancestors("form").each(function(){
						    this.errorStack.push({name:el.name.toString(),obj:el,label:jQuery("label[@for='"+el.name+"']")[0]});

					    })
			    }				
			}catch(e){
			    alert("ERROR");
			}
		});				
	},
	".v-phone":function(el){
		if(typeof el.validate == "undefined"){
			el.validate = new Array();
		}
		el.validate.push(function(el){
		
			if(!/^[\d\s()-]*((\s)+(ext|x|entension)?(\s)?(\d)+)?$/i.test(el.value)){
			    jQuery(el).ancestors("form").each(function(){
				    this.errorStack.push({name:el.name.toString(),obj:el,label:jQuery("label[@for='"+el.name+"']")[0]});

			    })
		    }
		    /*		
			if(!/^((1)?(\s)?(\+)?(\s)?(\()?\d{3}(\))?\s?-?\d{3}\s?-?\d{4}((\s)+(ext|x|entension)?(\s)?(\d)+)?)?$/i.test(el.value)){
				if(!/^(\+)?011(\s)?(43|32|385|420|45|358|33|49|44|30|353|39|31|47|351|34|46|41|90)(\s)[\d\s]+$/i.test(el.value)){
				    jQuery(el).ancestors("form").each(function(){
					    this.errorStack.push({name:el.name.toString(),obj:el,label:jQuery("label[@for='"+el.name+"']")[0]});

				    })
			    }
			}*/			
		});			
	},
	".v-FQDN":function(el){
		if(typeof el.validate == "undefined"){
			el.validate = new Array();
		}
		el.validate.push(function(el){
			if(!/^(([a-zA-Z0-9]+([.\-_]?))*[a-zA-Z0-9]+([.]{1}[a-zA-Z]{2,3}){1})?$/.test(el.value)){
				jQuery(el).ancestors("form").each(function(){
					this.errorStack.push({name:el.name.toString(),obj:el,label:jQuery("label[@for='"+el.name+"']")[0]});

				})
			}
		});		
	},
	".v-NOSpecial":function(el){
		if(typeof el.validate == "undefined"){
			el.validate = new Array();
		}
		el.validate.push(function(el){
			if(!/^([a-zA-Z0-9_]*)?$/.test(el.value)){
				jQuery(el).ancestors("form").each(function(){
					this.errorStack.push({name:el.name.toString(),obj:el,label:jQuery("label[@for='"+el.name+"']")[0]});

				})
			}
		});		
	},	
	".v-confirmation":function(el){
		if(typeof el.validate == "undefined"){
			el.validate = new Array();
		}
		el.validate.push(function(el){
			jQuery("#"+el.name.replace(validateConfig["confirmPrefix"],"")).each(function(){
				if(this.value != el.value){
					jQuery(el).ancestors("form").each(function(){
						this.errorStack.push({name:el.name.toString(),obj:el,label:jQuery("label[@for='"+el.name+"']")[0]});
					})
				}
			});
		});
	},
	"form.v-form":function(el){
		if(typeof el.validators == "undefined"){
			el.validators = new Array();
			for (selector in this){
				if(selector.indexOf(".") != -1){
					el.validators.push(selector);
				}
			}				
		}

	
		el.onsubmit = function(){
			for(element in this.errorStack){
				try{
					this.errorStack[element].onPreCheck = validateConfig["onPreCheck"];
					this.errorStack[element].onPreCheck();
				}catch(e){
				}
			}
			this.errorStack = new Array();
			this.stopCheck = new Array();
			this.validate = new Array();
				try{
					var removeError = new RegExp("(\s)?"+validateConfig["errorClass"],"gi");
					jQuery("//label[@class*='"+validateConfig["errorClass"]+"']").each(function(){
							this.className = this.className.replace(removeError,"");
					});					
				}catch(e){
				}
			jQuery(this.validators).each(function(){
				var valObj = this
				jQuery(el).find(valObj).each(function(){
					var added = false;
					var element = this
					jQuery(el.validate).each(function(){
						if(this.name == element.name){
							added = true;
						}
					})
					if(!added){
						el.validate.push(this);
					}

				});
			});
			jQuery(this.validate).each(function(){
				for(i=0; i < this.validate.length;i++){
					if(typeof this.validate[i] == "function"){
						this.validate[i](this);
					}
				}
			});

			if(this.errorStack.length){
				for(element in this.errorStack){
					try{
						this.errorStack[element].onError = validateConfig["onError"];
						this.errorStack[element].onError();						
					}catch(e){
					}

					try{
						this.errorStack[element].label.className += " "+validateConfig["errorClass"];
					}catch(e){
					}

				}
				try{
				    validateConfig["onPostError"]();
				}catch(e){
				}
				return false;
			}
			$$$(this).append("<input type='hidden' name='valid' id='valid' value='true' />");
			return true;
		}
	}
}
jQuery(function(){
	for(elements in validationBehaviours){
		jQuery(elements).each(function(){
			validationBehaviours[elements](this);
		})
	}
})

var validateConfig = {
	vPasswordSecurity:["[a-zA-Z0-9]{2,}"], 		//password validation REGEXP must match all
	errorClass:"error", 													//class for CSS error
	confirmPrefix:"confirm", 												//prefix for confirm validation fields
	errorContainerName:"div_error",
	errorNotificationType:"label",
	onBlur:"",                                          
	onPreCheck:function(){
		jQuery(this.obj).next().remove();
		jQuery("#div_error").hide();
	},
	onError:function(){
		var divError = document.createElement("div");
		divError.style.display = "none";
		divError.innerHTML = jQuery(this.obj).attr("title");
		jQuery(this.obj).after(divError);
		jQuery(divError).fadeIn('slow');
		this.obj.onfocus = function(){jQuery("label[@for='"+this.name+"']").each(function(){this.className = this.className.replace("error","")})};
	},
	onPostError:function(){
	    jQuery("#div_error").fadeIn('slow');
	}
}
