var kfnDisableEvent = new Function("return false");
var _arrIFrame = new Array();
var _arrWnd = new Array();
var _arrSelected = new Array();
var _nLevel = 0;
var _oRoot = null;
var _fIsRtL = false;
var _wzMGradPath = "";
var _wzMGradPathRtL = "";
var _wzChkMrkPath = "";
var _wzMArrPath = "";
var _wzMArrPathRtL = "";
var _wzPrefixID = "";
var L_Menu_BaseImg = "images/menu/"
var L_Menu_BaseUrl = "client/";
var bodyClickAction = "";
var isOpening = false;

function initMenu(maxLevel){
	for (var nLevel=0; nLevel<maxLevel; nLevel++)
		if (browseris.ie && browseris.win32)
			document.write('<IFRAME FRAMEBORDER="0" SRC="' + L_Menu_BaseUrl + 'iframe.htm" SCROLLING="no" style="position:absolute;display:none;background:white;" ID="ifrmWind_' + nLevel + '"></IFRAME>');
		else
			document.write('<IFRAME FRAMEBORDER="0" visibility="hide" SRC="' + L_Menu_BaseUrl + 'iframe.htm" SCROLLING="no" style="position:absolute;display:none;background:white;" ID="ifrmWind_' + nLevel + '"></IFRAME>');
}

function registerToOpenMenu(srcId, menuSrcId, isGroupMenu, isWhenOver){
	var srcItem = getElement(srcId, 'id');
	if (srcItem && menuSrcId){
		if (isGroupMenu == null) isGroupMenu = false;
		if (browseris.ie && browseris.win32){
			srcItem.onclick = new Function("ShowMenu('" + menuSrcId + "', this, 0)");
			if (isWhenOver){
			    srcItem.onmouseover = new Function("ShowMenu('" + menuSrcId + "', this, 0)");
			}else if (isGroupMenu){
				srcItem.onmouseover = new Function("ShowMenuWhenOver('" + menuSrcId + "', this, 0)");
			}
		}else{
			setAttribute(srcItem, "MenuID", menuSrcId);
			srcItem.addEventListener('click', ShowMenuNS, false);
			if (isWhenOver){
			    srcItem.addEventListener('mouseover', ShowMenuNS, false);
			}else if (isGroupMenu){
				srcItem.addEventListener('mouseover', ShowMenuWhenOverNS, false);
			}
		}
	}
}

function setChecked(menuItem, checked){
	alert(menuItem)
}

function WindowPosition(elt){
	var pos = new Object;
	pos.x = 0;
	pos.y = 0;
	while (elt.offsetParent){
		pos.x += elt.offsetLeft;
		pos.y += elt.offsetTop;
		elt = elt.offsetParent;
	}
	return pos;
}

function WindowPosition2(elt){
	var pos = new Object;
    pos.x = 0;
    pos.y = 0;
    
    if (elt.offsetParent) {
        while (elt) {
            pos.x += elt.offsetLeft;
            pos.y += elt.offsetTop;
            
            elt = elt.offsetParent;
        }
    } else if (elt.x) {
        pos.x += elt.x;
        pos.x += elt.y;
    }
    
	return pos;
}

function PrepContents(srcMenu){
	_oRoot = document.createElement("span");
	_oRoot.innerHTML = srcMenu.innerHTML;
	
	_wzMGradPath = L_Menu_BaseImg + "/menu_ground.gif";
	_wzMGradPathRtL = L_Menu_BaseImg + "/menu_ground_rtl.gif";
	_wzChkMrkPath = L_Menu_BaseImg + "/menu_check.gif";
	_wzMArrPath = L_Menu_BaseImg + "/menu_arrow.gif";
	_wzMArrPathRtL = L_Menu_BaseImg + "/menu_arrow_rtl.gif";
	
	if (L_Menu_BaseUrl){
		_wzMGradPath = L_Menu_BaseUrl + _wzMGradPath;
		_wzMGradPathRtL = L_Menu_BaseUrl + _wzMGradPathRtL;
		_wzChkMrkPath = L_Menu_BaseUrl + _wzChkMrkPath;
		_wzMArrPath = L_Menu_BaseUrl + _wzMArrPath;
		_wzMArrPathRtL = L_Menu_BaseUrl + _wzMArrPathRtL;
	}
}

