
function Product () {
	this.NAME = null;
	this.Family = null;
}

function Family() {
	this.NAME = null;
	this.OID = -1;
	this.Model = null;
}

function Model() {
	this.NAME = null;
	this.OID = -1;
	this.Sku = null;
}

function Sku() {
	this.NAME = null;
	this.OID = -1;
}

function modelSelectorChange(cm, psDisabledText) {
	var selectedProductIndex = productMenu[0].selectedIndex;
	var selectedProduct = "";
	var selectedProductIndex = -1;
	var selectedFamily = "";
	var selectedFamilyIndex = -1;
	var selectedModel = "";
	var selectedModelIndex = -1;
	var selectedConfig = "";
	var selectedConfigIndex = -1;
	var numChildren;
	var CatId;
	var CatName;
	var i;

	if (selectedProductIndex >= 0)
		selectedProduct = productMenu[0].options[selectedProductIndex].value;

	// make sure user didn't click on an empty box
	if ((cm != -1) && (productMenu[cm].length < 1))
	  	return;

	// get the chosen Product/Family from this ProductMenu
	if (cm == -1)
	  	selectedProduct = 0;
	else
	{
	  	if (productMenu[cm].selectedIndex == -1)
	    	return;
		selectedProductIndex = productMenu[0].selectedIndex;
		if (selectedProductIndex > -1) {
	  		selectedProduct = productMenu[0].options[selectedProductIndex].value;

			selectedFamilyIndex = productMenu[1].selectedIndex;
			if (selectedFamilyIndex > -1)
				selectedFamily = productMenu[1].options[selectedFamilyIndex].value;

			selectedModelIndex = productMenu[2].selectedIndex;
			if (selectedModelIndex > -1)
				selectedModel = productMenu[2].options[selectedModelIndex].value;
		} else {
			return;
		}
	}
//alert('cm='+cm);
	// fill up the next menu based on the selectedProduct
	if (cm == 0) {
		//a Product is selected, construct Family menu

		var familyCount = productArray[selectedProductIndex].Family.length;
		for (i = 0; i < familyCount; i++) {
			var familyName = productArray[selectedProductIndex].Family[i].NAME;
			var familyOid = productArray[selectedProductIndex].Family[i].OID;
			productMenu[cm+1].options[i] = new Option(familyName, familyOid);
		}
		productMenu[cm+1].length = familyCount;
		
		//If selected product is 'projectors' then hide the SKU selectbox.
        if(selectedProductIndex>0) { //Projectors
			if(document.getElementById("tdModelTitle")!=null) {
				document.getElementById("tdModelTitle").innerText="Choose SKU";
			}
			if(document.getElementById("tdSkuTitle")!=null) {
				document.getElementById("tdSkuTitle").style.visibility="hidden";
				document.getElementById("tdSkuTitle").style.display="none";
			}
			if(document.getElementById("tdSkuImg")!=null) {
				document.getElementById("tdSkuImg").style.visibility="hidden";
				document.getElementById("tdSkuImg").style.display="none";
			}
			if(document.getElementById("tdSkuSelect")!=null) {
				document.getElementById("tdSkuSelect").style.visibility="hidden";
				document.getElementById("tdSkuSelect").style.display="none";
			}
			document.modelselector.islaptop.value = 'false';
		} else { //Laptops
			if(document.getElementById("tdModelTitle")!=null) {
				document.getElementById("tdModelTitle").innerText="Choose Model";
			}
			if(document.getElementById("tdSkuImg")!=null) {
				
				document.getElementById("tdSkuImg").style.visibility="visible";
				document.getElementById("tdSkuImg").style.display="";
			}
			if(document.getElementById("tdSkuTitle")!=null) {
				document.getElementById("tdSkuTitle").style.visibility="visible";
				document.getElementById("tdSkuTitle").style.display="";
			}
			if(document.getElementById("tdSkuSelect")!=null) {
				document.getElementById("tdSkuSelect").style.visibility="visible";
				document.getElementById("tdSkuSelect").style.display="";
			}
			document.modelselector.islaptop.value = 'true';
		}
	} else if (cm == 1) {
		//a Family is selected, construct Model menu
		if (selectedFamilyIndex == 0 && selectedFamily == "") {
			productMenu[cm].selectedIndex = -1;
			return;
		}
		var modelCount = productArray[selectedProductIndex].Family[selectedFamilyIndex].Model.length;
		var modelName;
		var modelOID;
		for (i = 0; i < modelCount; i++) {
			modelName = productArray[selectedProductIndex].Family[selectedFamilyIndex].Model[i].NAME;
			modelOID = productArray[selectedProductIndex].Family[selectedFamilyIndex].Model[i].OID;
			productMenu[cm+1].options[i] = new Option(modelName, modelOID);
		}
		productMenu[cm+1].length = modelCount;
	} else if (cm == 2) {
		//a Model is selected, construct SKU menu
		if (selectedModelIndex == 0 && selectedModel == "") {
			productMenu[cm].selectedIndex = -1;
			return;
		}
		var configCount = productArray[selectedProductIndex].Family[selectedFamilyIndex].Model[selectedModelIndex].Sku.length;
		for (i = 0; i < configCount; i++) {
			var configName = productArray[selectedProductIndex].Family[selectedFamilyIndex].Model[selectedModelIndex].Sku[i].NAME;
			var configOID = productArray[selectedProductIndex].Family[selectedFamilyIndex].Model[selectedModelIndex].Sku[i].OID;
			productMenu[cm+1].options[i] = new Option(configName, configOID);
		}
		productMenu[cm+1].length = configCount;
	} else if (cm == 3) {
		//a Sku is selected
		selectedConfigIndex = productMenu[cm].selectedIndex;
		selectedConfig = productMenu[cm].options[selectedConfigIndex].value;
//alert('selectedConfig ='+selectedConfig);
		if (selectedConfigIndex == 0 && selectedConfig == "") {
			productMenu[cm].selectedIndex = -1;
			return;
		}
	}

	//clear out all menus to the right of this menu
	for (i = cm+2; i < productMenu.length; i++) {
	   	productMenu[i].length = 0;
		productMenu[i].options[0] = new Option(psDisabledText);
	}
}

