window.onerror=null;

// Default layers
var arrLayers1 = new Array();
var arrCurrent = new Array();

// Active Background Images
var arrToggles = new Array();

// Visibility attribute conventions
strOn = "visible";
strOff = "hidden";

// Set image toggle
function setImageToggle(imgName,toggleFlag) { 
	toggleFlag ? arrToggles[arrToggles.length] = imgName : deleteFromArray(arrToggles, imgName);
}

//check for image state and toggle accordingly
function checkToggle(theImg,newSrc) {
	if (!checkArray(arrToggles,theImg)) {
		toggleImage(theImg,newSrc,theImg.substr(0,theImg.length - 3) + "bg");
	}
}

// Toggle layer
function toggleLayer(id,mode) {
	strLayer = document.all[id].style;
	if (mode == 'on') {
		strLayer.visibility = strOn;
		checkSearch(id);
	} else if (mode == 'off') {
		strLayer.visibility = strOff;
	} else {
		strImg = id.replace("menu", "img");
		if (strLayer.visibility != strOff) {			
			clearLayers(1);
			strLayer.visibility = strOff;
			setImageToggle(strImg,false);
		} else {	
			//clear layers manually for cross browsers (can't call clearlayers)
			clearLayers(1,id);
			//turn on clicked layer and toggle image
			resetImages();
			strLayer.visibility = strOn;
			checkSearch(id);
			setImageToggle(strImg,true);
		}
	}
}

// Change image source
function toggleImage(theSrc,newSrc,theLayer){
	document.images[theSrc].src = eval(newSrc).src;
}


visParentLayer = "";
visChildLayer = "";
// Clear all layers
function clearLayers(mode,thisLayer) {
	if (mode == 2) {	
		if (visChildLayer != "") { document.all[visChildLayer].style.visibility = "hidden";	}
		if (thisLayer) { visChildLayer = thisLayer; }
	} else {
		if (visParentLayer != "") { 
			document.all[visParentLayer].style.visibility = "hidden";
			if (visChildLayer != "") { document.all[visChildLayer].style.visibility = "hidden"; }
		}
		if (thisLayer) { visParentLayer = thisLayer; }	
	}	

	//turn back on search layer if present
	if (mode == 1 && blnSearch) {
		strLayer3 = document.all["lysearch"].style ;
		strLayer3.visibility = strOn;
		if (document.all["logonSelect"]) {
			document.all["logonSelect"].style.visibility = "visible";
		}
		if (document.all["accountSelect"]) {
			document.all["accountSelect"].style.visibility = "visible";
		}
	}
	if (mode == 1 && blnForm){
		strLayer3 = document.all["lyfe0"].style ;
		strLayer3.visibility = strOn;
	}
	checkForm();
}

//reset images to default state and empty arrays
function resetImages() {
	for (var n=0; n < arrToggles.length; n++) {
		if (arrToggles[n] != "") {
			document.images[arrToggles[n]].src = document.images[arrToggles[n]].src.replace("_on.gif","_off.gif");
		}
	}
	arrToggles.length = 0;
}

//triggered by event capture
function checkHouse(e) {
	if(event.srcElement.name != "" && event.srcElement.tagName != "IMG" && event.srcElement.tagName != "DIV") {
		clearLayers(1);
		clearBackgrounds();
		resetImages();
	}
}

//pass on click event to checkHouse
document.onmouseup = checkHouse; 

// Checks Array for value
function checkArray(arrayName, val) {
	for (var i=0; i < arrayName.length; i++) {
		if (arrayName[i] == val) {
			return true;
		}
	}
	return false;
}

// Deletes Value from array
function deleteFromArray(arrayName, val) {
	for (var i=0; i < arrayName.length; i++) {
		if (arrayName[i] == val) {
			arrayName[i] = "";
		}
	}
	return arrayName;
}

// External Link Toggles
function imgOn(imgName) {
	document[imgName].src = eval(imgName + "on.src");
}
function imgOff(imgName) {
	document[imgName].src = eval(imgName + "off.src");
}

