
var geocoder;

function initialize() {
    geocoder = new GClientGeocoder();
}

// getLatLngFromAddress() is called when the geocoder returns an answer. 
function getLatLngFromAddress(response) {

	if (!response || response.Status.code != 200) 
	{

	  	document.getElementById("postcode").value="";
		document.getElementById("location").value="";
		document.getElementById("postcode").focus();
		document.getElementById("cLat").value = ""; 
		document.getElementById("cLong").value = "";
		document.getElementById("fetchingpostcode").className = "incomplete";
		document.getElementById("resultsArea").innerHTML = "&nbsp;";
		alert("Sorry, we were unable to find that postcode/location - please try again.\nEnsure postcode contains a space or enter just the first part of the postcode");
		
    } 
	//else if (document.getElementById("postcode").value != "")
	else if (document.getElementById("postcode").value != "" || document.getElementById("location").value != "")
	{
        place = response.Placemark[0];
        point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
		document.getElementById("cLat").value = point.lat(); 
		document.getElementById("cLong").value = point.lng();
		document.getElementById("fetchingpostcode").className = "completed";
		//attempt to hide form selects from IE6 until mask is removed
		document.getElementsByTagName("select").className = "show";
    }
}

// getLocation() is called when you click on the button/image
// in the form.  It geocodes the location and/or postcode entered into the form
function getLocation() {
    var address = document.getElementById("location").value + " " 
	address    += document.getElementById("postcode").value + " " 
	address    += document.getElementById("country_code").value;
	geocoder.getLocations(address, getLatLngFromAddress);

}
