// Copyright ©2007 HeliProz, Inc. and its affiliates. All rights reserved.

var menu;

// Initializes the drop-down lists and populates the manufacture list with all available options
// from the list file, using the first item listed in each line of the file.
function menus(list)
{
	if(document.all)
	{
		menu = document.all("form");
	}
	else if(document.getElementById)
	{
		menu = document.getElementById("form");
	}
	else if(document.layers)
	{
		menu = document.layers["form"];
	}

	var manufactureArray = new Array();
	var i = 0;
	for(j = 0; j < list.length; j++)
	{
		var item = list[j];
		item = item.split("/");
		duplicate = false;
		k = 0;
		while(duplicate == false && k < manufactureArray.length)
		{
			if(manufactureArray[k] == item[0])
			{
				duplicate = true;
			}
			k++;
		}
		if(!duplicate)
		{
			manufactureArray[i] = item[0];
			i++;
		}
	}
	
	menu.manufacture.options.length = manufactureArray.length+1;
	for(i = 1; i <= manufactureArray.length; i++)
	{
		menu.manufacture.options[i] = new Option(manufactureArray[i-1],manufactureArray[i-1]);
	}
	menu.make.style.display = "none";
	menu.model.style.display = "none";
	menu.part.style.display = "none";
}

// Populates and makes visible the Make drop-down menu with the second item from each line in the file,
// as long as the first item in the line matches the selection made in the manufacture menu.
function updateMake(list)
{
	manufactureTemp = menu.manufacture.options[menu.manufacture.selectedIndex].text;
	var makeArray = new Array();
	var i = 0;
	for(j = 0; j < list.length; j++)
	{
		var item = list[j];
		item = item.split("/");
		if(item[0] == manufactureTemp)
		{
			duplicate = false;
			k = 0;
			while(duplicate == false && k < makeArray.length)
			{
				if(makeArray[k] == item[1])
				{
					duplicate = true;
				}
				k++;
			}
			if(!duplicate)
			{
				makeArray[i] = item[1];
				i++;
			}
		}
	}
	
	menu.make.options.length = makeArray.length+1;
	menu.make.style.display = "none";
	menu.model.style.display = "none";
	menu.part.style.display = "none";
	for(i = 1; i <= makeArray.length; i++)
	{
		menu.make.options[i] = new Option(makeArray[i-1],makeArray[i-1]);
	}
	if(makeArray.length > 0)
	{
		menu.make.style.display = "inline";
	}
}

// Populates and makes visible the Model drop-down menu with the third item from each line in the file,
// as long as the first and second items in the line matches the selections made in the manufacture and make menus.

// If the lines that match the first two items only have 3 items total, populate the Part drop-down menu and make it
// visible instead.
function updateModel(list)
{
	manufactureTemp = menu.manufacture.options[menu.manufacture.selectedIndex].text;
	makeTemp = menu.make.options[menu.make.selectedIndex].text;
	var modelArray = new Array();
	var i = 0;
	var tier4 = true;
	for(j = 0; j < list.length; j++)
	{
		var item = list[j];
		item = item.split("/");
		if(item[0] == manufactureTemp && item[1] == makeTemp)
		{
			if(item.length == 3)
			{
				tier4 = false;
			}
			duplicate = false;
			k = 0;
			while(duplicate == false && k < modelArray.length)
			{
				if(modelArray[k] == item[2])
				{
					duplicate = true;
				}
				k++;
			}
			if(!duplicate)
			{
				modelArray[i] = item[2];
				i++;
			}
		}
	}
	menu.model.options.length = modelArray.length+1;
	menu.model.style.display = "none";
	menu.part.style.display = "none";
	//Check if the currently selected Make does not have a model
	if(tier4)
	{
		for(i = 1; i <= modelArray.length; i++)
		{
			menu.model.options[i] = new Option(modelArray[i-1],modelArray[i-1]);
		}
		if(modelArray.length > 0)
		{
			menu.model.style.display = "inline";
		}
	}
	else
	{
		menu.part.options.length = modelArray.length+1;
		for(i = 1; i <= modelArray.length; i++)
		{
			partSplit = modelArray[i-1];
			partSplit = partSplit.split("^");
			menu.part.options[i] = new Option(partSplit[0],partSplit[1]);
		}
		if(modelArray.length > 0)
		{
			menu.part.style.display = "inline";
		}
	}
}

// Populates and makes visible the Part drop-down menu with the fourth item from each line in the file,
// as long as the first, second, and third items in the line matches the selections made in the manufacture,
// make, and model menus.
function updatePart(list)
{
	manufactureTemp = menu.manufacture.options[menu.manufacture.selectedIndex].text;
	makeTemp = menu.make.options[menu.make.selectedIndex].text;
	modelTemp = menu.model.options[menu.model.selectedIndex].text;
	var partArray = new Array();
	var i = 0;
	for(j = 0; j < list.length; j++)
	{
		var item = list[j];
		item = item.split("/");
		if(item[0] == manufactureTemp && item[1] == makeTemp && item[2] == modelTemp)
		{
			duplicate = false;
			k = 0;
			while(duplicate == false && k < partArray.length)
			{
				if(partArray[k] == item[3])
				{
					duplicate = true;
				}
				k++;
			}
			if(!duplicate)
			{
				partArray[i] = item[3];
				i++;
			}
		}
	}
	
	menu.part.options.length = partArray.length+1;
	menu.part.style.display = "none";
	for(i = 1; i <= partArray.length; i++)
	{
		partSplit = partArray[i-1];
		partSplit = partSplit.split("^");
		menu.part.options[i] = new Option(partSplit[0],partSplit[1]);
	}
	if(partArray.length > 0)
	{
		menu.part.style.display = "inline";
	}
}

// Open the parts finder page with the GET data of the PLM file it should open, based on the selection in the Part
// drop-down menu. This version opens the finder in a new window named "finder".
function openFinder()
{
	open("finder/finder.asp?fname="+menu.part.options[menu.part.selectedIndex].value, "finder", "toolbar=no,menubar=no,resizable=yes,scrollbars=no,location=no");
}

// Open the parts finder page with the GET data of the PLM file it should open, based on the selection in the Part
// drop-down menu. This version opens the finder in the same window.
function openFinderNF()
{
	open("finder/findernf.asp?fname="+menu.part.options[menu.part.selectedIndex].value);
}