﻿// JScript File



function disableMenuTD(item, tf)
{
    if (typeof item == 'string')
    {
        obj=document.getElementById(item);
    }
    else
    {
        obj = item;
    }
  //  if (!obj.style)
  //  {
  //      obj.style = new Object();
  //  }
    if (tf)
    {
        obj.style.color = '#a9a9a9';
        obj.setAttribute('disabled', true );
    }
    else
    {
        obj.setAttribute('disabled', false )
        obj.style.color = '';
    }    
}





function cMenus(vInstanceName)
{
    this.instanceName = vInstanceName;
}


cMenus.prototype.isMenuDisplayed 	= false ;
cMenus.prototype.menuPoppedUpOverObjPkey = null ;
cMenus.prototype.menuPoppedUpOverObjPopup = null ;
cMenus.prototype.overpopupmenuSys = false;
cMenus.prototype.dragDrop;
cMenus.prototype.rootMenu;
cMenus.prototype.instanceName;
cMenus.prototype.designUserPkey;
cMenus.prototype.designUserRole;
cMenus.prototype.lastHi;


cMenus.prototype.popupAtX = 0;
cMenus.prototype.popupAtY = 0;
cMenus.prototype.popupXoffset = -20;
cMenus.prototype.popupYoffset = -30;


cMenus.prototype.TimerID = 0; 
cMenus.prototype.TimerIDSys = 0; 


cMenus.prototype.killTimer = function (menuDiv)
{
    if (menuDiv.getAttribute('typ') == '1')
    {
        if (this.TimerIDSys != 0 )
        {
            clearTimeout(this.TimerIDSys);
            this.TimerIDSys = 0;
        }
    }
    else
    {
        if (this.TimerID != 0 )
        {
            clearTimeout(this.TimerID);
            this.TimerID = 0;
        }
    }
}


cMenus.prototype.doCloseAll = function ()
{
    this.closeAll();
}

cMenus.prototype.doCloseAllSys = function ()
{
    this.closeAllSys();
}



cMenus.prototype.setDesignUserAndRole = function(vUserPkey, vUserRole)
{
    this.designUserPkey=vUserPkey;
    this.designUserRole = vUserRole;
}


cMenus.prototype.setOver = function(menuDiv, tf)
{
    var cmd;

    if (this.rootMenu.disabled == true) return;


    //lert('setover ' + menuDiv.id + ' ' + tf);

    if (isMenuMode(this.rootMenu, 'Popup'))
    {
        clearTimeout(this.TimerID);
        this.TimerID = 0;
    }

 
    this.killTimer(menuDiv);   

    if (menuDiv.getAttribute('typ') == '1' && (isMenuMode(this.rootMenu, 'Popup') == false) )
    {
        this.overpopupmenuSys = tf;
        this.overpopupmenu = true;
        clearTimeout(this.TimerID);
        this.TimerID = 0;
        if (tf == false)
        {
            cmd = this.instanceName + ".doCloseAllSys()"
            this.TimerIDSys = setTimeout(cmd, 400);
        }
    }
    else
    {
        this.overpopupmenu = tf;
        if (tf == false)
        {
            cmd = this.instanceName + ".doCloseAll();" 
            this.TimerID = setTimeout(cmd, 400);
        }
    }
}



cMenus.prototype.closeAllSys = function()
{
    var menusys;

    
        if (this.TimerIDSys != 0 )
        {
            clearTimeout(this.TimerIDSys);
            this.TimerIDSys = 0;
        }


    menusys = document.getElementById(this.rootMenu.id.toString() + '_SysMenuOffOn');
    this.overpopupmenuSys  = false;
    this.hideMenu(menusys);
    this.clearHilites(menusys);
}
  

cMenus.prototype.closeAll = function()
{
    if (this.rootMenu)
    {
        if (this.rootMenu.getAttribute('curChildId'))
        {
            if (this.overpopupmenu == false)
            {
                this.hideMenu(this.rootMenu.getAttribute('curChildId'));
                this.clearHilites(this.rootMenu);
            }
        }
        else
        {
            if (isMenuMode(this.rootMenu, 'Popup'))
            {
                this.hideMenu(this.rootMenu);
                this.clearHilites(this.rootMenu);
            }
        }
    }
}


cMenus.prototype.setRootMenu = function(name)
{
    this.rootMenu = document.getElementById(name); 
}



