// JavaScript Document

var headrequest=null;
var validpostcode=true;
var validrefcode=true;
var validphone=false;
var evvalidphone=false;
/*function init(){
   try {

	 headrequest = new XMLHttpRequest();
   } catch (trymicrosoft) {
     try {

	   headrequest = new ActiveXObject("Msxml2.XMLHTTP");
     } catch (othermicrosoft) {
       try {

		 headrequest = new ActiveXObject("Microsoft.XMLHTTP");
       } catch (failed) {

		 headrequest = false;
       }
     }
   }

   if (!headrequest)
     alert("Error initializing XMLHttpRequest!");
}//end init function
*/


function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

/*function checkValues()
{
	if(document.frmQuotation.Property.value.replace(/\s/g,'')!="" && document.frmQuotation.Postcode.value.replace(/\s/g,'')=="")
	{
		alert("Please provide us with your postcode.");
		document.frmQuotation.Postcode.value="";
		document.frmQuotation.Postcode.focus();
		return false;
	}
	if (isNotBlank(document.frmQuotation.Postcode,"Please provide us with your postcode.")==0)
	{
		return false;
	}
	else if (isAlphaNumeric(document.frmQuotation.Postcode,"Please enter only alpha numeric characters for Postcode.")==0)
	{
		return false;
	}

	if(document.frmQuotation.Postcode.value.replace(/\s/g,'')!="")
	{
		var strPostcode = document.frmQuotation.Postcode.value.replace(/\s/g,'');
		myWin = window.open("addresses.php?pc="+strPostcode,"AFDPostcode","scrollbars=1,width=520,height=425,left=10,top=10");
		myWin.focus();
	}
}

function callserver(url)
{
	headrequest=false;
	init();
	headrequest.open("GET",url,false);
	if (typeof window.ActiveXObject != 'undefined' ){
		headrequest.onreadystatechange=updatePage; }
	else
	{
		headrequest.onload =updatePage;
	}
	headrequest.send(null);
}

/*function updatePage()
{
	if(headrequest.readyState==4 && headrequest.status == 200)
	{
		var response=headrequest.responseText;
		if(response=='FL')
		{
			validpostcode=true;
		}
		else{
		validpostcode=false; }

	}
}*/

/*function callserverphone(url)
{
	headrequest=false;
	init();
	headrequest.open("GET",url,false);
	if (typeof window.ActiveXObject != 'undefined' ){
		headrequest.onreadystatechange=phoneupdatePage; }
	else
	{
		headrequest.onload =phoneupdatePage;
	}
	headrequest.send(null);
	alert(validphone);
}*/

/*function phoneupdatePage()
{
	if(headrequest.readyState==4 && headrequest.status == 200)
	{
		var response=headrequest.responseText;
		//alert(response);
		if(response=='1')
		{
			validphone=true;
		}
		else{
		validphone=false; }

	}
}
function callservernophone(url)
{
	headrequest=false;
	init();
	headrequest.open("GET",url,false);
	headrequest.send(null);
}

function callserverrefcode(url)
{
	headrequest=false;
	init();
	headrequest.open("GET",url,false);
	if (typeof window.ActiveXObject != 'undefined' ){
		headrequest.onreadystatechange=refcodeupdatePage; }
	else
	{
		headrequest.onload =refcodeupdatePage;
	}
	headrequest.send(null);
}

function refcodeupdatePage()
{
	if(headrequest.readyState==4 && headrequest.status == 200)
	{
		var response=headrequest.responseText;
		if(response=='notok')
		{
			validrefcode=false;
		}
		else if(response=='ok'){
		validrefcode=true; }

	}
}*/
function strtrim() {
    return this.replace(/^\s+/,'').replace(/\s+$/,'');
}
String.prototype.trim = strtrim;

function isAlphaNumeric(obj,msg)
{
	var nam = obj.value;
	var expspecialchr = /[^\w|\s]+/;
	nam = nam.trim();
	if(expspecialchr.test(nam) == true)
	{
		alert(msg);
		obj.focus();
		return 0;
	}	
} 


