﻿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.prototype.vehicleModelsSelect = 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.vehicleModelsSelect =  document.getElementById(control.getAttribute("vehicleModelsControlId"));
        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("vehicleColoursControlId"));
        inst.bodyStyleSelect = document.getElementById(control.getAttribute("bodyStyleControlId"));
        inst.submitButton = document.getElementById(control.getAttribute("submitButtonControlId"));
        inst.resetSearchButton = document.getElementById(control.getAttribute("resetSearchButtonControlId"));
        inst.vehicleModelSelect =  document.getElementById(control.getAttribute("vehicleModelControlId"));
        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)
{
    
    //alert('hi');
    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,
                -1,
                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"
            );

           toggleAdvSearch(this.vehicleTypeIdHidden.value);

        }
            
        if (this.optionsChangeEventSource.id == this.vehicleMakeSelect.id  || this.optionsChangeEventSource.id == this.vehicleTypeIdHidden.id )
        {
              
              var modelBox=document.getElementById("model");  
              if (modelBox!=null)
              {              
                var ModelSampleDiv='<li><label><input type="checkbox" Id="ctl00_ctl00_PageBody_cphContent_searchOptionsCtrl_vehicleModels" name="ctl00$ctl00$PageBody$cphContent$searchOptionsCtrl$vehicleModels" value="ModelValue" ifChecked >';
                ModelSampleDiv+='</input>ModelName</label>';                           
                UpdateModels(response.value.Tables[1], ModelSampleDiv,this.vehicleModelsSelect);          
              
                var ColourSampleDiv='<li class="colour item colourStyleName" title="ColourTitle">';
                ColourSampleDiv+='<input type="checkbox" value="valueId" id="ctl00_ctl00_PageBody_cphContent_searchOptionsCtrl_vehicleColours" name="ctl00$ctl00$PageBody$cphContent$searchOptionsCtrl$vehicleColours" class="styled" ifChecked ></input>';
                ColourSampleDiv+='<label class="hidden">ColourLabel</label></li>';                     
                UpdateColours(response.value.Tables[3], ColourSampleDiv,this.vehicleColourSelect);
              
              }
              else
              {
              
                SearchOptions.updateSelect(
                this.vehicleModelSelect,
                response.value.Tables[1],
                "Id",
                "Name"
                    );
              
              }      
                         
          
           
                 
        }
        
        
    }
}



 function toggleAdvSearch(vehicleType)
 {
      var AdvSrchCars=document.getElementById("CarsAdvSearch"); 
      var AdvSrchVans=document.getElementById("VansAdvSearch");
      var divWarrantyProgram = document.getElementById("divWarrantyProgram");
      var divWarrantyProgramnq3 = document.getElementById("divWarrantyProgramnq3");
      
      var imgNQLogo = document.getElementById("imgNQLogo");
      var imgNQINfo = document.getElementById("imgNQInfo");
      
      if (AdvSrchCars==null || AdvSrchVans==null) return;
        
     if (vehicleType==3)
     {
        AdvSrchCars.style.display='none';
        AdvSrchVans.style.display='block';
        // switch off Warranty program
        if (divWarrantyProgram != null)
        {
            divWarrantyProgram.style.display = 'none';
        }
        if (divWarrantyProgramnq3 != null)
        {
            divWarrantyProgramnq3.style.display = 'none';
        }
        if (imgNQLogo != null)
        {
            imgNQLogo.title = 'Network Q approved used vans';
        }
        
        if (imgNQINfo != null)
        {
            imgNQINfo.title = 'Network Q approved used vans';
        }
     }
     else
     {
        AdvSrchCars.style.display='block';
        AdvSrchVans.style.display='none';
        // switch on Warranty program
        if (divWarrantyProgram != null)
        {
            divWarrantyProgram.style.display = 'block';
        }
        if (divWarrantyProgramnq3 != null)
        {
            divWarrantyProgramnq3.style.display = 'block';
        }
        if (imgNQLogo != null)
        {
            imgNQLogo.title = 'Network Q approved used cars';
        }
        if (imgNQINfo != null)
        {
            imgNQINfo.title = 'Network Q approved used cars';
        }
     }
 }


function UpdateModels(dataTable,modelSampleDiv, modelsIfSelected)
{    
     var ModelItems;
     oForm = document.forms[0];  
     if (modelsIfSelected != null)   ModelItems=   oForm.elements[modelsIfSelected.id];
     var list='';
     
     if (ModelItems != null)
     {
     for(var i = 0; i < ModelItems.length; i++) 
      {
         if(ModelItems[i].checked == true) 
         {
           list=list+','+ModelItems[i].value;          
         }
      }
     }
     
     list=list+',';     
     var modelBox=document.getElementById("model");        
     modelBox.innerHTML='';
      
    var modelSampleDivItem;
    for (var i = 0; i < dataTable.Rows.length; i++)
        {
            modelSampleDivItem=modelSampleDiv;
            var value = dataTable.Rows[i]['Id'];           
            if (value<=0) continue;
            var text = dataTable.Rows[i]['Name'];
           modelSampleDivItem=modelSampleDivItem.replace("ModelValue",value);
           modelSampleDivItem=modelSampleDivItem.replace("ModelName",text);   
           if (list.indexOf(","+value+",")>-1)
             {
             modelSampleDivItem=modelSampleDivItem.replace('ifChecked','checked="checked"');
            }
            else
            {
              modelSampleDivItem=modelSampleDivItem.replace('ifChecked','');
            }         
         modelBox.innerHTML=modelBox.innerHTML+modelSampleDivItem;
         
        }
}




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("ColourCheckBoxes");        
     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("ColourTitle",text);
           colourSampleDivItem=colourSampleDivItem.replace("colourStyleName",text);
           colourSampleDivItem=colourSampleDivItem.replace("ColourLabel",text);
           
           if (list.indexOf(","+value+",")>-1)
             {
             colourSampleDivItem=colourSampleDivItem.replace('ifChecked','checked="checked"');
            }
            else
            {
              colourSampleDivItem=colourSampleDivItem.replace('ifChecked','');
            }         
         coloursBox.innerHTML=coloursBox.innerHTML+colourSampleDivItem;
         
        }
    
    
       var inputs = document.getElementsByTagName("input"), span = Array(), textnode, option, active;
		for(a = 0; a < inputs.length; a++) 
		{
			if((inputs[a].type == "checkbox" || inputs[a].type == "radio") && inputs[a].className == "styled") 
			{
				span[a] = document.createElement("span");
				span[a].className = inputs[a].type;

				if(inputs[a].checked == true) 
				{
					if(inputs[a].type == "checkbox") 
					{
						position = "0 -" + (checkboxHeight*2) + "px";
						span[a].style.backgroundPosition = position;
					} 
					else 
					{
						position = "0 -" + (radioHeight*2) + "px";
						span[a].style.backgroundPosition = position;
					}
				}
				inputs[a].parentNode.insertBefore(span[a], inputs[a]);
				inputs[a].onchange = Custom.clear;
				if(!inputs[a].getAttribute("disabled")) 
				{
					span[a].onmousedown = Custom.pushed;
					span[a].onmouseup = Custom.check;
				} 
				else 
				{
					span[a].className = span[a].className += " disabled";
				}
			}
		}
           
}

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));  
                
                
               
            }
        }
    }
}


