    

  //Google Geocode begin
var geocoder = new google.maps.Geocoder();
var geocoderTimer;
var geocodeTimeoutTimer;
var oldLocationString;
var locationLastQueryValid = false;
geocoder.firstItem = {};

function trim(stringToTrim) {
        return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function startTimeoutTimer(formId) {
    if (geocodeTimeoutTimer) {
        clearTimeout(geocodeTimeoutTimer);
    }
    try {
        dojo.removeClass(dojo.byId('submitting'), "noshow");
    }
    catch(err) {}
    geocodeTimeoutTimer = setTimeout(dojo.hitch(this, function() {
         try {
             if (!locationLastQueryValid) {
                document.getElementById(formId+':locationString').value = "San Francisco, CA";
                dojo.byId(formId+':locationLatitude').value = 37.7749295;
                dojo.byId(formId+':locationLongitude').value = -122.4194155;
             } else {
                 document.getElementById(formId+':locationString').value = oldLocationString;
             }
             dojo.removeClass(formId+':continueButton','noshow');
             dojo.addClass(dojo.byId('submitting'), "noshow");
             document.getElementById(formId+':locationString').blur();
         }
         catch(err) {}
    }),5000);
}
function roundNumber(num, dec) {
    var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
    return result;
}
function geocode(formId) {
    if (geocoderTimer) {
        clearTimeout(geocoderTimer);
    }
    dojo.byId(formId+':locationStatus').innerHTML = "Looking up your location...";
     startTimeoutTimer(formId);
    /*
     if (geocodeTimeoutTimer) {
        clearTimeout(geocodeTimeoutTimer);
     }
     geocodeTimeoutTimer = setTimeout(dojo.hitch(this, function() {
         try {
             document.getElementById(formId+':locationString').value = "San Francisco, CA";
             dojo.removeClass(formId+':continueButton','noshow');
             dojo.addClass(dojo.byId('submitting'), "noshow");
         }
         catch(err) {}
    }),5000);*/
    geocoderTimer = setTimeout(dojo.hitch(this, function() {
          var query = document.getElementById(formId+':locationString').value;
          query = trim(query); // trim space if browser supports
          
          if(query != oldLocationString && query.length > 0) { // no useless request
                //dojo.byId(formId+':locationStatus').innerHTML = "";
                console.debug("geocode begin");
                
                oldLocationString = query;
                console.debug("geocoder.resultAddress = '"+geocoder.resultAddress+"'");
                clearTimeout(geocoder.waitingDelay);
                geocoder.waitingDelay = setTimeout(function(){
                geocoder.geocode({address: query + " USA"}, function() {

                    try {dojo.addClass(dojo.byId('submitting'), "noshow");}
                    catch(err) {}
                    if (geocodeTimeoutTimer) {
                        clearTimeout(geocodeTimeoutTimer);
                    }

                    var response = arguments[0];
                    var status = arguments[1];
                    var cannotFindStr = "Cannot find your location. Will use San Francisco, California";
                    if (status == google.maps.GeocoderStatus.OK && response[0]) {
                        console.debug("geocode returned at least 1 response");
                        console.log(response[0]);
                        locationLastQueryValid = true;
                        try {
                            if (pageTracker) {
                                //demand tracking. quarter mile proximity. exact location is not tracked for privacy.
                                pageTracker._trackEvent('Preferred Location', 'Prefererred Location Set', roundNumber(response[0].geometry.location.lat(),2) + ", " + roundNumber(response[0].geometry.location.lng(),2));
                            }
                            console.log("latitude: " + response[0].geometry.location.lat());
                            dojo.byId(formId+':locationLatitude').value = response[0].geometry.location.lat();
                            dojo.byId(formId+':locationLongitude').value = response[0].geometry.location.lng();
                            console.log("longitude: " + response[0].geometry.location.lng());
                            dojo.byId(formId+':locationStatus').innerHTML = "Found: "+response[0].formatted_address;
                        } catch(err) {
                            console.error("index.js-geocode(): "+err.message);
                            dojo.byId(formId+':locationStatus').innerHTML = cannotFindStr;
                            dojo.byId(formId+':locationLatitude').value = 37.7749295;
                            dojo.byId(formId+':locationLongitude').value = -122.4194155;
                        }
                    } else if(status == google.maps.GeocoderStatus.ZERO_RESULTS) {
                        //no results
                        console.debug("geocode returned zero results");
                        dojo.byId(formId+':locationStatus').innerHTML = cannotFindStr;
                        dojo.byId(formId+':locationLatitude').value = 37.7749295;
                        dojo.byId(formId+':locationLongitude').value = -122.4194155;

                    }
                    dojo.removeClass(formId+':continueButton','noshow');
                });
                }, 300);
                console.debug("geocode end");
          }

    }), 2000);

}
//Google Geocode end
