var M_bDomOk=document.getElementById?1:0;  // IE5+ and Netscape 7 support
var M_GroupSize = 6;
var M_LabelPos = 0;
var M_SubMenuPos = 1;
var M_LinkPos = 2;
var M_CheckPos = 3;
var M_nInActive = 4;

var M_nBorder = 2;
var M_nMaxDim = 10;

var M_nUpArrow = 1;
var M_nDownArrow = 2;
var M_nCheckMk = 3;
var M_nSeparator = 4;

var M_oOldKeyDown;   // store reference to keydown event handler
var M_oOldMouseUp;	 // store reference to mouseup event handler

// This value will be set as the z-index style of the action menu, ensuring that it appears over
// all other windows
var M_nZIndex = 10000;

var M_strBtnId ='ActBtn';
var M_strBtnImgId = 'ActBtnImg';
var M_strBtnDivId = 'ActDiv';

var M_strSepClass = '.hrr';

var M_nDelayTime  = 1000;
var M_nDelayTimeNscp  = 50;
var M_nPaddingLeft = -1;
var M_nPaddingRight = -1;

var KeyCodeARROWLEFT  = 37;
var KeyCodeARROWUP    = 38;
var KeyCodeARROWRIGHT = 39;
var KeyCodeARROWDOWN  = 40;
var KeyCodeEnter      = 13;

// all the global variables used in this file.
var M_aTopPos = new Array();				// Top position of all the menu(submenus)
var M_bLoaded = false;						// True if the menu is showing.
var M_bInKeyHandler = false;           // True if in keydown handler routine
var M_nDepth = 0;								// Menu level relatively to main menu
var M_nMaxMenuWidth = new Array();		// Individual menu(submenu)'s maximum width
var M_nOrgTop = 0;							// Main menu's Top coordinate.
var M_nOrgLeft = 0;							// Main menu's Left coordinate.
var M_objCurrentMenu = null;				// Menu most recently opened.
var M_objHighliteItem = null;				// Menu item currently has hightlight.
var M_objMenu = null;						// Main menu(First menu shown when button is clicked
var M_objTimerId = null;					// Global timer id used for delayed menu display
var M_objTimerNscp = null;					// Global timer id used for Netscape keyboard handling
var M_objTopContainer = null;				// Div element in which the whole main is created.

// the following loop is to load the image as early as the js file is loaded.
var M_astrImgSrc = ["/Common/Images/tri.gif",
        "/Common/Images/triup.gif",
        "/Common/Images/tridown.gif",
        "/Common/Images/checkmark.gif",
        "/Common/Images/divider.gif"];
							
function M_pfnToggleBtnImage(i_bClosed)
{
	var objImg = document.getElementById(M_strBtnImgId);

	if (objImg) {
		if (i_bClosed) {
			objImg.src = G_strRootPath + M_astrImgSrc[M_nDownArrow];
		}
		else {
			objImg.src = G_strRootPath + M_astrImgSrc[M_nUpArrow];
		}
	}
}

function M_pfnCancelClickEvent(evt)
{
	var bHandle = true;	
	if (PWdsapp_bIsIE) {
		window.event.cancelBubble = true;
	}
	else {
		if (typeof(evt) != 'undefined' && evt.target) {
			if (evt.target.id == M_strBtnId) {
				bHandle = false;
			}
			else if (evt.target.nodeType == 3) {
				bHandle = false;
			}
		}
	}
	return bHandle;
}

// Initialize the event handler for the event occurred outside of any menu item.
// TBD: In fucntion M_pfnInitEventHandler() and M_pfnClearEventHandler(), the codes
// commented out match well with general event handling model, but it conflicts with 
// scrolling bar in table view, more investigation is needed.
function M_pfnInitEventHandler()
{  
	// Register the event handlers that will respond to the mouseout events
	// and the mouseup event that follow this mousedown event.
	M_bInHandler = true;
	if (PWdsapp_bIsIE) {
	    M_oOldKeyDown = document.body.onkeydown;
        M_oOldMouseUp = document.body.onmouseup;	
		document.body.onkeydown = M_fnCheckKeyDown;
		document.body.onmouseup = M_fnCloseIfNotInMenu;	
	}
	else {
		document.body.addEventListener('mouseup', M_fnCloseIfNotInMenu, false);
		document.body.addEventListener('keydown', M_fnCheckKeyDown, false);
	}
}