cMenus.prototype.mouseDown = function(e)
{
//var ns6	= document.getElementById&&!document.all;
    var B = new cBrowser();
	var obj =  B.ns6  ? e.target.parentNode : event.srcElement.parentElement;
    if (this.dragDrop)
    {
        if(!this.isMenuDisplayed ) this.dragDrop.mouseDown(e);
    }
    
	if(this.isMenuDisplayed )
	{
		if(this.overpopupmenu == false )
		{
			this.isMenuDisplayed = false ;
			this.overpopupmenu = false;
			this.hideMenu(this.menuPoppedUpOverObjPopup);
			document.getElementById(this.menuPoppedUpOverObjPopup).style.display = "none" ;
			return true ;
		}
		else
		{
		}
		return true ;
	}
	else
	{	
	}
	
	return false;
}



cMenus.prototype.item2Menu  = function (itemTD)   // get the menu ( a Div object) that this menu item belongs to
{
    var parent;
    var tr;
	tr = parentObj(itemTD);    //tr
	parent = parentObj(tr); //tbody
	parent = parentObj(parent); //table
	parent = parentObj(parent); //td
	parent = parentObj(parent); //tr
	parent = parentObj(parent); //tbody
	parent = parentObj(parent); //table
	parent = parentObj(parent); //div
    return parent;
}


