// JavaScript Document
function InitializeMainCategory(currentForm) {
	for (var i=0; i < MainCategory.length; i++) {
		eval(currentForm + ".MainCategory.options[i] = new Option('" + MainCategory[i].Name + "')");
		eval(currentForm + ".MainCategory.options[i].value = " + MainCategory[i].ID);
	}
}

function PopulatePane(currentPane, paneToPopulate) {
	var selectedArray, i;
	var selected = currentPane.options[currentPane.selectedIndex].value
	if (selected == 0) {
		// yeah, we should be able to just do the select set and index decrement no matter what,
		// and then check for selected being undefined, but browser bugs make life more "fun" than that
		if (currentPane.selectedIndex != 0)
			selected = currentPane.options[--currentPane.selectedIndex].value;
		else {
			currentPane.selectedIndex = -1;
			return false;
		}
	}
	
//	 try {
//		eval(paneToPopulate + selected);
//	} catch(e) {
//		alert("There is no " + paneToPopulate + " associated with this " + currentPane.name);
//		return;
//	}
		
	selectedArray = eval(paneToPopulate + selected);
	paneToPopulate = eval("currentPane.form." + paneToPopulate)

	// if we need to clear panes, we'll start clearing out options with the last pane and work back until we run out
	for (i = currentPane.form.elements.length - 1; i > 0; i--) {
		paneToClear = paneToPopulate.form.elements[i]
		if (paneToClear == paneToPopulate) {
			break;
		}
		// make sure we're only doing the clearing operation on panes we care about
		if (paneToClear.name == "OperatingSystem" || paneToClear.name == "SubCategory" || paneToClear.name == "MainCategory") {
			for (i = paneToClear.options.length; i != 0; i--) {
				paneToClear.options[i - 1] = null;
			}
		}
	}
	while (selectedArray.length < paneToPopulate.options.length) {
		paneToPopulate.options[(paneToPopulate.options.length - 1)] = null;
	}
	for (i = 0; i < selectedArray.length; i++) {
//		alert("paneToPopulate.options[i] = new Option('" + selectedArray[i].Name + "')");
		if ( selectedArray[i] == null || selectedArray[i] == "" ) {
			alert("There is no " + paneToPopulate.Name + " associated with this " + currentPane.Name);
		} else {
			if(currentPane.name != 'SubCategory'){
				eval("paneToPopulate.options[i] = new Option('" + selectedArray[i].Name + "')");
				eval("paneToPopulate.options[i].value = '" + selectedArray[i].ID + "'");
			} else {
				eval("paneToPopulate.options[i] = new Option('" + selectedArray[i].Name + "')");
				eval("paneToPopulate.options[i].value = '" + selectedArray[i].URL + "'");
			}
		}
	}
	if (document.welcome.OperatingSystem.options[0] == null) {
		document.welcome.OperatingSystem.options[0] = new Option("[Paso 3]                             ");
		document.welcome.OperatingSystem.options[0].value = 0;
	}
}

function changePage(form) {
	/*
		deal with nasty sporadic back button bug in Netscape 4, where user can have only the last pane selected.
		changing checkSelection() is a possibility, but if the user selects the third pane, and the other two are blank,
		he should really still get the driver he asked for.
		therefore, we need to extract the value of the second pane from the third
	*/
	if (form.OperatingSystem.selectedIndex != -1) {
		PaneValues = form.OperatingSystem.options[form.OperatingSystem.selectedIndex].value.split("_");
		SubCategoryValue = PaneValues[0] + "_" + PaneValues[1]
		DriverValue = SubCategoryValue + "_" + PaneValues[2];
		whichArray = eval("OperatingSystem" + PaneValues[0] + "_" + PaneValues[1]);
		for (i = 0; i < whichArray.length; i++) {
			if (whichArray[i].ID == DriverValue) {
				//alert(whichArray[i].URL);
				//form.action = "javascript:document.location.href = 'index.php?page=suministros&modelo=" + whichArray[i].URL + "';";
				form.action = "javascript:ajax_loadContent('contentDivContainer','suministros.php');";
				break;
			}
		}
		return true;
	} else {
		alert("Por Favor seleccione un modelo en la 2º columna.");
		return false;
	}
}