/*
** 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 or 'radio' for radios or 'checkbox' for checkbox
** 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;

function Validate() {
	this._clickedItem = null;
}

Validate.prototype.init = function() {
	var vd = this;
	var theforms = document.getElementsByClassName('validate');
	if (document.getElementById('validateError')) {
		if (document.getElementById('validateError').title == "") {
			document.getElementById('validateError').style.display = 'none';
		}
	}
	for (i=0; i<theforms.length; i++) {
		theforms[i].onsubmit = function () {
			return vd.checkValid();
		};
		var dates = document.getElementsByClassName('date');
		for (x=0;x<dates.length;x++){
			dates[x].onclick = function() {
				vd._clickedItem = this;
				vd.clearDate();
			};
		}
	}
}

Validate.prototype.clearDate = function() {
	if (isNaN(this._clickedItem.value)) {
		this._clickedItem.value = '';
	}
}

Validate.prototype.checkValid = function() {
	var infoerror;
	var theforms = document.getElementsByClassName('validate');
	var inputs = theforms[0].elements;
	var errorInfoId;
	var errorLocation;
	
	// 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/;
		var radiorexp = /radio/;
		var checkboxrexp = /checkbox/;
		var theErrors = document.getElementsByClassName('error');
		for (i = 0; i < theErrors.length; i ++) {
			//theErrors[i].style.display = '';
		}
		if (theclass.search(emailrexp) > -1) {
			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) {
				return handleError(inputs[x].id, 'Email address is not valid.');
			}
		} else if (theclass.search(daterexp) > -1) {
			if (inputs[x].value == 'MM' || inputs[x].value == 'DD' || inputs[x].value == 'YYYY') {
				return handleError(inputs[x].id, 'Date is not valid.');
			}
		} else if (theclass.search(rexp) > -1) {
			if (inputs[x].value == '') {
				return handleError(inputs[x].id);
			} else if (theclass.search(radiorexp) > -1) {
				var radioCheck = false;
				var radioInputs = document.getElementsByClassName("radio");
				for (i = 0; i < radioInputs.length; i ++) {
					if (radioInputs[i].name == inputs[x].name && radioInputs[i].checked) {
						radioCheck = true;
					}
				}
				if (!radioCheck) {
					return handleError(inputs[x].id);
				}
			} else if (theclass.search(checkboxrexp) > -1) {
				if (!inputs[x].checked) {
					return handleError(inputs[x].id);
				}	
			}
		}
	}
	return true;
}

addEvent(window, 'load', function(){
	var vd = new Validate();
	vd.init();
});


function handleError(theInput, theMessage) {
	if (theMessage == undefined) {
		theMessage = 'Please fill out all required fields';
	}
	theInput = document.getElementById(theInput);
	document.getElementById('validateError').style.display = '';
	document.getElementById('validateError').innerHTML = theMessage;
	theclass = theInput.className;
	theInput.className = theclass + ' highlight';
	/*errorLocation = window.location.href;
	errorLocation = errorLocation.substring(0, errorLocation.lastIndexOf("#"));
	errorLocation = errorLocation + "#" + errorInfoId;
	window.location.href = errorLocation;*/
	theInput.focus();
	return false;
}