﻿function initialize() {
	  if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map_canvas"));
		map.setCenter(new GLatLng(40.5,-96.5), 4);
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		geocoder = new GClientGeocoder();
		var originAddress = document.getElementsByName('originAddress')[0];
		var destinationAddress = document.getElementsByName('destinationAddress')[0];			
		var txtAddress1 = document.getElementsByName('txtAddress1')[0];
		var txtAddress2 = document.getElementsByName('txtAddress2')[0];
		var txtOrgCode = document.getElementsByName('txtOrgCode')[0];
		var txtDestCode = document.getElementsByName('txtDestCode')[0];
		var cmdContinue = document.getElementsByName('cmdContinue')[0];
		var cmdShowDestination = document.getElementsByName('cmdShowDestination')[0];
		var cmdResetDestination = document.getElementsByName('cmdResetDestination')[0];		
		originAddress.value = "";
		originAddress.disabled = false;
		destinationAddress.value = "";
		destinationAddress.disabled = true;
		txtAddress1.value = "";
		txtAddress2.value = "";
		txtOrgCode.value = "";
		txtDestCode.value = "";
		cmdShowDestination.disabled = true;
		cmdResetDestination.disabled = true;
		cmdContinue.disabled = true;
		originAddress.focus();
		}
	}

function whichRoute(Origin, Destination) {
	var txtAddress1 = document.getElementsByName('txtAddress1')[0];
	var txtAddress2 = document.getElementsByName('txtAddress2')[0];
	var originAddress = document.getElementsByName('originAddress')[0];
	var destinationAddress = document.getElementsByName('destinationAddress')[0];			
	txtAddress1.value = originAddress.toUpperCase();
	txtAddress2.value = destinationAddress.toUpperCase();
		showRouteCodesOnly(Origin, Destination);
}

		
function resetOrigin() {
	var originAddress = document.getElementsByName('originAddress')[0];
	var txtAddress1 = document.getElementsByName('txtAddress1')[0];
	var txtOrgCode = document.getElementsByName('txtOrgCode')[0];
	var txtDestCode = document.getElementsByName('txtDestCode')[0];
	var cmdShowOrigin = document.getElementsByName('cmdShowOrigin')[0];
	var cmdShowDestination = document.getElementsByName('cmdShowDestination')[0];
	map.clearOverlays();
	originAddress.value = "";
	txtAddress1.value = "";
	txtOrgCode.value = "";
	originAddress.disabled = false;
	originAddress.focus();
	}

function resetDestination() {
	var destinationAddress = document.getElementsByName('destinationAddress')[0];			
	var txtAddress2 = document.getElementsByName('txtAddress2')[0];
	var txtDestCode = document.getElementsByName('txtDestCode')[0];
	var cmdShowDestination = document.getElementsByName('cmdShowDestination')[0];
	var cmdContinue = document.getElementsByName('cmdContinue')[0];
	map.clearOverlays();
	destinationAddress.value = "";
	txtAddress2.value = "";
	txtDestCode.value = "";
	destinationAddress.disabled = false;
	destinationAddress.focus();
	}
   

function showCity(address1) {
	var cityIcon = new GIcon(G_DEFAULT_ICON,"http://labs.google.com/ridefinder/images/mm_20_red.png");
		cityIcon.iconSize = new GSize(12,20);
		cityIcon.shadowSize = new GSize(0,0);
	map.clearOverlays();
	var bounds = new GLatLngBounds();
	if (geocoder) {
	//process address1
		geocoder.getLatLng(address1,										//convert string address to lat/long
			function(point1){
	            if (!point1)
	            {
				alert(address1 + "City not found.  Please re-enter.");								//bad address error handling
	            }
	            else
	            {
				var marker1 = new GMarker(point1, cityIcon);							//create the marker
				bounds.extend(point1);										//increase the boundary to include the new point
				var lat = point1.lat();
				var lng = point1.lng();
			GEvent.addListener(marker1, "click", 
				function() {
		    	    marker1.openInfoWindowHtml('<div class="xxsmallText" style="text-transform:capitalize"><strong>' + address1 + '</strong><br />' + marker1.getPoint() + '</div>'
												);
				});
			map.addOverlay(marker1);										//place the marker at address1
			map.setZoom(9);					//rezoom the map to include both points
			map.setCenter(bounds.getCenter());								//recenter the map				
			GDownloadUrl("xhrairports.xml", function(data) {
			var xml = GXml.parse(data);
			var markers = xml.documentElement.getElementsByTagName("marker");
			for (var i = 0; i < markers.length; i++) {
				var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),
										parseFloat(markers[i].getAttribute("lng")));
				var minlat = lat - 1;
				var minlng = lng - 1.5;
				var maxlat = lat + 1;
				var maxlng = lng + 1.5;
				var marklng = point.lng();
				var marklat = point.lat();
				if (marklng > minlng && marklng < maxlng && marklat < maxlat && marklat > minlat)
					{
					var label = markers[i].getAttribute("label");
					var city = markers[i].getAttribute("city");
					var airport = markers[i].getAttribute("airport");
					var info = '<div class="xxsmallText"><strong>' + label + ':</strong><br />' + airport + '<br />' + city + '</div>';
					var baseIcon = new GIcon();
						baseIcon.iconSize=new GSize(32,32);
						baseIcon.shadowSize=new GSize(56,32);
						baseIcon.iconAnchor=new GPoint(16,32);
						baseIcon.infoWindowAnchor=new GPoint(16,0);
					var plane = new GIcon(baseIcon, "http://maps.google.com/mapfiles/kml/pal2/icon56.png", null, "http://maps.google.com/mapfiles/kml/pal2/icon56s.png");
					var spot = createMarker(point,info,plane,label,airport);
					map.addOverlay(spot);
					}
				}
			});
		}
	});
	}
}



