//This is the main function called when validating forms
//It can be pasted into the page itself as it is the only function
//that changes.
function validateForm(theForm) {
	
	if (!validRequired(theForm.message, "Answer")) {
	
		return false;
	
	}	
	
	if (!validRequired(theForm.name, "Name")) {
	
		return false;
	
	}		
	
	if (!validEmail(theForm.email)) {
	
		return false;
	
	}						
	
	return true;

}

//This function brings up an alert if field is empty
//The fieldLabel variable can be used to insert the field name in the alert
function validRequired(formField, fieldLabel) {

	var result = true;
	if (formField.value == "") {
	
		alert('Please enter a value for the "' + fieldLabel + '" field.');
		formField.focus();
		result = false;
		
	}
	return result;	

}

//This function validates emails simply
//It checks that the field is filled in
//It also checks to see if the @ and . are present
function validEmail(formField) {

	var result = true;
	var string1 = formField.value;
	if (formField.value == "") {
	
		alert("Please enter a value for the email field.");
		formField.focus();
		result = false;
		
	} else if (string1.indexOf("@")==-1 || string1.indexOf(".")==-1) {
	
		alert("You have not entered a correct email address.");
		formField.focus();
		result = false;
			
	}
	
	return result;

}

//This function makes sure the checkbox required is checked
function validCheckbox(formField) {

	var result = true;
	if (!formField.checked) {
	
		result = false;
		
	}
	return result;

}