if (document.all)    {n=0;ie=1;ns6=0;fShow="visible";fHide="hidden";}
if (document.getElementById&&!document.all)    {n=0;ie=0;ns6=1;fShow="visible";fHide="hidden";}
if (document.layers) {n=1;ie=0;ns6=0;fShow="show";fHide="hide";}

//Top Nav bar script v2- http://www.dynamicdrive.com/dynamicindex1/sm/index.htm

opr6=ie&&navigator.userAgent.indexOf("Opera")!=-1

//Top Nav Bar I v2- By Constantin Kuznetsov Jr. (script@esolutiononline.com)
//Modified by Dynamic Drive for NS6/Opera6 compatibility and code streamlining March 4th, 2002
//Visit http://www.dynamicdrive.com for this script

var keepstatic=0 //specify whether menu should stay static 0=non static (works only in IE4+)
//var menucolor="#6A89A6" //specify menu color
var menucolor="" //specify menu color
var submenuwidth=275 //specify sub menus' color
var menuDelayMs = 100 //specify menu delay

//window.onerror=new Function("return true")
////////////////////////////////////////////////////////////////////////////
// Function Menu()                                                        //
////////////////////////////////////////////////////////////////////////////
var menu;
var submenu;

var rightX=0;
var bottomY=0;
var topY=0;
var leftX=0;
var currentSubMenu='';
var currentSubMenuFolder='';
var lastMenuTemp=null;
var lastMenu=null;
var onTopMenu=0;
var lastShownMenu='';
var lastShownFolder='';
var lastShownMenuTemp='';
var lastShownFolderTemp='';

function setBG2(topitm, topfolder) {
//alert(onTopMenu + ' ' + currentSubMenu + ' ' + topitm)
  if (onTopMenu==1 && currentSubMenu==topitm) {
     clearBG(lastShownMenu,lastShownFolder)
     lastShownMenu = topitm;lastShownFolder = topfolder;
//     ie ? eval(topitm+".style.backgroundColor='#57749B'")
       ie ? eval(topitm+".style.backgroundColor=''")
//              :document.getElementById(topitm).style.backgroundColor='#57749B';
              :document.getElementById(topitm).style.backgroundColor='';


  }
}

function setBG(topitm, topfolder) {
  setTimeout("setBG2('" + topitm + "','" + topfolder + "')",menuDelayMs);
}


//function getClassForFolder(folder) {
//    folder = '/' + folder;
//    return (folder==location.pathname.substr(0,folder.length))?'tblMainMenuSelected':'tblMainMenu'
//}


function getBG(folder) {
    if ('\\' != '\\') {
        folder = '/' + folder;
    }
    //alert (folder + '\n' + location.pathname.substr(0,folder.length));
 //   return (folder==location.pathname.substr(0,folder.length))?'#57749B':'#6A89A6'
   return (folder==location.pathname.substr(0,folder.length))?'':''

}

function clearBG(id,folder) {

    color=getBG(folder);

    if (id!='') {
    
        if (n) {
            document.layers[id].bgColor=color;
        }
    
        if (ie||ns6) {
            ie ? eval(id+".style.backgroundColor='"+color+"'")
                    :document.getElementById(id).style.backgroundColor=color;
        
        }
    }
}

function Menu() {

    this.bgColor     = menucolor;

    if (ie) this.menuFont = "bold 12px Arial";
    if (n)  this.menuFont = "bold 12px Verdana";
    this.fontColor   = "black";

    this.addItem    = addItem;
    this.addSubItem = addSubItem;
    this.showMenu   = showMenu;
    this.mainPaneBorder = 0;
    this.subMenuPaneBorder = 0;

    this.subMenuPaneWidth = submenuwidth;

    lastMenu = null;
    
    rightY = 0;
    leftY = 0;
    leftX = 0;

    HTMLstr = "";
    HTMLstr += "<!-- MENU PANE DECLARATION BEGINS -->\n";
    HTMLstr += "\n";
//  if (ie||ns6) HTMLstr += "<div id='MainTable' style='position:absolute;'>\n";
//  if (n)  HTMLstr += "<layer name='MainTable'>\n";
    HTMLstr += "<table id=men  cellspacing=0 cellpadding=0 width='100%'>\n";
    HTMLstr += "<tr>";
    //if (n) HTMLstr += "<td>&nbsp;";
    HTMLstr += "<!-- MAIN MENU STARTS -->\n";
    HTMLstr += "<!-- MAIN_MENU -->\n";
    HTMLstr += "<!-- MAIN MENU ENDS -->\n";
    //if (n) HTMLstr += "</td>";
    HTMLstr += "</tr>\n";
    HTMLstr += "</table>\n";
    HTMLstr += "\n";
    HTMLstr += "<!-- SUB MENU STARTS -->\n";
    HTMLstr += "<!-- SUB_MENU -->\n";
    HTMLstr += "<!-- SUB MENU ENDS -->\n";
    HTMLstr += "\n";
//  if (ie||ns6) HTMLstr+= "</div>\n";
//  if (n)  HTMLstr+= "</layer>\n";
    HTMLstr += "<!-- MENU PANE DECALARATION ENDS -->\n";
}

