/**
 * @author patrickc
 */
	if (!window.validation){
		var validation = new Object;
	}
	
/**
 * Hashtable, contains all validation rules, validation[form name][element name]
 * [element name] array syntax 
 * ['unique string id' array( boolean[regex:function] array(return value|[regex:function]) 'string error message' )
 * remember to double back slash in js string regexp eg.. "\\w"  NOT  "\w"  
 *   
 * @id validation
 * @alias validation.validation
 */

/** @id validation */	
	validation.validation = new Object;
	validation.validation["aspnetForm"] = { 
		"ctl00_phPageBody_phPageContent_phContent_xForm_Title" : [
		  new Array(true,[1,1],"Please select a salutation")
		],
		"ctl00_phPageBody_phPageContent_phContent_xForm_FirstName" : [
			new Array(true,[true,"\\w+"],"First name should not be empty")
		],
		"ctl00_phPageBody_phPageContent_phContent_xForm_LastName" : [
			new Array(true,[true,"\\w+"],"Last name should not be empty")
		],		
		"ctl00_phPageBody_phPageContent_phContent_xForm_Email" : [
			new Array(true,[true,"\\w+"],"Email should not be empty"),
			new Array(true,[true,"^([a-zA-Z0-9_\\.\\-])+\\@(([a-zA-Z0-9\\-])+\\.)+([a-zA-Z0-9]{2,9})+$"],"Sorry, your email is invalid, please recheck")
		],		
		"ctl00_phPageBody_phPageContent_phContent_xForm_Telephone" : [
			new Array(true,[true,"\\w+"],"Telephone number should not be empty")
		],		
		"ctl00_phPageBody_phPageContent_phContent_xForm_OrganizationName" : [
			new Array(true,[true,"\\w+"],"Organisation should not be empty")
		],		
		"ctl00_phPageBody_phPageContent_phContent_xForm_WorkAddress" : [
			new Array(true,[true,"\\w+"],"Work address should not be empty")
		],		
		"ctl00_phPageBody_phPageContent_phContent_xForm_YourRole" : [
			new Array(true,[true,"\\w+"],"Role should not be empty")
		],		
		"ctl00_phPageBody_phPageContent_phContent_xForm_Postcode" : [
			new Array(true,[true,"\\w+"],"Postcode should not be empty")
		],		
		"ctl00_phPageBody_phPageContent_phContent_xForm_Country" : [
		  new Array(true,[1,1],"Please select a country")
		],
		"container_starter" : [
			new Array(true,[1,"validation.formCheckRadio"],"Please choose a starter")
		],
		"container_main" : [
			new Array(true,[1,"validation.formCheckRadio"],"Please choose a main course")
		],
		"container_dessert" : [
			new Array(true,[1,"validation.formCheckRadio"],"Please choose a dessert")
		] 		
		
		
		/*
		
		 "container_ctl00$phPageBody$phPageContent$phContent$xForm$main" : [
			new Array(true,[1,"validation.formCheckRadio"],"Please choose a main course")
		]
		*/
		
		/* ,
		"submit" : [
			new Array(false,[1,"isValid"],"Sorry, the form contains some errors and can't be submitted")
		] */
	}
	
		
	
	/** 
	* @id customValidationFunction 
	* @param (string) id
	* @return boolean
	*/
	
	/** @id customValidationFunction */
	function customValidationFunction(id){
		return true;
	}
	/** @id formCheckRadio */
	validation.formCheckRadio = function(el,returnVal){ // check radio and checkbox elements in a container
		var ok = false;
		var radiobuttons = el.getElementsByTagName('input');
		for (var i=0;i<radiobuttons.length;i++){
			if ( (radiobuttons[i].type == "radio" || radiobuttons[i].type == "checkbox" ) && radiobuttons[i].checked){
				ok = true;
				break;
			}
		}
		return ok;
	}
	/** @id formCheckPasswordsInit */	
	validation.formCheckPasswordsInit = function(el,returnVal){
		var ok = true;
		try {
			document.getElementById('divPasswords').checkme();
		} catch(e){ok = true;}
		return ok;
	}
	
	/** @id formCheckPasswords */	
	validation.formCheckPasswords = function(el,returnVal){
		var ok = true;
		if (document.getElementById('password').value !='' && document.getElementById('passwordConfirm').value != ''){
			ok = (validation.formCheckConfirm('password') == returnVal) ;
			if (window.logger){logger.log('checking passwords both have value, matching = '+ ok)};			
		}
		return ok;
	}
	
	/** @id formCheckConfirm */	
	validation.formCheckConfirm = function(el){
		var el_1,el_2,confirmClassname = "confirm";
		el = validation.formValidation.get(el);
		var id = el.id.replace(confirmClassname,'');
		try {
			el_1 = document.getElementById(id);
			el_2 = document.getElementById(id+confirmClassname);
		}catch(e){
			if (window.logger){logger.error(e,'checking confirm field faild')};
			return true;
		}
		if ( el_1.value != el_2.value ){
			return false;
		}
		return true;
	}		
	
	
	
