var map = null;

// Shape layers to add places and routes to.
var g_layerImportantPlaces = null;
var g_layerWalkRoute = null;

// NEW CODE.
var g_layerWalkRouteGCA = null;

//var g_layerImportantPlaces = new VEShapeLayer();
//var g_layerWalkRoute = new VEShapeLayer();

// Flag to indicate if the 'DisplayImportantPlaces' function should be called in the 'LoadSavedMap' function or not.
var g_blnImportantPlaces = false;	

// Flag to indicate if the 'DisplayMappedWalkRoutes' function should be called in the 'LoadSavedMap' function or not.
var g_blnMappedWalkRoutes = false;


// Get the current form being used.  
// Will be used to determine what should be done at various point in the code.
var objForm;


// Icon values used for mapping Important Places.
//		icon = ""  -  means use the default icon which is the pushpin.
var icon = "";

var icon_parking = "<img src='images/icon_parking.gif' width='30px' height='28px' border='0' />";
var icon_rest_area = "<img src='images/icon_rest_area.gif' width='32px' height='32px' border='0' />";
var icon_small_rest_area = "<img src='images/icon_small_rest_area.gif' width='30px' height='28px' border='0' />";
var icon_bench = "<img src='images/icon_bench.gif' width='30px' height='15px' border='0' />";

//var icon_start = "<img src='images/icon_start.gif' width='36px' height='10px' border='0' />";
//var icon_start = "<img src='images/icon_route.gif' width='30px' height='27px' border='0' />";
var icon_start = "<img src='images/icon_route.png' width='30px' height='27px' border='0' />";
var icon_route_start = "<img src='images/icon_route_start.gif' width='40px' height='54px' border='0' />";
var icon_park_space = "<img src='images/icon_park_space.gif' width='47px' height='38px' border='0' />";




function SetStandardMapCenterAndZoomLevel(p_objMap) {

	// Set map center to St. John's
	var mapCenter = new VELatLong(47.5655, -52.715);
	
	//map.SetCenter(mapCenter);
	p_objMap.SetCenterAndZoom(mapCenter, 13);

}



function LoadMap_WalkRoute()
{
	// Get the current form being used in the page.
	objForm = document.forms[0];
	//alert("objForm = " + objForm);

	// Make sure that a form has been created before doing the following or an error will occur.
	if (objForm != undefined) {
	
		// Load the Map
		map = new VEMap("myMap");
		
		// Attach our onclick map event handler
		map.AttachEvent("onclick", Map_Click);
	

		// Set map center to St. John's
		mapCenter = new VELatLong(47.5655, -52.715);
	
		
		// VALUES FOR VEMap Style.
		// VEMapStyle.Road = Road with Labels
		// VEMapStyle.Aerial = Aerial
		// VEMapStyle.Hybrid = Aerial with Labels
		
		// LoadMap(VELatLong, zoom, style, fixed, mode, showSwitch, tileBuffer, mapOptions);
		map.LoadMap(mapCenter, 15, VEMapStyle.Hybrid, false, VEMapMode.Mode2D, false, 0);
	
		// Set the look of the cursor over the map.
		document.getElementById("myMap").childNodes[0].style.cursor = "crosshair";
		
			
		// Display mapped important places if indicated.
		if (g_blnImportantPlaces == true) {
			DisplayImportantPlaces();
		}
			
	}
}



