window.addEvent('domready', function () {
  var _cache = {},
      _city = $('city'),
      _first = new Element('div');

  if (null != _city) {
    _city.getChildren()[0].clone().inject(_first);
    _first = _first.innerHTML;
  }

  function setOptions(options)
  {
    options = options || '';
    _city.setHTML(_first + options).setProperty('disabled', '');
  }

  window.getCities = function (select) {
    var province_id = select.value;

    if (province_id == 0) {
      setOptions();
      _city.setProperty('disabled', 'disabled');
      return;
    }

    if (typeof _cache[province_id] == "undefined") {
      _city.setProperty('disabled', 'disabled');
      select.setProperty('disabled', 'disabled');
      var xhr = new XHR({
        method: 'get',
        onSuccess: function (rs) {
          var pairs = rs.split('\\s'),
              pairsNr = pairs.length,
              options = '';

          for (var i = 0; i < pairsNr; i++) {
            var city = pairs[i].split('\\c');
            options += '<option value="' + city[0] + '">' + city[1] + '</option>';
          };
          _cache[province_id] = options;
          setOptions(_cache[province_id]);
          select.setProperty('disabled', '');
        }
      }).send('index.php', 'option=com_clubs&task=getCities&id=' + province_id);
    } else {
      setOptions(_cache[province_id]);
    }
  };

  _icons = {
    1: $$('.type1'),
    2: $$('.type2')
  };
  window.getIcons = function (select) {
    var index = select.selectedIndex + 1;
    _icons[index].removeClass('hidden');
    _icons[index%2+1].addClass('hidden');
  };
});


