/*
	Populate Cover Finder Drop Downs
	Author: Knut E Ellingsen, ACI Agri-Cover, Inc.
*/
	function populate_dropdown(change)
	{
		// set variables
		var year = document.productsearch.choose_year.value;
		var make = document.productsearch.choose_make.value;
		var model = document.productsearch.choose_model.value;
		var bed = document.productsearch.choose_bed.value;
		
		var make_dropdown = new Array();
		var model_dropdown = new Array();
		var bed_dropdown = new Array();
		var test = 0;

		var first_element = yearArray.indexOf(year.toString());		
		var last_element = yearArray.lastIndexOf(year.toString());
				
		// peform action determined by which drop-down had the change		
		switch(change)
		{
			case 1:			
				// clear the options
				ClearOptions(document.productsearch.choose_make);
				ClearOptions(document.productsearch.choose_model);
				ClearOptions(document.productsearch.choose_bed);
				// add the first option
				AddToOptionList(document.productsearch.choose_make, '', 'Choose Make:');
				AddToOptionList(document.productsearch.choose_model, '', 'Choose Model:');
				AddToOptionList(document.productsearch.choose_bed, '', 'Choose Bed:');
				
				// populate makes
				for(var i=first_element; i <= last_element; i++)
				{
					// look through all the makes
					for(j=0; j < make_dropdown.length; j++)
					{
						if(makeArray[i] == make_dropdown[j]) // check if it is allready their
						{
							test = 1;
							break;								
						} 
					}
					if(test == 0)
						make_dropdown.push(makeArray[i]); // add option
					else
						test = 0;		
				}  
				
				// sort make_dropdown array
				make_dropdown.sort();
				
				// look through all the makes
				for(j=0; j < make_dropdown.length; j++)
				{
					AddToOptionList(document.productsearch.choose_make, make_dropdown[j], make_dropdown[j]);						
				}			
				break;
			case 2:
				// clear the options
				ClearOptions(document.productsearch.choose_model);
				ClearOptions(document.productsearch.choose_bed);
				// add first option
				AddToOptionList(document.productsearch.choose_model, '', 'Choose Model:');
				AddToOptionList(document.productsearch.choose_bed, '', 'Choose Bed:');						
				
				// populate models
				for(var i=first_element; i <= last_element; i++)
				{
					if(makeArray[i] == make)
					{
						// look through all the makes
						for(j=0; j < model_dropdown.length; j++)
						{
							if(modelArray[i] == model_dropdown[j]) // check if it is allready their
							{								
								test = 1;
								break;								
							} 
						}
						if(test == 0)
							model_dropdown.push(modelArray[i]); // add option
						else
							test = 0;	
					}
				}
				
				// sort make_dropdown array
				model_dropdown.sort();				
				
				// look through all the makes
				for(j=0; j < model_dropdown.length; j++)
				{
					AddToOptionList(document.productsearch.choose_model, model_dropdown[j], model_dropdown[j]);						
				}
				
				break;
			case 3:
				// clear the options
				ClearOptions(document.productsearch.choose_bed);
				// add the first option
				AddToOptionList(document.productsearch.choose_bed, '', 'Choose Bed:');						
			
				// populate the bed size
				for(var i=first_element; i <= last_element; i++)
				{
					if(modelArray[i] == model)
					{
						// look through all the makes
						for(j=0; j < bed_dropdown.length; j++)
						{
							if(bedArray[i] == bed_dropdown[j]) // check if it is allready their
							{								
								test = 1;
								break;								
							} 
						}
						if(test == 0)
						{
							bed_dropdown.push(bedArray[i]); // add option
						}
						else
							test = 0;	
					}
				}				
				
				// sort make_dropdown array
				bed_dropdown.sort();				
				
				// look through all the makes
				for(j=0; j < bed_dropdown.length; j++)
				{
					AddToOptionList(document.productsearch.choose_bed, bed_dropdown[j], bed_dropdown[j]);						
				}
				
				break;
			default:
				// do nothing
		}
		
	}
	
/*
function clear_options(OptionList)
Created By: Knut Ellingsen 10/8/2009
Purpose: Clear the list of options in the second dropdown
*/
function ClearOptions(OptionList)
{
   	// Always clear an option list from the last entry to the first
   	for (x = OptionList.length; x >= 0; x = x - 1) 
   	{
		OptionList[x] = null;
	}
}


/*
function AddToOptionList(OptionList, OptionValue, OptionText) 
Created By: Knut Ellingsen 10/8/2009
Purpose: Add an option value to a drop-down
*/
function AddToOptionList(OptionList, OptionValue, OptionText) 
{
	// Add option to the bottom of the list
   	OptionList[OptionList.length] = new Option(OptionText, OptionValue);
}	