function addItem(idItem, text, hint, location, altLocation) {


    var Lookup = "<!-- ITEM "+idItem+" -->";
    if (HTMLstr.indexOf(Lookup) != -1)
    {
        alert(idParent + " already exist");
        return;
    }
    var MENUitem = "";
    MENUitem += "\n<!-- ITEM "+idItem+" -->\n";
    if (n) {
        MENUitem += "<td height=24 style=\"border:1px solid #999999; border-left:0px;\" align='center'>"
        MENUitem += "<ilayer name='"+idItem+"' width='100%'>";
        MENUitem += "<a href='.' class=clsMenuItemNS  onmouseover=\"hideAll();onTopMenu=1;if(currentSubMenu!='"+idItem+"'){clearBG(currentSubMenu);currentSubMenu='"+idItem+"';}displaySubMenu('"+idItem+"');"+idItem+".bgColor='#EBEBEB'\" onmouseout=\"onTopMenu=0\" onclick=\"return false;\">";
        MENUitem += "";
        MENUitem += text;
        MENUitem += "</a>";
        MENUitem += "</ilayer>";
        MENUitem += "</td>"
    }
    if (ie||ns6) {
        if (firstTopItem==0) {var strFirst=" style=\"border-left:0px;\" "; firstTopItem=1;}
        else {var strFirst="";}

        MENUitem += "<td class=\"tblMainMenu\" id='"+idItem+"' onclick=\"document.location='"+location+"'\" onmouseover=\"clearBG(currentSubMenu,currentSubMenuFolder);hideAll();onTopMenu=1;if(currentSubMenu!='"+idItem+"'){currentSubMenu='"+idItem+"';currentSubMenuFolder='"+"/menus"+"'}displaySubMenu('"+idItem+"');this.style.backgroundColor='#EBEBEB';this.style.cursor='hand'\" onmouseout=\"if(!hasSubMenu('"+idItem+"')){clearBG(currentSubMenu,currentSubMenuFolder);}onTopMenu=0\" height=24  align=\"center\">\n";
        MENUitem += "<div style='position:relative; height=24 font: "+this.menuFont+";'>\n";
        MENUitem += "<a ";
        MENUitem += "class=mainNavLink ";
//      MENUitem += "style='text-decoration: none; font: "+this.menuFont+"; color: "+this.fontColor+"; cursor: hand;' ";
        if (hint != null)
            MENUitem += "title='"+hint+"' ";
        if (location != null) {
            MENUitem += "href='"+location+"' ";
            MENUitem += "onmouseover=\"hideAll()\" ";
        } else {
            if (altLocation != null) {
                MENUitem += "href='"+altLocation+"' ";
            } else {
                MENUitem += "href='.' ";
            }
            MENUitem += "onmouseover=\"displaySubMenu('"+idItem+"');\" ";
            MENUitem += "onclick=\"return false;\" "
        }
        MENUitem += ">";
        MENUitem += "\n";
        MENUitem += text;
        MENUitem += "</a>\n";
        MENUitem += "</div>\n";
        MENUitem += "</td>\n";
    }
    MENUitem += "<!-- END OF ITEM "+idItem+" -->\n\n";
    MENUitem += "<!-- MAIN_MENU -->\n";

    HTMLstr = HTMLstr.replace("<!-- MAIN_MENU -->\n", MENUitem);

}

function hasSubMenu(id){
    for(var i=0;i<arr.length;i++) {
        if (arr[i]==id) return true;
    }
return false;
}
arr=new Array(0);
var firstTopItem=0;