function createMarker(point,info,icon,label,airport) {
iconOrigin=new GIcon(G_DEFAULT_ICON, "http://maps.google.com/mapfiles/dd-start.png");
iconDestination=new GIcon(G_DEFAULT_ICON, "http://maps.google.com/mapfiles/dd-end.png");
var marker = new GMarker(point,icon);
var originAddress = document.getElementsByName('originAddress')[0];
var txtOrigin = document.getElementsByName('txtOrigin')[0];
var txtOriginInfo = document.getElementsByName('txtOriginInfo')[0];
var txtOrgCode = document.getElementsByName('txtOrgCode')[0];
var txtDestCode = document.getElementsByName('txtDestCode')[0];
var txtAddress1 = document.getElementsByName('txtAddress1')[0];
var txtAddress2 = document.getElementsByName('txtAddress2')[0];
var destinationAddress = document.getElementsByName('destinationAddress')[0];
var txtDestination = document.getElementsByName('txtDestination')[0];
var txtDestinationInfo = document.getElementsByName('txtDestinationInfo')[0];
var cmdShowOrigin = document.getElementsByName('cmdShowOrigin')[0];
var cmdShowDestination = document.getElementsByName('cmdShowDestination')[0];
var cmdResetDestination = document.getElementsByName('cmdResetDestination')[0];
var cmdContinue = document.getElementsByName('cmdContinue')[0];
var location = label + " " + airport
GEvent.addListener(marker, "mouseover", function() {
	marker.openInfoWindowHtml(info + '<div class="xxsmallText"><strong>Click the icon to select</div>');
	});
GEvent.addListener(marker, "click", function() {
			if ((originAddress.value != "" && txtOrgCode.value == "" && destinationAddress.value == "")||
				(destinationAddress.value != "" && txtDestCode.value != ""))
			{
				txtOrigin.value = point.lat() + ", " + point.lng();
				txtOriginInfo.value = info;
				txtAddress1.value = point;
				txtOrgCode.value = label.toUpperCase();
				marker.openInfoWindowHtml(info);
				originAddress.value = label;
				originAddress.disabled = false;
				destinationAddress.disabled = false;
				destinationAddress.focus();
				cmdShowDestination.disabled = false;
				cmdResetDestination.disabled = false;
				map.clearOverlays();
				var newOrigin = createNewMarker(point,iconOrigin,info);
				map.addOverlay(newOrigin);
				newOrigin.openInfoWindowHtml(info);
			}
			else
			{
				txtDestination.value = point.lat()+ ", " + point.lng();
				txtDestinationInfo.value = info;
				txtAddress2.value = point;
				txtDestCode.value = label.toUpperCase();
				marker.openInfoWindowHtml(info);
				destinationAddress.value = label;
				destinationAddress.disabled = false;
				cmdContinue.disabled = false;
				cmdContinue.focus();
				map.clearOverlays();
				var newDestination = createNewMarker(point,iconDestination,info);
				map.addOverlay(newDestination);
				newDestination.openInfoWindowHtml(info);
  			}
			});
return marker;
}

function createNewMarker(point,icon,info) {
	var newmarker = new GMarker(point,icon);
	GEvent.addListener(newmarker, "click", function() {
	newmarker.openInfoWindowHtml(info);
	});
	return newmarker;
}
        
