//
//====================================================================================================
//
//					     TreeControl
//                                           Version 1.7
//
//                                    Copyright (c) 2005 - 2007
//                                        Linda Gail Walters
//                                       All Rights Reserved
//
//====================================================================================================
//

//
// Constant Literals
//
var treeConstant = {
	branchTag               : 'branch',
	seedTag                 : 'seed',
	leafTag                 : 'leaf',
	navigateTag             : 'navigate',
	expandTag               : 'expand',
	expandIconTag           : 'expandIcon',
	collapseIconTag         : 'collapseIcon',
	defaultBranchIconTag    : 'defaultBranchIcon',
	defaultLeafIconTag      : 'defaultLeafIcon',
	
	idAttrib                : 'id',
	labelAttrib             : 'label',
	tooltipAttrib           : 'tooltip',
	iconAttrib              : 'icon',
	targetAttrib            : 'target',
	refAttrib               : 'ref',
	ruleAttrib              : 'rule',
	
	expandDisplay           : 'block',
	collapseDisplay         : 'none',
	
	insertionPlace          : 'BeforeEnd',
	
	containerIDSuffix       : '_container',
	iconIDSuffix            : '_icon',
	toggleIDSuffix          : '_toggle',
	anchorIDSuffix          : '_anchor',
	bogusIDSuffix           : '_treeNode'
}; // end treeConstant


//
// Default Literals
//
var treeDefault = {
	collapseIcon	: 'images/collapseIcon.gif',
	expandIcon	: 'images/expandIcon.gif',
	blankIcon	: 'images/blankIcon.gif',

	rootIcon	: 'images/folder.gif',
	branchIcon	: 'images/CoffeeSignRedIcon.gif',
	leafIcon	: 'images/ind_green.gif',

	branchLabel	: 'treeNode',
	leafLabel	: 'treeLeaf',
	tooltip		: 'tool tip',
	expand		: 'javascript:void(0);',
	navigate	: '',
	target		: '_blank'
}; // end treeDefault


//
// Tree Generic Literals
//
var treeGeneric = {
	toggle    	: function (oItem) {if(oItem && oItem.id) this.all[oItem.id.replace('_toggle','')].toggle(); },
	bogusIDCounter	: 0,
	all       	: {}
}; // treeGeneric


//
//****************************************
//            TreeNode Class
// An abstract class that serves as the
// base class for all SC Tree Widget
// classes.
//****************************************
//


//
// treeNode Constructor
//
function treeNode() {
	this.id = "";
	this.label = "";
	this.tooltip = treeDefault.tooltip;
	this.icon = "";
	this.navigate = treeDefault.navigate;
	this.target = treeDefault.target;
} // end function treeNode()


//
//****************************************
//              Tree Class
//****************************************
//


//
// tree Constructor
//
function tree(sId, sLabel) {
	this.base = treeNode;
	this.base(sId, sLabel);
	this.children = [];
} // end function tree(sId, sLabel)


//
// set inheritance
//
tree.prototype = new treeNode;


//
// add method
//
tree.prototype.add = function(treeNode) {
	treeNode.parentNode = this;
	this.children[this.children.length] = treeNode;
} // end tree.prototype.add = function(treeNode)


