﻿//****************** IPTreeNode **********************************

function IPTreeNode(id,text,popOnDemand,value,expanded, checked, label) {
  this.Text = text;
  this.ClientID = id;
  this.Parent = null;
  this.TreeView = null;
  this.Nodes=new Array();
  this.Index=0;
  this.Value=value;
  this.Category=null;
  this.NodeCss=null;
  this.Expanded= expanded;
  this.ExpandOnServer=0;
  this.TableRow=null;
  this.PopulateOnDemand=popOnDemand;
  this.Checked=checked;
  this.Label=label;
  this.PartiallyIncluded=false;
}

IPTreeNode.prototype.AddNode = function (node) {
  node.Parent = this;
  this.Nodes[this.Nodes.length] = node;
}

IPTreeNode.prototype.TR = function () {
  if (this.TableRow == null) {
    this.TableRow = document.getElementById(this.ClientID);
  }
  return this.TableRow;
}

IPTreeNode.prototype.ShowRow = function () {
  var tr = this.TR();
  if (tr) {
    tr.style.display = ""
  }
}

IPTreeNode.prototype.HideRow = function () {
  var tr = this.TR();
  if (tr) {
    tr.style.display = "none"
  }
}

IPTreeNode.prototype.HideChildren = function () {
  for (var i=0; i<this.Nodes.length; i++) {
    this.Nodes[i].HideRow();
    this.Nodes[i].HideChildren();
  }
}

IPTreeNode.prototype.ShowChildren = function () {
  if (this.Expanded) {
      for (var i=0; i<this.Nodes.length; i++) {
        this.Nodes[i].ShowRow();
        this.Nodes[i].ShowChildren();
      }
  }
}

IPTreeNode.prototype.ValuePath = function () {
 var delim = this.TreeView.PathSeparator;
 var ret = this.Value;
 var p = this.Parent;
 while (p != null && p != this.TreeView.RootNode) {
   ret = p.Value + delim + ret;
   p = p.Parent;
 }
 return ret;
}

IPTreeNode.prototype.HasChildren = function() {
  return (this.Nodes.length > 0);
}

IPTreeNode.prototype.Expand = function () {
  this.Expanded = true;
  //IPTreeView_Populate
  if (this.PopulateOnDemand && ! this.HasChildren()) {
    IPTreeView_PopulateNode(this);
  } else {
    this.ShowChildren();
  }
  
  var tr = this.TR();
  tr.className = this.TreeView.NodeExpandedCssClass;
  
  var img = this.GetNodeImage();
  if (img) {
    img.src = this.TreeView.CollapseImage;
    //img.alt = this.TreeView.CollapseImageAlternateText;
    img.alt = this.TreeView.CollapseImageAlternateText + " " + this.Text;
  }
  var span = this.GetNodeHiddenText();
  if (span) {
    span.innerHTML = this.TreeView.CollapseImageAlternateText + " " + this.Text;
  }
}


IPTreeNode.prototype.ShowAllChildren = function () {
  if (this.Expanded) {
      for (var i=0; i<this.Nodes.length; i++) {
        this.Nodes[i].ShowRow();
            this.Nodes[i].ExpandAll();
      }
  }
}

IPTreeNode.prototype.ExpandAll = function () {

  if (this.HasChildren())
  {
    this.Expanded = true;
    this.ShowAllChildren();
    var tr = this.TR();
    tr.className = this.TreeView.NodeExpandedCssClass;
    var img = this.GetNodeImage();
    if (img) {
      img.src = this.TreeView.CollapseImage;
      //img.alt = this.TreeView.CollapseImageAlternateText;
      img.alt = this.TreeView.CollapseImageAlternateText + " " + this.Text;
    }
    var span = this.GetNodeHiddenText();
    if (span) {
      span.innerHTML = this.TreeView.CollapseImageAlternateText + " " + this.Text;
    }
  }
}


IPTreeNode.prototype.SetCheck = function (value) {
    if (value)
        this.Check();
    else
        this.UnCheck();
}

IPTreeNode.prototype.ToggleCheck = function () {
    if (this.Checked)
        this.UnCheck();
    else
        this.Check();
}

