var map;
var gdir;
var geocoder = null;
var addressMarker;

function checkEmpty()
{
	var custAddress = document.getElementById("fromAddress");
	if (custAddress.value == "")
	{
		alert("Please enter your address in the format 'Road Name, City' \n e.g. 'Hallen Close, Bristol'");
		exit();
		return false;
	}
	else
	{
		return true;
	}
}

function initialize() 
{
	if (GBrowserIsCompatible()) 
	{      
		map = new GMap2(document.getElementById("map_canvas"));
		gdir = new GDirections(map, document.getElementById("directions"));
		GEvent.addListener(gdir, "load", onGDirectionsLoad);
		GEvent.addListener(gdir, "error", handleErrors);
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		hallenClose = new GLatLng(51.486378, -2.474218);
		setCenter(hallenClose, 13);
	 }
}

function setDirections(fromAddress, toAddress, locale) 
{
	gdir.load("from: " + fromAddress + " to: " + toAddress,{ "locale": "en_UK" });
}

function reCentre()
{
	pcaGeocodePostcodeBegin(document.getElementById("txtPostcode").value, "", "", "AAAAA11111", "AA11-AA11-AA11-AA11")
}
		
function pcaGeocodePostcodeBegin(postcode, building, accuracy, account_code, license_code)
{
	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=geocode";
	strUrl += "&postcode=" + escape(postcode);
	strUrl += "&building=" + escape(building);
	strUrl += "&accuracy=" + escape(accuracy);
	strUrl += "&account_code=" + escape(account_code);
	strUrl += "&license_code=" + escape(license_code);
	strUrl += "&callback=pcaGeocodePostcodeEnd";

	//Make the request
	if (scriptTag) 
	{
		headTag.removeChild(scriptTag);
	}
		scriptTag = document.createElement("script");
		scriptTag.src = strUrl
		scriptTag.type = "text/javascript";
		scriptTag.id = "pcaScript";
		headTag.appendChild(scriptTag);
	}

	function pcaGeocodePostcodeEnd()
	{
		//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
		{
		
			//PUT YOUR CODE HERE
			var point = new GPoint(pca_wgs84_longitude[0], pca_wgs84_latitude[0]);    
		
			//Centre the map
			map.centerAndZoom(point,1);
		
			//Place the marker
			var marker = new GMarker(point);
			GEvent.addListener(marker, 'click', function() 
			{
				marker.openInfoWindowHtml(pca_location[0]);
			});
			map.addOverlay(marker);
		
		}
	}
}

function handleErrors()
{
	if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
	{
		alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
	}
	else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
	{
		alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
	}
	else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
	{
		alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);
	}

//   else if (gdir.getStatus().code == G_UNAVAILABLE_ADDRESS)  <--- Doc bug... this is either not defined, or Doc is wrong
//     alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + gdir.getStatus().code);
 
	else if (gdir.getStatus().code == G_GEO_BAD_KEY)
	{
		alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);
	}
	else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
	{
		alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
	}
	else 
	{
		alert("An unknown error occurred.");
	}		
}

function onGDirectionsLoad()
{ 
	returnJourney = Math.round(parseFloat(gdir.getDistance().html)*Math.pow(10,0))/Math.pow(10,0)*2;
	if (returnJourney <= 14)
	{
		document.getElementById("calloutFee").innerHTML = "There is No Callout Fee to this area!";
	}
	else if (returnJourney >= 14 && returnJourney <=60) 
	{
	 	calloutFee = returnJourney*0.45;
		document.getElementById("calloutFee").innerHTML = "Callout Fee: \u00A3" + (Math.round(calloutFee*Math.pow(10,2))/Math.pow(10,2));
	}
	else if (returnJourney >= 60 && returnJourney <=100) 
	{
	 	calloutFee = returnJourney*0.65;
		document.getElementById("calloutFee").innerHTML = "Callout Fee: \u00A3" + (Math.round(calloutFee*Math.pow(10,2))/Math.pow(10,2)+5);
	}
	else if (returnJourney >= 100) 
	{
		document.getElementById("calloutFee").innerHTML = "You are more than 50 miles from our office. Please contact us for more details";
	}
	else 
	{
		document.getElementById("calloutFee").innerHTML = "Error - Please amend your address";
	}
}