function ShowMenuNS(e){
	e.cancelBubble = true;
	var elt = e.currentTarget;
	if (elt){
		var srcId = getAttribute(elt, "MenuID");
		ShowMenu(srcId, elt);
	}
}

function ShowMenuWhenOverNS(e){
	if (isOpening)
		ShowMenuNS(e);
}

function ShowMenuWhenOver(srcId, elt){
	if (isOpening)
		ShowMenu(srcId, elt);
}

function ShowMenu(srcId, elt){
	_wzPrefixID = "Menu_" + srcId;
	if (isOpening) HideMenu();
	var srcMenu = getElement(srcId, 'id');
	PrepContents(srcMenu);
	Show(elt, 0, 0);
	
	bodyClickAction = getAttribute(document.body, "onclick");
	if (browseris.ie && browseris.win32){
		window.event.cancelBubble = true;
		document.body.onclick = HideMenu;
	}else{
		//document.body.addEventListener('click', HideMenu, false);
		setAttribute(document.body, "onclick", "HideMenu()");
	}
	isOpening = true;
	callWhenShow(srcMenu, elt);
}

function ShowSubMenu(nLevel, oParent){
	if (!oParent) return;
	if (browseris.ie4up && browseris.win32)
		_oRoot = oParent.submenuRoot;
	else
		_oRoot.innerHTML = getAttribute(oParent, "submenuRoot");
	_nLevel = nLevel + 1;
	Show(oParent, -1, _nLevel);
}