function check_modelselector() {
	if (productMenu[0].selectedIndex < 0) {
		alert('You must select a product from the list before a find can be initiated for downloads');
		return false;
	//} else if (productMenu[1].selectedIndex < 0) {
		//alert('You must select a family from the list before a find can be initiated for downloads');
		//return false;
	} else {
		if(productMenu[3].selectedIndex >= 0) {
			document.modelselector.modeltext.value = productMenu[3].options[productMenu[3].selectedIndex].text;
			document.modelselector.modelvalue.value = productMenu[3].options[productMenu[3].selectedIndex].value;
			if(productMenu[3].options[productMenu[3].selectedIndex].text == '<--Select First') {
				alert('You must select a model from the list before a find can be initiated for downloads');
				return false;
			}
			document.modelselector.searchby.value = '3'; // Search by SKU selected
		} else if(productMenu[2].selectedIndex >= 0) {
			document.modelselector.modeltext.value = productMenu[2].options[productMenu[2].selectedIndex].text;
			document.modelselector.modelvalue.value = productMenu[2].options[productMenu[2].selectedIndex].value;
			document.modelselector.searchby.value = '4';  // Search by Model selected
		} else if(productMenu[1].selectedIndex >= 0) {
			document.modelselector.modeltext.value = productMenu[1].options[productMenu[1].selectedIndex].text;
			document.modelselector.modelvalue.value = productMenu[1].options[productMenu[1].selectedIndex].value;
			document.modelselector.searchby.value = '5'; // Search by Family selected
		} else if(productMenu[0].selectedIndex >= 0) {
			document.modelselector.modeltext.value = productMenu[0].options[productMenu[0].selectedIndex].text;
			document.modelselector.modelvalue.value = productMenu[0].options[productMenu[0].selectedIndex].value;
			document.modelselector.searchby.value = '6'; // Search by Product selected
		}
		
		// Unselect other menus to shorted url
		productMenu[0].selectedIndex = -1;
		productMenu[1].selectedIndex = -1;
		productMenu[2].selectedIndex = -1;
	}
}

function check_typeselector() {
	var f = document.typeselector;
	if (f.type_oid.selectedIndex < 2) {
		alert('Please select a valid resource type');
		return false;
	} else {
		// If the option's value is not a number, open it in a new window
		if (isNaN(f.type_oid.options[f.type_oid.selectedIndex].value)) {
			window.open(f.type_oid.options[f.type_oid.selectedIndex].value);
			return false;
		} else {
			f.modeltext.value = f.type_oid.options[f.type_oid.selectedIndex].text;
		}
	}
}

function check_modelsearch() {
	var f = document.modelsearch;
	if (f.modelvalue.value == null || f.modelvalue.value == "") {
		alert('Please enter a model part number to search by');
		return false;
	}
	f.modeltext.value = f.modelvalue.value
}
