		// Function to load the customer selection data into the <SELECT ID="Select1" NAME="Select1"> drop list control
		//var addr = document.getElementById("address_ddlAddresses");
		var rtpointsstr;
		function verifyAddress()
		{
			var xmlHttpObj = CreateXmlHttpRequestObject();
			if (xmlHttpObj)
			{
				//var eleAddress = document.getElementById("address_txtAddress");
				var eleAddress = document.getElementById("txtAddress");
				var strAddress = eleAddress.value;
				
				strAddress = strAddress.replace(" & ", " and ");
				//alert(strAddress);
				if(strAddress.length < 5) 
				{				
					return;
				}
				
				// We want this request asynchronous
				xmlHttpObj.open("POST",strBaseURL +"wsProxy/proxy_LocVerifier.asmx/findLocation_all", true);
				xmlHttpObj.onreadystatechange = function()
				{
					if ( xmlHttpObj.readyState == READYSTATE_COMPLETE )
					
					{
						
						if(xmlHttpObj.status == HTTPSTATUS_OK)
						{
							
							//var ctrl = document.getElementById("address_ddlAddresses");
							var ctrl = document.getElementById("ddlAddresses");
							
							var doc = xmlHttpObj.responseXML;
							
							var nodesType = doc.getElementsByTagName("sourceOperation");
							var queryType = nodesType[0].childNodes[0].nodeValue;
							//alert(queryType);
							switch(queryType)
							{
								case "DC Place":
									//var nodesName1 = doc.getElementsByTagName("FULLADDRESS");
									var nodesName1 = doc.getElementsByTagName("ALIASNAME");
									var nodesX = doc.getElementsByTagName("XCOORD");
									var nodesY = doc.getElementsByTagName("YCOORD");
									break;
								case "DC Intersection":
									var nodesName1 = doc.getElementsByTagName("FULLINTERSECTION");
									//var nodesName2 = doc.getElementsByTagName("FULLSTREET2DISPLAY");
									var nodesX = doc.getElementsByTagName("REFX");
									var nodesY = doc.getElementsByTagName("REFY");
									break;
								case "DC Address":  //case "DC AID":
									//var nodesName1 = doc.getElementsByTagName("FULLADDRESS");
									var nodesName1 = doc.getElementsByTagName("FULLADDRESS");
									//var nodesName2 = doc.getElementsByTagName("ALIASNAME");
									//var nodesX = doc.getElementsByTagName("XCOORD");
									var nodesX = doc.getElementsByTagName("XCOORD");
									//var nodesY = doc.getElementsByTagName("YCOORD");
									var nodesY = doc.getElementsByTagName("YCOORD");
									break;
								case "DC Block":
								case "DC Block Address":
									var nodesName1 = doc.getElementsByTagName("FULLBLOCK");
									//var nodesName2 = doc.getElementsByTagName("ALIASNAME");
									var nodesX = doc.getElementsByTagName("CENTROIDX");
									var nodesY = doc.getElementsByTagName("CENTROIDY");
									break;
								
							}
							//clear first
							ctrl.options.length = 0;
							var catName;
							var catId;
							var htmlCode;
							
							
							if(! nodesName1) return;
							
							if(nodesName1.length == 0)
							{
								//alert("No match found, please try another address.");
								var nomatchvalue = document.createElement('option');
								nomatchvalue.value = "-999";
								nomatchvalue.text = "No match found, please correct your address and type again.";
																						
								ctrl.options.add(nomatchvalue);
								return;
							}
							else
							{
								var aryOptions = new Array();
								for (var i=0; i < nodesName1.length; i++)
								{
									var catName = nodesName1[i].childNodes[0].nodeValue;
									var catId = "showX="+nodesX[i].childNodes[0].nodeValue+"&showY="+nodesY[i].childNodes[0].nodeValue+"&Name="+catName;
									aryOptions[i] = catName + "::" + catId;
									
								}
								aryOptions.sort();
								var iLoc;
								for(i=0; i < aryOptions.length; i++)
								{
									var htmlCode = document.createElement('option');
									ctrl.options.add(htmlCode);
									catName = aryOptions[i];
									iLoc = catName.indexOf("::");
									htmlCode.text = catName.substring(0, iLoc);
									htmlCode.value = catName.substring(iLoc+2);
								}
							}
						}
						else
						{
							var fault = xmlHttpObj.responseText;
							alert("Error happened: " + fault);
						}
					}
				}
	            
				xmlHttpObj.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
				xmlHttpObj.setRequestHeader("SOAPAction", "str");
				// Execute the request        
				//alert(strAddress);    
				xmlHttpObj.send("str="+ strAddress);
			}
		}
		function showAddress()
		{
				
				document.getElementById("googletext").innerHTML = "Generating route, please wait....";
				var addr = document.getElementById("ddlAddresses");
				
				var selectValues = "";
				var x;
				var y;
				var htmlcode;
				for(var i=0; i < addr.options.length; i++)
				{
					selectValues += addr.options[i].value + ":";
					if(addr.options[i].selected)
					{
						var addrID = addr.options[addr.selectedIndex].value;
						
						var paras = addrID.split("&");
						x = paras[0].substring(paras[0].indexOf("=")+1);
						y = paras[1].substring(paras[1].indexOf("=")+1);
						selectValues += i + ":";
					}
				}
				
				if(selectValues.length > 0)
				{
					selectValues = selectValues.substring(0, selectValues.length -1);
				}
			
			var ret={};// holds returned caculated values
			
			var llCoords = SPCStoLL(x, y, ret);

			rtpointsstr=ret.lat + "," + ret.lon;
			
			var xmlHttpObj = CreateXmlHttpRequestObject();
			if (xmlHttpObj)
			{
				// We want this request asynchronous
				xmlHttpObj.open("POST",strBaseURL +"wsProxy/proxy_LocVerifier.asmx/getareas", true);

				xmlHttpObj.onreadystatechange = function()
				{
					if ( xmlHttpObj.readyState == READYSTATE_COMPLETE )
					{
						if(xmlHttpObj.status == HTTPSTATUS_OK)
						{
							var doc = xmlHttpObj.responseXML;
							
							var nodeFeatID = doc.getElementsByTagName("FeatureID");
								
							var FeatId;

							if(! nodeFeatID) return;
							
							if(nodeFeatID.length == 0)
							{
								alert("Cannot find block group zone for the address selected, please try another address.");
								document.getElementById("googletext").innerHTML = "";
								return;
							}
							else
							{
								var aryOptions = new Array();
								for (var i=0; i < nodeFeatID.length; i++)
								{
									var FeatId = nodeFeatID[i].childNodes[0].nodeValue;
								}
								
								
								GDownloadUrl("getData.aspx?s=" + FeatId + "&x=" + x + "&y=" + y,  pushLastTwoPts);
								
							}
								
						}
						else
						{
							var fault = xmlHttpObj.responseText;
							alert("Error happened: " + fault);
						}
					}
				}
	            
				xmlHttpObj.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
				xmlHttpObj.setRequestHeader("SOAPAction", "Test");
				// Execute the request    
				    
				xmlHttpObj.send("category=2000229&refx=" + x + "&refy=" + y);
			}
				
				
		}