function Show(oParent, y, nLevel){
	var oPopupDoc = null;
	var oPopupBody = null;
	
	var pos = WindowPosition(oParent);
	_arrIFrame[nLevel] = document.getElementById('ifrmWind_' + nLevel);
	if (!_arrIFrame[nLevel]) return false;
	
	if (browseris.ie4up && browseris.win32){
		_arrWnd[nLevel] = eval('ifrmWind_' + nLevel);
		oPopupDoc = _arrWnd[nLevel].document;
	}else{
		_arrWnd[nLevel] = _arrIFrame[nLevel];
		oPopupDoc = _arrIFrame[nLevel].contentDocument;
	}
	oPopupBody = oPopupDoc.body;
	oPopupBody.innerHTML = "";
	
	var oTable = oPopupDoc.createElement("table");
	var oTBody = oPopupDoc.createElement("tbody");
	oTable.appendChild(oTBody);
	oPopupBody.appendChild(oTable);
	var lastMenuSeparatorRow = null;
	var children = null;
	var length = 0;
	
	if (browseris.ie4up && browseris.win32){
		menuItems = _oRoot.children;
		length = menuItems.length;
	}else{
		menuItems = _oRoot.childNodes;
	}
	
	for (nIndex = 0; nIndex < menuItems.length; nIndex++){
		var menuRow = null;
		if (browseris.ie4up && browseris.win32)
			menuRow = menuItems[nIndex];
		else
			menuRow = getChildElementByIndex(_oRoot, "PQP:MENUITEM", nIndex);
		
		if (menuRow){
			var deleteRow = false;
			if (!isVisibility(menuRow)){
				deleteRow = true;
			}else if (FIsIType(menuRow, "separator")){
				if (lastMenuSeparatorRow != null || nIndex == 0)
					deleteRow = true;
				else
					lastMenuSeparatorRow = menuRow;
			}else{
				lastMenuSeparatorRow = null;
			}
			if (deleteRow == true){
				menuRow.removeNode(true);
				nIndex--;
			}
		}
	}
	
	if(lastMenuSeparatorRow != null)
		lastMenuSeparatorRow.removeNode(true);
	
	var id = 0;
	//var oScollUp = CreateMenuScroll(oPopupDoc, nLevel);
	//oTBody.appendChild(oScollUp);
	//var oEmptyUp = CreateEmptyMenu(oPopupDoc);
	//oTBody.appendChild(oEmptyUp);
	
	for (nIndex = 0; nIndex < menuItems.length; nIndex++){
		var oNode = null;
		if (browseris.ie4up && browseris.win32)
			oNode = menuItems[nIndex];
		else
			oNode = getChildElementByIndex(_oRoot, "PQP:MENUITEM", nIndex);
		
		if (oNode){
			if (!FIsIType(oNode, "label")){
				var oItem = CreateMenuItem(oPopupDoc, oNode, MakeID(nLevel, id), nLevel);
				if (oItem) oTBody.appendChild(oItem);
				id++;
			}
		}
	}
	
	oTable.className = "ms-MenuUI";
	setAttribute(oTable, "cellSpacing", "0");
	setAttribute(oTable, "cellPadding", "0");
	if (browseris.ie4up && browseris.win32){
		//oPopupBody.style.setExpression("overflow", "offsetHeight < window.screen.height && offsetWidth < window.screen.width ? 'visible' : 'auto'");
		oPopupBody.className = "ms-MenuUIPopupBody";
	}else{
		//oPopup.style.width = oTable.offsetWidth;
		oPopupBody.className = "ms-MenuUIPopupBody";
		//oPopup.style.height = oTable.offsetHeight;
	}
	
	var wzImgSrc, wzImgPosX;
	if (!_fIsRtL){
		wzImgSrc = _wzMGradPath;
		wzImgPosX = "left";
	}else{
		wzImgSrc = _wzMGradPathRtL;
		wzImgPosX = "right";
	}
	oTable.style.backgroundImage = "url(" + wzImgSrc + ")";
	oTable.style.backgroundPositionX = wzImgPosX;
	oTable.style.backgroundRepeat = "repeat-y"
	if (browseris.ie && browseris.win32){
		oPopupBody.oncontextmenu = kfnDisableEvent;
		oPopupBody.ondragstart = kfnDisableEvent;
		oPopupBody.onselectstart = kfnDisableEvent;
	}else{
		oPopupBody.addEventListener('contextmenu', kfnDisableEvent, false);
		oPopupBody.addEventListener('dragstart', kfnDisableEvent, false);
		oPopupBody.addEventListener('selectstart', kfnDisableEvent, false);
	}
	if (browseris.ie4up && browseris.win32){
		_arrIFrame[nLevel].style.pixelWidth = 24;
		_arrIFrame[nLevel].style.pixelHeight = 16;
	}else{
		_arrIFrame[nLevel].style.width = 24;
		_arrIFrame[nLevel].style.height = 16;
	}
	
	_arrIFrame[nLevel].style.position = "absolute";
	setVisibility(_arrIFrame[nLevel], true);
	
	var nRealWidth = oTable.offsetWidth;
	var nRealHeight = oTable.offsetHeight;
	if (browseris.ie4up && browseris.win32){
		_arrIFrame[nLevel].style.pixelWidth = nRealWidth;
		_arrIFrame[nLevel].style.pixelHeight = nRealHeight;
	}else{
		_arrIFrame[nLevel].style.width = nRealWidth;
		_arrIFrame[nLevel].style.height = nRealHeight;
	}
	
	var parentAlign = "left2right";		// tu trai qua phai
	var EdgeLeft = 0; //window.screenLeft;
	var EdgeTop = 0;
	var EdgeRight = EdgeLeft + window.document.body.clientWidth;
	var EdgeBottom = EdgeTop + window.document.body.clientHeight;
	var tmpX = pos.x;
	var tmpY = pos.y;
	
	var ParentLeft = pos.x;
	var ParentTop = pos.y;
	var ParentWidth = 0;
	if (nLevel > 0){
		parentAlign = getAttribute(_arrIFrame[nLevel-1], "menuAlign");
		
		if (browseris.ie4up && browseris.win32){
			ParentLeft = parseInt(_arrIFrame[nLevel-1].style.pixelLeft);
			ParentTop = parseInt(_arrIFrame[nLevel-1].style.pixelTop);
			ParentWidth = parseInt(_arrIFrame[nLevel-1].style.pixelWidth);
		}else{
			ParentLeft = parseInt(_arrIFrame[nLevel-1].style.left);
			ParentTop = parseInt(_arrIFrame[nLevel-1].style.top);
			ParentWidth = parseInt(_arrIFrame[nLevel-1].style.width);
		}
		
		tmpY = ParentTop + tmpY;
		if (parentAlign == "left2right")
			tmpX = ParentLeft + ParentWidth + 1;
		else
			tmpX = ParentLeft - nRealWidth - 1;
	}else{
		tmpY = tmpY + oParent.offsetHeight;
	}
	
	if (EdgeRight < (tmpX + nRealWidth)){
		if ((ParentLeft - nRealWidth) >= EdgeLeft){
			tmpX = ParentLeft - nRealWidth - 1;
			if (nLevel == 0) tmpX = tmpX + oParent.offsetWidth;
			parentAlign = "right2left";
		}
	}else if (tmpX < EdgeLeft){
		tmpX = ParentLeft + ParentWidth + 1;
		parentAlign = "left2right";
	}
	
	if (EdgeBottom < (tmpY + nRealHeight)){
		if ((EdgeBottom - nRealHeight) >= EdgeTop) 
			tmpY = EdgeBottom - nRealHeight;
		else{
			tmpY = 0;
			//Phat
			//if (nRealHeight > (EdgeBottom - EdgeTop))
			//	nRealHeight = EdgeBottom - EdgeTop - 10;
		}
	}
	
	pos.x = tmpX;
	pos.y = tmpY;
	setAttribute(_arrIFrame[nLevel], "menuAlign", parentAlign);
	
	if (browseris.ie4up && browseris.win32){
		_arrIFrame[nLevel].style.pixelHeight = nRealHeight;
		_arrIFrame[nLevel].style.pixelLeft = pos.x;
		_arrIFrame[nLevel].style.pixelTop = pos.y;
	}else{
		_arrIFrame[nLevel].style.height = nRealHeight;
		_arrIFrame[nLevel].style.left = pos.x;
		_arrIFrame[nLevel].style.top = pos.y;
	}
}

