var IsNN4;
var IsIE4;
var IsWin;
var IsMac;
var IsOtherOS;
user_agent = navigator.userAgent.toLowerCase();
IsNN4 = (document.layers) ? true : false;
IsIE4 = (document.all)    ? true : false;
IsWin = (user_agent.indexOf("win") != -1) ? true : false;
IsMac = (user_agent.indexOf("mac") != -1) ? true : false;
IsOtherOS = !IsWin && !IsMac              ? true : false;

function imkGetLastStyleAttr(selector, attr)
{
  var i;
  var j;
  var theAttr = "";
  for(i = 0; i < document.styleSheets.length; i++)
    for(j = 0; j < document.styleSheets(i).rules.length; j++)
      if(document.styleSheets(i).rules(j).selectorText == selector)
      {
        var attVal = eval("document.styleSheets(" + i + ").rules(" + j + ").style." + attr);
        if(attVal)
          theAttr = attVal;
      }
  return theAttr;
}

var NODE_IMG_HEIGHT = 18;
var NODE_IMG_WIDTH = 18;
var strNodeSize = " WIDTH=" + NODE_IMG_WIDTH + " HEIGHT=" + NODE_IMG_HEIGHT + " ALIGN=textTop BORDER=0"
var sImageDir = "/images/";
var strTeeSrc;
var strCornerSrc;
var strLineSrc;
var strSpaceSrc;
var strM_TeeSrc;
var strM_CornerSrc;
var strP_TeeSrc;
var strP_CornerSrc;
var str_GoBtnSrc;
var str_GoBtnOnSrc;

function imkNavTree(myName, nodeParams)
{ 
  this.Init = imkNavTree_Init;
  this.Setup = imkNavTree_Setup;
  this.DrawTree = imkNavTree_DrawTree;
  this.CreateNode = imkNavTree_CreateNode;
  this.AddLast = imkNavTree_AddLast;
  this.AddChild = imkNavTree_AddChild;
  this.NodeClick = imkNavTree_NodeClick;
  this.NodeDblClick = imkNavTree_NodeDblClick;
  this.NodeLinkClick = imkNavTree_NodeLinkClick
  this.NodeLinkDblClick = imkNavTree_NodeLinkDblClick;
  this.NodeCollapse = imkNavTree_NodeCollapse;
  this.NodeExpand = imkNavTree_NodeExpand;
  this.MoveBelow = imkNavTree_MoveBelow;
  this.MoveBranch = imkNavTree_MoveBranch;
  this.SetImgBasePath = imkNavTree_SetImgBasePath;
  this.DrawBranch = imkNavTree_DrawBranch;
  this.SwitchGoBtn = imkNavTree_SwitchGoBtn;
  this.strMyName = myName;
  this.NodeCount = 0;
  this.NodeIndex = new Array;
  this.RootNode = null;
  this.Top = 0;
  this.Left = 0;
  this.cx = 0;
  this.cy = 0;
  this.Height = 0;
  this.Width = 0;
  this.SelectedNode = 0;
  this.Margin = 5;
  this.BackColor = "white";
  this.BackBorderColor = "silver";
  this.BackBorderStyle = "inset";
  this.BackBorderWidth = 1;
  this.ImgBasePath = "";
  this.SelectEvent = "";
  this.DefTextFace = "Arial";
  this.DefTextSize = "10pt";
  this.DefTextWeight = "normal";
  this.DefTextColor = "black";
  this.DefSelTextSize = "10pt";
  this.DefSelTextWeight = "normal";
  this.DefSelTextColor = "white";
  this.DefSelColor = "navy";
  this.RootNode = this.CreateNode(nodeParams);
}

function imkNavTree_Init()
{
  if(IsIE4 == true)
  theTree.DrawTree();
}
function imkNavTree_Setup()
 { 
  this.strDocObj = "document.all"; 
  if(this.Height > 0 && this.Width > 0)
  {
  }
}

