Validation.prototype._dataURL = "/www/scripts/form_validator.php";

function Validation(dataURL){
	if (document.getElementsByTagName){
		var forms = document.getElementsByTagName('FORM');
		var _this = this;
		try{
			for(var i in forms){
				try{
					if(dataURL !== undefined)this._dataURL = dataURL;
					if(forms[i].onsubmit !== undefined && String(forms[i].onsubmit).toLowerCase().indexOf('validate') !== -1){
						forms[i].onsubmit = function(){return _this._validateForm};
						this._containerForm = forms[i];
					}
				}catch(e){
					alert(e);
				}
			}
		}
		catch(e){
			alert(e);
		}
	}
}

Validation.prototype.load = function(){
	if(this._containerForm !== undefined){
		this._request = new Object();
		this._falseFields = new Object();
		this._inputFields = new Object();
		this._orgClassNames = new Object();
		this._submitForm = false;
		this._targetDiv = new Object();
		this._responses = new Array();
		this._count = 0;
		
		var _this = this;
		

		if (document.getElementsByTagName){
			var objInput = document.getElementsByTagName('select');
			for (var iCounter=0; iCounter<objInput.length; iCounter++){
				var parent = objInput[iCounter].parentNode;
				while(parent.nodeName !== "FORM")parent = parent.parentNode;
				if(parent.name == this._containerForm.name){
					if(objInput[iCounter].getAttribute('alt')){
						this._inputFields[objInput[iCounter].name] = objInput[iCounter];
					}
				}
			}
			var objInput = document.getElementsByTagName('input');
			for (var iCounter=0; iCounter<objInput.length; iCounter++){
				var parent = objInput[iCounter].parentNode;
				while(parent.nodeName !== "FORM")parent = parent.parentNode;
				
				if(parent.name == this._containerForm.name && objInput[iCounter].type !== "submit"){
					this._inputFields[objInput[iCounter].name] = objInput[iCounter];
					try{
						if(objInput[iCounter].getAttribute('alt') != '' && objInput[iCounter].getAttribute('alt') != undefined){
							var sRules = new String(objInput[iCounter].alt).split(/[([^\]\]*)]/);
							for(var x in sRules){
								if(sRules[x] == '') sRules.splice(x, 1);
							}
							var id = sRules[3];
							if(this._targetDiv.id === undefined && id !== undefined){
								this._targetDiv = document.getElementById(id);
								var parent = this._targetDiv.parentNode;
								while(parent.nodeName !== "DIV"){
									parent = parent.parentNode;
								}
								this._faultContainer = parent;
							}
						}
					}catch(e){this._processError(e, "add");}
				}
			}
		}
		this._containerForm.onsubmit = function(){return _this._validateForm();}
	}else{
		//alert("Er is geen formulier gevonden om te evalueren");	
	}	
}

Validation.prototype._validateField = function(inputField){
	if(inputField.getAttribute('alt')){
		try{
			if(inputField.type == "checkbox")var sVal = inputField.checked;
			else var sVal = inputField.value;
		}catch(e){
			var sVal = inputField.value;
		}
		var sRules = new String(inputField.alt).split(/[([^\]\]*)]/);
		for(var x in sRules){
			if(sRules[x] == '') sRules.splice(x, 1);
		}
		var sFieldname = sRules[0];
		var sRequired = sRules[1];
		var sTypeCheck = sRules[2];
		if(sRules[4]){
			if(this._inputFields[sRules[4]]){
				sExtra = this._inputFields[sRules[4]].value;
			}else{
				sExtra = sRules[4];
			}
		} else{
			sExtra = '';
		}
		var param = new Object;
		param = {
				sVal:sVal,
				sRequired:sRequired,
				sTypeCheck:sTypeCheck,
				sFieldname:sFieldname,
				sExtra:sExtra
		};
		this._request[inputField.name] = this._getXMLHTTPRequest();
		var _this = this;
	//	this._processError( this._generateDataUrl("inputField", param),'add');
		this._request[inputField.name].onreadystatechange = function(){_this._onData()};
		this._request[inputField.name].open("GET", this._generateDataUrl("inputField", param), true);
		this._request[inputField.name].send(null);
	}else{
		this._count--;
	}
}

Validation.prototype._validateForm = function(){
		this._count = 0;
		this._submitForm = true;
		this._falseFields = new Array();
		for(var i in this._inputFields){
			this._count++;
			this._validateField(this._inputFields[i]);
		}
	return false;
}

