var isDOM = (document.getElementById ? true : false); 
var isIE4 = ((document.all && !isDOM) ? true : false);
var isNS4 = (document.layers ? true : false);
var NS4Move = true;
var AgntUsr=navigator.userAgent.toLowerCase();
var isExp=AgntUsr.indexOf('msie')!=-1?1:0;
var IEStart=(document.all?AgntUsr.search('msie\s*'):0);
var ExpVer = 
	(document.all?
	parseFloat(
		AgntUsr.substring(
			IEStart+5,
			AgntUsr.indexOf(
				';',
				IEStart+1
			)
		)
):''); 
var lngLayerStart = -999;

// Array showing highlighted menu items.
var litNow = new Array();


function findPosX(obj) {
var lngCurLeft = 0;
	if (document.getElementById || document.all)
	{
		while (obj.offsetParent)
		{
			lngCurLeft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (document.layers) {
		lngCurLeft += obj.x;
	}
	return lngCurLeft;
}

function findPosY(obj) {
var lngCurTop = 0;
	if (document.getElementById || document.all)
	{
		while (obj.offsetParent)
		{
			lngCurTop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (document.layers) {
		lngCurTop += obj.y;
	}
	return lngCurTop;
}

function HideElements(top, left, width, height) {
var intForm;
var intEle;
var objForm;
var lngX;
var lngY;	
var objArr = new Array();
var intCount = 0;
	if (document.all) {
		if (ExpVer > 5.5) { return null;}
		for(intForm=0; intForm < document.forms.length; intForm++) {
			objForm = document.forms[intForm];
			for (intEle=0; intEle < objForm.length; intEle++) {
				objEle = objForm.elements[intEle];
				switch( objEle.type ) {
					case "select-one":
					case "select-multiple":
					break;
					default:
					if (ExpVer >= 5.5) {
						continue;
					} 
					break;
				}					
				if (objEle.style.visibility.indexOf('hidden') == -1) {
					lngY=findPosY(objEle);
					if ((top + height) > lngY &&
						lngY + objEle.offsetHeight > (top)) {
						lngX=findPosX(objEle);
						if ((left + width) > lngX &&
							lngX + objEle.offsetWidth > (left)) {
							objEle.style.visibility = 'hidden';
							objArr[intCount++] = objEle;
						}				 				
					}
				}
			}
		}
	} else if (document.layers && document.layers.idMainLayer) {
		objEle = document.layers.idMainLayer;
		lngLayerStart = lngLayerStart==(-999)?objEle.top:lngLayerStart;
		if (top + height > objEle.top) {
			if (NS4Move) {
				objEle.top = top + height;
			} else {
				document.layers.idMainLayer.layers[0].visibility='hidden';
			}
		}
	}
	return objArr;
}

function getRef(id) {
	if (isDOM) return document.getElementById(id);
	if (isIE4) return document.all[id];
	if (isNS4) return document.layers[id];
}
function getSty(id) {
	return (isNS4 ? getRef(id) : getRef(id).style);
} 
function popOver(menuNum, itemNum, what) {
var objMnu;
var ScWinWdth=isExp?document.body.clientWidth:innerWidth;
var	ScWinHght=isExp?document.body.clientHeight:innerHeight;
var intTemp;

	var SubMnuNum = menu[menuNum][itemNum].SubMnu;
	if (!menu[SubMnuNum][0].IsDrawn) {
		writeMenus( SubMnuNum );
	}
	
	clearTimeout(popTimer);
	hideAllBut(menuNum,true);
	menu[menuNum][itemNum].what = what;
	litNow = getTree(menuNum, itemNum);
	changeCol(litNow, true, (menuNum==0?true:false));

	if (SubMnuNum > 0) {

		thisX = parseInt(menu[menuNum][0].ref.left) + parseInt(menu[menuNum][itemNum].ref.left);
		thisY = parseInt(menu[menuNum][0].ref.top) + parseInt(menu[menuNum][itemNum].ref.top);

		objMnu = menu[SubMnuNum][0];
		with (menu[SubMnuNum][0].ref) {
		
			if (thisX +  objMnu.x + objMnu.width > ScWinWdth) {
				if (menuNum > 0) {
					intTemp = parseInt(thisX - objMnu.width - (objMnu.x - menu[menuNum][0].width));
				} else {
					intTemp = ScWinWdth - objMnu.width;
				}
				left = intTemp>0?intTemp:0;
			} else {
				left = parseInt(thisX + objMnu.x);
			}
			if(thisY + objMnu.y > ScWinHght) {
				intTemp = ScWinHght - objMnu.y - 2;
				top = intTemp>0?intTemp:0;
			} else {
				top = parseInt(thisY + objMnu.y);
			} 
			
			
			visibility = 'visible';

			objMnu.HiddenElements=
				HideElements(parseInt(top), parseInt(left), objMnu.width, 
					parseInt(objMnu.height));
		}							
	}
}
function popOut(menuNum, itemNum, what) {
	if ((menuNum == 0) && !menu[menuNum][itemNum].SubMnu) {
		hideAllBut(0,false)
	} else {
 		popTimer = setTimeout('hideAllBut(0,false)', 500);
	}
}
function getTree(menuNum, itemNum) {
	// Array index is the menu number. The contents are null (if that menu is not a parent)
	// or the item number in that menu that is an ancestor (to light it up).
	itemArray = new Array(menu.length);
	while(1) {
		itemArray[menuNum] = itemNum;
		// If we've reached the top of the hierarchy, return.
		if (menuNum == 0) return itemArray;
			itemNum = menu[menuNum][0].parentItem;
		menuNum = menu[menuNum][0].parentMenu;
	}
}
// Pass an array and a boolean to specify colour change, true = over colour.
function changeCol(changeArray, isOver, isAll) {
var strClass;
var strInnerHTML;
	for (menuCount = 0; menuCount < changeArray.length; menuCount++) {
		if (changeArray[menuCount]) {
			strInnerHTML = menu[menuCount][changeArray[menuCount]].innerHTML;
			if (isOver) {
				newCol = menu[menuCount][0].overCol;
				strClass = (menuCount==0?menu[menuCount][0].textOnClass:menu[menuCount][0].textOnSubClass)
				if (isNS4) {
					strInnerHTML = NSReplaceClass(
						strInnerHTML, 
						strClass);
				}
			} else {
				if (!isAll && menuCount==0) {
					// Not resetting all, do not reset the main menu, it
					// can stay like it is.
					continue;
				}			
				newCol = menu[menuCount][0].backCol;			
				strClass = (menuCount==0?menu[menuCount][0].textClass:menu[menuCount][0].textSubClass);
			}
			// Change the colours of the div/layer background.
			with (menu[menuCount][changeArray[menuCount]].ref) {
				if (isNS4) {
					NSChangeContent(
						menu[menuCount][changeArray[menuCount]].what,
						strInnerHTML);
					menu[menuCount][changeArray[menuCount]].what.bgColor = newCol;
					if (menuCount != 0) {
						menu[menuCount][changeArray[menuCount]].what.parentLayer.bgColor = newCol;
					}
				} else {
					 backgroundColor = newCol;
					 DOMReplaceClass('a' + menu[menuCount][changeArray[menuCount]].what.id,
					 	strClass);
				}
			}
		}
	}
}
function hideAllBut(menuNum, blnNewMenu) {
var intCount;
var objMenu;
var keepMenus = getTree(menuNum, 1);

	for (count = 0; count < menu.length; count++) {
		if (!keepMenus[count]) {
		  //alp - added if to verify that menu[count] is an object.
		  if(menu[count]){
			objMenu = menu[count][0];
			if (objMenu.ref) {
				objMenu.ref.visibility = 'hidden';
				if (objMenu.HiddenElements) {
					for(intCount=0; intCount<objMenu.HiddenElements.length; intCount++) {
						objMenu.HiddenElements[intCount].style.visibility='visible';
					}
					objMenu.HiddenElements = null;
				} else if (document.layers && menuNum == 0 && blnNewMenu==false && 
					document.layers.idMainLayer) {

					objMenu = document.layers.idMainLayer;
					if (NS4Move) {
						if (lngLayerStart != objMenu.top &&
							lngLayerStart != -999) {
							objMenu.top = lngLayerStart;
					
						}
					} else {
						document.layers.idMainLayer.layers[0].visibility='visible';
					}
				}
			}
		   }
		}
	}			
	changeCol(litNow, false, (menuNum==0?true:false));
}

function NSReplaceClass(strText, strNewClass) {
	return strText.replace("class=[\",\'][^\"\']*[\",\']", "class=\"" + strNewClass + "\"")
}
function DOMReplaceClass( strElement, strNewClass) {
var objEle;
		objEle = document.getElementById(strElement);
		if (objEle) {
			objEle.className=strNewClass;
		}					
}
function NSChangeContent(what,text) {
    if (document.layers) {
        what.document.write(text);
        what.document.close();
    }
}
function Menu(
	x, y, width, 
	isVert,  
	popInd, overCol, backCol, 
	borderColor, borderWidth, 
	textClass, textOnClass, textSubClass, textOnSubClass,
	MainBorderStyle, SubBorderStyle, LstBorderStyle) {
	
	// Position and size settings.
	this.x = x;
	this.y = y;
	this.width = width;

	// True or false - a vertical menu?
	this.isVert = (isVert==null?true:isVert);
	
	// The popout indicator used (if any) for this menu.
	this.popInd = (popInd==null?defpopInd:popInd);

	// Colours of menu and items.
	this.overCol = (overCol==null?defOver:overCol);
	this.backCol = (backCol==null?defBack:backCol);
	// The stylesheet class used for item borders and the text within items.
	this.borderColor = (borderColor==null?defBorderColor:borderColor);
	this.borderWidth = (borderWidth==null?defBorderWidth:borderWidth);
	this.MainBorderStyle = (MainBorderStyle==null?defMainBorderStyle:MainBorderStyle);
	this.SubBorderStyle = (SubBorderStyle==null?defSubBorderStyle:SubBorderStyle);
	this.LstBorderStyle = (LstBorderStyle==null?defLstBorderStyle:LstBorderStyle);
		
	this.textClass = (textClass==null?defMainStyle:textClass);
	this.textOnClass = (textOnClass==null?defOnMainStyle:textOnClass);
	this.textSubClass = (textSubClass==null?defSubStyle:textSubClass);
	this.textOnSubClass = (textOnSubClass==null?defOnSubStyle:textOnSubClass);
	// Parent menu and item numbers, indexed later.
	this.parentMenu = null;
	this.parentItem = null;
	// Reference to the object's style properties (set later).
	this.ref = null;
	this.what = null;
	
	this.innerHTML = '';
	
	this.IsDrawn = false;
}

function Item(text, href, SubMnu, length, spacing, frame ) {

	this.text = text;
	this.href = href;

	// Optional arguments
	this.SubMnu = (SubMnu==null?0:SubMnu);
	this.length = (length==null?defLength:length);
	this.spacing = (spacing==null?defExtSpaces:spacing);
	this.frame = (frame==null?defFrame:frame);
	
	// Reference to the object's style properties (set later).
	this.ref = null;
}
function Go ( intMenuNbr, intMenuItem ) {
	var strURL = menu[intMenuNbr][intMenuItem].href;
	strURL = strURL.replace(/&amp;/g,"&");
	if (strURL.indexOf("javascript:") > -1) {
	  strURL = strURL.replace("javascript:","");
	  eval(strURL);
	}else{	
	  document.location = strURL;
	}		
}

function NSGo( e ) {
    var strURL = menu[this.currMenu][this.currItem].href
	strURL = strURL.replace(/&amp;/g,"&");
	document.location= strURL;
}
function writeMenus( intMenuNbr ) {
	if (!isDOM && !isIE4 && !isNS4) return;
	var str = '';
	var itemX = 0;
	var itemY = 0;
	var strOuter='';
	var strLayer='';
	var intStrtMenu;

	if (intMenuNbr == null) {
		intMenuNbr = 1;
 		intStrtMenu = 0;
	} else {
		intStrtMenu = intMenuNbr;
		intMenuNbr ++;
	}

	for (currMenu = intStrtMenu; currMenu < intMenuNbr; currMenu++) {
	with (menu[currMenu][0]) {
		menu[currMenu][0].IsDrawn = true;
		// Variable for holding HTML for items and positions of next item.
		str = '';
		if (isNS4 && currMenu > 0) {
			itemX = 1;
			itemY = 1;
		} else { 
			itemX = 0;
			itemY = 0;
		}
		strOuter='';
		strLayer='';

		// Remember, items start from 1 in the array (0 is menu object itself, above).
		// Also use properties of each item nested in the other with() for construction.
	 	for (currItem = 1; currItem < menu[currMenu].length; currItem++)
		with (menu[currMenu][currItem]) {
			var itemID = 'menu' + currMenu + 'item' + currItem;
			strOuter='';
			strLayer='';
			
			// The width and height of the menu item - dependent on orientation!
			var w = (isVert ? width : length);
			var h = (isVert ? length : width);
			var imgW=(popInd?popInd[1]:0);
			var imgH=(popInd?popInd[2]:0);
			var imgTop=(h-imgH)/2;
			var imgL=(w-imgW-5);
			
			if (SubMnu > 0) {
				// Set SubMnu's parents to this menu item.
				menu[SubMnu][0].parentMenu = currMenu;
				menu[SubMnu][0].parentItem = currItem;
			}
			if (isDOM || isIE4) {
				strLayer +='<div id="' + itemID + '" OnClick="Go(' + currMenu + ',' + currItem + ');"'
				if (borderWidth>0){
            		if(currMenu == 0 || (currItem + 1) == menu[currMenu].length){
            		  if(currMenu == 0 && (currItem + 1) != menu[currMenu].length) {
                        strLayer += ' class="' + MainBorderStyle + '"';  
            		  } else {
                        strLayer += ' class="' + LstBorderStyle + '"';
                      }
                    } else {
                        strLayer += ' class="' + SubBorderStyle + '"';
                    }
    			}
                strLayer += ' style="position: absolute; cursor: hand; left: ' + itemX + '; top: ' + itemY + '; width: ' + w + '; height: ' + h + '; visibility: inherit; overflow: hidden; ';
				if (backCol) {
					strLayer += 'background: ' + backCol + ';';				
				}	
				strLayer += '" ';
				// Add mouseover handlers and finish div/layer.
				strLayer += 'onMouseOver="popOver(' + currMenu + ',' + currItem + ', this)" onMouseOut="popOut(' + currMenu + ',' + currItem + ', this)"  OnMouseUp="Go(' + currMenu + ',' + currItem + ');">';
				strLayer += spacerImg + 
					'<span id="a' + itemID + '" class="' + (currMenu==0?textClass:textSubClass) + '">' + text;
				if (SubMnu > 0 && popInd) {					
					strLayer += '<div style="position:absolute; top:'+imgTop+'; left:'+imgL+'; width:'+imgW+'; height:'+imgH+';visibility:inherit"><img src="'+popInd[0]+'"></div>';
				}
				strLayer += '</span>';
				strLayer += strOuter + '</div>';
			} else if (isNS4) {
			    var menuwidth;
				if(currMenu == 0){
				 menuwidth = w+ ( 2 * borderWidth);
				}else{
				 menuwidth = w;
				}
				strLayer += '<layer id="' + itemID +
					'" left="' + 
					(itemX) + '" top="' + (itemY) + '" width="' +  
					(menuwidth) + '" height="' + (h) + '" visibility="inherit"';
				
				if (backCol) {
				    if(currMenu == 0){
						strLayer += ' bgcolor="'+borderColor+'" ';
					}else{
						strLayer += ' bgcolor="' + backCol + '" ';
					}		
				}
				strLayer += '>';
				strLayer += '<layer id="next' + itemID +  
					'" left="' + borderWidth + '" top="' + borderWidth + '" width="' +  
					(w) + '" height="' + (h-(2*borderWidth)) + '" clip="' + (w) + ',' + (h-(2*borderWidth)) + '" visibility="inherit"';
			
				if (backCol) {
					strLayer += ' bgcolor="' + backCol + '"';				
				}
				strLayer += ' onMouseOver="popOver(' + currMenu + ',' + currItem + ', this)" onMouseOut="popOut(' + currMenu + ',' + currItem + ', this)"  OnClick="Go(' + currMenu + ',' + currItem + ');">';
				
				strOuter += '<table width="' + (w - 4) +
					'" height="' + (h-4) +    
					'" border="0" cellspacing="0" cellpadding="0">' +
					'<tr><td width="5">'+spacerImg+'</td><td width="100%">';
				strOuter += '<span class="' + (currMenu==0?textClass:textSubClass) + '">' + text + '</span></td>';
				if (SubMnu > 0 && popInd) {
					strOuter += '<td class="' + textClass + '" align="right"><img src="' + popInd[0] + '" alt=""></td>';				
				}	
				strOuter += '</tr></table>';			
				strLayer += strOuter + '</layer></layer>';
				menu[currMenu][currItem].innerHTML = strOuter;
				
				if (currMenu > 0 && borderWidth>0) {
					if (isVert) { 
						itemY += borderWidth;
					} else {
						itemX += borderWidth;
					}
				}
			}
			str += strLayer;
			if (isVert) { 
				itemY += length + spacing;
			} else {
				itemX += length + spacing;
			}
		}

		if (isDOM) {
			// IE 5+ or NS 6+
			var newDiv = document.createElement('div');
			document.getElementsByTagName('body').item(0).appendChild(newDiv);
			newDiv.innerHTML = str;
			ref = newDiv.style;
			ref.position = 'absolute';
			ref.visibility = 'hidden';
			
//?? Uncomment This if you want to view the HTML.
/*
var id;
	id = document.getElementById('output');
	if (id) {
		id.value = str;
	}
*/	
		} else if (isIE4) {
			// Insert a div tag to the end of the BODY with menu HTML in place for IE4.
			document.body.insertAdjacentHTML('beforeEnd', '<div id="menu' + currMenu + 'div" ' + 'style="position: absolute; visibility: hidden">' + str + '</div>');
			ref = getSty('menu' + currMenu + 'div');
		} else if (isNS4) {
			// In NS4, create a reference to a new layer and write the items to it.		
//??  Uncomment This if you want to view the HTML.
/*
if (document.forms[1] && document.forms[1].output) {
	document.forms[1].output.value = str;
}
*/		
			ref = new Layer((w+borderWidth));
			ref.document.write(str);
			ref.document.close();
			if (currMenu > 0 && borderWidth>0) {
				ref.bgColor=borderColor;
				ref.resizeBy(borderWidth,borderWidth);
			}
		}

		for (currItem = 1; currItem < menu[currMenu].length; currItem++) {
			itemName = 'menu' + currMenu + 'item' + currItem;
			if (isDOM || isIE4) menu[currMenu][currItem].ref = getSty(itemName);
			if (isNS4) {
				itemID = 'menu' + currMenu + 'item' + currItem;
				menu[currMenu][currItem].ref = ref.document[itemName];
				ref.layers[itemID].onmouseup=NSGo;
				ref.layers[itemID].captureEvents(Event.MOUSEUP);
				ref.layers[itemID].currMenu = currMenu;
				ref.layers[itemID].currItem = currItem;
				ref.currMenu = currMenu;
				ref.currItem = currItem;
			}
		}
		menu[currMenu][0].height=isVert?itemY:itemX;
	}
	}
	with(menu[0][0]) {
		ref.left = x;
		ref.top = y;
		ref.visibility = 'visible';
	}

}

// These two lines handle the window resize bug in NS4. See <body onResize="...">.
// I recommend you leave this here as otherwise when you resize NS4's width menus are hidden.
var popOldWidth = window.innerWidth;
nsResizeHandler = new Function('if (popOldWidth != window.innerWidth) location.reload()');

// This is a quick snippet that captures all clicks on the document and hides the menus
// every time you click. Use if you want.
//if (isNS4) document.captureEvents(Event.CLICK);
document.onclick = clickHandle;
function clickHandle(evt)
{
// if (isNS4) document.routeEvent(evt);
 hideAllBut(0);
}


