// --------------------------------------------------------------------------------
// auto_Contact.js
// Travis Musika Aug 18, 2005
// Contains Javascript relevant to the AutoDirectory contact form.
// --------------------------------------------------------------------------------

var auto_invDFieldPrefix = "tmpl_autoInventoryDetails_aID_";
var auto_invASFieldPrefix = "tmpl_autoInventoryAdvancedSearch_aIAS_";

// page/make matrix
var auto_invModelMakes = null;


// --- Attach JS to document elements and execute simple statements. ---
ow_f_AppendLoadEvent(
    function() {

		// set up the page/make matrix for the advanced search control
		var auto_lstMake = document.getElementById(auto_invASFieldPrefix + "ow_lstMake");
		var auto_lstModel = document.getElementById(auto_invASFieldPrefix + "ow_lstModel");
		if (auto_lstMake != null && auto_lstModel != null) {
			
			if (document.getElementById(auto_invASFieldPrefix + "ow_xModelMakes") != null) {

				ow_f_AddEvent(auto_lstMake, "change", auto_invSelectMake, false);
				
				var invMakes = document.getElementById(auto_invASFieldPrefix + "ow_xModelMakes").value.split(";");
				auto_invModelMakes = new Array(auto_lstMake.options.length);
				
				var makeIdx;
				for (i = 0; i < invMakes.length; i++) {
					makeIdx = parseInt(invMakes[i]);
					if (auto_invModelMakes[makeIdx] == null)
						auto_invModelMakes[makeIdx] = new Array();
					if (auto_lstModel.options[i] != null)
						auto_invModelMakes[makeIdx].push(auto_lstModel.options[i]);
				}
				
				// do the initial call for the listbox
				auto_invSelectMake(null);
			}

		}

		// add the rollovers
		var auto_img = document.getElementById(auto_invDFieldPrefix + "auto_img");
		if (auto_img != null) {
			var auto_thumb;
			var auto_invImages = new Array();

			for (var i=1;i<6;i++) {
				auto_thumb = document.getElementById(auto_invDFieldPrefix + "auto_thumb" + i);
				if (auto_thumb != null && auto_thumb.getAttribute("imageSrc") != null) {
					ow_f_AddEvent(auto_thumb, "mouseover", auto_invThumbRollover, false);
					
					// preload rollover images
					if (document.images) {
						auto_invImages[i] = new Image(auto_thumb.getAttribute("width"),auto_thumb.getAttribute("height"));
						auto_invImages[i].src = auto_thumb.getAttribute("imageSrc");
					}
				}
			}
		}

    }
);

// --------------------------------------------------------------------------------
// auto_invSelectMake()
// Changes the options in the select lists depending on the current make of the model.
// --------------------------------------------------------------------------------
// Arguments:
//	- none
// Returns:
//	- none
// --------------------------------------------------------------------------------
function auto_invSelectMake(e) {
	var ow_lstMake = document.getElementById(auto_invASFieldPrefix + "ow_lstMake");
	if (ow_lstMake != null) {
		var makeIdx = ow_lstMake.selectedIndex;
		
		if (auto_invModelMakes != null) {
			var ow_lstModel = document.getElementById(auto_invASFieldPrefix + "ow_lstModel");
			var currModel = "";
			if (ow_lstModel.selectedIndex > -1)
				currModel = ow_lstModel.options[ow_lstModel.selectedIndex].value;
			ow_lstModel.options.length = 0;
			if (makeIdx != 0)
				ow_lstModel.options.add(auto_invModelMakes[0][0]);
				
			if (auto_invModelMakes[makeIdx] != null)
				for (i = 0; i<auto_invModelMakes[makeIdx].length;i++) {
					var option = auto_invModelMakes[makeIdx][i];
					if (option != null) {
						if (option.value == currModel)
							option.selected = true;
						ow_lstModel.options.add(option);
					}
				}
		}
	}
}

// --------------------------------------------------------------------------------
// auto_invThumbRollover()
// Rolls over an image
// --------------------------------------------------------------------------------
// Arguments:
//	- none
// Returns:
//	- none
// --------------------------------------------------------------------------------
function auto_invThumbRollover(e) {
	if (!e) var e = window.event;
	var tg = (window.event) ? e.srcElement : e.target;
	if (tg.getAttribute("imageSrc") != null) {
		var auto_img = document.getElementById(auto_invDFieldPrefix + "auto_img");
		if (auto_img != null)
			auto_img.src = tg.getAttribute("imageSrc");
	}
	
}
