function Toggle(objTree, strNodeId) { // If there are any menus or children open, close them first. closeAll(); var objNode = objTree[objTree.getIndexById(strNodeId)]; var nodeChild = document.all[objTree.getId()+'_'+objNode.id+'_img']; if (objNode.expanded) { hideChildren(objTree, objNode) nodeChild.src='images/treeview/folder_closed.gif'; } else { showChildren(objTree, objNode) nodeChild.src='images/treeview/folder_open.gif'; }; objNode.expanded = !objNode.expanded objTree.replaceByIndex(objTree.getIndexById(strNodeId), objNode); }; function showChildren(objTree, objNode) { for (var i = 0; i < objTree.size(); i++) { if (objTree[i].parent == objNode.id) { var nodeChild = document.all[objTree.getId()+'_'+objTree[i].id]; nodeChild.style.display = 'block'; if (objTree[i].expanded) { showChildren(objTree, objTree[i]); } }; }; }; function hideChildren(objTree, objNode) { for (var i = 0; i < objTree.size(); i++) { if (objTree[i].parent == objNode.id) { var nodeChild = document.all[objTree.getId()+'_'+objTree[i].id]; nodeChild.style.display = 'none'; hideChildren(objTree, objTree[i]); }; }; }; function Tree(newId) { // Number of nodes in the tree: var lngNodeCount=0; var id; this.getId = function() { return id; }; this.render = function(){ renderChildren(this, -1, 0, id, true); }; this.numChildren = function(lngParent) { return countChildren(this, lngParent); }; function countChildren(objTree, lngParent) { var lngChildren=0; for (var i = 0; i < lngNodeCount; i++) { if (objTree[i].parent == lngParent) { lngChildren++; } } return lngChildren; }; function renderChildren(objTree, lngParent, lngIndent, strID, bolExpanded) { //alert('Rendering children of: '+lngParent); for (var i = 0; i < lngNodeCount; i++) { if (objTree[i].parent == lngParent) { renderNode(objTree, objTree[i], lngIndent, bolExpanded) renderChildren(objTree, objTree[i].id, lngIndent+1, strID+'_'+objTree[i].id, objTree[i].expanded); }; }; }; function repeat(strToRepeat, lngNumTimes) { var n=''; for (var i = 0; i < lngNumTimes; i++) { n+=strToRepeat; }; return n; }; // Tree initialization function. this.init = function(thisId){ //alert('Initializing as '+thisId+'.'); lngNodeCount=0; id = thisId; }; // Add a node to the tree this.add = function(objNode) { //alert('Adding node: '+objNode.id) lngNodeCount++; this[lngNodeCount - 1] = objNode; }; // Get a node's index, given it's id this.getIndexById = function(id) { for (var i = 0; i < lngNodeCount; i++) if (this[i].id == id) return i; }; // Replace a node in the tree with another one. this.replaceByIndex = function(objNode, lngIndex) { this[lngIndex] = objNode; }; // Remove a node from the tree this.remove = function(lngIndex) { alert('Removing node: '+lngIndex) this[lngIndex] = null; for (var i = lngIndex; i <= lngNodeCount; i++) this[i] = this[i + 1]; lngNodeCount--; }; // Clears out the entire tree this.clear = function() { for (var i = 0; i < lngNodeCount; i++) this[i] = null; lngNodeCount = 0; }; // Get the size of the tree (number of nodes) this.size = function() { return lngNodeCount; }; // Determine if the tree is empty this.isEmpty = function() { return lngNodeCount==0; }; // Call the initialization routing automatically. this.init(newId); };