﻿window.usestep = false;

var gdir = null;
var geocoder = null;
var map = null;

var add1 = true;
var add2 = true;

var isgeocodingfrom = false;
var isgeocodingto = false;

window.musthavefinishstreet = true;

window.haschangemap = true;
function changemap(){
    if (map !== null){
    var country = getcountry(); //cc[Get_Cookie("cc")];
    var sw = new GLatLng(country[4],country[5]);
    var ne = new GLatLng(country[3],country[6]);                                
    var bounds = new GLatLngBounds(sw, ne);
    map.setCenter(bounds.getCenter(),map.getBoundsZoomLevel(bounds));
    if (geocoder !== null){
        geocoder.setBaseCountryCode(getCountryCode());
    }
    }
}

window.onload = function() {
    if (GBrowserIsCompatible()) {
        if(window.loadmap == 'yes'){
            document.getElementById('mapc').style.display = 'block';
            map = new GMap2(document.getElementById("map"));
            map.addControl(new GScaleControl());
            map.addControl(new GSmallMapControl());
            map.addControl(new GMapTypeControl(true));
            changemap();
        }
        geocoder = new GClientGeocoder();
        geocoder.setBaseCountryCode(getCountryCode());
               
    } else {
        alert("Brower doesn't support google maps");
    }
};





window.onunload = function() {
    GUnload();
};


function getCountryCode(){
    return getcountry()[1];
}

function getfromaddress(){
    return document.getElementById(window.fromID).value;

}

function gettoaddress(){
    
    if (window.hLocationID !== 'none'){
        return document.getElementById(window.hLocationID).value;
    } else {
        return document.getElementById(window.toID).value + ", " + getCountryCode();
    }
}

function settoaddress(add){
    if (window.hLocationID !== 'none'){
        document.getElementById(window.hLocationID).value = add;
    } else {
        document.getElementById(window.toID).value = add;
    }
}

function findroute() {
document.getElementById("toerr").style.display = "none";
document.getElementById("fromerr").style.display = "none";
    if (window.StepID !== "1" && window.usestep){
        var b = document.getElementById(window.startNext);
        b.click();
    } else {
    if (document.getElementById(window.fromID).value.length <= 1) {
        alert("please enter a start address");
    } else if (gettoaddress().length <= 1) {
        alert("please enter a finish address");
    } else {

        add1 = false;
        add2 = false;
        if (gdir == null) {
            if (map !== null) {
                gdir = new GDirections(map);
            } else {
                gdir = new GDirections();
            }
            GEvent.addListener(gdir, "load", onGDirectionsLoad);
            GEvent.addListener(gdir, "error", onGDirectionsError);
        }
       // alert("from: " + document.getElementById(window.fromID).value + " to: " + gettoaddress());
        gdir.load("from: " + document.getElementById(window.fromID).value + " to: " + gettoaddress() , { preserveViewport: false, locale: "en", getPolyline: true });
    }
    }
    

}


