
function getDirections() {
	var saddr = document.getElementById("saddr").value;
	var daddr = document.getElementById("daddr").value;
	map.closeInfoWindow();
	
	for(i=0;i<gmarkers.length;i++){
		map.removeOverlay(gmarkers[i]);
	}
//	document.getElementById("data").className = "hiddenDiv"
	document.getElementById("container").className = "hiddenDiv"
//	document.getElementById("titleDiv").className = "hiddenDiv"
//	document.getElementById("captionDiv").className = "hiddenDiv"
//	document.getElementById("instrDiv").className = "hiddenDiv"
//	document.getElementById("divFooter").className = "hiddenDiv"
	document.getElementById("directions").className = "whitebg"
	
	gdir.load("from: "+saddr+" to: "+daddr);
}

// functions that open the directions forms
function tohere(i) {
	gmarkers[i].openInfoWindowHtml(to_htmls[i]);
}

function fromhere(i) {
	gmarkers[i].openInfoWindowHtml(from_htmls[i]);
}

function formatHtml(name, blurb, address, lat, lon) {

	var i = gmarkers.length;

	to_htmls[i] = blurb + '<br>Directions: <b>To here<\/b> - <a href="javascript:fromhere(' + i + ')">From here<\/a>' +
	   '<br>Start address:<form action="javascript:getDirections()">' +
	   '<input type="text" SIZE=40 MAXLENGTH=40 name="saddr" id="saddr" value="" /><br>' +
	   '<INPUT value="Get Directions" TYPE="SUBMIT"><br>' +
	   '<input type="hidden" id="daddr" value="'+  name  +"@"+ lat + ',' + lon + '"/>';

	from_htmls[i] = blurb + '<br>Directions: <a href="javascript:tohere(' + i + ')">To here<\/a> - <b>From here<\/b>' +
	   '<br>End address:<form action="javascript:getDirections()">' +
	   '<input type="text" SIZE=40 MAXLENGTH=40 name="daddr" id="daddr" value="" /><br>' +
	   '<INPUT value="Get Directions" TYPE="SUBMIT">' +
	   '<input type="hidden" id="saddr" value="'+ name +"@"+ lat + ',' + lon + '"/>';

	theHtml =  '<div class="blurb">' 
				+ blurb 
				+ '</div>\n<div class="address">' 
				+ address + '</div><br>Directions: <a href="javascript:tohere('+i+')">To here<\/a> - <a href="javascript:fromhere('+i+')">From here<\/a>';
	
	return theHtml;
}

// set up a new marker
function addMarker(lat, lon, html){
	var marker = new GMarker(new GLatLng(lat, lon));
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(html);
	});
	gmarkers.push(marker);
	htmls.push(html);
	return marker;
}
	
// handle clicks from the listing:
function click(i){
	gmarkers[i].openInfoWindowHtml(htmls[i]);
}

// === 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.";
reasons[G_GEO_BAD_REQUEST]        = "A directions request could not be successfully parsed.";
reasons[G_GEO_MISSING_QUERY]      = "No query was specified in the input.";
reasons[G_GEO_UNKNOWN_DIRECTIONS] = "The GDirections object could not compute directions between the points.";

GEvent.addListener(gdir, "error", function() {
	var code = gdir.getStatus().code;
	var reason="Code "+code;
	if (reasons[code]) {
		reason = reasons[code]
	} 

	alert("Failed to obtain directions, "+reason);
});
  