function pushLastTwoPts(text, code) {

	var items = text.split(",");
	var x1 = "";
	var y1 = "";
	var x2 = "";
	var y2 = "";
	var x3 = "";
	var y3 = "";
	var x;
	var y;
	var lastx="";
	var lasty="";
	var llCoords;
	var ret={};// holds returned caculated values
	
	if (items.length == 0) {
		alert("No evacuation route has been assigned to the block goup you are in.");
	} else if (items.length == 2) {
		//alert("Error finding the nearest evacuation route intersection from the address you selected. Route will be generated based on your address and the exit point for the evacuation route you are assigned.");
		llCoords = SPCStoLL(items[0], items[1], ret);
		lastx = ret.lat;
		lasty = ret.lon;
		rtpointsstr += ";" + lastx + "," + lasty;
		generateRoute(true);
	} else if (items.length == 4) {
		//alert("Error finding the nearest evacuation route intersection from the address you selected. Route will be generated based on your address and the exit point for the evacuation route you are assigned.");
		llCoords = SPCStoLL(items[0], items[1], ret);
		x1 = ret.lat;
		y1 = ret.lon;
		rtpointsstr += ";" + x1 + "," + y1;
		llCoords = SPCStoLL(items[2], items[3], ret);
		lastx = ret.lat;
		lasty = ret.lon;
		rtpointsstr += ";" + lastx + "," + lasty;
		generateRoute(false);
		
	} else if (items.length == 6) {
		//alert("Error finding the nearest evacuation route intersection from the address you selected. Route will be generated based on your address and the exit point for the evacuation route you are assigned.");
		x= rtpointsstr.split(",")[0];
		y= rtpointsstr.split(",")[1];
		llCoords = SPCStoLL(items[0], items[1], ret);
		x1 = ret.lat;
		y1 = ret.lon;
		llCoords = SPCStoLL(items[2], items[3], ret);
		x2 = ret.lat;
		y2 = ret.lon;
		llCoords = SPCStoLL(items[4], items[5], ret);
		lastx = ret.lat;
		lasty = ret.lon;
		getnearestOfTwo(x, y, x1, y1, x2, y2, lastx,lasty)
	} else if (items.length == 8) {
		//alert("Error finding the nearest evacuation route intersection from the address you selected.<br>Three possible reasons:  1. Cannot find any intersection point on the evacuation route you address belong to. 2. Cannot get the exit point for the evacuation route you address belong to. 3. Your address is not located in any block group are, so no evacuation route can be identified.");
		x= rtpointsstr.split(",")[0];
		y= rtpointsstr.split(",")[1];
		//alert(items[0] + ",,, " + items[1]);
		llCoords = SPCStoLL(items[0], items[1], ret);
		x1 = ret.lat;
		y1 = ret.lon;
		llCoords = SPCStoLL(items[2], items[3], ret);
		//alert(items[2] + ",,, " + items[3]);
		x2 = ret.lat;
		y2 = ret.lon;
		llCoords = SPCStoLL(items[4], items[5], ret);
		//alert(items[4] + ",,, " + items[5]);
		x3 = ret.lat;
		y3 = ret.lon;
		llCoords = SPCStoLL(items[6], items[7], ret);
		//alert(items[6] + ",,, " + items[7]);
		lastx = ret.lat;
		lasty = ret.lon;
		getnearestOfThree(x, y, x1, y1, x2, y2, x3, y3,lastx,lasty)
	}
}
function switchSearchButton()
		{
			var dvFeatures = document.getElementById("ddlAddresses");
			if(dvFeatures.options.length > 0 && dvFeatures.selectedIndex != -1)
			{
				document.getElementById("btnSearch").disabled = false;
			}
			else
			{
				document.getElementById("btnSearch").disabled = true;
			}
			
			return;
		}    
