//To make this sample work - you need a Google Map license key, a Postcode Anywhere account and Postcode Anywhere license key: 
//1. Drop the Google Map license key at the top of the page in the script include for Google.
//2. Register for a Postcode Anywhere account at www.postcodeanywhere.co.uk/register/
//3. Create a new location list: 
//		- Click on "My Account"
//		- Click on "Location Lists" then "Add new"
//		- Give the list a name (e.g. "Stores") and select "Pre-paid" as the type then click on "Next"
//		- Either upload a list of stores or enter them in the input boxes on the next page.
//4. Go back to "My Account" and click on "Setup my account"
//5. Select "Create a store locater".
//6. Pick the new location list you created, run through the wizard and enter the license key it creates below.

var CONST_ACCOUNT = "ELVIL11112"
var CONST_LICENSE = "GH93-JY15-BP56-XD17"

var pcaIcon = new GIcon();
var pcaMarkers = new Array();
var pcaMap = new GMap(document.getElementById("pcaDivMap"));

//Add the nav control to the map
pcaMap.addControl(new GLargeMapControl());

//This is the pin that is used on the map
//By default we draw it from our servers as you can upload an icon against your location list
//It can be any URL if you prefer though
pcaIcon.image = "http://services.postcodeanywhere.co.uk/popups/pin.aspx?account_code=" + CONST_ACCOUNT + "&license_key=" + CONST_LICENSE + "&ex=.png";
pcaIcon.iconSize = new GSize(12, 20);
pcaIcon.shadowSize = new GSize(22, 20);
pcaIcon.iconAnchor = new GPoint(6, 20);
pcaIcon.infoWindowAnchor = new GPoint(5, 1);		

//This is called when you click on the "Find" button
function pcaStartSearch()
	{
		pcaByLocationBegin(document.getElementById("txtLocation").value, CONST_ACCOUNT, CONST_LICENSE, "");
	}
	
function pcaByLocationBegin(location, account_code, license_code, machine_id)
	 {
			var scriptTag = document.getElementById("pcaScript");
			var headTag = document.getElementsByTagName("head").item(0);
			var strUrl = "";

		//ByLocation gets a list of locations that match the search. If it's a postcode, only 1 item is listed
		//but if the user enters a town name, there may be more than 1 matching location. These are then
		//listed for the user to select.

			//Clear the list
			document.getElementById("pcaLstLocations").length=0;

			//Build the url
			strUrl = "http://services.postcodeanywhere.co.uk/inline.aspx?";
			strUrl += "&action=lookup";
			strUrl += "&type=by_location";
			strUrl += "&location=" + escape(location);
			strUrl += "&account_code=" + escape(account_code);
			strUrl += "&license_code=" + escape(license_code);
			strUrl += "&machine_id=" + escape(machine_id);
			strUrl += "&callback=pcaByLocationEnd";

			//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 = "pcaScript";
			headTag.appendChild(scriptTag);
	 }

function pcaByLocationEnd()
	 {
	 
			//This is called when the ByLocation method returns a value. 
	 
			//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
							 {
									//Populate the select list
									document.getElementById("pcaLstLocations").length=pcaRecordCount;
									for (intCounter=0; intCounter < pcaRecordCount; intCounter++)
					{
						document.getElementById("pcaLstLocations").options[intCounter].text = pca_description[intCounter];
						document.getElementById("pcaLstLocations").options[intCounter].value = pca_id[intCounter];
					}
					//Show it if there are items in it, otherwise redirect
					if (pcaRecordCount==1)
					{
						//Because only 1 item was found, we can automatically start get the nearest stores. In this case
						//we just get the nearest 20. 
							pcaStoredNearestBegin(pca_id[0], "", "20", CONST_ACCOUNT, CONST_LICENSE, "");
					}
					else
					{
						document.getElementById("pcaDivLocations").style.display="";
					}
							 }
				 }
	 }
	 
