
function IsValidateEmail(Email) 
{
	var regEmail  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	return regEmail.test(Email.replace(/\s/g, ""));
}

var containErrors = false;
function SetError(divError, msg)
{
	
	document.getElementById(divError).style.display = "block";
	document.getElementById(divError).innerHTML = msg;
	
	containErrors = true;
}

function ClearErrors()
{
	
	document.getElementById("divErrorTitle").style.display = "none";
	document.getElementById("divErrorFirstName").style.display = "none";
	document.getElementById("divErrorSurname").style.display = "none";
	document.getElementById("divErrorContactNumber").style.display = "none";
	document.getElementById("divErrorWorkPhone").style.display = "none";
	document.getElementById("divErrorMobilePhone").style.display = "none";
	document.getElementById("divErrorEmail").style.display = "none";
	document.getElementById("divErrorPropertyAddress").style.display = "none";
	document.getElementById("divErrorCallBack").style.display = "none";
	
	containErrors = false;
	
}

function CheckForm() 
{
	
	var form = document.getElementById("form1");
	var now = new Date();
	
	ClearErrors();
	
	if(form.title.selectedIndex <= 0)
		SetError("divErrorTitle", "You must enter your title");
	
	if(form.firstname.value.replace(/\s/g, "") == "")
		SetError("divErrorFirstName", "You must enter your first name");
	
	if(form.surname.value.replace(/\s/g, "") == "")
		SetError("divErrorSurname", "You must enter your surname");
	
	if(form.contactnumber.value.replace(/\s/g, "") == "")
		SetError("divErrorContactNumber", "You must enter a contact number");
	
	else if(String(form.contactnumber.value.replace(/\s/g,"")).search(/[^\d]/) > -1)
		SetError("divErrorContactNumber", "Your contact number must only contain numbers without spaces");
		
	if(form.workphone.value.replace(/\s/g, "") != "" && String(form.workphone.value.replace(/\s/g,"")).search(/[^\d]/) > -1)
		SetError("divErrorWorkPhone", "Your work number must only contain numbers without spaces");

	if(form.mobilephone.value.replace(/\s/g, "") != "" && String(form.mobilephone.value.replace(/\s/g,"")).search(/[^\d]/) > -1)
		SetError("divErrorMobilePhone", "Your mobile number must only contain numbers without spaces");
	
	if(form.emailaddress.value.replace(/\s/g, "") == "")
		SetError("divErrorEmail", "You must enter an email address");
	
	else if(!IsValidateEmail(String(form.emailaddress.value.replace(/\s/g,""))))
		SetError("divErrorEmail", "You have entered an invalid email address");
	
	if(form.propertyaddress.value.replace(/\s/g, "") == "")
		SetError("divErrorPropertyAddress", "You must enter a property address");
	
	if(form.callback.value.replace(/\s/g, "") == "")
		SetError("divErrorCallBack", "You must enter a suitable time to be contacted");
	
	// keep the same window scroll position when the form submit in all internet browsers
	window.scrollTo(0, 1000);
	
	if(!containErrors)
		return true;
	else
		return false;
	
}

function ValuationClearErrors()
{
	
	document.getElementById("divErrorTitle").style.display = "none";
	document.getElementById("divErrorFirstName").style.display = "none";
	document.getElementById("divErrorSurname").style.display = "none";
	document.getElementById("divErrorContactNumber").style.display = "none";
	document.getElementById("divErrorWorkPhone").style.display = "none";
	document.getElementById("divErrorMobilePhone").style.display = "none";
	document.getElementById("divErrorEmail").style.display = "none";
	document.getElementById("divErrorDates").style.display = "none";
	document.getElementById("divErrorTimeOfDay").style.display = "none";	
	containErrors = false;
	
}

function ValuationCheckForm() 
{
	
	var form = document.getElementById("form1");
	var now = new Date();
	
	ValuationClearErrors();
	
	if(form.title.selectedIndex <= 0)
		SetError("divErrorTitle", "You must enter your title");
	
	if(form.firstname.value.replace(/\s/g, "") == "")
		SetError("divErrorFirstName", "You must enter your first name");
	
	if(form.surname.value.replace(/\s/g, "") == "")
		SetError("divErrorSurname", "You must enter your surname");
	
	if(form.contactnumber.value.replace(/\s/g, "") == "")
		SetError("divErrorContactNumber", "You must enter a contact number");
	
	else if(String(form.contactnumber.value.replace(/\s/g,"")).search(/[^\d]/) > -1)
		SetError("divErrorContactNumber", "Your contact number must only contain numbers without spaces");
		
	if(form.workphone.value.replace(/\s/g, "") != "" && String(form.workphone.value.replace(/\s/g,"")).search(/[^\d]/) > -1)
		SetError("divErrorWorkPhone", "Your work number must only contain numbers without spaces");

	if(form.mobilephone.value.replace(/\s/g, "") != "" && String(form.mobilephone.value.replace(/\s/g,"")).search(/[^\d]/) > -1)
		SetError("divErrorMobilePhone", "Your mobile number must only contain numbers without spaces");
	
	if(form.emailaddress.value.replace(/\s/g, "") == "")
		SetError("divErrorEmail", "You must enter an email address");
	
	else if(!IsValidateEmail(String(form.emailaddress.value.replace(/\s/g,""))))
		SetError("divErrorEmail", "You have entered an invalid email address");
	
	if(form.dates.selectedIndex <= 0)
		SetError("divErrorDates", "You must enter a Date");;
	
	if(form.timeofday.value.replace(/\s/g, "") == "")
		SetError("divErrorTimeOfDay", "You must enter a suitable time of day");
	
	// keep the same window scroll position when the form submit in all internet browsers
	window.scrollTo(0, 1000);
	
	if(!containErrors)
		return true;
	else
		return false;
	
}