function LoadMap_ImportantPlace()
{
	// Get the current form being used in the page.
	objForm = document.forms[0];
	//alert("objForm = " + objForm);

	// Make sure that a form has been created before doing the following or an error will occur.
	if (objForm != undefined) {
	
		// Load the Map
		map = new VEMap("myMap");
		
		// Attach our onclick map event handler
		map.AttachEvent("onclick", Map_Click);
		
		
		// Get values from the form.  These will help determine what to do next.
		var strMapPoints = objForm.txtMapPoints_Hidden.value;
		
		if (strMapPoints != "") {
			// Split up the strMapPoints to an array and use the first 2 values.  
			// If this isn't done the map won't center correctly.
			var aMapCenter = strMapPoints.split(", ");
			mapCenter = new VELatLong(aMapCenter[0], aMapCenter[1]);
			
			// Get the rest of the form values if there is a value for strMapPoints.
			var strTitle = objForm.txtTitle.value;
			var strDescription = objForm.txtDescription.value;
			var strType = objForm.txtIcon_Hidden.value;
		} else {
			// Set map center to St. John's
			mapCenter = new VELatLong(47.5655, -52.715);
		}
		
		
		// VALUES FOR VEMap Style.
		// VEMapStyle.Road = Road with Labels
		// VEMapStyle.Aerial = Aerial
		// VEMapStyle.Hybrid = Aerial with Labels
		
		// LoadMap(VELatLong, zoom, style, fixed, mode, showSwitch, tileBuffer, mapOptions);
		map.LoadMap(mapCenter, 15, VEMapStyle.Hybrid, false, VEMapMode.Mode2D, false, 0);
				
		// Set the look of the cursor over the map.
		document.getElementById("myMap").childNodes[0].style.cursor = "crosshair";
		
		
		// If there are is a value for strMapPoints then a single important place is being edited so
		// only display the selected important place.
		if (strMapPoints != "") {
			
			// mapCenter is used here because the points are formatted to work correctly.
			AddPushpin(mapCenter, strTitle, strDescription, strType);
			
		} else {
			
			// Display mapped important places if indicated.
			if (g_blnImportantPlaces == true) {
				DisplayImportantPlaces();
				
				// Hide important places by default.
				HideImportantPlaces();
				
			}
			
		}

		
	}
}





function LoadMap()
{
	// Get the current form being used in the page.
	objForm = document.forms[0];
	//alert("objForm = " + objForm);

	// Make sure that a form has been created before doing the following or an else an error will occur.
	if (objForm != undefined) {
	
		// Load the Map
		map = new VEMap("myMap");
		
		// Attach our onclick map event handler
		map.AttachEvent("onclick", Map_Click);
	
		
		// Get values from the form.  These will help determine what to do next.
		var strMapPoints = objForm.txtMapPoints_Hidden.value;
		
				
		// If there is a value for strMapPoints then use it for the center of the map.
		if (strMapPoints != "") {
			mapCenter = strMapPoints;
			
			// Get the rest of the form values if there is a value for strMapPoints.
			var strTitle = objForm.txtTitle.value;
			var strDescription = objForm.txtDescription.value;
			var strType = objForm.txtIcon_Hidden.value;
		} else {
			// Set map center to St. John's
			mapCenter = new VELatLong(47.5655, -52.715);
		}
		
		// VALUES FOR VEMap Style.
		// VEMapStyle.Road = Road with Labels
		// VEMapStyle.Aerial = Aerial
		// VEMapStyle.Hybrid = Aerial with Labels
		
		// LoadMap(VELatLong, zoom, style, fixed, mode, showSwitch, tileBuffer, mapOptions);
		map.LoadMap(mapCenter, 15, VEMapStyle.Hybrid, false, VEMapMode.Mode2D, false, 0);		
		
		// If there are is a value for strMapPoints then a single important place is being edited so
		// only display the selected important place.
		if (strMapPoints != "") {
			DisplayImportantPlaces();
		} else {
			DisplayImportantPlaces();
		}
		
	
		// Set the look of the cursor over the map.
		document.getElementById("myMap").childNodes[0].style.cursor = "crosshair";
		
	}
}