//
// Plants the tree on the page
//
tree.prototype.plant = function(xmlSource, xmlInit) {
var xmlHttp;
var xmlDoc;

	if (xmlInit) {
		if (xmlInit != "") {
			this.initializeTree(xmlInit);
		} // end if (xmlInit != "")
	} // end if (xmlInit)

	if (window.ActiveXObject) {
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	} else if (window.XMLHttpRequest) {
		xmlHttp = new XMLHttpRequest();
	} else {
		alert("Error - Cannot create xmlHttp object");
	} // end if (window.ActiveXObject)

	xmlHttp.open("GET", xmlSource, false);
	xmlHttp.send(null);

	try {
		if (window.ActiveXObject) {
			xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
			xmlDoc.loadXML(xmlHttp.responseText);
		} else {
	  		xmlDoc = xmlHttp.responseXML;
		} // end if (window.ActiveXObject)

	} catch (ex) {
		alert("Error creating xmlDoc");
	} // end try..catch


	//
	// Because of the schema which we are using, we can 
	// assume that the initial XML document has a single <branch />
	// as its first and only element and that this single <branch />
	// then has the branches, seeds and leaves of the tree. This first
	// branch is essentially a "Root" for the tree.
	//
	var xmlRootNode = xmlDoc.firstChild;
	
	//
	//	Get and iterate over the xmlRootNode's children
	//
	var xmlChildren = xmlRootNode.childNodes;
	
	for (var i = 0, iMax = xmlChildren.length; i < iMax; i++) {
		var xmlChildNode = xmlChildren[i];
		
		if (xmlChildNode.nodeName == treeConstant.branchTag) {
			var branchChild = new treeBranch(xmlChildNode);		
			this.add(branchChild);
		} else if (xmlChildNode.nodeName == treeConstant.leafTag) {
			var leafChild = new treeLeaf(xmlChildNode);
			this.add(leafChild);
		} else {
			// Do Nothing. Here for completeness and future expansion
			//
		} // end if (xmlChildNode.nodeName == treeConstant.branchTag)
	} // end for (var i = 0, iMax = xmlChildren.length; i < iMax; i++)
} // end tree.prototype.plant = function(xmlSource)


tree.prototype.initializeTree = function(xmlInit) {
var xmlHttp;
var xmlInitDoc;
	
	if (window.ActiveXObject) {
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	} else if (window.XMLHttpRequest) {
		xmlHttp = new XMLHttpRequest();
	} else {
		alert("Error - Cannot create xmlHttp object");
	} // end if (window.ActiveXObject)

	xmlHttp.open("GET", xmlInit, false);
	xmlHttp.send(null);

	try {
		if (window.ActiveXObject) {
			xmlInitDoc = new ActiveXObject("Microsoft.XMLDOM");
			xmlInitDoc.loadXML(xmlHttp.responseText);
		} else {
	  		xmlInitDoc = xmlHttp.responseXML;
		} // end if (window.ActiveXObject)

	} catch (ex) {
		alert("Error creating xmlInitDoc");
	} // end try..catch

	var xmlRootNode = xmlInitDoc.firstChild;
	var xmlChildren = xmlRootNode.childNodes;

	for (var i = 0, iMax = xmlChildren.length; i < iMax; i++) {
		var xmlChildNode = xmlChildren[i];
		
		if (xmlChildNode.nodeName == treeConstant.expandIconTag) {
			var newExpandIcon = xmlChildNode.getAttribute(treeConstant.iconAttrib);
			if (newExpandIcon) {
				if (newExpandIcon != "") {
					treeDefault.expandIcon = newExpandIcon;
				} // end if (newExpandIcon != "")
			} // end if (newExpandIcon)

		} else if (xmlChildNode.nodeName == treeConstant.collapseIconTag) {
			var newCollapseIcon = xmlChildNode.getAttribute(treeConstant.iconAttrib);
			
			if (newCollapseIcon) {
				if (newCollapseIcon != "") {
					treeDefault.collapseIcon = newCollapseIcon;
				} // end if (newCollapseIcon != "")
			} // end if (newCollapseIcon)

		} else if (xmlChildNode.nodeName == treeConstant.defaultBranchIconTag) {
			var newDefaultBranchIcon = xmlChildNode.getAttribute(treeConstant.iconAttrib);

			if (newDefaultBranchIcon) {
				if (newDefaultBranchIcon != "") {
					treeDefault.branchIcon = newDefaultBranchIcon;
				} // end if (newDefaultBranchIcon != "")
			} // end if (newDefaultBranchIcon)

		} else if (xmlChildNode.nodeName == treeConstant.defaultLeafIconTag) {
			var newDefaultLeafIcon = xmlChildNode.getAttribute(treeConstant.iconAttrib);
			if (newDefaultLeafIcon) {
				if (newDefaultLeafIcon != "") {
					treeDefault.leafIcon = newDefaultLeafIcon;
				} // end if (newDefaultLeafIcon != "")
			} // end if (newDefaultLeafIcon)

		} else {
			//
			// Do Nothing. Here for completeness and future expansion
			//
		} // end if (xmlChildNode.nodeName == treeConstant.expandIconTag)
	} // end for (var i = 0, iMax = xmlChildren.length; i < iMax; i++)
} // end tree.prototype.initializeTree = function(xmlInit)


