var CityLoader = Class.create({
	path: null,
	countryId: null,
	cityId: null,

	initialize: function(path, countryId, cityId) {
		this.path = path;
		this.countryId = countryId;
		this.cityId = cityId;
	},

	_markWork: function(state)
	{
		$(this.countryId).disabled = state;
		$(this.cityId).disabled = state;
	},

	_insertOption: function(selectId, value, text, current)
	{
		var option = new Element('option', { 'value': value}).update(text);
		if (option.value == current)
			option.selected = true;

		$(selectId).appendChild(option);
	},

	getCityList: function(event)
	{
		var _this = this;
		this._markWork(true);

		new Ajax.Request(this.path + '&country=' + Event.element(event).value,
		{
		  method: 'get',
		  onSuccess: function(out) {
		  	for (var i = $(_this.cityId).length; i>0; i--)
				$(_this.cityId).remove(i);

	  		cityList = eval(out.responseText);
	  		for (var i = 0; i < cityList.length; i++)
	  			_this._insertOption(_this.cityId, cityList[i].id, cityList[i].name);

	  		$(_this.cityId).selectedIndex = 0;

		  	_this._markWork(false);
		  }
		});
	}
});