function imkNavTree_DrawTree()
{ 
  if(this.ImgBasePath == "")
  this.SetImgBasePath();
  if(this.Height > 0 && this.Width > 0)
  { 
    this.cx = this.Margin;
    this.cy = this.Margin;
    var str = "<DIV ID=" + this.strMyName + " CLASS=Back></DIV>";
    document.body.insertAdjacentHTML("BeforeEnd", str);
  }
  else 
  {
    this.cx = this.Left;
    this.cy = this.Top;
  }
  this.RootNode.Draw(this.cy);
  this.RootNode.Show();
  this.RootNode.IsSelected = true;
  this.RootNode.SwitchSelect();
  this.RootNode.SwitchSelect();
  this.NodeExpand(this.RootNode);
}

function imkNavTree_CreateNode(nodeParams)
{
  var newNode = new Node(this, this.NodeCount, nodeParams);
  this.NodeIndex[this.NodeCount] = newNode;
  this.NodeCount++;
  return newNode;
}

function imkNavTree_AddLast(relative, nodeParams)
{
  var node;
  var newNode;
  newNode = this.CreateNode(nodeParams);
  if(relative == null)
    node = this.RootNode;
  else
    node = relative;
  while(node.NextNode != null)
    node = node.NextNode;
  newNode.Parent = node.Parent;
  node.NextNode = newNode;
  return newNode;
}

function imkNavTree_AddChild(relative, nodeParams)
{
  var node;
  var newNode;
  if(relative == null)
    node = this.RootNode;
  else
   node = relative;
  if(node.ChildNode == null)
  {
    newNode = this.CreateNode(nodeParams);
    newNode.Parent = node;
    node.ChildNode = newNode;
  }
  else
  {
    newNode = this.AddLast(node.ChildNode, nodeParams);
  }
  return newNode;
}

function imkNavTree_NodeClick(nodeID)
{
  this.NodeLinkClick(nodeID);
  var node = this.NodeIndex[nodeID];
  if(node.IsExpanded == true)
    this.NodeCollapse(node);
  else
   this.NodeExpand(node);
}

function imkNavTree_NodeDblClick(nodeID)
{
 var node = this.NodeIndex[nodeID];
 
 if(node.HasChildren())
 {
 if(node.IsExpanded == true)
 this.NodeCollapse(node);
 else
 this.NodeExpand(node);
 }
 else
 {
 this.NodeLinkDblClick(nodeID);
 } 
}