// restore old event handler for the event occurred outside of any menu item.
function M_pfnClearEventHandler()
{
	// Unregister the capturing event handlers.
   if (PWdsapp_bIsIE) {
		document.body.onkeydown = M_oOldKeyDown;
		document.body.onmouseup = M_oOldMouseUp;
	}
	else {
		document.body.removeEventListener('mouseup', M_fnCloseIfNotInMenu, false);
		document.body.removeEventListener('keydown', M_fnCheckKeyDown, false);
	}   	
}

// Open one menu(submenu), if there exists any other child menu, child menu
// will be closed.
function M_pfnOpenMenu(i_objItem)
{
	// to prevent when mouse moves onto an arrow
	if ( (i_objItem.m_bIsHighlighted || i_objItem == M_objHighliteItem) && !i_objItem.m_bNoDelay)
	{
		return;
	}
	if (i_objItem.m_objContainer.m_nDepth < M_objCurrentMenu.m_nDepth) {
		M_pfnCloseChildren(i_objItem.m_objContainer);
		M_objCurrentMenu = i_objItem.m_objContainer;
	}
	M_pfnRemoveHighLight(i_objItem.m_objContainer, M_objHighliteItem);
	if (!i_objItem.m_bIsActive) {
		return;
	}
	if (i_objItem.className == "hrr") {
		M_objHighliteItem = null;
	}
	else {
		M_objHighliteItem = i_objItem;
		i_objItem.m_bIsHighlighted = true;
		i_objItem.className = "menuItemOver";
		M_pfnKillTimer(M_objTimerId);
		if (i_objItem.m_objChildMenu) {
			if (i_objItem.m_bNoDelay) {
				M_pfnDelayOpenChildren(false);
			}
			else {
				M_objTimerId = setTimeout("M_pfnDelayOpenChildren(false)", M_nDelayTime);
			}
		}
	}
}

function M_pfnFormatStyle(i_nInt)
{
	return i_nInt.toString() + 'px';
}

function M_pfnGetNewPos()
{
	var objSrcObject;

	if (M_bDomOk) {
		objSrcObject= document.getElementById(M_strBtnId);
		M_objTopContainer = document.getElementById(M_strBtnDivId);
		M_nOrgLeft = M_pfnCalculateX(M_objTopContainer) + M_nBorder;
		M_nOrgTop = M_pfnCalculateY(M_objTopContainer) + objSrcObject.offsetHeight + M_nBorder;
	}
	else {
		//non dom support
		alert("not supported");
		return;
	}
}

// IE has no control over the z index order of select control, so select control 
// will always "bleed" through whatever on top of it. Netscape7 doesn't have the 
// similar problem. This function is just a very specific work around for displaying
// the chart. To finally fix the problem,  we need to move the select control to
// somewhere else. 

function M_pfnToggleIEControls(i_bShow)
{
	var objControl;
	var nIndex;
	var strVisibility = 'hidden';

	if(i_bShow) {
		strVisibility = 'visible';
	}
	if (PWdsapp_bIsIE) {
		for (nIndex = 0; nIndex < M_nMaxDim; nIndex++) {
			objControl = document.getElementById("WD_Dim" + (nIndex + 1).toString());
			if (objControl) {
				objControl.style.visibility = strVisibility;
			}
		}
	}
}

//Find the padding value for a specific class
function M_pfnInitPadding(i_strClassName)
{
	var nClassIndex = getStyleSheetRuleIndex(i_strClassName);

	// If the browser supports the W3C DOM way, use cssRules.
	if (document.styleSheets[0].cssRules) {
		M_nPaddingLeft = parseInt(document.styleSheets[0].cssRules[nClassIndex].style.paddingLeft);
		M_nPaddingRight = parseInt(document.styleSheets[0].cssRules[nClassIndex].style.paddingRight);
	}
	// If it supports the Microsoft way, use rules.
	else if (document.styleSheets[0].rules) {
		M_nPaddingLeft = document.styleSheets[0].rules[nClassIndex].style.paddingLeft;
		M_nPaddingRight = document.styleSheets[0].rules[nClassIndex].style.paddingRight;
	}
}

