
function SearchOptionBasic()
{
}

SearchOptionBasic.prototype.languageIdHidden = null;
SearchOptionBasic.prototype.oemIdHidden = null;
SearchOptionBasic.prototype.dealerIdHidden = null;
SearchOptionBasic.prototype.dealerGroupIdHidden = null;
SearchOptionBasic.prototype.currencyIdHidden = null;
SearchOptionBasic.prototype.shareInventoryHidden = null;
SearchOptionBasic.prototype.vehicleMakeSelect = null;
SearchOptionBasic.prototype.vehicleModelSelect = null;
SearchOptionBasic.prototype.submitButton = null;
SearchOptionBasic.prototype.optionsChangeEventSource = null;


SearchOptionBasic.getClassInstance = function(controlId)
{
    var control = document.getElementById(controlId);
        
    if (control != null && control.searchOptionClassInstance == null)
    {
        var inst = new SearchOptionBasic();
                                
        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.currencyIdHidden = document.getElementById(control.getAttribute("currencyIdControlId"));
        inst.shareInventoryHidden = document.getElementById(control.getAttribute("shareInventoryControlId"));
        inst.vehicleMakeSelect = document.getElementById(control.getAttribute("vehicleMakeControlId"));
        inst.vehicleModelSelect = document.getElementById(control.getAttribute("vehicleModelControlId"));

        inst.submitButton = document.getElementById(control.getAttribute("submitButtonControlId"));
        
            
        control.searchOptionClassInstance = inst;
    }
       
    return control == null ? null : control.searchOptionClassInstance;
}


SearchOptionBasic.optionChange = function(controlId, eventSource)
{    
    var inst = SearchOptionBasic.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;
        }
        
        if (SearchOptionsControl != null &&      
            inst.languageIdHidden != null &&      
            inst.oemIdHidden != null &&
            inst.dealerIdHidden != null &&
            inst.dealerGroupIdHidden != null &&
            vehicleMakeId != null &&
            vehicleModelId != null &&
            inst.currencyIdHidden != null &&
            inst.shareInventoryHidden != null)
        {       
            inst.optionsChangeEventSource = eventSource;
                                   
            SearchOptionsControl.SearchOptionBasicChange(    
                inst.languageIdHidden.value,            
                inst.oemIdHidden.value,
                inst.dealerIdHidden.value,
                inst.dealerGroupIdHidden.value,
                vehicleMakeId,
                vehicleModelId,
                inst.currencyIdHidden.value,
                inst.shareInventoryHidden.value,          
                Delegate.create(inst, inst.optionsChangeCallBack));      
        }
    }
}
    


SearchOptionBasic.prototype.optionsChangeCallBack = function(response)
{    
    if (response.error == null)   
    {        
        // If VehicleMake has been selected repopulate VehicleModel
        // (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");         
         }    
    }   
    
}


SearchOptionBasic.submitButtonClick = function(controlId, controlName)
{
    var inst = SearchOptionBasic.getClassInstance(controlId);
    
    if (inst != null && inst.submitButton != null)
    {
        Utils.createSubmitHidden(Utils.getForm(), controlName, inst.submitButton.name);
    }
}




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.vehicleVariantSelect = null;
SearchOptionCore.prototype.minPriceToSelect = null;
SearchOptionCore.prototype.vehicleColourSelect = null;
SearchOptionCore.prototype.handOfDriveSelect = null;
SearchOptionCore.prototype.maxMileageSelect = null;
SearchOptionCore.prototype.maxPriceToSelect = 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.vehicleVariantSelect = document.getElementById(control.getAttribute("vehicleVariantControlId"));
        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.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;
        }
        
        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.vehicleVariantSelect,
                response.value.Tables[1],
                "Id",
                "Name");   
         }
         
         //If VehicleModel has been selected repopulate VehicleVariant
         if (this.optionsChangeEventSource.id == this.vehicleModelSelect.id)
         {      
            UpdateSelect(
                this.vehicleVariantSelect,
                response.value.Tables[1],
                "Id",
                "Name");   
         }        
    }
}
    

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)
{
    
    //window.location.href=window.location.href;
    var inst = SearchOptionCore.getClassInstance(controlId);
    
    if (inst != null && inst.resetSearchButton != null)
    {
        Utils.createSubmitHidden(Utils.getForm(), controlName, inst.resetSearchButton.name);
        Utils.getForm().submit();
    }
}


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));            
        }
    }
}




function SearchOptionGlobal()
{
}