function CreateMenuItem(oPopupDoc, oNode, wzID, nLevel){
	if (FIsIType(oNode, "label")) return;
	
	var oRow = oPopupDoc.createElement("tr");
	mergeAttributes(oNode, oRow);
	//oRow.mergeAttributes(oNode);
	//oRow.onMenuClick = oNode.onclick;
	setAttribute(oRow, "onMenuClick", getAttribute(oNode, "onclick"));
	if (FIsIType(oNode, "separator")){
		CreateMenuSeparator(oPopupDoc, oRow, oNode);
		return oRow;
	}
	if (!GetIType(oNode)) SetIType(oNode, "option");
	var oFmtTableRow = oPopupDoc.createElement("tr");
	var oFmtTableCell = oPopupDoc.createElement("td");
	var oFmtTable = oPopupDoc.createElement("table");
	var oFmtTableBody = oPopupDoc.createElement("tbody");
	oFmtTableRow.appendChild(oFmtTableCell);
	oFmtTableCell.appendChild(oFmtTable);
	oFmtTable.appendChild(oFmtTableBody);
	oFmtTableBody.appendChild(oRow);
	oFmtTableCell.className = "ms-MenuUIItemTableCell";
	oFmtTable.className = "ms-MenuUIItemTable";
	oFmtTable.cellSpacing = 0;
	oFmtTable.cellPadding = 0;
	
	if (FIsIType(oNode, "submenu"))
		CreateSubmenu(oPopupDoc, oRow, oNode, wzID);
	else if (FIsIType(oNode, "option"))
		CreateMenuOption(oPopupDoc, oRow, oNode, wzID);
	/*else if (FIsIType(oNode, "radio"))
		CreateMenuInput(oPopupDoc, oRow, oNode, wzID, "", "radio");
	else if (FIsIType(oNode, "checkbox"))
		CreateMenuInput(oPopupDoc, oRow, oNode, wzID, "", "checkbox");
	*/
	
	oFmtTableRow.onmouseover = new Function("PopupMouseOver(this, " + nLevel + ")");
	oFmtTableRow.onmouseout = new Function("PopupMouseLeave(this, " + nLevel + ")");
	oFmtTableRow.onmouseup = new Function("PopupMouseUp(this, " + nLevel + ")");
	
	var isDisabled = ((getAttribute(oRow, "disabled") == "true") ? true : false);
	if (isDisabled){
		setAttribute(oRow, "disabled", "false");
		var rowChildren = null;
		if (browseris.ie4up && browseris.win32)
			rowChildren = oRow.children;
		else
			rowChildren = oRow.childNodes;
		
		for (var nIndex = 0; nIndex < rowChildren.length; nIndex++){
			var childItem = null;
			if (browseris.ie4up && browseris.win32)
				childItem = rowChildren[nIndex];
			else
				childItem = rowChildren.item(nIndex);
			
			if (childItem){
				setAttribute(childItem, "disabled", "true");
			}
			//oRow.children(nIndex).setAttribute("disabled", "true");
		}
		setAttribute(oRow, "optionDisabled2", "true");
	}
	//Here2
	mergeAttributes(oRow, oFmtTableRow);
	setAttribute(oFmtTableRow, "id", wzID);
	//oFmtTableRow.mergeAttributes(oRow);
	//oFmtTableRow.id = wzID;
	SetIType(oFmtTableRow, GetIType(oRow));
	setAttribute(oRow, "onclick", "");
	return oFmtTableRow;
}

