/*var CityAutocompleter = {
    instance : null,

    onCountryChange : function (context, e) {
        var cityLabel = false;
        var country_id = $('country-id').value || 0;
        var isUSA = (country_id === context.country_id_usa);
        if(cityLabel = $('city-label')) {
            cityLabel.normalize();
        }
        if(isUSA) {
            cityLabel.firstChild.nodeValue = 'Zip code:';
            if(CityAutocompleter.instance instanceof Ajax.Autocompleter) {
                CityAutocompleter.instance.getUpdatedChoices = function(){};
            }
            CityAutocompleter.instance = null;
        } else {
            cityLabel.firstChild.nodeValue = 'City:';
            if(CityAutocompleter.instance instanceof Ajax.Autocompleter) {
                CityAutocompleter.instance.url = context.autocomplete_url + '/' + country_id;
            } else {
                CityAutocompleter.instance = new Ajax.Autocompleter(
                    context.city_field_id,
                    context.city_field_id + '_autoComplete',
                    context.autocomplete_url + '/' + country_id,
                    {minChars: 4}
                );
            }
        }
    }
};*/

var CityAutocompleter = {
    instance : null,

    onCountryChange : function (context, e) {
        var cityLabel = false;
        var country_id = $('country-id').value || 0;
        var isUSA = (country_id === context.country_id_usa);
        if(cityLabel = $('city-label')) {
            cityLabel.normalize();
        }
        if(isUSA) {
            cityLabel.firstChild.nodeValue = 'Zipcode:';
            if(CityAutocompleter.instance instanceof Ajax.Autocompleter) {
                CityAutocompleter.instance.url = context.autocomplete_usa;
            } else {
                CityAutocompleter.instance = new Ajax.Autocompleter(
                    context.city_field_id,
                    context.city_field_id + '_autoComplete',
                    context.autocomplete_usa,
                    {minChars: 4}
                );
            }
        } else {
            cityLabel.firstChild.nodeValue = 'City:';
            if(CityAutocompleter.instance instanceof Ajax.Autocompleter) {
                CityAutocompleter.instance.url = context.autocomplete_url + '/' + country_id;
            } else {
                CityAutocompleter.instance = new Ajax.Autocompleter(
                    context.city_field_id,
                    context.city_field_id + '_autoComplete',
                    context.autocomplete_url + '/' + country_id,
                    {minChars: 4}
                );
            }
        }
    }
};

