﻿function AccessoryOptions()
{
}

AccessoryOptions.prototype.languageIdHidden = null;
AccessoryOptions.prototype.dealerIdHidden = null;
AccessoryOptions.prototype.oemMarketIdHidden = null;
AccessoryOptions.prototype.groupModelSelect = null;
AccessoryOptions.prototype.groupVariantSelect = null;
AccessoryOptions.prototype.categorySelect = null;
AccessoryOptions.prototype.subCategorySelect = null;
AccessoryOptions.prototype.submitButton = null;
AccessoryOptions.prototype.optionsChangeEventSource = null;

AccessoryOptions.getClassInstance = function(controlId)
{
    var control = document.getElementById(controlId);
    
    if (control != null && control.accessoryOptionsClassInstance == null)
    {
        var inst = new AccessoryOptions();

        inst.languageIdHidden = document.getElementById(control.getAttribute("languageIdControlId"));
        inst.dealerIdHidden = document.getElementById(control.getAttribute("dealerIdControlId"));
        inst.oemMarketIdHidden = document.getElementById(control.getAttribute("oemMarketIdControlId"));        
        inst.groupModelSelect = document.getElementById(control.getAttribute("groupModelIdControlId"));
        inst.groupVariantSelect = document.getElementById(control.getAttribute("groupVariantIdControlId"));
        inst.categorySelect = document.getElementById(control.getAttribute("categoryIdControlId"));
        inst.subCategorySelect = document.getElementById(control.getAttribute("subCategoryIdControlId"));
        inst.submitButton = document.getElementById(control.getAttribute("submitButtonControlId"));
        
        control.accessoryOptionsClassInstance = inst;
    }
    
    return control == null ? null : control.accessoryOptionsClassInstance;
}

AccessoryOptions.optionsChange = function(controlId, eventSource)
{
    var inst = AccessoryOptions.getClassInstance(controlId);
    
    if (inst != null)
    {
        if (AccessoryOptionsControl != null &&
            inst.languageIdHidden != null &&
            inst.dealerIdHidden != null &&
            inst.oemMarketIdHidden != null &&            
            inst.categorySelect != null &&
            inst.groupModelSelect != null)
        {            
            inst.optionsChangeEventSource = eventSource;
            
            AccessoryOptionsControl.AccessoryOptionsChange(
                inst.languageIdHidden.value,
                inst.dealerIdHidden.value,
                inst.oemMarketIdHidden.value,
                inst.categorySelect.value,
                inst.groupModelSelect.value,
                Delegate.create(inst, inst.optionsChangeCallBack)
            );
        }
    }
}

AccessoryOptions.prototype.optionsChangeCallBack = function(response)
{
    if (response.error == null)
    {
        if (this.optionsChangeEventSource.id == this.groupModelSelect.id)
        {
            AccessoryOptions.updateSelect(
                this.groupVariantSelect,
                response.value.Tables[1],
                "GroupID",
                "Name"
            );
        }
        
        if (this.optionsChangeEventSource.id == this.categorySelect.id)
        {
            AccessoryOptions.updateSelect(
                this.subCategorySelect,
                response.value.Tables[0],
                "CategoryID",
                "Name"
            );
        }
    }
}

AccessoryOptions.updateSelect = function(select, dataTable, valueFieldName, textFieldName)
{
    if (select != null)
    {       
        select.options.length = 1;
                
        for (var i = 0; i < dataTable.Rows.length; i++)
        {
            var value = dataTable.Rows[i][valueFieldName];
            var text = dataTable.Rows[i][textFieldName];
            
            if (value != 0)
            {
                option = new Option( text, value );
                select.options[select.length] = option;
            }
        }
        
        var element = $(select)[0];
		$.jNice.SelectUpdate(element);        
    }
}