SearchOptionGlobal.prototype.globalBlock = null;
SearchOptionGlobal.prototype.globalDistanceBlock = null;
SearchOptionGlobal.prototype.languageIdHidden = null;
SearchOptionGlobal.prototype.oemIdHidden = null;
SearchOptionGlobal.prototype.dealerIdHidden = null;
SearchOptionGlobal.prototype.dealerGroupIdHidden = null;
SearchOptionGlobal.prototype.shareInventoryHidden = null;
SearchOptionGlobal.prototype.postcodeSearchRegionsHidden = null;
SearchOptionGlobal.prototype.postcodeSearchMarketssHidden = null;
SearchOptionGlobal.prototype.oemRegionIdRadio = null;
SearchOptionGlobal.prototype.oemMarketIdSelect = null;
SearchOptionGlobal.prototype.distanceSelect = null;
SearchOptionGlobal.prototype.zipCodeText = null;
SearchOptionGlobal.prototype.vehicleMakeSelect = null;
SearchOptionGlobal.prototype.vehicleModelSelect = null;
SearchOptionGlobal.prototype.vehicleVariantSelect = null;
SearchOptionGlobal.prototype.currencyIdSelect = null;
SearchOptionGlobal.prototype.unitOfMeasurementIdSelect = null;
SearchOptionGlobal.prototype.maxPriceToSelect = null;
SearchOptionGlobal.prototype.maxMileageSelect = null;
SearchOptionGlobal.prototype.zipCodeTranslationText = null;
SearchOptionGlobal.prototype.postCodeTranslationText = null;
SearchOptionGlobal.prototype.submitButton = null;
SearchOptionGlobal.prototype.resetCookieLink = null;
SearchOptionGlobal.prototype.optionsChangeEventSource = null;


SearchOptionGlobal.getClassInstance = function(controlId)
{    
    var control = document.getElementById(controlId);
        
    if (control != null && control.searchOptionClassInstance == null)
    {
        var inst = new SearchOptionGlobal();

        inst.globalBlock = document.getElementById(control.getAttribute("globalBlockControlId"));
        inst.globalDistanceBlock = document.getElementById(control.getAttribute("globalDistanceBlockControlId"));
                                
        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.shareInventoryHidden = document.getElementById(control.getAttribute("shareInventoryControlId"));
        inst.postcodeSearchRegionsHidden = document.getElementById(control.getAttribute("postcodeSearchRegionsControlId"));
        inst.postcodeSearchMarketsHidden = document.getElementById(control.getAttribute("postcodeSearchMarketsControlId"));
        
        inst.oemRegionIdRadio = document.getElementById(control.getAttribute("oemRegionIdControlId"));
        inst.oemMarketIdSelect = document.getElementById(control.getAttribute("oemMarketIdControlId"));
        inst.distanceSelect = document.getElementById(control.getAttribute("distanceControlId"));
        inst.zipCodeText = document.getElementById(control.getAttribute("zipCodeControlId"));
        
        inst.vehicleMakeSelect = document.getElementById(control.getAttribute("vehicleMakeControlId"));
        inst.vehicleModelSelect = document.getElementById(control.getAttribute("vehicleModelControlId"));
        inst.vehicleVariantSelect = document.getElementById(control.getAttribute("vehicleVariantControlId"));
        inst.currencyIdSelect = document.getElementById(control.getAttribute("currencyIdControlId"));
        inst.unitOfMeasurementIdSelect = document.getElementById(control.getAttribute("unitOfMeasurementIdControlId"));
        inst.maxPriceToSelect = document.getElementById(control.getAttribute("priceToControlId"));
        inst.maxMileageSelect = document.getElementById(control.getAttribute("mileageToControlId"));
        inst.zipCodeTranslationText = document.getElementById("zipCodeTranslationTextId");
        inst.postCodeTranslationText = document.getElementById("postCodeTranslationTextId");
                
        inst.submitButton = document.getElementById(control.getAttribute("submitButtonControlId"));
        inst.resetCookieLink = document.getElementById(control.getAttribute("resetCookieLinkControlId"));
            
        control.searchOptionClassInstance = inst;
    }
       
    return control == null ? null : control.searchOptionClassInstance;
}

SearchOptionGlobal.HideGlobalBlock = function(controlId)
{
    var inst = SearchOptionGlobal.getClassInstance(controlId);
    
    if (inst != null)
    {        
        this.globalBlock.style.display = "none";     
    }
}

