/*
function InitMenu(){
*/

var bodyIngoreClick=false;
var timerActive = false;   // true if a timeout is pending
var timerEvent = null;     // store timeout ID (write-only)
var hideSelects = true;
var openMenu=null;
var openSubMenu=null;


 
function InitMenu(){

    var menuBar = document.getElementById("menuBar");
    if(menuBar == null){
        return;
    }
    var bar = menuBar.childNodes;
    for(var i=0;i < bar.length;i++){
        if(bar[i].nodeType==3)
            continue;
        var menu=getMenu(bar[i]);
        menu.style.visibility = "hidden";
        bar[i].onmouseover = new Function("ShowMenu2('"+bar[i].id+"')");
        bar[i].onclick = new Function("ShowMenuC('"+bar[i].id+"')");
        bar[i].onmouseout = new Function("mouseOut()");
        var Items = menu.childNodes;

        for(var j=0; j<Items.length; j++){
            var menuItem = Items[j];
            
            if(getMenu(menuItem) != null){ 
                menuItem.innerHTML += " >>";
                FindSubMenu(getMenu(menuItem));
            }else{
                if(getCmd(menuItem) != null){
                    menuItem.onclick = new Function("Do('"+menuItem.id+"')") ;
                }
            } 
            
            menuItem.onmouseover = new Function("highlight('"+Items[j].id+"')");
        }    
    }  
}

function FindSubMenu(subMenu){
    if(subMenu==null){
        return;
    }
    subMenu.style.visibility = "hidden";
    var Items = subMenu.childNodes;
    for(var j=0; j<Items.length; j++){
        
        if(getMenu(Items[j])!= null){
//            Items[j].innerHTML += "<Span Id="+menuItem.id+"_Arrow class='Arrow'>4</Span>";
            Items[j].innerHTML += " >>";
            // var tmp = document.getElementById(menuItem.id+"_Arrow")
            //tmp.style.pixelLeft = 35 //menuItem.getBoundingClientRect().right - tmp.offsetWidth - 15
            FindSubMenu(getMenu(Items[j]));
        }
        
        if(getCmd(Items[j]) != null){
            Items[j].onclick = new Function("Do('"+Items[j].id+"')");
        } 
        
        Items[j].onmouseover = new Function("highlight('"+Items[j].id+"')");
    }
} 

function ShowMenuC( obj){

    if( timerActive ){
        clearTimeout( timerEvent );
        timerActive = false;
    }

    if(document.getElementById(obj).className=="barOver"){
        return;
    }

    bodyIngoreClick = true;
    ShowMenu(obj);
}
   

var events=0;
function ShowMenu2(obj){
    if(document.getElementById(obj).className=="barOver"){
        return;
    }

    var delayOn = 1000
    this.status = ++events;
    timerEvent=setTimeout("timerActive=false;clearTimeout( timerEvent );ShowMenu('"+obj+"')",delayOn)
    timerActive = true
}
   
function ShowMenu(objName){

//    var menuBar = document.getElementById("menuBar");
    
    HideOpenMenu();
    var obj = document.getElementById(objName);
    openMenu=obj;
    var menu = getMenu(obj);
    
    obj.className="barOver";
    /*
    menu.style.pixelTop =  obj.getBoundingClientRect().top + obj.offsetHeight + Bdy.scrollTop;
    menu.style.pixelLeft = obj.getBoundingClientRect().left + Bdy.scrollLeft;
    alert(  "o.clientLeft:"+obj.clientLeft+"\n"+
            "o.offsetLeft:"+obj.offsetLeft+"\n"+
            "o.margin:"+obj.margin+"\n"+
            "o.style.borderLeft:"+obj.style.borderLeft+"\n"+
            "o.style.left:"+obj.style.left+"\n"+
            "o.style.padding:"+obj.style.padding+"\n"+
            "obj.offsetTop"+ obj.offsetTop +"\n"+
            "obj.offsetHeight:"+obj.offsetHeight+"\n"+
            "obj.scrollLeft:"+obj.scrollLeft+"\n"+
            "obj.clientTop:"+obj.clientTop +"\n");
    */

    menu.style.top =  obj.offsetTop + obj.offsetHeight +5;
    menu.style.left =  menuPosLeft(obj); //obj.offsetLeft + 5; 
    menu.style.visibility = "visible";

    if(hideSelects){
        for (nCounter1 = 0; nCounter1 < document.forms.length; nCounter1++){
            nElements = document.forms[nCounter1].elements.length;
            if(nElements >300){
                nElements=300;
            }
            for (nCounter2 = 0; nCounter2 < nElements; nCounter2++){
                sType = document.forms[nCounter1].elements[nCounter2].type;
                if (sType == "select-one" || sType == "select-multiple"){
                    document.forms[nCounter1].elements[nCounter2].style.visibility ="hidden";
                }
            }
        }
    }
}
   
