/*
** Validation function
** Author: Nick Linnell, Kyanmedia
** Date: 13/07/2005
*/

/*
** Give form a class of 'validate' and each required
** element a class of 'require'.  To validate date also add 'date' or 'email' for emails
** Add an element with an id of 'validateError' for the error message.
** Create a class of 'highlight' in css to style the required elements
*/
//var prepareValidation;

/*30/08/05 update added by Steven
** Statement added to checkValid() allowing extra sql / asp validation
*/

function Validate() {
	this._clickedItem = null;
}

Validate.prototype.init = function() {
	var vd = this;
	var theforms = document.getElementsByClassName('validate');
	for (i=0; i<theforms.length; i++) {
		theforms[i].onsubmit = function () {
			return vd.checkValid();
			//return false;
		};
		
		var dates = document.getElementsByClassName('date');
		for (x=0;x<dates.length;x++){
			dates[x].onclick = function() {
				vd._clickedItem = this;
				vd.clearDate();
				//return false;
				};
		}
	}
	document.getElementById('validateError').style.display = 'none';
}

Validate.prototype.clearDate = function() {
	if (isNaN(this._clickedItem.value)) {
		this._clickedItem.value = '';
	}
}

Validate.prototype.checkValid = function() {
	var infoerror = document.getElementById('validateError');
	var theforms = document.getElementsByClassName('validate');
	var inputs = theforms[0].elements;
	
	// runthrough elements and remove highlight
	for (i=0; i<inputs.length; i++) {
		var highlightindex = inputs[i].className.indexOf('highlight');
		if (highlightindex > -1) {
			var highlightclass = inputs[i].className;
			var newclass = highlightclass.substring(0, highlightindex);
			inputs[i].className = newclass;
		}
	}
	
	// run check
	for (x=0; x<inputs.length; x++) {
		var theclass = inputs[x].className;
		var rexp = /require/;
		var daterexp = /date/;
		var emailrexp = /email/;
		if (theclass.search(rexp) > -1) {
			if (inputs[x].value == '') {
				/*This was added by Steven 30/08/05 - make function compatible with sql / asp validation, 
				showing only one error message if element exists i.e. errored*/
				document.getElementById('error_message').style.display = 'none';
				infoerror.innerHTML = 'Please fill out all required fields';
				infoerror.style.display = '';
				inputs[x].className = theclass + ' highlight';
				inputs[x].focus();
				return false;
			} else if (theclass.search(daterexp) > -1) {
				if (inputs[x].value == 'MM' || inputs[x].value == 'DD' || inputs[x].value == 'YYYY') {
					infoerror.innerHTML = 'Please fill out all required fields';
					infoerror.style.display = '';
					inputs[x].value = '';
					inputs[x].className = theclass + ' highlight';
					inputs[x].focus();
					return false;
				}
			}
		} else if (theclass.search(emailrexp) > -1 && inputs[x].value != '') {
			var emailvalue = inputs[x].value;
			var rexpE = /^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$/;
			if (emailvalue.search(rexpE) == -1) {
				infoerror.innerHTML = 'Email address is not valid.';
				infoerror.style.display = '';
				inputs[x].className = theclass + ' highlight';
				inputs[x].focus();
				return false;
			}
		}
	}
	return true;
}

addEvent(window, 'load', function(){
	var vd = new Validate();
	vd.init();												
});
