//=================================================================
// JavaScript Tree
// Version 1.0
// by Nicholas C. Zakas, nicholas@nczonline.net
// Copyright (c) 2002 Nicholas C. Zakas.  All Rights Reserved.
//-----------------------------------------------------------------
// Browsers Supported:
//  * Netscape 6.1+
//  * Internet Explorer 5.0+
//=================================================================
// History
//-----------------------------------------------------------------
// January 27, 2002 (Version 1.0)
//  - Works in Netscape 6.1+ and IE 5.0+  
//=================================================================
// Software License
// Copyright (c) 2002 Nicholas C. Zakas.  All Rights Reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// 1. Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer. 
//
// 2. Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimer in
//    the documentation and/or other materials provided with the
//    distribution.
//
// 3. The end-user documentation included with the redistribution,
//    if any, must include the following acknowledgment:  
//       "This product includes software developed by the
//        Nicholas C. Zakas (http://www.nczonline.net/)."
//    Alternately, this acknowledgment may appear in the software itself,
//    if and wherever such third-party acknowledgments normally appear.
//
// 4. Redistributions of any form are free for use in non-commercial
//    ventures. If intent is to use in a commercial product, contact
//    Nicholas C. Zakas at nicholas@nczonline.net for purchasing of
//    a commercial license.
//
// THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED.  IN NO EVENT SHALL NICHOLAS C. ZAKAS  BE LIABLE FOR ANY 
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 
// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 
// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
// OF THE POSSIBILITY OF SUCH DAMAGE.
//-----------------------------------------------------------------
// Any questions, comments, or suggestions should be e-mailed to 
// nicholas@nczonline.net.  For more information, please visit
// http://www.nczonline.net/. 
//=================================================================

var DIR_IMAGES = "";
var IMG_PLUS = DIR_IMAGES + "arrow_r.gif";
var IMG_MINUS = DIR_IMAGES + "arrow_d.gif";
var kDefaltTarget = null;

var imgPlus = new Image();
imgPlus.src = IMG_PLUS;
var imgMinus = new Image();
imgMinus.src = IMG_MINUS;

//var objLocalTree = null;

var INDENT_WIDTH = 10;

//-----------------------------------------------------------------
// Class jsTree
//-----------------------------------------------------------------
// Author(s)
//  Nicholas C. Zakas (NCZ), 1/27/02
//
// Description
//  The jsTreeNode class encapsulates the functionality of a tree.
//
// Parameters
//  (none)
//-----------------------------------------------------------------
function jsTree(index)
{
  //Public Properties (NCZ, 1/27/02)
  this.root = null;           //the root node of the tree
  this.index = index;

   //Public Collections (NCZ, 1/27/02)
  this.nodes = new Array;     //array for all nodes in the tree
  this.selectedObject = null;   // The currently selected node
}

//-----------------------------------------------------------------
// Method jsTree.createRoot()
//-----------------------------------------------------------------
// Author(s)
//  Nicholas C. Zakas (NCZ), 1/27/02
//
// Description
//  This method creates the root of the tree.
//
// Parameters
//  strIcon (string) - the icon to display for the root.
//  label (string) - the text to display for the root.
//  strURL (string) - the URL to navigate to when the root is clicked.
//  strTarget (string) - the target for the URL (optional).
//
// Returns
//  The jsTreeNode that was created.
//-----------------------------------------------------------------
jsTree.prototype.createRoot = function(strIcon, label, strURL, strTarget)
{
  //create a new node (NCZ, 1/27/02)
  this.root = new jsTreeNode(strIcon, label, strURL, strTarget, this, null);
  
  //assign an ID for internal tracking (NCZ, 1/27/02)
  this.root.id = "root";
  
  //add it into the array of all nodes (NCZ, 1/27/02)
  this.nodes[this.root.id] = this.root;
  
  //make sure that the root is expanded (NCZ, 1/27/02)
  this.root.expanded = true;
  
  //return the created node (NCZ, 1/27/02)
  return this.root;
}

//-----------------------------------------------------------------
// Method jsTree.buildDOM()
//-----------------------------------------------------------------
// Author(s)
//  Nicholas C. Zakas (NCZ), 1/27/02
//
// Description
//  This method creates the HTML for the tree.
//
// Parameters
//  (none)
//
// Returns
//  (nothing)
//-----------------------------------------------------------------
jsTree.prototype.buildDOM = function(id, objName)
{
  //call method to add root to document, which will recursively
  //add all other nodes (NCZ, 1/27/02)
  //All the user to pass in where to put the menu.
  if (id == null)
  {
    this.root.addToDOM(document.body, objName);
  }
  else
  {
    this.root.addToDOM(document.getElementById (id), objName);
  }
}

