
function stv_getAnchorPosition(anchorId)
{
	var point=new Object();

	var o=document.getElementById(anchorId);
	if(o)
	{
		point.x=stv_getPageOffsetLeft(o);
		point.y=stv_getPageOffsetTop(o);
   }
	else
	{
		point.x=0;
		point.y=0;
	}

/*
   // Add in window offset
   if (isNaN(window.screenX))
   {
      point.x = point.x-document.body.scrollLeft+window.screenLeft;
      point.y = point.y-document.body.scrollTop+window.screenTop;
   }
   else
   {
      point.x=point.x+window.screenX+(window.outerWidth-window.innerWidth)-window.pageXOffset;
      point.y=point.y+window.screenY+(window.outerHeight-24-window.innerHeight)-window.pageYOffset;
   }
*/

   return point;
}


function stv_getPageOffsetLeft (el)
{
	var ol=el.offsetLeft;
	while ((el=el.offsetParent) != null) { ol += el.offsetLeft; }
	return ol;
}

function stv_getPageOffsetTop (el)
{
	var ot=el.offsetTop;
	while((el=el.offsetParent) != null) { ot += el.offsetTop; }
	return ot;
}

//placeholder for the active submenu
visibleMenu = null;

// Background colors
var COLOR_SEL = "#f1f1c4"; // f0f0f0
var COLOR_NORMAL = "#ffffff";
var TEXT_NORMAL = "#000000";
var TEXT_HILITE = "#000000";

function showSubMenu(event, index, tableId)
{
	
/*
   var div = document.getElementById("subMenu" + tableId + "_" + index);
   div.style.display = "block";
   div.style.left = event.clientX - 10;
   div.style.top = event.clientY + 10;

return;
*/

   if(!event) event = window.event;
   
   var div = document.getElementById("menu" + tableId + "_" + index);
   if(div==null)
   {
      createHeaderMenu(""+tableId + "_"+index, 185, 187, tableId);
      div = document.getElementById("menu" + tableId + "_" + index);
   }

   div.showDiv();
   div.menuOn();
   div.rows = new Array();
   for(i=1; i<=div.itemCount; i++)
   {
   	div.rows[i-1] = document.getElementById("row"+tableId + "_"+index+"_"+i);
      div.rows[i-1].onmouseover();
   }
   div.rows[0].onmouseover();
//   div.selectedRow = 0;

   div.style.position = "absolute";

   var source = (event.target ? event.target : event.srcElement);
   div.source = source;

   div.style.left = stv_getPageOffsetLeft(source); //event.clientX - 10;
   div.style.top = 20+ stv_getPageOffsetTop(source); //event.clientY + 10;
   visibleMenu = div;
}

function selectAllRows(masterCheckbox, tableId)
{
   var i=0;
   var cb = document.getElementById("table_cb_" + tableId + "_" + i);
   while(cb)
   {
      cb.checked = masterCheckbox.checked;
      selectRow(i, tableId, masterCheckbox.checked);
      i++;
      cb = document.getElementById("table_cb_" + tableId + "_" + i);
   }
}


function selectRow(index, tableId, isChecked)
{
   var row = document.getElementById("table_row_" + tableId + "_" + index);
   if(isChecked)
   {
      row.bgColor=COLOR_SEL;
      row.style.color=TEXT_HILITE;
   }
   else
   {
      row.bgColor=COLOR_NORMAL;
      row.style.color=TEXT_NORMAL;
   }
}

function toggleCheckbox(cb, index, tableId)
{
   var row = document.getElementById("table_row_" + tableId + "_" + index);

   if(cb.checked)
   {
      row.bgColor=COLOR_SEL;
      row.style.color=TEXT_HILITE;
   }
   else
   {
      row.bgColor=COLOR_NORMAL;
      row.style.color=TEXT_NORMAL;
   }
}

function getSelectedTableRows(tableId)
{
   var list = new Array();

   var i=0;
   var done = false;
   while(true)
   {
      var cb=document.getElementById('table_cb_'  + tableId + "_" + i);

      if(!cb)
         break;

      if(cb.checked) list[list.length] = i;
      i++;
   }
   return list;
}


ss = "";  // MREMIND: fix
userColor1 = "#458ab9";
userColor2 = "orange";
var stv_timeout = 500;

var cM=null;
var mHT=null;     // timeout pointer
var mouseOverMenu=false;
var tM=[];        // list of all menus



