// 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);
	}
}


function openMenuJsp(thisNode) {
	var targetNode = getTargetNode(thisNode);
	/*if (targetNode.style.display!='none')	{
		//alert("openmenuNone "+thisNode.id);
		targetNode.style.display='none';
		thisNode.firstChild.nodeValue='[+]';
		createCookie(thisNode.id,'none',0);
	}
	else	{*/		
		//alert("openmenuJspBlock "+thisNode.id);
		targetNode.style.display='block';
		thisNode.firstChild.nodeValue='[-]';
		createCookie(thisNode.id,'block',0);
	//}
}

function mainTitle() { killMenuCookies() }

// 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++) {
		//alert("configmenu "+item[i].id);
		if (item[i].id.indexOf('pm') != -1) {
			var cook = readCookie(item[i].id);
			//alert("configmenu val cook "+cook);
			if (cook == 'block') {
				openMenu(item[i]);
				// since it's open find the current item and format it too.
				if (item[i].id.indexOf('_')==-1) {
					//alert("configmenu val item[i].id.indexOf "+item[i].id.indexOf('_'));
					// 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';
							}
						}
					}
				}
			}
		}
	}
}

function configMenuJsp() {
	var item = document.getElementById('menu').getElementsByTagName('span');
	createCookie('pm2','',0);
	createCookie('pm5','',0);
	for (var i=0;i<item.length;i++) {
		//alert("configmenuJsp "+item[i].id);
		if (item[i].id.indexOf('pm') != -1) {
			var cook = readCookie(item[i].id);
			//alert("configmenuJsp val cook "+cook);
			if (item[i].id=='pm4') {
				if (cook == 'block' ) {
					openMenuJsp(item[i]);
					// since it's open find the current item and format it too.
					if (item[i].id.indexOf('_') == -1 ) {
						//alert("configmenuJsp val item[i].id.indexOf "+item[i].id.indexOf('_'));
						// 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() {
	//alert("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) {
	//alert("setOpen");
	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) {
	//alert("itemcurrent ");
	// 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
		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 parent menu to hold open
		createCookie(reverseTargetNode(findVnav(writeNode),'lower').id,'block',0);
		// go find the grandparent to hold open
		createCookie(reverseTargetNode(findVnav(findVnav(writeNode)),'upper').id,'block',0);		
	}
}

function itemCurrentJsp(currNode) {
	//alert("itemcurrentJsp ");
	// first kill em all
	killMenuCookies();
	var writeNode = currNode.parentNode;
	if (writeNode.nodeName.toLowerCase() == 'td') {
		//alert("itemcurrentJsp td");
		createCookie(writeNode.id,'headerRow',0);
		//go find the parent 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) {
				//alert("itemcurrentJsp block "+item[i].id);
				createCookie(item[i].id,'block',0);
			}
		}
	} else {
		createCookie(writeNode.id,'italic',0);	
		//go find the parent menu to hold open
		createCookie(reverseTargetNode(findVnav(writeNode),'lower').id,'block',0);
		// go find the grandparent to hold open
		createCookie(reverseTargetNode(findVnav(findVnav(writeNode)),'upper').id,'block',0);		
	}
	configMenuJsp();
}

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);
		}
	}
}

// show and hide for the vertical menus and swaps the + - images

function swapMenuFirms(newMenu) {
	if (getStyleById('menu' + newMenu,'display')=='none') {
		setStyleById('menu' + newMenu,'display','inline');
	} else {
		setStyleById('menu' + newMenu,'display','none');
	}
}

function setStyleById(i, p, v) {
	var n = document.getElementById(i);
	n.style[p] = v;
}

function getStyleById(i, p) {
	var n = document.getElementById(i);
	var s = eval("n.style." + p);
	// try inline
	if((s != "") && (s != null)) return s;
	// try currentStyle
	if(n.currentStyle) {
		var s = eval("n.currentStyle." + p);
		if((s != "") && (s != null)) return s;
	}
	// try styleSheets
	var sheets = document.styleSheets;
	if(sheets.length > 0) {
		// loop over each sheet
		for(var x = 0; x < sheets.length; x++) {
			// grab stylesheet rules
			var rules = sheets[x].cssRules;
			if(rules.length > 0) {
				// check each rule
				for(var y = 0; y < rules.length; y++) {
					var z = rules[y].style;
					// selectorText broken in NS 6/Mozilla: see
					// http://bugzilla.mozilla.org/show_bug.cgi?id=51944
					ugly_selectorText_workaround();
					if(allStyleRules) {
						if(allStyleRules[y] == i) return z[p];
					} else {
						// use the native selectorText and style stuff
						if(((z[p] != "") && (z[p] != null)) || (rules[y].selectorText == i))
							return z[p];
					}
				}
			}
		}
	}
	return null;
}

function swapMenu( obj ) {
	if ( typeof obj == 'string' ) obj = document.getElementById(obj);
	if ( ! obj ) return;
	var curView = 'none';
	if ( document.defaultView && document.defaultView.getComputedStyle ) {
		curView = document.defaultView.getComputedStyle( obj, null).getPropertyValue('display');
	} else if ( obj.style ) {
		curView = obj.style.display;
	}
	obj.style.display = curView == 'none' ? 'block' : 'none';
}
