Autocompleter.Ezkode = Class.create(Autocompleter.Local, {
  getUpdatedChoices: function() {
    var address = document.getElementById(location_input).value;
  
    geocoder.geocode( { address: address},
                      function(results, status) {

      location_autocomplete.options.array.clear();
      location_json.clear();

      if (status == google.maps.GeocoderStatus.OK && results.length) {
      
        if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {

          for (var i = 0; i < results.length; i++) {
            location_autocomplete.options.array.push(results[i].formatted_address);
            temp = results[i];
            temp.latitude = temp.geometry.location.lat();
            temp.longitude = temp.geometry.location.lng();
            location_json.push(temp);
         }
        
          location_autocomplete.updateChoices("<ul><li>" + location_autocomplete.options.array.join('</li><li>') + "</li></ul>");
          location_autocomplete.show();
        } else {
          location_autocomplete.hide();
        }
      
      } else {
        location_autocomplete.options.array.push("Google geocoder was unsuccessful due to: " + status);
        location_autocomplete.updateChoices("<ul><li>" + location_autocomplete.options.array.join('</li><li>') + "</li></ul>");
        location_json.push(status);
        location_autocomplete.show();
      }
    });
    
  },
getCurrentIndex: function()
	{
//		alert("Returning Current Index: " + this.index);
		return this.index;
	}
});


var geocoder;
var location_json;
var selected_location_json;
var location_autocomplete;
var location_input; //initialized in sfWidgetFormInputGeoComplete


function initialize() {
  geocoder = new google.maps.Geocoder();
  location_autocomplete = new Autocompleter.Ezkode(location_input, 'geo_complete_suggestions', [], {});
  location_json = new Array();
  var hiddenElement = $(selected_location_json);
  //alert(hiddenElement.value);
  var element = $(location_input);
  //alert(element.value);
}

function validate() {
	var doSubmit = true;
	//alert(selected_location_json);
	//alert(location_input);
	var hiddenElement = $(selected_location_json);
    var element = $(location_input);
	var geocode = null;
	var geocode_JSON = null;

	geocode = location_json[location_autocomplete.getCurrentIndex()];
	geocode_JSON = Object.toJSON(geocode);


//	alert("geocode: " + geocode_JSON);
	if(geocode != 'undefined')
	{
		address_components = geocode.address_components;
		address_components.each(function(component) {
			  types = component.types;
			  types.each(function(type) {
				  if(type == "country")
				  {
					  if(component.short_name != "US")
					  {
						  alert("The requested address is not in the United States. Please enter an address in the United States or select one from the list of choices that is in the United States.");
						  doSubmit = false;
					  }
				  }
			});
		});
		hiddenElement.value = geocode_JSON;
	}
	else
	{
		alert("There is no address entered. Please enter an address in the United States or select one from the list of choices that is in the United States.");
		doSubmit = false;
	}
	if(doSubmit)
	{
		element.form.submit();
	}
}

window.onload = initialize;