function onGDirectionsLoad() {
    var addr0 = gdir.getGeocode(0);
    var addr1 = gdir.getGeocode(1);
    var regular = document.getElementById(window.RegularID).checked;
    if (regular) {
        if (addr0.AddressDetails.Accuracy < 6 && addr1.AddressDetails.Accuracy >= 6) {
            alert("Please use street address for start address");return;
        }
        if (addr0.AddressDetails.Accuracy >= 6 && (addr1.AddressDetails.Accuracy < 6 && window.musthavefinishstreet)) {
            alert("Please use a street address for finish address"); return;
        }
        if (addr0.AddressDetails.Accuracy < 6 && (addr1.AddressDetails.Accuracy < 6 && window.musthavefinishstreet)) {
            alert("Please use street address for start and finish addresses"); return;
        }
    }
//    var pl = gdir.getPolyline();
//    var bounds = pl.getBounds();
//    createmapb(bounds);
//    map.clearOverlays();
//    map.addOverlay(pl);
    
    
    document.getElementById(window.fromID).value = addr0.address;
    settoaddress(addr1.address);
    document.getElementById(window.hMeters).value = gdir.getDistance().meters;
    document.getElementById(window.hSeconds).value = gdir.getDuration().seconds;
    document.getElementById(window.hX1).value = addr0.Point.coordinates[0];
    document.getElementById(window.hY1).value = addr0.Point.coordinates[1];
    document.getElementById(window.hX2).value = addr1.Point.coordinates[0];
    document.getElementById(window.hY2).value = addr1.Point.coordinates[1];
    
   

    if (addr0.AddressDetails.Country) {
        document.getElementById(window.hCountry1).value = addr0.AddressDetails.Country.CountryNameCode;
        if (addr0.AddressDetails.Country.AdministrativeArea) {
            document.getElementById(window.hState1).value = addr0.AddressDetails.Country.AdministrativeArea.AdministrativeAreaName;
            if (addr0.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea) {
                if (addr0.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality) {
                    this.city = addr0.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName;
                    if (addr0.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.Thoroughfare) {
                        document.getElementById(window.hStreet1).value = addr0.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.Thoroughfare.ThoroughfareName;
                    }
                    if (addr0.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.PostalCode) {
                        document.getElementById(window.hPostcode1).value = addr0.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.PostalCode.PostalCodeNumber;
                    }
                } else {
                    this.city = addr0.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.SubAdministrativeAreaName;
                    if (addr0.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Thoroughfare) {
                        this.streetaddr = addr0.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Thoroughfare.ThoroughfareName;
                    }
                    if (addr0.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.PostalCode) {
                        document.getElementById(window.hPostcode1).value = addr0.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.PostalCode.PostalCodeNumber;
                    }
                }
            } else {
                if (addr0.AddressDetails.Country.AdministrativeArea.Locality) {
                    document.getElementById(window.hCity1).value = addr0.AddressDetails.Country.AdministrativeArea.Locality.LocalityName;
                    if (addr0.AddressDetails.Country.AdministrativeArea.Locality.Thoroughfare) {
                        document.getElementById(window.hStreet1).value = addr0.AddressDetails.Country.AdministrativeArea.Locality.Thoroughfare.ThoroughfareName;
                    }
                    if (addr0.AddressDetails.Country.AdministrativeArea.Locality.PostalCode) {
                        document.getElementById(window.hPostcode1).value = addr0.AddressDetails.Country.AdministrativeArea.Locality.PostalCode.PostalCodeNumber;
                    }
                } else {
                    document.getElementById(window.hCity1).value = addr0.AddressDetails.Country.AdministrativeArea.SubAdministrativeAreaName;
                    if (addr0.AddressDetails.Country.AdministrativeArea.Thoroughfare) {
                        document.getElementById(window.hStreet1).value = addr0.AddressDetails.Country.AdministrativeArea.Thoroughfare.ThoroughfareName;
                    }
                    if (addr0.AddressDetails.Country.AdministrativeArea.PostalCode) {
                        document.getElementById(window.hPostcode1).value = addr0.AddressDetails.Country.AdministrativeArea.PostalCode.PostalCodeNumber;
                    }
                }
            }
        }
    }

    if (addr1.AddressDetails.Country) {
        document.getElementById(window.hCountry2).value = addr1.AddressDetails.Country.CountryNameCode;
        if (addr1.AddressDetails.Country.AdministrativeArea) {
            document.getElementById(window.hState2).value = addr1.AddressDetails.Country.AdministrativeArea.AdministrativeAreaName;
            if (addr1.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea) {
                if (addr1.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality) {
                    this.city = addr1.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName;
                    if (addr1.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.Thoroughfare) {
                        document.getElementById(window.hStreet2).value = addr1.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.Thoroughfare.ThoroughfareName;
                    }
                    if (addr1.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.PostalCode) {
                        document.getElementById(window.hPostcode2).value = addr1.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.PostalCode.PostalCodeNumber;
                    }
                } else {
                    this.city = addr1.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.SubAdministrativeAreaName;
                    if (addr1.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Thoroughfare) {
                        this.streetaddr = addr1.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Thoroughfare.ThoroughfareName;
                    }
                    if (addr1.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.PostalCode) {
                        document.getElementById(window.hPostcode2).value = addr1.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.PostalCode.PostalCodeNumber;
                    }
                }
            } else {
                if (addr1.AddressDetails.Country.AdministrativeArea.Locality) {
                    document.getElementById(window.hCity2).value = addr1.AddressDetails.Country.AdministrativeArea.Locality.LocalityName;
                    if (addr1.AddressDetails.Country.AdministrativeArea.Locality.Thoroughfare) {
                        document.getElementById(window.hStreet2).value = addr1.AddressDetails.Country.AdministrativeArea.Locality.Thoroughfare.ThoroughfareName;
                    }
                    if (addr1.AddressDetails.Country.AdministrativeArea.Locality.PostalCode) {
                        document.getElementById(window.hPostcode2).value = addr1.AddressDetails.Country.AdministrativeArea.Locality.PostalCode.PostalCodeNumber;
                    }
                } else {
                    document.getElementById(window.hCity2).value = addr1.AddressDetails.Country.AdministrativeArea.SubAdministrativeAreaName;
                    if (addr1.AddressDetails.Country.AdministrativeArea.Thoroughfare) {
                        document.getElementById(window.hStreet2).value = addr1.AddressDetails.Country.AdministrativeArea.Thoroughfare.ThoroughfareName;
                    }
                    if (addr1.AddressDetails.Country.AdministrativeArea.PostalCode) {
                        document.getElementById(window.hPostcode2).value = addr1.AddressDetails.Country.AdministrativeArea.PostalCode.PostalCodeNumber;
                    }
                }
            }
        }
    }    
    add1 = true;
    add2 = true;

    if (window.startNext !== undefined && window.startNext  !== null) {
        var b = document.getElementById(window.startNext);
        b.click();
    }
}

