// JavaScript Library
// Validation Class

var validation = new Class({
	
			
	//--------------------------------------------------
	// Initialize Class
	//--------------------------------------------------
	initialize: function (intControls,normalTxtCss,errorTxtCss,goodTxtCss,errorInfoCss) {
		
		//Setup array and control counter
		this.intControlCounter=-1;
		this.arrControls = new Array(intControls -1);
		
		//Load CSS Configurations
		this.normalTxtCss = normalTxtCss;
		this.errorTxtCss = errorTxtCss;
		this.goodTxtCss = goodTxtCss;
		this.errorInfoCss = errorInfoCss;
		
	},
	
	//--------------------------------------------------
	// Add Control to Validate
	//--------------------------------------------------
	addControl: function(elementID,type,value,errorMsg) {
		
		//Increment counter
		this.intControlCounter++;
		
		//Add Control to array
		this.arrControls[this.intControlCounter] = new Array(3);
		this.arrControls[this.intControlCounter][1] = elementID;
		this.arrControls[this.intControlCounter][2] = type;
		this.arrControls[this.intControlCounter][3] = value;
		this.arrControls[this.intControlCounter][4] = errorMsg;
		
			
	},
	
	//--------------------------------------------------
	// Validate Form Elements
	//--------------------------------------------------
	validate: function() {
		
		// Loop through all entries
		var i=0;
		var blnOk;
		var blnWasNull;
		for (i=0;i<=this.intControlCounter;i++) {
			
			//Set as invalid
			blnOk=false;
			blnWasNull=false;
			
			
			//Required Validation
			if (this.arrControls[i][2] == "required") {
				if (document.getElementById(this.arrControls[i][1]).value == "") {
					blnOk=false;
				} else {
					blnOk=true;
				}
			}
			
			//Numerical Validation
			if (this.arrControls[i][2] == "numerical") {
				if (isNumerical(document.getElementById(this.arrControls[i][1]).value) == true) {
					blnOk=true;
				} else {
					blnOk=false;
				}
			}
			
			//Email Validation
			if (this.arrControls[i][2] == "email") {
				if (isEmail(document.getElementById(this.arrControls[i][1]).value) == true) {
					blnOk=true;
				} else {
					blnOk=false;
				}
			}
			
			//Equals Validation
			if (this.arrControls[i][2] == "equals") {
				if (document.getElementById(this.arrControls[i][1]).value == this.arrControls[i][3]) {
					blnOk=true;
				} else {
					blnOk=false;
				}
			}
			
			//URL True Validation
			if (this.arrControls[i][2] == "url-post-true") {
				var url = this.arrControls[i][3];
				var value = document.getElementById(this.arrControls[i][1]).value;
				alert(url + "\n" + value);
				var req = new Request({ url: url, method: "post", data: { "return" : value },
	                onSuccess: function(txt) {
						if (txt == "True") {
    	                	blnOk=true;
						} else {
							blnOk=false;
						}
	                },
    	            onFailure: function() {
						alert("error");
        	            blnOk=false;
					}
	            });
    	        req.send();
			}
			
			//Display error
			if (blnOk == false) {
				
				//Textbox
				//document.getElementById(this.arrControls[i][1]).className=this.errorTxtCss;
				
				//Error Info
				var element = document.createElement("div");
				element.className = this.errorInfoCss;
				element.innerHTML = this.arrControls[i][4];
				element.id = this.arrControls[i][1] + "_errorInfo";
				if (document.getElementById(element.id) == null) {
					insertAfter(document.getElementById(this.arrControls[i][1]),element);
					blnWasNull = true;
				}
				var vSlide = new Fx.Slide(element.id);
				if (blnWasNull == true) {
					vSlide.hide();
				}
				
				setTimeout("$('" + this.arrControls[i][1] + "').morph('." + this.errorTxtCss + "')",((i + 1)*50));
				setTimeout("var vSlide = new Fx.Slide('" + element.id + "').slideIn()", ((i + 1)*100));
				
			} else {
				
				setTimeout("$('" + this.arrControls[i][1] + "').morph('." + this.normalTxtCss + "')",((i + 1)*50));
				if (document.getElementById(this.arrControls[i][1] + "_errorInfo") != null) {
					setTimeout("var vSlide = new Fx.Slide('" + this.arrControls[i][1] + "_errorInfo').slideOut()", ((i + 1)*100));
				}
				
				
			}
		}
		
	}
	
})