function getIconFilename(selector) {
	var httpRequest = makeHTTPRequest();

	httpRequest.open("POST", "http://" + serverName + "/vamos/admins/ajaxfunctions.php", false);
	httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	
	var encoded = 'function=getMapIconInfo' +
					'&id=' + encodeURIComponent(selector.value);

	httpRequest.send(encoded);
	
	/*
	<FunctionResult>
		<FunctionStatus success="1" message="Request processed" />
		<MapIconInfo dbID="1" url="http://maps.google.com/mapfiles/ms/micons/red-dot.png"
						nickname="Google Red Pin" localFilename="red-dot.png" anchorAtCenter="1" />
	</FunctionResult>
	*/
	if (httpRequest.status == 200) {
		var xml = httpRequest.responseXML;
		if(xml) {
			var functionResultsGroup = xml.getElementsByTagName("FunctionResult");
			if(functionResultsGroup.length > 0) {
				var functionResults = functionResultsGroup[0];
				var functionStatusGroup = functionResults.getElementsByTagName("FunctionStatus");
				if(functionStatusGroup.length > 0) {
					var functionStatus = functionStatusGroup[0];
				
					var success = functionStatus.attributes.getNamedItem("success").value;
					var message = stripslashes(functionStatus.attributes.getNamedItem("message").value);
					if(success == 1) {
						var mapIconInfoGroup = functionResults.getElementsByTagName('MapIconInfo');

						if(mapIconInfoGroup.length > 0) {
							var mapIconInfo = mapIconInfoGroup[0];
							var localFilename = stripslashes(mapIconInfo.attributes.getNamedItem('localFilename').value);
 							return localFilename;
						} else {
							error_log("Empty MapIconInfo node.");
							alert("Empty MapIconInfo node.");
						}
					} else {
						error_log("Failed; server response is " + message);
						alert("Failed; server response is " + message);
					}
				} else {
					error_log("Empty FunctionStatus node.");
					alert("Empty FunctionStatus node.");
				}
			} else {
				error_log("Empty FunctionResults node.");
				alert("Empty FunctionResults node.");
			}
		} else {
			error_log("Empty response from server.");
			alert("Empty response from server.");
		}
	} else {
		error_log("There was a communications error: " + htmlentities(stripslashes(httpRequest.responseText)));
		alert("There was a communications error: " + stripslashes(httpRequest.responseText));
	}
	
	return null;
}

function getIconFromIconsArray(iconID) {
	for(var i in iconsArray) {
		if(iconsArray[i].mDBID == iconID)
			return iconsArray[i];
	}
	
	return new cIcon(1, 'red-dot.png', false);
}

function loadRoute(startID, endID, forAdmin) {
	var httpRequest = makeHTTPRequest();

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

	var encoded = 'startID=' + startID + '&endID=' + endID;

	httpRequest.send(encoded);

	if (httpRequest.status == 200) {
		var xml = httpRequest.responseXML;
		if(xml) {
			var loadResults = xml.getElementsByTagName("LoadResult");
			if(loadResults.length > 0) {
				var loadStatuses = loadResults[0].getElementsByTagName("LoadStatus");
				if(loadStatuses.length > 0) {
					var loadStatus = loadStatuses[0];
				
					var success = loadStatus.attributes.getNamedItem("success").value;
					var message = stripslashes(loadStatus.attributes.getNamedItem("message").value);
					if(success == 1) {
						var vertexCount = loadStatus.attributes.getNamedItem("vertexCount").value;
						if(vertexCount > 0) {
							var vertices = loadResults[0].getElementsByTagName("Vertex");
							for(var i = 0; i < vertexCount; ++i) {
								var vertexInfo = vertices[i];
						
								var latitude = vertexInfo.attributes.getNamedItem("latitude").value;
								var longitude = vertexInfo.attributes.getNamedItem("longitude").value;
								var markerHTMLAttribute = vertexInfo.attributes.getNamedItem("html");
								var markerHTML = ((markerHTMLAttribute == null) ?
													'' : stripslashes(markerHTMLAttribute.value));
													
								// Don't show the admin-style markers if we're in non-admin mode; instead, leave
								// the start/finish markers that we already showed when the user clicked on those
								// cities.
								var isVisible = forAdmin ?
												intToBool(vertexInfo.attributes.getNamedItem("isVisible").value) :
												false;
												
								var isWaypoint = intToBool(vertexInfo.attributes.getNamedItem("isWaypoint").value);
								var isRouteLinePoint = intToBool(vertexInfo.attributes.getNamedItem("isRouteLinePoint").value);
								var isFromClick = intToBool(vertexInfo.attributes.getNamedItem("isFromClick").value);

								var latlng = new GLatLng(latitude, longitude);
								
								// Note that we're telling insertRoutePoint() not to draw the route; we want to
								// wait until all of the vertices are in place first.
								theRoute.insertRoutePoint(latlng, markerHTML, isVisible, isWaypoint, isRouteLinePoint,
															isFromClick, false);
							}
							
							overriddenRouteLength = loadStatus.attributes.getNamedItem("routeLength").value;
							if(overriddenRouteLength == 0)
								overriddenRouteLength = 'unspecified';
								
							drivingTime = loadStatus.attributes.getNamedItem("drivingTime").value;
								
							theRoute.renewRouteLine();	// Now we draw the route
						} else {
							return false;
						}

						// Load the markers after the route, because in non-admin mode we want to leave out the
						// start and end markers.
						var markersCount = loadStatus.attributes.getNamedItem("markersCount").value;
						if(markersCount > 0) {
							var markers = loadResults[0].getElementsByTagName("Marker");
							
							for(var i = 0; i < markersCount; ++i) {
								var markerInfo = markers[i];

								var latitude = markerInfo.attributes.getNamedItem("latitude").value;
								var longitude = markerInfo.attributes.getNamedItem("longitude").value;
								var latlng = new GLatLng(latitude, longitude);
								
								var iconID = markerInfo.attributes.getNamedItem("iconID").value;
								var markerHTMLAttribute = markerInfo.attributes.getNamedItem("html");
								var markerHTML = ((markerHTMLAttribute == null) ?
													'' : stripslashes(markerHTMLAttribute.value));
												
								var icon = getIconFromIconsArray(iconID);

								var openHandler = (forAdmin ? handleInfoWindowOpen : null);
								var closeHandler = (forAdmin ? handleInfoWindowClose : null);
								var announceMarker = forAdmin;	// User's don't need auto-announced
								theRoute.addMarker(latlng, openHandler, closeHandler, icon, markerHTML, announceMarker);
							}
						}
					} else {
						error_log('Could not load the route; server says ' + message);
						alert('Could not load the route; server says ' + message);
					}
				} else {
					error_log('Could not load the route; no load status available.');
					alert('Could not load the route; no load status available.');
				}
				return true;
			}
		}
	}

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

function setIconDisplay(selector) {
	var iconFilename = getIconFilename(selector);
	var fullPath = "http://" + serverName + "/vamos/images/mapicons/" + iconFilename;
	document.getElementById('mapIconDisplay').setAttribute('src', fullPath);
}
