// JavaScript Document

var account_code='PALME11121';
var license_code='DZ26-NK41-HK98-JN56';
var machine_id='';
var label='';
var PCAform;

$(document).ready(function(){
	
	pcaInitialize();
	
});

window.pcaInitialize = function (){
			
	$("#PCA_selectAddress").hide();
	$("#PCA_Fetch").hide();
	
	$("#PCA_Find").click(function(){
										
		PCAform = $(this).parents('form:first');
				
		var postcode = PCAform.find(".PCA_PostCode").val();
		var strUrl = "";		
		
		//Build the url
		strUrl = "http://services.postcodeanywhere.co.uk/inline.aspx?";
		strUrl += "&action=lookup";
		strUrl += "&type=by_postcode";
		strUrl += "&postcode=" + escape(postcode);
		strUrl += "&account_code=" + escape(account_code);
		strUrl += "&license_code=" + escape(license_code);
		strUrl += "&machine_id=" + escape(machine_id);
		strUrl += "&callback=pcaByPostcodeEnd";

		if( $("#pcaScriptTag") ){
			$("#pcaScriptTag").remove();
		}
		
		scriptTag = document.createElement("script");
		scriptTag.src = strUrl
		scriptTag.type = "text/javascript";
		scriptTag.id = "pcaScriptTag";		
		
		$("head").append(scriptTag);


	});
	
	$("#PCA_Fetch").click(function(){
		
		PCAform = $(this).parents('form:first');
								   
		var address_id = PCAform.find("#PCA_selectAddress").val();
		var strUrl = "";

		//Build the url
		strUrl = "http://services.postcodeanywhere.co.uk/inline.aspx?";
		strUrl += "&action=fetch";
		strUrl += "&id=" + escape(address_id);
		strUrl += "&account_code=" + escape(account_code);
		strUrl += "&license_code=" + escape(license_code);
		strUrl += "&machine_id=" + escape(machine_id);
		strUrl += "&callback=pcaFetchEnd";

		if( $("#pcaScriptTag") ){
			$("#pcaScriptTag").remove();
		}
		
		scriptTag = document.createElement("script");
		scriptTag.src = strUrl
		scriptTag.type = "text/javascript";
		scriptTag.id = "pcaScriptTag";		
		
		$("head").append(scriptTag);
		
		$("#PCA_selectAddress").hide();
		$("#PCA_Fetch").hide();								 
								 
	});
	
}

function pcaByPostcodeEnd(){


	$("#divLoading").hide();
		
	//Test for an error
	if( pcaIsError ){
		//Show the error message
		$("#PCA_selectAddress").hide();
		$("#PCA_Fetch").hide();
		alert(pcaErrorMessage);
	}else{
		//Check if there were any items found
		if (pcaRecordCount==0){
			$("#PCA_selectAddress").hide();
			$("#PCA_Fetch").hide();
			alert("Sorry, no matching items found. Please try another postcode.");
		}else{
			$("#PCA_selectAddress").show();
			$("#PCA_Fetch").show();				
			$('#PCA_selectAddress option').remove();
			
			// console.log(pca_id);
			// console.log(pca_description);

			for (i=0; i<pca_id.length; i++){
				$('#PCA_selectAddress').append('<option value="'+pca_id[i]+'">'+pca_description[i]+'</option>');
			}
		}
	}

}

function pcaFetchEnd(){
	//Test for an error
	if( pcaIsError ){
		//Show the error message
		alert(pcaErrorMessage);
	}else{
		//Check if there were any items found
		if (pcaRecordCount==0){
			alert("Sorry, no matching items found");
		}else{
			PCAform.find(".PCA_AddressLine1").val(pca_line1[0]);
			PCAform.find(".PCA_AddressLine2").val(pca_line2[0]);
			PCAform.find(".PCA_AddressLine2").val(pca_line3[0]);
			PCAform.find(".PCA_TownCity").val(pca_post_town[0]);
			PCAform.find(".PCA_County").val(pca_county[0]);
			
			PCAform.find(".PCA_AddressLine4").val(pca_line4[0]);
			PCAform.find(".PCA_AddressLine5").val(pca_line5[0]);			
			PCAform.find(".PCA_companyName").val(pca_organisation_name[0]);
			PCAform.find(".PCA_postcode").val(pca_postcode[0]);
						
			if (pca_line1[0] == ""){
				PCAform.find(".PCA_AddressLine1").val(pca_organisation_name[0]);
			}
			
			PCAform.find(".PCA_Country").val('United Kingdom');
			
		}
	}
}