function M_pfnRemoveHighLight(i_oContainer, i_objHighlitItem)
{
	if (i_objHighlitItem != null) {
		if (i_oContainer != i_objHighlitItem.m_objChildMenu) {
			if (i_objHighlitItem.m_bIsActive) {
				i_objHighlitItem.className = "menuItem";
			}
			else {
				i_objHighlitItem.className = "menuItemInActive";
			}
			i_objHighlitItem.m_bIsHighlighted = false;
			i_objHighlitItem = null;
		}
	}
}

function initMenu()
{   
	M_pfnCancelClickEvent();
	M_pfnGetNewPos();
	if (M_objMenu == null) {
		M_objMenu = M_pfnCreateMenu(null, null, "main_menu", M_nOrgTop, M_nOrgLeft);
		if (M_objMenu == null) {
			return;
		}
		M_pfnDisplayMenu();
	}
	else {
	   if (M_pfnDuplicatedHandler()) return;
	   M_pfnEvalProc("M_pfnCloseOrOpen()");
	}
	M_objCurrentMenu = M_objMenu;
}

function CloseActMenuOnResize()
{
	if (M_objMenu == null || !M_bLoaded) {
		return;
	}
	else {
		M_fnCloseAll(M_objMenu);
	}
}

// function used only for Netscape to test if onClick event is handled both my
// initMenu and M_fnCheckKeyDown() function
function M_pfnDuplicatedHandler()
{
   return (!PWdsapp_bIsIE && M_objTimerNscp && M_bInKeyHandler);   
}

function M_pfnDisplayMenu()
{
	HideAllMenus(); //close any dropdowns such as languages, useful links, alternatives to the Actions Menu
   M_objMenu.style.top = M_pfnFormatStyle(M_nOrgTop);
   M_objMenu.style.left = M_pfnFormatStyle(M_nOrgLeft);
   M_bLoaded = true;
   M_pfnToggleIEControls(false);
   M_pfnToggleBtnImage(false);
   M_pfnInitEventHandler();
   M_pfnShowMenu(M_objMenu);
}

function M_pfnCloseOrOpen()
{
   if (M_bLoaded) {
      M_fnCloseAll();
   }
   else {
      M_pfnDisplayMenu();
   }
}

function M_pfnShowMenu(i_objMainMenu)
{
	var objItem;

	objItem = i_objMainMenu.m_oLastItem;
	M_nDepth++;
	i_objMainMenu.style.zIndex = M_nZIndex;
	i_objMainMenu.style.visibility = M_nDepth ==1 ? 'visible':'hidden';
	while(objItem != null){
		if(objItem.m_objChildMenu) {
			M_pfnShowMenu(objItem.m_objChildMenu);
		}
		objItem=objItem.m_oPrevItem;
	}
	M_nDepth--;
}

function M_pfnMenuInit(i_objParentMenu, i_objParentItem, i_nDepth, i_nId)
{
	this.m_oFirstItem = null;
	this.m_oLastItem = null;
	this.m_oParentMenu = i_objParentMenu;
	this.m_oParentItem = i_objParentItem;
	this.className="menu";
	this.m_nDepth = i_nDepth;
	this.m_nId = i_nId;
}

