﻿// JScript File for GoogleMap

    var map;
    var gdir;
    var geocoder = null;
    var addressMarker;

    function initialize(from,to,lang) {
 
      if (GBrowserIsCompatible()) { 
      //  alert("dans initialize -" + document.getElementById(from).value + " - " + document.getElementById(to).value + " - " + lang);     
        map = new GMap2(document.getElementById("map_canvas"));
        
        gdir = new GDirections(map, document.getElementById("directions"));
        GEvent.addListener(gdir, "addoverlay", onGDirectionsLoad);
        GEvent.addListener(gdir, "error", handleErrors);
        setDirections( from , to, lang);
      }
    }
    
    function setDirections(fromAddress, toAddress, locale) {
   // alert(document.getElementById(toAddress).value);
        var to = document.getElementById(toAddress).value;
        while(to.indexOf("_") != -1){
             to = to.replace("_","'");
        }
        var from = document.getElementById(fromAddress).value;
        while(from.indexOf("_") != -1){
             from = from.replace("_","'");
        }
    
      gdir.load("from: " + from + " to: " + to,
                { "locale": locale });
    }

    function handleErrors(){
   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
     alert(document.getElementById('ctl00_hfError1').value + gdir.getStatus().code);
   else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
     alert(document.getElementById('ctl00_hfError2').value + gdir.getStatus().code);
   else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
     alert(document.getElementById('ctl00_hfError3').value + gdir.getStatus().code);
   else if (gdir.getStatus().code == G_GEO_BAD_KEY)
     alert(document.getElementById('ctl00_hfError4').value + gdir.getStatus().code);
   else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
     alert(document.getElementById('ctl00_hfError5').value + gdir.getStatus().code);
   else alert(document.getElementById('ctl00_hfError6').value);
    }

  function onGDirectionsLoad(){ 
   var poly = gdir.getPolyline();
   
   if (poly.getVertexCount() > 100) {
   //  alert("This route has too many vertices");
     return;
   }
   var baseUrl = "http://maps.google.com/staticmap?";

   var params = [];
   var markersArray = [];
   markersArray.push(poly.getVertex(0).toUrlValue(5) + ",greena");
   markersArray.push(poly.getVertex(poly.getVertexCount()-1).toUrlValue(5) + ",greenb");
   params.push("markers=" + markersArray.join("|"));

   var polyParams = "rgba:0x0000FF80,weight:5|";
   var polyLatLngs = [];
  
   for (var j = 0; j < poly.getVertexCount(); j++) {
     polyLatLngs.push(poly.getVertex(j).lat().toFixed(5) + "," + poly.getVertex(j).lng().toFixed(5));
   }

   params.push("path=" + polyParams + polyLatLngs.join("|"));
   params.push("size=300x300");
   
   params.push("key=" + document.getElementById('ctl00_hfGoogleMapKey').value );
  // params.push("key=ABQIAAAA3YaFLZZI2zdJOq0Z6zXjnRSMbLTlJcDGseYPGYX7DPkFaGcAVxSGfOq-8UuvnT-ixOgoTDEfjghXbg");

   baseUrl += params.join("&");

   var extraParams = [];
   extraParams.push("center=" + map.getCenter().lat().toFixed(6) + "," + map.getCenter().lng().toFixed(6));
   extraParams.push("zoom=" + map.getZoom());
   addImg(baseUrl + "&" + extraParams.join("&"), "staticMapOverviewIMG");

   var extraParams = [];
   extraParams.push("center=" + poly.getVertex(0).toUrlValue(5));
   extraParams.push("zoom=" + 15);
   addImg(baseUrl + "&" + extraParams.join("&"), "staticMapStartIMG");

   var extraParams = [];
   extraParams.push("center=" + poly.getVertex(poly.getVertexCount()-1).toUrlValue(5));
   extraParams.push("zoom=" + 15);
   addImg(baseUrl + "&" + extraParams.join("&"), "staticMapEndIMG");
}

function addImg(url, id) {
 var img = document.createElement("img");
 img.src = url;
 document.getElementById(id).innerHTML = "";
 document.getElementById(id).appendChild(img);
}


 function LinkNewLanguage(url){
        var opt = '_self';
      //  if (url.indexOf("facture.aspx")!= -1) {opt = '_blank';}
        window.open(url,opt);
     //   initialize('ctl00_hfFrom','ctl00_hfTo','en_usa');
        return false;
}
      