/**
* Javascript Ajax Form Validation Script
* 
* @author Mike Howarth
* @date 16/06/06
* 
*/


//attach form handlers on loading the window
window.onload = attachFormHandlers;

function attachFormHandlers() { 

	//check we've got a form to attach to 
	if($("form1")) { 
	
		//get an array of all the inputs from the form
		var elementList = new Form.getElements($("form1"));   
		var nodes = $A(elementList);
		
		//loop through and attach an onblur event and call validateMe when changed
		nodes.each(function(node){
							
				node.onblur = function(){return validateMe(this);}
				node.onkeyup = function(){ return validateMe(this);} 
				
				if(node.type == "checkbox") {
					node.onchange = function(){return validateMe(this);}
				}
							
					
			});
		
		$("form1").onsubmit = function(){ return validateForm(); } //attach validate() to the form

	}

}

//validateMe is called each time a user leaves an input field
function validateMe(objInput) { 

	var sVal = objInput.value; //input fields value
	var sRules = $A(objInput.className.split(' ')); // split the string and get the validation rules
	
	var sRequired = sRules.first(); //determine whether the field is required
	var sTypeCheck = sRules.without(sRules.first(), sRules.last()); //determine the types of validation we are doing
	var gShow = sRules.last(); //which id we are displaying a message in

	//Set up a basic object with the types of validation
	var validate = {
			value: sVal,
			required: sRequired,
			types: sTypeCheck
			};
			
	//Convert the object to a hash for easy url manipulation
	var h = $H(validate);
	
	var url = window.location.protocol + "//" + window.location.hostname + '/scripts/validate.php';
	//var url = 'http://www.everybodyhurts.co.uk/url_test/scripts/validate.php';
	
	var pars = h.toQueryString();
	
	var myAjax = new Ajax.Request(
		url, 
		{
			method: 'get', 
			parameters: pars, 
			onComplete: showResponse 
		});
	
	function showResponse(request) { 
		$(gShow).innerHTML = request.responseText;
	}
		
}


//Validate the entire form
function validateForm() { 

var spans; 
var i = 0;
var gErrors = 0;

//return all the spans within the document
spans = document.getElementsByTagName('span')

var nodes = $A(spans);

	//loop through and attach an onblur event and call validateMe when changed
	nodes.each(function(node){
			
		//check whether we have a rule element
		if(node.className == "rules") 
		{ 
		
			//if we have a thank you or it is blank move on
			if(node.innerHTML == 'Thank You' || node.innerHTML == '') 
			{ 
				node.style.color = '#000000';//the color is changed to black or stays black
			}
			else 
			{
				gErrors = gErrors + 1; //the error count increases by 1
				node.style.color = '#ff0000';//error messages are changed to red
			}
		
		}
		
	});

	if (gErrors > 0)
	{
		//if there are any errors give a message
		alert ("Please make sure all fields are properly completed.  Errors are marked in red!");
		gErrors = 0;// reset errors to 0
		return false;
	}
	
	return true;
		
}