//Builds Default layers and Current Layers
function initLayers() {
	buildCurrent();	
	initMain();
	
	//detect the search layer
	blnSearch = false;
	blnForm = false;
	
	if (document.all["lysearch"]) { blnSearch = true; }			
	if (document.all["lyfe0"]) { blnForm = true; }
	for(n=0;n<document.forms.length;n++) {
		if ((navigator.appVersion.indexOf("MSIE 4") > -1) && (navigator.appVersion.indexOf("Mac") > -1)) {
			if (document.forms[n].offsetParent.id == "lyfe0") {	theFormObj = document.forms[n];	}
		} else {
			if (document.forms[n].offsetParent.id == "formContainer") { theFormObj = document.forms[n];	}
		}
	}

	//position the search layer to right side of screen
	if (blnSearch) {
		lysearch.style.left = document.body.scrollWidth - lysearch.clientWidth;
		lysearch.style.visibility = strOn;
	}
	//position the form layer to bottom of FormContent Layer
	if (blnForm) {
		document.all["lyfe0"].style.top = parseInt(document.all["lyfeContent"].clientHeight) + 176;
		document.all["lyfe0"].style.visibility = strOn;
		document.all["footer"].style.top = parseInt(document.all["lyfe0"].style.top) + parseInt(document.all["lyfe0"].clientHeight);
		document.all["footer"].style.visibility = strOn;
		if (navigator.appVersion.indexOf("Mac") > -1) {
			document.images[arrTier2Arrows[0]].src = nav_hlt.src
		}
	}
}


//hides search form if layers are on top
function checkSearch(id) {	
	if (blnSearch) {	
		// Assign all postioning variables
		strLayerSearch 			= document.all["lysearch"];
		strLayerId       		= document.all[id];
		strLayerIdRightPos 		= strLayerId.clientWidth + strLayerId.offsetLeft;
		strLayerIdTopPos		= parseInt(strLayerId.style.top);
		strLayerIdBotPos 		= parseInt(strLayerId.style.top) + strLayerId.clientHeight;
		strLayerSearchLeftPos 	= strLayerSearch.offsetLeft;
		strLayerSearchTopPos	= parseInt(strLayerSearch.style.top);
		strLayerSearchBotPos 	= parseInt(strLayerSearch.style.top) + strLayerSearch.clientHeight;
		hideLayer();
	}
	checkForm();
	return;
}

function checkForm() {
	if ((navigator.appVersion.indexOf("Mac") > -1) && (navigator.appVersion.indexOf("MSIE") > -1)) {return;}
	if (blnForm) {
		// Assign all postioning variables
		for (f=0;f<theFormObj.elements.length;f++) {
			theFormObj.elements[f].style.visibility = strOn;
		}
		for (l=0;l<arrLayers1.length;l++) {
			if (document.all[arrLayers1[l]].style.visibility == "visible") {
				for (f=0;f<theFormObj.elements.length;f++) {
					if (theFormObj.elements[f].type.indexOf("select") > -1) {
						strLayerSearch 			= theFormObj.elements[f];
						strLayerId       		= document.all[arrLayers1[l]];
						strLayerIdRightPos 		= strLayerId.clientWidth + strLayerId.offsetLeft;
						strLayerIdTopPos		= parseInt(strLayerId.style.top);
						strLayerIdBotPos 		= parseInt(strLayerId.style.top) + strLayerId.clientHeight;
						strLayerSearchLeftPos 	= strLayerSearch.offsetParent.offsetLeft + 225;
						strLayerSearchRightPos 	= strLayerSearchLeftPos + strLayerSearch.clientWidth;
						if (navigator.appVersion.indexOf("MSIE 4") > -1) {
							strLayerSearchTopPos = strLayerSearch.offsetParent.offsetParent.offsetTop + document.all["lyfeContent"].clientHeight + 200;
						} else {
							strLayerSearchTopPos = strLayerSearch.offsetParent.offsetTop + document.all["lyfeContent"].clientHeight + 200;
						}
						strLayerSearchBotPos 	= strLayerSearchTopPos + strLayerSearch.clientHeight;
						if ((strLayerIdRightPos > strLayerSearchLeftPos) && (strLayerIdBotPos > strLayerSearchTopPos) && (strLayerSearchBotPos > strLayerIdTopPos) && (strLayerId.offsetLeft < strLayerSearchRightPos)) {
							strLayerSearch.style.visibility = strOff;
						}
					}
				}
			}
		}
	}
}


 function hideLayer() {
	if ((navigator.appVersion.indexOf("Mac") > -1) && (navigator.appVersion.indexOf("MSIE") > -1)) {return;}
 	// Hide if visible	
	if ((strLayerIdRightPos > strLayerSearchLeftPos) && (strLayerIdBotPos > strLayerSearchTopPos) ) {
		if (document.all["logonSelect"]) {
			document.all["logonSelect"].style.visibility = "hidden";
		}
		if (document.all["accountSelect"]) {
			document.all["accountSelect"].style.visibility = "hidden";
		}
	} else {
		if (document.all["logonSelect"]) {
			document.all["logonSelect"].style.visibility = "visible";
		}
		if (document.all["accountSelect"]) {
			document.all["accountSelect"].style.visibility = "visible";
		}
	} 
 }

