var ConfigSelectionForm = Class.create({
    /**
     * initialize()
     */
    initialize: function(options) {
        this.options = {
            address: 'address-cct',
            form: 'selectModelForm',
            makeSelect: 'makeSelect',
            modelSelect: 'modelSelect',
            yearSelect: 'yearSelect',
            zipLabel: 'zip-label-cct'
        }
        Object.extend(this.options, options || {});
        this.address = this.options.address;
        this.form = this.options.form;
        this.makeSelect = this.options.makeSelect;
        this.modelSelect = this.options.modelSelect;
        this.yearSelect = this.options.yearSelect;
        this.ymmError = 'Please choose a Year, Make, and Model.';
        this.zipError = 'Please enter a valid 5 digit ZIP Code.';
        this.zipLabel = this.options.zipLabel;

        $(this.form).reset();
        $(this.makeSelect).observe('change', this.changeHandler.bindAsEventListener(this));
        $(this.modelSelect).observe('change', this.changeHandler.bindAsEventListener(this));
        $(this.form).observe('submit', this.submitHandler.bindAsEventListener(this));
    },

    changeHandler: function(e) {
        var el = e.element();
        if (el.id == this.makeSelect) this.changeMake();
        if (el.id == this.modelSelect) this.updateYears();
    },

    changeMake: function() {
        var make = $F(this.makeSelect);
        if (!make) return;
        this.updateModels();
        this.updateYears();
    },

    updateMakes: function() {
        var makeOptions = $(this.makeSelect).select('option');
        var optionLength = makeOptions.length;
        $(this.makeSelect).update('');
        for (var i = 0; i < optionLength; i++) {
            $(this.makeSelect).insert(makeOptions[i]);
        }
    },

    updateModels: function() {
        this.updateAdsByType('make');
        var makeValue = $F(this.makeSelect);
        var model = $(this.modelSelect);
        model.disable();
        var selectOne = new Element('option').update('Select One');
        model.update(selectOne);
        if (!makeValue || (makeValue == 'Select One')) return;
        var callback = function(models) {
            if (!models) return;
            var modelLength = models.length;
            for (var i = 0; i < modelLength; i++) {
                var thisModel = models[i];
                var thisOption = new Element('option', {value: thisModel}).update(thisModel);
                model.insert(thisOption);
            }
            model.enable();
        }.bind(this);
        ModelSearchUtil.getAllNewModels(makeValue, callback);
    },

    updateYears: function() {
        this.updateAdsByType('model');
        var makeValue = $F(this.makeSelect);
        var modelValue = $F(this.modelSelect);
        var year = $(this.yearSelect);
        year.disable();
        var selectOne = new Element('option').update('Select One');
        year.update(selectOne);
        if (!modelValue || (modelValue == 'Select One')) return;
        var callback = function(years) {
            if (!years) return;
            var yearLength = years.length;
            for (var i = 0; i < yearLength; ++i) {
                var thisYear = years[i];
                var thisOption = new Element('option', {value: thisYear}).update(thisYear);
                year.insert(thisOption);
            }
            year.enable();
        }.bind(this);
        ModelSearchUtil.getNewYearRangesForMakeModelDescending(makeValue, modelValue, callback);
    },

    updateAdsByType: function(value) {
        //def 23051 - when cobranded, we don't dynamically update the ads, and adManager is false, so this will break....
        if (!adManager) {return;}
        var makeValue = $F(this.makeSelect);
        var modelValue = $F(this.modelSelect);
        switch (value) {
            case 'make':
                adManager.updateAdsByMake(makeValue);
                break;
            case 'model':
                adManager.updateAdsByModel(modelValue);
                break;
        }
    },

    submitHandler: function(e) {
        e.stop();
        var makeModel = this.makeModelProcess();
        if (makeModel) {
            zipValidation.zipProcess();
        }
    },

    makeModelProcess: function() {
        var modelSelectValue = $F(this.modelSelect);
        var yearSelectValue = $F(this.yearSelect);
        if (modelSelectValue == null || modelSelectValue == '' || modelSelectValue == 'Select One') {
            alert(this.ymmError);
            return false;
        }
        if (yearSelectValue == '' || yearSelectValue == 'Select One') {
            alert(this.ymmError);
            return false;
        }
        return true;
    },

    zipShowError: function() {
        $(this.zipLabel).setStyle({color: '#f00'});
        alert(this.zipError);
    },

    zipShowSuccess: function() {
        $(this.form).submit();
    }

})