﻿function getLatitudeLongitude(txtCityID,txtStateID,txtContryID,hdfLongID,hdfLatID,hdfErrorID) 
{
//    alert("in getLatitudeLongitude");

    if (GBrowserIsCompatible()) 
    {

        // ====== Create a Client Geocoder ======
        var geo = new GClientGeocoder();

        // ====== Array for decoding the failure codes ======
        var reasons = [];
        reasons[G_GEO_SUCCESS] = "Success";
        reasons[G_GEO_MISSING_ADDRESS] = "Missing Address: The address was either missing or had no value.";
        reasons[G_GEO_UNKNOWN_ADDRESS] = "Unknown Address:  No corresponding geographic location could be found for the specified address.";
        reasons[G_GEO_UNAVAILABLE_ADDRESS] = "Unavailable Address:  The geocode for the given address cannot be returned due to legal or contractual reasons.";
        reasons[G_GEO_BAD_KEY] = "Bad Key: The API key is either invalid or does not match the domain for which it was given";
        reasons[G_GEO_TOO_MANY_QUERIES] = "Too Many Queries: The daily geocoding quota for this site has been exceeded.";
        reasons[G_GEO_SERVER_ERROR] = "Server error: The geocoding request could not be successfully processed.";

        // ====== Geocoding ======
        var txtCity = document.getElementById(txtCityID).value;
        var txtState = document.getElementById(txtStateID).value;
        var txtCountry = document.getElementById(txtContryID).value;
        var hfdLong = document.getElementById(hdfLongID);
        var hfdLat = document.getElementById(hdfLatID);
        var hfdError = document.getElementById(hdfErrorID);
        var txtLocation;
        var bolReturn = false;
        
        if (txtCity.length <= 0)
        {
            alert("Please enter the country and nearest city.");
            return false;
        }
        if (txtCountry.length <= 0)
        {
            alert("Please enter the country and nearest city.");
            return false;
        }

        if ((txtState.length <= 0) || (txtState == "0"))
        {
            //concatenate the city, country
            txtLocation = txtCity + ", " + txtCountry
        }
        else
        {
            //concatenate the city, state, country
            txtLocation = txtCity + ", " + txtState + ", " + txtCountry
        }

//        alert("txtLocation = " + txtLocation);
//        var search = document.getElementById("search").value;

        // ====== Perform the Geocoding ======
        geo.getLocations(txtLocation, function(result) {
            // If that was successful
            if (result.Status.code == G_GEO_SUCCESS) {
                // How many resuts were found
                //                document.getElementById("message").innerHTML = "Found " + result.Placemark.length + " results";
                // Loop through the results, placing markers
                for (var i = 0; i < result.Placemark.length; i++) {
                    var p = result.Placemark[i].Point.coordinates;
                    hfdLong.value = p[0];
                    hfdLat.value = p[1];
//                    alert("hfdLong = " + hfdLong.value);
//                    alert("hfdLat = " + hfdLat.value);
                }
                __doPostBack('btnSave','Args')
//                bolReturn = false;
//                document.forms(0).submit();
            }
            // ====== Decode the error status ======
            else {
                var reason = "Code " + result.Status.code;
                if (reasons[result.Status.code]) {
                    reason = reasons[result.Status.code]
                }
                alert('Could not find "' + txtLocation + '" ' + reason);
                hfdError.value = reason;
                return false;
            }
        }
        );        //close of function call

        return false;

    }

    // display a warning if the browser was not compatible
    else 
    {
        alert("Sorry, the Google Maps API is not compatible with this browser");
        return false;
    }

}

