/*
	Copyright Information:
	TreeControl Script From PardesiService's Softomarix division. For any
	bug reporting or suggestion please send an email to
	info@pardesiservices.com.
	
	NOTE:
	This control has only been tested on Internet Explorer5.5 and higher.
	
	Disclaimer:
	PardesiServices does not hold any responsibilites direct or implied if 
	running of this script causes any trouble on your system.
*/
// Java script code for Tree Contol.
var curParentNodeId = 1;
var curSubNodeId = 1;
var curSubparentId = 1;
var arrTreeNodes = new Array();

var parentLeftPos = 5;
var parentTopPos = 5;

var bParentDivOpen = false;
var bSubParentDivOpen = false;

// TreeNode object that stores all the attributes of the node.
function TreeNode(leafParent, leafDisplayName, leafHref, leafId, bHasSubNode)
{
	this.leafParent = leafParent;
	this.leafDisplayName = leafDisplayName;
	this.leafHref = leafHref;
	this.leafId = leafId;
	this.bHasSubNode = bHasSubNode;
}

// Call this function to add a new Treenode to the array of nodes.
function addTreeNode(tNode)
{
	var tempID;
	// Check if this node has parentID..
	if (tNode.leafParent == null)
	{
		tempID = "PS_parent_";
		tempID = tempID + curParentNodeId;
		// Bump the number of parent node ID.
		curParentNodeId++;
		
		// Reset the counter for sub node IDs.
		curSubNodeId = 1;
		curSubparentId = 1;
	}
	else if (tNode.leafParent != null && tNode.bHasSubNode)
	{
		tempID = "PS_subparent_";
		if (curParentNodeId > 1)
		{
			tempID = tempID + (curParentNodeId - 1);
		}
		tempID = tempID + curSubparentId;
		curSubparentId++;
		curSubNodeId = 1;
	}
	else
	{
		tempID = "PS_leaf_";
		if (curParentNodeId > 1)
		{
			tempID = tempID + (curParentNodeId - 1);
		}
		
		if (curSubparentId > 1)
		{
			tempID = tempID + (curSubparentId - 1);
		}
		tempID = tempID + curSubNodeId;
		curSubNodeId++;
	}
	
	tNode.leafId = tempID;
	
	// Add this node to array.
	arrTreeNodes[arrTreeNodes.length] = tNode;
	
	return tempID;
}

// Call this function after you have added all the tree nodes to the
// array. This function walks through the array and displays the nodes
// according to their attributes.
function displayTree()
{
	var strStyle;
	var arrLength = arrTreeNodes.length;
	var curNode = null;
	// Top level DIV tag for tree control.
	window.document.writeln('<div id="PSTreeCntrl" style="position:relative;background-color:#f0f8f8;width=100%;">');
	//Only create tree if there are any entris in the array.
	if (arrTreeNodes.length > 0)
	{
		for (var i = 0; i < arrLength; i++)
		{
			strStyle = "";
			curNode = arrTreeNodes[i];
			if (curNode.leafParent == null)
			{
				// Check if any sub parent DIV tag is open. If it is, then
				// close that first before we close any parent tags.
				if (bSubParentDivOpen)
				{
					window.document.writeln('</div>');
					bSubParentDivOpen = false;
				}
				if (bParentDivOpen)
				{
					window.document.writeln('</div>');
					bParentDivOpen = false;
				}
				
				// If the parent node does not have any sub nodes, then add it as a leaf..
				writeParentNode(curNode);	
			}
			else if (curNode.leafParent != null && curNode.bHasSubNode)
			{
				// If there is a previous subparent node div open,
				// close it now.
				if (bSubParentDivOpen)
				{
					window.document.writeln('</div>');
					bSubParentDivOpen = false;
				}
				writeSubParentNode(curNode);
			}
			else if (curNode.leafParent != null && !curNode.bHasSubNode)
			{
				writeLeafNode(curNode);
			}
		}
		window.document.writeln("</div>");
	}
	
	window.document.writeln("</div>");
}

