// --------------------------------------------------------------------------------
// auto_Application.js
// Travis Musika July 26, 2005
// Contains Javascript relevant to the AutoDirectory application pages.
// --------------------------------------------------------------------------------

var auto_catFieldPrefix = "tmpl_autoCatalogueSearch_aCS_";

/* page/make matrix
function auto_catModel(name, value, make) {
	this.name = name;
	this.value = value;
	this.make = make;
	this.hasMake = function(make) { if (this.make==0 || this.make == make) return true; return false;};
} */
var auto_catModelMakes = null;

// --- Attach JS to document elements and execute simple statements. ---
ow_f_AppendLoadEvent(
function() {

	// set up the page/make matrix
	var auto_lstMake = document.getElementById(auto_catFieldPrefix + "ow_lstMake");
	var auto_lstModel = document.getElementById(auto_catFieldPrefix + "ow_lstModel");
	if (auto_lstMake != null && auto_lstModel != null) {
		
		if (document.getElementById(auto_catFieldPrefix + "ow_xModelMakes") != null) {
		
			ow_f_AddEvent(auto_lstMake, "change", auto_catSelectMake, false);
			
			var catMakes = document.getElementById(auto_catFieldPrefix + "ow_xModelMakes").value.split(";");
			auto_catModelMakes = new Array(auto_lstMake.options.length);

			var makeId;
			for (i = 0; i < auto_lstModel.options.length; i++) {
				makeId = parseInt(catMakes[i]);
				if (auto_catModelMakes[makeId] == null)
					auto_catModelMakes[makeId] = new Array();
				if (auto_lstModel.options[i] != null)
					auto_catModelMakes[makeId].push(auto_lstModel.options[i]);
			}

			/*			
			for (i = 0; i < catMakes.length; i++) {
				
				auto_catModelMakes[i] = new auto_catModel(options[i].text, options[i].value, makes[i]);
			}
			*/

		}

		// do the initial call for the listbox
		if (auto_lstMake.options.length > 2)
			auto_catSelectMake(null);
		else {
			// select the first (and only) model if there is one
			auto_lstMake.selectedIndex = 1;
			auto_catSelectMake(null);
		}	
	}
}
);

// --------------------------------------------------------------------------------
// auto_catSelectMake()
// Changes the options in the select lists depending on the current make of the model.
// --------------------------------------------------------------------------------
// Arguments:
//	- none
// Returns:
//	- none
// --------------------------------------------------------------------------------
function auto_catSelectMake(e) {
	var auto_lstMake = document.getElementById(auto_catFieldPrefix + "ow_lstMake");
	if (auto_lstMake != null) {
		//var make = auto_lstMake.options[auto_lstMake.selectedIndex].value;
		var makeIdx = auto_lstMake.selectedIndex;
		
		if (auto_catModelMakes != null) {
			var auto_lstModel = document.getElementById(auto_catFieldPrefix + "ow_lstModel");
			var currModel = "";
			if (auto_lstModel.selectedIndex > -1)
				currModel = auto_lstModel.options[auto_lstModel.selectedIndex].value;
			auto_lstModel.options.length = 0;
			if (makeIdx != 0)
				auto_lstModel.options.add(auto_catModelMakes[0][0]);
				
			if (auto_catModelMakes[makeIdx] != null)
				for (i = 0; i<auto_catModelMakes[makeIdx].length;i++) {
					var option = auto_catModelMakes[makeIdx][i];
					if (option != null) {
						if (option.value == currModel)
							option.selected = true;
						auto_lstModel.options.add(option);
					}
				}
		}
	}
}
