﻿var myPxyServer = "";
var myPxyPort = "";
var myPxyPath = "/MapQuestDrivingDirectionProxy/MapQuestProxy.aspx";
var mqPort = "80";
var mqPath = "mq";
var myGExec = new MQExec("geocode.access.mapquest.com",mqPath,mqPort,myPxyServer,myPxyPath,myPxyPort);
var myRExec = new MQExec("route.access.mapquest.com",mqPath,mqPort,myPxyServer,myPxyPath,myPxyPort);
var myTAddress = new MQAddress();
var myFAddress = new MQAddress();
var myGcColl = new MQLocationCollection("MQGeoAddress");
var myRtColl = new MQLocationCollection("MQGeoAddress");
var myOptions = new MQRouteOptions();
var myRtResults = new MQRouteResults();
var myTrkRtColl = new MQTrekRouteCollection();
var myManeuverColl = new MQManeuverCollection();
var myManStr = "";
var myTrkStr = "";
var mySessID = myRExec.createSessionEx(new MQSession());
var myBoundingBox = new MQRectLL(new MQLatLng(),new MQLatLng());
var FromAddress;
var FromCity;
var FromPostalCode;
var FromState;
var ToAddress;
var ToCity;
var ToState;
var ToPostalCode;
var myMap;
var myMapInit;
var startLatLong;
var endLatLong;

Sys.Application.add_unload(page_unload);

function ShowDynamicMap()
{
       
    var myPoint = new  MQLatLng(document.getElementById('ComLat').value, document.getElementById('ComLong').value);
    myMapInit = new MQMapInit(); 
    myMap = new MQTileMap(document.getElementById('mapDiv'),8,myPoint,"map",myMapInit);
    var protocol = window.location.protocol;
    var hostName = window.location.host;
    var startImageUrl = protocol+"//"+hostName+"/images/icons/map_icons/house.gif";
    document.getElementById("mapDiv").style.visibility = "visible";
	AddPointToMap(myPoint.lat, myPoint.lng, startImageUrl,22,25, myMap, "start")
    myMap.addControl(new MQZoomControl());
    
}

function  HasQueryStringMQ()
{
     var queryString = window.location.search.substring(1);
     return (queryString.indexOf("mqmap")>-1);
}

function IsGetRouteClicked(args)
{
    try
    {
     postbackElement = args.get_postBackElement();
    if (typeof(postbackElement) === "undefined") 
    {
        return false;
    } 
    else if (postbackElement.id.indexOf('btnRoute') > -1 )
    {
       return true
    }
    }
    catch(ex)
    {
      return false;
    }
}

function IsMapQuestLinkClicked(args)
{
  try
    {
    postbackElement = args.get_postBackElement();
    if (typeof(postbackElement) === "undefined") 
    {
        return false;
    } 
    else if (postbackElement.id.indexOf('plhMapQuestLink') > -1 )
    {
       return true
    }
    }
     catch(ex)
    {
      return false;
    }
}

function GetDrivingDirections()
{
    clearInfo()
   myOptions.setMaxShapePointsPerManeuver(200);
   GetDrivingDirectionsValue();
}