function addSubItem(idParent, text, hint, location) {

    var MENUitem = "";
    Lookup = "<!-- ITEM "+idParent+" -->";
    if (HTMLstr.indexOf(Lookup) == -1)
    {
        //alert(idParent + " not found");
        //return;
    }

    Lookup = "<!-- NEXT ITEM OF SUB MENU "+ idParent +" -->";
    if (HTMLstr.indexOf(Lookup) == -1)
    {
        //if (n)
        //{
        //  MENUitem += "\n";
        //  MENUitem += "<layer id='"+idParent+"submenu' visibility=hide bgcolor='"+this.bgColor+"'>\n";
        //  MENUitem += "<table style=\"border-top:1px solid #999999\" border='"+this.subMenuPaneBorder+"' bgColor="+this.bgColor+" width="+this.subMenuPaneWidth+">\n";
        //  MENUitem += "<!-- NEXT ITEM OF SUB MENU "+ idParent +" -->\n";
        //  MENUitem += "</table>\n";
        //  MENUitem += "</layer>\n";
        //  MENUitem += "\n";
        //}
        if (ie||ns6)
        {
            MENUitem += "\n";
            MENUitem += "<div id='"+idParent+"submenu' onmouseout=operahide() style='position:absolute; visibility: hidden; z-index:100; width: "+this.subMenuPaneWidth+"; font: "+this.menuFont+"; top: -300;'>\n";
            MENUitem += "<table cellspacing=0 cellpadding=4 border='"+this.subMenuPaneBorder+"' bgcolor='"+this.bgColor+"' width="+this.subMenuPaneWidth+">\n";
            MENUitem += "<!-- NEXT ITEM OF SUB MENU "+ idParent +" -->\n";
            MENUitem += "</table>\n";
            MENUitem += "</div>\n";
            MENUitem += "\n";
        }
        MENUitem += "<!-- SUB_MENU -->\n";
        HTMLstr = HTMLstr.replace("<!-- SUB_MENU -->\n", MENUitem);
        if (!hasSubMenu(idParent)) {arr.length++;arr[arr.length-1]=idParent;}


    }
    
    Lookup = "<!-- NEXT ITEM OF SUB MENU "+ idParent +" -->\n";
    //if (n)  MENUitem = "<tr><td style=\"border:1px solid #999999;border-top:0px\" onmouseover=\"this.style.backgroundColor='#57749B';this.style.cursor='hand'\" onmouseout=\"this.style.backgroundColor='#6A89A6'\" onclick=\"document.location='"+location+"'\"><a class=clsMenuItemNS href='"+location+"'>"+text+"</a><br></td></tr>\n";
    if (ie||ns6) MENUitem = "<tr><td height=20 onclick=\"document.location='"+location+"'\" onmouseover=\"this.style.backgroundColor='#FFFFFF';this.style.cursor='hand'\" onmouseout=\"this.style.backgroundColor='#EBEBEB'\" class=\"tdMenuItem\"><a class=mainNavLink href='"+location+"'>"+text+"</a><br></td></tr>\n";
    MENUitem += Lookup;
    HTMLstr = HTMLstr.replace(Lookup, MENUitem);

}



function showMenu() {
    //document.writeln('<textarea>'+HTMLstr+'</textarea>')
    //alert(temp)
    document.writeln(HTMLstr);
}

////////////////////////////////////////////////////////////////////////////
// Private declaration
function displaySubMenu(idMainMenu) {
 //displaySubMenu2(idMainMenu);
 setTimeout("displaySubMenu2('" + idMainMenu + "')",menuDelayMs)
}