// Add an icon to the map.
function AddIconToMap(p_strPoints, p_strTitle, p_strDescription, p_strType, p_strLayer) {
	
	
	shape = new VEShape(VEShapeType.Pushpin, p_strPoints);
		
	switch (p_strType) {
		case "Parking" :
			icon = icon_parking;
			break;
		case "RestArea" :
			icon = icon_rest_area;
			break;
		case "SmallRestArea" :
			icon = icon_small_rest_area;
			break;
		case "Bench" :
			icon = icon_bench;
			break;
		case "RouteStart" :
			icon = icon_route_start;
			break;
		case "Start" :
			icon = icon_start;
			break;
		case "ParkSpace" :
			icon = icon_park_space;
			break;
		case "Default" :
			icon = "";
			break;
		default :
			//icon = "";
	}
	
	
	// Set the Custom Icon if one is specified.
	if (icon != "") {
		shape.SetCustomIcon(icon);
	}
	
	
	// Set the value the shapes title and description if there are values.
	if (p_strTitle != "") {
		shape.SetTitle(p_strTitle);
	}
	if (p_strDescription != "") {
		shape.SetDescription(p_strDescription);
	}
	
	
	// Make sure that the shape layer exists.
	if (p_strLayer == "Walk Route") {
		
		if (g_layerWalkRoute == null) {
			CreateLayer_WalkRoute();
		}
		
		g_layerWalkRoute.AddShape(shape);
		
	} else if (p_strLayer == "Walk Route GCA") {
		
		if (g_layerWalkRouteGCA == null) {
			CreateLayer_WalkRoute_GCA();
		}
		
		g_layerWalkRouteGCA.AddShape(shape);
		
	} else {
		
		if (g_layerImportantPlaces == null) {
			CreateLayer_ImportantPlaces();
		}
		
		g_layerImportantPlaces.AddShape(shape);
		
	}
	
}





// Add a pushpin to the map.
function AddPushpin(p_strPoints, p_strTitle, p_strDescription, p_strType) {
		
	shape = new VEShape(VEShapeType.Pushpin, p_strPoints);
		
	switch (p_strType) {
		case "Parking" :
			icon = icon_parking;
			break;
		case "RestArea" :
			icon = icon_rest_area;
			break;
		case "SmallRestArea" :
			icon = icon_small_rest_area;
			break;
		case "Bench" :
			icon = icon_bench;
			break;
		case "RouteStart" :
			icon = icon_route_start;
			break;
		case "Start" :
			icon = icon_start;
			break;
		case "ParkSpace" :
			icon = icon_park_space;
			break;
		case "Default" :
			icon = "";
			break;
		default :
			//icon = "";
	}
	
	
	// Set the Custom Icon if one is specified.
	if (icon != "") {
		shape.SetCustomIcon(icon);
	}
	
	
	// Set the value the shapes title and description if there are values.
	if (p_strTitle != "") {
		shape.SetTitle(p_strTitle);
	}
	if (p_strDescription != "") {
		shape.SetDescription(p_strDescription);
	}
	
	
	// Make sure that the shape layer exists.
	if (g_layerImportantPlaces == null) {
		CreateLayer_ImportantPlaces();
	}
	
	
	// Add the new shape to the indicated shape layer.
	if (g_layerImportantPlaces != null) {
		g_layerImportantPlaces.AddShape(shape);
	}
}


// Hide all important places.
function HideImportantPlaces() {
	
	g_layerImportantPlaces.Hide();
	
}


// Show all important places.
function ShowImportantPlaces() {
	
	g_layerImportantPlaces.Show();
	
}


// NEW CODE.
// Hide all walk routes.
function HideWalkRoutes() {
	
	g_layerWalkRoute.Hide();
	
}


// Show all walk routes.
function ShowWalkRoutes() {
	
	g_layerWalkRoute.Show();
	
}

// Hide all gca walk routes.
function HideWalkRoutes_GCA() {
	
	g_layerWalkRouteGCA.Hide();
	
}


// Show all gca walk routes.
function ShowWalkRoutes_GCA() {
	
	g_layerWalkRouteGCA.Show();
	
}