function GetDrivingDirectionsValue()
{
    FromAddress=document.getElementById(document.getElementById('FromAddressField').value).value; //$get('FromAddressField').value;
    FromCity=document.getElementById(document.getElementById('FromCityField').value).value;
    FromState=document.getElementById(document.getElementById('FromStateField').value).value;
    FromPostalCode=document.getElementById(document.getElementById('FromPostalCodeField').value).value;
    ToAddress=document.getElementById('ToAddressField').value;
    ToCity=document.getElementById('ToCityField').value;
    ToState=document.getElementById('ToStateField').value;
    ToPostalCode=document.getElementById('ToPostalCodeField').value;
    popInfo();
    
    
}
 
 
//---------------------------------------------------------------------------------------------------------------------------
function popInfo() {
//	if (document.getElementById("btnRoute").value == "Get Route"){
		myFAddress.setStreet(FromAddress);
		myFAddress.setCity(FromCity);
		myFAddress.setState(FromState);
		myFAddress.setPostalCode(FromPostalCode);
		myFAddress.setCountry("US");
		myTAddress.setStreet(ToAddress);
		myTAddress.setCity(ToCity);
		myTAddress.setState(ToState);
		myTAddress.setPostalCode(ToPostalCode);
		myTAddress.setCountry("US");
		myOptions.setLanguage('english');
		//document.getElementById("btnRoute").value = "Clear Route"
		geocodeThem();
//	}
//	else {
//		document.getElementById("btnRoute").value = "Get Route"
//		clearInfo();
//	}
}
 
 
//-----------------------------------------------------------------------------------------------------------------------------
function geocodeThem() {
	myGExec.geocode(myFAddress, myGcColl);
	myRtColl.add(myGcColl.get(0));
	startLatLong = myGcColl.get(0).getMQLatLng();
	myGExec.geocode(myTAddress, myGcColl);
	myRtColl.add(myGcColl.get(0));
	endLatLong = myGcColl.get(0).getMQLatLng();
	getRoute();
}
 
 
//------------------------------------------------------------------------------------------------------------------------------
function getRoute() {
	myRExec.doRoute(myRtColl,myOptions,myRtResults,mySessID,myBoundingBox);
	displayIt();
}
 
 
//------------------------------------------------------------------------------------------------------------------------------
function displayIt() {
	var myMinutes = myRtResults.getTime()/60;
	var timeTaken=null;
	var time=null;
	if (myMinutes > 60) {
		if (myMinutes/60 == 1) {
			var myTotTime = "1 hr ";
		}
		else {
			var myTotTime = Math.round((myMinutes/60)*100)/100 + " hrs";
		}
	}
	else {
		var myTotTime = myMinutes + " min";
	}
	var myDist = Math.round(myRtResults.getDistance()*100)/100;
 
	myTrkRtColl = myRtResults.getTrekRoutes();
	myManeuverColl = myTrkRtColl.get(0).getManeuvers();
	myTrkStr ="<table><tr>"+
        "<td align='right' width='25'>&nbsp;</td>"+
        "<td align='left' width='450' colspan='2'><strong>Directions</strong></td>"+
        "<td align='right' width='80'><strong>Distance (mi)</strong></td>"+
         "<td align='right' width='80'><strong>Driving Time</strong></td>"+
      "</tr>"
   
	for (intX = 0; intX < myManeuverColl.getSize(); intX ++){
	    
		myManDist = Math.round(myManeuverColl.get(intX).getDistance()*100)/100;
		timeTaken =  myManeuverColl.get(intX).getTime()/60;
		if (timeTaken > 60)
		{
		    if (timeTaken/60 == 1)
		    {
			    var time = "1 hr ";
		    }
		    else 
		    {
			    var time = Math.round((timeTaken/60)*100)/100 + " hrs";
		    }
	    }
	    else 
	    {
		    var time = timeTaken + " min";
	    }
		myManStr ="<tr><td align='right' width='25'><b>"+(intX +1)+ ".</b></td><td align='left' width='450' colspan='2'><b>"+ myManeuverColl.get(intX).getNarrative()+"</b></td><td align='right' width='80'><b>" + myManDist + "</b></td><td align='right' width='80'><b>"+ time+"</b></td></tr>" ;
		myTrkStr = myTrkStr + myManStr;
 
	}
	//myTrkStr = myTrkStr +"<hr/>";
	document.getElementById("routeDiv").innerHTML =  myTrkStr + "<tr><td colspan='5'><hr/></td><tr/><tr><td align='right' width='25'>&nbsp;</td><td align='left' width='400' colspan='2'></td><td align='right' width='80'><strong>Total: </b>" + myDist + "<b> </b></td><td align='right' ><b>" + myTotTime + "</b></td></tr></table>";
 
	myMap = new MQTileMap(document.getElementById('mapDiv'));
  	myMap.addRouteHighlight(myBoundingBox,"http://map.access.mapquest.com",mySessID,true);
  	myMap.addControl(new MQZoomControl());
  	var protocol = window.location.protocol;
    var hostName = window.location.host;
    var startImageUrl = protocol+"//"+hostName+"/images/icons//map_icons/start.gif";
    var endImageUrl = protocol+"//"+hostName+"/images/icons/map_icons/end.gif";
	document.getElementById("mapDiv").style.visibility = "visible";
	AddPointToMap(startLatLong.lat, startLatLong.lng, startImageUrl,16,52, myMap, "start")
	AddPointToMap(endLatLong.lat, endLatLong.lng, endImageUrl,17,52,myMap, "end")
	
}


 function AddPointToMap(latitude, longitude, mqIconPath,height,width,theMap, pointKey)
{
               var nhsMapIcon = new MQMapIcon();
                nhsMapIcon.setImage(mqIconPath,width,height,true,false);
                nhsMapIcon.setShadow("",0,0,0,0,false);        
                mqPoint = new MQPoi(new MQLatLng(latitude,longitude), new MQMapIcon(nhsMapIcon));            
                mqPoint.setKey(pointKey);     
                theMap.addPoi(mqPoint); 
}
 
 
//--------------------------------------------------------------------------------------------------------------------------------
function clearInfo(){
    if(myMap!=null)
    {
	    myMap.removeRouteHighlight();
	}
	myTrkStr = "";
	myTAddress = new MQAddress();
	myFAddress = new MQAddress();
	myGcColl = new MQLocationCollection("MQGeoAddress");
	myRtColl = new MQLocationCollection("MQGeoAddress");
	myOptions = new MQRouteOptions();
	myRtResults = new MQRouteResults();
	myBoundingBox = new MQRectLL(new MQLatLng(),new MQLatLng());
	document.getElementById("routeDiv").innerHTML = '';
	document.getElementById("mapDiv").style.visibility = "hidden";
	document.getElementById("mapDiv").innerHTML = '';
}

function page_unload(sender, e) {
    if(myMap!=null)
    {
        myMap.removeAllPois();
    }
}
