// ====== Geocoding ======

function showAddress() {

		var map = new GMap(document.getElementById("map"));
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.setCenter(new GLatLng(40.742217, -74.004636), 12);


//	alert("ding");
// ====== Create a Client Geocoder ======
	var geo = new GClientGeocoder();
// ====== Array for decoding the failure codes ======
	var reasons=[];
	reasons[G_GEO_SUCCESS] = "Success";
	reasons[G_GEO_MISSING_ADDRESS] = "Missing Address: The address was either missing or had no value.";
	reasons[G_GEO_UNKNOWN_ADDRESS] = "Unknown Address:  No corresponding geographic location could be found for the specified address.";
	reasons[G_GEO_UNAVAILABLE_ADDRESS] = "Unavailable Address:  The geocode for the given address cannot be returned due to legal or contractual reasons.";
	reasons[G_GEO_BAD_KEY] = "Bad Key: The API key is either invalid or does not match the domain for which it was given";
	reasons[G_GEO_TOO_MANY_QUERIES] = "Too Many Queries: The daily geocoding quota for this site has been exceeded.";
	reasons[G_GEO_SERVER_ERROR] = "Server error: The geocoding request could not be successfully processed.";
	var search = document.getElementById("search").value;

// ====== Perform the Geocoding ======
	geo.getLocations(search, function (result)
		{
// If that was successful
			if (result.Status.code == G_GEO_SUCCESS) {
// How many resuts were found
				if (result.Placemark.length==1) {
					sResultPlural = ""
				} else {
					sResultPlural = "s"
				}

				document.getElementById("message").innerHTML = "Found " +result.Placemark.length +" result"+sResultPlural+" -- click the address to add it to your submission.<br>";
// Loop through the results, placing markers
				document.getElementById("message").innerHTML += "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\">";
				for (var i=0; i<result.Placemark.length; i++) {
					document.getElementById("message").innerHTML += "<tr>";
					var p = result.Placemark[i].Point.coordinates;
					var marker = new GMarker(new GLatLng(p[1],p[0]));
					document.getElementById("message").innerHTML += "<td valign=\"top\">&#149;&nbsp;<a id = 'address"+ i +"' name = 'address" + i + "'href='#' onclick='displayOnForm(\"address" + i + "\", \"" + result.Placemark[i].address + "\"); addToForm(\"" + marker.getPoint() + "\", \"" + result.Placemark[i].address + "\");'>"+ result.Placemark[i].address + "<\/a><\/td>";
					map.addOverlay(marker);
					document.getElementById("message").innerHTML += "</tr>";
				}
				document.getElementById("message").innerHTML += "</table>";
// centre the map on the first result
				var p = result.Placemark[0].Point.coordinates;
				map.setCenter(new GLatLng(p[1],p[0]),14);
			} else {
// ====== Decode the error status ======
				var reason="Code "+result.Status.code;
				if (reasons[result.Status.code]) {
					reason = reasons[result.Status.code]
				}
			alert("Could not find "+search+ " " + reason);
			}
		}
	);
}

function displayOnForm(address, addressLocal){
	//alert(addressLocal);
	
	document.getElementById(address).className = 'selected';
	document.getElementById('addressFORM').value = addressLocal;
	document.getElementById('submitBtn').style.display = 'block';
	//placeIcon();
	
}