SearchOptionGlobal.ToggleGlobalDistanceBlock = function(controlId, eventSource)
{
    var inst = SearchOptionGlobal.getClassInstance(controlId);
            
    if (inst != null)
    {
        if(inst.globalDistanceBlock != null)
        {
            var postcodeSearchEnabled = false;
            
            if(eventSource.id == inst.oemRegionIdRadio.id && inst.postcodeSearchRegionsHidden != null)
            {                            
                var oemRegionIds = inst.postcodeSearchRegionsHidden.value;
                var oemRegionIdArray = oemRegionIds.split(",");
                
                
                for(var i=0; i<oemRegionIdArray.length; i++)
                {
                    if(oemRegionIdArray[i] == eventSource.value)
                    {
                        postcodeSearchEnabled = true;                        
                        break;
                    }
                }                  
            }        
            else if(eventSource.id == inst.oemMarketIdSelect.id && inst.postcodeSearchMarketsHidden != null)
            {
                var oemMarketIds = inst.postcodeSearchMarketsHidden.value;
                var oemMarketIdArray = oemMarketIds.split(",");
                
                for(var i=0; i<oemMarketIdArray.length; i++)
                {
                    if(oemMarketIdArray[i] == eventSource.value)
                    {
                        postcodeSearchEnabled = true;    
                        break;
                    }
                }                     
            }   
            
            if(postcodeSearchEnabled)
            {
                inst.globalDistanceBlock.style.display = "block";
            }
            else
            {
                inst.globalDistanceBlock.style.display = "none";
            }
        }    
    }
}

SearchOptionGlobal.CheckRadioHidden = function(controlId)
{
  var radioHidden = document.getElementById(controlId);

  if (radioHidden != null)
  {
    radioHidden.checked = "checked";
  }
}

SearchOptionGlobal.optionChange = function(controlId, eventSource)
{
    var inst = SearchOptionGlobal.getClassInstance(controlId);
            
    if (inst != null)
    {
        // Check Global, if Dealer level then set Global params to 0
        var oemRegionId = inst.oemRegionIdRadio.value;
        var oemMarketId = inst.oemMarketIdSelect.value;
        var vehicleMakeId = inst.oemIdHidden.value;
        var vehicleModelId = 0;
        var currencyId = 0;
        var unitOfMeasurementId = 0;
        
        if(inst.globalBlock.style.display != "none")
        {
            if(eventSource.id == inst.oemRegionIdRadio.id)
            {                
                oemRegionId = eventSource.value;
                oemMarketId = 0;
                                
                // US                
                if(oemRegionId == 1117)
                {
                    inst.zipCodeText.value = inst.zipCodeTranslationText.value;
                    oemRegionId = 0;
                    oemMarketId = 1447;                    
                }
                else if(oemRegionId == 1120)
                {
                    inst.zipCodeText.value = inst.postCodeTranslationText.value;                    
                    oemRegionId = 0;
                    oemMarketId = 1446;                    
                }
                else
                {
                    inst.zipCodeText.value = inst.postCodeTranslationText.value;                    
                }
                                
                if(inst.distanceSelect != null)
                {
                    inst.distanceSelect.value = "-1";
                }
            }
            
            if(eventSource.id == inst.oemMarketIdSelect.id)
            {
                oemMarketId = eventSource.value;
                oemRegionId = 0;
                                
                // US
                if(oemMarketId == "1447")
                {
                    inst.zipCodeText.value = inst.zipCodeTranslationText.value;
                }
                else
                {
                    inst.zipCodeText.value = inst.postCodeTranslationText.value;                    
                }                  
                                
                if(inst.distanceSelect != null)
                {
                    inst.distanceSelect.value = "-1";
                }
            }
            
            if(eventSource.id == inst.currencyIdSelect.id)
            {
                currencyId = eventSource.value;
            }
            
            if(eventSource.id == inst.unitOfMeasurementIdSelect.id)
            {
                unitOfMeasurementId = eventSource.value;
            }            
        }    
        
        if(eventSource.id == inst.vehicleModelSelect.id)
        {
            vehicleModelId = eventSource.value;
        }
        
        //  region/market overlap
//        if(oemRegionId == 1117)
//        {
//            oemMarketId = 1447;
//            oemRegionId = 0;
//        }
//        else if(oemRegionId == 1120)
//        {
//            oemMarketId = 1446;
//            oemRegionId = 0;
//        }
    
        if (SearchOptionsControl != null &&      
            inst.languageIdHidden != null &&      
            inst.oemIdHidden != null &&
            inst.dealerIdHidden != null &&
            inst.dealerGroupIdHidden != null &&
            oemRegionId != null &&
            oemMarketId != null &&
            vehicleMakeId != null &&
            vehicleModelId != null &&
            currencyId != null &&
            unitOfMeasurementId != null &&
            inst.shareInventoryHidden != null)
        {
            inst.optionsChangeEventSource = eventSource;
                       
            SearchOptionsControl.SearchOptionGlobalOverviewChange(    
                inst.languageIdHidden.value,            
                inst.oemIdHidden.value,
                inst.dealerIdHidden.value,
                inst.dealerGroupIdHidden.value,
                oemRegionId,
                oemMarketId,
                vehicleMakeId,
                vehicleModelId,
                currencyId,
                unitOfMeasurementId,
                inst.shareInventoryHidden.value,
                Delegate.create(inst, inst.optionsChangeCallBack));               
        }
    }
}
    