function isAlphaNumericAndHyphen(obj,msg)
{
	var nam = obj.value;
	var expspecialchr = /[^\w|\s|\-]+/;
	nam = nam.trim();
	if(expspecialchr.test(nam) == true)
	{
		alert(msg);
		obj.focus();
		return 0;
	}	
	
} 

function isAlphaNumericAndHyphenAmpercent(obj,msg)
{
	var nam = obj.value;
	var expspecialchr = /[^\w|\s|\-|\&]+/;
	nam = nam.trim();
	if(expspecialchr.test(nam) == true)
	{
		alert(msg);
		obj.focus();
		return 0;
	}	
	
} 


function validation()
{
	//alert('test');
	
	var expTelephone = /[^\d|\s]+/;
	var expDigit = /[^\d]+/;
	var expname=/[^A-Za-z\s\.\'-]+/;
	//chkvalidation();
	if((document.frmQuotation.no_of_windows.value.replace(/\s/g,'')=="") && (document.frmQuotation.no_of_doors.value.replace(/\s/g,'')=="") && (document.frmQuotation.no_of_doors2.value.replace(/\s/g,'')==""))
	{	
		
		alert("Please provide the number of windows, doors, or French / patio doors needed for your property.");
		/* document.frmQuotation.no_of_windows.value=""; */
		document.frmQuotation.no_of_windows.focus();
		return false;
	}


///////
 else if(expDigit.test(document.frmQuotation.no_of_windows.value)==true)
	{
		
		alert("Please provide the number of windows, doors, or French / patio doors needed for your property.");
		document.frmQuotation.no_of_windows.focus();
		return false;
	}
else if(expDigit.test(document.frmQuotation.no_of_doors.value)==true )
	{
		
		alert("Please provide the number of windows, doors, or French / patio doors needed for your property.");
		document.frmQuotation.no_of_doors.focus();
		return false;
	}
		else if(expDigit.test(document.frmQuotation.no_of_doors2.value)==true)
	{
		
		alert("Please provide the number of windows, doors, or French / patio doors needed for your property.");
		document.frmQuotation.no_of_doors2.focus();
		return false;
	}
	
   if(document.frmQuotation.no_of_windows.value.replace(/\s/g,'')==0 && document.frmQuotation.no_of_doors.value.replace(/\s/g,'')==0 &&document.frmQuotation.no_of_doors2.value.replace(/\s/g,'')==0  )
   {
	   
	  
		alert("Please provide the number of windows, doors, or French / patio doors needed for your property.");
		document.frmQuotation.no_of_windows.focus();
		return false;	
   }


	if(document.frmQuotation.name.value.replace(/\s/g,'')=="")
	{
		
		alert("Please provide your first name. \nFor example: John"); 
		//alert("Insert Name");
		document.frmQuotation.name.value="";
		document.frmQuotation.name.focus();
		return false;
	}

	if(expname.test(document.frmQuotation.name.value)==true)
	{
		
		alert("Please provide your first name.");
		document.frmQuotation.name.value="";
		document.frmQuotation.name.focus();
		return false;
	}

	if(document.frmQuotation.surname.value.replace(/\s/g,'')=="")
	{
		alert("Please provide your surname.\nFor example Smith"); 
		//alert("Insert Surname");
		document.frmQuotation.surname.value="";
		document.frmQuotation.surname.focus();
		return false;
	}

	if(expname.test(document.frmQuotation.surname.value)==true)
	{
		alert("Please provide your surname.");
		document.frmQuotation.surname.value="";
		document.frmQuotation.surname.focus();
		return false;
	}
	if(document.frmQuotation.email.value.replace(/\s/g,'')=="")
	{
		
		alert("Please provide your email address.\nFor example, john.smith@safestyle.co.uk");
		document.frmQuotation.email.value="";
		document.frmQuotation.email.focus();
		return false;
	}
	else if(!/^\w+([\.\-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(document.frmQuotation.email.value))
	{
		alert("Please provide a valid email address.\nFor example, john.smith@safestyle.co.uk");
		document.frmQuotation.email.focus();
		return false;
	}
		var searchstring=/[0-9A-Za-z]*/i;
	var postcodevalue=document.frmQuotation.Postcode.value;

	if(document.frmQuotation.Postcode.value.replace(/\s/g,'')=="")
	{
		alert("Please provide your postcode.\nFor example, BD1 3AZ");
		document.frmQuotation.Postcode.value="";
		document.frmQuotation.Postcode.focus();
		return false;
	}
	else if (isAlphaNumeric(document.frmQuotation.Postcode,"Please enter only alpha numeric characters for Postcode.")==0)
	{
		
		return false;
	}
	else{
		
		var ajaxObj=new ajax(true);
	var today = new Date();
	var time=today.getTime();
	
	ajaxObj.doReq('checkpostcode.php?pc='+escape(postcodevalue)+'&t='+time,postcoderesult,false);
	
	if(!validpostcode){
	alert("Please enter valid UK postcode");
	return false;
	}
		
		var pcode=document.frmQuotation.Postcode.value;
		
		var pcode_len=pcode.length;
		if(pcode.charAt(pcode_len-3)=='O' ||  pcode.charAt(pcode_len-3)=='o'){
			pcode1=pcode.substr(0,pcode_len-3);
			pcode2=0;
			pcode3=pcode.substr((pcode_len-2));
			
			pcode=pcode1+pcode2+pcode3;
			document.frmQuotation.Postcode.value='';
			document.frmQuotation.Postcode.value=pcode;
		}
	}
if(document.frmQuotation.phone.value.replace(/\s/g,'')=="")
	{

		alert("Please provide your daytime phone number.\nFor example, 01274 106107");
		document.frmQuotation.phone.value="";
		document.frmQuotation.phone.focus();
		return false;
	}
	if(expTelephone.test(document.frmQuotation.phone.value.replace(/\s/g,''))==true)
	{
		alert("Phone number must be numeric");
		document.frmQuotation.phone.focus();
		return false;
	}
	var dayphone=document.frmQuotation.phone.value.replace(/\s/g,'');
	if((dayphone.length!=10&&dayphone.length!=11)||(dayphone.charAt(0)!=0))
	{
			alert("Please provide a valid UK telephone number.\nFor example, 01274 106107");
			document.frmQuotation.phone.focus();
			return false;	
	}
	/*if(document.frmQuotation.phone.value.replace(/\s/g,'')!="")
	{
		var flag=true;
		if(expTelephone.test(document.frmQuotation.phone.value)==true)
		{
			alert("Telephone number must be numeric.");
			document.frmQuotation.phone.focus();
			return false;
		}
	 if(!validphone)
		  {
			
			var name=document.frmQuotation.name.value.replace(/\s/g,'');//+" "+document.frmQuotation.surname.value.replace(/\s/g,'');
			var dayphone=document.frmQuotation.phone.value.replace(/\s/g,'');
			if((dayphone.length==10||dayphone.length==11)&&(dayphone.charAt(0)==0))
			{	var nourl="sendtele.php?phone="+escape(dayphone)+"&name="+escape(name)+"&windows="+escape(document.frmQuotation.no_of_windows.value.replace(/\s/g,''))+"&doors="+escape(document.frmQuotation.no_of_doors.value.replace(/\s/g,''))+"&color="+escape(document.frmQuotation.colour.value.replace(/\s/g,''))+"&postcode="+escape(document.frmQuotation.Postcode.value.replace(/\s/g,''))+"&propertytype="+escape(document.frmQuotation.is_your_property.value.replace(/\s/g,''))+"&email="+escape(document.frmQuotation.email.value.replace(/\s/g,''))+"&telcat=Daytime Phone";
				//callservernophone(nourl);
			}
			alert("Please provide a valid UK telephone number.\nFor example, 01274 106107");
			document.frmQuotation.phone.focus();
			return false;
		  }
	   }
	}*/
if(document.frmQuotation.evening_phone.value.replace(/\s/g,'')=="")
	{
	
		alert("Please provide your evening phone number.\nFor example, 01274 106107");
		document.frmQuotation.evening_phone.value="";
		document.frmQuotation.evening_phone.focus();
		return false;
	}
	if(expTelephone.test(document.frmQuotation.evening_phone.value.replace(/\s/g,''))==true)
	{
		alert("Phone number must be numeric");
		document.frmQuotation.evening_phone.focus();
		return false;
	}
	var eveningphone=document.frmQuotation.evening_phone.value.replace(/\s/g,'');
	if((eveningphone.length!=10&&eveningphone.length!=11)||(eveningphone.charAt(0)!=0))
	{
			alert("Please provide a valid UK telephone number.\nFor example, 01274 106107");
			document.frmQuotation.evening_phone.focus();
			return false;	
	}
	/*<!--if(document.frmQuotation.evening_phone.value.replace(/\s/g,'')!="")
	{
		if(expTelephone.test(document.frmQuotation.evening_phone.value)==true)
		{
			alert("Telephone number must be numeric.\nFor example 01274 106107");
			document.frmQuotation.evening_phone.focus();
			return false;
		}
		else
		{
		  var evphonevalue=document.frmQuotation.evening_phone.value;
		 // var url1="phonevalidate.php?phone="+escape(evphonevalue);
		  var ajaxObj1=new ajax(true);
		var today = new Date();
		var time=today.getTime();
		
	ajaxObj1.doReq('phonevalidate.php?phone='+escape(evphonevalue)+'&t='+time,eveningphoneresult,false);
	
	
		
		  if(!evvalidphone)
		  {
			var name=document.frmQuotation.name.value.replace(/\s/g,'');//+" "+document.frmQuotation.surname.value.replace(/\s/g,'');
			var dayphone=document.frmQuotation.phone.value.replace(/\s/g,'');
			var evenphone=document.frmQuotation.evening_phone.value.replace(/\s/g,'');
			if((evenphone.length==10||evenphone.length==11)&&(evenphone.charAt(0)==0))
			{	var nourl="sendtele.php?phone="+escape(dayphone)+"&eveningphone="+escape(evenphone)+"&name="+escape(name)+"&windows="+escape(document.frmQuotation.no_of_windows.value.replace(/\s/g,''))+"&doors="+escape(document.frmQuotation.no_of_doors.value.replace(/\s/g,''))+"&color="+escape(document.frmQuotation.colour.value.replace(/\s/g,''))+"&postcode="+escape(document.frmQuotation.Postcode.value.replace(/\s/g,''))+"&propertytype="+escape(document.frmQuotation.is_your_property.value.replace(/\s/g,''))+"&email="+escape(document.frmQuotation.email.value.replace(/\s/g,''))+"&telcat=Evening Phone";
				//callservernophone(nourl);
			}
			alert("Please provide a valid UK telephone number. \nFor example, 01274 106107");
			document.frmQuotation.evening_phone.focus();
			return false;
		  }
	   }
	}-->*/	
	
	return true;
	
	
}

function postcoderesult(resp)
{
	if(resp=='FL')
	{
		validpostcode=true;
	}
	else
	{
		validpostcode=false;
	}
		
	
}

function eveningphoneresult(resp)
{
	if(resp==1)
	{
		evvalidphone=true;
	}
	else if(resp==0)
	{
		evvalidphone=false;
	}
		
	
}

function phoneresult(resp)
{
	//alert(resp);
	if(resp==1)
	{
		validphone=true;
		//alert("1");
	}
	else if(resp==0)
	{
		validphone=false;
		//alert("0");
	}
		
	
}