Validation.prototype._render = function(response){
		if(response.indexOf("noError") == -1){
			try{
				this._targetDiv.innerHTML = Trim(response);
				this._targetDiv.style.cursor = "pointer";
			}catch(e){
				this._processError(response);
				alert(e);
			}
		}else{
			try{
				this._targetDiv.innerHTML = "";
			}catch(e){
				this._processError("");
				alert(e);
			}
			
		}
}

Validation.prototype._renderForm = function(response, fieldName){
		this._count--;
		if(response.indexOf("noError") == -1 && response != ""){
			this._responses[fieldName] = response;
			this._falseFields.push(fieldName);
		}
		if(this._count === 0){
			this._responses.reverse();
			this._processFields(this._falseFields);
			this._responses = new Array();
		}
}

Validation.prototype._generateDataUrl = function(type, param){
	if(type == "inputField")
		var sUrl = new String;
		if(param !== undefined){
			for(var i in param){
				if(param[i] !== "" || i == 'sVal')sUrl+="&"+i+"="+param[i];
			}
		}
		return  this._dataURL+"?validationtype=ajax"+sUrl;
}

Validation.prototype._onData = function(){
	for(var i in this._request){
		if(this._request[i].readyState == 4){
			if(this._request[i].status == "200"){
				try{
					if(this._submitForm == true){
						this._renderForm(this._request[i].responseText, i);
					}else this._render(this._request[i].responseText);
				}catch(e){alert(e);}
			}else{	
				try{
					this._processError(this._request[i].responseText,"add");
				}catch(e){alert(e);}
			}
			delete this._request[i];
		}
	}
}

Validation.prototype._processFields = function(aFields){
	var _this = this;
	var response = '';
	var error = '';
	var temp = new Array;
	for(var i in this._inputFields){
		var test = false;
		//this._inputFields[i].style.border = "none";
		for( var x in this._falseFields){
			if(this._falseFields[x] == i)test = true;
		}
		if(test)temp.push(i);
	}
	this._falseFields = temp;
	for(var i=0;i<this._falseFields.length;i++){
		this._focusField = this._inputFields[this._falseFields[i]];
		response = this._responses[this._falseFields[i]];
		if(response.indexOf("noError") == -1 && response != ""){
			error += response;
		}
	}
	if(i == 0)try{this._containerForm.submit();}catch(e){alert(e)};
	if(this._count == 0 && i!= 0){
		this._render(error);
		this._faultContainer.onclick = function(){_this._focusOn();}
		if(this._faultContainer !== undefined) this._faultContainer.style.display = 'block';
		this._focusField = this._inputFields[this._falseFields[0]];
	}
	this._submitForm = false;
	this._falseFields.length = 0;
}

Validation.prototype._processError = function(error, type){
	var debug = document.getElementById("DEBUG");
	try{
		if(type !== undefined)debug.innerHTML += error+"<BR />";
		else debug.innerHTML = error;
	}catch(e){
		debug = document.createElement('div');
		debug.id = "DEBUG";
		debug.style.color = '#000';
		document.getElementsByTagName('body')[0].appendChild(debug);
		if(type !== undefined)debug.innerHTML += error+"<BR />";
		else debug.innerHTML = error;
	}
}

Validation.prototype._getXMLHTTPRequest = function(){
	var xmlHttp;
	try{
		xmlHttp = new ActiveXObject("Msxml2.XMLHttp");
	}catch(e){
		try{
			xmlHttp = new ActiveXObject("Microsoft.XMLHttp");
		}catch(e2){}
	}	
	if(xmlHttp == undefined && (typeof XMLHttpRequest != 'undefined')){
		xmlHttp = new XMLHttpRequest();
	}	
	return xmlHttp;
}

Validation.prototype._focusOn = function (fieldName){
	if (window.innerHeight)
		pos = window.pageYOffset
	else if (document.documentElement && document.documentElement.scrollTop)
		pos = document.documentElement.scrollTop
	else if (document.body)
		pos = document.body.scrollTop
	this._focusField.focus();
	window.scroll(0, pos - 30)
	
	if(this._faultContainer !== undefined){
		this._faultContainer.style.display = 'none';
	}
}

function Trim(STRING){
	STRING = LTrim(STRING);
	return RTrim(STRING);
}

function RTrim(STRING){
	while(STRING.charAt((STRING.length -1))==" ") STRING = STRING.substring(0,STRING.length-1);
	return STRING;
}

function LTrim(STRING){
	while(STRING.charAt(0)==" ") STRING = STRING.replace(STRING.charAt(0),"");
	return STRING;
}

window.onload = function(){
	var validation = new Validation();
	validation.load();
}