// Hide all gca walk routes.
function HideGCAWalkRoutes() {
	
	g_layerWalkRoute.Hide();
	
}


// Show all gca walk routes.
function ShowGCAWalkRoutes() {
	
	g_layerWalkRoute.Show();
	
}



// Create a layer and add it to the map.
function CreateLayerAndAddToMap(p_objLayer) {   
	  
	p_objLayer = new VEShapeLayer();         
	map.AddShapeLayer(p_objLayer);  
	
	return p_objLayer;
		
} 




// Create a layer for the important places.
function CreateLayer_ImportantPlaces() {   
	  
	g_layerImportantPlaces = new VEShapeLayer();         
	map.AddShapeLayer(g_layerImportantPlaces);  
		
}            



// Create a layer for mapping a walk route.
function CreateLayer_WalkRoute() {   
	  
	g_layerWalkRoute = new VEShapeLayer();         
	map.AddShapeLayer(g_layerWalkRoute);  
		
}  

// Delete the layer for mapping a walk route.
function DeleteLayer_WalkRoute() {   
	  
	if (g_layerWalkRoute != null) {
		map.DeleteShapeLayer(g_layerWalkRoute);
		g_layerWalkRoute = null;
	}
		
}  


// NEW CODE.
// Create a layer for mapping gca walk routes.
function CreateLayer_WalkRoute_GCA() {   
	  
	g_layerWalkRouteGCA = new VEShapeLayer();         
	map.AddShapeLayer(g_layerWalkRouteGCA);  
		
}  



function DeleteShape(objShape) {     

	if(objShape != null) {            
		map.DeleteShape(objShape);            
		objShape = null;         
	}      
}




// Change the icon used for mapping important places.
function ChangeImportantPlaceIcon(p_strIcon) {
	
	// Set the value of the hidden icon text field.
	document.forms.frmImportantPlace.txtIcon_Hidden.value = p_strIcon;
	
	switch (p_strIcon){
		case "Parking": 
			icon = icon_parking;
			break;
		case "RestArea": 
			icon = icon_rest_area;
			break;
		case "SmallRestArea" :
			icon = icon_small_rest_area;
			break;
		case "Bench" :
			icon = icon_bench;
			break;
		case "RouteStart" :
			icon = icon_route_start;
			break;
		case "Start" :
			icon = icon_start;
			break;
		case "ParkSpace" :
			icon = icon_park_space;
			break;
		case "Default" :
			icon = "";
			break;
		default : 
			//icon = "";
	}

	//alert("document.forms.frmImportantPlace.txtIcon_Hidden.value = " + document.forms.frmImportantPlace.txtIcon_Hidden.value);

}



// Show or hide the important places layer based on the value of the checkbox.
function ShowHideImportantPlaces(objCheckbox) {
	
	if (objCheckbox.checked) {
		ShowImportantPlaces();
	} else {
		HideImportantPlaces();
	}

}




// Show or hide the gca walk routes layer based on the value of the checkbox.
function ShowHideWalkRoutes_GCA(objCheckbox) {
	
	if (objCheckbox.checked) {
		ShowWalkRoutes_GCA();		
	} else {
		HideWalkRoutes_GCA();
	}

}



// Global variable for Map_Click event only.
var g_blnRouteStart = false;
var g_strRouteStart = "";



