﻿// JScript File

function addtextBox()
{
     document.getElementById("extradiv").innerHTML +='<input id="Text1" type="text" />';
}
  var directionDisplay;
  var directionsService = new google.maps.DirectionsService();
  var map;
 
  function initialize() 
  {
  //alert('map1');
    directionsDisplay = new google.maps.DirectionsRenderer();
    var chicago = new google.maps.LatLng(16.41667,80.25); //andhra pradesh
    //var chicago = new google.maps.LatLng(15.866667,75.00);
    var myOptions = 
                    {
                      zoom:7,
                      mapTypeId: google.maps.MapTypeId.ROADMAP,
                      center: chicago
                    }
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    directionsDisplay.setMap(map);//alert('s');
    directionsDisplay.setPanel(document.getElementById("ctl00_ContentPlaceHolder5_directionsPanel"));
       //alert('map');
  }
  
  function calcRoute() 
  { //alert('calcRoute');
    var start = document.getElementById("ctl00_ContentPlaceHolder5_txtFrom").value+',Andhra Pradesh,India';
    var end = document.getElementById("ctl00_ContentPlaceHolder5_txtTo").value+',Andhra Pradesh,India';
    
     var waypts = [];

      var checkboxArray;
      if(document.getElementById("extradiv")!=null)
      {    //alert(start+','+end);
      checkboxArray = document.getElementById("extradiv").getElementsByTagName('input');
      
      }
//alert(checkboxArray.length);
      
      for (var i = 0; i < checkboxArray.length; i++) 
      {
        if (checkboxArray[i].value != "") 
        {
        //checkboxArray[i].value=checkboxArray[i].value;
          waypts.push({location:checkboxArray[i].value+',Andhra Pradesh,India',stopover:true});
        }
        
      }

    var request = 
                    {
                        origin:start, 
                        destination:end,
                        waypoints: waypts,
                        optimizeWaypoints: true,
                        travelMode: google.maps.DirectionsTravelMode.DRIVING
                    };
                   
    directionsService.route(request, function(response, status) 
                        {//alert(status);
                                  if (status == google.maps.DirectionsStatus.OK) 
                                  {
                                    directionsDisplay.setDirections(response);
                                    //this is for the summary of route
                                    
//                                    var route = response.routes[0];
                        var summaryPanel = document.getElementById("ctl00_ContentPlaceHolder5_directionsPanel");
                        summaryPanel.innerHTML = "";
                        //alert(route);
                        // For each route, display summary information.
//                        for (var i = 0; i < route.legs.length; i++) {
//                          var routeSegment = i + 1;
//                          summaryPanel.innerHTML += "<b>Route Segment: " + routeSegment + "</b><br />";
//                          summaryPanel.innerHTML += route.legs[i].start_geocode.formatted_address + " to ";
//                          summaryPanel.innerHTML += route.legs[i].end_geocode.formatted_address + "<br />";
//                          summaryPanel.innerHTML += route.legs[i].distance.text + "<br /><br />";
//                        }
                                  }
                        }
    
                        );
  }

    var mapone;
    var directionsPanel;
    var directions;
function getDirections(fromcity,toCity,dirPanel,mapPanel) 
{
   //alert('getdire');
      mapone = new GMap2(document.getElementById(mapPanel));
      mapone.setCenter(new GLatLng(16.41667,80.25), 6);
      //document.getElementById(dirPanel).innerHTML="";
      directionsPanel = document.getElementById(dirPanel);
      directions = new GDirections(mapone, directionsPanel);
      GEvent.addListener(directions, "error", handleErrors);
        if(fromcity!=null &&toCity!=null)
        {
//        var point1 = new GLatLng(15.333, 76.667);
//    var point2 = new GLatLng(11.8081, 76.6918);
            directions.load("from: "+fromcity+" to: "+toCity, {travelMode:G_TRAVEL_MODE_DRIVING});
    // directions.load("from: " + point1.lat() + "," + point1.lng()  + " to: " + point2.lat() + "," + point2.lng());

        }
        mapone.setUIToDefault(); 
    }

function handleErrors()
{
	   if (directions.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: " + directions.getStatus().code);
	   else if (directions.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: " + directions.getStatus().code);
	   
	   else if (directions.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: " + directions.getStatus().code);
 
	
	   else if (directions.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: " + directions.getStatus().code);
 
	   else if (directions.getStatus().code == G_GEO_BAD_REQUEST)
	     alert("A directions request could not be successfully parsed.\n Error code: " + directions.getStatus().code);
	    
	   else alert("An unknown error occurred.");
	  
	  
}


