﻿function SearchOptions()
{
}

SearchOptions.prototype.languageIdHidden = null;
SearchOptions.prototype.dealerIdHidden = null;
SearchOptions.prototype.dealerGroupIdHidden = null;
SearchOptions.prototype.oemMarketIdHidden = null;
SearchOptions.prototype.vehicleTypeIdHidden = null;
SearchOptions.prototype.vehicleMakeSelect = null;
SearchOptions.prototype.vehicleModelSelect = null;
SearchOptions.prototype.vehicleVariantSelect = null;
SearchOptions.prototype.fuelTypeSelect = null;
SearchOptions.prototype.transmissionTypeSelect = null;
SearchOptions.prototype.vehicleColourSelect = null;
SearchOptions.prototype.bodyStyleSelect = null;
SearchOptions.prototype.submitButton = null;
SearchOptions.prototype.optionsChangeEventSource = null;

SearchOptions.getClassInstance = function(controlId)
{
    var control = document.getElementById(controlId);
    
    if (control != null && control.searchOptionsClassInstance == null)
    {
        var inst = new SearchOptions();

        inst.languageIdHidden = document.getElementById(control.getAttribute("languageIdControlId"));
        inst.dealerIdHidden = document.getElementById(control.getAttribute("dealerIdControlId"));
        inst.dealerGroupIdHidden = document.getElementById(control.getAttribute("dealerGroupIdControlId"));
        inst.oemMarketIdHidden = document.getElementById(control.getAttribute("oemMarketIdControlId"));        
        inst.vehicleTypeIdHidden = document.getElementById(control.getAttribute("vehicleTypeIdControlId"));
        inst.vehicleMakeSelect = document.getElementById(control.getAttribute("vehicleMakeControlId"));
        inst.vehicleModelSelect = document.getElementById(control.getAttribute("vehicleModelControlId"));
        inst.vehicleVariantSelect = document.getElementById(control.getAttribute("vehicleVariantControlId"));
        inst.fuelTypeSelect = document.getElementById(control.getAttribute("fuelTypeControlId"));
        inst.transmissionTypeSelect = document.getElementById(control.getAttribute("transmissionTypeControlId"));
        inst.vehicleColourSelect = document.getElementById(control.getAttribute("vehicleColourControlId"));
        inst.bodyStyleSelect = document.getElementById(control.getAttribute("bodyStyleControlId"));
        inst.submitButton = document.getElementById(control.getAttribute("submitButtonControlId"));
        
        control.searchOptionsClassInstance = inst;
    }
    
    return control == null ? null : control.searchOptionsClassInstance;
}

SearchOptions.submitButtonClick = function(controlId, controlName)
{
    var inst = SearchOptions.getClassInstance(controlId);
    
    if (inst != null && inst.submitButton != null)
    {        
        Utils.createSubmitHidden(Utils.getForm(), controlName, inst.submitButton.name);   
        Utils.getForm().submit();   
    }
}

SearchOptions.optionsChange = function(controlId, eventSource)
{
    var inst = SearchOptions.getClassInstance(controlId);
    
    if (inst != null)
    {
        if (SearchOptionsControl != null &&
            inst.languageIdHidden != null &&
            inst.dealerIdHidden != null &&
            inst.dealerGroupIdHidden != null &&
            inst.oemMarketIdHidden != null &&            
            inst.vehicleTypeIdHidden != null &&
            inst.vehicleMakeSelect != null &&
            inst.vehicleModelSelect != null)
        {            
            //  if we changed the vehicle type, reset the make and model
            if(eventSource.id == inst.vehicleTypeIdHidden.id)
            {
                inst.vehicleMakeSelect.value = "0";
                inst.vehicleModelSelect.value = "0";
            }

            inst.optionsChangeEventSource = eventSource;
            
            SearchOptionsControl.SearchOptionsChange(
                inst.languageIdHidden.value,
                inst.dealerIdHidden.value,
                inst.dealerGroupIdHidden.value,
                inst.oemMarketIdHidden.value,
                inst.vehicleTypeIdHidden.value,
                inst.vehicleMakeSelect.value,
                inst.vehicleModelSelect.value,
                Delegate.create(inst, inst.optionsChangeCallBack)
            );
        }
    }
}

SearchOptions.prototype.optionsChangeCallBack = function(response)
{
    if (response.error == null)
    {
        if (this.optionsChangeEventSource.id == this.vehicleTypeIdHidden.id)
        {
            SearchOptions.updateSelect(
                this.vehicleMakeSelect,
                response.value.Tables[0],
                "Id",
                "OemName"
            );

            SearchOptions.updateSelect(
                this.vehicleModelSelect,
                response.value.Tables[1],
                "Id",
                "Name"
            );
        }
        
        if (this.optionsChangeEventSource.id == this.vehicleMakeSelect.id)
        {
            SearchOptions.updateSelect(
            this.vehicleModelSelect,
            response.value.Tables[1],
            "Id",
            "Name"
            );
        }
    
        if (this.optionsChangeEventSource.id == this.vehicleMakeSelect.id || this.optionsChangeEventSource.id == this.vehicleModelSelect.id)
        {                  
             SearchOptions.updateSelect(
                this.vehicleVariantSelect,
                response.value.Tables[2],
                "Name",
                "Name"
            );
            
             SearchOptions.updateSelect(
                this.bodyStyleSelect,
                response.value.Tables[3],
                "Id",
                "Name"
            );
            
             SearchOptions.updateSelect(
                this.vehicleColourSelect,
                response.value.Tables[4],
               "Id",
                "Name"
            );
            
            SearchOptions.updateSelect(
                this.fuelTypeSelect,
                response.value.Tables[5],
               "Id",
                "Name"
            );
            
            SearchOptions.updateSelect(
                this.transmissionTypeSelect,
                response.value.Tables[6],
                "Id",
                "Name"
            );
        }
        
    }
}

SearchOptions.updateSelect = function(select, dataTable, valueFieldName, textFieldName)
{
    if (select != null)
    {
        var selectedValue = select.value;
        
        for (var i = 1; i < select.options.length; i++)
        {   
            select.removeChild(select.childNodes[i]);
            i--;
        }
        
        for (var i = 0; i < dataTable.Rows.length; i++)
        {
            var value = dataTable.Rows[i][valueFieldName];
            var text = dataTable.Rows[i][textFieldName];
            
            if (value != 0)
            {
                select.options.add(new Option(text, value));
            }
        }
    }
}

