/*
# class webcheck
# handles form validation
*/
var webcheck = {
	
	/*
	# bool hasBeenSent
	# bool based on send progress
	*/
	hasBeenSent : false,
	
	/*
	# method bool validate()
	# organises form validateion, send and error calls
	*/
	validate : function(){
		
		/*
		# no multi submissions
		*/
		if(this.hasBeenSent==true)
		return this.badForm(null,'The form has been submitted, please be patient.');
		
		/*
		# collect all required form elements
		*/
		var req = new Array()
		req[0] = $('name');
		req[1] = $('email');
		req[2] = $('scode');
		req[3] = $('how-you-heard');
		
		/*
		# create the errors array
		*/
		var errors = new Array()
		errors[0] = 'Please enter your name';
		errors[1] = 'Please enter a valid email address';
		errors[2] = 'Please enter the security code as shown';
		errors[3] = 'Please let us know how you heard about us to continue';
		
		/*
		# check that the name is not empty
		*/
		if(req[0].value.length<1)
		return this.badForm(req[0],errors[0]);
		
		/*
		# validate select box seperately
		*/
		var selectedIndex = req[3].selectedIndex;
		if(req[3][selectedIndex].value.length<1){
			return this.badForm(req[3],errors[3]);
		}
		
		/*
		# validate the email address seperately
		*/
		if(!this.isValidEmail(req[1].value))
		return this.badForm(req[1],errors[1]);
		
		/*
		# check that the name is not empty
		*/
		if(req[2].value.length<1 || req[2].value=='Enter the code above')
		return this.badForm(req[2],errors[2]);
		
		/*
		# return false to stop the page reload 
		# as we are using an ajax call
		*/
		this.hasBeenSent = true ;
		return true;
	},
	
	/*
	# method bool isValidEmail(string strAddress)
	# validates string email address
	*/
	isValidEmail : function(strAddress){
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		return(reg.test(strAddress));
	},
	
	/*
	# method bool isValidUrl(string strAddress)
	# validates string web address
	*/
	isValidUrl : function(strAddress){
		var reg = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
		return(reg.test(strAddress));
	},
	
	/*
	# method bool badForm()
	# 
	*/
	badForm : function(objElement,strError){
		/*
		# put the custom error string into the dom
		*/
		$('formResponse').innerHTML = '<label><em>'+strError+'</em></label>';
		return false;
	}
}