﻿function SearchOptions()
{
}

SearchOptions.prototype.summaryBlock = null;
SearchOptions.prototype.optionsBlock = null;
SearchOptions.prototype.advancedOptionsBlock = null;
SearchOptions.prototype.advancedOptionsLink = null;
SearchOptions.prototype.advancedSearchModeHidden = null;
SearchOptions.prototype.languageIdHidden = null;
SearchOptions.prototype.dealerIdHidden = null;
SearchOptions.prototype.dealerGroupIdHidden = null;
SearchOptions.prototype.vehicleTypeIdHidden = null;
SearchOptions.prototype.vehicleMakeSelect = null;
SearchOptions.prototype.vehicleModelSelect = 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.summaryBlock =
            document.getElementById(control.getAttribute("summaryBlockControlId"));
        inst.optionsBlock =
            document.getElementById(control.getAttribute("optionsBlockControlId"));
        inst.advancedOptionsBlock =
            document.getElementById(control.getAttribute("advancedOptionsBlockControlId"));
        inst.advancedOptionsLink =
            document.getElementById(control.getAttribute("advancedOptionsLinkControlId"));
        inst.advancedSearchModeHidden =
            document.getElementById(control.getAttribute("advancedSearchModeControlId"));
        inst.languageIdHidden =
            document.getElementById(control.getAttribute("languageIdControlId"));
        inst.dealerIdHidden =
            document.getElementById(control.getAttribute("dealerIdControlId"));
        inst.dealerGroupIdHidden =
            document.getElementById(control.getAttribute("dealerGroupIdControlId"));
        inst.vehicleTypeIdHidden =
            document.getElementById(control.getAttribute("vehicleTypeIdControlId"));
        inst.vehicleMakeSelect =
            document.getElementById(control.getAttribute("vehicleMakeControlId"));
        inst.vehicleModelSelect =
            document.getElementById(control.getAttribute("vehicleModelControlId"));
        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);        
    }
}

SearchOptions.closeSummary = function(controlId)
{
    var inst = SearchOptions.getClassInstance(controlId);
    
    if (inst != null)
    {
        if (inst.summaryBlock != null)
        {
            inst.summaryBlock.style.display = "none";
        }
        if (inst.optionsBlock != null)
        {
            inst.optionsBlock.style.display = "block";
        }
    }
}

SearchOptions.advancedOptionsClick = function(controlId)
{
    var inst = SearchOptions.getClassInstance(controlId);
    
    if (inst != null)
    {
        if (inst.advancedOptionsBlock != null)
        {
            if (inst.advancedOptionsBlock.style.display == "none")
            {
                inst.showAdvancedOptions();
            }
            else
            {
                inst.hideAdvancedOptions();
            }
        }
        else
        {
            inst.advancedSearchModeHidden.value = 'true';
            inst.submitButton.click();
        }
    }
}

SearchOptions.prototype.showAdvancedOptions = function()
{
    if (this.advancedOptionsBlock != null)
    {
        this.advancedOptionsBlock.style.display = "block";
    }
    if (this.advancedOptionsLink != null)
    {
        this.advancedOptionsLink.innerHTML = 
            this.advancedOptionsLink.getAttribute("basicSearchText");
    }
    if (this.advancedSearchModeHidden != null)
    {
        this.advancedSearchModeHidden.value = "true";
    }
}

SearchOptions.prototype.hideAdvancedOptions = function()
{
    if (this.advancedOptionsBlock != null)
    {
        this.advancedOptionsBlock.style.display = "none";
    }
    if (this.advancedOptionsLink != null)
    {
        this.advancedOptionsLink.innerHTML =
            this.advancedOptionsLink.getAttribute("advancedSearchText");
    }
    if (this.advancedSearchModeHidden != null)
    {
        this.advancedSearchModeHidden.value = "false";
    }
}


SearchOptions.optionsChange = function(controlId, eventSource, value)
{
    var inst = SearchOptions.getClassInstance(controlId);
    
    if (inst != null)
    {
        if (SearchOptionsControl != null &&
            inst.languageIdHidden != null &&
            inst.dealerIdHidden != null &&
            inst.dealerGroupIdHidden != null &&
            inst.vehicleTypeIdHidden != null &&
            inst.vehicleMakeSelect != null &&
            inst.vehicleModelSelect != null)
        {            

            var makeValue = inst.vehicleMakeSelect.value;
            var modelValue = inst.vehicleModelSelect.value;
            
            if(eventSource.id == inst.vehicleTypeIdHidden.id)
            {
                makeValue = "-1";
                modelValue = "-1";
            }
            else
            {
                var vehicleTypeControls = document.getElementsByName(inst.vehicleTypeIdHidden.name);
                
                if(vehicleTypeControls.length > 1)
                {                    
                    for (var j=0; j<vehicleTypeControls.length; j++)  
                    { 
                        if (vehicleTypeControls[j].checked)  
                        {
                            value = vehicleTypeControls[j].value;
                            break;
                        } 
                    }
                }
                else
                {
                    value = inst.vehicleTypeIdHidden.value;
                }            
            }
                        
            
            inst.optionsChangeEventSource = eventSource;
            
            SearchOptionsControl.SearchOptionsChange(
                inst.languageIdHidden.value,
                inst.dealerIdHidden.value,
                inst.dealerGroupIdHidden.value,
                value,
                makeValue,
                modelValue,
                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",
                "Name"
            );
            
            SearchOptions.updateSelect(
                this.vehicleModelSelect,
                response.value.Tables[1],
                "Id",
                "Name"
            );
        }
               
        
        if (this.optionsChangeEventSource != this.vehicleModelSelect)
        {
            SearchOptions.updateSelect(
                this.vehicleModelSelect,
                response.value.Tables[1],
                "Id",
                "Name"
            );
        }
        if (this.optionsChangeEventSource != this.fuelTypeSelect)
        {
            SearchOptions.updateSelect(
                this.fuelTypeSelect,
                response.value.Tables[2],
                "Id",
                "Name"
            );
        }
        if (this.optionsChangeEventSource != this.transmissionTypeSelect)
        {
            SearchOptions.updateSelect(
                this.transmissionTypeSelect,
                response.value.Tables[3],
                "Id",
                "Name"
            );
        }
        if (this.optionsChangeEventSource != this.vehicleColourSelect)
        {
            SearchOptions.updateSelect(
                this.vehicleColourSelect,
                response.value.Tables[4],
                "Id",
                "Name"
            );
        }
        if (this.optionsChangeEventSource != this.bodyStyleSelect)
        {
            SearchOptions.updateSelect(
                this.bodyStyleSelect,
                response.value.Tables[5],
                "Id",
                "Name"
            );
        }
    }
}

SearchOptions.updateSelect = function(select, dataTable, valueFieldName, textFieldName)
{
    if (select != null)
    {
        var selectedValue = select.value;
        
        for (var i = 0; i < select.options.length; i++)
        {
            if (select.options[i].value < 0)
                continue;
            
            select.removeChild(select.childNodes[i]);
            i--;
        }
        
        var selectedIndex = 0;
        
        for (var i = 0; i < dataTable.Rows.length; i++)
        {
            var value = dataTable.Rows[i][valueFieldName];
            var text = dataTable.Rows[i][textFieldName];
            
            select.options.add(new Option(text, value));

            /*if (value == selectedValue)
            {
                select.selectedIndex = select.options.length - 1;
            }*/
        }
    }
}