//-----------------------------------------------------------------
// Method jsTree.OnMouseOut()
//-----------------------------------------------------------------
// Author(s)
//  jsmith
//
// Description
//  On mouse out change the style
//
// Parameters
//  strNodeID (string) - the ID of the node that is being expanded/collapsed.
//
// Returns
//  (nothing)
//-----------------------------------------------------------------
jsTree.prototype.OnMouseOut = function(strNodeID)
{
  //get the node (NCZ, 1/27/02)
  var objNode = this.nodes[strNodeID];
  var selectedObject = objNode.root.selectedObject;
  
  // If this is not the currently selected item change the class back
  if (selectedObject != objNode)
  { 
    document.getElementById (objNode.id).className = "treeIndent"+objNode.indent;
  }
}

//-----------------------------------------------------------------
// Method jsTree.OnMouseOver()
//-----------------------------------------------------------------
// Author(s)
//  jsmith
//
// Description
//  On mouse over change the style
//
// Parameters
//  strNodeID (string) - the ID of the node that is being expanded/collapsed.
//
// Returns
//  (nothing)
//-----------------------------------------------------------------
jsTree.prototype.OnMouseOver = function(strNodeID)
{
  //get the node (NCZ, 1/27/02)
  var objNode = this.nodes[strNodeID];
  var selectedObject = objNode.root.selectedObject;
  
  // If this is not the currently selected item change the class
  if (selectedObject != objNode)
  { 
    document.getElementById (objNode.id).className = "treeIndent"+objNode.indent+"Selected";
  }
}

//-----------------------------------------------------------------
// Method jsTree.selectNode()
//-----------------------------------------------------------------
// Author(s)
//  jsmith
//
// Description
//  Saves a pointer to the current node and sets the content
//
// Parameters
//  strNodeID (string) - the ID of the node that is being expanded/collapsed.
//
// Returns
//  (nothing)
//-----------------------------------------------------------------
jsTree.prototype.selectNode = function(strNodeID)
{
  //get the node (NCZ, 1/27/02)
  var objNode = this.nodes[strNodeID];
  var selectedObject = objNode.root.selectedObject;
  
  // Reset the class of the previously selected node
  if (selectedObject != null)
  { 
    var oldRow = document.getElementById (selectedObject.id);
    // If we changed the main menu then the old row will be gone
    if (oldRow != null) oldRow.className = ("treeIndent" + selectedObject.indent);
  }
  // Set class of the currently selected node
  document.getElementById (objNode.id).className = "treeIndent"+objNode.indent+"Selected";
  objNode.root.selectedObject = objNode; 
  SetContent (objNode.url);
}

//-----------------------------------------------------------------
// Method jsTree.toggleExpand()
//-----------------------------------------------------------------
// Author(s)
//  Nicholas C. Zakas (NCZ), 1/27/02
//
// Description
//  This toggles the expansion of a node identified by an ID.
//
// Parameters
//  strNodeID (string) - the ID of the node that is being expanded/collapsed.
//
// Returns
//  (nothing)
//-----------------------------------------------------------------
jsTree.prototype.toggleExpand = function(strNodeID)
{
  //get the node (NCZ, 1/27/02)
  var objNode = this.nodes[strNodeID];
  
  //determine whether to expand or collapse
  if (objNode.expanded)
      objNode.collapse();
  else
      objNode.expand();
}

//-----------------------------------------------------------------
// Class jsTreeNode
//-----------------------------------------------------------------
// Author(s)
//  Nicholas C. Zakas (NCZ), 1/27/02
//
// Description
//  The jsTreeNode class encapsulates the basic information for a node
//  in the tree.
//
// Parameters
//  strIcon (string) - the icon to display for this node.
//  label (string) - the text to display for this node.
//  strURL (string) - the URL to navigate to when this node is clicked.
//  strTarget (string) - the target for the URL (optional).
//-----------------------------------------------------------------
function jsTreeNode(strIcon, label, strURL, strTarget, root, condition, parent)
{
  //Public Properties (NCZ, 1/27/02)
  this.icon = strIcon;            //the icon to display
  this.label = label;            //the text to display
  this.url = strURL;              //the URL to link to
  //the target for the URL
  this.target = (strTarget && strTarget != "") ? strTarget : kDefaltTarget;
  
  //Private Properties (NCZ, 1/27/02)
  this.indent = -1;                //the indent for the node
  
  //Public States (NCZ, 1/27/02)
  this.expanded = false;          //is this node expanded?

  //Public Collections (NCZ, 1/27/02)   
  this.childNodes = new Array;    //the collection of child nodes
  this.condition = condition;
  this.root=root;
  this.parent=parent;
}

