// This file was copied and adapted from: 
// http://www.autotrader.co.uk/CARS/common/_searchF.js

function clearModel()
{
	var modelKeyword = $("model_keyword");
	if (modelKeyword)
	{
		if (typeof modelKeyword.selectedIndex == "number")
		{
			modelKeyword.selectedIndex = 0;
		}
		else
		{
			modelKeyword.value = '';
		}
	}
}

function loadModels()
{
	var modelField = $("model_keyword");
	var makeField = $("make_keyword");
	if (modelField && typeof modelField.selectedIndex == "number" &&
		makeField && makeField.type == "hidden")
	{
		var selectedMake = makeField.value;
		for (var i = 0; i < make_v.length; i++)
		{
			if (make_v[i] == selectedMake)
			{
				modelField.length = 0;
				var isAddedDefault = false;
				for (var j = 0; j < _model[i].length; j++)
				{
					if (_model[i][j] == "Model" || _model[i][j] == "MODEL"  ||
						_model[i][j] == "ANY" || _model[i][j] == "ANY MODEL")
					{
						if (!isAddedDefault)
						{
							modelField[j] = new Option("Any model", "");
							isAddedDefault = true;
						}
					}
					else
					{
						modelField[j] = new Option(_model[i][j], _model[i][j]);
					}
				}
				break;
			}
		}
	}
}

function loadMakes()
{
	var makeField = $("make_keyword");
	if (makeField)
	{
		makeField.length = 0;
		var isAddedDefault = false;
		var dropDownIndex = 0;
		for (var i = 0; i < make_d.length; i++)
		{
			if (make_d[i] == "Make" || make_d[i] == "MAKE" ||
				make_d[i] == "ANY MAKE" || make_d[i] == "Any make")
			{
				if (!isAddedDefault)
				{
					makeField[dropDownIndex] = new Option("Any make", "");
					isAddedDefault = true;
				}
				else
				{
					// we do this so that we can skip over the duplicate 
					// defaults 'MAKE' and 'ANY MAKE' which are hardcoded in the
					// make array JavaScript files
					dropDownIndex--;
				}
			}
			else
			{
				makeField[dropDownIndex] = new Option(make_d[i], make_v[i]);
			}
			dropDownIndex++;
		}
	}
}

function loadPlantCat()
{
	var catField = $("category_keyword");
	if (catField)
	{
		catField.length = 0;
		var isAddedDefault = false;
		var dropDownIndex = 0;
		for (var i = 0; i < plant_cat_d.length; i++)
		{
			if (plant_cat_d[i] == "Category" || plant_cat_d[i] == "CATEGORY" ||
				plant_cat_d[i] == "ANY CATEGORY" || plant_cat_d[i] == "Any Category")
			{
				if (!isAddedDefault)
				{
					catField[dropDownIndex] = new Option("Any Category", "");
					isAddedDefault = true;
				}
				else
				{
					// we do this so that we can skip over the duplicate 
					// defaults 'MAKE' and 'ANY MAKE' which are hardcoded in the
					// make array JavaScript files
					dropDownIndex--;
				}
			}
			else
			{
				catField[dropDownIndex] = new Option(plant_cat_d[i], plant_cat_v[i]);
			}
			dropDownIndex++;
		}
	}
}