function M_pfnItemInit(i_objContainer, i_strArrayName, i_nStartPos, i_objPrevItem)
{
	var aArray;
	var bCheck;
	var bInactive;
	var nElementsWidth = 0;
	var nWidth = 0;
	var strLabel;
	var strLink;
	var strSubMenu;
	var objCheckMark = null;
	var objLabelNode = null;
	var objNode;
	var objTextNode = null;
	var unImgTop;
	var unImgRight;

	aArray=eval(i_strArrayName);
	strLabel = aArray[i_nStartPos + M_LabelPos];
	strSubMenu = aArray[i_nStartPos + M_SubMenuPos];
	bCheck = parseInt(aArray[i_nStartPos + M_CheckPos]) != 0;
	bInactive = parseInt(aArray[i_nStartPos + M_nInActive]) != 0;

	this.m_bIsHighlighted = false;
	this.m_bNoDelay = false;
	this.m_nIndex = i_nStartPos / M_GroupSize;
	this.m_nLevel = M_nDepth;
	this.m_bIsSeparator = false;
	this.m_bIsActive = true;
	this.className = "menuItem";
	if (bInactive) {
		this.className="menuItemInActive";
		this.m_bIsActive = false;
	}

	//this parameter is created programmatically from ASP code, make sure the string
	// trims off the blanks in ASP code.
	this.m_strLink = unescape(aArray[i_nStartPos + M_LinkPos]);
	this.style.left= "0px";
	this.m_objContainer = i_objContainer;
	this.m_oPrevItem = i_objPrevItem;

	if (M_bDomOk) {
		if (strLabel == '_HR_') {
			this.className="hrr";
			this.m_bIsSeparator = true;
			objNode = document.createElement('img');
			objNode.setAttribute('src',G_strRootPath + M_astrImgSrc[M_nSeparator]);
			objNode.setAttribute('height','2');			
			objNode.style.position='absolute';
			objNode.style.visibility = 'inherit';

			//IE and Netscape have different ways of positioning an hr tag.
			if (PWdsapp_bIsIE) {
				this.style.paddingTop = '3px';
				this.style.paddingBottom = '7px';
			}
			else {
				this.style.paddingTop = '3px';
				this.style.paddingBottom = '5px';
			}
			this.appendChild(objNode);
		}
		else {
			if (bCheck) {
				objCheckMark = document.createElement('img');
				objCheckMark.style.position='absolute';
				objCheckMark.style.visibility='inherit';
				objCheckMark.setAttribute('src', G_strRootPath + M_astrImgSrc[M_nCheckMk]);
				objCheckMark.style.left = M_pfnFormatStyle(3 * M_nBorder);
				this.appendChild(objCheckMark);
			}
			objLabelNode = document.createElement('span');
			this.appendChild(objLabelNode);
			if (PWdsapp_eBrowser == GJSSYS_BROWSER_MS50) 
			{
				objLabelNode = document.createElement('nobr');
				this.appendChild(objLabelNode);
			}
			objLabelNode.style.visibility='inherit';
			objTextNode = document.createTextNode(strLabel);
			objLabelNode.appendChild(objTextNode);
			// Netscape always returns 0 for offsetHeight and image's height unless after the 
			// text node has been inserted into the document.
			if (objCheckMark != null) {
				// The problem seems to happen if we try to get the icon height before it is loaded.
				// To fix the problem, Check if the parent's height is smaller than 
				// the image's height, use parent's height and parent's width to calculate 
				// image's top and image's right. 
				if (this.offsetHeight > objCheckMark.height) {
					unImgTop = (this.offsetHeight - objCheckMark.height) / 2;
				}
				else {
					unImgTop = this.offsetHeight / 4;
				}
				objCheckMark.style.top = M_pfnFormatStyle(unImgTop);
			}
			if (strSubMenu != "") {
				objNode = document.createElement('img');
				objLabelNode.appendChild(objNode);
				objNode.style.position='absolute';
				objNode.style.visibility='inherit';
				objNode.setAttribute('src', G_strRootPath + M_astrImgSrc[0]);
				// The problem seems to happen if we try to get the icon height before it is loaded.
				// To fix the problem, Check if the parent's height is smaller than 
				// the image's height, use parent's height and parent's width to calculate 
				// image's top and image's right. 
				if (this.offsetHeight > objNode.height) {
					unImgTop = (this.offsetHeight - objNode.height) / 2;
					unImgRight = objNode.width;
				}
				else {
					unImgTop = this.offsetHeight / 4;
					unImgRight = this.offsetLeft + 5;
				}
				objNode.style.top = M_pfnFormatStyle(unImgTop);
				objNode.style.right = M_pfnFormatStyle(unImgRight);
			}
		}
		if(PWdsapp_bIsIE){
			this.onmouseover=fnOpenMenu;
			this.onmouseout=M_fnCloseMenu;
			this.onclick=M_fnClickOnItem;
		}
		else {
			this.addEventListener('mouseover',fnOpenMenu,false);		
			this.addEventListener('click',M_fnClickOnItem,true);
			this.addEventListener('mouseout',M_fnCloseMenu,false);
		}
	}
	else {
		alert("not supported");
	}
	if (M_nPaddingLeft < 0 && M_nPaddingRight < 0) {
		M_pfnInitPadding(M_strSepClass);
	}	
	if (PWdsapp_bIsIE) {
		nWidth = this.offsetWidth;
	}
	else {
		if (strLabel != '_HR_') {
			nWidth = objLabelNode.offsetWidth + M_nPaddingLeft + M_nPaddingRight;
		}
	}
	if (M_nMaxMenuWidth[i_objContainer.m_nId] < nWidth ) {
		M_nMaxMenuWidth[i_objContainer.m_nId] = nWidth;
	}
	this.m_nHeight = this.offsetHeight;
	this.style.top= M_pfnFormatStyle(M_aTopPos[M_nDepth - 1]);
	M_aTopPos[M_nDepth - 1] += this.offsetHeight;
}