function CreateMenuOption(oPopupDoc, oRow, oNode, wzID, wzHtml){
	var oIcon = oPopupDoc.createElement("td");
	var oLabel = oPopupDoc.createElement("td");
	var oAccKey = oPopupDoc.createElement("td");
	var oArrow = oPopupDoc.createElement("td");
	var oHref = oPopupDoc.createElement("a");
	oRow.appendChild(oIcon);
	oRow.appendChild(oLabel);
	oRow.appendChild(oAccKey);
	oRow.appendChild(oArrow);
	oLabel.appendChild(oHref);
	
	oIcon.className = !_fIsRtL ? "ms-MenuUIIcon" : "ms-MenuUIIconRtL";
	oLabel.className = !_fIsRtL ? "ms-MenuUILabel" : "ms-MenuUILabelRtL";
	oAccKey.className = "ms-MenuUIAccessKey";
	oArrow.className = "ms-MenuUISubmenuArrow";
	oHref.className = "ms-MenuUIHref";
	
	oIcon.noWrap = true;
	oLabel.noWrap = true;
	oAccKey.noWrap = true;
	oArrow.noWrap = true;
	oLabel.id = oNode.id;
	
	if (!wzHtml) wzHtml = oNode.innerHTML;
	var href = getAttribute(oNode, "href");
	if ((!href) || (href == "")){
		href = getAttribute(oRow, "onclick");
		if ((href) && (href != ""))
			setAttribute(oHref, "href", "javascript:" + href);
	}else
		setAttribute(oHref, "href", href);
	
	setAttribute(oRow, "onclick", "");
	setAttribute(oHref, "target", "_parent");
	oHref.innerHTML = wzHtml;
	
	var wzIconSrc = null, wzIconAlt = null;
	var isChecked = getAttribute(oNode, "checked")
	
	if (isChecked == "true"){
		wzIconSrc = _wzChkMrkPath;
		wzIconAlt = "*";
	}else{
		wzIconSrc = getAttribute(oNode, "iconSrc");
		wzIconAlt = getAttribute(oNode, "iconAltText");
	}
	
	if (wzIconSrc){
		var oImg = oPopupDoc.createElement("IMG");
		//var oImgLbl = oPopupDoc.createElement("LABEL");
		oIcon.appendChild(oImg);
		//oLabel.appendChild(oImgLbl);
		var wzIconId = wzID + "_" + "ICON";
		oImg.id = wzIconId;
		oImg.src = wzIconSrc;
		if (wzIconAlt){
			oImg.alt = wzIconAlt;
			oImg.title = "";
		}
		//oImgLbl.htmlFor = wzIconId;
		//oImgLbl.innerHTML = wzHtml;
	}else{
		oIcon.width = 10;
		oIcon.innerHTML = "&nbsp;";
		//oLabel.innerHTML = wzHtml;
	}
	var wzAccKey = getAttribute(oNode, "accessKeyText");
	if (wzAccKey) oAccKey.innerText = wzAccKey;
	SetIType(oRow, "option");
}

