// Working from the bottom up
function PostcodeAnywhere_Interactive_Find_v1_10Begin(Key, SearchTerm, PreferredLanguage, Filter, UserName)
   {
      var scriptTag = document.getElementById("PCA6d35cfc188f1451f9cfdf1b5d751a716");
      var headTag = document.getElementsByTagName("head").item(0);
      var strUrl = "";

      //Build the url
      strUrl = "http://services.postcodeanywhere.co.uk/PostcodeAnywhere/Interactive/Find/v1.10/json.ws?";
      strUrl += "&Key=" + encodeURI(Key);
      strUrl += "&SearchTerm=" + encodeURI(SearchTerm);
      strUrl += "&PreferredLanguage=" + encodeURI(PreferredLanguage);
      strUrl += "&Filter=" + encodeURI(Filter);
      strUrl += "&UserName=" + encodeURI(UserName);
      strUrl += "&CallbackFunction=PostcodeAnywhere_Interactive_Find_v1_10End";

      //Make the request
      if (scriptTag) 
         {
            try
              {
                  headTag.removeChild(scriptTag);
              }
            catch (e)
              {
                  //Ignore
              }
         }
      scriptTag = document.createElement("script");
      scriptTag.src = strUrl
      scriptTag.type = "text/javascript";
      scriptTag.id = "PCA6d35cfc188f1451f9cfdf1b5d751a716";
      headTag.appendChild(scriptTag);
   }

function PostcodeAnywhere_Interactive_Find_v1_10End(response)
   {
      //Test for an error
      if (response.length==1 && typeof(response[0].Error) != 'undefined')
         {
            //Show the error message
            alert(response[0].Description);
         }
      else
         {
            //Check if there were any items found
            if (response.length==0)
               {
                  alert("Sorry, no matching items found");
               }
            else
               {
                   document.getElementById('return').style.display = '';

                    for (var i=0;i<response.length;i++)
			            document.getElementById('return').options.add(new Option(response[i].StreetAddress + ", " + response[i].Place, response[i].Id));
						$.colorbox({width: "500", height: "250", inline: true, href:"#return", overlayClose: false, escKey: false});
               }
         }
   }
   
   
   
   
   
   function PostcodeAnywhere_Interactive_RetrieveById_v1_10Begin(Key, Id, PreferredLanguage, UserName)
   {
      var scriptTag = document.getElementById("PCAa73f9bc2b60d4e4cbd595512478a3291");
      var headTag = document.getElementsByTagName("head").item(0);
      var strUrl = "";

      //Build the url
      strUrl = "http://services.postcodeanywhere.co.uk/PostcodeAnywhere/Interactive/RetrieveById/v1.10/json.ws?";
      strUrl += "&Key=" + encodeURI(Key);
      strUrl += "&Id=" + encodeURI(Id);
      strUrl += "&PreferredLanguage=" + encodeURI(PreferredLanguage);
      strUrl += "&UserName=" + encodeURI(UserName);
      strUrl += "&CallbackFunction=PostcodeAnywhere_Interactive_RetrieveById_v1_10End";

      //Make the request
      if (scriptTag) 
         {
            try
              {
                  headTag.removeChild(scriptTag);
              }
            catch (e)
              {
                  //Ignore
              }
         }
      scriptTag = document.createElement("script");
      scriptTag.src = strUrl
      scriptTag.type = "text/javascript";
      scriptTag.id = "PCAa73f9bc2b60d4e4cbd595512478a3291";
      headTag.appendChild(scriptTag);
   }

function PostcodeAnywhere_Interactive_RetrieveById_v1_10End(response)
   {
      //Test for an error
      if (response.length==1 && typeof(response[0].Error) != 'undefined')
         {
            //Show the error message
            alert(response[0].Description);
         }
      else
         {
            //Check if there were any items found
            if (response.length==0)
               {
                  alert("Sorry, no matching items found");
               }
            else
               {
			       var retstring = 
				   "<input type='hidden' name='udprn' id='udprn' value='" + response[0].Udprn + "' />" + 				   
				   "<input type='hidden' name='company' id='company' value='" + response[0].Company + "' />" + 
				   "<input type='hidden' name='department' id='department' value='" + response[0].Department + "' />" + 
				   "<input type='hidden' name='line1' id='line1' value='" + response[0].Line1 + "' />" + 
				   "<input type='hidden' name='line2' id='line2' value='" + response[0].Line2 + "' />" + 
				   "<input type='hidden' name='line3' id='line3' value='" + response[0].Line3 + "' />" + 
				   "<input type='hidden' name='line4' id='line4' value='" + response[0].Line4 + "' />" + 
				   "<input type='hidden' name='line5' id='line5' value='" + response[0].Line5 + "' />" + 
				   "<input type='hidden' name='posttown' id='posttown' value='" + response[0].PostTown + "' />" + 
				   "<input type='hidden' name='county' id='county' value='" + response[0].County + "' />" + 
				   "<input type='hidden' name='postcode' id='postcode' value='" + response[0].Postcode + "' />" + 
				   "<input type='hidden' name='mailsort' id='mailsort' value='" + response[0].Mailsort + "' />" + 
				   "<input type='hidden' name='barcode' id='barcode' value='" + response[0].Barcode + "' />" + 
				   "<input type='hidden' name='type' id='type' value='" + response[0].Type + "' />" + 
				   "<input type='hidden' name='deliverypointsuffix' id='deliverypointsuffix' value='" + response[0].DeliveryPointSuffix + "' />" + 
				   "<input type='hidden' name='subbuilding' id='subbuilding' value='" + response[0].SubBuilding + "' />" + 
				   "<input type='hidden' name='buildingname' id='buildingname' value='" + response[0].BuildingName + "' />" + 
				   "<input type='hidden' name='buildingnumber' id='buildingnumber' value='" + response[0].BuildingNumber + "' />" + 
				   "<input type='hidden' name='primarystreet' id='primarystreet' value='" + response[0].PrimaryStreet + "' />" + 
				   "<input type='hidden' name='secondarystreet' id='secondarystreet' value='" + response[0].SecondaryStreet + "' />" + 
				   "<input type='hidden' name='doubledependentlocality' id='doubledependentlocality' value='" + response[0].DoubleDependentLocality + "' />" + 
				   "<input type='hidden' name='dependentlocality' id='dependentlocality' value='" + response[0].DependentLocality + "' />" + 
				   "<input type='hidden' name='pobox' id='pobox' value='" + response[0].PoBox + "' />" + 
				   "<input type='hidden' name='countryname' id='countryname' value='" + response[0].CountryName + "' />";
		   
			   	   document.getElementById('address').innerHTML = retstring;
				   
				   var retaddress = 
				   response[0].Line1 + ", " + 
				   response[0].Line2 + ", " + 
				   response[0].PostTown;
				   
				   document.getElementById('status').innerHTML = retaddress;
			   
			       document.getElementById('return').style.display = 'none';
				   $.colorbox.close()
				  
               }
         }
   }
   
