/*
	Gebruikt Classnamen: FormValidator_TextHolder

*/
var FormValidator = Class.create({
	initialize: function(inForm_id) {
		this.ImageLocation = "/images/formvalidator/"; // Verander het naar de correct path als het nodig is.
	
		this.form_id = inForm_id;
		this.formOBj = $(inForm_id);
		this.fieldList = new Array();			
		Event.observe(this.formOBj, "submit", this.validateFields.bind(this));		
		
		
	},
	validateFields: function(event) {
		var Formcorrect = true;
		for (var a = 0; a < this.fieldList.length; a++) {
			this.fieldList[a].validateField(null, false);
			if (this.fieldList[a].currentValidateStatus == false)  {			
				Formcorrect = false;				
			}		
		}	
		if (Formcorrect == false) {					
			Event.stop(event);			
		}
		return Formcorrect;	
	},
	addCheck:function(inFieldName, inFriendlyname, inValidateon) {
		Fields = this.formOBj[inFieldName];
		var validateField = new FormValidator_field(Fields, inFieldName, inFriendlyname, inValidateon, this);
		this.fieldList.push(validateField);
	
	
	}	
});

var FormValidator_field = Class.create({
	initialize: function(inFieldObj, inFieldName, inFriendlyName, inValidateon, inFormValidator) {	
		this.fieldObj 		= inFieldObj;
		this.fieldname		= inFieldName;
		this.friendlyName 	= inFriendlyName;
		this.validateOn 	= inValidateon;		
		this.formValidator 	= inFormValidator;	
		this.currentValidateStatus = false;
		
		this.ValidatorTextHolder = new Element("div", { "id" : "validator_" + inFormValidator.form_id + "_" + this.fieldname });
		this.ValidatorTextHolder.addClassName("FormValidator_TextHolder");
		
		this.ValidatorTextHolder.update();
		
		var fieldType = "";
		
		if (this.fieldObj.constructor) {
			fieldType = this.fieldObj.constructor.toString();		
		} else { // IE 7 fallback
			fieldType = "HTMLInputElement";
	
			if (this.fieldObj.length) {
				fieldType = "Array";					
			} else {
				fieldType = "HTMLInputElement";
			}
			
		}
				
		if ((fieldType.indexOf("HTMLInputElement") != -1) || (fieldType.indexOf("HTMLTextAreaElement") != -1)) {
			//this.fieldObj = Element.extend(this.fieldObj);
			Element.extend(this.fieldObj);
			this.fieldObj.insert({ before: this.ValidatorTextHolder });
			this.fieldObj.observe("blur", this.validateField.bind(this));
		} else {			
			var fieldsLength = this.fieldObj.length
		
			//this.fieldObj[fieldsLength - 1] = Element.extend(this.fieldObj[fieldsLength - 1]);			
			Element.extend(this.fieldObj[fieldsLength - 1]);
			this.fieldObj[fieldsLength - 1].insert({ after: this.ValidatorTextHolder });
			for (var a = 0; a < fieldsLength; a++) {
				Element.extend(this.fieldObj[a]);
				this.fieldObj[a].observe("blur", this.validateField.bind(this));
			}
		}		
		// this.setValidateStatus(true);
	},
	validateField: function(event, showalert) {
		
		this.currentValidateStatus = false;
		
		var validate = this.validateOn.split('|');
		var formValue = '';		
		
		// if (this.fieldObj.constructor.toString().indexOf("Object") == -1) {
		
		if (this.fieldObj.constructor) {
			fieldType = this.fieldObj.constructor.toString();		
		} else { // IE 7 fallback
/* 			if (typeof(this.fieldObj) == "Array") {
				fieldType = "Array";			
			} else {
				fieldType = "HTMLInputElement";						
			} */
			if (this.fieldObj.length) {
				fieldType = "Array";					
			} else {
				fieldType = "HTMLInputElement";
			}			
		}
		
		if ((fieldType.indexOf("HTMLInputElement") != -1) || (fieldType.indexOf("HTMLTextAreaElement") != -1)) {
			formValue = Form.Element.getValue(this.fieldObj);		
			formObj = this.fieldObj
		} else {						
			var fieldsLength = this.fieldObj.length
			for (var a = 0; a < fieldsLength; a++) {
				if (this.fieldObj[a].checked) {
					formValue = this.fieldObj[a].value;
					formObj = this.fieldObj[a]
					break;
				}			
			}		
		}
		
		for (var a = 0; a < validate.length; a++) {
			var validatestatus = true;
			switch (validate[a]) {
				case "required":
					validatestatus = this.isEmptyField(formValue);
					break;
				case "email":
					validatestatus = this.isEmailAddress(formValue);				
					if ((validatestatus == false) && (showalert != false)) {
						alert(this.friendlyName);
						// formObj.focus();
					}
					break;
				case "onlynumbers":
					validatestatus = this.hasOnlyNumbers(formValue);				
					if ((validatestatus == false) && (showalert != false)) {
						alert(this.friendlyName);
						// formObj.focus();
					}
					break;	
				case "phonenumber":
					validatestatus = this.isPhoneNumber(formValue);				
					if ((validatestatus == false) && (showalert != false)) {
						alert(this.friendlyName);
						// formObj.focus();
					}	
					break;
				case "nlpostcode":
					validatestatus = this.nlPostcodeCheck(formValue);				
					if ((validatestatus == false) && (showalert != false)) {
						alert(this.friendlyName);
						// formObj.focus();
					}
					break;					
			}
			if (validatestatus == false) {				
				break;
			}
		}
		this.currentValidateStatus = validatestatus;				
		this.setValidateStatus(validatestatus, this.friendlyName);	
	},
	setValidateStatus: function (status, errorstatus) {		
		if (status == true) {
			this.ValidatorTextHolder.update(new Element("img", { "src" : this.formValidator.ImageLocation + "validate_correct.gif", "alt" : "OK", "title" : "" }));
		} else {
			var infodiv =  new Element("span");
				infodiv.appendChild(new Element("img", { "src" : this.formValidator.ImageLocation + "validate_fail.gif", "alt" : "Not OK", "title" : errorstatus }))
				// infodiv.appendChild(new Element("span", { "class": "formvalidator_error_text" }).update("Geen geldige invoer"));
			this.ValidatorTextHolder.update(infodiv);
		}	
	},
	cleanValidateStatus: function() {
		this.ValidatorTextHolder.update();	
	},
	

	// Controlleer formulieren
	isEmptyField: function(inValue) {
		if (inValue == '') {
			return false;			
		} else {
			return true;
		}
	},
	
	isEmailAddress: function (inValue) {		
		var p1=0;
		var p2=0;
		var startPos = 0;
		var email = inValue;
		
		while (startPos != email.length){
			if ((email.charAt(startPos)=='@')&&(p1==0)) p1=startPos;
			if ((email.charAt(startPos)=='.')&&(p2==0)) p2=startPos;
			
			startPos++;
		}
		
		if ((p1==0)||(p2==0)){
			return false
		} else {
			return true;
		}	
	},
	
	hasOnlyNumbers: function (inValue) {
		return  /^\d+$/.test(inValue);
	},
	
	isPhoneNumber: function (inValue) {
		var re3 = new RegExp("^[\\d\\s\\-]+$");
	
		 if (inValue.match(re3)) {
			return true;
		 } else {
			return false;
		 }
			
	
	},	
	nlPostcodeCheck: function (inValue) {
		 var re = new RegExp("[0-9][0-9][0-9][0-9][A-Za-z][A-Za-z]");
		 var re2 = new RegExp("[0-9][0-9][0-9][0-9] [A-Za-z][A-Za-z]");		 
		 
		 if ((inValue.match(re)) || (inValue.match(re2))) {
			return true;
		 } else {
			return false;
		 }
	}
	
});