// The Map onclick event handler
function Map_Click(eventArgs) {
	

    
    // When the user Right-Clicks, Draw the Polygon
    if (eventArgs.rightMouseButton)
    {
		
		
		// ******************************************************************************************
		// MOVED THE CODE STARTING BELOW FROM ABOVE THE 'rightMouseButton' CHECK.
		// ******************************************************************************************
		
		// New Code.
		//alert("g_blnRouteStart = " + g_blnRouteStart);
		//alert("g_strRouteStart = " + g_strRouteStart);
		//alert("txtMapPoints_Hidden.value = " + document.getElementById("txtMapPoints_Hidden").value);
		
		if (g_strRouteStart == "") {
			document.getElementById("txtMapPoints_Hidden").value = "";	
		}
		
		// If this is the start of the route set the flag.
		if (document.getElementById("txtMapPoints_Hidden").value != '') {
			g_blnRouteStart = false;
			g_strRouteStart = "NO"
			
		} else {
			g_blnRouteStart = true;
			g_strRouteStart = "YES"
		}
		
		var strMSG = "";
		
		strMSG = "txtMapPoints_Hidden.value = " + document.getElementById("txtMapPoints_Hidden").value;
		//strMSG += "\n";
		//strMSG += "g_blnRouteStart = " + g_blnRouteStart;
		strMSG += "\n";
		strMSG += "g_strRouteStart = " + g_strRouteStart;
		
		//alert(strMSG);
		
	
		
		// Get the lat and long of clicked location.
		var clickedLatLong = eventArgs.latlong;
		
		if (map.GetMapMode() == VEMapMode.Mode2D)
		{
			// In 2D Mode the eventArgs.latlong property doesn't contain the lat/long point that was clicked.
			// So we must figure it out using the x and y coordinates of the point on the map that was clicked.
			clickedLatLong = map.PixelToLatLong(new VEPixel(eventArgs.mapX, eventArgs.mapY));
		}
		
		
		// ******************************************************************************************
		// MOVED THE CODE ABOVE FROM ABOVE THE 'rightMouseButton' CHECK.
		// ******************************************************************************************
		
		
		
		
		
		// Only do this when dealing with the Walk Route form.
		if (objForm.name == "frmWalkRoute"){
				
		
			// Show the points in the textbox. seperate each coordinate with a ';'
			if (document.getElementById("txtMapPoints_Hidden").value != '')
			{
				document.getElementById("txtMapPoints_Hidden").value = document.getElementById("txtMapPoints_Hidden").value + ';' + clickedLatLong;
			}
			else
			{
				document.getElementById("txtMapPoints_Hidden").value = clickedLatLong;
			}
			
			var newShape = null;
			var color = new VEColor(255,0,0,1);	// Red
			var width = 3;
			
			
			
			if (map.GetShapeLayerCount() > 0)
			{
				// Get reference to the Default Shape Layer
				var shapeLayer = map.GetShapeLayerByIndex(0);
				
				if (shapeLayer.GetShapeCount() > 0)
				{
					// Get points already plotted
					var points = shapeLayer.GetShapeByIndex(0).GetPoints();
					// Add the newly clicked point to the collection of points
					points[points.length] = clickedLatLong;
					
					newShape = new VEShape(VEShapeType.Polyline, points);
					
					newShape.SetLineColor(color);
					newShape.SetLineWidth(width);
					
					// Don't show the shapes icon on the map, we only want to see the line
					newShape.HideIcon();
				}
			} 
			
			
			
			if (newShape == null)
			{
				// If this is the first Point plotted, then show it as a Pushpin
				newShape = new VEShape(VEShapeType.Pushpin, clickedLatLong);
				// newShape.SetCustomIcon("<img src=\'pins.png\' />");
			}
			
			
			// Clear out the old shape
			// If this isn't done then the lines all start from the first place clicked.
			map.DeleteAllShapes();
			
			// Display the important places again because the code above deletes them all.
			if (g_blnImportantPlaces == true) {
				DisplayImportantPlaces();
			}
			
			// NEW CODE.
			//alert("g_blnMappedWalkRoutes = " + g_blnMappedWalkRoutes);
			// Display mapped walk routes if indicated.
			if (g_blnMappedWalkRoutes == true) {
				DisplayMappedWalkRoutes('No');
			}
			
			
			
			// New Code.
			// This code didn't work.
//			if (g_blnRouteStart == true){
//				alert("clickedLatLong = " + clickedLatLong);
//				var route_start_points = new VELatLong(clickedLatLong);
//				route_start_points = clickedLatLong;
//				alert("route_start_points = " + route_start_points);
//				AddPushpin(route_start_points, "Route Start", "", "");	
//			}
			
			
			
			// Add our new shape
			map.AddShape(newShape);
			
			CalculateDistance();
				
		} else {
			// Important Places Form.
		
			// Set the value of the hidden textbox to the points (Lat and Long). 
			document.getElementById("txtMapPoints_Hidden").value = clickedLatLong;

			var newShape = null;
		
			// If this is the first Point plotted, then show it as a Pushpin
			newShape = new VEShape(VEShapeType.Pushpin, clickedLatLong);
		
		
			// New Code.
			// This is for the Park Space Form only.
			if (objForm.name == "frmParkSpace"){
				icon = icon_park_space;
			}
			
		
			
			// Use a custom icon if indicated.
			if (icon != "") {
				newShape.SetCustomIcon(icon);
				
				// Save the value of icon because it gets reset to "" for some reason.  Will set icon to last_icon later.
				var last_icon = icon;
			}
			
		
			// If the user has made another selection delete the existing one.
			map.DeleteAllShapes();
			
			// Display the important places again because the code above deletes them all.
			if (g_blnImportantPlaces == true) {
				DisplayImportantPlaces();
			}
			
			
			// Display the new shape (Pushpin) on the map.
			map.AddShape(newShape);
			
			
			// For some reason the icon value gets reset to "" after this method so save the value of icon after it is used
			// then set it to the variable last_icon.  Here we set the value of icon to last_icon.
			icon = last_icon;
					
		}
    }
}




