/** 
 *  Loosely based on Folding Menu Tree - Dynamic Drive (www.dynamicdrive.com)
 *  For full source code, installation instructions, 100's more DHTML 
 *  scripts, and Terms Of  Use, visit dynamicdrive.com
 *
 *  by Mark Quinn (mark@robocast.com) November 2nd 1998
 */

	// declare a constant that indicates what the ID of any container
	// nodes are in the tree. This helps us search for the most 
	// immediately parent container tag when one of a container's 
	// children is clicked on
	//
	// NOTE: These constants must match the VBScript equivalent defined
	// in SDReportInitialize.ASP
	var strID_CONTAINER = "ObjectContainer"
	var strID_CHILDREN = "ObjectChildren"
	var strID_LIST = "ObjectList"
	var strID_OPEN_CLOSE_IMAGE = "openCloseImage"
	
	// paths to the various image files used to represent containers
	// in the object list
	var strOPEN_CONTAINER = "/dpws/images/OpenFolder.gif"
	var strCLOSED_CONTAINER = "/dpws/images/ClosedFolder.gif"
	
	// various style attributes for containers
	var strHIDDEN_CONTAINER = "none"
	var strVISIBLE_CONTAINER = ""
	

	function DocumentOnClick()
	{
		// declare a constant that indicates the position in the child
		// collection of an ObjectList where the children of the
		// container may be found
		var INDEX_CONTAINER_CHILDREN = 1
		
		// if we don't have access to the DOM, no point in continuing
		// with the click event
		if ( !document.all )
		{
			return
		      
		}  // if document object model available
		   
		// starting at the element that was clicked on, iterate up through
		// the hierarchy of parent tags until (a) we reach the top of the
		// document, in which case the user did not click in the tree; OR
		// (b) we reach the nearest container node
		var pParentElement = event.srcElement
		while ( pParentElement != null && pParentElement.id != strID_CONTAINER )
		{
		 	pParentElement = pParentElement.parentElement
		}
		 
		// if a container element was found, change it's style to represent
		// an open or closed container  
		if ( ( pParentElement != null ) && ( pParentElement.id == strID_CONTAINER ) )
		{
			// the parent of the current parent element should be the list element 
			// that surrounds the element that was clicked upon, as well as the 
			// children of that element. Take care of hiding or displaying the
			// children of the element that was clicked upon.
			var childElement = pParentElement.parentElement.children( INDEX_CONTAINER_CHILDREN )
			
			if ( childElement != null )
			{   
				// if the container's children are hidden, make them visible. If
				// the container's children are visible, hiden them. Clicking
				// the container always has the effect of making its hidden/
				// visible state the opposite of its current state
				if ( childElement.style.display == strHIDDEN_CONTAINER ) 
				{
					childElement.style.display = strVISIBLE_CONTAINER
					
					var pElement = pParentElement.all( strID_OPEN_CLOSE_IMAGE )
					pElement.src = strOPEN_CONTAINER
					
				}  // if element hidden
						   
				else 
				{
					childElement.style.display = strHIDDEN_CONTAINER

					var pElement = pParentElement.all( strID_OPEN_CLOSE_IMAGE )
					pElement.src = strCLOSED_CONTAINER
					
				}  // else element visible
				
			}  // if child element exists
				      
		}  // if expected parent element found
		   
	}  // DocumentOnClick

	document.onclick = DocumentOnClick
	
	// Get cookie routine by Shelley Powers 
	// (shelley.powers@ne-dev.com)
	//
	// This utility function returns the value of a cookie with the 
	// provided name
	//
	function getCookie( strCookieName ) 
	{
		// string to search for - cookies always have an '=' after the name
		var strCookieString = strCookieName + "="
		
		// initialize the return value - if the cookie is not found, we
		// return a blank string
		var strCookieValue = "";
	
		if ( document.cookie.length > 0 ) 
		{
			// find the cookie
			var lOffset = document.cookie.indexOf( strCookieString )
	    
			// if cookie exists grab the value of it
			if ( lOffset != -1 ) 
			{ 
				// increment the offset into the cookie file to just past
				// the "Name=" string. Set index to beginning of value
				lOffset += strCookieString.length
				var lCookieEndIndex = document.cookie.indexOf( ";", lOffset );
			  
				// set index to the end of the cookie value
				if ( lCookieEndIndex == -1 ) 
				{
					lCookieEndIndex = document.cookie.length;
				}
		  
				// set the return value to be the unescaped value of the 
				// cookie, which is computed based on the indexes set above
				strCookieValue = unescape( document.cookie.substring( lOffset, lCookieEndIndex ) )
				
			}  // if cookie found
			
		}  // if cookies are presnet

		return strCookieValue
		
	}  // getCookie


	var listContainers = null
	var strOpenContainers = ""
	
	// This event handler is triggered when the main HTML document is loaded
	// on the client side. It will simply examine the cookies for the 
	// URL and attempt to restore the persisted states of the objects in the
	// tree
	//
	function documentOnLoad()
	{
		// find all the tags representing children - do this in the OnLoad
		// handler to ensure that the document.all collection is fully formed
		listContainers = document.all( strID_CHILDREN )
				
		// get the value of the cookie that corresponds to the currently loaded
		// page
		var strCookie = getCookie( window.location.pathname )
		if ( ( strCookie != "" ) && ( listContainers ) )
		{
			// tokenize the cookie value according to space delimiters
			var cookieTokens = strCookie.split( " " )
			
			// loop over the cookie tokens - these values indicate what
			// containers are currently open
			for ( i = 0; i < cookieTokens.length; i++ )
			{
				// make sure the open containers are set to visible
				listContainers[ cookieTokens[i] ].style.display = strVISIBLE_CONTAINER
				
				// set the image associated with open containers
				//var pContainer = listContainers [ cookieTokens[i] ].children( 0 ).children( 0 )
				var pContainer = listContainers [ cookieTokens[i] ].parentElement.children( 0 )
				if ( pContainer.id == strID_CONTAINER )
				{
					var pElement = pContainer.all( strID_OPEN_CLOSE_IMAGE )
					pElement.src = strOPEN_CONTAINER
					//	listContainers[ cookieTokens[i] ].children( 0 ).style.listStyleImage = "url(images/OpenFolder.gif)"
				
				}  // if element is a container
					
			}  // for i
			
		}  // if cookie for this page found

	}  // documentOnLoad
	
	
	// This function is called when the HTML document is unloaded from the 
	// web browser. Its main purpose is to persist the open/closed state
	// of all the container nodes in the tree
	//
	function persistContainerStates()
	{
		// loop over the list of containers and add the index of each container
		// to a space-delimited string that indicates what containers are
		// currently hidden
		if ( listContainers )
		{
			for ( i = 0; i <= ( listContainers.length - 1 ); i++ )
			{
				if ( listContainers[i].style.display == strVISIBLE_CONTAINER )
				{
					strOpenContainers = strOpenContainers + " " + i
					
				}  // if container visible
				
			}  // for i
			
			// set the value of the cookie for this page to indicate what
			// containers are currently open
			document.cookie = window.location.pathname + "=" + strOpenContainers
		
		}  // if listContainers not NULL
		
	}  // persistContainerStates
	
	function expandAll() {

		listContainers = document.all( strID_CHILDREN )
		for (i=0; i < listContainers.length; i++) {
			listContainers[i].style.display = strVISIBLE_CONTAINER;
		}

		listContainers = document.all( strID_OPEN_CLOSE_IMAGE )
		for (i=0; i < listContainers.length; i++) {
			listContainers[i].src = strOPEN_CONTAINER
		}  // if element hidden
	}
	
	function collapseAll() {

		listContainers = document.all( strID_CHILDREN )
		for (i=0; i < listContainers.length; i++) {
			listContainers[i].style.display = strHIDDEN_CONTAINER;
		}

		listContainers = document.all( strID_OPEN_CLOSE_IMAGE )
		for (i=0; i < listContainers.length; i++) {
			listContainers[i].src = strCLOSED_CONTAINER
		}  // if element hidden
	}