//
// toString method
//
tree.prototype.toString = function() {
	var strOut = "";

	strOut = strOut + "<ul class=\"treeRoot\">";
	
	if (this.children) {
		for (var i = 0, iMax = this.children.length; i < iMax; i++) {
			var childNode = this.children[i];
			strOut = strOut + childNode.toString();
		} // end for (var i = 0, iMax = this.children.length; i < iMax; i++)
	} // end if (this.children)

	strOut = strOut + "</ul>";
	return strOut;
} // end tree.prototype.toString = function()


//
//****************************************
//           Tree Branch Class
//****************************************
//


//
// Constructor
//
function treeBranch(xmlNodeIn) {
	this.base = treeNode;
	this.base();

	this.loaded = false;
	this.loading = false;
	this.expanded = false;
	this.label = treeDefault.branchLabel;
	this.icon = treeDefault.branchIcon;
	this.children = [];
	
	this.parseXML(xmlNodeIn);	

	treeGeneric.all[this.id] = this;
} // end function treeBranch(xmlNodeIn)


//
// set inheritance
//
treeBranch.prototype = new treeNode;


//
// add method
//
treeBranch.prototype.add = function(treeNode) {
	treeNode.parentNode = this;
	this.children[this.children.length] = treeNode;
} // end treeBranch.prototype.add = function(treeNode)


//
// parseXML method
//
treeBranch.prototype.parseXML = function(xmlNodeIn) {
	if (xmlNodeIn.attributes) {
		if ((this.id = xmlNodeIn.getAttribute(treeConstant.idAttrib)) == null) {
			treeGeneric.bogusIDCounter++;
			this.id = treeGeneric.bogusIDCounter + treeConstant.bogusIDSuffix;
		} // end if ((this.id = xmlNodeIn.getAttribute(treeConstant.idAttrib)) == null)

		if ((this.label = xmlNodeIn.getAttribute(treeConstant.labelAttrib)) == null) {
			this.label = treeDefault.branchLabel;
		} // end if ((this.label = xmlNodeIn.getAttribute(treeConstant.labelAttrib)) == null)
				
		if ((this.tooltip = xmlNodeIn.getAttribute(treeConstant.tooltipAttrib)) == null) {
			this.tooltip = treeDefault.tooltip;
		} // end if ((this.tooltip = xmlNodeIn.getAttribute(treeConstant.tooltipAttrib)) == null)
		
		if ((this.icon = xmlNodeIn.getAttribute(treeConstant.iconAttrib)) == null) {
			this.icon = treeDefault.branchIcon;
		} // end if ((this.icon = xmlNodeIn.getAttribute(treeConstant.iconAttrib)) == null)
	} // end if (xmlNodeIn.attributes)

	//
	// If the node is a Branch as opposed to a Seed, then its children
	// are in the current XML document, so we can load them
	//		
	if (xmlNodeIn.nodeName == treeConstant.branchTag) {
		//
		//	Get and iterate over the xmlRootNode's children
		//
		var xmlChildren = xmlNodeIn.childNodes;
		
		for (var i = 0, iMax = xmlChildren.length; i < iMax; i++) {
			var xmlChildNode = xmlChildren[i];
			
			if (xmlChildNode.nodeName == treeConstant.branchTag) {
				var branchChild = new treeBranch(xmlChildNode);		
				this.add(branchChild);
			} else if (xmlChildNode.nodeName == treeConstant.leafTag) {
				var leafChild = new treeLeaf(xmlChildNode);
				this.add(leafChild);
			} else {
				//
				// Do Nothing. Here for completeness and future expansion
				//
			} // end if (xmlChildNode.nodeName == treeConstant.branchTag)
		} // end for (var i = 0, iMax = xmlChildren.length; i < iMax; i++)
		
		this.loaded = true;
		this.expand = "";

	//
	// Otherwise, this is a seed, parse out its expandAction element
	//		
	} else {
		this.loaded = false;
		this.expand = treeDefault.expand;

		//
		// Parse out the navigateAction element
		//
		var xmlChildren = xmlNodeIn.childNodes;
		
		for (var i = 0, iMax = xmlChildren.length; i < iMax; i++) {
			var xmlChildNode = xmlChildren[i];
			
			if (xmlChildNode.nodeName == treeConstant.expandTag) {
				if ((this.expand = xmlChildNode.getAttribute(treeConstant.ruleAttrib)) == null) {
					this.expand = treeDefault.expand;
				} // end if ((this.expand = xmlChildNode.getAttribute(treeConstant.ruleAttrib)) == null)
			} // end if (xmlChildNode.nodeName == treeConstant.expandTag)
		} // end for (var i = 0, iMax = xmlChildren.length; i < iMax; i++)
	} // end if (xmlNodeIn.nodeName == treeConstant.branchTag)

	//
	// Parse out the navigateAction element
	//
	var xmlChildren = xmlNodeIn.childNodes;
	
	for (var i = 0, iMax = xmlChildren.length; i < iMax; i++) {
		var xmlChildNode = xmlChildren[i];
		
		if (xmlChildNode.nodeName == treeConstant.navigateTag) {
			if ((this.navigate = xmlChildNode.getAttribute(treeConstant.refAttrib)) == null) {
				this.navigate = treeDefault.navigate;
			} // end if ((this.navigate = xmlChildNode.getAttribute(treeConstant.refAttrib)) == null)

			if ((this.target = xmlChildNode.getAttribute(treeConstant.targetAttrib)) == null) {
				this.target = treeDefault.target;
			} // end if ((this.target = xmlChildNode.getAttribute(treeConstant.targetAttrib)) == null)
		} // end if (xmlChildNode.nodeName == treeConstant.navigateTag)
	} // end for (var i = 0, iMax = xmlChildren.length; i < iMax; i++)
} // end treeBranch.prototype.parseXML = function(xmlNodeIn)


