

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("vehicleColoursControlId"));
        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 (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,
                inst.vehicleMakeSelect.value,
                inst.vehicleModelSelect.value,
                inst.currencyIdHidden.value,
                inst.unitOfMeasurementIdHidden.value,
                inst.shareInventoryHidden.value,
                inst.bodyStyleSelect.value,
                inst.handOfDriveSelect.value,
                inst.maxAgeSelect.value,
                inst.maxMileageSelect.value,
                inst.minPriceToSelect.value,
                inst.maxPriceToSelect.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.bodyStyleSelect,
                response.value.Tables[0],
                "Id",
                "Name",2,0);        
            
           if (this.optionsChangeEventSource.id != this.vehicleMakeSelect.id || this.vehicleMakeSelect.value=='-2' || this.vehicleMakeSelect.value=='0' )
              {     
            UpdateSelect(
                this.vehicleMakeSelect,
                response.value.Tables[1],
                "Id",
                "Name",1,1);        
              }

          if (this.optionsChangeEventSource.id != this.vehicleModelSelect.id || this.vehicleModelSelect.value=='-1' || this.vehicleModelSelect.value=='0' )
              {     
          
            UpdateSelect(
                this.vehicleModelSelect,
                response.value.Tables[2],
                "Id",
                "Name",2,0);        
             }
          
          
                                                                        
            UpdateSelect(
                this.minPriceToSelect,
                response.value.Tables[3],
                "Value",
                "Text",2,0);             
            
            UpdateSelect(
                this.maxPriceToSelect,
                response.value.Tables[4],
                "Value",
                "Text",2,0);            
            
            UpdateSelect(
                this.maxMileageSelect,
                response.value.Tables[5],
                "Value",
                "Text",2,0);            
            
            UpdateSelect(
                this.maxAgeSelect,
                response.value.Tables[6],
                "Value",
                "Text",1,1);                  
         
             UpdateSelect(
                this.handOfDriveSelect,
                response.value.Tables[8],
                "Value",
                "Text",1,0);                  
            
             
             var ColourSampleDiv='<div class="cBlock group floatLeft" id="ColoursDiv">';
             ColourSampleDiv+='<div class="group"><div class="colorSelected"><div class="color colorcss"><!-- --></div>';
          ColourSampleDiv+='</div><input id='; 
          ColourSampleDiv+='"ctl00_ctl00_cphbasecontent_cphbasecontent_searchOptionsCoreControl_vehicleColours"';
          ColourSampleDiv+=' name="ctl00$ctl00$cphbasecontent$cphbasecontent$searchOptionsCoreControl$vehicleColours"'; 
          ColourSampleDiv+=' class="styledCB" value="valueId" ifChecked type="checkbox">';
          ColourSampleDiv+=' </div> <label for="colorcss">ColorText</label> </div>';
 
          UpdateColours(response.value.Tables[7], ColourSampleDiv,this.vehicleColourSelect);
                        
         
		           
           
           //updateWrapper();      
    }
}




function UpdateColours(dataTable,colourSampleDiv, coloursIfSelected)
{
     oForm = document.forms[0];  
     ColorItems=   oForm.elements[coloursIfSelected.id];
     var list='';
     for(var i = 0; i < ColorItems.length; i++) 
      {
         if(ColorItems[i].checked == true) 
         {
           list=list+','+ColorItems[i].value;          
         }
      }
     list=list+',';     
     var coloursBox=document.getElementById("ColoursBox");    
     coloursBox.innerHTML='';
       
    var colourSampleDivItem;
    for (var i = 0; i < dataTable.Rows.length; i++)
        {
            colourSampleDivItem=colourSampleDiv;
            var value = dataTable.Rows[i]['Id'];           
            var text = dataTable.Rows[i]['Name'];
            var cssText = dataTable.Rows[i]['CssName'];
            var selected = dataTable.Rows[i]['Selected'];
                        
           colourSampleDivItem=colourSampleDivItem.replace("valueId",value);
           colourSampleDivItem=colourSampleDivItem.replace("ColorText",text);
           colourSampleDivItem=colourSampleDivItem.replace("colorcss",cssText);
           colourSampleDivItem=colourSampleDivItem.replace("colorcss",cssText);
           
           if (list.indexOf(","+value+",")>-1)
             {
             colourSampleDivItem=colourSampleDivItem.replace('ifChecked','checked="checked"');
            }
            else
            {
              colourSampleDivItem=colourSampleDivItem.replace('ifChecked','');
            }         
         coloursBox.innerHTML=coloursBox.innerHTML+colourSampleDivItem;
        }
    if ($('.styledCB').length) {
			$('.styledCB').ezMark()
		};

}


function UpdateSelect(select, dataTable, valueFieldName, textFieldName, updateFromIndex, needLastOption)
{
    selectedSpan=select.parentNode.childNodes[1];
    //alert(selectedSpan.tagName);
   // alert(selectedSpan.innerHTML);
    var lastOption=null;   
    if (needLastOption==1)
      {
       lastOption=select.options[select.options.length-1];      
      }
    var selectedItem=select.value;  
    if (select != null)
    { 
        // remove all but option 1
        //select.options.length = 1;
    
        for (var i = updateFromIndex; i < select.options.length; i++)
        {            
           // if (select.options[i].value > 0)
           // {   
                select.options[i] = null;
                i--;
           // }             
        }
        
        var selectedIndex = 0;
        select.options[0].selected=true;
        for (var i = 0; i < dataTable.Rows.length; i++)
        {
            
            var value = dataTable.Rows[i][valueFieldName];           
            var text = dataTable.Rows[i][textFieldName];
            var option= new Option(text, value)
           // alert(option.value + ' loop ' + option.text);
            if (value==selectedItem)
            {
              option.selected=true;
            }
            select.options.add(option);
            
            //select.options.add(new Option(text, value));            
        }
        if (lastOption!=null)
           {
             select.options.add(lastOption);
           }
    
    }

   //selectedSpan.innerHTML=select.options[select.selectedIndex].text;

}
    

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();
    }
}

