var theRoute = null;
var overriddenRouteLength;
var drivingTime = 0;

function getDestination(dbID) {
	var httpRequest = makeHTTPRequest();

	httpRequest.open("POST", "http://" + serverName + "/admin/ajaxfunctions.php", false);
	httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

	var encoded = 'function=getDestination&dbID=' + dbID;

	httpRequest.send(encoded);
	
	/*
	<FunctionResult>
		<FunctionStatus success="1" message="Request processed" />
		<Destination dbID="12" latitude="9.5478" longitude="-89.7328" name="location name" address="location address"
						city="city name" notes="notes" />
	</FunctionResult>
	*/
	
	if (httpRequest.status == 200) {
		var xml = httpRequest.responseXML;
		if(xml) {
			var functionResultTags = xml.getElementsByTagName('FunctionResult');
			if(functionResultTags.length > 0) {
				var functionResult = functionResultTags[0];
				
				var functionStatusTags = functionResult.getElementsByTagName('FunctionStatus');
				if(functionStatusTags.length > 0) {
					var functionStatus = functionStatusTags[0];

					var success = functionStatus.attributes.getNamedItem("success").value;
					var message = stripslashes(functionStatus.attributes.getNamedItem("message").value);

					if(success == 1) {
						var destinationTags = functionResult.getElementsByTagName('Destination');
						if(destinationTags.length > 0) {
							var destination = destinationTags[0];

							var name = stripslashes(destination.attributes.getNamedItem("name").value);
							var address = stripslashes(destination.attributes.getNamedItem("address").value);
							var city = stripslashes(destination.attributes.getNamedItem("city").value);
							var latitude = destination.attributes.getNamedItem("latitude").value;
							var longitude = destination.attributes.getNamedItem("longitude").value;
							var notes = stripslashes(destination.attributes.getNamedItem("notes").value);
							var dbID = destination.attributes.getNamedItem("dbID").value;

							return new cDestination(theMap, name, address, city, longitude, latitude, notes, dbID);
						}
					}
				}
			}
		}
	}

	error_log("There was a communications error: " + httpRequest.responseText);
	alert("There was a communications error: " + httpRequest.responseText);
	
	return false;
}

function showRoute() {
	if(theRoute == null) {
		theRoute = new cV4x4Route(theMap);
	} else {
		theRoute.restartAll();	// Resets the route object entirely, including erasing the visible line from the map
	}

	startID = document.getElementById('start').value;
	endID = document.getElementById('end').value;
	
	overriddenRouteLength = 'unspecified';

	var routeExists = loadRoute(startID, endID, false);

	showRouteLength();
}

function showRouteLength() {
	//var totalRouteLengthTag = document.getElementById('totalDistance');
	
	// Calculate the total route length and show it
	var routeLengthM = (overriddenRouteLength == 'unspecified') ? theRoute.getLength() : (overriddenRouteLength * 1000);
	var routeHours = parseInt(drivingTime / 60);
	var routeMinutes = (drivingTime - (routeHours * 60));
	if(routeLengthM >= 0) {
		var routeLengthKm = routeLengthM / 1000;
		var routeLengthMi = routeLengthKm * 0.621371192;

		document.getElementById('distance').value = routeLengthKm.toFixed(2) + 'km (' + routeLengthMi.toFixed(2) + 'mi)';
		document.getElementById('traveltime').value = routeHours + 'h ' + routeMinutes + 'm.';
		/* totalRouteLengthTag.innerHTML =
			'Total route length is ' + routeLengthKm.toFixed(2) + 'km (' + routeLengthMi.toFixed(2) + 'mi)' +
			', driving time is ' + routeHours + 'h ' + routeMinutes + 'm.'; */
	} else {
		//totalRouteLengthTag.innerHTML = 'No route has been defined between these two destinations.';
		document.getElementById('distance').value = '';
		document.getElementById('traveltime').value = '';
	}
}