function CreateMenuInput(oPopupDoc, oRow, oNode, wzID, wzHtml, type){
	var oIcon = oPopupDoc.createElement("td");
	var oLabel = oPopupDoc.createElement("td");
	var oAccKey = oPopupDoc.createElement("td");
	var oArrow = oPopupDoc.createElement("td");
	var oInput = oPopupDoc.createElement("<input type='" + type + "'>");
	
	oRow.appendChild(oIcon);
	oRow.appendChild(oLabel);
	oRow.appendChild(oAccKey);
	oRow.appendChild(oArrow);
	oIcon.className = !_fIsRtL ? "ms-MenuUIIcon" : "ms-MenuUIIconRtL";
	oLabel.className = !_fIsRtL ? "ms-MenuUILabel" : "ms-MenuUILabelRtL";
	oAccKey.className = "ms-MenuUIAccessKey";
	oArrow.className = "ms-MenuUISubmenuArrow";
	oIcon.noWrap = true;
	oLabel.noWrap = true;
	oAccKey.noWrap = true;
	oArrow.noWrap = true;
	oLabel.id = oNode.id;
	if (!wzHtml) wzHtml = oNode.innerHTML;
	var wzIconSrc = null, wzIconAlt = null;

	var isChecked = getAttribute(oNode, "checked")
	
	wzIconSrc = getAttribute(oNode, "iconSrc");
	wzIconAlt = getAttribute(oNode, "iconAltText");
	
	if (wzIconSrc){
		var oImg = oPopupDoc.createElement("IMG");
		var oImgLbl = oPopupDoc.createElement("LABEL");
		oIcon.appendChild(oImg);
		oLabel.appendChild(oImgLbl);
		var wzIconId = wzID + "_" + "ICON";
		oImg.id = wzIconId;
		oImg.src = wzIconSrc;
		if (wzIconAlt){
			oImg.alt = wzIconAlt;
			oImg.title = "";
		}
		oImgLbl.htmlFor = wzIconId;
		oImgLbl.innerHTML = wzHtml;
	}else{
		setAttribute(oInput, "name", getAttribute(oNode, "name"));
		setAttribute(oInput, "value", getAttribute(oNode, "value"));
		setAttribute(oInput, "checked", isChecked);
		
		if (browseris.ie && browseris.win32)
			oInput.onmouseup = InputClick;
		else
			oInput.addEventListener('mouseup', InputClickNS, false);
	
		oIcon.appendChild(oInput);
		oLabel.innerHTML = wzHtml;
	}
	
	var wzAccKey = getAttribute(oNode, "accessKeyText");
	if (wzAccKey) oAccKey.innerText = wzAccKey;
	SetIType(oRow, "option");
}

function CreateMenuSeparator(oPopupDoc, oRow, oNode){
	var oCell = oPopupDoc.createElement("td");
	var oDiv = oPopupDoc.createElement("div");
	oRow.appendChild(oCell);
	oCell.appendChild(oDiv);
	oDiv.className = !_fIsRtL ? "ms-MenuUISeparator" : "ms-MenuUISeparatorRtL";
	oDiv.innerHTML = "&nbsp;";
	SetIType(oRow, "separator");
}

function CreateSubmenu(oPopupDoc, oRow, oNode, wzID){
	var oLabelNode = FindLabel(oNode);
	CreateMenuOption(oPopupDoc, oRow, oNode, wzID, oLabelNode ? oLabelNode.innerHTML : null);
	
	var oArrow = null;
	if (browseris.ie4up && browseris.win32)
		oArrow = oRow.children[3];
	else
		oArrow = getChildElementByIndex(oRow, "TD", 3);
	var oArrowImg = oPopupDoc.createElement("IMG");
	
	oArrow.appendChild(oArrowImg);
	setAttribute(oArrowImg, "src", ((!_fIsRtL) ? _wzMArrPath : _wzMArrPathRtL));
	setAttribute(oArrowImg, "alt", ((!_fIsRtL) ? ">" : "<"));
	setAttribute(oArrowImg, "title", "");
	SetIType(oRow, "submenu");
	if (browseris.ie4up && browseris.win32)
		oRow.submenuRoot = oNode;
	else
		setAttribute(oRow, "submenuRoot", oNode.innerHTML);
}