function pcaDoSelect()
	{
		//This is called when the user click on the location. The search gets the nearest 20 stores.
		pcaStoredNearestBegin(document.getElementById("pcaLstLocations").value, "", "20", CONST_ACCOUNT, CONST_LICENSE, "");
	}

function pcaStoredNearestBegin(origin, units, items, account_code, license_code, machine_id)
	 {
			
			var scriptTag = document.getElementById("pcaScript");
			var headTag = document.getElementsByTagName("head").item(0);
			var strUrl = "";
		
		//This method gets the stores nearest to the location.
		
			//Clear the list, the map's pins and store list
			document.getElementById("pcaLstDistances").length=0;
			pcaMap.clearOverlays;
			
			//Build the url
			strUrl = "http://services.postcodeanywhere.co.uk/inline.aspx?";
			strUrl += "&action=stored_nearest";
			strUrl += "&origin=" + escape(origin);
			strUrl += "&units=" + escape(units);
			strUrl += "&items=" + escape(items);
			strUrl += "&account_code=" + escape(account_code);
			strUrl += "&license_code=" + escape(license_code);
			strUrl += "&machine_id=" + escape(machine_id);
			strUrl += "&callback=pcaStoredNearestEnd";

			//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 = "pcaScript";
			headTag.appendChild(scriptTag);
	 }

function pcaStoredNearestEnd()
	 {
	 
			//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
							 {

				//Hide the locations list
				document.getElementById("pcaDivLocations").style.display="none";

				//Centre the map on the nearest item
				pcaMap.centerAndZoom(new GLatLng(pca_wgs84_latitude[0], pca_wgs84_longitude[0]),5);
				
				//Reset the pcaMarkers list
				pcaMarkers.length = pcaRecordCount;
				
				//Add the pins
				for (i=0;  i<pcaRecordCount; i++)
					{
						
						//Get a point for this location
						var point = new GLatLng(pca_wgs84_latitude[i], pca_wgs84_longitude[i]);    
														
						//Generate the text for the marker
						var strLabel = "";
						strLabel = "<b>" + pca_name[i] + "</b><br>" + pca_description[i] + "<br><br>" + "Distance: " + pca_distance[i] + " miles";
							
						//Add a marker for the locations
						var marker = pcaCreateMarker(point, strLabel, i);
						pcaMarkers[i] = marker;									
						pcaMap.addOverlay(marker);
							
					}
						
									//Populate the select list of stores
									document.getElementById("pcaLstDistances").length=pcaRecordCount;
									for (intCounter=0; intCounter < pcaRecordCount; intCounter++)
					{
						document.getElementById("pcaLstDistances").options[intCounter].text = pca_distance[intCounter] + " miles - " + pca_name[intCounter];
						document.getElementById("pcaLstDistances").options[intCounter].value = intCounter;
					}
					
					 //Show the list of stores
					 document.getElementById("pcaDivDistances").style.display="";
				
				}
				 }
	 }

function pcaReCentre()
	{
		
		//Move the map
		pcaMap.panTo(new GLatLng(pca_wgs84_latitude[document.getElementById("pcaLstDistances").value], pca_wgs84_longitude[document.getElementById("pcaLstDistances").value]));
		
		//Generate the text for the marker
		var strLabel = "";
		strLabel = "<b>" + pca_name[document.getElementById("pcaLstDistances").value] + "</b><br>" + pca_description[document.getElementById("pcaLstDistances").value] + "<br><br>" + "Distance: " + pca_distance[document.getElementById("pcaLstDistances").value] + " miles";
		
		//Open the marker
		pcaMarkers[document.getElementById("pcaLstDistances").value].openInfoWindowHtml(strLabel);
		
	}
	 

function pcaCreateMarker(point, info, number) 
{

var marker = new GMarker(point,pcaIcon);

GEvent.addListener(marker, "click", function() {
marker.openInfoWindowHtml(info);
});

return marker;
}

pcaMap.centerAndZoom(new GLatLng(55, -2),12);