function M_fnCheckKeyDown(evt)
{
   var bHandled = false;
	var nKeyCode;

	if (M_objMenu == null || !M_bLoaded) {
		return;
	}
	if (!evt)
		evt = window.event;

	if (PWdsapp_bIsIE) {
		nKeyCode = window.event.keyCode;
	}
	else if (M_bDomOk) {
		nKeyCode = evt.which;
	}
	else {
		alert("not supported");
	}
	// handle escape key stroke.
	if (nKeyCode == 27) {
		if (M_objCurrentMenu) {
			M_pfnCloseCurrent();
			if (M_objCurrentMenu == null) {
				M_bLoaded = false;
				M_pfnToggleIEControls(true);
				M_pfnToggleBtnImage(true);
            M_pfnClearEventHandler();
            M_bInKeyHandler = false;
			}
         bHandled = true;
		}
	}
	else if (evt.altKey) {
      M_fnCloseAll();
	   bHandled = true;
	}
	else if (nKeyCode == KeyCodeARROWLEFT && !evt.altKey) {
      // handle left key
      M_pfnArrowLeftRightKeyHandler(true);
      bHandled = true;
	}
	else if (nKeyCode == KeyCodeARROWRIGHT && !evt.altKey) {
	   // handle right key
	   M_pfnArrowLeftRightKeyHandler(false);
      bHandled = true;
	}	
	else if (nKeyCode == KeyCodeARROWUP && !evt.altKey) {
	   // handle up key
	   M_pfnArrowUpDownKeyHandler(true);
      bHandled = true;
	}
	else if (nKeyCode == KeyCodeARROWDOWN && !evt.altKey) {
	   // handle down key
	   M_pfnArrowUpDownKeyHandler(false);
      bHandled = true;
	}
	else if (nKeyCode == KeyCodeEnter && !evt.altKey) {
	   // handle enter key
      M_bInKeyHandler = true;
      M_fnEnterKeyHandler();
      bHandled = true;
	}
	if (bHandled) {
	   M_pfnCancelEventBubble(evt);
	}
}

function M_pfnArrowLeftRightKeyHandler(i_bIsLeft) 
{
   if (M_objCurrentMenu) {
      if (!i_bIsLeft) {
         i_bIsLeft = false;
      }
      if (i_bIsLeft) {
         // left arrow
         if (M_objCurrentMenu.m_nDepth > 1) {
            M_pfnCloseCurrent();
         }         
      }
      else {
         //right arrow
         if (M_objHighliteItem) {
            if (M_objHighliteItem.m_objChildMenu && (M_objCurrentMenu.m_nDepth == M_objHighliteItem.m_nLevel))
            {
               M_pfnDelayOpenChildren(true);
            }
         }         
      }
   }   
}

