/*
	Function:    validate
	Called From: join.php form memberForm 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('ccOrderForm');
	var errorFound = false; // Assume no error found
	// List of all compulsory fields
	var fields = new Array('name', 'address', 'postcode', 'telephone', 'email', 'dispatchMonth');
	// 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?
			if (x != 'dispatchMonth')
				userForm[x].focus(); // Set the focus here if it's not the dispatchMonth field
			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('ccOrderForm');
		if (fieldIdfr == 19)
			fieldIdfr = 20;
		if (fieldIdfr >= 20) { // If this is a cake row in error
			var x = 'quantity[' + String(fieldIdfr - 20) + ']';
			if (userForm[x])
				userForm[x].focus(); // Set the focus
		}
		else { // The error is in the User details area
			var fields = new Array('null', 'null', 'name', 'address', 'postcode',
					'telephone', 'email', 'recipientName', 'recipientAddress', 'recipientPostcode',
					'recipientTelephone', 'recipientEmail', 'message', 'dispatchMonth', 'cakeInst',
					'comments', 'paymentMethod');
			x = fields[fieldIdfr]; // Get the field name in error
			if (x != 'null') // Set the focus if field is not null
				userForm[x].focus(); // Set the focus
		}
	}
}
