/*
	Validation script
	
	requires: 		
	can use:		messageDlg
					"alert" class in style sheet
					
	what is does: 	uses special attributes in an input tag
					to validate on keypress, on blur, and on submit
					
	notes:			no real need for object orientated classes here
					as this is already provided by the DOM
	
*/


//the initiation bit - iterates through INPUT tags adding 
//relevant events where 'validate' attribute exists
function validateInit() {
	//find all validation tags
	addEventsToNodeList(document.getElementsByTagName("INPUT"));
	addEventsToNodeList(document.getElementsByTagName("TEXTAREA"));
			
	//if messageDlg not included
	if(typeof messageDlg == "undefined") {
		messageDlg = function(message) {
			window.status = message;
		}
		clearMessageDlg = function() {
			window.status = "";
		}
	}
}

function addEventsToNodeList(tags) {
	tagCount = tags.length;
	for (var i=0; i<tagCount; i++)
		if((tags.item(i).getAttribute("validate") == "true") ||
			(tags.item(i).getAttribute("req") == "true")){
			//we first need to get the parent form as
			//to disallow summiting if validation fails
			parentForm = tags.item(i);
			while(parentForm = parentForm.parentNode)
				if(parentForm.tagName.toUpperCase() == "FORM")
					break;
			
			//attach various properties to our form
			parentForm.validationErrors = 0;
			parentForm.lastError = "";
			tags.item(i).parentForm = parentForm;
			if(!parentForm.getAttribute("oldsubmit")) {
				parentForm.oldonsubmit = (parentForm.onsubmit) ? parentForm.onsubmit : function () {};
				parentForm.setAttribute("oldsubmit","true");
			}
			parentForm.onsubmit = null;
			parentForm.onsubmit = validateForm;
			
			
			//keypress validation
			if(typeof tags.item(i).getAttribute("keyreg") == "string") {
				//add events to capture key presses
				tags.item(i).onkeypress = validateKeyStroke;
			} 
			else tags.item(i).onkeypress = _justClear; 
			
			//onblur validation
			if(typeof tags.item(i).getAttribute("blurreg") == "string") {
				tags.item(i).onblur = validateField;	
			}
			else tags.item(i).onblur = _justClear;
			
			//required fields validation
			if(tags.item(i).getAttribute("req") == "true") {
				if(tags.item(i).value.length == 0)
					tags.item(i).className += " req";
				if(typeof tags.item(i).getAttribute("blurreg") != "string")
					tags.item(i).onblur = checkRequiredAndClear;
					
			}
			
			//make sure there's an id to refer to
			//we dont have to use getAttribute here as a property
			//is created for real HTML attributes
			if(!tags.item(i).id)
				tags.item(i).id = tags.item(i).name;
			
		}
}

function validateKeyStroke(inObj) {
	if(document.all) key=event.keyCode;
	else key=inObj.which;

	var regExp = new RegExp(this.getAttribute("keyreg"));
	if(key < 33 || regExp.test(String.fromCharCode(key))) {
		_resetVisuals(this);
		return true;
	}
	else {
		message = "You can only enter<BR>";
		message += "<B>" + this.getAttribute("keyErrMsg") + "</B><BR>";
		message += "in this field!";
		messageDlg(message,"right",this.id,"tooltip");
		//if(!/ alert/.test(this.className))
			//this.className += " alert";
		return false;
	}
}

function validateField(inObj) {
	//check empty + required indicator
	checkRequired(this);

	//validate field
	regExp = new RegExp(this.getAttribute("blurreg"));
	if(regExp.test(this.value)) {
		this.parentForm.validationErrors -= 1;	
		return true;
	}
	else {
		message = "This field has to be in the form<BR>"
		message += "<B>" + this.getAttribute("blurErrMsg") + "</B><BR>";
		messageDlg(message,"right",this.id,"tooltip");
		if(!/ alert/.test(this.className))
			this.className += " alert";
		if(this.parentForm.validationErrors == 0 || (this.parentForm.validationErrors > 0 && this.parentForm.lastError != this.name))
			this.parentForm.validationErrors += 1;
		this.parentForm.lastError = this.name;

		return false;
	}
	
}

function checkRequiredAndClear() {
	checkRequired(this);
	_resetVisuals(this);
}

function checkRequired(object) {
	//remove/add required className if field empty + required
	if(object.getAttribute("req") == "true") {
		if (object.value.length == 0) {
			if(!/ req/.test(object.className)) {
				object.className += " req";
			}
		}
		else {
			object.className = object.className.replace(" req","");
		}
	}
}

function validateForm(inObj) {
	//validate form - only look at one problem per submital
	submitMe = true;
	message = "";
	
	//really would be better to rescan all fields?
	if (this.validationErrors > 0) {
		messageDlg("There be errors!\nMake sure you fill in all the fields correctly.","centre",screen,null);
		submitMe = false;
	}
	else {
		//go through each input field in the submitting form
		//and check required field
		tags = this.getElementsByTagName("INPUT");
		tagCount = tags.length;
		for(var i=0; i<tagCount; i++)
			if(tags.item(i).getAttribute("req") =="true" && tags.item(i).value.length == 0) {
				submitMe = false;
				message = "You've accidently forgotten to fill some things in!";
				if(!/ alert/.test(tags.item(i).className))
					tags.item(i).className += " alert";
			}
	
		if(message) messageDlg(message,"centre",screen,null);
	}
	
	//run previous submit code
	oldsubmitMe = this.oldonsubmit();
	if(typeof(oldsubmitMe == "boolean") && oldsubmitMe == false)
		oldsubmitMe = false;
	else
		oldsubmitMe = true;
	
	return (oldsubmitMe && submitMe);
}


function _resetVisuals(object) {
	clearMessageDlg();
	object.className = object.className.replace(" alert","");
}

function _justClear() {
	_resetVisuals(this);
}
	
	
//add onload event so we can use this script as an include easily
//this doesn't work in mozilla if there is already an onLoad function
if (window.attachEvent)	{// IE5
	window.attachEvent("onload", validateInit);
}
else {// IE4 / other
	window.onload = validateInit;
}