IPTreeNode.prototype.Check = function () {

    this.Checked=true;
    var img = this.GetCheckImage();
    if (img) {
        if (this.PartiallyIncluded)
            img.src = this.TreeView.PartialCheckImage;
        else
            img.src = this.TreeView.CheckImage;
    }
}

IPTreeNode.prototype.UnCheck = function () {
    this.Checked=false;
    var img = this.GetCheckImage();
    if (img) {
        img.src = this.TreeView.UnCheckImage;
    }
}

IPTreeNode.prototype.SetPartial = function (value) {

    this.PartiallyIncluded=value;
    if (this.Checked)
    {
        var img = this.GetCheckImage();
        if (img) {
            if (this.PartiallyIncluded)
                img.src = this.TreeView.PartialCheckImage;
            else
                img.src = this.TreeView.CheckImage;
        }
    }
}

IPTreeNode.prototype.GetCheckImage = function () {
  var  tr = this.TR();
  elemList = tr.getElementsByTagName("IMG");
  for (var i = 0; i<elemList.length; i++) {
    if (elemList[i].id == (this.ClientID + "chkbox")) {
      return elemList[i];
    }
  }
  return null;
}


IPTreeNode.prototype.GetNodeImage = function () {
  var  tr = this.TR();
  elemList = tr.getElementsByTagName("IMG");
  for (var i = 0; i<elemList.length; i++) {
    if (elemList[i].id == (this.ClientID + "img")) {
      return elemList[i];
    }
  }
  return null;
}

IPTreeNode.prototype.GetNodeHiddenText = function () {
  var  tr = this.TR();
  elemList = tr.getElementsByTagName("SPAN");
  for (var i = 0; i<elemList.length; i++) {
    if (elemList[i].className == "readableButHidden") {
      return elemList[i];
    }
  }
  return null;
}

IPTreeNode.prototype.Collapse = function () {
  this.Expanded = false;
  this.HideChildren();

  var tr = this.TR();
  tr.className = this.TreeView.NodeCssClass;
  
  var img = this.GetNodeImage();
  if (img) {
    img.src = this.TreeView.ExpandImage;
    //img.alt = this.TreeView.ExpandImageAlternateText;
    img.alt = this.TreeView.ExpandImageAlternateText + " " + this.Text;
  }
  var span = this.GetNodeHiddenText();
  if (span) {
    span.innerHTML = this.TreeView.ExpandImageAlternateText + " " + this.Text;
  }
}

IPTreeNode.prototype.Level = function () {
  if (this.Parent == this.TreeView.RootNode || this.Parent == null) {
    return 0;
  } else {
     return this.Parent.Level() + 1;
  }
}

IPTreeNode.prototype.Toggle = function () {
  
  if (this.Expanded) {
    this.Collapse();
  } else {
    this.Expand();
  }
  
}



//********************End IPTreeNode***************************

//********************IPTreeView*******************************

function IPTreeView(uniqueID,clientID,useCallback) {
  this.ClientID = clientID;
  this.UniqueID = uniqueID;
  this.RootNode = new IPTreeNode();
  this.RootNode.ClientID = clientID;
  this.UseCallback = useCallback
  this.AllNodes = new Array();
  this.PathSeparator = "/";
}

IPTreeView.prototype.AddNode = function (parentNodeID,node) {
  
  var pNode;
  
  if (parentNodeID == "") {
    pNode = this.RootNode;
  } else {
    pNode = this.AllNodes[parentNodeID];
  }
  
  if (pNode != null) {
    node.TreeView = this;
    pNode.AddNode(node);
    this.AllNodes[node.ClientID] = node;
  }
}

IPTreeView.prototype.FindNode = function (nodeID) {
  return this.AllNodes[nodeID];
}

IPTreeView.prototype.ToggleNode = function(nodeID) {
  var pNode = this.FindNode(nodeID);
  if (pNode) {
    pNode.Toggle();
  }
}

