﻿function ValidateForm()
{
	var ValidationResult = true;
	var ErrorMessage = "";
	var ErrorColour = "#F8E1E7";

	//Check that a name has been entered
	if (document.getElementById("NameTextbox").value == "")
	{
		document.getElementById("NameTextbox").style.backgroundColor = ErrorColour;
		ValidationResult = false;
		ErrorMessage += "Please enter your name into the box provided.\n";
	}
	else
	{
		document.getElementById("NameTextbox").style.backgroundColor = "#FFF";
	}
	//Check that either a valid email address or telephone number has been provided
	if (document.getElementById("EmailTextbox").value == "" && document.getElementById("TelTextbox").value == "")
	{
		document.getElementById("EmailTextbox").style.backgroundColor = ErrorColour;
		document.getElementById("TelTextbox").style.backgroundColor = ErrorColour;
		
		ValidationResult = false;
		ErrorMessage += "Please enter an email address or a contact telephone number into the into the boxes provided.\n";
	}
	else
	{
		document.getElementById("EmailTextbox").style.backgroundColor = "#FFF";
		document.getElementById("TelTextbox").style.backgroundColor = "#FFF";
	}
	//Check that a comment has been entered
	if (document.getElementById("CommentsTextarea").value == "")
	{
		document.getElementById("CommentsTextarea").style.backgroundColor = ErrorColour;
		ValidationResult = false;
		ErrorMessage += "Please enter your message into the box provided.\n";
	}
	else
	{
		document.getElementById("CommentsTextarea").style.backgroundColor = "#FFF";
	}
	//Check that a valid email address has been entered
	if (document.getElementById("EmailTextbox").value != "")
	{
		if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.getElementById("EmailTextbox").value))
		{
			document.getElementById("EmailTextbox").style.backgroundColor = "#FFF";		}
		else
		{
			document.getElementById("EmailTextbox").style.backgroundColor = ErrorColour;
			ValidationResult = false;
			ErrorMessage += "Please enter a valid email address into the box provided.\n";
		}		
	}
	if (ValidationResult)
	{
		return true;
	}
	else
	{
		alert(ErrorMessage);
		return false;
	}
}