function imkNavTree_NodeLinkClick(nodeID)
{ 
 this.NodeIndex[nodeID].SwitchSelect();
 this.NodeIndex[this.SelectedNode].SwitchSelect();
 this.SelectedNode = nodeID;

 
 if(this.NodeIndex[nodeID].strClickAction != "")
 eval(this.NodeIndex[nodeID].strClickAction);
  if(this.SelectEvent != "")
 eval(this.SelectEvent);
}
function imkNavTree_NodeLinkDblClick(nodeID)
{
 var node = this.NodeIndex[nodeID];
 if(node.HasChildren())
 {
 if(node.IsExpanded == true)
 this.NodeCollapse(node);
 else
 this.NodeExpand(node);
 }
 
 if(node.strDblClickAction != "")
 {
 eval(node.strDblClickAction);
 }
}
function imkNavTree_NodeCollapse(node)
{
 var theDocObj;
 
 
 if(node.IsExpanded == true)
 {
 
 if(node.HasChildren() == true)
 { 
 var child = node.ChildNode; 
 var dy = 0;
 
 while(child != null) 
 {
 this.NodeCollapse(child); 
 child.Hide(); 
 this.MoveBranch(child, -NODE_IMG_HEIGHT);
 child = child.NextNode;
 dy -= NODE_IMG_HEIGHT;
 }
 this.MoveBelow(node, dy);
 
 
 theDocObj = eval("document");
 
 if(node != this.RootNode)
 {
 if(node.IsEnd() == true)
 theDocObj.images["nodeLine" + node.ID].src = strP_CornerSrc;
 else
 theDocObj.images["nodeLine" + node.ID].src = strP_TeeSrc;
 }
 
 
 if(node.strIconNormalSrc != "")
 theDocObj.images["nodeIcon" + node.ID].src = node.strIconNormalSrc;
 
 node.IsExpanded = false;
 } 
 }
}
function imkNavTree_NodeExpand(node)
{
 var theDocObj;
 
 if(node.IsExpanded == false)
 {
 
 if(node.Parent != null)
 this.NodeExpand(node.Parent);
 
 
 if(node.HasChildren() == true)
 {
 var child = node.ChildNode; 
 var dy = 0;
 
 
 if(!child.IsDrawn)
 this.DrawBranch(child, node.GetTop());
 
 
 while(child != null) 
 { 
 this.MoveBranch(child, NODE_IMG_HEIGHT); 
 child.Show(); 
 child = child.NextNode;
 dy += NODE_IMG_HEIGHT;
 }
 
 
 this.MoveBelow(node, dy);
 
 
 theDocObj = eval("document");

 if(node != this.RootNode)
 {
 if(node.IsEnd() == true)
 theDocObj.images["nodeLine" + node.ID].src = strM_CornerSrc;
 else
 theDocObj.images["nodeLine" + node.ID].src = strM_TeeSrc;
 }
 
 
 if(node.strIconOpenSrc != "")
 theDocObj.images["nodeIcon" + node.ID].src = node.strIconOpenSrc;
 
 node.IsExpanded = true; 
 }
 }
}
function imkNavTree_MoveBelow(node, dy)
{
 if(node != this.RootNode)
 {
 
  this.MoveBelow(node.Parent, dy)
  node = node.NextNode;
 this.MoveBranch(node, dy);
 }
}
function imkNavTree_MoveBranch(node, dy)
{
 while(node != null && node.IsDrawn)
 {
 
 this.MoveBranch(node.ChildNode, dy);
 
 
 node.MoveBy(0, dy);
 node = node.NextNode;
 }
}
function imkNavTree_DrawBranch(node, top)
{
 while(node != null)
 {
 if(!node.IsDrawn)
 node.Draw(top);
 node = node.NextNode;
 }
}
function imkNavTree_SetImgBasePath(thePath)
{
  strTeeSrc      = sImageDir + "tee.gif";
  strCornerSrc   = sImageDir + "corner.gif";
  strLineSrc     = sImageDir + "line.gif";
  strSpaceSrc    = sImageDir + "space.gif";
  strM_TeeSrc    = sImageDir + "m_tee.gif";
  strM_CornerSrc = sImageDir + "m_corner.gif";
  strP_TeeSrc    = sImageDir + "p_tee.gif";
  strP_CornerSrc = sImageDir + "p_corner.gif";
  str_GoBtnSrc   = sImageDir + "go_btn.gif"; 
  str_GoBtnOnSrc = sImageDir + "go_btn_on.gif"; 
} 

function imkNavTree_SwitchGoBtn(nodeID, sw)
{
 var node = this.NodeIndex[nodeID]; 
 eval("node.GoBtn" + sw + "()");
} 

function Node(treeObj, id, nodeParams)
{ 
  this.HasChildren = Node_HasChildren;
  this.IsEnd = Node_IsEnd;
  this.Draw = Node_Draw;
  this.Hide = Node_Hide;
  this.Show = Node_Show;
  this.SwitchSelect = Node_SwitchSelect;
  this.MoveTo = Node_MoveTo;
  this.MoveBy = Node_MoveBy;
  this.GetTop = Node_GetTop;
  this.GoBtnOn = Node_GoBtnOn;
  this.GoBtnOff = Node_GoBtnOff;
  this.TreeObj = treeObj;
  this.ID = id 
  this.NextNode = null;
  this.ChildNode = null;
  this.Parent = null;
  this.IsExpanded = false;
  this.IsSelected = false;
  this.IsDrawn = false;
  var paramArray = nodeParams.split(";");
  if(paramArray[0])
    this.strDesc = paramArray[0];
  else
    this.strDesc = "";
  if(paramArray[1])
    this.StyleID = paramArray[1];
  else
    this.StyleID = 0; 
  if(paramArray[2])
    this.strIconNormalSrc = sImageDir + paramArray[2];
  else
    this.strIconNormalSrc = "";
  if(paramArray[3])
    this.strIconOpenSrc = sImageDir + paramArray[3];
  else
    this.strIconOpenSrc = ""; 
  if(paramArray[4])
  {
    this.strClickAction = paramArray[4];
    this.strDblClickAction = paramArray[5];
  }
  else
  {
    this.strClickAction = "";
    this.strDblClickAction = "";
  }
}

