/*
	Javascript code written by PSLWeb.co.uk for David Kinsey (www.davidkinsey.co.uk)
	Copyright ©2008 PSLWeb.co.uk - All Rights Reserved.
*/
// Attach Window event onLoad
if (window.addEventListener)
	window.addEventListener("load", setFocus, false);
else
	if (window.attachEvent)
		window.attachEvent("onload", setFocus);
	else
		window.onload = setFocus;
/*
	Function:    validate
	Called From: pages/contact.php form helpForm Submit button (onsubmit method)
	Description: Checks all required form fields to see if any are blank. If so then it highlights
					the Label text and sets the focus to that field (bottom up). Form submission is
					aborted if any fields are in error.
	Inputs:      None.
	Outputs:     Side-effects: Pop-up window and document elements class amendment.
*/
function validate() {
	var userForm = document.getElementById('helpForm');
	var errorFound = false; // Assume no error found
	// List of all compulsory fields
	var fields = new Array('name', 'email', 'problem');
	// Count backwards so we highlight first field in error on the form
	for (i = fields.length - 1; i >= 0; i--) {
		x = fields[i]; // Get this field name
		if (helpForm[x].value == '') { // Is it blank?
			helpForm[x].focus(); // Set the focus here
			document.getElementById(x + 'Label').className = 'error'; // Label text shows error class
			errorFound = true; // Set flag
		}
		else
			document.getElementById(x + 'Label').className = ''; // If no error clear error class
	}
	if (errorFound) { // Pop-up error window
		alert("Please complete required form fields");
		return false;
	}
	else
		return true;
}

/*
	Function:    setFocus
	Inputs:      fieldIdfr: integer denoting the field in error (top down)
	Called From: Body element (onload method) - only if server-side form validation error detected
	Description: Sets the focus to the first field that server-side code has detected as being in
					error.
*/
function setFocus() {
	if (document.getElementById('helpForm')) { // Does this form exist on this particular page rendering?
		var bookletForm = document.getElementById('helpForm');
		var fieldFocus = bookletForm.fieldFocus.value;
		if (fieldFocus != "") { // Has a field IDFR been specified?
			var fieldIdfr = parseInt(fieldFocus);
			var fields = new Array('name', 'name', 'email', 'problem');
			var x = fields[fieldIdfr]; // Get the field name
			bookletForm[x].focus(); // Set the focus
		}
	}
}