/*
	Javascript code written by PSLWeb.co.uk for Find a Tea Room (www.findatearoom.co.uk)
	Copyright ©2007 PSLWeb.co.uk - All Rights Reserved.
*/

var skipValidation = false; // Assume we'll validate form on submit

/*
	Function:    validate
	Called From: add_basic.php form basicForm 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() {
	if (skipValidation)
		return confirm("Please confirm CANCEL (form data changes will not be saved)");
	var userForm = document.getElementById('basicForm');
	var errorFound = false; // Assume no error found
	// List of all compulsory fields
	var fields = new Array('name', 'addr1', 'city', 'county', 'park', 'contactName');
	if (userForm.isAdd.value) { // If adding, then email address is required
		fields[6] = 'email';
		fields[7] = 'email2';
	}
	// 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 (userForm[x].value == '') { // Is it blank?
			userForm[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) {
	if (fieldIdfr > 0) { // If server-side script sets error field
		var userForm = document.getElementById('basicForm');
		var fields = new Array('null', 'name', 'addr1', 'addr2', 'addr3', 'city',
				'county', 'park', 'postcode', 'teleT', 'contactName', 'email',
				'email2', 'teleC', 'terms');
		x = fields[fieldIdfr]; // Get the field name in error
		userForm[x].focus(); // Set the focus
	}
}

/*
	Function:    confirmReset
	Inputs:      None.
	Called From: Reset Button
	Description: asks for User Reset Confirmation. Re-initialises FILE input fields.
*/
function confirmReset() {
	if (confirm("Please confirm RESET of all form field data to their starting values")) {
		var userForm = document.getElementById('basicForm');
		userForm.reset();
	}
}

/*
	Function:    showTerms
	Inputs:      None.
	Called From: add_basic.php
	Description: Shows the Terms and Conditions Textarea.
*/
function showTerms() {
	document.getElementById('termsTextLabel').style.display = "inline";
	document.getElementById('termsText').style.display = "inline";
}
