CityUpdater = Class.create();
CityUpdater.prototype = {
    initialize: function (countryEl, cityTextEl, citySelectEl, cities, disableAction)
    {
        this.countryEl = $(countryEl);
        this.cityTextEl = $(cityTextEl);
        this.citySelectEl = $(citySelectEl);
        this.cities = cities;

        this.disableAction = (typeof disableAction=='undefined') ? 'hide' : disableAction;

        if (this.citySelectEl.options.length<=1) {
            this.update();
        }

        Event.observe(this.countryEl, 'change', this.update.bind(this));
    },

    update: function()
    {
        if (this.cities[this.countryEl.value]) {
            var i, option, city, def;

            if (this.cityTextEl) {
                def = this.cityTextEl.value.toLowerCase();
                this.cityTextEl.value = '';
            }
            if (!def) {
                def = this.citySelectEl.getAttribute('defaultValue');
            }

            this.citySelectEl.options.length = 1;
            for (cityId in this.cities[this.countryEl.value]) {
                city = this.cities[this.countryEl.value][cityId];

                option = document.createElement('OPTION');
                option.value = cityId;
                option.text = city.name;

                if (this.citySelectEl.options.add) {
                    this.citySelectEl.options.add(option);
                } else {
                    this.citySelectEl.appendChild(option);
                }

                if (cityId==def || city.name.toLowerCase()==def || city.code.toLowerCase()==def) {
                    this.citySelectEl.value = cityId;
                }
            }

            if (this.disableAction=='hide') {
                if (this.cityTextEl) {
                    this.cityTextEl.style.display = 'none';
                }

                this.citySelectEl.style.display = '';
            } else if (this.disableAction=='disable') {
                if (this.cityTextEl) {
                    this.cityTextEl.disabled = true;
                }
                this.citySelectEl.disabled = false;
            }
            this.setMarkDisplay(this.citySelectEl, true);
        } else {
            if (this.disableAction=='hide') {
                if (this.cityTextEl) {
                    this.cityTextEl.style.display = '';
                }
                this.citySelectEl.style.display = 'none';
                Validation.reset(this.citySelectEl);
            } else if (this.disableAction=='disable') {
                if (this.cityTextEl) {
                    this.cityTextEl.disabled = false;
                }
                this.citySelectEl.disabled = true;
            } else if (this.disableAction=='nullify') {
                this.citySelectEl.options.length = 1;
                this.citySelectEl.value = '';
                this.citySelectEl.selectedIndex = 0;
                this.lastCountryId = '';
            }
            this.setMarkDisplay(this.citySelectEl, false);
        }
    },

    setMarkDisplay: function(elem, display){
        elem = $(elem);
        var labelElement = elem.up(1).down('label > span.required') || 
                           elem.up(2).down('label > span.required') ||
                           elem.up(1).down('label.required > em') ||
                           elem.up(2).down('label.required > em');
        if(labelElement) {
            display ? labelElement.show() : labelElement.hide();
        }
    }
}
