//--------------------------------------------------------------------------------------------------
// All material contained within this and associated downloaded pages is the property of 4thorder(TM) 
// Copyright © 2005.  All rights reserved.
//
// Author: Michael Falatine || Authors email: 4thorder@4thorder.us
//
// USAGE: You may use this script for commercial or personal use, however, the copyright is retained-
// by 4thorder (TM).
//
// For other free Scripts visit: http://www.4thorder.us/Scripts/
//---------------------------------------------------------------------------------------------------

// :::::::::::::::::::::::::
// :::: Global Variables :::
// :::::::::::::::::::::::::

var firstLoad=0
var GlobalECState=0 // 0=collapsed, 1=expanded; used for inital state of ALL branches

// :::::::::::::::::::::::::
// :::: Global Functions :::
// :::::::::::::::::::::::::

function InitializePage()
{
document.getElementsByTagName("DIV")[0].setAttribute("id", "Container");
Icons();
attachEventhandlers();
}

window.onload=InitializePage

// Attach event handlers to all images within container
function attachEventhandlers()
{
IMGCollection=document.getElementsByTagName("IMG");
if (IMGCollection!=null)
	{	for (l=0; l<IMGCollection.length; l++)
			{IMGCollection[l].onmouseup=onMouseUpHandler;}
	}
}

function Icons()
{
// Collect all tables
TABLECollection=Container.getElementsByTagName("TABLE");
// Loop through and insert icon in all TH elements
if (TABLECollection!=null)
	{for (i=0; i<TABLECollection.length; i++)
		{	
			// Grab second TH element and second TR element		
			THCollection=TABLECollection[i].getElementsByTagName("TH");
			THElement=THCollection[1];

			TRCollection=TABLECollection[i].getElementsByTagName("TR");
			TRElement=TRCollection[1];	
					
			if(firstLoad==0)
				{// Yes: FIRST LOAD OF PAGE -- insert image
					if(GlobalECState==0)
						{// Global ECState is COLLAPSED (+)	(0)	
						THElement.innerHTML="<IMG border='0' src='+.gif' width='13' height='13'>"				
						
						IMGCollection=TABLECollection[i].getElementsByTagName("IMG");
						IMGCollection[0].setAttribute("ECState",0);
						
						TRElement.style.display='none';
						}
					else
						{// Global ECState is EXPANDED (-) (1)
						THElement.innerHTML="<IMG border='0' src='-.gif' width='13' height='13'>"
						
						IMGCollection=TABLECollection[i].getElementsByTagName("IMG");
						IMGCollection[0].setAttribute("ECState",1);
						
						TRElement.style.display='inline';				
						}
				}
			else
				{// No: FIRST LOAD OF PAGE - change image
				 //	Grab ECState of image and expand or collapse branch

				IMGCollection=TABLECollection[i].getElementsByTagName("IMG");
				State=IMGCollection[0].getAttribute("ECState");
				if(State==0)
					{// ECState is COLLAPSED (+) (0)
					// Change Image
					IMGCollection[0].setAttribute("src","+.gif");
					// Hide DIV
					TRElement.style.display='none';
					}
				else
					{// ECState is EXPANDED (-) (1)
					// Change Image
					IMGCollection[0].setAttribute("src","-.gif");
					// Show DIV
					TRElement.style.display='inline';
					}
				}
			}
	}
if(firstLoad==0){firstLoad=1;}
}

// ::::::::::::::::::::::::
// :::: Event Handlers ::::
// ::::::::::::::::::::::::

function onMouseUpHandler()
{
if(window.event.button==1)
	{
	// Grab Target Element
	    imgObj=window.event.srcElement;
	    
	// Toggle ECState
	State=imgObj.getAttribute("ECState");
	if(State==0)
		{imgObj.setAttribute("ECState",1);}
	else{imgObj.setAttribute("ECState",0);}
	}
Icons();
}
