

function SearchOptionCore()
{
}

SearchOptionCore.prototype.languageIdHidden = null;
SearchOptionCore.prototype.oemIdHidden = null;
SearchOptionCore.prototype.dealerIdHidden = null;
SearchOptionCore.prototype.dealerGroupIdHidden = null;
SearchOptionCore.prototype.oemRegionIdHidden = null;
SearchOptionCore.prototype.oemMarketIdHidden = null;
SearchOptionCore.prototype.currencyIdHidden = null;
SearchOptionCore.prototype.unitOfMeasurementIdHidden = null;
SearchOptionCore.prototype.shareInventoryHidden = null;
SearchOptionCore.prototype.vehicleMakeSelect = null;
SearchOptionCore.prototype.vehicleModelSelect = null;
SearchOptionCore.prototype.bodyStyleSelect = null;
SearchOptionCore.prototype.minPriceToSelect = null;
SearchOptionCore.prototype.vehicleColourSelect = null;
SearchOptionCore.prototype.handOfDriveSelect = null;
SearchOptionCore.prototype.maxMileageSelect = null;
SearchOptionCore.prototype.maxPriceToSelect = null;
SearchOptionCore.prototype.maxAgeSelect = null;
SearchOptionCore.prototype.zipCodeText = null;
SearchOptionCore.prototype.distanceSelect = null;
SearchOptionCore.prototype.resetSearchButton = null;
SearchOptionCore.prototype.submitButton = null;
SearchOptionCore.prototype.optionsChangeEventSource = null;


SearchOptionCore.getClassInstance = function(controlId)
{
    var control = document.getElementById(controlId);
        
    if (control != null && control.searchOptionClassInstance == null)
    {
        var inst = new SearchOptionCore();
                                
        inst.languageIdHidden = document.getElementById(control.getAttribute("languageIdControlId"));
        inst.oemIdHidden = document.getElementById(control.getAttribute("oemIdControlId"));
        inst.dealerIdHidden = document.getElementById(control.getAttribute("dealerIdControlId"));
        inst.dealerGroupIdHidden = document.getElementById(control.getAttribute("dealerGroupIdControlId"));
        inst.oemRegionIdHidden = document.getElementById(control.getAttribute("oemRegionIdControlId"));
        inst.oemMarketIdHidden = document.getElementById(control.getAttribute("oemMarketIdControlId"));
        inst.currencyIdHidden = document.getElementById(control.getAttribute("currencyIdControlId"));
        inst.unitOfMeasurementIdHidden = document.getElementById(control.getAttribute("unitOfMeasurementIdControlId"));
        inst.shareInventoryHidden = document.getElementById(control.getAttribute("shareInventoryControlId"));
        inst.vehicleMakeSelect = document.getElementById(control.getAttribute("vehicleMakeControlId"));
        inst.vehicleModelSelect = document.getElementById(control.getAttribute("vehicleModelControlId"));
        inst.bodyStyleSelect = document.getElementById(control.getAttribute("bodyStyleControlId"));
        inst.minPriceToSelect = document.getElementById(control.getAttribute("priceFromControlId"));
        inst.vehicleColourSelect = document.getElementById(control.getAttribute("vehicleColourControlId"));
        inst.handOfDriveSelect = document.getElementById(control.getAttribute("handOfDriveControlId"));
        inst.maxMileageSelect = document.getElementById(control.getAttribute("mileageToControlId"));
        inst.maxPriceToSelect = document.getElementById(control.getAttribute("priceToControlId"));
        inst.maxAgeSelect = document.getElementById(control.getAttribute("maxAgeControlId"));
        inst.zipCodeText = document.getElementById(control.getAttribute("zipCodeControlId"));
        inst.distanceSelect = document.getElementById(control.getAttribute("distanceControlId"));
               
        inst.resetSearchButton = document.getElementById(control.getAttribute("resetSearchButtonControlId"));
        inst.submitButton = document.getElementById(control.getAttribute("submitButtonControlId"));
        
            
        control.searchOptionClassInstance = inst;
    }
       
    return control == null ? null : control.searchOptionClassInstance;
}