// keydown event handler for left and right arrow key
function M_pfnArrowUpDownKeyHandler(i_bIsUpKey)
{
   var oItem;
	if (M_objCurrentMenu) {
	   M_pfnKillTimer(M_objTimerId);
	   oItem = M_pfnFindNextHighlightItem(i_bIsUpKey);
	   if (M_objHighliteItem != null && M_objHighliteItem.style.visibility == 'visible')
	   {
		   M_objHighliteItemclassName="menuItem";
	   }
	   if (oItem) {
	      oItem.m_bIsHighlighted = true;
	      oItem.className="menuItemOver";
      }
      M_objHighliteItem = oItem;
   }
}

function M_fnEnterKeyHandler()
{
   if (M_objCurrentMenu) {
      if (M_objHighliteItem && M_objCurrentMenu.m_nDepth == M_objHighliteItem.m_nLevel) {
         // there is item of current menu is shown
         if (M_objHighliteItem.m_objChildMenu) {
            M_pfnKillTimer(M_objTimerId);
            M_pfnEvalProc("M_pfnDelayOpenChildren(true)");
         }
         else {
            //it is a command, run it.
            M_pfnEvalProc("M_pfnExecuteCommand(M_objHighliteItem)");
         }
      }
      else {         
         // current menu has nothing highlighted, enter key will close all menu    
         M_pfnEvalProc("M_fnCloseAll()");
      }
   }
}

// Utility function for calling a function with name i_strFuncName
function M_pfnEvalProc(i_strFuncName)
{
   if (i_strFuncName == null) {
      alert("Fatal Error: Empty Function Name");
      return;
   }
   else {
      if (PWdsapp_bIsIE) {
         eval(i_strFuncName);         
      }
      else {
         M_pfnKillTimer(M_objTimerNscp);
         M_objTimerNscp = setTimeout(i_strFuncName, M_nDelayTimeNscp);
      }
   }      
}

// To find next item which can be highlighted
function M_pfnFindNextHighlightItem(i_bMoveUp)
{
   var bIsTopMenuHighlighted;
   var oItem = null;
   var oContainer = null;
   
   if (!i_bMoveUp) {
      i_bMoveUp = false;
   }
   bNotTopMenuHighlighted = !M_objHighliteItem || M_objHighliteItem.m_nLevel < M_objCurrentMenu.m_oFirstItem.m_nLevel;
   if (bNotTopMenuHighlighted) {
      oContainer = M_objCurrentMenu;
   }
   else {
      oContainer = M_objHighliteItem.m_objContainer;
   }
   if (i_bMoveUp) {
	   if (!bNotTopMenuHighlighted){
         oItem = M_objHighliteItem.m_oPrevItem;
         M_pfnRemoveHighLight(oContainer, M_objHighliteItem);
	   }
      if (!oItem) {
         oItem = oContainer.m_oLastItem;
      }
	   while (oItem.m_bIsSeparator)
	   {
         oItem = oItem.m_oPrevItem;
      }
   }
   else {
	   if (!bNotTopMenuHighlighted)
	   {
         oItem = M_objHighliteItem.m_oNextItem;
         M_pfnRemoveHighLight(oContainer, M_objHighliteItem);
	   }
      if (!oItem) {
         oItem = oContainer.m_oFirstItem;
      }
	   while (oItem.m_bIsSeparator)
	   {
         oItem = oItem.m_oNextItem;
      }   
   }
   return oItem;
}

