function validate_email(field) {
	with (field) {
		apos=value.indexOf("@");
		dotpos=value.lastIndexOf(".");
		if (apos<1||dotpos-apos<2) {
			return false;
		} else {
			return true;
		}
	}
}

function markMissing(theFieldId, color) {
	var theFieldLabel = document.getElementById(theFieldId + '_label');
	theFieldLabel.style.color = color;	
}

function unMarkMissing(theFieldId) {
	var theFieldLabel = document.getElementById(theFieldId + '_label');
	theFieldLabel.style.color = '';	
}

function validateEnquiry() {
	var checkAndFields = new Array('Firstname', 'Surname',
		'Pickup_Address','Time_of_Pickup', 'Dropoff_Address', 'Event_Date', 'Number_of_People',
		'Occasion', 'Reply_Email');

	var checkOrFields = new Array('Home_Phone', 'Work_Phone', 'Mobile_Phone');
		
	var emailFieldId = 'Reply_Email';	
		
	var baseDOM = 'document.enquiryform';
	
	/* Status flag variables. */
	var missingAnd = 0;
	var missingOr = 1;
	
	/* Default return is true if nothing changes it */
	var returnStatus = true;

	/* Array incrementor variable. */
	var x;

	/* Base alert message. */	
	var alertMsg = baseAlertMsg = "Sorry, you have missed some fields that affect our ability "
		+ 	"to give a quote.";
	
	/* Check the fields that must all be set, (un)marking as we go. */
	for(x in checkAndFields) {
		var theField = eval(baseDOM + '.' + checkAndFields[x]);
		if(theField.value == '' ) {
			markMissing(checkAndFields[x], 'red');
			missingAnd = 1;
		} else {
			unMarkMissing(checkAndFields[x]);	
		}
	}
	
	/* Now check the fields where only 1 must be set (OR'd). */
	for(x in checkOrFields) {
		var theField = eval(baseDOM + '.' + checkOrFields[x]);

		if(theField.value != '' ) {
			missingOr = 0;
		}	
	}	
	
	if(missingAnd) {
		alertMsg += "\n - Please fill in ALL fields marked in red.";	
	}

	
	if(missingOr) {
	/* Go through and highlight all the OR fields in blue. */
		for(x in checkOrFields) {
			var theField = eval(baseDOM + '.' + checkOrFields[x]);
			markMissing(checkOrFields[x], 'blue');
		}	

		alertMsg += "\n - Please fill in at least one of the fields marked in blue.";	
	} else {
		for(x in checkOrFields) {
			unMarkMissing(checkOrFields[x]);			
		}
	}
	
	if( !validate_email(document.getElementById(emailFieldId)) ) {
		alertMsg += "\n - Ensure your email address is correct.";
	}
	
	if( alertMsg != baseAlertMsg) {
		alert(alertMsg);
		returnStatus = false;
	}
	
	return returnStatus;
	
}