function postcodeAnywhere()
	{
		if (xmlhttp.responseText>0) {
		var getpopup = PostcodeAnywhere_Interactive_Find_v1_10Begin('RE39-PZ29-XW26-UJ99', document.getElementById('pcode').value, '', '', '')
		//	alert(xmlhttp.responseText + "Postcode Anywhere Called");
		}
		else {
		//	alert("Postcode Anywhere NOT called because no operator is available")
		}
	}

// Function to allocate an operator
function checkPostcode(str) {
	if (str=="") {
		document.getElementById("status").innerHTML="";
		return;
	}
		if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
		}
		else {// code for IE6, IE5
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4 && xmlhttp.status==200) {
			var operatorAvailable = postcodeAnywhere();
			//Activate line below to test
			//document.getElementById("status").innerHTML=xmlhttp.responseText;
		}
	}
	xmlhttp.open("GET","/inc/allocate-to-operator.asp?q="+str,true);
	xmlhttp.send();
}

function postcodeCheck(pcode) {
	var regPostcode = /^([a-zA-Z]){1}([0-9][0-9]|[0-9]|[a-zA-Z][0-9][a-zA-Z]|[a-zA-Z][0-9][0-9]|[a-zA-Z][0-9]){1}([ ])([0-9][a-zA-z][a-zA-z]){1}$/;
	if(regPostcode.test(pcode) == false) 
	{
		document.getElementById('pcode').className = 'text clearField clearFieldActive hightlight'; // prints the error message
		document.getElementById('status').innerHTML = 'Please enter full Postcode with a space';
	}
	else
	{
		document.getElementById('pcode').className = 'text clearField clearFieldActive';
		document.getElementById('status').innerHTML = '';
		var findOperator = checkPostcode(pcode);
		var upperPostcode = document.form.pcode.value.toUpperCase();
		document.form.pcode.value = document.form.pcode.value.toUpperCase();
	}
}

// Function for the Free Estimate Form to show up...
function freeEstimate() {
	if (xmlhttp.responseText>0) {
		$.colorbox({width:546, height:770, iframe:true, href:"/inc/free-estimate.asp"});
	}
	else {
		location.reload(true)
	};
}

function checkZipcode(str) {
	if (str=="") {
		document.getElementById("status").innerHTML="";
		return;
	}
		if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
		}
		else {// code for IE6, IE5
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4 && xmlhttp.status==200) {
			var operatorAvailable = freeEstimate();
			// Activate line below to test
			//document.getElementById("zipstatus").innerHTML=xmlhttp.responseText;
		}
	}
	xmlhttp.open("GET","/inc/allocate-to-operator.asp?q="+str,true);
	xmlhttp.send();
}

// Function to validate the postcode and if successful to call other functions...
function zipCheck(zipcode) {
	var regZipcode = /^([a-zA-Z]){1}([0-9][0-9]|[0-9]|[a-zA-Z][0-9][a-zA-Z]|[a-zA-Z][0-9][0-9]|[a-zA-Z][0-9]){1}([ ])([0-9][a-zA-z][a-zA-z]){1}$/;
	if(regZipcode.test(zipcode) == false) 
	{
		document.getElementById('zipcode').className = 'zipinput clearField clearFieldActive hightlight'; // prints the error message
		document.getElementById('zipstatus').innerHTML = '<span style=color:#ff0000;>Please enter your Postcode with spaces</span>';
	}
	else
	{
		document.getElementById('zipcode').className = 'zipinput clearField clearFieldActive';
		document.getElementById('zipstatus').innerHTML = '';
		var findOperator = checkZipcode(zipcode);
		var upperZipcode = document.enterzip.zipcode.value.toUpperCase();
		document.enterzip.zipcode.value = document.enterzip.zipcode.value.toUpperCase();
	}
}