function M_pfnCreateMenu(i_objParentMenu, i_objParentItem, i_strArrayName, i_nTopPos, i_nRightPos)
{
	var aArray;
	var nIndex;
	var nTopPos;
	var nRightPos;
	var objMenu;
	var objItem;
	var objPrevItem = null;

	try {
		aArray = eval(i_strArrayName);
	}
	catch(e){
		alert("Fatal Error: menu name doesn't exist");
		return null;
	}
	M_aTopPos[M_nDepth] = M_nBorder;
	M_nDepth++;
	if(M_bDomOk){      
		objMenu=document.createElement("div");
		objMenu.style.position='absolute';
		objMenu.style.visibility='hidden';
		objMenu.M_pfnMenuInit = M_pfnMenuInit;
		objMenu.M_pfnMenuInit(i_objParentMenu, i_objParentItem, M_nDepth, M_nMaxMenuWidth.length);
		document.body.appendChild(objMenu);
	}
	M_nMaxMenuWidth[M_nMaxMenuWidth.length] = 0;
	for (nIndex = 0; nIndex < aArray.length; nIndex=nIndex + M_GroupSize)
	{
		if (M_bDomOk) {
			objItem = document.createElement("div");
			objItem.style.position='absolute';
			objItem.style.left='0px';
			objItem.id="item" + nIndex;
			objItem.style.visibility='inherit';
			objMenu.appendChild(objItem);
		}
		else {
			// for non DOM support
		}
		if (nIndex == 0) {
			objMenu.m_oFirstItem = objItem;
		}
		else {
			objPrevItem.m_oNextItem = objItem;
		}
		objItem.M_fnItemInit = M_pfnItemInit;
		objItem.M_fnItemInit(objMenu, i_strArrayName, nIndex, objPrevItem);
		if (aArray[nIndex + M_SubMenuPos] != "") {
			nTopPos = i_nTopPos + M_aTopPos[M_nDepth - 1] - objItem.m_nHeight;
			objItem.m_objChildMenu = M_pfnCreateMenu(objMenu, objItem, aArray[nIndex + M_SubMenuPos], nTopPos, i_nRightPos);
			if (objItem.m_objChildMenu == null) return;
			objItem.m_objChildMenu.style.top = M_pfnFormatStyle(parseInt(objItem.style.top) + i_nTopPos);
		}
		objPrevItem = objItem;
	}
	objMenu.m_oLastItem = objItem;
	objMenu.style.height = M_pfnFormatStyle(M_aTopPos[M_nDepth - 1] + 2 * M_nBorder);
	objMenu.style.width = M_pfnFormatStyle(M_nMaxMenuWidth[objMenu.m_nId]);
	objMenu.style.top = M_pfnFormatStyle(M_aTopPos[M_nDepth - 1] - M_nBorder * (M_nDepth - 1));
	M_pfnAdjustWidth(objMenu);
	M_nDepth--;
	return objMenu;
}

function M_pfnAdjustWidth(i_objMenu)
{
	var nWidth;
	var objItem = i_objMenu.m_oLastItem;

	while(objItem) {
		objItem.style.width = i_objMenu.style.width;
		if (!PWdsapp_bIsIE) {
			objItem.style.width = M_pfnFormatStyle(parseInt(objItem.style.width) - M_nPaddingLeft - M_nPaddingRight);
		}
		if (objItem.className == "hrr") {
			nWidth = parseInt(objItem.style.width)
			if (PWdsapp_bIsIE) {
				nWidth = nWidth - parseInt(M_nPaddingLeft) - parseInt(M_nPaddingLeft);
			}
			objTemp = objItem.firstChild;
			objTemp.setAttribute('width', nWidth);
		}
		objItem = objItem.m_oPrevItem;
	}
	i_objMenu.style.width = M_pfnFormatStyle(parseInt(i_objMenu.style.width) + 2 * M_nBorder);
}

function M_fnCloseAll(i_oEvent)
{  
	M_pfnToggleIEControls(true);
	if (M_pfnCancelClickEvent(i_oEvent)) {
		M_bLoaded = false;
		if (M_objMenu == null) return;
		M_pfnCloseChildren(M_objMenu);
		M_objMenu.style.visibility='hidden';
		
		M_bLoaded = false;
		M_pfnToggleBtnImage(true);
	}
	M_pfnClearEventHandler();
   M_bInKeyHandler = false;
}

function M_pfnCloseChildren(i_oCurrentMenu)
{
	var objItem;

	objItem = i_oCurrentMenu.m_oLastItem;
	while (objItem){
		if(objItem.m_objChildMenu){
         M_pfnCloseChildren(objItem.m_objChildMenu);
			objItem.m_objChildMenu.style.visibility = 'hidden';
		}	
		if (objItem.m_bIsHighlighted) {
			objItem.m_bIsHighlighted = false;
			M_objHighliteItem = null;
			objItem.className="menuItem";
			if (!objItem.m_bIsActive) {
				objItem.className="menuItemInActive";
			}
		}
		objItem = objItem.m_oPrevItem;
	}
}