function displaySubMenu2(idMainMenu) {
 if (hasSubMenu(idMainMenu)) {
    ie ? eval(idMainMenu+"submenu.style.backgroundColor='#EBEBEB'")
            :document.getElementById(idMainMenu+"submenu").style.backgroundColor='#EBEBEB';
 }
//if (!(eval("window."+idMainMenu+"HasSubMenu"))) {eval(idMainMenu+"HasSubMenu=0")}
//if (eval("window."+idMainMenu+"HasSubMenu")) {

if (hasSubMenu(idMainMenu) && idMainMenu==currentSubMenu && onTopMenu==1) {

    if (n)
    {
        submenu = document.layers[idMainMenu+"submenu"];
        if (lastMenu != null && lastMenu != submenu) hideAll();
        submenu.left = document.layers[idMainMenu].pageX;
        submenu.top  = document.layers[idMainMenu].pageY + 27;
        submenu.visibility = fShow;

        leftX  = document.layers[idMainMenu+"submenu"].left;
        rightX = leftX + document.layers[idMainMenu+"submenu"].clip.width;
        topY = document.layers[idMainMenu+"submenu"].top-30
        bottomY  = topY+30+ //document.layers[idMainMenu+"submenu"].top
            document.layers[idMainMenu+"submenu"].clip.height;
        
    } else if (ie||ns6) {
        menu = ie? eval(idMainMenu) : document.getElementById(idMainMenu);
        submenu = ie? eval(idMainMenu+"submenu.style") : document.getElementById(idMainMenu+"submenu").style;
        submenu.left = calculateSumOffset(menu, 'offsetLeft');
        
        //check If submenu will go over the right edge of the page, If so, right align the submenu
        var ns6offset = ns6? 7 : 0;
        windowWidth = ie? document.body.clientWidth : window.innerWidth;
        var rightEdge=(windowWidth-750)/2 < 10 ? 760 : ((windowWidth-750)/2)+750-ns6offset;
        var leftPos= ie? document.all[idMainMenu+"submenu"].style.posLeft : parseInt(document.getElementById(idMainMenu+"submenu").style.left);
        submenu.left = (leftPos + submenuwidth > rightEdge) ? rightEdge-submenuwidth : submenu.left;
        
        //submenu.left  = eval(idMainMenu+".style.left+men.style.left");
        submenu.top  = calculateSumOffset(menu, 'offsetTop') + 27;
        //submenu.top  = menu.style.top+30;
        submenu.visibility = fShow;
        if (lastMenu != null && lastMenu != submenu) {lastMenuTemp=lastMenu;hideAll2(false);}
        leftX  = ie? document.all[idMainMenu+"submenu"].style.posLeft - document.body.scrollLeft: parseInt(document.getElementById(idMainMenu+"submenu").style.left);
        rightX = ie? leftX + Math.max(document.all[idMainMenu].offsetWidth,document.all[idMainMenu+"submenu"].offsetWidth) : leftX+Math.max(parseInt(document.getElementById(idMainMenu+"submenu").offsetWidth),menu.offsetWidth);
        topY= ie? calculateSumOffset(menu, 'offsetTop') - document.body.scrollTop   //document.all[idMainMenu+"submenu"].style.posTop+
             : calculateSumOffset(menu, 'offsetTop');//parseInt(document.getElementById(idMainMenu+"submenu").style.top);
        bottomY= ie? topY + document.all[idMainMenu+"submenu"].offsetHeight + 27
             : topY + parseInt(document.getElementById(idMainMenu+"submenu").offsetHeight)+27;
//alert(leftX + ' ' + rightX + ' ' + topY + ' ' + bottomY)
//      bottomY  = ie? calculateSumOffset(menu, 'offsetTop')+30+ //document.all[idMainMenu+"submenu"].style.posTop+
//          document.all[idMainMenu+"submenu"].offsetHeight - document.body.scrollTop: parseInt(document.getElementById(idMainMenu+"submenu").style.top)+parseInt(document.getElementById(idMainMenu+"submenu").offsetHeight);
//      topY= ie? calculateSumOffset(menu, 'offsetTop') - document.body.scrollTop//document.all[idMainMenu+"submenu"].style.posTop+
//           : parseInt(document.getElementById(idMainMenu+"submenu").style.top)+parseInt(document.getElementById(idMainMenu+"submenu").offsetHeight);
    }


    
    lastMenu = submenu;
}
if (!hasSubMenu(idMainMenu) && onTopMenu==1 && currentSubMenu==idMainMenu) {
    if (lastMenu!=null) {lastMenu.visibility=fHide;lastMenu.left=0;}
}
    selectVisibility();
}