function isMenuMode(obj, mode)
{
    var mmode;   
    mmode= obj.getAttribute('mmode');
    if (mmode.indexOf(mode) >= 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}

cMenus.prototype.clearHilites = function (menu)
{
    var mnu
    var rows;
    var designMode;
    var dynamicClass;
    var staticClass;
    
    if (typeof menu == 'string')
    {
        mnu = document.getElementById(menu);
    }
    else
    {
        mnu = menu;
    }
    if (mnu)
    {
        designMode = isMenuMode(mnu, 'Design');
        if (!designMode)
        {
            dynamicClass='mnuDynamicItemLo';
            staticClass='mnuStaticItemLo';
        }
        else
        {
            dynamicClass='mnuSysItemLo';
            staticClass='mnuSysItemLo';
        }
        
        if (mnu.getAttribute('uplink'))  //indicates that this is a child menu
        {
        
            rows=objChild(mnu, 0).rows;
            table=objChild(rows[0], 0);
            table=objChild(table, 0);
            rows = table.rows;
            
            for (i = 0; i < rows.length; i ++)
            {
                objChild(rows[i], 0).className=dynamicClass;
            }
        }
        else
        {
            rows=objChild(mnu, 0).rows;
            table=objChild(rows[0], 0);
            table=objChild(table, 0);
            rows = table.rows;
            for (i = 0; i < rows[0].childNodes.length; i++)
            {
                if (rows[0].childNodes[i].nodeType == 1)
                {
                    rows[0].childNodes[i].className=staticClass;
                }
            }
            
        }
    }
}


cMenus.prototype.hideMenu = function(menu, andParents )
{
    var uplink;
    var uplinksParent;
    var mnu
    
    //debug('hide menu|<br/>');

    if (typeof menu == 'string')
    {
        mnu = document.getElementById(menu);
    }
    else
    {
        mnu = menu;
    }
    if (mnu)
    {
        this.clearHilites(mnu);
        
        if (mnu.getAttribute('curChildId'))
        {
            this.hideMenu(mnu.getAttribute('curChildId'));
            mnu.curChildId = null;
        }
        
        if (mnu.getAttribute('uplink'))  //indicates that this is a child menu
        {
            uplink = document.getElementById(mnu.getAttribute('uplink'));
            uplinksParent = this.item2Menu(uplink);
    	
            //uplink.style.backgroundColor = uplinksParent.style.backgroundColor;
            //uplink.style.color = uplinksParent.style.color;
        }

        mnu.style.display = "none";
        mnu.style.top  =  '-1000px';
        mnu.style.left =  '-1000px';
        
        if (andParents)
        {
            if (mnu.getAttribute('uplink'))  //indicates that this is a child menu
            {
                uplink = document.getElementById(mnu.getAttribute('uplink'));
                uplinksParent = this.item2Menu(uplink.firstChild);
                this.hideMenu(uplinksParent, true);
            }
        }
    }
}
      
            
cMenus.prototype.mnuHighlight = function(obj)
{
 //   var obj = document.getElementById(id);
    var childmenu;
    var x;
    var y;
    var parent;
    var tr;
    var uplink;
    var coord;
    var sizexy;
    var scrollxy;
    var edgex;
    var childWidth;
    var designMode;
    var dynamicClass;
    var staticClass;

    //if(!this.isMenuDisplayed ) return;
        

if (this.rootMenu.disabled == true) return;

    
	tr = parentObj(obj);    //tr
	parent = parentObj(tr); //tbody
	parent = parentObj(parent); //table
	parent = parentObj(parent); //td
	parent = parentObj(parent); //tr
	parent = parentObj(parent); //tbody
	parent = parentObj(parent); //table
	parent = parentObj(parent); //div


    if (parent.getAttribute('typ') != '1')
    {    
        if (this.lastHi != obj)
        {
            this.closeAllSys();
        }
        this.lastHi = obj;
    }
    else
    {
        uplink=parent.getAttribute('uplink');
        if (uplink)
        {
            this.mnuHighlight(document.getElementById(uplink));
        }
    }

    
    if (parent.style.display == "none") return;

    designMode = isMenuMode(parent, 'Design');
    if (!designMode)
    {
        dynamicClass='mnuDynamicItemHi';
        staticClass='mnuStaticItemHi';
    }
    else
    {
        dynamicClass='mnuSysItemHi';
        staticClass='mnuSysItemHi';
    }

    
    this.setOver(parent, true);
    this.clearHilites(parent);


    coord = getPos(obj);
    obj.style.left = coord.left + 'px'; 
    obj.style.top = coord.top + 'px';

    
    if (parent.getAttribute('curChildId'))
    {
        this.hideMenu(parent.getAttribute('curChildId'));
        parent.curChildId = null;
    }
    
    if (parent.getAttribute('uplink'))  //indicates that this is a child menu
    {
        obj.className=dynamicClass;
    }
    else
    {
        obj.className=staticClass;
    }

    
    if (obj.getAttribute('childmenu'))
    {

        sizexy = getWindowSize();
        scrollxy=getScrollXY();
        edgex=sizexy.x + scrollxy.x - 10;
        childmenu=document.getElementById(obj.getAttribute('childmenu'));
        childmenu.style.display = "";
        childWidth=parseFloat(childmenu.offsetWidth);
        if (parent.getAttribute('uplink'))  // indicates dynamic menu item
        {
            x = parseFloat(parent.style.left) + parseFloat(obj.offsetWidth) - 4;
            if ((x + childWidth) > edgex)
            {
                x = parseFloat(parent.style.left) - childWidth + 4;
            }
            
            y = parseFloat(parent.style.top) + tr.offsetTop;
        }
        else    // top level menu
        {           
            x = parseFloat(obj.style.left) -10;  
            if ((x + childWidth) > edgex)
            {
                x = x - ((x + childWidth) - edgex);
            }
            y = parseFloat(obj.style.top) + tr.offsetTop + 7 ;
        }
        
        childmenu.setAttribute('uplink', obj.getAttribute('id'));
        this.showMenuAt(obj.getAttribute('childmenu'), x, y);
        parent.setAttribute('curChildId', obj.getAttribute('childmenu'));     
    }    
}

 
 
           
cMenus.prototype.mnuLowlight = function(obj)
{
    var parent;
    var designMode;
    var dynamicClass;
    var staticClass;


    parent = this.item2Menu(obj);

    designMode = isMenuMode(parent, 'Design');

    if (!designMode)
    {
        dynamicClass='mnuDynamicItemLo';
        staticClass='mnuStaticItemLo';
    }
    else
    {
        dynamicClass='mnuSysItemLo';
        staticClass='mnuSysItemLo';
    }
    
    
    this.setOver(parent, false);

    if (obj.getAttribute('childmenu'))
    {
        if (parent.getAttribute('uplink'))
        {
        }
        else
        {
        }
    }
    else
    {
        if (parent.getAttribute('uplink'))
        {
            obj.className=dynamicClass; 
        }
        else
        {
            obj.className=staticClass;
        }
    }
}
 




cMenus.prototype.showMenuAt = function(mnuId, x, y)
{
    var mnu;
    mnu = document.getElementById(mnuId)
	mnu.style.left = x + 'px';
	mnu.style.top = y + 'px';
	mnu.style.display = "";
}



cMenus.prototype.sayHello = function()
{
    this.setOver(true);
    alert('hello');
}


cMenus.prototype.click = function(obj)
{
    var parent;
    var designMode;
    var tr;
    var typ;
    var fn;
    var url;
    var uplink;
    var disabled;


if (this.rootMenu.disabled == true) return;
    parent = this.item2Menu(obj);

    disabled = obj.getAttribute('disabled');
    if (disabled)
    {
        if (disabled == 'true') 
        {
            this.setOver(parent, false);
            this.killTimer(parent);
            this.closeAll();
            return;
        }
    }
    
    designMode = isMenuMode(parent, 'Design');
    typ = parent.getAttribute('typ');

    if (designMode && (typ == '0'))
    {

        //coord = getPos(parent);
       // parent.style.left = coord.left + 'px'; 
       // parent.style.top = coord.top + 'px';
            
            
    	tr = parentObj(obj);    //tr
        coord = getPos(tr);
        childmenu=document.getElementById(this.rootMenu.id.toString() + '_SysMenuOffOn');
        childmenu.setAttribute('uplink',obj.getAttribute('id'));
        
        sizexy = getWindowSize();
        scrollxy=getScrollXY();
        edgex=sizexy.x + scrollxy.x - 10;

        childmenu.style.display = "";
        childWidth=parseFloat(childmenu.offsetWidth);

        if (parent.getAttribute('uplink'))  //indicates that this is a child (vertical) menu
        {
            coord = getPos(parentObj(obj));
            x = parseFloat(coord.left) + parseFloat(obj.offsetWidth) - 16;
            if ((x + childWidth) > edgex)
            {
                x = parseFloat(coord.left) - childWidth + 4;
            }
            y = parseFloat(coord.top) - 17;
        }
        else
        {
            coord = getPos(obj);
            x = parseFloat(coord.left) -10;  
            y = parseFloat(coord.top) + 5 ;
        }
        this.showMenuAt(this.rootMenu.id.toString() + '_SysMenuOffOn', x, y);
        
        return;
    }




 
    fn = obj.getAttribute('fn');
    if (fn) fn=trim(fn)
    url = obj.getAttribute('url');

    this.setOver(parent, false);
    this.killTimer(parent);
    if (fn || url)
    {
        this.closeAll();
    }

    if (fn > '')
    {
        if (parent.getAttribute('uplink'))
        {
            uplink=document.getElementById(parent.getAttribute('uplink'));
            if (uplink)
            {
                fn = nCaseReplace(fn,'%pkey%',uplink.getAttribute('pkey'));
                fn = nCaseReplace(fn,'%id%',uplink.getAttribute('id'));
            }
        }
        else
        {   
            if (this.menuPoppedUpOverObjPkey)
            {
                fn = nCaseReplace(fn,'%pkey%',this.menuPoppedUpOverObjPkey);
            }
        }
        fn = nCaseReplace(fn,'%instance%',this.instanceName);
        fn = nCaseReplace(fn,'%designrole%',this.designUserRole);
        fn = nCaseReplace(fn,'%designuserpkey%',this.designUserPkey);

        if (isMenuMode(parent, 'Popup')) //6/29
        {
            this.hideMenu(parent);
        }


        eval(fn);
    }
    else
    {
        if (url)
        {
            MenuGoTo(url); // in CbsCore.js
        }
    }
    if (typ=='1')
    {
        this.hideMenu(parent);
    }
    
    
}

cMenus.prototype.contextMenu = function(e)
{
    var B = new cBrowser();
    var evtObj =  B.ns6   ? e.target: event.srcElement;	
    var x;
    var y;
    var popup;
    
    //popup = new String(evtObj.getAttribute('popup'));
    
    if (evtObj.getAttribute('popup'))
    {
        popup = new String(evtObj.getAttribute('popup'));
        {
            this.popupAtX = mouse_X;
            this.popupAtY = mouse_Y;
            //lert('cMenus.prototype.contextMenu popup=' + popup);
            
            this.menuPoppedUpOverObjPkey = evtObj.getAttribute('pkey');
            this.menuPoppedUpOverObjPopup = popup;
            
            if (popup > '' && popup != 'null')
            {
	           // this.setOver(popup, false); //6/29
	            this.setRootMenu(popup);
	            this.showMenuAt(popup, mouse_X + this.popupXoffset, mouse_Y + this.popupYoffset); 
	            this.isMenuDisplayed = true; 
            }
        }
    }
    return false ;
}