//Navigator Layer Bug Window Resizing Fix
function netscapeFix() {
	if (blnSearch) {
		document.all["lysearch"].style.left = document.body.scrollWidth - document.all["lysearch"].clientWidth;
	}
}
start_width = document.width;
start_height = document.height;
window.onresize = netscapeFix;

// change background color of layer to effect highlight
function rollMen(obj, toggle, nest) {
	document.all[obj].setAttribute('className',toggle);
}

hlLayer = "";
function clearBackgrounds(thisLayer) {
	if (hlLayer != "") {
		document.all[hlLayer].setAttribute('className','brcolor0');
	}
	if (thisLayer) {hlLayer = thisLayer};
}






// Misc vars
var layerPrefix      = 'ly';
var itemCount        = 0;

// jpmMenuTree constructor (pseudo-public)
function jpmMenuTree(name, leftPos, topPos, width, colorCode) {
	this.name           = name;
	this.section        = getSectionId(name);
	this.left           = leftPos;
	this.top            = topPos;
	this.width          = width;
	this.color		    = colorCode;
	this.brcolor        = 'brcolor' + colorCode;
	this.items          = new Array();
	this.addMenuItem    = tier1addMenuItem;
	this.toHtml         = tier1toHtml;	
	this.renderChildren = tier1childrenToHtml;
	arrLayers1[arrLayers1.length] = layerPrefix + name;
}

// jpmMenu constructor (pseudo-public)
function jpmMenu(name, leftPos, topPos, width, colorCode) {
	this.name         = name;
	this.color		  = colorCode;
	this.brcolor      = 'brcolor' + colorCode;
	this.items        = new Array();
	this.addMenuItem  = tier2addMenuItem;
	this.toHtml       = tier2toHtml;
	arrLayers1[arrLayers1.length] = layerPrefix + name;
}

// jpmMenuItem constructor (pseudo-private)
function jpmMenuItem(menuText, menuHref, childId) {
	this.id       = ++itemCount;
	this.text     = menuText;
	this.href     = menuHref;
	this.child    = childId;
	this.hasChild = (childId != "") ? 1 : 0;
}


// Add one menu item for Tier 1 menu (pseudo-private)
function tier1addMenuItem(itemHref, itemText, itemChild) {
	this.items[this.items.length] = new jpmMenuItem(itemText, itemHref, itemChild);
}

// Add one menu item for Tier 2 menu (pseudo-private)
function tier2addMenuItem(itemHref, itemText) {
	this.items[this.items.length] = new jpmMenuItem(itemText, itemHref, '');
}

// Write HTML for Tier 1 menu (pseudo-public)
function tier1toHtml() {
	theItems = this.items;
	theName = this.name;
	theWidth = this.width;
	theColor = this.brcolor;
	theTop = this.top;
	theLeft = this.left;
	menuHLColor = 'highlight';
	html = '';
	html += '<table cellpadding="0" cellspacing="0" border="0" class="' + theColor + '" width="'+theWidth+'" id="colorTable'+theName+'"><tr><td align="center">'
	html += '<table cellpadding="0" cellspacing="0" border="0" bgcolor="' + menuBgColor + '" width="'+(theWidth - 2)+'">'
	html += '<tr><td class="bc-spacer">&nbsp;</td></tr>';
	for (var p = 0; p < theItems.length; p++) {
		theItemObj = theItems[p];	
		html += '<tr class="padLeft">';
		if (theItemObj.hasChild == 1) {
			html += '<td id="Sub'+ layerPrefix + theName + p +'" onmouseover="clearBackgrounds(\''+ 'Sub' + layerPrefix + theName + p +'\');rollMen(\'Sub'+layerPrefix + theName + p+'\', \''+menuHLColor+'\', \''+layerPrefix + theName+'\');clearLayers(2,\'' + layerPrefix + theItemObj.child + '\');toggleLayer(\'' + layerPrefix + theItemObj.child + '\',\'on\');" onclick="javascript:location=\'' + theItemObj.href + '\';"><a class="navsub5" href="' + theItemObj.href + '">';
		} else {
			html += '<td id="Sub'+ layerPrefix + theName + p +'" onmouseover="clearBackgrounds(\''+ 'Sub' + layerPrefix + theName + p +'\');rollMen(\'Sub'+layerPrefix + theName + p+'\', \''+menuHLColor+'\', \''+layerPrefix + theName+'\');clearLayers(2);" onmouseout="clearBackgrounds(\''+ 'Sub' + layerPrefix + theName + p +'\');" onclick="javascript:location=\'' + theItemObj.href + '\';"><a class="navsub5" href="' + theItemObj.href + '">';
		}
		html += theItemObj.text + '</a></td></tr>';
		html += '<tr><td class="bc-spacer-cell"><span class="bc-spacer">&nbsp;</span></td></tr>';
	}
	html += '</table></td></tr>'
	html += '<tr><td class="bc-spacer-big">&nbsp;</td></tr>';
	html += '</table>&nbsp;'
	document.all[layerPrefix + theName].innerHTML = html;
	document.all[layerPrefix + theName].style.top = theTop;
	document.all[layerPrefix + theName].style.left = theLeft;
	
	// Render children
	this.renderChildren();
}

