/*
	Javascrip code written by PSLWeb.co.uk for the Small Pilgrim Places Network (www.smallpilgrimplaces.org)
	Copyright ©2008 PSLWeb.co.uk - All Rights Rerserved.
*/
// Globals
var skipValidation = false; // Assume we'll validate form on submit

/*
	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) {
	if (fieldIdfr > 0) { // If server-side script sets error field
		var commentForm = document.getElementById('commentForm');
		var fields = new Array(null, 'name', 'email', 'comment');
		x = fields[fieldIdfr]; // Get the field name in error
		if (x) // If x is not null
			commentForm[x].focus(); // Set the focus
	}
}

/*
	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() {
	//return true; // *** TESTING server-side validation only
	if (skipValidation)
		return confirm("Please confirm CANCEL (form data will be lost)");
	var commentForm = document.getElementById('commentForm');
	var errorFound = false; // Assume no error found
	// List of all compulsory fields
	var fields = new Array('name', 'email', 'comment');
	// 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 (commentForm[x].value == '') { // Is it blank?
			commentForm[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;
}