// The Map onclick event handler
function Map_Click_Original(eventArgs)
{
    var clickedLatLong = eventArgs.latlong;
	
    if (map.GetMapMode() == VEMapMode.Mode2D)
    {
        // In 2D Mode the eventArgs.latlong property doesn't contain the lat/long point that was clicked.
        // So we must figure it out using the x and y coordinates of the point on the map that was clicked.
        clickedLatLong = map.PixelToLatLong(new VEPixel(eventArgs.mapX, eventArgs.mapY));
    }
    
    // When the user Right-Clicks, Draw the Polygon
    if (eventArgs.rightMouseButton)
    {
		
		// New Code.
		// Only do this when dealing with the Walk Route form.
		if (objForm.name == "frmWalkRoute"){			
		
			// Show the points in the textbox. seperate each coordinate with a ';'
			if (document.getElementById("txtMapPoints_Hidden").value != '')
			{
				document.getElementById("txtMapPoints_Hidden").value = document.getElementById("txtMapPoints_Hidden").value + ';' + clickedLatLong;
			}
			else
			{
				document.getElementById("txtMapPoints_Hidden").value = clickedLatLong;
			}
			
			var newShape = null;
			
			
			if (map.GetShapeLayerCount() > 0)
			{
				// Get reference to the Default Shape Layer
				var shapeLayer = map.GetShapeLayerByIndex(0);
				
				if (shapeLayer.GetShapeCount() > 0)
				{
					// Get points already plotted
					var points = shapeLayer.GetShapeByIndex(0).GetPoints();
					// Add the newly clicked point to the collection of points
					points[points.length] = clickedLatLong;
					
					newShape = new VEShape(VEShapeType.Polyline, points);
					// Don't show the shapes icon on the map, we only want to see the line
					newShape.HideIcon();
				}
			}
				
			
			if (newShape == null)
			{
				// If this is the first Point plotted, then show it as a Pushpin
				newShape = new VEShape(VEShapeType.Pushpin, clickedLatLong);
				// newShape.SetCustomIcon("<img src=\'pins.png\' />");
			}
			
			// Clear out the old shape
			map.DeleteAllShapes();
			
			// Add our new shape
			map.AddShape(newShape);
			
			CalculateDistance();
			
		} else {
			// Important Places Form.
		
			// Set the value of the hidden textbox to the points (Lat and Long). 
			document.getElementById("txtMapPoints_Hidden").value = clickedLatLong;

			var newShape = null;
		
			// If this is the first Point plotted, then show it as a Pushpin
			newShape = new VEShape(VEShapeType.Pushpin, clickedLatLong);
			
			
			// Use a custom icon if indicated.
			if (icon != "") {
				newShape.SetCustomIcon(icon);
			}
			
		
			// If the user has made another selection delete the existing one.
			map.DeleteAllShapes();
			
			// Display the new shape (Pushpin) on the map.
			map.AddShape(newShape);
		}
    }
}