// Create Header Menu
function createHeaderMenu(menuId,xPos,mWidth, tableId)
{
   cM=null;
   cMT={width:mWidth,id:menuId,x:xPos,y:85};
   tM[tM.length]=createMenu(menuId, cMT, tableId);
}


// main or child menu
function createMenu(menuId,cMT, tableId)
{
   var specialText=null;
   var lyr=document.createElement("DIV");
   // added to fix bugs with mozilla.  Should not needed for ie and firefox fixes menu resize issues.
   var b=document.getElementsByTagName('body')[0];
   var span = document.createElement("span");
   b.appendChild(span);
   lyr.id="menu"+menuId;
   lyr.a=eval("menu_"+menuId);   // children
   
   //set the width of the menu dynamically
   var childScrollWidth = 0;
   for(i=0; i<lyr.a.length; i++)
   {
   	span.innerHTML=lyr.a[i][0];
   	if(span.scrollWidth > childScrollWidth)
   	{
   		childScrollWidth = span.scrollWidth;
   	}
   }
   b.removeChild(span);
   //use cMT.width as the minimum width
   if(cMT.width < childScrollWidth)
   {
   	iman.debug("Resizing menu width to "+ childScrollWidth);
   	cMT.width = childScrollWidth; 
   }
   
   lyr.itemCount=0;
   lyr.tree=cMT;
   lyr.hasCVisible=false;
   lyr.isOn=false;
   lyr.currentItem=null;

   // methods
   lyr.showDiv=showDiv;
   lyr.hideDiv=hideDiv;
   lyr.onmouseover=menuOn;
   lyr.menuOn=menuOn;
   lyr.onmouseout=menuOut;
   lyr.hideTree=hideTree;
   lyr.hideChildren=hideChildren;

   var l_s=lyr.style;
   l_s.width=cMT.width+"px";
   l_s.position="absolute";
   l_s.padding="0px";
   l_s.margin="0px";

   if(cM)
   {
      l_s.visibility="hidden";
      l_s.left=l_s.top="-200px";
      l_s.zIndex=cM.style.zIndex+5;
      lyr.parentMenu=cM;
      lyr.level=lyr.parentMenu.level+1;
      lyr.parentRow=cM.row;
      lyr.parentRow.child=lyr;
      lyr.hasParent=true;
   }
   else
   {
      l_s.backgroundColor=userColor1;
      l_s.visibility="hidden";
      l_s.left=cMT.x+"px";
      l_s.top=cMT.y+"px";
      l_s.zIndex=1001;
      cMT.treeParent=cMT.startChild=lyr;
      lyr.level=2;
   }

// AWL   lyr.style.border = "1px solid #b5b5b5";
   lyr.style.border = "1px solid #000";

// AWL   if(lyr.level==2)l_s.backgroundColor="#EDEEEC"; //EDEEEC b5b5b5
   if(lyr.level==2)l_s.backgroundColor="#fff"; //EDEEEC b5b5b5 This defines the background color of the menu
   if(lyr.level==3)l_s.backgroundColor=userColor1;

   cM=lyr;
   var itr=cM.a.length;
   while(itr--)
   {
      cM.itemCount++;
      var row=createRow(menuId, cMT);
      if(row)
      {
         cM.row=row;
         if(row.hasMore)
         {
            var childMenu=createMenu(menuId+"_"+cM.itemCount,cMT, tableId);
            if(childMenu)cM=cM.parentMenu;
         }

         if(row.index)
         {
            var sb_s=row.siblingBelow.style;
            row.style.top=(parseInt(sb_s.top)+parseInt(sb_s.height.substring(0,sb_s.height.indexOf("px"))))+"px";
         }
         else
         row.style.top=(cM.level==1)?"3px":"10px";
      }
   }

   cM.style.height=(cM.childNodes.length*20)+((cM.level>1)?23:6)+"px";
   if(cM.childNodes.length==0)cM.style.backgroundColor="";
   if(cM.level==2)
   {
      var stripe=document.createElement("DIV");
      var s_st=stripe.style;
      s_st.backgroundColor=userColor1;
      s_st.top=(cM.childNodes.length*20)+20+"px";
      s_st.position="absolute";
      s_st.visibility="inherit";
      s_st.height="3px";
      s_st.lineHeight="3px";
      s_st.left="0px";
      s_st.width=cMT.width - 2 +"px";
      s_st.padding="0px";
      s_st.margin="0px";
      stripe.innerHTML='<img src="'+ss+'/img/spacer.gif" width="1" height="1" border="0" alt="">';
      cM.appendChild(stripe);
   }

   document.getElementById("menuDivs_" + tableId).appendChild(lyr);
   return cM;
}


