var GeoUtils = Class.create({
  
  countryId: '',
  regionId: '',
  cityId: '',
  defaultCountry: '',
  defaultRegion: '',
  defaultCity: '',
  urls: {region: "", city: ""},
  
  initialize: function(options) {
    Object.extend(this, options);
    var obj = this;

    if(this.defaultCountry)
    {
      $(this.regionId).enable();
    }

    if(this.defaultRegion)
    {
      $(this.cityId).enable();
    }
    
    $(this.countryId).observe('change', function(evt) {
      obj.display(obj.urls.region, obj.countryId, obj.regionId);
    });

    $(this.regionId).observe('change', function(evt) {
      obj.display(obj.urls.city, obj.regionId, obj.cityId);
    });
  },
  
  reset: function(exceptElement) {
    if(this.regionId != exceptElement)
    {
      $(this.regionId).disable();
    }
    if(this.cityId != exceptElement)
    {
      $(this.cityId).disable();
    }
  },

  fill: function(elementId, data) {
    $(elementId).options.length = 0;
    
    $H(data).each(function(obj)
    {
      option = new Option(obj.value, obj.key);
      $(elementId).options.add(option);
    });
  },

  display: function(url, element, childElement) {
    var obj = this;
    this.reset(element);
    if($F(element).length == 0)
    {
      return;
    }
    new Ajax.Request(url, {
      parameters: {id : $F(element)},
      onSuccess: function(response) {
        $(childElement).enable();
        obj.fill(childElement, response.responseJSON);
      }
    });
  }
});