function M_pfnCloseCurrent()
{
   M_pfnCloseChildren(M_objCurrentMenu);
   M_objCurrentMenu.style.visibility = 'hidden';
   M_objHighliteItem = M_objCurrentMenu.m_oParentItem;
   M_objCurrentMenu = M_objCurrentMenu.m_oParentMenu;
}

function fnOpenMenu() 
{
	M_pfnOpenMenu(this);
}

function M_pfnDelayOpenChildren(i_bHighlightFirstItem)
{
	var nWidth;
	var objContainer;
	// open a child menu here
	if (M_objHighliteItem != null && M_objHighliteItem.m_bIsHighlighted) {
		objContainer = M_objHighliteItem.m_objContainer;
		nWidth = parseInt(objContainer.style.width);
		if (PWdsapp_bIsIE) {
			nWidth -= M_nBorder;
		}
		M_objHighliteItem.m_objChildMenu.style.left = M_pfnFormatStyle(parseInt(objContainer.style.left) + nWidth);
		M_objCurrentMenu = M_objHighliteItem.m_objChildMenu;
      M_pfnShowMenu(M_objHighliteItem.m_objChildMenu);
		if (i_bHighlightFirstItem) {
			M_objCurrentMenu.m_oFirstItem.className = 'menuItemOver';
			M_objCurrentMenu.m_oFirstItem.m_bIsHighlighted = true;
			M_objHighliteItem = M_objCurrentMenu.m_oFirstItem;
		}
	}
}

// This function is called for handling mouse out event, when arrow image is 
// showing, mouseout event is fired as well, to eliminate the unnecessary closing
// of menu, both close and open menu function is perfermed in M_pfnOpenMenu function.
function M_fnCloseMenu()
{
	if (this.className == "hrr") {
		return;
	}
}

function M_fnClickOnItem(evt)
{
   M_pfnCancelEventBubble(evt);
   M_pfnExecuteCommand(this);
}

function M_pfnCancelEventBubble(evt)
{
   if (!evt) 
      evt = window.event; 
      
	// We've handled this event. Don't let anybody else see it.
	if( evt.stopPropagation ) {
		evt.stopPropagation();		// DOM Level 2
	}
	else 
		evt.cancelBubble = true;		// IE

	// Now prevent any default action.
	if( evt.preventDefault )
		evt.preventDefault();			// DOM Level 2
	else
		evt.returnValue = false;		// IE	
 }
 
function M_pfnExecuteCommand(i_oItem)
{
	if (i_oItem.className == "hrr") {
	   return true;
	}
	if (i_oItem.m_strLink !="") {
      if (i_oItem.m_bIsActive) {
			eval(i_oItem.m_strLink);
		}
      M_fnCloseAll();
	}
	else {
		i_oItem.m_bNoDelay = true;
		M_pfnOpenMenu(i_oItem);
		i_oItem.m_bNoDelay = false;
	}
}

function M_pfnKillTimer(i_oTimerId)
{
	if (i_oTimerId != null) {
		clearTimeout(i_oTimerId);
		i_oTimerId = null;
   }
}

// Check to see if the menu should be closed because of a click outside the menu.
function M_fnCloseIfNotInMenu(i_oEvent)
{ 
	// Determine what was clicked and if it was the Action menu button
	var clickElem = PWdsapp_bIsIE ? window.event.srcElement : i_oEvent.target;
	var bInMenu = clickElem.id == M_strBtnId;

	// Loop until we find that the clicked item is the menu,
	// moving up from the clicked it to parent, grandparent, etc.
	while( !bInMenu && clickElem != null )
	{
		bInMenu = M_objMenu == clickElem;
		clickElem = clickElem.parentNode;
	}

	// If the clicked item was not the Action menu button, 
	// nor the menu itself, nor a child of the menu, close the menu.
	if( !bInMenu )
		M_fnCloseAll( i_oEvent );
}