//-----------------------------------------------------------------
// Method jsTreeNode.addChild()
//-----------------------------------------------------------------
// Author(s)
//  Nicholas C. Zakas (NCZ), 1/27/02
//
// Description
//  This method adds a child node to the current node.
//
// Parameters
//  strIcon (string) - the icon to display for this node.
//  label (string) - the text to display for this node.
//  strURL (string) - the URL to navigate to when this node is clicked.
//  strTarget (string) - the target for the URL (optional).
//
// Returns
//  The jsTreeNode that was created.
//-----------------------------------------------------------------
jsTreeNode.prototype.addChild = function (strIcon, label, strURL, strTarget, condition)
{
    //create a new node (NCZ, 1/27/02)
  var objNode = new jsTreeNode(strIcon, label, strURL, strTarget, this.root, condition, this);
  
  //assign an ID for internal tracking (NCZ, 1/27/02)
  objNode.id = this.id + "_" + this.childNodes.length;
  
  //assign the indent for this node
  objNode.indent = this.indent + 1;
  
  //add into the array of child nodes (NCZ, 1/27/02)
  this.childNodes[this.childNodes.length] = objNode;
  
  //add it into the array of all nodes (NCZ, 1/27/02)
  this.root.nodes[objNode.id] = objNode;
  
  //return the created node (NCZ, 1/27/02)
  return objNode;
}

//-----------------------------------------------------------------
// Method jsTreeNode.addToDOM()
//-----------------------------------------------------------------
// Author(s)
//  Nicholas C. Zakas (NCZ), 1/27/02
//
// Description
//  This method adds DOM elements to a parent DOM element.
//
// Parameters
//  objDOMParent (HTMLElement) - the parent DOM element to add to.
//
// Returns
//  (nothing)
//-----------------------------------------------------------------
jsTreeNode.prototype.addToDOM = function (objDOMParent, objName)
{
  // Dont show the root
  if (this.condition == null || this.condition == "" || eval(this.condition))
  {
    if (this.indent >= 0)
    {
      var strHTMLLink;

      //create the layer for the node (NCZ, 1/27/02)
      var objNodeDiv = document.createElement("div");
    
      //add it to the DOM parent element (NCZ, 1/27/02)
      objDOMParent.appendChild(objNodeDiv);
    
      //create string buffer (NCZ, 1/27/02)
      var d = new jsDocument;
    
      //begin the table (NCZ, 1/27/02)
      d.writeln("<table class=\"sideNavigation\" cellpadding=\"0\" cellspacing=\"0\">");
      if (this.IsSeperator ())
      {
        d.writeln('<tr><td class="treeSeperator"></td></tr>');
      }
      else
      {
        var style = "treeIndent" + this.indent;
        strHTMLLink = (this.childNodes.length > 0) ? (objName+".toggleExpand('"+this.id+"');") :
          (this.url.match (/^MM_/)) ? this.url :
            (objName+".selectNode('" + this.id + "');");
        
        d.writeln("<tr id=\""+this.id+"\" class=\""+style+"\" onmouseout=\""+objName+
          ".OnMouseOut('"+this.id+"\')\" onmouseover=\""+objName+".OnMouseOver('"+
          this.id+"\')\" onClick=\""+strHTMLLink+"\">");
    
      //no indent needed for level under root (NCZ, 1/27/02)
        var indentIndex;
        for (indentIndex = 0; indentIndex < this.indent; indentIndex++)
        {
          d.writeln('<td class="spacer">');
            d.write('<img src="TransparentBox.png" border="0" hspace="1" />');
          d.writeln('</td>');
        }
        d.write('<td class="plusMinus">');
        //if there are children, then add a plus/minus image (NCZ, 1/27/02)
          if (this.childNodes.length > 0)
          {
  //          strHTMLLink = "<a class=\""+style+"\" href=\"javascript:"+objName+".toggleExpand('"+this.id+"');\">";
  //          d.write(strHTMLLink);
            d.write("<img src=\"");
            d.write(this.expanded ? imgMinus.src : imgPlus.src);
            d.write("\" border=\"0\" hspace=\"1\" id=\"");
            d.write("imgPM_" + this.id + "\" />");
  //          d.write("</a>");
          }
          else
          {
            d.write('<img src="TransparentBox.png" border="0" hspace="1" />');
          }
          /*
          else
          {
            //create the URL (NCZ, 1/27/02)
            var strHREF = (this.url.match (/^javascript:/)) ? this.url + ";"
              : "javascript:"+objName+".selectNode('" + this.id + "');";
            strHTMLLink = "<a class=\""+style+"\" href=\""+strHREF+"\"";
            if (this.target)strHTMLLink += " target=\"" + this.target;
            strHTMLLink += "\">"
          }
          */
        d.write("</td>");
    
      //finish by drawing the icon and text (NCZ, 1/27/02)
  //    d.write("<td width=\"22\">" +strHTMLLink);
//      d.write("<td>" +strHTMLLink);
      //JJS 6/13/05
      if (this.icon != "")
      {
        d.write("<td>");
          d.write("<img hspace=\"1\" src=\"" + this.icon + "\" border=\"0\" align=\"absmiddle\" />");
        d.write("</td>");
      }
//      d.write("</a></td>");
//      d.write("<td nowrap>" + strHTMLLink + langString(this.label) + "</a></td>");
      else
      {
        d.write('<td nowrap>' + langString(this.label) + '</td>');
      }
      d.writeln("</tr>");
      d.writeln("<tr class='rowSpacing'></tr>");
      d.writeln("</table>");
      }
        
      //assign the HTML to the layer (NCZ, 1/27/02)
      objNodeDiv.innerHTML = d;
    }
    
    //create the layer for the children (NCZ, 1/27/02)
    var objChildNodesLayer = document.createElement("div");
    objChildNodesLayer.setAttribute("id", "divChildren_" + this.id);
    objChildNodesLayer.style.position = "relative";
    objChildNodesLayer.style.display = (this.expanded ? "block" : "none");
    if (this.indent >= 0)
    {
       objNodeDiv.appendChild(objChildNodesLayer);
    }
    else
    {
       objDOMParent.appendChild(objChildNodesLayer);
    }
    
    //call for all children (NCZ, 1/27/02)
    for (var i=0; i < this.childNodes.length; i++)
        this.childNodes[i].addToDOM(objChildNodesLayer, objName);
  }
}