function roundIt(number, length) {
	var result = Math.round(number * Math.pow(10, length))/Math.pow(10, length);
	return result;
}

function showRouteCodesOnly(originCode, destinationCode) {
	var txtOrgCode = document.getElementsByName('txtOrgCode')[0];
	var txtDestCode = document.getElementsByName('txtDestCode')[0];
	var txtCost = document.getElementsByName('txtCost')[0];
	var originAddress = document.getElementsByName('originAddress')[0];
	var destinationAddress = document.getElementsByName('destinationAddress')[0];
	iconOrigin=new GIcon(G_DEFAULT_ICON, "http://maps.google.com/mapfiles/dd-start.png");
	iconDestination=new GIcon(G_DEFAULT_ICON, "http://maps.google.com/mapfiles/dd-end.png");
	txtOrgCode.value = originCode.toUpperCase();
	txtDestCode.value = destinationCode.toUpperCase();
	originAddress.value = originCode.toUpperCase();
	destinationAddress.value = destinationCode.toUpperCase();
	var oCode = "airport:" + originCode;
	var dCode = "airport:" + destinationCode;
	if (originAddress.value == "" || destinationAddress.value == "") {
		alert("Your trip information is incomplete.  Please check the origin and destination and try again.");
		}
		else
		{
	if (originCode.length > 3) {
		alert("The origin must contain a valid airport code!");
		resetOrigin();
		}
		else
		if (destinationCode.length > 3) {
		alert("The destination must contain a valid airport code!");
		resetDestination();
		}
		else
		{
	map.clearOverlays();
	var bounds = new GLatLngBounds();
		if (geocoder) {
		//process address1
			geocoder.getLatLng(oCode,
				function(point1){
		            if (!point1)
		            {
					alert(originCode + " not found");
		            }
		            else
		            {
					var marker1 = new GMarker(point1, iconOrigin);
					bounds.extend(point1);
					geocoder.getLatLng(dCode,
	          		function(point2){
						if (!point2)
						{
						alert(destinationCode + " not found");
						}
						else
						{
						var marker2 = new GMarker(point2, iconDestination);
						bounds.extend(point2);
						var polyOptions = {geodesic:true};
				   	    var polyline = new GPolyline([
							point1,
							point2,
						], "#ff0000", 6, 0.75, polyOptions);
						var pline = polyline.getLength();
						var km = roundIt((pline / 1000),2);
						var nmiles = roundIt((km * 0.539956803),2);
						var miles = (km * 0.621371192);
						var airtime = (miles / 530)*60;
						var txtCost = document.getElementsByName('txtCost')[0];
						var cost = (1600 + 7.4 * nmiles)/.85 * 1.075
							if (nmiles > 1500)
								{
								cost = (cost * 1.50);
								}
							else
								{
								cost = cost;
								}
						cost = cost.toFixed(2);
						var cost75 = roundIt((cost * .75),0);
						var cost50 = roundIt((cost * .5),0);
						var cost25 = roundIt((cost * .25),0);
						txtCost.value = cost;
						miles = roundIt(miles,0);
						originCode = originCode.toUpperCase();
						destinationCode = destinationCode.toUpperCase();
						GEvent.addListener(marker1, "click", 
							function() {
					    	    marker1.openInfoWindowHtml('<div class="xxsmallText" style="text-transform:capitalize"><b>Originating: </b><br />' + 
			    	    							originCode + '<div>'
															);
							});
						GEvent.addListener(marker2, "click", 
							function() {
					    	    marker2.openInfoWindowHtml('<div class="xxsmallText" style="text-transform:capitalize"><b>Destination: </b><br />'+ 
					    	    							destinationCode + '</div>'
															);
							});
				map.addOverlay(marker1);
				map.addOverlay(marker2);
				map.addOverlay(polyline);					
				map.setZoom(map.getBoundsZoomLevel(bounds)-1);
				map.setCenter(bounds.getCenter());
	    	   // marker1.openInfoWindowHtml('<div class="xxsmallText" style="text-transform:capitalize"><b>Entire Trip Cost:</b> $' + cost + '<br />'+
	    	   // 						   '<b>3/4 Cost of Trip:</b> $' + cost75 + '<br />'+
	    	   // 						   '<b>1/2 Cost of Trip:</b> $' + cost50 + '<br />'+
	    	   // 						   '<b>1/4 Cost of Trip:</b> $' + cost25 + '<br /></div>');
	    	   // marker1.openInfoWindowHtml('<div class="xxsmallText" style="text-transform:capitalize"><b>Estimated Trip Cost:</b> $' + cost + '<br />'+
	    	   // 						   '<b>1/2 Cost of Trip:</b> $' + cost50 + '<br /></div>');
	    	    marker1.openInfoWindowHtml('<div class="xxsmallText" style="text-transform:capitalize"><b>Target Price per Passenger:</b> $' + cost25 + '<br /></div>');
				GEvent.addListener(marker1, 'dblclick', 
					function() {
			    	    //marker1.openInfoWindowHtml('<div class="xxsmallText" style="text-transform:capitalize"><b>Entire Trip Cost:</b> $' + cost + '<br />'+
	    	    		//				   '<b>3/4 Cost of Trip:</b> $' + cost75 + '<br />'+
	    	    		//				   '<b>1/2 Cost of Trip:</b> $' + cost50 + '<br />'+
	    	    		//				   '<b>1/4 Cost of Trip:</b> $' + cost25 + '<br /></div>');
						//							});
			    	    //marker1.openInfoWindowHtml('<div class="xxsmallText" style="text-transform:capitalize"><b>Estimated Trip Cost:</b> $' + cost + '<br />'+
	    	    		//				   '<b>Target Price per Passenger:</b> $' + cost25 + '<br /></div>');
   			    	    marker1.openInfoWindowHtml('<div class="xxsmallText" style="text-transform:capitalize"><b>Target Price per Passenger:</b> $' + cost25 + '<br /></div>');
													});
				GEvent.addListener(marker2, 'dblclick', 
					function() {
			    	    //marker2.openInfoWindowHtml('<div class="xxsmallText" style="text-transform:capitalize"><b>Entire Trip Cost:</b> $' + cost + '<br />'+
	    	    		//				   '<b>3/4 Cost of Trip:</b> $' + cost75 + '<br />'+
	    	    		//				   '<b>1/2 Cost of Trip:</b> $' + cost50 + '<br />'+
	    	    		//				   '<b>1/4 Cost of Trip:</b> $' + cost25 + '<br /></div>');
			    	    //marker2.openInfoWindowHtml('<div class="xxsmallText" style="text-transform:capitalize"><b>Estimated Trip Cost:</b> $' + cost + '<br />'+
	    	    		//				   '<b>Target Price per Passenger:</b> $' + cost25 + '<br /></div>');
   			    	    marker2.openInfoWindowHtml('<div class="xxsmallText" style="text-transform:capitalize"><b>Target Price per Passenger:</b> $' + cost25 + '<br /></div>');
				txtCost.value = cost;
				var hiddenCost = '<%= inpHideCost.ClientID %>';
				document.getElementById(hiddenCost).value=cost;
				var hiddenAddress1 = '<%= inpHideAddress1.ClientID %>';
				document.getElementById(hiddenAddress1).value=originCode
				var hiddenAddress2 = '<%= inpHideAddress2.ClientID %>';
				document.getElementById(hiddenAddress2).value=destinationCode
							});
						}
				});
				}
		});
}
}
}
}

