var CurrentPageName;
var ParentStack;
var LastSiblingStack;
var RootMenuItem;
var SelectedItem;
var MenuString;

function BeginLeftMenu (TheCurrentPageName)
{
	var DummyRootItem;
	
	CurrentPageName = TheCurrentPageName;
	ParentStack = new Array();
	LastSiblingStack = new Array();
	
	RootMenuItem = new LeftMenuItem(null, 0, 0, '', '', '');
	ParentStack[0] = RootMenuItem;
}

function LeftMenuItem(Parent, MenuID, MenuLevel, MenuName, PageName, URL, target_location)
{
	
	this.Parent = Parent;
	this.FirstChild = null;
	this.NextSibling = null;
	this.MenuID = MenuID;
	this.MenuLevel = MenuLevel;
	this.MenuName = MenuName;
	this.PageName = PageName;
	this.URL = URL;
	this.Visible = false;
	this.Selected = false;
	this.target_location = target_location;
}

function RegisterLeftMenuItem (ParentID, MenuID, MenuLevel, MenuName, PageName, URL, target_location)
{
	var MenuItem;
	var ParentItem;
	var SiblingItem;
	
	// Set the parent item 
	ParentItem = ParentStack[MenuLevel - 1];
	
	// Create the new menu item
	MenuItem = new LeftMenuItem(ParentItem, MenuID, MenuLevel, MenuName, PageName, URL, target_location);
	
	// Get the previous sibling 
	SiblingItem = LastSiblingStack[MenuLevel];
	
	if (SiblingItem == null) {
		// If there was no previous sibling, then this 
		// menu item is the first child of the parent
		ParentItem.FirstChild = MenuItem;
	} else {
		// Set the next sibling of the previous sibling to the new menu item
		SiblingItem.NextSibling = MenuItem;
	}
	
	// Set the new menu item to the last sibling and reset the next level
	LastSiblingStack[MenuLevel] = MenuItem;
	LastSiblingStack[MenuLevel+1] = null;
	
	// Set the new menu item to the current parent for the current level
	ParentStack [MenuLevel] = MenuItem;

	// If the item is selected, then record the selection
	if (PageName == CurrentPageName) {
		SelectedItem = MenuItem;
	}
}

function OpenMenuStructure(MenuItem)
{
	var CurrentItem;
	
	if (MenuItem.Parent != null)
	{
		OpenMenuStructure (MenuItem.Parent);
	}
	
	// mark the current item as selected
	MenuItem.Selected = true;
	
	// Open all of the child items below this item
	CurrentItem = MenuItem.FirstChild;
	
	while (CurrentItem != null)
	{
		CurrentItem.Visible = true;
		CurrentItem = CurrentItem.NextSibling;
	}
}

function DisplayMenu (MenuItem)
{
	var CurrentItem;
	var MenuLevel;
	
	MenuLevel = MenuItem.MenuLevel;
	if (MenuLevel == 1) {
		MenuString = MenuString + "<tr><td class=\"lftNavLev1tbl\"><a class=\"lftNavHead\" href=\"" + MenuItem.URL + "\" target=\""+ MenuItem.target_location +"\">" + MenuItem.MenuName + "</a></td></tr>";
		MenuString = MenuString + "<tr><td class=\"lftNavLev1Space\"><img src=\"/images/footer/de_clear.gif\" width=\"124\" height=\"1\" border=\"0\" alt=\"\"></td></tr>";
	} else if (MenuItem.Selected == true) {
		MenuString = MenuString + "<tr><td class=\"lftNavLev" + MenuLevel + "tbl\"><a class=\"lftNavLev" + MenuLevel + "on\" href=\"" + MenuItem.URL + "\" target=\""+ MenuItem.target_location +"\">" + MenuItem.MenuName + "</a></td></tr>";
	} else if (MenuItem.Visible == true) {
		if (MenuLevel == 2 && SelectedItem.MenuLevel == 1)
		{
			// Second level menu items should be dark if a root level item is selected
			MenuString = MenuString + "<tr><td class=\"lftNavLev" + MenuLevel + "tbl\"><a class=\"lftNavLev" + MenuLevel + "dark\" href=\"" + MenuItem.URL + "\" target=\""+ MenuItem.target_location +"\">" + MenuItem.MenuName + "</a></td></tr>";
		} else {
			MenuString = MenuString + "<tr><td class=\"lftNavLev" + MenuLevel + "tbl\"><a class=\"lftNavLev" + MenuLevel + "off\" href=\"" + MenuItem.URL + "\" target=\""+ MenuItem.target_location +"\">" + MenuItem.MenuName + "</a></td></tr>";
		}
	}

	// Open all of the child items below this item
	CurrentItem = MenuItem.FirstChild;
		//alert(MenuItem.MenuName + ':' + (CurrentItem == null ? 'null' : CurrentItem.FirstChild.MenuName));
	
	while (CurrentItem != null)
	{
		//alert("Traversing:" + CurrentItem.MenuName);
		if (CurrentItem.Visible == true)
		{
			DisplayMenu(CurrentItem)
		}
		CurrentItem = CurrentItem.NextSibling;
	}
}

function EndLeftMenu ()
{
	OpenMenuStructure (SelectedItem);
	
	MenuString = "<table cellPadding=0 cellSpacing=0 border=0 width=155>";
	MenuString = MenuString + "<tr><td><img width=20 height=20 src=\"/images/footer/de_clear.gif\"></td></tr>";
	MenuString = MenuString + "<tr><td><table cellPadding=0 cellSpacing=0 border=0><tr><td><img width=10 src=\"/images/footer/de_clear.gif\"></td>";
	MenuString = MenuString + "<td><table cellPadding=0 cellSpacing=0 border=0 width=100%>";
	
	DisplayMenu (RootMenuItem.FirstChild);
	
	MenuString = MenuString + "</table></td></tr></table></td></tr></table>";
	
	document.write(MenuString);
}