/**************************************************************************
	Copyright (c) 2001 Geir Landrö (drop@destroydrop.com)
	JavaScript Tree - www.destroydrop.com/hugi/javascript/tree/
	Version 0.96	

	This script can be used freely as long as all copyright messages are
	intact.
**************************************************************************/

/*************************************************************************
Editor: Dale Holborow
Edit Date: 	29-08-02 -> 
Notes:	Loads a set of custom tree images for the dynamic drawing of an expandable tree set.
		Tests these against hard coded named conventions.
		Several adaptations including ability to prefix pages with checkboxes, and a list of which checkboxes autochecked
		Ability to make tree branches not linked
		Ability to open all branches at start
*************************************************************************/

function TreeTextObject () {

	// Arrays for nodes and icons
	this.nodes			= new Array();			// array of nodes making up the tree
	this.openNodes		= new Array();			// array of nodes in the tree which are open
	this.icons			= new Array(11);		// icons used to draw the tree structure
	this.deleteNodes	= new Array();			// array of nodes which are set to be deleted by SimpleWiz (used for checkbox render)

	this.myName			= 'treeObj';			// object name
	this.myId			= 0;					// placement id, to allow multiple instances on a single page
	this.cssNavLink 	= '';
	this.cssNavLinkHi 	= '';
	this.CustSrcDir 	= '';
	//this.startNode;
	//this.openNode;
	//this.disableActive;
	this.linkPrefix 	= '/asp/index.asp?pgid=';
	this.linkTarget 	= null;
	
	// Push and pop not implemented in IE(crap!    don´t know about NS though)
	if (!Array.prototype.push) {
		function array_push() {
			for(var i = 0; i < arguments.length; i++)
				this[this.length] = arguments[i];
			return this.length;
		}
		Array.prototype.push = array_push;
	}
	if (!Array.prototype.pop) {
		function array_pop(){
			lastElement = this[this.length-1];
			this.length = Math.max(this.length-1, 0);
			return lastElement;
		}
		Array.prototype.pop = array_pop;
	}
	
	
	
	//------------------------------------------------------
	// Loads all icons that are used in the tree
	//------------------------------------------------------
	this.preloadIcons = function (aImageArray, aCustSrcDir) {
		
		var defaultSrcDir = "/public/images/"	// folder storing default images
		var IconArray = new Array();
		for (var i in aImageArray) {
			var imageValues = ("" + aImageArray[i]).split("<|>");
			IconArray[IconArray.length] = imageValues
		}


		// SHOW THE DEFAULT TREE STRUCTURE IMAGES
		// set up the icons to be the default, then go thru and populate with any custom ones defined by a user design
		this.icons[0] = new Image();
		this.icons[1] = new Image();
		this.icons[2] = new Image();
		this.icons[3] = new Image();
		this.icons[4] = new Image();
		this.icons[5] = new Image();
		this.icons[6] = new Image();
		this.icons[7] = new Image();
		this.icons[8] = new Image();
		this.icons[9] = new Image();
		this.icons[10] = new Image();
		this.icons[11] = new Image();
		
		
/*
		// SHOW ALL THE TREE STRUCTURE IMAGES AS DEFAULT IMAGES
		// set up the icons to be the default, then go thru and populate with any custom ones defined by a user design
		this.icons[0].src = defaultSrcDir + "treeclosed.gif"
		this.icons[1].src = defaultSrcDir + "treeclosedbottom.gif"
		this.icons[2].src = defaultSrcDir+ "treeopen.gif"
		this.icons[3].src = defaultSrcDir + "treeopenbottom.gif"
		this.icons[4].src = defaultSrcDir + "treespacerclosed.gif"
		this.icons[5].src = defaultSrcDir + "treespaceropen.gif"		
		this.icons[6].src = defaultSrcDir + "treeempty.gif"
		this.icons[7].src = defaultSrcDir + "treejoin.gif"
		this.icons[8].src = defaultSrcDir + "treejoinbottom.gif"
		this.icons[9].src = defaultSrcDir + "treeline.gif"
		this.icons[10].src = defaultSrcDir + "treespacergeneric.gif"
		this.icons[11].src = defaultSrcDir + "treespacergenericactive.gif"
*/
		//this.icons[4].src = defaultSrcDir;

		// SET UP THE CUSTOM IMAGES
		for (var i in IconArray) {
			//alert(IconArray[i][0]);
			if (IconArray[i][0] == "TreeClosedImage") {
				this.icons[0].src = aCustSrcDir + IconArray[i][1]
			} else if (IconArray[i][0] == "TreeClosedBottomImage") {
				this.icons[1].src = aCustSrcDir + IconArray[i][1]
			} else if (IconArray[i][0] == "TreeOpenImage") {
				this.icons[2].src = aCustSrcDir + IconArray[i][1]
			} else if (IconArray[i][0] == "TreeOpenBottomImage") {
				this.icons[3].src = aCustSrcDir + IconArray[i][1]
			} else if (IconArray[i][0] == "TreeSpacerClosedImage") {
				
				this.icons[4].src = aCustSrcDir + IconArray[i][1]
			} else if (IconArray[i][0] == "TreeSpacerOpenImage") {
				this.icons[5].src = aCustSrcDir + IconArray[i][1]
			} else if (IconArray[i][0] == "TreeEmptySpaceImage") {
				this.icons[6].src = aCustSrcDir + IconArray[i][1]
			} else if (IconArray[i][0] == "TreeJoinImage") {
				this.icons[7].src = aCustSrcDir + IconArray[i][1]
			} else if (IconArray[i][0] == "TreeJoinBottomImage") {
				this.icons[8].src = aCustSrcDir + IconArray[i][1]
			} else if (IconArray[i][0] == "TreeLineImage") {
				this.icons[9].src = aCustSrcDir + IconArray[i][1]
			} else if (IconArray[i][0] == "TreeSpacerGenericImage") {
				this.icons[10].src = aCustSrcDir + IconArray[i][1]
			} else if (IconArray[i][0] == "TreeSpacerGenericActiveImage") {
				this.icons[11].src = aCustSrcDir + IconArray[i][1]
			} 
		}
		//alert('at start: '+document.location.href)
	}
	
	
	//---------------------------------------------------
	// Create the tree
	//---------------------------------------------------
	this.createTree = function (aImageArray, aImageSourceFolder, arrName, aPlacementId, aNavLink, startNode, openNode, disableActive, aNavLinkHi, aOpenAllNodes, aNoLinks, aShowCheckBox, aDeleteNodes, aHomePageId, aLinkPrefix) {
		
//		ImageArray 			= aImageArray;
		this.CustSrcDir 	= aImageSourceFolder;
		this.nodes 			= arrName;
		this.cssCurNavStyle = aNavLink;
		this.cssNavLink 	= aNavLink;
		this.cssNavLinkHi 	= aNavLinkHi;
		this.startNode 		= startNode;
		this.openNode 		= openNode;
		this.disableActive 	= disableActive;
		this.deleteNodes	= aDeleteNodes;
		this.homePageId		= aHomePageId;

		//test the openallnodes and nolinks params
		if (typeof(aOpenAllNodes) == "boolean") {
			this.openall = aOpenAllNodes
		} else {
			this.openall = false
		}
		if (typeof(aNoLinks) == "boolean") {
			this.nolinks = aNoLinks
		} else {
			this.nolinks = false
		}
		// should simple wizard be showing the "select page" checkboxes
		if (typeof(aShowCheckBox) == "boolean" && aShowCheckBox == true) {
			this.showCheckBox	= true
		} else {
			this.showCheckBox	= false;
		}
		if (typeof(aLinkPrefix) == "string") {
			this.linkPrefix = aLinkPrefix
		}
		this.myId = aPlacementId
		this.myName += aPlacementId
	
		if (this.nodes.length > 0) {
			this.preloadIcons(aImageArray, this.CustSrcDir);
	
			if (this.startNode == null) this.startNode = 0;
			
			if (this.openNode != 0 || this.openNode != null) {
				this.setOpenNodes(this.openNode);
			}
			//Should we open all nodes?
			if (this.openall) {
				for (var i in this.nodes) {
					this.setOpenNodes(i)
				}
			}
			
			//if (this.startNode != 0) {
			//	var nodeValues = ("" + this.nodes[this.getArrayId(this.startNode)]).split("<|>");
			//	if (this.nolinks) {
			//		document.write("<img src=\"/public/images/folderopen.gif\" border=\"0\" align=\"absbottom\" alt=\"\" />" + nodeValues[1] + "<br />");
			//	} else {
			//		document.write("<a href=\"/asp/index.asp?id=" + this.getArrayId(this.startNode) + "\" onmouseover=\"window.status='" + this.getArrayId(this.startNode) + "';return true;\" onmouseout=\"window.status=' ';return true;\"><img src=\"/public/images/folderopen.gif\" border=\"0\" align=\"absbottom\" alt=\"\" />" + nodeValues[1] + "</a><br />");
			//	}
			//} 
			//else { 
			// commented to not show root image
			//	document.write("<img src=\"/public/images/base.gif\" align=\"absbottom\" border=\"0\" alt=\"\" />Dale Expand Tree<br />");
			//}
		
			var recursedNodes = new Array();
			this.addNode(this.startNode, recursedNodes);
		}	
	}
	
	
	//---------------------------------------------------
	// Returns the position of a node in the array
	//---------------------------------------------------
	this.getArrayId = function (node) {
		for (var i in this.nodes) {
			var nodeValues = ("" + this.nodes[i]).split("<|>");
			if (i==node) return i;
		}
	}
	
	
	//---------------------------------------------------
	// Puts in array nodes that will be open
	//---------------------------------------------------
	this.setOpenNodes = function (openNode) {
		for (var i in this.nodes) {
			var nodeValues = ("" + this.nodes[i]).split("<|>")
			if (i == openNode) {
				this.openNodes.push(i);
				this.setOpenNodes(nodeValues[0]);
			}
		} 
	}
	
	
	//---------------------------------------------------
	// Checks if a node is open
	//---------------------------------------------------
	this.isNodeOpen = function (node) {
		for (var i in this.openNodes)
			if (this.openNodes[i]==node) return true;
		return false;
	}
	
	
	//---------------------------------------------------
	// Checks if a node has any children
	//---------------------------------------------------
	this.hasChildNode = function (parentNode) {
		for (var i in this.nodes) {
			var nodeValues = ("" + this.nodes[i]).split("<|>");
			if (nodeValues[0] == parentNode) return true;
		}
		return false;
	}
	
	
	//---------------------------------------------------
	// Checks if a node is the last sibling
	//---------------------------------------------------
	this.lastSibling = function (node, parentNode) {
		var lastChild = 0;
		for (var i in this.nodes) {
			var nodeValues = ("" + this.nodes[i]).split("<|>");
			if (nodeValues[0] == parentNode) {
				lastChild = i;
			}
		}
		if (lastChild == node) {
			return true;
		}
		return false;
	}
	
	
	//--------------------------------------------------------------------
	// Creates the html for the checkbox to sit before the page name, if in simple wizard
	// and am choosing pages to exclude. If the page exists in the array of pages to delete, 
	// the checkbox is checked
	//		params:	
	//			node		- node to use to create a checkbox's html
	//		returns:
	//			html for checkbox if showcheckbox is true, empty string otherwise
	//--------------------------------------------------------------------
	this.createCheckBoxText	= function (node) {
		var tItemName		= "selectpage_" + node;
		if (this.showCheckBox) {
			if (this.homePageId == node) {
				tCheckBoxText	= "<input type=hidden name=" + tItemName + ">";
			} else {
				var tToDelete	= false;
				for (var c in this.deleteNodes) {
					if (this.deleteNodes[c] == node) {
						tToDelete = true;
					}
				}
				if (tToDelete) {
					var tOnClick		= 'javascript: doSelectPages("'+ tItemName +'")'
					var tCheckBoxText	= "&nbsp;<input type=checkbox name=" + tItemName + " value=1 onClick='" + tOnClick + "'>&nbsp;";
				} else {
					var tOnClick		= 'javascript: doSelectPages("'+ tItemName +'")'
					var tCheckBoxText	= "&nbsp;<input type=checkbox name=" + tItemName + " value=1 onClick='" + tOnClick + "' checked>&nbsp;";
				}
			}
		} else {
			//tCheckBoxText	= "&nbsp;";
			tCheckBoxText	= "";
		}
		return tCheckBoxText;
	}
	
	
	//--------------------------------------------------------------------
	// Adds a new node in the tree
	//--------------------------------------------------------------------
	this.addNode = function (parentNode, recursedNodes) {
		
		for (var i in this.nodes) {
			var nodeValues = ("" + this.nodes[i]).split("<|>");
			this.cssCurNavStyle = (this.openNode == i) ? this.cssNavLinkHi : this.cssNavLink
			
			if (nodeValues[0] == parentNode) {
				document.write('<table width="100%" border="0" cellspacing="1" cellpadding="0" >');
				document.write("<tr>");
				document.write('<td align="left" valign="top" width="1%" nowrap>');

				var ls	= this.lastSibling(i, nodeValues[0]);
				var hcn	= this.hasChildNode(i);
				var ino = this.isNodeOpen(i);
				
				// Write out line & empty icons
				for (var g in recursedNodes) {
					if (recursedNodes[g] == 1) {
						
						if (this.icons[9].src > '' && this.icons[9].src != document.location.href) {
							document.write('<img src="' + this.icons[9].src + '" align="absbottom" border="0" alt="" >');
						}
					} else {
						if (this.icons[6].src > '' && this.icons[6].src != document.location.href) {
							document.write('<img src="' + this.icons[6].src + '" align="absbottom" border="0" alt="" >');
						}
					}
				}
				// put in array line & empty icons
				if (ls) {
					recursedNodes.push(0);
				} else {
					recursedNodes.push(1);
				}
				// Write out join icons
				//alert(parentNode + " and " + nodeValues[0] + ' hcn:' + hcn + ' and ls: ' + ls + ' and ino: ' + ino);
				if (hcn) {
					if (ls) {
						if (ino) {
							if (this.icons[3].src > '' && this.icons[3].src != document.location.href) {
								if (this.nolinks) {
									document.write('<img id="' +this.myId+ 'join' + i + '" src="' +this.icons[3].src+ '" border="0" align="absbottom" alt="Locked "' +nodeValues[1]+ '" >'); 
								} else {
									document.write('<a href="javascript: ' + this.myName + '.oc(' + i + ', 1);"><img id="' +this.myId+ 'join' + i + '" src="' +this.icons[3].src+ '" border="0" align="absbottom" alt="Open/Close "' +nodeValues[1]+ '" ></a>'); 
								}
							}
						} else {
							if (this.icons[1].src > '' && this.icons[1].src != document.location.href) {
								if (this.nolinks) {
									document.write('<img id="' +this.myId+ 'join' + i + '" src="' +this.icons[1].src+ '" border="0" align="absbottom" alt="Locked ' +nodeValues[1]+ '" >'); 
								} else {
									document.write('<a href="javascript: ' + this.myName + '.oc(' + i + ', 1);"><img id="' +this.myId+ 'join' + i + '" src="' +this.icons[1].src+ '" border="0" align="absbottom" alt="Open/Close ' +nodeValues[1]+ '" ></a>'); 
								}
							}
						}
					} else {
						if (ino) {
							if (this.icons[2].src > '' && this.icons[2].src != document.location.href) {
								if (this.nolinks) {
									document.write('<img id="' +this.myId+ 'join' + i + '" src="' +this.icons[2].src+ '" border="0" align="absbottom" alt="Locked "' +nodeValues[1]+ '" >'); 
								} else {
									document.write('<a href="javascript: ' + this.myName + '.oc(' + i + ', 0);"><img id="' +this.myId+ 'join' + i + '" src="' +this.icons[2].src+ '" border="0" align="absbottom" alt="Open/Close "' +nodeValues[1]+ '" ></a>'); 
								}
							}
						} else {
							if (this.icons[0].src > '' && this.icons[0].src != document.location.href) {
								if (this.nolinks) {
									document.write('<img id="' +this.myId+ 'join' + i + '" src="' +this.icons[0].src+ '" border="0" align="absbottom" alt="Locked "' +nodeValues[1]+ '" >'); 
								} else {
									document.write('<a href="javascript: ' + this.myName + '.oc(' + i + ', 0);"><img id="' +this.myId+ 'join' + i + '" src="' +this.icons[0].src+ '" border="0" align="absbottom" alt="Open/Close "' +nodeValues[1]+ '" ></a>'); 
								}
							}
						}
					}
				} else {
					if (ls) {
						if (this.icons[7].src > '' && this.icons[7].src != document.location.href) {
							document.write('<img src="' + this.icons[7].src + '" border="0" align="absbottom" alt="" >');
						}
					} else {
						if (this.icons[8].src > '' && this.icons[8].src != document.location.href) {
							document.write('<img src="' + this.icons[8].src + '" border="0" align="absbottom" alt="" >');
						}
					}
				}
				
				// remove any quote marks from the name of the object, since they will mess with the window status
				var nodeName = nodeValues[1].toString();
				for (var c = 0; c < nodeName.length; c++) {
					if (nodeName.charAt(c) == "'") {
						nodeName = nodeName.replace("'", "");
					}
				}
				var mouseOverTxt = "window.status='" +nodeName+ "';return true;";
				var mouseOutTxt = "window.status=' ';return true;";

				if (!this.nolinks) {
					document.write('<a href="' + this.linkPrefix + i + '"');
					if (this.linkTarget != null) {
						document.write(' target="' + this.linkTarget + '"');
					}
					document.write(' class="' + this.cssCurNavStyle + '" onmouseover="' +mouseOverTxt+ '" onmouseout="' + mouseOutTxt + '">');
				}

				// Write out spacer images (eg: folders icons in Explorer de
				if (hcn) {
					
					if (ino) {
						
						if (this.icons[5].src > '' && this.icons[5].src != document.location.href) {
							
							document.write("<img id=\"" +this.myId+ "icon" + i + "\" src=\"" +this.icons[5].src+ "\" align=\"absbottom\" border=\"0\" alt=\"" +nodeValues[1]+ "\" />");
						}
					} else {
						if (this.icons[4].src > '' && this.icons[4].src != document.location.href) {
							
							document.write("<img id=\"" +this.myId+ "icon" + i + "\" src=\"" +this.icons[4].src+ "\" align=\"absbottom\" border=\"0\" alt=\"" +nodeValues[1]+ "\" />");
						} 						
					}
				} else {
					// test if this is the active node, if so, draw its active image
					if (i == this.openNode) {
						if (this.icons[11].src > '' && this.icons[11].src != document.location.href) {
							document.write("<img id=\"" +this.myId+ "icon" + i + "\" src=\"" +this.icons[11].src+ "\" align=\"absbottom\" border=\"0\" alt=\"" +nodeValues[1]+ "\" />");
						} 
					} else {
						if (this.icons[10].src > '' && this.icons[10].src != document.location.href) {
							document.write("<img id=\"" +this.myId+ "icon" + i + "\" src=\"" +this.icons[10].src+ "\" align=\"absbottom\" border=\"0\" alt=\"" +nodeValues[1]+ "\" />");
						} 
					}
				}
				if (!this.nolinks) {
					document.write("</a>");									// end images link
				}
				document.write('</td> <td align="left" width="99%">');				// close the first cell, open the next
				
				// test if is current page, if so, dont create the link
				var mouseOverTxt = "window.status='" +nodeName+ "';return true;";
				var mouseOutTxt = "window.status=' ';return true;";
				
				
				if (i == this.openNode && this.disableActive == 1) {
					document.write('<span class="' + this.cssCurNavStyle + '" onmouseover="' + mouseOverTxt + '" onmouseout="' + mouseOutTxt + '">' + this.createCheckBoxText(i) + nodeValues[1] + ' </span>');	// Write out node name
				} else {
					// start link for name
					if (i > 0) {
						if (!this.nolinks) {
							document.write('<a href="' + this.linkPrefix + i + '"');
							if (this.linkTarget != null) {
								document.write(' target="' + this.linkTarget + '"');
							}
							document.write(' class="' + this.cssCurNavStyle + '">');
						}
						document.write(this.createCheckBoxText(i) + nodeValues[1]);				// Write out node name
						if (!this.nolinks) {
							document.write("</a>");												// End link for the text name
						}
					} else {
						document.write(this.createCheckBoxText(i) + nodeValues[1]);				// Write out node name
					}
				}
				// close table
				document.write("</td> </tr> </table>");
				
				// If node has children write out divs and go deeper
				if (hcn) {
					document.write("<div id=\"" +this.myId+ "div" + i + "\"");
					if (!ino) {
						document.write(" style=\"display: none;\"");
					}
					document.write(">");
					this.addNode(i, recursedNodes);
					document.write("</div>");
				}
				// remove last line or empty icon 
				recursedNodes.pop();
			}
		}
	}
	
	
	//---------------------------------------------------
	// Opens or closes a node
	//---------------------------------------------------
	this.oc = function (node, bottom) {
		theDiv = (document.getElementById) ? document.getElementById(this.myId + "div" + node) : document.all[this.myId + "div" + node]
		theJoin	= (document.getElementById) ? document.getElementById(this.myId + "join" + node) : document.all[this.myId + "join" + node]
		theIcon = (document.getElementById) ? document.getElementById(this.myId + "icon" + node) : document.all[this.myId + "icon" + node]
		
		// OPENING
		if (!((theDiv == null) || (typeof(theDiv) != "object"))) {
			if (theDiv.style.display == 'none') {
				if (bottom == 1) {
					theJoin.src = this.icons[3].src;
				} else theJoin.src = this.icons[2].src; {
					if (theIcon != 'null' && theIcon != null) {
						theIcon.src = this.icons[5].src;
					}
				}
				theDiv.style.display = '';
	
			// CLOSING
			} else {
				if (bottom == 1) {
					theJoin.src = this.icons[1].src;
				} else theJoin.src = this.icons[0].src; {
					if (theIcon != 'null' && theIcon != null) {
						theIcon.src = this.icons[4].src;
					}
				}
				theDiv.style.display = 'none';
			}
		}
	}
}