function getnearestOfTwo(x, y, x1, y1, x2, y2, lastx, lasty) {
	var gdir1 = new GDirections();
	gdir1.load("from: " + x + "," + y + " to: " + x1 + "," + y1 + " to: " + lastx + "," + lasty);
	//GEvent.addListener(gdir1, "error", handleErrors1);
	GEvent.addListener(gdir1,"load", function() { 
		var distance1 = gdir1.getDistance().meters;
		alert(distance1);
		var gdir2 = new GDirections();
		
		gdir2.load("from: " + x + "," + y + " to: " + x2 + "," + y2 + " to: " + lastx + "," + lasty);
		GEvent.addListener(gdir2,"load", function() { 
				var distance2 = gdir2.getDistance().meters;
				var shortestDis=Math.min(distance1,distance2);
				
				var shortestLat;
				var shortestLon;
				if (shortestDis == distance1) {

					shortestLat = x1;
					shortestLon = y1;
				} else if (shortestDis==distance2) {
					shortestLat = x2;
					shortestLon = y2;
				}
				//add the route distance nearest point (second point)
				rtpointsstr += ";" + shortestLat + "," + shortestLon;
				//add last point (the exit point)
				rtpointsstr += ";" + lastx + "," + lasty;
				generateRoute(false);
		});
	});
}
		
