﻿Type.registerNamespace('Vehix.Presentation.ResearchUsedVehicleValues.Selector')
Vehix.Presentation.ResearchUsedVehicleValues.Selector = function(id, validationGroup, category, yearElementID, makeElementID, modelElementID, trimElementID, zipElementID, mileageElementID, navigateElementID, href, scopeKey) {
    Vehix.Presentation.ResearchUsedVehicleValues.Selector.initializeBase(this);
    var element;

    this.set_id(id);
    this.set_enableChangedEvent(false);
    this._validationGroup = validationGroup;
    this._category = category;

    this._yearElementID = yearElementID;
    if (element = $get(this._yearElementID)) {
        element.onchange = new Function('$find(\'' + id + '\').changed(\'year\',this.options[this.selectedIndex].value)');
    }

    this._makeElementID = makeElementID;
    if (element = $get(this._makeElementID)) {
        element.onchange = new Function('$find(\'' + id + '\').changed(\'make\',this.options[this.selectedIndex].value)');
    }

    this._modelElementID = modelElementID;
    if (element = $get(this._modelElementID)) {
        element.onchange = new Function('$find(\'' + id + '\').changed(\'model\',this.options[this.selectedIndex].value)');
    }

    this._trimElementID = trimElementID;
    if (element = $get(this._trimElementID)) {
        element.onchange = new Function('$find(\'' + id + '\').changed(\'trim\',this.options[this.selectedIndex].value)');
    }

    this._zipElementID = zipElementID;
    this._mileageElementID = mileageElementID;

    this._navigateElementID = navigateElementID;
    if (element = $get(this._navigateElementID)) {
        element.href = 'javascript:$find(\'' + id + '\').navigate()';
    }

    this._href = href;
    if (Sys.Application.findComponent(id) == null) {
        Sys.Application.addComponent(this);
        Sys.Application.add_load(new Function('if(!Sys.WebForms.PageRequestManager.getInstance().get_isInAsyncPostBack()){$find(\'' + id + '\').restore()}'));
    }

    this._scopeKey = scopeKey;

    this.set_enableChangedEvent(true);
}
Vehix.Presentation.ResearchUsedVehicleValues.Selector.prototype =
{
    get_category: function() {
        return this._category;
    },
    get_year: function() {
        return Vehix.Presentation.tryGetSelectedValue($get(this._yearElementID));
    },
    set_year: function(value) {
        var element = $get(this._yearElementID);
        if (element) {
            Vehix.Presentation.trySetSelectedValue(element, value);
            Sys.UI.Behavior.getBehaviorByName(element, 'CascadingDropDownBehavior').set_SelectedValue(value);
        }
    },
    get_makeID: function() {
        return Vehix.Presentation.tryGetSelectedValue($get(this._makeElementID));
    },
    set_makeID: function(value) {
        var element = $get(this._makeElementID);
        if (element) {
            Vehix.Presentation.trySetSelectedValue(element, value);
            Sys.UI.Behavior.getBehaviorByName(element, 'CascadingDropDownBehavior').set_SelectedValue(value);
        }
    },
    get_make: function() {
        return Vehix.Presentation.tryGetSelectedText($get(this._makeElementID));
    },
    get_modelID: function() {
        return Vehix.Presentation.tryGetSelectedValue($get(this._modelElementID));
    },
    set_modelID: function(value) {
        var element = $get(this._modelElementID);
        if (element) {
            Vehix.Presentation.trySetSelectedValue(element, value);
            Sys.UI.Behavior.getBehaviorByName(element, 'CascadingDropDownBehavior').set_SelectedValue(value);
        }
    },
    get_model: function() {
        return Vehix.Presentation.tryGetSelectedText($get(this._modelElementID));
    },
    get_trimID: function() {
        return Vehix.Presentation.tryGetSelectedValue($get(this._trimElementID));
    },
    set_trimID: function(value) {
        var element = $get(this._trimElementID);
        if (element) {
            Vehix.Presentation.trySetSelectedValue(element, value);
            Sys.UI.Behavior.getBehaviorByName(element, 'CascadingDropDownBehavior').set_SelectedValue(value);
        }
    },
    get_trim: function() {
        return Vehix.Presentation.tryGetSelectedText($get(this._trimElementID));
    },
    get_zip: function() {
        var result = null;
        var element = $get(this._zipElementID);
        if (element) {
            result = element.value;
        }
        return result;
    },
    set_zip: function(value) {
        var element = $get(this._zipElementID);
        if (element) {
            element.value = value;
        }
    },
    get_mileage: function() {
        var result = null;
        var element = $get(this._mileageElementID);
        if (element) {
            result = element.value;
        }
        return result;
    },
    set_mileage: function(value) {
        var element = $get(this._mileageElementID);
        if (element) {
            element.value = value;
        }
    },
    get_href: function() {
        return this._href;
    },
    set_href: function(value) {
        this._href = value;
    },
    restore: function(state) {
        if (!state) state = Vehix.Presentation.getCookie('Used Vehicle Values Selector State');
        if (state) {
            try {
                this.set_enableChangedEvent(false)
                state = eval('(' + state + ')')
                if (state.scopeKey == this._scopeKey) {
                    this.set_year(state.year);
                    this.set_makeID(state.make);
                    this.set_modelID(state.model);
                    this.set_trimID(state.trim);
                    this.set_zip(state.zip);
                    this.set_mileage(state.mileage);
                }
            }
            catch (e) {

            }
            finally {
                this.set_enableChangedEvent(true)
            }
        }
    },
    persist: function() {
        Vehix.Presentation.setCookie('Used Vehicle Values Selector State', { scopeKey: this._scopeKey, year: this.get_year(), make: this.get_makeID(), model: this.get_modelID(), trim: this.get_trimID(), zip: this.get_zip(), mileage: this.get_mileage() })
    },
    navigate: function() 
    {
        //REMOVE ALL UVV
        if (Page_ClientValidate(this._validationGroup) == true) 
        {
            this.persist();
            var href = this.get_href();
            var queryString = new Vehix.Presentation.QueryString(href);
            if (this.get_year()) 
            {
                queryString.set_item('year', this.get_year());
            }
            if (this.get_makeID()) 
            {
                queryString.set_item('makeID', this.get_makeID());
            }
            if (this.get_make()) 
            {
                queryString.set_item('uvv_make', this.get_make());
            }
            if (this.get_modelID()) 
            {
                queryString.set_item('modelID', this.get_modelID());
            }
            if (this.get_model()) {
                queryString.set_item('model', this.get_model());
            }
            if (this.get_trimID()) 
            {
                queryString.set_item('trim', this.get_trimID());
            }
            if (this.get_trim()) 
            {
                queryString.set_item('uvv_trim', this.get_trim());
            }
            if (this.get_zip()) 
            {
                queryString.set_item('zip', this.get_zip());
            }
            if (this.get_mileage()) 
            {
                queryString.set_item('uvv_mileage', this.get_mileage());
            }
            document.location.href = queryString.toString(Vehix.Presentation.QueryString.stripQuery(this.get_href()));
        }
    }
}
Vehix.Presentation.ResearchUsedVehicleValues.Selector.registerClass('Vehix.Presentation.ResearchUsedVehicleValues.Selector', Vehix.Presentation.Selector);
