﻿var geo;
var reasons;
var locHasChanged;

$(document).ready(function() {

    geo = new google.maps.Geocoder();

    if ($("#" + hfLatitude).val() == '')
        $('#' + ddlDistanceLimit).attr("disabled", true);


    $('#' + tbLocation).blur(function() {
        if (this.value == '') {
            this.value = ResourceManager.GetString("member_SearchEnterLocation");
            $('#' + ddlDistanceLimit).attr("disabled", true);
            $("#" + hfLatitude).val('');
            $("#" + hfLongitude).val('');
        }
    });

    $('#' + tbLocation).focus(function() {
    if (this.value == ResourceManager.GetString("member_SearchEnterLocation"))
            this.value = '';

        $('#' + ddlDistanceLimit).attr("disabled", false);
    });

    if (($("#" + tbLocation).val() == '') || ($("#" + tbLocation).val() == ResourceManager.GetString("member_SearchEnterLocation"))) {
        locHasChanged = false;
    }
    else
        locHasChanged = true;

    $('#' + tbLocation).change(function() {
        locHasChanged = true;
    });

});




function UpdateLatLongFromAddress() {

    if (!locHasChanged ||  ($("#" + tbLocation).val() == '') || ($("#" + tbLocation).val() == ResourceManager.GetString("member_SearchEnterLocation"))) {
        __doPostBack('ctl00$STC$btnSearch', '');
        return false;
    }        

    var addr = $("#" + tbLocation).val() + "," + $("#" + hfMemberLoc).val();
    locHasChanged = false;

    geo.geocode({ address: addr }, function(result, status) {

        if (status == google.maps.GeocoderStatus.OK && result[0]) {
            var lat = result[0].geometry.location.lat();
            var lng = result[0].geometry.location.lng();
             $("#" + hfLatitude).val(lat);
                $("#" + hfLongitude).val(lng);                
                
                __doPostBack('ctl00$STC$btnSearch', '');

                return false;            
        }
        else {
            
            //alert('Could not find "' + addr + '" ' + reason);            
            return false;
        }
    });



    return false;
    
}
