
/* ============================================================================
*  Copyright: 2008 Montway Auto Transport, Inc.
*  Date: 2008-09-12-17:46
*
*  This file is to activate the make/model selection lists of pages. The
*  selection list elements are expected to be passed in as arguments for the
*  methods to avoid being tied to any one implementation.
*
*  The allMakeArray, makeArray, and modelArray variables store all the
*  possible makes and models that are available to the system. These are most
*  likely generated values with backend PHP/JSP/ASP/CGI tied to a flat file
*  or other form of database.
*  ========================================================================= */
// Boolean to determine whether a selection element has a "select all" option
var defaultAbleToSelectAll = false;

// DEPRECATED: Used for thread management. May not really need, anymore.
var makingBrowseChanges = false;

/* ============================================================================
*  Called after setting up the make and model selection, to select particular
*  values for make and model. Useful to reflect preset or existing values.
*  ========================================================================= */
function selectMakeModel(formName, elementMake, inMake, elementModel, inModel)
{
	var make_options = eval("document." + formName + "." + elementMake + ".options");
	var model_options = eval("document." + formName + "." + elementModel + ".options");

	for (var j = 0; j < make_options.length; j++)
	{
		if (make_options[j].text == inMake)
		{
			make_options[j].selected = true;
			changeModel('Model', formName, elementMake, elementModel);
			break;
		}
	}

	for (var k = 0; k < model_options.length; k++)
	{
		if (model_options[k].text == inModel)
		{
			model_options[k].selected = true;
			break;
		}
	}
}

/* ============================================================================
*  Called from HTML form with onChange(), whenever an automobile make selection
*  pull-down option is changed. Usually to fill in the model selection
*  pull-down options with the appropriate models, based on make.
*  ========================================================================= */
function changeModel(selectText, formName, makeSelectName, modelSelectName, canSelectAll)
{
	var selectObject = eval("document." + formName + "." + makeSelectName);

	if (selectObject.options[selectObject.selectedIndex].value == "")
	{
		resetSelection(selectText, formName, modelSelectName);
		return;
	}

	fillSelection(selectText, formName, modelSelectName, selectObject.options[selectObject.selectedIndex].value, canSelectAll);
}


/* ============================================================================
*  Called by changeModel() to reset the model options back to default, or
*  zeroed values.
*  ========================================================================= */
function resetSelection(selectText, formName, selectName)
{
	var selectObject = eval("document." + formName + "." + selectName);
	selectObject.options[0] = new Option();
	// selectObject.length = 1;	// Another way to get a new option;
	selectObject.options[0].value = "";

	if (selectText == null || selectText == '' || selectText == undefined)
	{
		selectObject.options[0].text = "Select One";
	}
	else
	{
		selectObject.options[0].text = "Select " + selectText;
	}

	selectObject.selectedIndex = 0;
}


/* ============================================================================
*  Called by changeModel() and from the HTML onLoad() to fill an HTML form's
*  selection lists, based on application. It will use the first parameter,
*  selectText, as a switch to determine whether to fill the make pull-down
*  selection list with the available makes, or to fill the model pull-down list
*  with the appropriate values, based on make. The values are taken from
*  different array vars defined dynamically by PHP, via file or database.
*  ========================================================================= */
function fillSelection(selectText, formName, targetSelectName, sourceSelectIndex, canSelectAll)
{
	//alert("Inside fillSelection: selectText[" + selectText + "] formName[" + formName + "] targetSelectName[" + targetSelectName + "]");

	// Make sure incoming variables are properly populated
	if (selectText == null || selectText == '' || selectText == undefined)
	{
		return;
	}

	if (canSelectAll == null)
	{
		canSelectAll = defaultAbleToSelectAll;
	}

	// Boolean to indicate that we are starting to change things
	makingBrowseChanges = true;

	// First clear out the old array
	resetSelection(selectText, formName, targetSelectName);

	// We'll be using this as a reference to the selection element to manipulate
	var selectObject = eval("document." + formName + "." + targetSelectName);

	// Add a select-all options, if specified
	var counter = 1;
	if (canSelectAll)
	{
		selectObject.options[1] = new Option();
		selectObject.options[1].value = "all";
		selectObject.options[1].text = "All " + selectText + "s";
		counter++;
	}

	// Populate the selection options!
	if (selectText == "Make")
	{
		if (makeArray)
		{
			for (var i = 0; i < makeArray.length; i++)
			{
				selectObject.options[counter] = new Option();
				selectObject.options[counter].text = makeArray[i];
				selectObject.options[counter].value = makeArray[i];
				counter++;
			}
		}
	}
	else if (selectText == "Model")
	{
		for(var j = 0; j < modelArray.length; j++)
		{
			if (modelArray[j][1] == sourceSelectIndex)
			{
				selectObject.options[counter] = new Option();
				selectObject.options[counter].text = modelArray[j][0];
				selectObject.options[counter].value = modelArray[j][0];
				counter++;
			}
		}

		if (makeArray) selectObject.length = counter;	// What does this do?!
	}


	// Boolean to indicate that we are finished changing things
	makingBrowseChanges = false;
}