function selectVisibility() {
    
    selectBoxes=ie?document.all.tags("SELECT"):document.getElementsByTagName("SELECT");
    for(i=0;i<selectBoxes.length;i++){
    if (submenu!=null) {
        selx=0; sely=0; var selp;
        if(selectBoxes[i].offsetParent){selp=selectBoxes[i]; while(selp.offsetParent){selp=selp.offsetParent; selx+=selp.offsetLeft; sely+=selp.offsetTop;}}
        selx+=selectBoxes[i].offsetLeft - (ie?document.body.scrollLeft:0);
        sely+=selectBoxes[i].offsetTop - (ie?document.body.scrollTop:0);
        selw=selectBoxes[i].offsetWidth; selh=selectBoxes[i].offsetHeight
        if(((selx+selw)>leftX && selx<(rightX))
        &&((sely+selh)>topY && sely<(bottomY))
        && submenu.visibility=="visible"){
            if(selectBoxes[i].style.visibility!="hidden"){
                //selectBoxes[i].level=l;
                selectBoxes[i].style.visibility="hidden";
                //this.hcode+=this.name+".sel["+i+"].style.visibility='visible';"
            }
            else {
                //selectBoxes[i].style.visibility="visible";
            }
        } else {
          selectBoxes[i].style.visibility = "visible";
        }
        //else if(l<=this.sel[i].level) this.sel[i].style.visibility="visible"
    }
    }
}

function noSubMOver(idMainMenu) {
         var coordY = ie?window.event.clientY:e.pageY;
         menu = ie? eval(idMainMenu) : document.getElementById(idMainMenu);

         var mtop= ie? calculateSumOffset(menu, 'offsetTop') - document.body.scrollTop   //document.all[idMainMenu+"submenu"].style.posTop+
             : calculateSumOffset(menu, 'offsetTop');//parseInt(document.getElementById(idMainMenu+"submenu").style.top);
         var mbottom= ie? mtop + 25
             : top + 25;
         return coordY>=mbottom||coordY<=mtop;
}

function hideAll() {
    lastMenuTemp=lastMenu;
    hideAll2(true);
}

function hider() {
    if (lastMenuTemp!=null) {lastMenuTemp.visibility=fHide;lastMenuTemp.left=0;}
    selectVisibility();
    
    if (ie) {//ie ? eval(currentSubMenu+"submenu.style.visibility==fHide"):document.getElementById(currentSubMenu+"submenu").style==fHide){ //&& ( (ie ? eval(currentSubMenu+"submenu.style") : document.getElementById(currentSubMenu).style) == lastMenuTemp)){
        //if (hasSubMenu(currentSubMenu)) {
        //if(eval(currentSubMenu+"submenu.style.visibility==fHide"))
          //if (onTopMenu==0) clearBG(currentSubMenu,currentSubMenuFolder);
        //}
    }
    else {
       //if (hasSubMenu(currentSubMenu)) {
       //if (document.getElementById(currentSubMenu+"submenu").style.visibility==fHide)
         //if (onTopMenu==0) clearBG(currentSubMenu,currentSubMenuFolder);
       //}
    }
    if (!hasSubMenu(lastShownMenuTemp)) {
        if (lastShownMenuTemp!=currentSubMenu || onTopMenu==0) {
            clearBG(lastShownMenuTemp,lastShownFolderTemp);
        }
    } else {
        clearBG(lastShownMenuTemp,lastShownFolderTemp);
    }
}

function hideAll2(isDelay) {

    if (lastMenuTemp != null) {
       
       if (isDelay) {
          setTimeout("hider()",menuDelayMs);
       }
       else {
          hider();
       }
        
    }
    else {
       //clearBG(currentSubMenu,currentSubMenuFolder);
    }

       lastShownMenuTemp=lastShownMenu;
       lastShownFolderTemp=lastShownFolder;
}

function calculateSumOffset(idItem, offsetName) {

    var totalOffset = 0;
    var item = eval('idItem');
    do {
        totalOffset += eval('item.'+offsetName);
        item = eval('item.offsetParent');

    } while (item != null);
    return totalOffset;
}

function updateIt(e) {

    if (ie&&!opr6)
    {
        var x = window.event.clientX;
        var y = window.event.clientY;

        if (x > rightX || x < leftX) hideAll();
        else if (y > bottomY || y < topY) hideAll();
    }
    if (n||ns6)
    {
        var x = e.pageX;
        var y = e.pageY;


        if (x > rightX || x < leftX) hideAll();
        else if (y > bottomY || y < topY) hideAll();
    }
}

function operahide(){
        if (opr6){
        if (!MainTable.contains(event.toElement))
        hideAll()
}
}

if (ie||ns6)
{
    document.body.onclick=hideAll;
    document.body.onscroll=hideAll;
    document.body.onmousemove=updateIt;
}


if (document.layers)
{
    window.captureEvents(Event.MOUSEMOVE);
    window.captureEvents(Event.CLICK);
    window.onmousemove=updateIt;
    window.onclick=hideAll;
}