SearchOptionGlobal.prototype.optionsChangeCallBack = function(response)
{    
    if (response.error == null)   
    {
        this.globalBlock
        //If OEMRegion has been changed the following applies (this only applies at the Global level):
        // -Amend the selected item for OEMMarket
        // -Repopulate the Currency & Maximum Price
        // -Repopulate the UnitOfMeasurement & Maximum Mileage
        if (this.optionsChangeEventSource.id == this.oemRegionIdRadio.id)
        {
            var oemRegionId = this.optionsChangeEventSource.value; 
            var oemMarketId = 0;         
                                      
            if(oemRegionId == "1117")
            {
                oemMarketId = 1447;
            }
            else if(oemRegionId == "1120")
            {
                oemMarketId = 1446;
            }
            
            SearchOptionGlobal.updateSelect(                
                this.oemMarketIdSelect,
                response.value.Tables[6],
                "OemMarketID",
                "Name",
                "false",
                oemMarketId);
                
            SearchOptionGlobal.updateSelect(                           
                this.currencyIdSelect,
                response.value.Tables[8],
                "CurrencyId",
                "ShortName,LongName",
                "true",
                0);
                
            SearchOptionGlobal.updateSelect(                           
                this.distanceSelect,
                response.value.Tables[7],
                "Value",
                "Text",
                "false",
                0);
                
            SearchOptionGlobal.updateSelect(                                
                this.maxPriceToSelect,
                response.value.Tables[3],
                "Value",
                "Text",
                "false",
                0);
                
            SearchOptionGlobal.updateSelect(                                
                this.unitOfMeasurementIdSelect,
                response.value.Tables[9],
                "Id",
                "Name",
                "true",
                0);
                
            SearchOptionGlobal.updateSelect(                                
                this.maxMileageSelect,
                response.value.Tables[4],
                "Value",
                "Text",
                "false",
                0);        
                
            SearchOptionGlobal.updateSelect(
                this.vehicleModelSelect,
                response.value.Tables[1],
                "Id",
                "Name",
                "false",
                -1);        
                
            SearchOptionGlobal.updateSelect(
                this.vehicleVariantSelect,
                response.value.Tables[2],
                "Id",
                "Name",
                "false",
                -1);   
        }
        
        //If OEMMarket has been changed the following applies (this only applies at the Global level):        
        // -Repopulate the Currency & Maximum Price
        // -Repopulate the UnitOfMeasurement & Maximum Mileage
        // -If UK or US Market has been selected, repopulate OEMRegion and check UK or US respectively
        if (this.optionsChangeEventSource.id == this.oemMarketIdSelect.id)
        {   
            // If UK or US has been selected in the Market drop down set the UK or US Region Radio Button to true.
            var oemMarketId = this.optionsChangeEventSource.value; 
            var oemRegionId = 0;  
            var repopulateOemRegion = false;       
                                      
            if(oemMarketId == "1447")
            {
                oemRegionId = 1117;
                repopulateOemRegion = true;                
            }
            else if(oemMarketId == "1446")
            {
                oemRegionId = 1120;
                repopulateOemRegion = true;                
            }
            
            if(repopulateOemRegion)
            {               
                var regionRadioButtons = document.getElementsByName(this.oemRegionIdRadio.name);
                
                for (var i = 0; i < regionRadioButtons.length; i++)                
                {
                    if (regionRadioButtons[i].value == oemRegionId)
                    {
                        regionRadioButtons[i].checked = "true";
                    }
                }   
            }
            
        
            SearchOptionGlobal.updateSelect(   
                this.currencyIdSelect,
                response.value.Tables[8],
                "CurrencyId",
                "ShortName,LongName",
                "true",
                0);
                
            SearchOptionGlobal.updateSelect(                           
                this.distanceSelect,
                response.value.Tables[7],
                "Value",
                "Text",
                "false",
                0);

            SearchOptionGlobal.updateSelect(                
                this.maxPriceToSelect,
                response.value.Tables[3],
                "Value",
                "Text",
                "false",
                0);
                
            SearchOptionGlobal.updateSelect(                
                this.unitOfMeasurementIdSelect,
                response.value.Tables[9],
                "Id",
                "Name",
                "true",
                0);
                
            SearchOptionGlobal.updateSelect(                
                this.maxMileageSelect,
                response.value.Tables[4],
                "Value",
                "Text",
                "false",
                0); 
                
            SearchOptionGlobal.updateSelect(
                this.vehicleModelSelect,
                response.value.Tables[1],
                "Id",
                "Name",
                "false",
                0);        
                
            SearchOptionGlobal.updateSelect(
                this.vehicleVariantSelect,
                response.value.Tables[2],
                "Id",
                "Name",
                "false",
                0);   
        }
                 
         //If VehicleModel has been selected repopulate VehicleVariant
         if (this.optionsChangeEventSource.id == this.vehicleModelSelect.id)
         {      
            SearchOptionGlobal.updateSelect(
                this.vehicleVariantSelect,
                response.value.Tables[2],
                "Id",
                "Name",
                "false",
                0);   
         }
         
         //If Currency has been selected repopulate Maximum Price
         if (this.optionsChangeEventSource.id == this.currencyIdSelect.id)
         {
               SearchOptionGlobal.updateSelect(
                this.maxPriceToSelect,
                response.value.Tables[3],
                "Value",
                "Text",
                "false",
                0);   
         }
         
         //If Mileage(UnitOfMeasurement) has been selected repopulate Maximum Mileage
         if (this.optionsChangeEventSource.id == this.unitOfMeasurementIdSelect.id)
         {
               SearchOptionGlobal.updateSelect(
                this.maxMileageSelect,
                response.value.Tables[4],
                "Value",
                "Text",
                "false",
                0);   
         }
    }
}
    