function createmap(x, y, z) {
    if (map === null) {
        document.getElementById('mapc').style.display = 'block';
        map = new GMap2(document.getElementById("map"));
        //map.addControl(new GScaleControl());
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl(true));
    }
    if (x !== undefined) {
        map.setCenter(new GLatLng(y, x), z);
    }
    
}
function createmapb(bounds) {
    if (map === null) {
        document.getElementById('mapc').style.display = 'block';
        map = new GMap2(document.getElementById("map"));
        map.addControl(new GScaleControl());
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl(true));
        map.setCenter(bounds.mid,map.getBoundsZoomLevel(bounds));
    }
    map.clearOverlays();
}

function geocodeandmapfrom() {
    isgeocodingfrom = true;
    document.getElementById("fromerr").style.display = "none";
    document.getElementById("fromerr").innerHTML = "";
    //alert(document.getElementById(window.fromID).value + ", " + getCountryCode());
    geocoder.getLocations(document.getElementById(window.fromID).value, fromDisambig);
}




function geocodeandmapto() {
    isgeocodingto = true;
    document.getElementById("toerr").style.display = "none";
    document.getElementById("toerr").innerHTML = "";
    geocoder.getLocations(gettoaddress(), toDisambig);
}

function showAddress(x, y, z, s) {
    createmap(x,y,z);
    map.clearOverlays();
    var point = new GLatLng(y, x);
    var marker = new GMarker(point);
    map.setCenter(point, z);
    map.addOverlay(marker);
    //marker.openInfoWindowHtml(s);
}

// Event from directions directions object
function onGDirectionsError() {
    var status = gdir.getStatus();
    if (status.code == G_GEO_UNKNOWN_ADDRESS) {
        geocoder.getLocations(document.getElementById(window.fromID).value, fromDisambig);
        geocoder.getLocations(gettoaddress(), toDisambig);
    } else {
        alert("We could not find a route between these addresses. Please add more address information such as state and country, and make sure street, city, state, and country are separated by commas.");
    }
}


function setFrom(address) {
    document.getElementById(window.fromID).value = address;
    add1 = true;
    document.getElementById("fromerr").style.display = "none";
    document.getElementById("fromerr").innerHTML = "";
    if (add1 && add2) {
        findroute();
    }
}


function setTo(address) {
    settoaddress(address);
    add2 = true;
    document.getElementById("toerr").style.display = "none";
    document.getElementById("toerr").innerHTML = "";
    if (add1 && add2) {
        findroute();
    }    
}