function createRow(menuId, cMT)
{
   //var fc=new Array("#FFFFFF","#333333","#FFFFFF","#FFFFFF");
   //var fc_o=new Array("#333333","#FFFFFF","#FFFFFF","#FFFFFF");
   var fc=new Array("#000000","#000000","#000000","#000000");
   var fc_o=new Array("#FFFFFF","#FFFFFF","#FFFFFF","#FFFFFF");
   //var bg=new Array(userColor1,"#EDEEEC",userColor1,userColor2);
   //var bg_o=new Array("#EDEEEC",userColor1,userColor2,"#2E3436");
// AWL   var bg=new Array(userColor1,"#EDEEEC",userColor1,userColor2);
// AWL   var bg_o=new Array("#EDEEEC",userColor1,userColor2,"#2E3436");
   var bg=new Array(userColor1,"#fff",userColor1,userColor2); // This defines the actual menu item
   var bg_o=new Array("#fff",userColor1,userColor2,"#2E3436"); // This defines the actual menu item on roll over
   var nline='<img src="'+ss+'/img/ni_h_leftnav-horizbar_w.gif" width="183" height="1" border="0" alt="">';
   var row=document.createElement("DIV");

   row.id="row"+menuId+"_"+cM.itemCount;
   row.menu=cM;
   row.tree=cMT;
   row.index=cM.itemCount-1;
   row.child=null;
   row.onmouseover=rowOn;
   row.onmouseout=rowOut;
   row.showChildMenu=showChildMenu;
   row.a=cM.a[cM.itemCount-1];
   row.hasMore=row.a[2];

   row.txt=row.a[0];
   row.linktxt=row.a[1];
   if(row.linktxt==null)
      return null;

   if(typeof row.linktxt=='undefined')
      row.linktxt="";
   row.isSecHeading=!(row.linktxt.length>0||row.hasMore);
   row.showF=true;
   for(var i=cM.itemCount+1;i<cM.a.length;i++)
   {
      if(cM.a[i][1]!=null)row.showF=false;
   }

   if(typeof row.linktxt=='undefined')
      row.linktxt="";

   row.fc=fc[cM.level-1];
   row.bg=bg[cM.level-1];
   if(row.isSecHeading)
   {
      row.fc_o=row.fc;
      row.bg_o=row.bg;
   }
   else
   {
      row.fc_o=fc_o[cM.level-1];
      row.bg_o=bg_o[cM.level-1];
   }

   var r_s=row.style;
   r_s.position="absolute";
   r_s.visibility="inherit";
   r_s.backgroundColor=row.bg;
   r_s.color=row.fc;
   r_s.fontFamily="Arial,sans-serif";
   r_s.fontSize="11px";
   r_s.height="20px";
   r_s.lineHeight="18px";
   r_s.left="0px";
   r_s.width=cMT.width - 2 +"px";
   r_s.padding="0px";
   r_s.margin="0px";
   if(row.linktxt.length>0)
   {
      row.onclick=goTo;
      //r_s.cursor=setCursor(); // MREMIND: fix
      //var r_cont='<a href="#" onClick="'+row.linktxt+'" style="text-decoration:none;" id="'+row.id+'_link">&nbsp;&nbsp;</a><span style="cursor:default">' + row.txt + "</span>";
      var r_cont='<a href="#" style="text-decoration:none;cursor:default;color:inherit;" id="'+row.id+'_link">' + row.txt + '</a>';
   }
   else
   {
      r_s.cursor="default";
      var r_cont='&nbsp;&nbsp;'+row.txt;
   }

   if(cM.level==1)
   {
      r_cont=nline+"<br>"+r_cont;
      if(row.showF)
         r_cont+="<br>"+nline;
   }
   else
      r_cont="&nbsp;&nbsp;"+r_cont;

   row.innerHTML=r_cont;
   if(row.hasMore)
   {
      row.imageSrc=(cM.level==1)?ss+"/img/ni_h_leftnav_submenuarrow_w.gif":ss+"/img/ni_h_leftnav_submenuarrow.gif";
      row.imageSrcOver=ss+"/img/spacer.gif";
      var img=document.createElement("IMG");
      img.src=row.imageSrc;
      img.height=img.width=14;
      var i_s=img.style;
      i_s.position="absolute";
      i_s.top="3px";
      i_s.left=(row.tree.width-18)+"px";
      row.imageLayer=img;
      row.insertBefore(row.imageLayer,row.firstChild);
   }

   cM.appendChild(row);
   row.siblingBelow=row.previousSibling;
   return row;
}

