/*
	Javascript code written by PSLWeb.co.uk for Find a Tea Room (www.findatearoom.co.uk)
	Copyright ©2007 PSLWeb.co.uk - All Rights Reserved.
*/
/*
	Function:    validate
	Called From: add_basic.php form addBasicForm 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.
*/
function validate() {
	var userForm = document.getElementById('contactForm');
	var errorFound = false; // Assume no error found
	// List of all compulsory fields
	var fields = new Array('name', 'email', 'enquiry');
	// 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 (contactForm[x].value == '') { // Is it blank?
			contactForm[x].focus(); // Set the focus here
			document.getElementById(x + 'Label').className = 'error'; // Label text shows error class
			errorFound = true;
		}
		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(fieldIdfr) {
	var contactForm = document.getElementById('contactForm');
	// Firstly, enable form elements
	contactForm.submitBtn.disabled = false;
	if (fieldIdfr > 0) { // If server-side script sets error field
		var fields = new Array('null', 'name', 'email', 'telephone', 'enquiry');
		var x = fields[fieldIdfr]; // Get the field name in error
		contactForm[x].focus(); // Set the focus
	}
}