function Node_HasChildren()
{
 if(this.ChildNode != null)
 return true;
 else
 return false;
}
function Node_IsEnd()
{
 if(this.NextNode == null)
 return true;
 else
 return false;
}
function Node_Draw(top)
{
  if(this.IsDrawn)
 return;
 
 if(top == null)
 top = this.TreeObj.cy; 
 
 var str_divHTML = "<DIV class=Node id=node" + this.ID + " name=node" + this.ID + ">";
 var str_nodeHTML = "";
 var str_NodeBoxLnkTxt = "<A HREF='JavaScript:void(0);' onClick='" + this.TreeObj.strMyName + ".NodeClick(" + this.ID + "); return false;'>"
 var str_NodeIconLnkTxt = "<A HREF='JavaScript:void(0);' onClick='" + this.TreeObj.strMyName + ".NodeLinkClick(" + this.ID + "); return false;' onDblClick='" + this.TreeObj.strMyName + ".NodeDblClick(" + this.ID + "); return false;'>"
 var str_linkHTML;
 var str_goHTML;
 
 
 if(this.Parent != null) 
 {
 
 
 if(this.IsEnd()) 
 {
 if(this.HasChildren()) 
 str_nodeHTML = str_NodeBoxLnkTxt + "<IMG src=" + strP_CornerSrc + strNodeSize + " name=nodeLine" + this.ID + "></A>";
 else
 str_nodeHTML = "<IMG src=" + strCornerSrc + strNodeSize + ">";
 } 
 else 
 {
 if(this.HasChildren()) 
 str_nodeHTML = str_NodeBoxLnkTxt + "<IMG src=" + strP_TeeSrc + strNodeSize + " name=nodeLine" + this.ID + "></A>";
 else
 str_nodeHTML = "<IMG src=" + strTeeSrc + strNodeSize + ">";
 }
 
 
 var parNode = this.Parent;
 while(parNode != this.TreeObj.RootNode)
 {
 if(parNode.IsEnd() == true)
 str_nodeHTML = "<IMG src=" + strSpaceSrc + strNodeSize + ">" + str_nodeHTML;
 else
 str_nodeHTML = "<IMG src=" + strLineSrc + strNodeSize + ">" + str_nodeHTML;
 
 parNode = parNode.Parent;
 }
 
 }
 
 
 if(this.strIconNormalSrc != "")
 str_nodeHTML += str_NodeIconLnkTxt + "<IMG src=" + this.strIconNormalSrc + strNodeSize + " name=nodeIcon" + this.ID + " ALT='" + this.strDesc + "'></A>"; 
 var str_linkHTML = "<A HREF='JavaScript:void(0);' ID=nodeLinkA" + this.ID + " name=nodeLinkA" + this.ID + " onClick='" + this.TreeObj.strMyName + ".NodeLinkClick(" + this.ID + ");return false;' onDblClick='" + this.TreeObj.strMyName + ".NodeLinkDblClick(" + this.ID + "); return false;'>";
 if(this.strDblClickAction != "") 
 {
 str_goHTML = "<A HREF='javascript:void(0);' onClick='" + 
 this.TreeObj.strMyName + ".NodeLinkDblClick(" + 
 this.ID + "); return false;' onmouseover=\"" +
 this.TreeObj.strMyName + ".SwitchGoBtn(" + 
 this.ID + ", 'On');\" onmouseout=\"" +
 this.TreeObj.strMyName + ".SwitchGoBtn(" + 
 this.ID + ", 'Off');\"><IMG SRC=" + 
 str_GoBtnSrc + strNodeSize + " NAME=goBtnImg" + 
 this.ID + " ID=goBtnImg" + this.ID + " ALT='Go to details'></A>"
 }
 else
 {
 str_goHTML = "";
 }
 var contElem;
 if(this.TreeObj.Height > 0 && this.TreeObj.Width > 0)
 contElem = eval("document.all." + this.TreeObj.strMyName);
 else
 contElem = document.body
 
 var str_spanHTML = "<SPAN class=NodeTxt" + this.StyleID + " ID=nodeLink" + this.ID + " name=nodeLink" + this.ID + ">" + str_linkHTML + "&nbsp;" + this.strDesc + "&nbsp;</A></SPAN>";
 
 if(!this.HasChildren())
 str_goHTML = "<SPAN CLASS='GoBtn' ID=goBtn" + this.ID + " NAME=goBtn" + this.ID + ">" + str_goHTML + "</SPAN>";
 
 contElem.insertAdjacentHTML("BeforeEnd", str_divHTML + "<NOBR>" + str_nodeHTML + str_spanHTML + str_goHTML + "</NOBR></DIV>");

 this.MoveTo(this.TreeObj.cx, top);
 this.IsDrawn = true;
}
function Node_Hide()
{
 var theNodeObj;
 theNodeObj = eval(this.TreeObj.strDocObj + ".node" + this.ID);
 theNodeObj.style.visibility = "hidden";
}
function Node_Show()
{
 var theNodeObj; 
 var str_NodeLink = this.IsSelected ? "nodeLinkSel" : "nodeLink";
 theNodeObj = eval(this.TreeObj.strDocObj + ".node" + this.ID);
 theNodeObj.style.visibility = "visible";
}
function Node_SwitchSelect()
{ 
  var theNodeObj; 
  var str_oldNodeLink = this.IsSelected ? "nodeLinkSel" : "nodeLink";
  var str_newNodeLink = this.IsSelected ? "nodeLink" : "nodeLinkSel";
  this.IsSelected = !this.IsSelected;
  if(this.IsSelected)
  {
    if(this.Parent)
    this.TreeObj.NodeExpand(this.Parent);
  }
 theNodeObj = eval(this.TreeObj.strDocObj + ".nodeLinkA" + this.ID);
 if(this.IsSelected == true)
 {
 theNodeObj.style.backgroundColor = imkGetLastStyleAttr(".NodeTxtSel" + this.StyleID, "backgroundColor");
 theNodeObj.style.color = imkGetLastStyleAttr(".NodeTxtSel" + this.StyleID, "color");
 
 if(!this.HasChildren())
 {
 var theGoBtnObj = eval(this.TreeObj.strDocObj + ".goBtn" + this.ID);
 theGoBtnObj.style.visibility = "visible";
 }
 }
 else
 {
 theNodeObj.style.backgroundColor = "";
 theNodeObj.style.color = imkGetLastStyleAttr(".NodeTxt" + this.StyleID, "color");
 
 if(!this.HasChildren())
 {
 var theGoBtnObj = eval(this.TreeObj.strDocObj + ".goBtn" + this.ID);
 theGoBtnObj.style.visibility = "hidden";
 }
 }
}
function Node_MoveTo(cx, cy)
{
 var theNodeObj;
 theNodeObj = eval(this.TreeObj.strDocObj + ".node" + this.ID + ".style");
 theNodeObj.pixelLeft = cx;
 theNodeObj.pixelTop = cy;
}
function Node_MoveBy(dx, dy)
{ 
 var theNodeObj;
 theNodeObj = eval(this.TreeObj.strDocObj + ".node" + this.ID + ".style");
 theNodeObj.pixelLeft += dx;
 theNodeObj.pixelTop += dy;
}
function Node_GetTop()
{
 var theNodeObj;
 theNodeObj = eval(this.TreeObj.strDocObj + ".node" + this.ID + ".style");
 return theNodeObj.pixelTop;
}
function Node_GoBtnOn()
{
 var theDocObj;
 theDocObj = eval("document");
 theDocObj.images["goBtnImg" + this.ID].src = str_GoBtnOnSrc;
}
function Node_GoBtnOff()
{
 var theDocObj;
 theDocObj = eval("document");
 theDocObj.images["goBtnImg" + this.ID].src = str_GoBtnSrc;
}