function menuOn()
{
   if(mHT!=null) clearTimeout(mHT);
   if(this.tree.startChild==this)
      hideTMs(this);
   cM=this;
   mouseOverMenu=true;
   this.isOn=true;
}

function menuOut()
{
   mouseOverMenu=false;
   this.isOn=false;
   mHT=setTimeout("cM.hideTree();", stv_timeout);
}

function hideTMs(visibleM)
{
   for(var i=tM.length-1;i>=0;i--)
   {
      var topMenu=tM[i].tree.startChild;
      if(topMenu!=visibleM)
      {
         topMenu.isOn=false;
         if(topMenu.hasCVisible)topMenu.hideChildren();
         topMenu.hideDiv();
      }
   }
}

// member method of row
function showChildMenu()
{
   var menu=this.menu;
   var c_s=this.child.style;
   c_s.top=(parseInt((menu.style.top)?menu.style.top:menu.offsetTop)+this.offsetTop)+"px";
   c_s.left=(parseInt((menu.style.left)?menu.style.left:menu.offsetLeft)+parseInt(this.style.width))+"px";
   menu.hasCVisible=true;
   menu.visibleChild=this.child;
   this.child.showDiv();
}

// member method of row; called onmouseover
function rowOn()
{
   this.style.backgroundColor=this.bg_o;
   this.style.color=this.fc_o;
   var link = document.getElementById(this.id+"_link");
   link.focus();
   link.style.color=this.fc_o;
   if(this.menu.hasCVisible)
   {
      var v_c=this.menu.visibleChild;
      if(v_c==this.child&&v_c.hasCVisible)
         v_c.hideChildren(this);
      else
         this.menu.hideChildren(this);
   }

   if(this.menu.currentItem&&this.menu.currentItem!=this)
   {
      var current=this.menu.currentItem;
      current.style.backgroundColor=current.bg;
      current.style.color=current.fc;
      var curLink = document.getElementById(current.id+"_link");
      curLink.style.color=current.fc;
      if(current.hasMore)
         current.imageLayer.src=current.imageSrc;
   }

   this.menu.currentItem=this;
   if(this.hasMore)
   {
      this.imageLayer.src=this.imageSrcOver;
      this.showChildMenu();
   }

   status=this.linktxt;
}

// member method of row; called onmouseout
function rowOut()
{
   if((this.tree.treeParent==this)&&!this.menu.hasCVisible)
   {
      this.style.backgroundColor=this.bg;
      this.style.color=this.fc;
      var link = document.getElementById(this.id+"_link");
      link.style.color=this.fc;
      if(this.hasMore)this.imageLayer.src=this.imageSrc;
   }
}

// member of the DIV
function showDiv()
{
   clearTimeout(mHT);
   hideSelects();
   this.style.visibility="visible";
}

// member of the DIV
function hideDiv()
{
   this.style.visibility="hidden";
   visibleMenu = null;
   
   if(this.currentItem)
   {
      var current=this.currentItem;
      current.style.backgroundColor=current.bg;
      current.style.color=current.fc;
      if(current.hasMore)
         current.imageLayer.src=current.imageSrc;
   }

   this.currentItem=null;
/*
   if(this.tree.treeParent==this)
   {
      var hdrimg=eval("document.hdr"+this.id.substring(4));
      if(hdrimg)
         hdrimg.src=ss+"/img/noflashhdr/"+themeDir+"/"+this.id.substring(4)+".gif";
      else
      {
         if(this.id=="menucommander")
         {
            if(this.navid)resetHdrLink("hdr"+this.navid);
         }
         else
            resetHdrLink("hdr"+this.id.substring(4));
      }
   }
*/
}

function resetHdrLink(id)
{
   //hdrelmt=document.getElementById(id);
   //hdrelmt.style.backgroundColor="";
   //hdrelmt.style.color="#fff";
}

// starts at lowest menu and hides whole tree
function hideTree()
{
   try
   {
      mHT=null;
      if(!mouseOverMenu)
      {
         if(this.hasCVisible)
            this.hideChildren(this);
         var menu=this;
         while(menu.hasParent)
         {
            menu.hideDiv();
            menu.parentMenu.isOn=false;
            menu=menu.parentMenu;
         }

         menu.hideDiv();
         showSelects();
      }
   }
   catch(e)
   {
      // do nothing;
   }
}

