﻿var geocoder, location1, location2, gDir;

function initialize() {
    geocoder = new GClientGeocoder();
    gDir = new GDirections();
    GEvent.addListener(gDir, "load", function() {
        // document.getElementById('sub_button').enabled = false;
        var drivingDistanceMiles = gDir.getDistance().meters / 1609.344;
        var drivingDistanceKilometers = gDir.getDistance().meters / 1000;
        document.forms[0].miles.value = drivingDistanceMiles;
        document.forms[0].kilometers.value = drivingDistanceKilometers;
        document.forms[0].time_seconds.value = gDir.getDuration().seconds;
        document.forms[0].time_text.value = gDir.getDuration().html;
		document.forms[0].submit();
        // document.getElementById('sub_button').enabled = true;
    });
}

function showLocation1() {

/*
    var city_state = document.forms[0].FromZip.value.split(",");
    var city_name = city_state[0].replace(" ", "");
    var state_name = city_state[1].replace(" ", "");
  */
    
    geocoder.getLocations(document.forms[0].FromZip.value, function(response) {
        if (!response || response.Status.code != 200) {
            alert("Sorry, we were unable to geocode the first address");
        }
        else {
            if (typeof response.Placemark[0].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.PostalCode == "object" &&
                typeof response.Placemark[0].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.PostalCode.PostalCodeNumber == "string")
                thezip = response.Placemark[0].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.PostalCode.PostalCodeNumber;
            else
                thezip = null;

            location1 = {
                lat: response.Placemark[0].Point.coordinates[1],
                lon: response.Placemark[0].Point.coordinates[0],
                state: response.Placemark[0].AddressDetails.Country.AdministrativeArea.AdministrativeAreaName,
                city: response.Placemark[0].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName,
                zip: thezip,
                address: response.Placemark[0].address
            };

            document.forms[0].lat1.value = location1.lat;
            document.forms[0].long1.value = location1.lon;
            document.forms[0].city1.value = location1.city;
            document.forms[0].zip1.value = location1.zip;
            document.forms[0].state1.value = location1.state;
        }
    });
	showLocation2();
}
            
function showLocation2() {
    geocoder.getLocations(document.forms[0].ToZip.value, function(response) {
        if (!response || response.Status.code != 200) {
            alert("Sorry, we were unable to geocode the second address");
        }
        else {
            if (typeof response.Placemark[0].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.PostalCode == "object" &&
                typeof response.Placemark[0].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.PostalCode.PostalCodeNumber == "string")
                thezip = response.Placemark[0].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.PostalCode.PostalCodeNumber;
            else
                thezip = null;

            location2 = {
                lat: response.Placemark[0].Point.coordinates[1],
                lon: response.Placemark[0].Point.coordinates[0],
                state: response.Placemark[0].AddressDetails.Country.AdministrativeArea.AdministrativeAreaName,
                city: response.Placemark[0].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName,
                zip: thezip,
                address: response.Placemark[0].address
            };
            document.forms[0].lat2.value = location2.lat;
            document.forms[0].long2.value = location2.lon;
            document.forms[0].city2.value = location2.city;
            document.forms[0].zip2.value = location2.zip;
            document.forms[0].state2.value = location2.state;
            gDir.load('from: ' + location1.address + ' to: ' + location2.address);

        }
    });
}
    

