﻿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.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.prototype.resetSearchButton = 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.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"));
        inst.resetSearchButton =
            document.getElementById(control.getAttribute("resetSearchButtonControlId"));
        
        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.resetSearchClick = function(controlId, controlName)
{
    
    var inst = SearchOptions.getClassInstance(controlId);
    
    if (inst != null && inst.resetSearchButton != null)
    {
        Utils.createSubmitHidden(Utils.getForm(), controlName, inst.resetSearchButton.name);   
        Utils.getForm().submit();   
    }
}

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)
{
    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)
        {
            //  if we changed the vehicle type, reset the make and model
            if(eventSource.id == inst.vehicleTypeIdHidden.id)
            {
                var elementFound = false;

                // add the default option in to the dropdown if empty                
                for (var i = 0; i < inst.vehicleMakeSelect.options.length; i++)
                {
                    if (inst.vehicleMakeSelect.options[i].value == "1315")
                    { 
                        elementFound = true;
                    }
                }
                
                if (!elementFound)
                {
                    inst.vehicleMakeSelect.options.add(new Option("Vauxhall", "1315"));
                }
            
                inst.vehicleMakeSelect.value = "1315";
                inst.vehicleModelSelect.value = "-1";
            }
        
            inst.optionsChangeEventSource = eventSource;
            
            SearchOptionsControl.SearchOptionsChange(
                inst.languageIdHidden.value,
                inst.dealerIdHidden.value,
                inst.dealerGroupIdHidden.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.vehicleModelSelect.id)
        {
            SearchOptions.updateSelect(
                this.vehicleVariantSelect,
                response.value.Tables[2],
                "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];
            var selectedIndex = false;
                       
            if (value != 0)
            {
                if (value == selectedValue)
                {        
                    selectedIndex = true;
                }
                else
                {
                    selectedIndex = false;
                }

                select.options.add(new Option(text, value, selectedIndex, selectedIndex));  
            }
        }
    }
}