//
// toggle method
//
treeBranch.prototype.toggle = function(){

	//
	// If this is a seed, load its children
	//
	if (!this.loaded) {
		this.loadSeed(this.expand);
		this.loaded = true;
	} // end if (!this.loaded)
	
	if ((this.loaded) && (this.children.length > 0)) {
		if (this.expanded) {
			document.getElementById(this.id + treeConstant.containerIDSuffix).style.display = treeConstant.collapseDisplay
			document.getElementById(this.id + treeConstant.toggleIDSuffix).src = treeDefault.expandIcon; 
		} else {
			document.getElementById(this.id + treeConstant.containerIDSuffix).style.display = treeConstant.expandDisplay
			document.getElementById(this.id + treeConstant.toggleIDSuffix).src = treeDefault.collapseIcon; 
		} // end if (this.expanded)

		this.expanded = !this.expanded;
	} else {
		document.getElementById(this.id + treeConstant.toggleIDSuffix).src = treeDefault.blankIcon; 
	} // end if ((this.loaded) && (this.children.length > 0))
} // end treeBranch.prototype.toggle = function()


//
// loadSeed method
//
treeBranch.prototype.loadSeed = function(serviceName, parameter) {
	var xmlDoc = tpzService("name=" + serviceName);	
	var xmlSeedNode = xmlDoc.firstChild;
	
	//
	//	Get and iterate over the xmlSeedNode's children
	//
	var xmlChildren = xmlSeedNode.childNodes;
	
	//
	// Iterate over the XML child nodes of the seed node and add
	// each and its children, if any, to the tree model
	//
	for (var i = 0, iMax = xmlChildren.length; i < iMax; i++) {
		var xmlChildNode = xmlChildren[i];
		
		if (xmlChildNode.nodeName == treeConstant.branchTag) {
			var branchChild = new treeBranch(xmlChildNode);		
			this.add(branchChild);
		} else if (xmlChildNode.nodeName == treeConstant.leafTag) {
			var leafChild = new treeLeaf(xmlChildNode);
			this.add(leafChild);
		} else {
			//
			// Do Nothing. Here for completeness and future use
			//
		} // end if (xmlChildNode.nodeName == treeConstant.branchTag) 
	} // end for (var i = 0, iMax = xmlChildren.length; i < iMax; i++)

	//
	// If the tree seed node has children, iterate over them, outputting
	// each and its children, if any, to an HTML string and inserting that
	// HTML into the container (<ul>...</ul>) for the seed.
	//
	if (this.children) {
		var oElement = document.getElementById(this.id + treeConstant.containerIDSuffix);

		for (var i = 0, iMax = this.children.length; i < iMax; i++) {
			var childNode = this.children[i];
			var strInsert = childNode.toString();
	
			//
			// If the element supports the insertAdjacenmtHTML method, use it,
			// otherwise, do it the hard way.
			//
			if (oElement.insertAdjacentHTML != null) {
				oElement.insertAdjacentHTML(treeConstant.insertionPlace, strInsert)
			} else {
				var range = oElement.ownerDocument.createRange();
				range.selectNodeContents(oElement);
				range.collapse(false);
				
				var docFrag = range.createContextualFragment(strInsert);
				oElement.appendChild(docFrag);
			} // end if (oElement.insertAdjacentHTML != null)
		} // end for (var i = 0, iMax = this.children.length; i < iMax; i++)
	} // end if (this.children)
} // end treeBranch.prototype.loadSeed = function(serviceName, parameter)