//-----------------------------------------------------------------
// Method jsTreeNode.IsSeperator()
//-----------------------------------------------------------------
// Author(s)
//  Nicholas C. Zakas (NCZ), 1/27/02
//
// Description
//  Return true if this system is just a Seperator (it has no children
// and no action/url.
//
// Parameters
//  (none)
//
// Returns
//  true or false.
//-----------------------------------------------------------------
jsTreeNode.prototype.IsSeperator = function ()
{
  return (this.childNodes.length == 0 && (this.url == null || this.url == ""))
}

//-----------------------------------------------------------------
// Method jsTreeNode.collapse()
//-----------------------------------------------------------------
// Author(s)
//  Nicholas C. Zakas (NCZ), 1/27/02
//
// Description
//  This method expands the jsTreeNode's children to be hidden.
//
// Parameters
//  (none)
//
// Returns
//  (nothing)
//-----------------------------------------------------------------
jsTreeNode.prototype.collapse = function ()
{
  //check to see if the node is already collapsed (NCZ, 1/27/02)
  if (!this.expanded)
  {
    //throw an error (NCZ, 1/27/02)
    throw "Node is already collapsed"
  }
  else
  {
    //change the state of the node (NCZ, 1/27/02)
    this.expanded = false;
    
    //change the plus/minus image to be plus (NCZ, 1/27/02)
    document.images["imgPM_" + this.id].src = imgPlus.src;
    
    //hide the child nodes (NCZ, 1/27/02)
    document.getElementById("divChildren_" + this.id).style.display = "none";
  }
}


//-----------------------------------------------------------------
// Method jsTreeNode.expand()
//-----------------------------------------------------------------
// Author(s)
//  Nicholas C. Zakas (NCZ), 1/27/02
//
// Description
//  This method expands the jsTreeNode's children to be displayed.
//
// Parameters
//  (none)
//
// Returns
//  (nothing)
//-----------------------------------------------------------------
jsTreeNode.prototype.expand = function ()
{
  //check to see if the node is already expanded (NCZ, 1/27/02)
  if (this.expanded)
  {
    //throw an error (NCZ, 1/27/02)
    throw "Node is already expanded"
  }
  else
  {
    //change the state of the node (NCZ, 1/27/02)
    this.expanded = true;
    
    //change the plus/minus image to be minus (NCZ, 1/27/02)
    document.images["imgPM_" + this.id].src = imgMinus.src;
    
    //show the child nodes (NCZ, 1/27/02)
    document.getElementById("divChildren_" + this.id).style.display = "block";
  }
}