function CalculateDistance()
{
//    var distMessage = document.getElementById("spanDistanceMessage");
//    var dist = document.getElementById("spanDistance");
//	var txtDist = document.getElementById("txtDistanceKM_Hidden");
	
	// New Code.
	var spanDistanceKM = document.getElementById("spanDistanceKM")
	var spanDistanceMI = document.getElementById("spanDistanceMI")
	
	var txtDistanceKM_Hidden = document.getElementById("txtDistanceKM_Hidden");
		
//	var txtDistanceKM = document.getElementById("txtDistanceKM")
//	var txtDistanceMI = document.getElementById("txtDistanceMI")
    
    var points = new Array();
    
    // Get all the Points being plotted
    if (map.GetShapeLayerCount() > 0)
    {
        var shapeLayer = map.GetShapeLayerByIndex(0);
        if (shapeLayer.GetShapeCount() > 0)
        {
            points = shapeLayer.GetShapeByIndex(0).GetPoints();
        }
    }
    
    if (points.length <= 1)
    {   
        // Display message to plot more points
//        distMessage.style.display = "";
//        dist.style.display = "none";
		
		// New Code.
		// Clear the distance span values.
		spanDistanceKM.innerHTML = "0.00";
		spanDistanceMI.innerHTML = "0.00";
		// Clear the textbox values.
//		txtDistanceKM.value = "";
//		txtDistanceMI.value = "";
    }
    else
    {
        // Display the Distance of all the points being plotted
//        distMessage.style.display = "none";
//        dist.style.display = "";
        
//        var dblDistance = 0.0;
        
//        var unit = GeoCodeCalc.EarthRadiusInMiles;
//        if (!document.getElementById("rdMiles").checked) unit = GeoCodeCalc.EarthRadiusInKilometers;
//        
//        for(var i = 1; i < points.length; i++)
//        {
//            dblDistance += GeoCodeCalc.CalcDistance(points[i - 1].Latitude, points[i - 1].Longitude, points[i].Latitude, points[i].Longitude, unit);
//        }
//        
//        dist.innerHTML = (Math.round(dblDistance*100)/100).toString() + " " + (unit == GeoCodeCalc.EarthRadiusInMiles ? "Miles" : "Kilometers");
//		txtDist.value = dblDistance.toString();
		
		
		// New Code.
		
		// Calulate the distance in both KM's and Miles and display the values in the correct textbox.
		var dblDistance = 0.0;
		var unit = GeoCodeCalc.EarthRadiusInKilometers;
		for(var i = 1; i < points.length; i++)
        {
            dblDistance += GeoCodeCalc.CalcDistance(points[i - 1].Latitude, points[i - 1].Longitude, points[i].Latitude, points[i].Longitude, unit);
        }
		
		spanDistanceKM.innerHTML = (Math.round(dblDistance*100)/100).toString(); // + " " + "Kilometers";
		
		txtDistanceKM_Hidden.value = (Math.round(dblDistance*100)/100).toString();
								
		//txtDistanceKM.value = (Math.round(dblDistance*100)/100).toString();
		
		
		
		dblDistance = 0.0;
		unit = GeoCodeCalc.EarthRadiusInMiles;
		for(var i = 1; i < points.length; i++)
        {
            dblDistance += GeoCodeCalc.CalcDistance(points[i - 1].Latitude, points[i - 1].Longitude, points[i].Latitude, points[i].Longitude, unit);
        }
		
		spanDistanceMI.innerHTML = (Math.round(dblDistance*100)/100).toString(); // + " " + "Miles";
		
		//txtDistanceMI.value = (Math.round(dblDistance*100)/100).toString();
		
    }
}