function HideOpenMenu(){
    if(openMenu==null) return;
    openMenu.className="Bar";
    var menu=getMenu(openMenu);
    HideMenuLoop(menu);
    openMenu=null;

    if(hideSelects){
        for (nCounter1 = 0; nCounter1 < document.forms.length; nCounter1++){
            
            nElements = document.forms[nCounter1].elements.length;
            if(nElements >300){
                nElements=300;
            }
            for (nCounter2 = 0; nCounter2 < nElements; nCounter2++){
                sType = document.forms[nCounter1].elements[nCounter2].type;
                if (sType == "select-one" || sType == "select-multiple"){
                    document.forms[nCounter1].elements[nCounter2].style.visibility ="inherit";
                }
            }
        }
    }

}
 
   
function HideMainMenu(){
    var menuBar = document.getElementById("menuBar");
    if(menuBar==null){
        return;
    }
    if(!bodyIngoreClick){
       HideOpenMenu();
    }else{
       bodyIngoreClick = false;
    }
}
   
function HideMenuLoop(obj){

    if(obj==null) return;
    obj.style.visibility = "hidden" 
    if(obj.hasChildNodes()){  
        var child = obj.childNodes;
        for(var j =0;j<child.length;j++){
            var childMenu = getMenu(child[j]);
            if(childMenu != null){
                if(childMenu.hasChildNodes()) {
                    HideMenuLoop(childMenu)
                }
            }
        }
    }
}


function HideSubMenuLoop(obj){

    if(obj==null) return;
    if(obj.hasChildNodes()){  
        var child = obj.childNodes;
        for(var j =0;j<child.length;j++){
            var childMenu = getMenu(child[j]);
            if(childMenu != null){
                if(childMenu.hasChildNodes()) {
                    HideSubMenuLoop(childMenu)
                }
                childMenu.style.visibility = "hidden" 
            }
        }
    }
}
   
function ShowSubMenu(obj){
    
    var menu = getMenu(obj)
    if(menu != null)
    {
        menu.style.visibility = "visible"
        menu.style.top =  obj.offsetTop + obj.offsetHeight +5;
        menu.style.left = obj.parentNode.offsetLeft + obj.offsetLeft+obj.offsetWidth;

/*
        menu.style.pixelTop =  obj.getBoundingClientRect().top + Bdy.scrollTop
        menu.style.pixelLeft = obj.getBoundingClientRect().right + Bdy.scrollLeft
        if(menu.getBoundingClientRect().right > window.screen.availWidth )
            menu.style.pixelLeft = obj.getBoundingClientRect().left - menu.offsetWidth
*/
    }
} 

function mouseOut( ){
    if( timerActive ){
        clearTimeout( timerEvent )
        timerActive= false
    }
}

function getMenu(obj){
    if(obj.nodeType==3)
          return null;
//    alert(obj.getAttribute('menu'));
    return document.getElementById(obj.getAttribute('menu'));
}

function getCmd(obj){
    if(obj.nodeType==3)
          return null;
    return obj.getAttribute('cmd');
}    

function highlight(objName){
    obj= document.getElementById(objName);

    HideSubMenuLoop(obj.parentNode)
    
    var PElement = obj.parentNode;
    if(PElement.hasChildNodes() == true){  
        var Elements = PElement.childNodes;
        for(var i=0;i<Elements.length;i++){
//            TE = document.getElementById(Elements[i].id)
//            TE.className = "menuItem"
            Elements[i].className = "menuItem";
        }
    } 
    obj.className="ItemMouseOver";
    window.defaultStatus = obj.title;
    ShowSubMenu(obj);
}

function Do(objName){
    obj=document.getElementById(objName);
    var cmd = getCmd(obj);
    var target = obj.getAttribute('target');
    if ((target=="same") || (target=="")) {
        window.location.href=cmd;
    } else if (target=="new") {
        var tmpWidth    = document.body.clientWidth + 10
        var tmpHeight    = document.body.clientHeight + 60
        var params    = "width=" + tmpWidth + ", height=" + tmpHeight + ",left=0,top=0,screenX=0,screenY=0,status=no,toolbar=no,menubar=yes,location=no,directories=no,scrollbars=yes"
        window.open(cmd,"Reports",params)
    }
     
}