IPTreeView.prototype.NodeIconClicked = function(nodeID) {
  var pNode = this.FindNode(nodeID);
  if (pNode) {
     if (IPTreeView_NodeIconClicked(pNode)) {
       pNode.Toggle();
     }
  }
}

IPTreeView.prototype.CheckboxClicked = function(nodeID) {
  var pNode = this.FindNode(nodeID);
  if (pNode) {
     IPTreeView_CheckboxClicked(pNode);
  }
}


IPTreeView.prototype.NodeTextClicked = function(nodeID) {
  var pNode = this.FindNode(nodeID);
  if (pNode) {
     if (IPTreeView_NodeTextClicked(pNode)) {
       pNode.Toggle();
     }
  }
}

IPTreeView.prototype.CollapseAll = function () {
  for (var i=0; i<this.RootNode.Nodes.length; i++) {
    this.RootNode.Nodes[i].Collapse();
  }
}


//Default callback
function IPTreeView_NodeTextClicked(pNode) {
  return true;
}

function IPTreeView_NodeIconClicked(pNode) {
  return true;
}

function IPTreeView_NodePopulated(pNode) {
}

function IPTreeView_CheckboxClicked(pNode) {
}

//*******************End IPTreeView****************************


function IPTreeView_PopulateNode(node) {

    //Set up context object.  This will be available when callback returns.
    var context = new Object();
    context.Node = node;
    context.ClientID = node.TreeView.ClientID;
    context.UniqueID = node.TreeView.UniqueID;
    context.TR = node.TR();
/*    
    var tr = WebForm_GetParentByTagName(node, "TR");
    if (tr) {
        var checkbox = tr.getElementsByTagName("INPUT");
        if (checkbox && (checkbox.length > 0)) {
            for (var i = 0; i < checkbox.length; i++) {
                if (checkbox[i].type.toLowerCase() == "checkbox") {
                    if (checkbox[i].checked) {
                        context.isChecked = "t";
                    }
                    break;
                }
            }
        }
    }
*/    
    //param is what gets sent to server.
   
    var valuePath = node.ValuePath();
    var text = node.Text;
     
    var param = node.ClientID + "|" + valuePath.length + "|" + valuePath + text.length + "|" + text;
    IPTreeView_PopulateNodeDoCallBack(context, param);
}

//This routine gets called when callback request complete
function IPTreeView_ProcessNodeData(result, context) {

  if (result.length > 0) {
  
    var ci =  result.indexOf("|", 0);
    var jscriptSize = parseInt(result.substring(0,ci));
    var jscript = result.substr(ci+1,jscriptSize);
    ci = ci + jscriptSize + 1;
    var ci2 = result.indexOf("|",ci);
    var htmlSize = parseInt(result.substring(ci,ci2));
    var html = result.substr(ci2+1,htmlSize);
    
    var treeNode = context.Node;
    var row = context.TR;
    var tempDiv = document.createElement("div");
    
    treeNode.PopulateOnDemand = false;
    
    tempDiv.innerHTML = html;
    
    var tempTable = tempDiv.firstChild;
    var newTbody = document.createElement("tbody");
    if (tempTable && tempTable.tagName == "TABLE") {
 /*
      for (var i = 0; i<tempTable.rows.length; i++) {
        newTbody.appendChild(tempTable.rows[i]);
      }
      insertAfter(row.parentNode.parentNode, newTbody, row.parentNode);
      
//      newTbody.tabIndex = -1;
//      newTbody.focus();
*/      
      var rowList = new Array(tempTable.rows.length);

      for (var i = 0; i<rowList.length; i++) {
        rowList[i] = tempTable.rows[i];
      }
    
        
      for (var i = rowList.length-1; i >= 0; i--) {
        if (row.nextSibling) {
          row.parentNode.insertBefore(rowList[i],row.nextSibling);
        } else {
          row.parentNode.appendChild(rowList[i]);      
        }
      }

      eval(jscript);
     
    } else {
      alert("Information returned by server is invalid");
    }
    
    IPTreeView_NodePopulated(treeNode);
    
  }
}    


function insertAfter(parent, node, referenceNode) {
  parent.insertBefore(node, referenceNode.nextSibling);
}