function showRoute(origin, destination) {
	var txtCost = document.getElementsByName('txtCost')[0];
	iconOrigin=new GIcon(G_DEFAULT_ICON, "http://maps.google.com/mapfiles/dd-start.png");
	iconDestination=new GIcon(G_DEFAULT_ICON, "http://maps.google.com/mapfiles/dd-end.png");
	map.clearOverlays();
	var bounds = new GLatLngBounds();
	if (geocoder) {
	//process address1
		geocoder.getLatLng(origin,
			function(point1){
	            if (!point1)
	            {
				alert(origin + " not found");
	            }
	            else
	            {
				var marker1 = new GMarker(point1, iconOrigin);
				bounds.extend(point1);
				geocoder.getLatLng(destination,
          		function(point2){
					if (!point2)
					{
					alert(destination + " not found");
					}
					else
					{
					var marker2 = new GMarker(point2, iconDestination);
					bounds.extend(point2);
					var polyOptions = {geodesic:true};
			   	    var polyline = new GPolyline([
						point1,
						point2,
					], "#ff0000", 6, 0.75, polyOptions);
					var pline = polyline.getLength();
					var km = roundIt((pline / 1000),2);
					var nmiles = roundIt((km * 0.539956803),2);
					var miles = (km * 0.621371192);
					var airtime = (miles / 530)*60;
					var txtCost = document.getElementsByName('txtCost')[0];
					var cost = (1600 + 7.4 * nmiles)/.85 * 1.075 
							if (nmiles > 1500)
								{
								cost = (cost * 1.50);
								}
							else
								{
								cost = cost;
								}
					cost = cost.toFixed(2);
					var cost75 = roundIt((cost * .75),0);
					var cost50 = roundIt((cost * .5),0);
					var cost25 = roundIt((cost * .25),0);
					cost = roundIt(cost,0);
					txtCost.value = cost;
					miles = roundIt(miles,0);
						GEvent.addListener(marker1, "click", 
							function() {
					    	    marker1.openInfoWindowHtml(origin);
							});
						GEvent.addListener(marker2, "click", 
							function() {
					    	    marker2.openInfoWindowHtml(destination);
							});
			map.addOverlay(marker1);
			map.addOverlay(marker2);
			map.addOverlay(polyline);					
			map.setZoom(map.getBoundsZoomLevel(bounds)-1);
			map.setCenter(bounds.getCenter());
	    	   // marker1.openInfoWindowHtml('<div class="xxsmallText" style="text-transform:capitalize"><b>Entire Trip Cost:</b> $' + cost + '<br />'+
	    	   // 						   '<b>3/4 Cost of Trip:</b> $' + cost75 + '<br />'+
	    	   // 						   '<b>1/2 Cost of Trip:</b> $' + cost50 + '<br />'+
	    	   // 						   '<b>1/4 Cost of Trip:</b> $' + cost25 + '<br /></div>');
	    	   // marker1.openInfoWindowHtml('<div class="xxsmallText" style="text-transform:capitalize"><b>Estimated Trip Cost:</b> $' + cost + '<br />'+
	    	   // 						   '<b>1/2 Cost of Trip:</b> $' + cost50 + '<br /></div>');
	    	    marker1.openInfoWindowHtml('<div class="xxsmallText" style="text-transform:capitalize"><b>Target Price per Passenger:</b> $' + cost25 + '<br /></div>');
				GEvent.addListener(marker1, 'dblclick', 
					function() {
			    	    //marker1.openInfoWindowHtml('<div class="xxsmallText" style="text-transform:capitalize"><b>Entire Trip Cost:</b> $' + cost + '<br />'+
	    	    		//				   '<b>3/4 Cost of Trip:</b> $' + cost75 + '<br />'+
	    	    		//				   '<b>1/2 Cost of Trip:</b> $' + cost50 + '<br />'+
	    	    		//				   '<b>1/4 Cost of Trip:</b> $' + cost25 + '<br /></div>');
						//							});
			    	    //marker1.openInfoWindowHtml('<div class="xxsmallText" style="text-transform:capitalize"><b>Estimated Trip Cost:</b> $' + cost + '<br />'+
	    	    		//				   '<b>Target Price per Passenger:</b> $' + cost25 + '<br /></div>');
   			    	    marker1.openInfoWindowHtml('<div class="xxsmallText" style="text-transform:capitalize"><b>Target Price per Passenger:</b> $' + cost25 + '<br /></div>');
													});
				GEvent.addListener(marker2, 'dblclick', 
					function() {
			    	    //marker2.openInfoWindowHtml('<div class="xxsmallText" style="text-transform:capitalize"><b>Entire Trip Cost:</b> $' + cost + '<br />'+
	    	    		//				   '<b>3/4 Cost of Trip:</b> $' + cost75 + '<br />'+
	    	    		//				   '<b>1/2 Cost of Trip:</b> $' + cost50 + '<br />'+
	    	    		//				   '<b>1/4 Cost of Trip:</b> $' + cost25 + '<br /></div>');
			    	    //marker2.openInfoWindowHtml('<div class="xxsmallText" style="text-transform:capitalize"><b>Estimated Trip Cost:</b> $' + cost + '<br />'+
	    	    		//				   '<b>Target Price per Passenger:</b> $' + cost25 + '<br /></div>');
   			    	    marker2.openInfoWindowHtml('<div class="xxsmallText" style="text-transform:capitalize"><b>Target Price per Passenger:</b> $' + cost25 + '<br /></div>');
			txtCost.value = cost;
			var hiddenCost = '<%= inpHideCost.ClientID %>';
			document.getElementById(hiddenCost).value=cost;
			var hiddenAddress1 = '<%= inpHideAddress1.ClientID %>';
			document.getElementById(hiddenAddress1).value=address1
			var hiddenAddress2 = '<%= inpHideAddress2.ClientID %>';
			document.getElementById(hiddenAddress2).value=address2
						});
					}
			});
			}
	});
}
}