// show and hide for the vertical menus and swaps the + - images

function openMenu(thisNode) {
	var targetNode = getTargetNode(thisNode);
	if (targetNode.style.display!='none') {
		targetNode.style.display='none';
		thisNode.firstChild.nodeValue='[+]';
		createCookie(thisNode.id,'none',0);
	} else {
		targetNode.style.display='block';
		thisNode.firstChild.nodeValue='[-]';
		createCookie(thisNode.id,'block',0);
	}
}

// an onload function to set the menu correctly for the JSP
function configMenu() {
	var item = document.getElementById('menu').getElementsByTagName('span');
	for (var i=0;i<item.length;i++) {
		if (item[i].id.indexOf('pm') != -1) {
			var cook = readCookie(item[i].id);
			if (cook == 'block') {
				openMenu(item[i]);
				// since it's open find the current item and format it too.
				if (item[i].id.indexOf('_')==-1) {
					// in this case it is top level
					var currItem = document.getElementById('menu').getElementsByTagName('td');
					for (var j=0;j<currItem.length;j++) {
						//look for td elements that have an id that matches the start of the open menu id
						if (currItem[j].id.indexOf(item[i].id) != -1) {
							// if we find cookie name that matches id we know it's current so write format from cookie
							var cook = readCookie(currItem[j].id);
							if (cook=='headerRow') {
								currItem[j].className="headerRow";
								currItem[j].firstChild.style.color='black';
								currItem[j].firstChild.style.fontWeight='bold';
								currItem[j].firstChild.style.fontSize='11px';
							}
						}
					}
				} else {
					// it's bottom level
					var currItem = document.getElementById('menu').getElementsByTagName('li');
					for (var j=0;j<currItem.length;j++) {
						//look for li elements that have an id that matches the start of the open menu id
						if (currItem[j].id.indexOf(item[i].id) != -1)	{
							// if it has an italic style element we know it's current so write format from cookie
							var cook = readCookie(currItem[j].id);
							if (cook.indexOf('italic')!=-1)	{
								currItem[j].firstChild.style.color='black';
								currItem[j].firstChild.style.fontWeight='italic';
								currItem[j].firstChild.style.fontSize='11px';
							}
						}
					}
				}
			}
		}
	}
}

// only for the xsl, to set the current open menus and styles so the JSP stays open and displays current menu correctly
function setMenu() {
	var item = document.getElementById('menu').getElementsByTagName('span');
	for (var i=0;i<item.length;i++)  if (item[i].id.indexOf('pm') != -1) setOpen(item[i]);
}

// finds the target node that we want to display (or not)
function getTargetNode(thisNode) {
	// top level menu open
	if (thisNode.parentNode.parentNode.nextSibling.nextSibling.firstChild.nextSibling) {
		return thisNode.parentNode.parentNode.nextSibling.nextSibling.firstChild.nextSibling;
	}
	// second level menu open
	return thisNode.nextSibling.firstChild.firstChild.firstChild;
}


// writes an OPEN cookie if the menu is open
function setOpen(thisNode) {
	var targetNode = getTargetNode(thisNode);
	if (targetNode.style.display=='block') {
		createCookie(thisNode.id,'block',0);
		// since it's open find the current item and make a cookie for it too.
		if (thisNode.id.indexOf('_')==-1) {
			// in this case it is top level
			var currItem = document.getElementById('menu').getElementsByTagName('td');
			for (var i=0; i<currItem.length; i++) {
				//look for td elements that have an id that matches the start of the open menu id
				if (currItem[i].id.indexOf(thisNode.id) != -1) {
					// if it is headerRow classname we know it's current so write cookies
					if (currItem[i].className=='headerRow') {
						createCookie(currItem[i].id,'headerRow',0);
					} else {
						createCookie(currItem[i].id,'',0);
					}
				}
			}
		} else {
			// it's bottom level
			var currItem = document.getElementById('menu').getElementByTagName('li');
			for (var i=0; i<currItem.length; i++) {
				//look for li elements that have an id that matches the start of the open menu id
				if (currItem[i].id.indexOf(thisNode.id) != -1) {
					// if it has an italic style element we know it's current so write cookies
					if (currItem[i].style.indexOf('italic')!=-1) {
						createCookie(currItem[i].id,'italic',0);
					} else {
						createCookie(currItem[i].id,'',0);
					}
				}
			}
		}
	} else {
		createCookie(thisNode.id,'none',0);
	}
}

// writes a cookie to say the node has been clicked so the JSP will know to format it
function itemCurrent(currNode) {
	// first kill em all
	killMenuCookies();
	var writeNode = currNode.parentNode;
	if (writeNode.nodeName.toLowerCase() == 'td') {
		createCookie(writeNode.id,'headerRow',0);
		//go find the parent menu to hold open
		var findId = writeNode.id.substring(0,3);
		var item = document.getElementById('menu').getElementsByTagName('span');
		for (var i=0;i<item.length;i++) if (item[i].id.indexOf(findId) != -1) createCookie(item[i].id,'block',0);
	} else {
		createCookie(writeNode.id,'italic',0);	
		//go find the grandparent menu to hold open
		findId = writeNode.id.substring(0,3);
		var item = document.getElementById('menu').getElementsByTagName('span');
		for (var i=0;i<item.length;i++) if (item[i].id.indexOf(findId) != -1) createCookie(item[i].id,'block',0);
		// go find the parent to hold open
		var findId = writeNode.id.substring(0,5);
		for (var i=0;i<item.length;i++) if (item[i].id.indexOf(findId) != -1) createCookie(item[i].id,'block',0);
	}
}

function killMenuCookies() {
	var nameEQ = "pm";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0 && nameEQ.length != c.length) createCookie(c.substring(0,c.indexOf('=')),'',0);
	}
}