//
// toString method
//
treeBranch.prototype.toString = function() {
	var strOut = "";
	
	strOut = strOut + "<li id=\"" + this.id + "\" class=\"tree\" title=\"" + this.tooltip + "\">";
	strOut = strOut + "<img id=\"" + this.id + treeConstant.toggleIDSuffix + "\" class=\"treeToggle\" src=\"";
	strOut = strOut + treeDefault.expandIcon + "\" alt=\"\" onClick=\"treeGeneric.toggle(this);\">";
	strOut = strOut + "<img id=\"" + this.id + treeConstant.iconIDSuffix + "\" class=\"treeIcon\" src=\"" + this.icon;
//	strOut = strOut + "\" alt=\"\" onClick=\"" + this.navigate + "\";>";
	strOut = strOut + "\" alt=\"\">";
	strOut = strOut + "&nbsp;"
	
	if ((this.navigate != "") && (this.navigate != null)) {
		strOut = strOut + "<a class=\"contentLine\" id=\"" + this.id + treeConstant.anchorIDSuffix;
                strOut = strOut + "\" target=\"" + this.target + "\" class=\"treeAnchor\" href=\"";
                strOut = strOut + this.navigate + "\">" + this.label + "</a>";
	} else {
		strOut = strOut + this.label
	} // end if ((this.navigate != "") && (this.navigate != null))
	
	strOut = strOut + "</li>";
	strOut = strOut + "<ul id=\"" + this.id + treeConstant.containerIDSuffix + "\" class=\"treeBranch\">";
	
	//
	// If the branch has been loaded and has children, iterate over them
	// and output each to the output string.
	//
	if (this.loaded) {
		if (this.children) {
			for (var i = 0, iMax = this.children.length; i < iMax; i++) {
				var childNode = this.children[i];
				strOut = strOut + childNode.toString();
			} // end for (var i = 0, iMax = this.children.length; i < iMax; i++)
		} // end if (this.children)
	} // end if (this.loaded)
	
	strOut = strOut + "</ul>";
	return strOut;		
} // end treeBranch.prototype.toString = function()


//
//****************************************
//            Tree Leaf Class
//****************************************
//


//
// treeLeaf Constructor
//
function treeLeaf(xmlNodeIn) {
	this.base = treeNode;
	this.base();

	this.label = treeDefault.leafLabel;
	this.icon = treeDefault.leafIcon;
	
	this.parseXML(xmlNodeIn);	

	treeGeneric.all[this.id] = this;
} // end function treeLeaf(xmlNodeIn)