function CreateMenuScroll(oPopupDoc, nLevel, isArrowUp){
	var oFmtTableRow = oPopupDoc.createElement("tr");
	var oFmtTableCell = oPopupDoc.createElement("td");
	var oFmtTable = oPopupDoc.createElement("table");
	var oFmtTableBody = oPopupDoc.createElement("tbody");
	var oRow = oPopupDoc.createElement("tr");
	var oEmptyRow = oPopupDoc.createElement("tr");
	oFmtTableRow.appendChild(oFmtTableCell);
	oFmtTableCell.appendChild(oFmtTable);
	oFmtTable.appendChild(oFmtTableBody);
	oFmtTableBody.appendChild(oRow);
	
	//setVisibility(oFmtTableRow, false);
	oFmtTableRow.style.position = "absolute";
	oFmtTableRow.style.left = 1;
	oFmtTableRow.style.top = 0;
	
	oFmtTableCell.className = "ms-MenuUIItemTableCellHover";
	oFmtTable.className = "ms-MenuUIItemTableHover";
	//oFmtTableCell.className = "ms-MenuUIItemTableCell";
	//oFmtTable.className = "ms-MenuUIItemTable";
	
	oFmtTable.cellSpacing = 0;
	oFmtTable.cellPadding = 0;
	
	//oFmtTableRow.onmouseover = new Function("PopupMouseOver(this, " + nLevel + ")");
	//oFmtTableRow.onmouseout = new Function("PopupMouseLeave(this, " + nLevel + ")");
	//oFmtTableRow.onmouseup = new Function("PopupMouseUp(this, " + nLevel + ")");
	
	var oArrow = oPopupDoc.createElement("td");
	oRow.appendChild(oArrow);
	//oArrow.className = "ms-MenuUISubmenuArrow";
	oArrow.align = "middle";
	oArrow.noWrap = true;
	oArrow.innerHTML = "&nbsp;";
	
	var oImg = oPopupDoc.createElement("IMG");
	oArrow.appendChild(oImg);
	oImg.src = "../Images/menu_arrow_up.gif";
	oImg.alt = "";
	oImg.title = "";
	SetIType(oFmtTableRow, "option");
	return oFmtTableRow;
}

function CreateEmptyMenu(oPopupDoc){
	var oFmtTableRow = oPopupDoc.createElement("tr");
	var oFmtTableCell = oPopupDoc.createElement("td");
	var oFmtTable = oPopupDoc.createElement("table");
	var oFmtTableBody = oPopupDoc.createElement("tbody");
	var oRow = oPopupDoc.createElement("tr");
	var oArrow = oPopupDoc.createElement("td");
	
	oFmtTableRow.appendChild(oFmtTableCell);
	oFmtTableCell.appendChild(oFmtTable);
	oFmtTable.appendChild(oFmtTableBody);
	oFmtTableBody.appendChild(oRow);
	
	//setVisibility(oFmtTableRow, false);
	
	oFmtTable.cellSpacing = 0;
	oFmtTable.cellPadding = 0;
	oFmtTableCell.className = "ms-MenuUIItemTableCell";
	oFmtTable.className = "ms-MenuUIItemTable";
	
	oRow.appendChild(oArrow);
	//oArrow.className = "ms-MenuUISubmenuArrow";
	oArrow.align = "middle";
	oArrow.noWrap = true;
	oArrow.innerHTML = "Ph&#7841;m Quang Ph&#7845;t";
	return oFmtTableRow;
}

function MakeID(nLevel, nIndex){
	return _wzPrefixID + "_" + nLevel + "_" + nIndex;
}
	
function FIsIType(oItem, wzType){
	return FIStringEquals(GetIType(oItem), wzType);
}

function GetIType(oItem){
	return getAttribute(oItem, "type");
}

function SetIType(oItem, wzType){
	if (oItem) setAttribute(oItem, "type", wzType);
}

function FIStringEquals(wzX, wzY){
	return wzX != null && wzY != null && wzX.toLowerCase() == wzY.toLowerCase();
}

function FIStringContains(wzX, wzY){
	return wzX != null && wzY != null && wzX.toLowerCase().indexOf(wzY.toLowerCase()) >= 0;
}