// Write HTML for Tier 2 menu (pseudo-public)
function tier2toHtml() {
	theItems = this.items;
	theName = this.name;
	theWidth = this.width;
	theColor = this.brcolor;
	theTop = this.top;
	theLeft = this.left;
	menuHLColor = 'highlight';
	html = '';		
	html += '<table cellpadding="0" cellspacing="0" border="0" class="' + theColor + '" width="'+theWidth+'">'
	html += '<tr><td><span class="bc-spacer">&nbsp;</span></td></tr>';
	html += '<tr><td align="center">';
	html += '<table cellpadding="0" cellspacing="0" border="0" bgcolor="' + menuBgColor + '" width="'+(theWidth - 2)+'"><tr><td>'
	html += '<tr><td class="bc-spacer">&nbsp;</td></tr>';
	for (var p = 0; p < theItems.length; p++) {
		theItemObj = theItems[p];
		html += '<tr class="padLeft"><td id="'+layerPrefix + theName + p +'" onmouseover="rollMen(\''+layerPrefix + theName + p+'\', \''+menuHLColor+'\', \''+layerPrefix + theName+'\')" onmouseout="rollMen(\''+layerPrefix + theName + p+'\', \'brcolor0\', \''+layerPrefix + theName+'\')" onclick="javscript:location=\'' + theItemObj.href + '\';"><a href="' + theItemObj.href + '" class="navsub5">' + theItemObj.text + '</a></td></tr>';
		html += '<tr><td class="bc-spacer-cell"><span class="bc-spacer">&nbsp;</span></td></tr>';
	}
	html += '</table></td></tr>'
	html += '<tr><td class="bc-spacer-big">&nbsp;</td></tr>';
	html += '</table>&nbsp;'
	
	document.all[layerPrefix + theName].innerHTML = html;
	document.all[layerPrefix + theName].style.top = theTop;
	document.all[layerPrefix + theName].style.left = theLeft - 3;

}

// Generate Tier 2 menus
function tier1childrenToHtml() {
	theNewItems = this.items;
	thisWidth = this.width;
	thisLeft = this.left;
	thisTop = this.top;
	for (var c = 0; c < theNewItems.length; c++) {
		theItemObj = theNewItems[c];
		if (theItemObj.hasChild == 1) {
			lMenu = eval(theItemObj.child);
			lMenu.width = thisWidth;
			lMenu.left = thisLeft + thisWidth;
			// Set height
			if ((navigator.appVersion.indexOf("MSIE 4") > -1) && (navigator.appVersion.indexOf("Mac") == -1)) {
				lMenu.top = document.all["Subly"+lMenu.name].offsetParent.offsetTop + thisTop;
			} else if((navigator.appVersion.indexOf("MSIE 5") > -1) && (navigator.appVersion.indexOf("Mac") > -1)) {
				lMenu.top = document.all["Subly"+lMenu.name].clientTop + thisTop;
			} else {
				lMenu.top = document.all["Subly"+lMenu.name].offsetTop + thisTop;
			}
			lMenu.toHtml();
		}
	}
}

// Get section ID from string
function getSectionId(str) {
	return str.substring(0,2);
}

// Builds the current section box
function buildCurrent(){
	if (arrCurrent.length > 0) {
		html = '<table border="0" cellpadding="0" cellspacing="0" width="'+arrCurrent[3]+'" height="20" class="brcolor'+arrCurrent[4]+'">';
		html += '<tr><td width="4"><span class="bc-current">&nbsp;&nbsp;</span></td><td height="16" width="'+(arrCurrent[3] -4)+'"><span class="bc-current">'+arrCurrent[0]+'</span></td></tr>';
		html += '</table>&nbsp;';
		document.all[layerPrefix + "current"].innerHTML = html;
		document.all[layerPrefix + "current"].style.top = arrCurrent[2];
		document.all[layerPrefix + "current"].style.left = arrCurrent[1];
	}
}