function getnearestOfThree(x, y, x1, y1, x2, y2, x3, y3, lastx, lasty) {
	gdir = new GDirections();
	//alert(x1 + "," + y1);
	gdir.load("from: " + x + "," + y + " to: " + x1 + "," + y1 + " to: " + lastx + "," + lasty);
	GEvent.addListener(gdir, "error", handleErrors);
	GEvent.addListener(gdir,"load", function() { 
		var distance1 = gdir.getDistance().meters;
		//alert(distance1);
		gdir = new GDirections();
		
		gdir.load("from: " + x + "," + y + " to: " + x2 + "," + y2 + " to: " + lastx + "," + lasty);
		//alert(x2 + "," + y2);
		//GEvent.addListener(gdir2, "error", handleErrors2);
		GEvent.addListener(gdir,"load", function() { 
			var distance2 = gdir.getDistance().meters;
			//alert(distance2);
			gdir = new GDirections();
			gdir.load("from: " + x + "," + y + " to: " + x3 + "," + y3 + " to: " + lastx + "," + lasty);
			//alert(x3 + "," + y3);
			//GEvent.addListener(gdir3, "error", handleErrors3);
			GEvent.addListener(gdir,"load", function() { 
				var distance3 = gdir.getDistance().meters;
				//alert(distance3);
				var shortestDis=Math.min(distance1,distance2);
				shortestDis=Math.min(shortestDis,distance3);
				var shortestLat;
				var shortestLon;
				//alert(distance1);
				//alert(distance2);
				//alert(distance3);
				if (shortestDis==distance1) {
					//alert("1st");
					shortestLat = x1;
					shortestLon = y1;
				} else if (shortestDis==distance2) {
					//alert("2nd");
					shortestLat = x2;
					shortestLon = y2;
				} else if (shortestDis==distance3) {
					//alert("3rd");
					shortestLat = x3;
					shortestLon = y3;
				}
				//add the route distance nearest point (second point)
				rtpointsstr += ";" + shortestLat + "," + shortestLon;
				//add last point (the exit point)
				rtpointsstr += ";" + lastx + "," + lasty;
				generateRoute(false);
			});
		});
	});
}


function generateRoute(twoptsonly) {
	directionsPanel = document.getElementById("route");
	directionsPanel.innerHTML='';

	var gdir = null;
    gdir = new GDirections(map, directionsPanel);
    //gdir.clear();
    
    GEvent.addListener(gdir, "error", handleErrors);
    var directionStr="";
    var items=rtpointsstr.split(";");
    //alert(items[2]);
    directionStr="from: " + items[0] + " to: " + items[1];
    if (twoptsonly == false) {directionStr += " to: " + items[2];}
    
    map.clearOverlays();
    addDistrictBound();
    gdir.load(directionStr);
    
    if (twoptsonly == false) {
		document.getElementById("googletext").innerHTML = "<a target='_blank' href='http://maps.google.com/maps?saddr=" + items[0] + "&daddr=" + items[1] + " to:+ " + items[2] + "&pw=2'><b>Print My Route</b></a>";
	} else {
		document.getElementById("googletext").innerHTML = "<a target='_blank' href='http://maps.google.com/maps?saddr=" + items[0] + "&daddr=" + items[1] + "&pw=2'><b>Print My Route</b></a>";
	}
	
     //var queryOpts;	

     //gdir.loadFromWaypoints(items[0],items[1],items[2]);

}

function handleErrors(){
	   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
	     alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
	   else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
	     alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
	   
	   else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
	     alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);

	//   else if (gdir.getStatus().code == G_UNAVAILABLE_ADDRESS)  <--- Doc bug... this is either not defined, or Doc is wrong
	//     alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + gdir.getStatus().code);
	     
	   else if (gdir.getStatus().code == G_GEO_BAD_KEY)
	     alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);

	   else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
	     alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
	    
	   else //alert("An unknown error occurred.error code: " + gdir.getStatus().code);
	   alert("The exit point on that route is not routable.\n Error code: " + gdir.getStatus().code);
	   
	}
	