function fromDisambig(response) {
    add1 = false;
    if (response.Placemark == undefined || response.Placemark == null) {
        alert("Could not locate From addres");
        return;
    }
    if (response.Placemark.length > 1) {
        document.getElementById("fromerr").style.display = "block";
        document.getElementById("fromerr").innerHTML = "<strong>Did you mean:</strong><br /><table width=\"100%\">";
        for (var i = 0; i < response.Placemark.length; ++i) {
            var href = "javascript:setFrom('" + response.Placemark[i].address + "', true)";
            var href1 = "javascript:showAddress(" + response.Placemark[i].Point.coordinates[0] + "," + response.Placemark[i].Point.coordinates[1] + ",13,'" + response.Placemark[i].address + "')";
            document.getElementById("fromerr").innerHTML += "<tr><td><a href=\"" + href + "\">" + response.Placemark[i].address + "</a></td><td align=\"right\" valign=\"middle\">" +
      "<a href=\"" + href1 + "\"><img border=\"0\" src=\"/images/map.png\" alt=\"Show this address on the map.\" /></a></td></tr><tr><td colspan=\"2\"><hr /></td></tr>";
        }
        document.getElementById("fromerr").innerHTML += "</table>";
        //document.getElementById("frominput").style.display = "none";
        document.getElementById("fromerr").style.display = "block";
    } else if (response.Placemark.length < 1) {
        alert("Could not locate From addres");
    } else {

        document.getElementById(window.fromID).value = response.Placemark[0].address;
        add1 = true;
        if (isgeocodingfrom) {
            showAddress(response.Placemark[0].Point.coordinates[0], response.Placemark[0].Point.coordinates[1], 13, response.Placemark[0].address);
            isgeocodingfrom = false;
        }        
    }
    
}


function locationDisambig(response) {
   
    add1 = false;
    if (response.Placemark == undefined || response.Placemark == null) {
        lat11=0;
        long11=0;
        document.getElementById("fromerr").style.display = "block";
        document.getElementById("fromerr").innerHTML="<b>Could not locate From addres</b>";
        return;
    }
    if (response.Placemark.length > 1) {
        document.getElementById("fromerr").innerHTML = "<strong>Did you mean:</strong><br /><table width=\"100%\">";
        for (var i = 0; i < response.Placemark.length; ++i) {
            var href = "javascript:setLocation('" + response.Placemark[i].address + "', true)";
            var href1 = "javascript:showAddress(" + response.Placemark[i].Point.coordinates[0] + "," + response.Placemark[i].Point.coordinates[1] + ",13,'" + response.Placemark[i].address + "')";
            document.getElementById("fromerr").innerHTML += "<tr><td><a href=\"" + href + "\">" + response.Placemark[i].address + "</a></td><td align=\"right\" valign=\"middle\">" +
      "</td></tr><tr><td colspan=\"2\"><hr /></td></tr>";
        }
        document.getElementById("fromerr").innerHTML += "</table>";
        //document.getElementById("frominput").style.display = "none";
        document.getElementById("fromerr").style.display = "block";
        lat11=0;
        long11=0;

    } else if (response.Placemark.length < 1) {
        document.getElementById("fromerr").style.display = "block";
        document.getElementById("fromerr").innerHTML="<b>Could not locate from addres</b>";
    } else {


        

    lat11=response.Placemark[0].Point.coordinates[1];
    long11=response.Placemark[0].Point.coordinates[0];

     
   if (response.Placemark[0].AddressDetails.Country) {
        GeoCodeCountry = response.Placemark[0].AddressDetails.Country.CountryNameCode;
        if (response.Placemark[0].AddressDetails.Country.AdministrativeArea) {
            GeoCodeState = response.Placemark[0].AddressDetails.Country.AdministrativeArea.AdministrativeAreaName;
            if (response.Placemark[0].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea) {
                if (response.Placemark[0].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality) {
                    GeoCodeCity = response.Placemark[0].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName;
                    if (response.Placemark[0].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.Thoroughfare) {
                        GeoCodeStreet = response.Placemark[0].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.Thoroughfare.ThoroughfareName;
                    }
                    if (response.Placemark[0].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.PostalCode) {
                        GeoCodeZip = response.Placemark[0].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.PostalCode.PostalCodeNumber;
                    }
                } else {
                    GeoCodeCity = response.Placemark[0].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.SubAdministrativeAreaName;
                    if (response.Placemark[0].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Thoroughfare) {
                        GeoCodeStreet = response.Placemark[0].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Thoroughfare.ThoroughfareName;
                    }
                    if (response.Placemark[0].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.PostalCode) {
                        GeoCodeZip = response.Placemark[0].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.PostalCode.PostalCodeNumber;
                    }
                }
            } else {
                if (response.Placemark[0].AddressDetails.Country.AdministrativeArea.Locality) {
                    GeoCodeCity = response.Placemark[0].AddressDetails.Country.AdministrativeArea.Locality.LocalityName;
                    if (response.Placemark[0].AddressDetails.Country.AdministrativeArea.Locality.Thoroughfare) {
                        GeoCodeStreet = response.Placemark[0].AddressDetails.Country.AdministrativeArea.Locality.Thoroughfare.ThoroughfareName;
                    }
                    if (response.Placemark[0].AddressDetails.Country.AdministrativeArea.Locality.PostalCode) {
                        GeoCodeZip = response.Placemark[0].AddressDetails.Country.AdministrativeArea.Locality.PostalCode.PostalCodeNumber;
                    }
                } else {
                    GeoCodeCity = response.Placemark[0].AddressDetails.Country.AdministrativeArea.SubAdministrativeAreaName;
                    if (response.Placemark[0].AddressDetails.Country.AdministrativeArea.Thoroughfare) {
                        GeoCodeStreet = response.Placemark[0].AddressDetails.Country.AdministrativeArea.Thoroughfare.ThoroughfareName;
                    }
                    if (response.Placemark[0].AddressDetails.Country.AdministrativeArea.PostalCode) {
                        GeoCodeZip = response.Placemark[0].AddressDetails.Country.AdministrativeArea.PostalCode.PostalCodeNumber;
                    }
                }
            }
        }
    }

  
        add1 = true;
     
        if (isgeocodingfrom) {
            showAddress(response.Placemark[0].Point.coordinates[0], response.Placemark[0].Point.coordinates[1], 13, response.Placemark[0].address);
            isgeocodingfrom = false;
        }        
    }
    
}



