/***** ADDRESS FINDER ******/		
var account_code='YOUDO11111';
var license_code='UW38-HK19-WZ67-ZC18';

function pcaByPostcodeBegin(model)
{
	var postcode = document.getElementById(model+"Postcode").value;
	var scriptTag = document.getElementById("pcaScriptTag");
	var headTag = document.getElementsByTagName("head").item(0);
	var strUrl = "";
	
	document.getElementById(model+"divLoading").style.display = '';
	
	//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 += "&callback=pcaByPostcodeEnd"+model;
	
	//Make the request
	if (scriptTag)
	{
		headTag.removeChild(scriptTag);
	}
	scriptTag = document.createElement("script");
	scriptTag.src = strUrl
	scriptTag.type = "text/javascript";
	scriptTag.id = "pcaScriptTag";
	headTag.appendChild(scriptTag);
	
	document.getElementById(model+"AddressSelect").style.display = '';
	document.getElementById(model+"SelectAddress").style.display = '';
	document.getElementById(model+"btnFetch").style.display = '';
}

function pcaByPostcodeEndUser()
{
	document.getElementById("UserdivLoading").style.display = 'none';
	
	//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
		{
			for (i=document.getElementById("UserSelectAddress").options.length-1; i>=0; i--){
				document.getElementById("UserSelectAddress").options[i] = null;
			}
			for (i=0; i<pca_id.length; i++){
				document.getElementById("UserSelectAddress").options[document.getElementById("UserSelectAddress").length] = new Option(pca_description[i], pca_id[i]);
			}
		}
	}
}


function pcaByPostcodeEndProperty()
{
	document.getElementById("PropertydivLoading").style.display = 'none';
	
	//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
		{
			for (i=document.getElementById("PropertySelectAddress").options.length-1; i>=0; i--){
				document.getElementById("PropertySelectAddress").options[i] = null;
			}
			for (i=0; i<pca_id.length; i++){
				document.getElementById("PropertySelectAddress").options[document.getElementById("PropertySelectAddress").length] = new Option(pca_description[i], pca_id[i]);
			}
		}
	}
}

function pcaFetchBegin(model)
{
	var address_id = document.getElementById(model+"SelectAddress").value;
	var scriptTag = document.getElementById("pcaScriptTag");
	var headTag = document.getElementsByTagName("head").item(0);
	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 += "&style=raw";
	strUrl += "&callback=pcaFetchEnd"+model;
	
	//Make the request
	if (scriptTag)
	{
		headTag.removeChild(scriptTag);
	}
	scriptTag = document.createElement("script");
	scriptTag.src = strUrl
	scriptTag.type = "text/javascript";
	scriptTag.id = "pcaScriptTag";
	headTag.appendChild(scriptTag);
	
	document.getElementById(model+"AddressSelect").style.display = 'none';
	document.getElementById(model+"SelectAddress").style.display = 'none';
	document.getElementById(model+"btnFetch").style.display = 'none';
}

function pcaFetchEndUser()
{
	//Test for an error
	if (pcaIsError)
	{
		//Show the error message
		//alert("Sorry but you've used today's free credits. Please call us if you'd like more.");
		alert(pcaErrorMessage);
	}
	else
	{
		//Check if there were any items found
		if (pcaRecordCount==0)
		{
			alert("Sorry, no matching items found");
		}
		else
		{
			//document.getElementById("custom_building_number").value = '' + pca_building_number[0];
			if(pca_organisation_name[0] != "")
			{
				document.forms[0]["UserAddress1"].value = '' + pca_organisation_name[0];
				document.forms[0]["UserAddress2"].value = '' + pca_line1[0];
				document.forms[0]["UserAddress3"].value = '' + pca_line2[0];
				document.forms[0]["UserAddress4"].value = '' + pca_line3[0];
				document.forms[0]["UserAddress5"].value = '' + pca_line4[0];
			}
			else
			{
				document.forms[0]["UserAddress1"].value = '' + pca_line1[0];
				document.forms[0]["UserAddress2"].value = '' + pca_line2[0];
				document.forms[0]["UserAddress3"].value = '' + pca_line3[0];
				document.forms[0]["UserAddress4"].value = '' + pca_line4[0];
				document.forms[0]["UserAddress5"].value = '' + pca_line5[0];
			}
			
			document.forms[0]["UserTown"].value = '' + pca_post_town[0];
			//document.forms[0]["UserCounty"].value = '' + pca_county[0];
			document.forms[0]["UserPostcode"].value = '' + pca_postcode[0];
			/*if (pca_building_flat[0]!='')
			{
				document.forms[0]["flat"].value = '' + pca_building_flat[0];
			}
			else
			{
				document.forms[0]["flat"].value = '' + pca_sub_building_name[0];
			}*/
			document.forms[0]["UserBuildingNum"].value = '' + pca_building_number[0];
			document.forms[0]["UserBuildingName"].value = '' + pca_building_name[0];
			document.forms[0]["UserStreet"].value = '' + pca_thoroughfare_name[0] + ' ' + pca_thoroughfare_descriptor[0];
		}
	}
}

function pcaFetchEndProperty()
{
	//Test for an error
	if (pcaIsError)
	{
		//Show the error message
		//alert("Sorry but you've used today's free credits. Please call us if you'd like more.");
		alert(pcaErrorMessage);
	}
	else
	{
		//Check if there were any items found
		if (pcaRecordCount==0)
		{
			alert("Sorry, no matching items found");
		}
		else
		{
			//document.getElementById("custom_building_number").value = '' + pca_building_number[0];
			if(pca_organisation_name[0] != "")
			{
				document.forms[0]["PropertyAddress1"].value = '' + pca_organisation_name[0];
				document.forms[0]["PropertyAddress2"].value = '' + pca_line1[0];
				document.forms[0]["PropertyAddress3"].value = '' + pca_line2[0];
				document.forms[0]["PropertyAddress4"].value = '' + pca_line3[0];
				document.forms[0]["PropertyAddress5"].value = '' + pca_line4[0];
			}
			else
			{
				document.forms[0]["PropertyAddress1"].value = '' + pca_line1[0];
				document.forms[0]["PropertyAddress2"].value = '' + pca_line2[0];
				document.forms[0]["PropertyAddress3"].value = '' + pca_line3[0];
				document.forms[0]["PropertyAddress4"].value = '' + pca_line4[0];
				document.forms[0]["PropertyAddress5"].value = '' + pca_line5[0];
			}
			
			document.forms[0]["PropertyTown"].value = '' + pca_post_town[0];
			//document.forms[0]["PropertyCounty"].value = '' + pca_county[0];
			document.forms[0]["PropertyPostcode"].value = '' + pca_postcode[0];
			/*if (pca_building_flat[0]!='')
			{
				document.forms[0]["flat"].value = '' + pca_building_flat[0];
			}
			else
			{
				document.forms[0]["flat"].value = '' + pca_sub_building_name[0];
			}*/
			document.forms[0]["PropertyBuildingNum"].value = '' + pca_building_number[0];
			document.forms[0]["PropertyBuildingName"].value = '' + pca_building_name[0];
			document.forms[0]["PropertyStreet"].value = '' + pca_thoroughfare_name[0] + ' ' + pca_thoroughfare_descriptor[0];
		}
	}
}

/***** END ADDRESS FINDER ******/
