// CONTACT PAGE - FORM VALIDATION


function fnValidateContactForm() {

	// Declare messages to output to user
	var InvalidHeading = "Your message has not been sent.";
	var InvalidMessage = "Please re-enter valid details for highlighted areas.";

	var NameInput  = document.getElementById("idNameInput");
	var EmailInput = document.getElementById("idEmailInput");
	var PhoneInput = document.getElementById("idPhoneInput");
	var Comments   = document.getElementById("idCommentsInput");

	var bValid = true;
	var oRegExp = new RegExp();

	//NAME - Ensure name input has characters entered

	if(NameInput.value.length==0)
		{
		//Not Valid
		NameInput.className = "cInvalid";
		bValid = false;
		}
	else
		{
		//Check for only characters and spaces
		var oRegExp = /^[A-Za-z\s]+$/;

			if(NameInput.value.match(oRegExp) == null)
				{
				//Not Valid
				NameInput.className = "cInvalid";
				bValid = false;
				//NameInput.value = "";  // This would reset the entry to a blank field
				}
		else
			{
			NameInput.className = "cValid";
			}
		}

	//EMAIL - Ensure either email address or phone input has characters entered
	if ( (EmailInput.value.length==0) && (PhoneInput.value.length==0) )
		{
		//Not Valid
		EmailInput.className = "cInvalid";
		PhoneInput.className = "cInvalid";
		bValid = false;
		}
	else
		{
		//Check that the email address is valid
		var oRegExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-Z0-9]{2,4}$/;

			if((EmailInput.value.length > 0 && EmailInput.value.match(oRegExp) == null))
				{
				//Not Valid
				EmailInput.className = "cInvalid";
				bValid = false;
				//EmailInput.value = ""; // This would reset the entry to a blank field
				}
			else
				{
				EmailInput.className = "cValid";
				}
		}

	// PHONE	- Ensure phone input has characters entered and that it is of correct length
	if( ((PhoneInput.value.length > 0 && PhoneInput.value.length < 8) || PhoneInput.value.length > 16))
		{
		//Not Valid
		PhoneInput.className = "cInvalid";
		bValid = false;
		}

	else
		{
		//Check that the phone number is valid
		var oRegExp = /^[0-9]+$/
		if((PhoneInput.value.length > 0 && PhoneInput.value.match(oRegExp) == null))
			{
			//Not Valid
			PhoneInput.className = "cInvalid";
			bValid = false;
			//PhoneInput.value = "";  // This would reset the entry to a blank field
			}
		else
				{
				if(PhoneInput.value.length >0) { PhoneInput.className = "cValid" };
				}
		}

		// Comments area - Ensure textarea has characters entered
		if (!(Comments.value.length > 0))
		{

		//Not Valid
		//Comments.innerHTML = "Please provide some comments.";
		Comments.className = "cInvalidComments";
		bValid = false;
		}

	//Notify user whether form will be validated or whether they will need to re-enter data
	if(bValid != true)
	{
		document.getElementById("idJSInvalidMsg").innerHTML 	= InvalidHeading;
		document.getElementById("idJSInvalidMsgs").innerHTML 	= InvalidMessage;
	}
	return bValid;
}

function fnResetContactForm() {

	/* // Not currently used - could be used to apply Valid styles
	document.getElementById("idNameInput").className  = "cValid";
	document.getElementById("idEmailInput").className = "cValid";
	document.getElementById("idPhoneInput").className = "cValid";
	*/
	document.getElementById("idCommentsInput").className   = "cValidComments";


	//  Reapply styles (width of input boxes defaults to default width otherwise)
	document.getElementById("idNameInput").className  = "clInputText";
	document.getElementById("idEmailInput").className = "clInputText";
	document.getElementById("idPhoneInput").className = "clInputText";

	//  Clear the error messages displayed to user for invalid details
	document.getElementById("idJSInvalidMsg").innerHTML 	= " ";
	document.getElementById("idJSInvalidMsgs").innerHTML 	= " ";
}