//
// set inheritance
//
treeLeaf.prototype = new treeNode;


//
// parseXML method
//
treeLeaf.prototype.parseXML = function(xmlNodeIn) {
	if (xmlNodeIn.attributes) {
		if ((this.id = xmlNodeIn.getAttribute(treeConstant.idAttrib)) == null) {
			treeGeneric.bogusIDCounter++;
			this.id = treeGeneric.bogusIDCounter + treeConstant.bogusIDSuffix;
		} // end if ((this.id = xmlNodeIn.getAttribute(treeConstant.idAttrib)) == null)

		if ((this.label = xmlNodeIn.getAttribute(treeConstant.labelAttrib)) == null) {
			this.label = treeDefault.leafLabel;
		} // end if ((this.label = xmlNodeIn.getAttribute(treeConstant.labelAttrib)) == null)
				
		if ((this.tooltip = xmlNodeIn.getAttribute(treeConstant.tooltipAttrib)) == null) {
			this.tooltip = treeDefault.tooltip;
		} // end if ((this.tooltip = xmlNodeIn.getAttribute(treeConstant.tooltipAttrib)) == null)

		if ((this.icon = xmlNodeIn.getAttribute(treeConstant.iconAttrib)) == null) {
			this.icon = treeDefault.leafIcon;
		} // end if ((this.icon = xmlNodeIn.getAttribute(treeConstant.iconAttrib)) == null)
	} // end if (xmlNodeIn.attributes)
	
	//
	// Parse out the navigateAction element
	//
	var xmlChildren = xmlNodeIn.childNodes;
	
	for (var i = 0, iMax = xmlChildren.length; i < iMax; i++) {
		var xmlChildNode = xmlChildren[i];
		
		if (xmlChildNode.nodeName == treeConstant.navigateTag) {
			if ((this.navigate = xmlChildNode.getAttribute(treeConstant.refAttrib)) == null) {
				this.navigate = treeDefault.navigate;
			} // end if ((this.navigate = xmlChildNode.getAttribute(treeConstant.refAttrib)) == null)

			if ((this.target = xmlChildNode.getAttribute(treeConstant.targetAttrib)) == null) {
				this.target = treeDefault.target;
			} // end if ((this.target = xmlChildNode.getAttribute(treeConstant.targetAttrib)) == null)
		} // end if (xmlChildNode.nodeName == treeConstant.navigateTag)
	} // end for (var i = 0, iMax = xmlChildren.length; i < iMax; i++)
} // end treeLeaf.prototype.parseXML = function(xmlNodeIn)


//
// toString method
//
treeLeaf.prototype.toString = function() {
var strOut = "";

	strOut = strOut + "<li id=\"" + this.id + "\" class=\"tree\" title=\"" + this.tooltip + "\">";
	strOut = strOut + "<img id=\"" + this.id + treeConstant.toggleIDSuffix + "\" class=\"treeToggle\" src=\"";
        strOut = strOut + treeDefault.blankIcon + "\" alt=\"\">";
	strOut = strOut + "<img id=\"" + this.id + treeConstant.iconIDSuffix + "\" class=\"treeIcon\" src=\"" + this.icon;
        strOut = strOut + "\" alt=\"\">";
	strOut = strOut + "&nbsp;"
	
	//
	// If there is no navigate value, don't output the anchor tag
	//
	if ((this.navigate != "") && (this.navigate != null)) {
		strOut = strOut + "<a class=\"contentLine\" id=\"" + this.id + treeConstant.anchorIDSuffix;
                strOut = strOut + "\" target=\"" + this.target + "\" class=\"treeAnchor\" href=\"";
                strOut = strOut + this.navigate + "\">" + this.label + "</a>";
	} else {
		strOut = strOut + this.label;
	} // end if ((this.navigate != "") && (this.navigate != null))
	
	strOut = strOut + "</li>";

	return strOut;	
} // end treeLeaf.prototype.toString = function()