SearchOptionGlobal.updateSelect = function(select, dataTable, valueFieldName, textFieldName, checkSelected, selectedValue)
{
    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;
                        
        var commaIndex = textFieldName.indexOf(',');
        var textFieldNameExt = '';        
        
        if(commaIndex != -1)
        {
            textFieldNameExt = textFieldName.substr(commaIndex+1,textFieldName.length - (commaIndex+1));
            textFieldName = textFieldName.substr(0,commaIndex);
        }
        
        for (var i = 0; i < dataTable.Rows.length; i++)
        {
            var value = dataTable.Rows[i][valueFieldName];           
            var text = dataTable.Rows[i][textFieldName];
            
            if(textFieldNameExt != '')
            {
                text = text + ' ' + dataTable.Rows[i][textFieldNameExt];
            }
            
            select.options.add(new Option(text, value));      
            
            if(selectedValue != 0 && (value == selectedValue))
            {
                select.selectedIndex = select.options.length - 1;
            }
            else if((checkSelected == "true") && (dataTable.Rows[i]["Selected"] == true))
            {
                select.selectedIndex = select.options.length - 1;
            }
        }
    }
}

SearchOptionGlobal.submitButtonClick = function(controlId, controlName)
{
    var inst = SearchOptionGlobal.getClassInstance(controlId);
    
    if (inst != null && inst.submitButton != null)
    {
        Utils.createSubmitHidden(Utils.getForm(), controlName, inst.submitButton.name);
    }
}

SearchOptionGlobal.resetCookies = function(controlId, controlName)
{
    var inst = SearchOptionGlobal.getClassInstance(controlId);
    
    if (inst != null && inst.resetCookieLink != null)
    {
        Utils.createSubmitHidden(Utils.getForm(), controlName, inst.resetCookieLink.name);
        Utils.getForm().submit();
    }
}

SearchOptionGlobal.checkZipCodeBox = function(controlId)
{
    var inst = SearchOptionGlobal.getClassInstance(controlId);
    
    if(inst.zipCodeText.value == inst.zipCodeTranslationText.value ||
        inst.zipCodeText.value == inst.postCodeTranslationText.value)
        {
            inst.zipCodeText.value = '';
        }   
}














