 function FocusBox(q,text)
 {
   if (q.value == text)
   {
    q.value = "";
   }
 }

 function BlurBox(q,text)
 {
  if (q.value == "")
   {
    q.value = text;
   }
 }




function validate_form() 
{ 
	// Check Business Name 
	var Bname=document.getElementById('txtCompany') 
	var Emailaddr=document.getElementById('txtEmail');
	var Fname=document.getElementById('txtFirstName') ;
	var Lname=document.getElementById('txtLastName');
	var Phone=document.getElementById('txtContact'); 
	var Title=document.getElementById('cboType'); 
	var ContactDate=document.getElementById('txtContactDate');
	var SpendOnGas = document.getElementById('cboSpendOnGas');
	var Postcode=document.getElementById('txtPostCode');
	 
	if (Title.value == "Title")
	{
		alert("Please Pick a title e.g Mr,mrs etc"); 
		return false; 
	}


	// Check First Name 
	var FnameRegex=/(\d+)/; 
	//No numerics 

	if( Fname.value == "" || Fname.value == "First Name:") 
	{ 
		alert("The First Name field is incomplete.\n\nPlease try again."); 
		//Fname.focus(); 
		return false; 
	} 
	if (check_using_regex(Fname.value,FnameRegex)==true) 
	{ 	
		alert("The First Name field is invalid - it can't contain numbers.\n\nPlease try again."); 
		Fname.value=""; 
		//Fname.focus(); 
		return false; 
	} 



	// Check Surname 
	var LnameRegex=/(\d+)/; 
	//No numerics 
	if( Lname.value == "" || Lname.value == "Last Name:") 
	{ 
		alert("The Surname field is incomplete.\n\nPlease try again."); 
		//Lname.focus(); 
		return false; 
	} 
	
	if (check_using_regex(Lname.value,LnameRegex)==true)
	{ 
		alert("The Surname field is invalid - it can't contain numbers.\n\nPlease try again."); 
		Lname.value=""; 
		//Lname.focus(); 
		return false; 
	} 

	if( Bname.value == "" || Bname.value == "Company:") 
	{ 
		alert("The 'Business Name' field is incomplete.\n\nPlease try again."); 
		//Bname.focus(); 
		return false; 
	} 

	if (ContactDate.value == "")
	{
		alert("Please select a time/date that would suit you best to be contact on."); 
		return false; 
	}


	var PcodeRegex=/(^[A-Za-z]{1,2}[\d]{1,2}([A-Za-z])?\s?[\d][A-Za-z]{2}$)/; 

	if (Postcode.value == "" || Postcode.value == "Postcode:") 
	{ 
	 alert("The Post Code field is incomplete.\n\nPlease try again."); 
	 //Postcode.focus(); 
	 return false; 
	} 
	
	if (check_using_regex(Postcode.value, PcodeRegex)==false) 
	{ 
	 alert("The Post Code is invalid - must be a UK one.\n\nPlease try again."); 
	 Postcode.value=""; 
	 //Postcode.focus(); 
	 return false; 
	}

	// Check PhoneNO 
	var PhoneRegex=/(^((\(?0\d{4}\)?\s?\d{3}\s?\d{3})|(\(?0\d{3}\)?\s?\d{3}\s?\d{4})|( \(?0\d{2}\)?\s?\d{4}\s?\d{4}))(\s?#(\d{4}|\d{3}))?$)/; 
	
	if ( Phone.value == null || Phone.value == "" || Phone.value == "Phone:") 
	{ 
		alert("The Telephone Number field is incomplete.\n\nPlease try again."); 
		//Phone.focus();
		return false; 
	} 
	
	if (check_using_regex(Phone.value,PhoneRegex)==false) 
	
	{ 
		 alert("The Telephone Number is invalid - must be a UK number with no international codes.\n\nPlease try again."); 
		 Phone.value=""; 
		 //Phone.focus(); 
		 return false; 
	} 
	
	// Check Email 
	
	var EmailRegex=/(^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$)/; 
	
	if ( Emailaddr.value==null || Emailaddr.value=="" || Emailaddr.value=="Email") 
	{ 
	 alert("The Email field is incomplete.\n\nPlease try again."); 
	 //Emailaddr.focus(); 
	 return false; 
	} 
	
	if (check_using_regex(Emailaddr.value,EmailRegex)==false) 
	{ 
	 alert("The Email address is invalid.\n\nPlease try again."); 
	 Emailaddr.value=""; 
	 //Emailaddr.focus(); 
	 return false; 
	} 
	if (email_final_checks(Emailaddr.value)==false) 
	{ 
	 alert("The Email address is invalid.\n\nPlease try again."); 
	 Emailaddr.value=""; 
	 //Emailaddr.focus(); 
	 return false; 
	} 

	if (SpendOnGas.value == "" || SpendOnGas.value == "Annual Spend:")
	{
		 alert("Please select the amount you spend on gas from the drop down list."); 
		 SpendOnGas.value=""; 
		// SpendOnGas.focus(); 
		 return false; 

	}

	return true; 
} 

function check_using_regex(strtotest, regexstr) 
{ 
 var re = new RegExp(regexstr); 
 if (strtotest.match(re)) 
 { 
  return true; 
 } else { 
 return false; 
 } 
} 

function email_final_checks(strToCheck) 
{ 
// Get number of chars that occur after @ 
var AtPosition = strToCheck.indexOf("@");
var Position = strToCheck.length - AtPosition; 

// Check position of @ is at least 5 chars before end of string 

if (Position < 6) { return false; } 

// Check for at least 1 stop after @ + 2 
if (strToCheck.indexOf(".", AtPosition + 2) == -1) { return false; } 

// Check for at least two chars before @ 
if (AtPosition < 2) { return false; } 

// email format should be OK return true; 
}