function toDisambig(response) {
    add2 = false;
    if (response.Placemark == undefined || response.Placemark == null) {
        alert("Could not locate To addres");
        return;
    }    
    if (response.Placemark.length > 1) {
       
        document.getElementById("toerr").innerHTML = "<strong>Did you mean:</strong><br /><table width=\"100%\">";
        for (var i = 0; i < response.Placemark.length; ++i) {
            var href = "javascript:setTo('" + response.Placemark[i].address + "', true)";
            var href1 = "javascript:showAddress(" + response.Placemark[i].Point.coordinates[0] + "," + response.Placemark[i].Point.coordinates[1] + ",13,'" + response.Placemark[i].address + "')";
            document.getElementById("toerr").innerHTML += "<tr><td><a href=\"" + href + "\">" + response.Placemark[i].address + "</a></td><td align=\"right\" valign=\"middle\">" +
      "<a href=\"" + href1 + "\"><img border=\"0\" src=\"/images/map.png\" alt=\"Show this address on the map.\" /></a></td></tr><tr><td colspan=\"2\"><hr /></td></tr>";
        }
        document.getElementById("toerr").innerHTML += "</table>";
      //  document.getElementById("toinput").style.display = "none";
        document.getElementById("toerr").style.display = "block";
    } else if (response.Placemark.length < 1) {
        alert("Could not locate To addres");
    } else {
        settoaddress(response.Placemark[0].address);
        add2 = true;
        if (isgeocodingto) {
            showAddress(response.Placemark[0].Point.coordinates[0], response.Placemark[0].Point.coordinates[1], 13, response.Placemark[0].address);
            isgeocodingto = false;
        }        
    }
}





function parSeValues()
{ 
   if(lat11==0 || long11 ==0)
   {
     return false;
   }

   document.getElementById("ctl00_ContentPlaceHolder1_hdLat").value=lat11;
   document.getElementById("ctl00_ContentPlaceHolder1_hdLong").value=long11;
   document.getElementById("ctl00_ContentPlaceHolder1_hdCountry").value=GeoCodeCountry;
   document.getElementById("ctl00_ContentPlaceHolder1_hdStreet").value=GeoCodeStreet;
   document.getElementById("ctl00_ContentPlaceHolder1_hdState").value=GeoCodeState;
   document.getElementById("ctl00_ContentPlaceHolder1_hdCity").value=GeoCodeCity;
   document.getElementById("ctl00_ContentPlaceHolder1_hdPostalCode").value=GeoCodeZip;
   __doPostBack(bid,'');
 }
 