function btnClearMap_Click()
{
    // Clear all points plotted
    map.DeleteAllShapes();
	
	
	// Show or hide Mapped Important Places based on if the checkbox is checked or not.
	var chkShowImportantPlaces = document.getElementById("chkShowImportantPlaces");
	
	// NEW CODE.
	var chkShowGCARoutes = document.getElementById("chkShowGCARoutes");
	
	// Display the important places again because the code above deletes them all.
	if (g_blnImportantPlaces == true) {
		DisplayImportantPlaces();
		ShowHideImportantPlaces(chkShowImportantPlaces);
	}			
	
	// NEW CODE.
	// Display the gca walk routes again because the code above deletes them all.	
	if (g_blnMappedWalkRoutes == true) {
		DisplayMappedWalkRoutes('No');
		ShowHideWalkRoutes_GCA(chkShowGCARoutes);
	}
	
	// Clear textbox.
	document.getElementById("txtMapPoints_Hidden").value = '';
	
	// Only do this when dealing with the Walk Route form.
	if (objForm.name == "frmWalkRoute"){
	    // ReCalculate - so we can hide the last calculated distance
    	CalculateDistance();
	}
}


function Unit_Click()
{
	// Only do this when dealing with the Walk Route form.
	if (objForm.name == "frmWalkRoute"){
	    // ReCalculate the distance when the distance unit is changed
    	CalculateDistance();
	}
}


function trimString(str)
{
  return str.replace(/^\s+|\s+$/g, '');
}


function checkRequired() {
	
	var errmsg = "";
	var errhdr = "";
	var errftr = "";
	
	if (trimString(document.forms.frmCreateMap.txtMapPoints_Hidden.value) == '') {
		errmsg = errmsg +  "   - Please right-click the map to create your route\n";
	}
	
	if (trimString(document.forms.frmCreateMap.txtTitle.value) == '') {
		errmsg = errmsg +  "   - Please enter a title\n";
	}
	
	if (errmsg != "") {
		errhdr = "The following fields are required:\n\n";
		errftr = "\nClick 'OK' to correct these errors.";
		errmsg = errhdr + errmsg + errftr;
		alert(errmsg);
		return false;
	} else {
		return true;
	}
}






/* ******************************************************** */
/* The code for the GeoCodeCalc class was copied from here: */
/* http://pietschsoft.com/Blog/Post.aspx?PostID=1452        */
/* ******************************************************** */
var GeoCodeCalc = {};
GeoCodeCalc.EarthRadiusInMiles = 3956.0;
GeoCodeCalc.EarthRadiusInKilometers = 6367.0;

GeoCodeCalc.ToRadian = function(v) {
    return v * (Math.PI / 180);
};

GeoCodeCalc.DiffRadian = function(v1, v2) {
    return GeoCodeCalc.ToRadian(v2) - GeoCodeCalc.ToRadian(v1);
};

GeoCodeCalc.CalcDistance = function(lat1, lng1, lat2, lng2, radius) {
    return radius * 2 * Math.asin(
            Math.min(1,
                Math.sqrt(
                    (
                        Math.pow(Math.sin((GeoCodeCalc.DiffRadian(lat1, lat2)) / 2.0), 2.0) +
                        Math.cos(GeoCodeCalc.ToRadian(lat1)) * Math.cos(GeoCodeCalc.ToRadian(lat2)) *
                        Math.pow(Math.sin((GeoCodeCalc.DiffRadian(lng1, lng2)) / 2.0), 2.0)
                    )
               )
           )
       );
};