function hideChildren(caller)
{
   var menu=this.visibleChild;
   while(menu.hasCVisible)
   {
      menu.visibleChild.hideDiv();
      menu.hasCVisible=false;
      menu=menu.visibleChild;
   }

   if((caller&&(!caller.hasMore||this.visibleChild!=caller.child))||(!caller&&!this.isOn))
   {
      this.visibleChild.hideDiv();
      this.hasCVisible=false;
   }
}

function goTo()
{
   //if(this.linktxt.indexOf("javascript:")!=-1)
      eval(this.linktxt);
   //else
   //   window.location.href=this.linktxt;
}


// hide all select boxes (so divs don't mess up in IE/Opera)
function hideSelects()
{
   for(var i=document.forms.length-1;i>=0;i--)
   {
      var form=document.forms[i];
      for(var j=form.elements.length-1;j>=0;j--)
      {
         var elmt=form.elements[j];
         if(elmt.type=="select-one"||elmt.type=="select-multiple")elmt.style.visibility="hidden";
      }
   }
}


// show divs hidden by hideF
function showSelects()
{
   for(var i=document.forms.length-1;i>=0;i--)
   {
      var form=document.forms[i];
      for(var j=form.elements.length-1;j>=0;j--)
      {
         var elmt=form.elements[j];
         if(elmt.type=="select-one"||elmt.type=="select-multiple")elmt.style.visibility="inherit";
      }
   }
}

// optional function to resize the table to fit the window
// DO NOT DELETE THIS FUNCTION!
function resizeTableToWindow(objectID, buffer)
{ 
   // The optional buffer allows the user to set the distance from the bottom
   // of the page that the dataTable will stretch to. If not set, buffer = 0.
   if(buffer == null)
   {
      buffer = 0;
   }
   
   // Get the dataTable div object in the most flexible way possible.
   // It accepts a string representing the table ID or the table object itself.
   var obj = null;
   if(typeof(objectID) == "string")
   {
      obj = eval("document.table_"+objectID);
      if(!obj)
      {
         obj = document.getElementById(objectID);
      }
   }
   else
   {
      obj = objectID;
   }
   
   originalObj = obj;
   
   //get the screen vertical offset of the table
   var tableOffset = 0;
   if (obj.offsetParent)
   {
      while (obj.offsetParent)
      {
         tableOffset += obj.offsetTop
         obj = obj.offsetParent;
      }
   }
   
   //get the viewport height
   var vpHeight = 0;
   if(self.innerHeight)
   {
      vpHeight = self.innerHeight;
   }
   else if(document.documentElement && document.documentElement.clientHeight)
   {
      vpHeight = document.documentElement.clientHeight;
   }
   else if(document.body && document.body.clientHeight)
   {
      vpHeight = document.body.clientHeight;
   }
   
   tableHeight = vpHeight - tableOffset - buffer;
   originalObj.style.height = tableHeight + "px";
}

//This function allows the menu system to be navigated with a keyboard
function navigateTree(event)
{
	if(!event) event = window.event;

	//hide the menu when ESC is pressed
	if(event.keyCode == 27  && visibleMenu)
	{
		visibleMenu.source.focus();
		visibleMenu.hideDiv();
		visibleMenu = null;
	}
	
	//move the highlighter down when DOWN ARROW or TAB are pressed
	if((event.keyCode == 40 || (event.keyCode == 9 && !event.shiftKey)) && visibleMenu)
	{
		newRow = visibleMenu.rows[visibleMenu.selectedRow+1];
		if(newRow)
		{
			visibleMenu.rows[visibleMenu.selectedRow].onmouseout();
			newRow.onmouseover();
			visibleMenu.selectedRow++;
		}
		else
		{
			newRow = visibleMenu.rows[0];
			newRow.onmouseover();
			visibleMenu.selectedRow = 0;
		}
	}
	
	//move the highlighter up when UP ARROW or SHIFT+TAB are pressed
	if((event.keyCode == 38 || (event.keyCode == 9 && event.shiftKey)) && visibleMenu)
	{
		newRow = visibleMenu.rows[visibleMenu.selectedRow-1];
		if(newRow)
		{
			visibleMenu.rows[visibleMenu.selectedRow].onmouseout();
			newRow.onmouseover();
			visibleMenu.selectedRow--;
		}
		else
		{
			visibleMenu.source.focus();
			visibleMenu.hideDiv();
			visibleMenu = null;
		}
	}
	return true;
}

//this is necessary to keep focus on the submenu as long as it is visible
if(window.Event)
{
	document.captureEvents(Event.ONKEYUP);
}
document.onkeyup = navigateTree;