function FindLabel(oParent){
	if (oParent){
		var arrNodes = null;
		if (browseris.ie4up && browseris.win32)
			arrNodes = oParent.children;
		else
			arrNodes = oParent.childNodes;
	
		if (arrNodes){
			for (var nIndex = 0; nIndex < arrNodes.length; nIndex++){
				var oNode = arrNodes[nIndex];
				if (FIsIType(oNode, "label"))
					return oNode;
			}
		}
	}
	return null;
}

function getSelectedItem(nLevel){
	if (nLevel < 0) return null;
	if (_arrSelected.length > nLevel)
		return _arrSelected[nLevel];
	return null;
}

function UpdateLevel(nLevel){
	var oPopup;
	while (_nLevel > nLevel){
		oPopup = _arrIFrame[_nLevel];
		if (oPopup){
			setVisibility(oPopup, false);
		}
		_arrIFrame[_nLevel] = null;
		_arrWnd[_nLevel] = null;
		_arrSelected[_nLevel] = null;
		if (_oRoot.parentNode) _oRoot = _oRoot.parentNode;
		_nLevel--;
	}
	if (_nLevel < 0) _nLevel = 0;
}

function ToggleMenuItem(nLevel, oItem){
	var oOld = getSelectedItem(nLevel);
	if (!oItem || (oOld && oItem.id == oOld.id)) return;
	if (oOld) UnselectItem(oOld);
	UpdateLevel(nLevel);
	SelectItem(oItem);
	_arrSelected[nLevel] = oItem;
	oItem.tabIndex = 0;
	//oItem.setActive();
}

function SelectItem(oItem){
	try{
		if (!oItem) return;
		var oItemTableCell = oItem.firstChild;
		oItemTableCell.className = "ms-MenuUIItemTableCellHover";
		var oItemTable = oItemTableCell.firstChild;
		oItemTable.className = "ms-MenuUIItemTableHover";
	}catch(e){}
}

function UnselectItem(oItem){
	try{
		if (!oItem) return;
		var oItemTableCell = oItem.firstChild;
		oItemTableCell.className = "ms-MenuUIItemTableCell";
		var oItemTable = oItemTableCell.firstChild;
		oItemTable.className = "ms-MenuUIItemTable";
	}catch(e){}
}

function PopupMouseOver(oItem, nLevel){
	if (nLevel < 0) return;
	
	if (oItem){
		if (oItem != _arrSelected[nLevel]){
			if (FIsIType(oItem, "separator")) return;
			ToggleMenuItem(nLevel, oItem);
			if (FIsIType(oItem, "submenu")) EngageSelection(true, true, false);
		}else if (nLevel < _nLevel){
			UnselectCurrentOption();
		}
	}
}

function PopupMouseLeave(oItem, nLevel){
	if (nLevel == _nLevel) UnselectCurrentOption();
}

function PopupMouseUp(oItem, nLevel){
	if (nLevel < 0) return;
	UpdateLevel(nLevel);
	EngageSelection(true, false, false);
}

function UnselectCurrentOption(){
	if (_nLevel >= 0){
		var oItem = _arrSelected[_nLevel];
		if (FIsIType(oItem, "option")){
			UnselectItem(oItem);
			_arrSelected[_nLevel] = null;
		}
	}
}

function EngageSelection(fDoSelection, fDelayExpandSM, fEnterSM){
	var oItem = _arrSelected[_nLevel];
	if (!oItem || oItem.optionDisabled) return;
	if (FIsIType(oItem, "submenu")){
		ShowSubMenu(_nLevel, oItem);
	}else if (fDoSelection){
		HideMenu();
	}
}

function HideMenu(){
    UpdateLevel(-1);
    setAttribute(document.body, "onclick", bodyClickAction);
    isOpening = false;
    callWhenHide();
}

function InputClick(inputItem){
	window.event.cancelBubble = true;
}

function InputClickNS(e){
	e.cancelBubble = true;
}

function winStatus(msg){
	try{
		window.status = msg;
	}catch(e){}
	return true;
}

function doNothing(){}

/* Cho phep nap chong*/
function callWhenShow(srcMenu, elt){
	return true;
}

function callWhenHide(){
	return true;
}