// function writes the Parent Nodes.
function writeParentNode(curNode)
{
	bParentDivOpen = true;
	strStyle = '<div class="treeParentNode" id="';
	strStyle += curNode.leafId;
	strStyle += '">';
	
	window.document.writeln(strStyle);
	// If the parent node does not have any children, then show it as
	// leaf in the main tree.
	if (curNode.bHasSubNode)
	{
		window.document.writeln('<span class="nodeCollapsed" onclick="onClickNode(this);"><img src="plus_blue.gif"  align="middle"></span>');
	}
	else
	{
		window.document.writeln('<span class="nodeCollapsed"><img src="dot_blue.gif"  align="middle"></span>');
	}
		
	window.document.writeln(curNode.leafDisplayName);
}

// Function writes sub-parent Nodes.
function writeSubParentNode(curNode)
{
	bSubParentDivOpen = true;

	strStyle = '<div class="treeSubParentNode">';
	
	window.document.writeln(strStyle);
	window.document.writeln('<span class="nodeCollapsed" onclick="onClickNode(this);"><img src="plus_blue.gif" align="middle"></span>');
	window.document.writeln(curNode.leafDisplayName);
}

// Function writes leaf nodes that children of parent or
// subparent nodes.
function writeLeafNode(curNode)
{
	var strATag = '<a href="';
	strATag += curNode.leafHref;
	strATag += '">';

	strStyle = '<div class="treeLeafNode">';

	window.document.writeln(strStyle);
	window.document.writeln('<span class="nodeCollapsed"><img src="dot_blue.gif" align="middle"></span>');
	
	window.document.writeln(strATag);
	window.document.writeln(curNode.leafDisplayName);
	window.document.writeln('</a>');
	window.document.writeln('</div>');
}

// This event gets raised when user clicks on any of the nodes that
// has leaf nodes.
function onClickNode(ob)
{
	// Check the clas name of the node clicked.
	// If it is collapsed, then we need to expand it.
	// Otherwise we need to collapse it.
	if (null == ob)
	{
		return;
	}
	
	if (ob.className == 'nodeCollapsed')
	{
		expandNode(ob);
	}
	else if (ob.className == 'nodeExpanded')
	{
		collapseNode(ob);
	}
	
}

// Function collapsed the node that has been clicked.
function collapseNode(ob)
{
	if (null == ob)
	{
		return;
	}
	
	ob.className = "nodeCollapsed";
	toggleNodeImage(ob, true);
	
	// Get all the children and set their display status to show..
	var obChildren = null;
	if (ob.parentElement != null && ob.parentElement.children != null)
	{
		obChildren = ob.parentElement.children;
	}
	
	for (var i = 0; obChildren != null && i < obChildren.length; i++)
	{
		if (obChildren[i].className == "treeLeafNode" ||
			obChildren[i].className == "treeSubParentNode")
		{
			obChildren[i].style.display = "none";
		}
	}
}

// Function expands the node that has been clicked.
function expandNode(ob)
{
	if (null == ob)
	{
		return;
	}
	
	ob.className = "nodeExpanded";
	toggleNodeImage(ob, false);
	
	// Get all the children and set their display status to show..
	var strParentClass = null;
	var obChildren = null;
	if (ob.parentElement != null && ob.parentElement.children != null)
	{
		obChildren = ob.parentElement.children;
		strParentClass = ob.parentElement.className;
	}
	
	
	for (var i = 0; obChildren != null && i < obChildren.length; i++)
	{
		if (obChildren[i].className == "treeLeafNode" ||
			obChildren[i].className == "treeSubParentNode")
		{
			obChildren[i].style.display = "block";
		}
	}
}

// Function toggles the image between + & -
function toggleNodeImage(ob, bCollapsed)
{
	if (ob == null)
	{
		return;
	}
	// If the bCollapsed flafgis set, then put the "+"
	// iage, otherwise put "-" image.
	var obChildren = ob.children;
	for (var i = 0; obChildren != null && i < obChildren.length; i++)
	{
		if (obChildren[i].tagName.toUpperCase() == "IMG")
		{
			obChildren[i].src = (bCollapsed) ? "plus_blue.gif" : "minus_blue.gif";
		}
	}
}
