var account_code='CONCE11113';
var license_code='FF98-XD88-XE58-XP78';

function pcaFastAddressBegin()
   {
	  var postcode = document.forms[0]["searchpostcode"].value;
	  var building = document.forms[0]["searchbuilding"].value;
	  var scriptTag = document.getElementById("pcaScriptTag");
	  var headTag = document.getElementsByTagName("head").item(0);
	  var strUrl = "";
	  
	  //Build the url
	  strUrl = "https://services.postcodeanywhere.co.uk/inline.aspx?";
	  strUrl += "&action=fetch";
	  strUrl += "&postcode=" + escape(postcode);
	  strUrl += "&building=" + escape(building);
	  strUrl += "&account_code=" + escape(account_code);
	  strUrl += "&license_code=" + escape(license_code);
	  strUrl += "&callback=pcaFastAddressEnd";
	  
	  if (scriptTag)
		 {
			headTag.removeChild(scriptTag);
		 }
	  scriptTag = document.createElement("script");
	  scriptTag.src = strUrl
	  scriptTag.type = "text/javascript";
	  scriptTag.id = "pcaScriptTag";
	  headTag.appendChild(scriptTag);
	  
   }
   
function resetaddress(){

	document.forms[0]["address1"].value = '';
	document.forms[0]["address2"].value = '';
	document.forms[0]["address3"].value = '';
	document.forms[0]["address4"].value = '';
	document.forms[0]["postcode"].value = '';
	$(".addressbox").addClass("hidden");
	$(".addressboxhide").removeClass("hidden");
	$("#country").removeAttr("disabled");
	$("#country").val("Great Britain");

}

function entermaually(){

	document.forms[0]["address1"].value = '';
	document.forms[0]["address2"].value = '';
	document.forms[0]["address3"].value = '';
	document.forms[0]["address4"].value = '';
	document.forms[0]["postcode"].value = '';
	$(".addressbox").removeClass("hidden");
	$(".addressboxhide").addClass("hidden");
	$("#postcodeerror").addClass("hidden");
	$("#country").val("Great Britain");
	$("#country").removeAttr("disabled");

}

function pcaFastAddressEnd()
{
  if (pcaIsError)
	 {
		if (pcaErrorNumber == 24){ 
			$('#postcodeerror').html("Please enter the post code");
		}else{
			$('#postcodeerror').html(pcaErrorMessage);
		}	
		$("#postcodeerror").removeClass("hidden");
		
	 }
  else
	 {
		if (pcaRecordCount==0)
		   {
			  $('#postcodeerror').html("We could not find any address at this postcode please check the postcode and try again");
			  $("#postcodeerror").removeClass("hidden");
		   }
		else
		   {
			  document.forms[0]["address1"].value = '' + pca_line1[0];
			  document.forms[0]["address2"].value = '' + pca_line2[0];
			  document.forms[0]["address3"].value = '' + pca_post_town[0];
			  document.forms[0]["address4"].value = '' + pca_county[0];
			  document.forms[0]["postcode"].value = '' + pca_postcode[0];
			  checkpostcode();
			  $(".addressbox").removeClass("hidden");
			  $(".addressboxhide").addClass("hidden");
			  $('#postcodeerror').addClass("hidden");
		   }
	 }
}


function checkpostcode(){
	var thepostcode = $("#postcode").val();
	thepostcode = thepostcode.toUpperCase();
	var thepostcodestart = thepostcode.substring(0,2);
	var thepostcodestart2 = thepostcode.substring(0,4);
	if (thepostcodestart == "BT"){
		$("#country").val("Northern Ireland");
		$("#country").attr("disabled","disabled");
	}else if (thepostcodestart == "IM"){
		$("#country").val("Isle Of Man");
		$("#country").attr("disabled","disabled");
	}else if (thepostcodestart == "GY" || thepostcodestart == "JE"){
		$("#country").val("Channel Isles");
		$("#country").attr("disabled","disabled");
	}else if (thepostcodestart2 == "KW15" || thepostcodestart2 == "KW16" || thepostcodestart2 == "KW17" || thepostcodestart == "HS" || thepostcodestart == "ZE"){
		$("#country").val("Scottish Isles");
		$("#country").attr("disabled","disabled");
	}else{
		$("#country").val("Great Britain");
		$("#country").removeAttr("disabled");
	}
	$("#postcode").val(thepostcode);
}

$("input[name='postcode']").keyup( function() {
  	checkpostcode();
});