var ARMakes; // Array to hold makes
var ARModels; // Array to hold models
var NewUsedFlag; // flag to indicate which makes and models have to be populated depending on the checkboxes
ARMakes = new Array();
ARModels = new Array();
// function to call onload of the page
function Initialize(cnChk,cuChk,FMake,FModel)
{

	// if new is checked and used is unchecked then fill new makes and models
	if (cnChk.checked==true && cuChk.checked==false)NewUsedFlag="N";
	// if new is unchecked and used is checked then fill used makes and models
	if (cnChk.checked==false && cuChk.checked==true)NewUsedFlag="U";
	// if both are checked then fill both kind of makes and models
	if (cnChk.checked==true && cuChk.checked==true)NewUsedFlag="U";
	// if both are unchecked then fill used both kind of makes and models
	if (cnChk.checked==false && cuChk.checked==false)NewUsedFlag="U";	
	// N for new, U for used or both checked or none
	FillMakes(NewUsedFlag,FMake,FModel);
	
}
// function to call when any of the checkboxes is clicked
function ReLoadMakesModels(cnChk,cuChk,FMake,FModel)
{
Initialize(cnChk,cuChk,FMake,FModel)
}
// Function to add model in the ARModels array
function AddModel(pi,i,n,nu)
{
	var index=ARModels.length;
	ARModels[index]=new Array();
	ARModels[index][0]=i; //id
	ARModels[index][1]=pi; //makeid
	ARModels[index][2]=n; //modelname
	ARModels[index][3]=nu; //new or used
}
// Function to add makes in the ARMakes array
function AddMake(i,n)
{
	var index=ARMakes.length;
	ARMakes[index]=new Array();
	ARMakes[index][0]=i; // id
	ARMakes[index][1]=n; //make name
}
// Function to be called at the time of initialization of the page or whenever any of the new or used checkboxes is checked or unchecked.
function FillMakes(newused,FMake,FModel)
{
var prevMake=FMake.value;
// empty the makes combo
FMake.options.length=0;
// add default item in make combo
if(location.pathname.toUpperCase().indexOf("WEBVEHICLELIST.ASPX")!=-1)
FMake.options[FMake.options.length]=new Option("All Makes","");
else if(location.pathname.toUpperCase().indexOf("WEBADVSEARCH_ES.ASPX")!=-1)
FMake.options[FMake.options.length]=new Option("Seleccione la Marca","");
else
FMake.options[FMake.options.length]=new Option("Select Make","");

// add makes from ARMakes array into makes combo depending on what is the current condition of new and used.
	for(a=0;a<ARMakes.length;a++)
	{
		for(b=0;b<ARModels.length;b++)
		{
			if (ARMakes[a][0]==ARModels[b][1])
			{
				index=0;
				if (NewUsedFlag=="N" && ARModels[b][3]=="N")
				{
				index=FMake.options.length;
				FMake.options[index]=new Option(ARMakes[a][1],ARMakes[a][1]);
				if (ARMakes[a][1]==prevMake)  {
				FMake.selectedIndex=index;
				}
				break;
				}
				if (NewUsedFlag=="U")
				{
				index=FMake.options.length;
				FMake.options[index]=new Option(ARMakes[a][1],ARMakes[a][1]);
				if (ARMakes[a][1]==prevMake) 
				{
				FMake.selectedIndex=index;
				}
				break;
				}
			}
		}
	}

FillModels(FMake,FModel);

}
function PreserveModelsSelection(FModel)
{
	var selArr=new Array()
	var b=0;
	for(a=0;a<FModel.options.length;a++)
	{
		if(FModel.options[a].selected)
		{
			selArr[b]=FModel.options[a].value;
			b++;
		}
	}
	return selArr;
}
function CheckIn(val,Arr)
{
	var bool=false;
	for(var a=0;a<Arr.length;a++)
	{
		if(Arr[a]==val)
		{
			bool=true;
			break;
		}
	}
	return bool;
}
// function to be called whenever FillMakes is called and whenever a value is changed in makes combo
function FillModels(FMake,FModel)
{
// what is the current selected item in makes combo
var selectedMake=FMake.value;
var selectedModel=FModel.value;
var PreservedModels=PreserveModelsSelection(FModel);
// empties the models combo
FModel.options.length=0;
// default selected index set to -1 (not found)
var MakeIndex=-1;
var flag=false;

	// finding the index for the selected make
	for(var a=0;a<ARMakes.length;a++)
	{
		if (ARMakes[a][1]==selectedMake) MakeIndex=a+1;
	}
	// adding default item into models combo
	index=FModel.options.length;
	var currentURL=window.location.href.toUpperCase();
	if(currentURL.indexOf("WEBQUOTEREQUEST.ASPX")!=-1  || currentURL.indexOf("WEBREBATESINCENTIVES.ASPX")!=-1)
	FModel.options[index]=new Option("Select Model","");
	else if(currentURL.indexOf("WEBVEHICLELIST.ASPX")!=-1)
	FModel.options[FModel.options.length]=new Option("All Models","");
	else if(currentURL.indexOf("WEBADVSEARCH_ES.ASPX")!=-1)
	FModel.options[FModel.options.length]=new Option("Cualquier Modelo","''");	
	else	
	{
		if(FModel.multiple)		
		FModel.options[index]=new Option("Any Model","''");
		else
		FModel.options[index]=new Option("Any Model","");
	}
	// adding items depending on the value of global variable of NewUsedFlag
	for(var a=0;a<ARModels.length;a++)
	{
		if(ARModels[a][1]==MakeIndex)
		{
			index=0;
			if (NewUsedFlag=="N")
			{
				index=FModel.options.length;
				if (ARModels[a][3]=="N")
				{
					if(FModel.multiple)	
					FModel.options[index]=new Option(ARModels[a][2],"'"+ARModels[a][2]+"'");
					else
					FModel.options[index]=new Option(ARModels[a][2],ARModels[a][2]);
					
					FModel.options[index].selected=CheckIn(FModel.options[index].value,PreservedModels);
				}
			}
			if (NewUsedFlag=="U")
			{
				index=FModel.options.length;
					if(FModel.multiple)	
					FModel.options[index]=new Option(ARModels[a][2],"'"+ARModels[a][2]+"'");
					else
					FModel.options[index]=new Option(ARModels[a][2],ARModels[a][2]);
					
					FModel.options[index].selected=CheckIn(FModel.options[index].value,PreservedModels);
			}
		}
	}	
	if(CheckIn("''",PreservedModels) || CheckIn("",PreservedModels))FModel.options[0].selected=true;	
}