SearchOptionCore.optionChange = function(controlId, eventSource)
{    
    var inst = SearchOptionCore.getClassInstance(controlId);
            
    if (inst != null)
    {
        var vehicleMakeId = 0;
        var vehicleModelId = 0;
                    
        if(eventSource.id == inst.vehicleMakeSelect.id)
        {
            vehicleMakeId = eventSource.value;
        }
        
        if(eventSource.id == inst.vehicleModelSelect.id)
        {
            vehicleModelId = eventSource.value;
            vehicleMakeId = inst.vehicleMakeSelect.value;
        }
        
        if (SearchOptionsControl != null &&      
            inst.languageIdHidden != null &&      
            inst.oemIdHidden != null &&
            inst.dealerIdHidden != null &&
            inst.dealerGroupIdHidden != null &&
            inst.oemRegionIdHidden != null &&
            inst.oemMarketIdHidden != null &&
            vehicleMakeId != null &&
            vehicleModelId != null &&
            inst.currencyIdHidden != null &&
            inst.unitOfMeasurementIdHidden != null &&
            inst.shareInventoryHidden != null)
        {
            inst.optionsChangeEventSource = eventSource;
                       
            SearchOptionsControl.SearchOptionCoreChange(    
                inst.languageIdHidden.value,            
                inst.oemIdHidden.value,
                inst.dealerIdHidden.value,
                inst.dealerGroupIdHidden.value,
                inst.oemRegionIdHidden.value,
                inst.oemMarketIdHidden.value,
                vehicleMakeId,
                vehicleModelId,
                inst.currencyIdHidden.value,
                inst.unitOfMeasurementIdHidden.value,
                inst.shareInventoryHidden.value,          
                Delegate.create(inst, inst.optionsChangeCallBack));      
        }
    }
}
    
SearchOptionCore.prototype.optionsChangeCallBack = function(response)
{    
    if (response.error == null)   
    {   
        //If VehicleMake has been selected repopulate VehicleModel and 
        //VehicleVariant (this is not directly linked to Make, however repopulation is required to clear any previous selections)
        if (this.optionsChangeEventSource.id == this.vehicleMakeSelect.id)
        {
            UpdateSelect(
                this.vehicleModelSelect,
                response.value.Tables[0],
                "Id",
                "Name");        
                
            UpdateSelect(
                this.bodyStyleSelect,
                response.value.Tables[1],
                "Id",
                "Name");   
                
            UpdateSelect(
                this.maxAgeSelect,
                response.value.Tables[2],
                "Value",
                "Text");                  
         }
         
         //If VehicleModel has been selected repopulate BodyStyle and MaxAge
         if (this.optionsChangeEventSource.id == this.vehicleModelSelect.id)
         {      
            UpdateSelect(
                this.bodyStyleSelect,
                response.value.Tables[1],
                "Id",
                "Name");  
            
            UpdateSelect(
                this.maxAgeSelect,
                response.value.Tables[2],
                "Value",
                "Text");                   
         }        
    }
}


function UpdateSelect(select, dataTable, valueFieldName, textFieldName)
{
    if (select != null)
    { 
        for (var i = 0; i < select.options.length; i++)
        {            
            if (select.options[i].value > 0)
            {   
                select.options[i] = null;
                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));            
        }
    }
}
    

SearchOptionCore.ToggleAllColours = function(controlName, AskingState)
{
    var elements = document.getElementsByName(controlName);
    
   for (i=0; i<elements.length; i++)
    {
   
    if (AskingState=='true')
       elements[i].checked='Checked';
    else
       elements[i].checked='';
 
   }
}


SearchOptionCore.submitButtonClick = function(controlId, controlName)
{
    var inst = SearchOptionCore.getClassInstance(controlId);
    
    if (inst != null && inst.submitButton != null)
    {
        Utils.createSubmitHidden(Utils.getForm(), controlName, inst.submitButton.name);
    }
}

SearchOptionCore.resetSearchClick = function(controlId, controlName)
{
    var inst = SearchOptionCore.getClassInstance(controlId);
    
    if (inst != null && inst.resetSearchButton != null)
    {
        Utils.createSubmitHidden(Utils.getForm(), controlName, inst.resetSearchButton.name);
        Utils.getForm().submit();
    }
}
