var theImagePath = "/_root/images/icons/";
var theImagePlus
var theImageMinus

var nodeArray = new Array();
var nodeEOF;

var nodeExpandedArray = new Array();
var nodeExpandedEOF;

var nodePathArray = new Array();
var nodePathEOF;


//*******************************************************
//     Get image function
//*******************************************************

function getImgUrl (theBranchId){
   	theImgId = document.getElementById("img" + theBranchId );
	if (theImgId) {
		theImgId = theImgId.src
		theImgId = theImgId.substring( theImgId.lastIndexOf( '/' ) + 1, theImgId.lastIndexOf( '_' ) );

		theImagePlus = theImagePath + theImgId + "_pil1.gif";
		theImageMinus = theImagePath + theImgId + "_pil2.gif";

	}
}

//*******************************************************
//     Image functions
//*******************************************************
function closeBranchImg (theBranchId){
	getImgUrl (theBranchId)
   	theImage = document.getElementById("img" + theBranchId);
	if (theImage) {
		theImage.src = theImagePlus;
	}
}

function openBranchImg (theBranchId){
	getImgUrl (theBranchId)
   	theImage = document.getElementById("img" + theBranchId);
	if (theImage) {
		theImage.src = theImageMinus;
	}
}

function toggleBranchImg (theBranchId){
	getImgUrl (theBranchId)
   	theImage = document.getElementById("img" + theBranchId);
	
	imgUrl = theImage.src;
	imgUrl = imgUrl.substring( imgUrl.lastIndexOf( '/' ) + 1 );
	imgUrl = theImagePath + imgUrl
		
	if (imgUrl == theImagePlus) {
		openBranchImg (theBranchId);
	}
	else {
		closeBranchImg (theBranchId);
	}
}

//*******************************************************
//     Table functions
//*******************************************************


function closeBranchTbl (theBranchId){
	theTable = document.getElementById("tbl" + theBranchId);
	if (theTable) {
		theTable.style.display = 'none';
	}
}

function openBranchTbl (theBranchId){
	theTable = document.getElementById("tbl" + theBranchId);
	if (theTable) {
		theTable.style.display = 'block';
	}
}

function toggleBranchTbl (theBranchId){
	theTable = document.getElementById("tbl" + theBranchId);
	
	if (theTable.style.display == 'none') {
		openBranchTbl (theBranchId);
	}
	else {
		closeBranchTbl (theBranchId);
	}
}

//*******************************************************
//     General functions
//*******************************************************

function toggleBranch (theBranchId){
	toggleBranchImg (theBranchId)
	toggleBranchTbl (theBranchId)
}

function collapseAll ()
{	
	for (pointer = 0 ; pointer <= nodeEOF ; pointer++)	
	{
		theBranchId = nodeArray[pointer].id;
		theBranchId = theBranchId.substring( theBranchId.lastIndexOf( 'img' ) + 3 );
		closeBranchImg (theBranchId);
		closeBranchTbl (theBranchId);
	}
}		

function expandAll ()
{	
	for (pointer = 0 ; pointer <= nodeEOF ; pointer++)	
	{
		theBranchId = nodeArray[pointer].id;
		theBranchId = theBranchId.substring( theBranchId.lastIndexOf( 'img' ) + 3 );
		openBranchImg (theBranchId);
		openBranchTbl (theBranchId);
	}
}		

function storeExpanded () {
	
//alert("storing");

	i = 0
	nodeExpandedArray.length = 0
	
	//alert(nodeArray[0].src);
	for (pointer = 0 ; pointer <= nodeEOF ; pointer++)	
	{
		
		theBranchId = nodeArray[pointer].id;
		theBranchId = theBranchId.substring( theBranchId.lastIndexOf( 'img' ) + 3 );
		getImgUrl (theBranchId);
		//alert(theBranchId);
		
		imgUrl = nodeArray[pointer].src;
		imgUrl = imgUrl.substring( imgUrl.lastIndexOf( '/' ) + 1 );
		imgUrl = theImagePath + imgUrl
		//alert(imgUrl);
		
		if (imgUrl == theImageMinus){
			nodeExpandedArray[i] = nodeArray[pointer].id;
			//alert(nodeExpandedArray[i]);
			i++;
		}
	}
	//alert(nodeExpandedArray.length)
	
	setCookie("treeExpandedNodes", nodeExpandedArray);
	
	//alert(document.cookie);

}

function setExpanded () {

//alert("setting");
	nodeExpandedArray.length = 0;
	if (getCookie("treeExpandedNodes")) {
		//theCookieString = readCookie("treeExpandedNodes");
		theCookieString = getCookie("treeExpandedNodes");
		//alert(theCookieString);
	
		nodeExpandedArray = theCookieString.split(',');
		//alert(nodeExpandedArray[0]);
		
		nodeExpandedEOF = (nodeExpandedArray.length - 1);
	
		for (pointer = 0 ; pointer <= nodeExpandedEOF ; pointer++) {
			theBranchId = nodeExpandedArray[pointer];
			theBranchId = theBranchId.substring( theBranchId.lastIndexOf( 'img' ) + 3 );
			openBranchImg (theBranchId);
			openBranchTbl (theBranchId);
		}
	}
}

function setPath () {

	collapseAll();
	
	nodePathArray.length = 0;
	
		thePathString = "50923,39673,";
		thePathString = thePathString.substring(0, thePathString.lastIndexOf( ',' ));

		//alert(thePathString);
	
		nodePathArray = thePathString.split(',');
		//alert(nodePathArray[0]);
		
		nodePathEOF = (nodePathArray.length - 1);
	
		for (pointer = 1 ; pointer <= nodePathEOF ; pointer++) {
			theBranchId = nodePathArray[pointer];
			openBranchImg (theBranchId);
			openBranchTbl (theBranchId);
		}
	
}

function setCookie (name, value) {
    document.cookie = name + "=" + escape (value) + "; path=/";
    }                                                  

function getCookie (name) {
    var dcookie = document.cookie; 
    var cname = name + "=";
    var clen = dcookie.length;
    var cbegin = 0;
        while (cbegin < clen) {
        var vbegin = cbegin + cname.length;
            if (dcookie.substring(cbegin, vbegin) == cname) { 
            var vend = dcookie.indexOf (";", vbegin);
                if (vend == -1) vend = clen;
            return unescape(dcookie.substring(vbegin, vend));
            }
        cbegin = dcookie.indexOf(" ", cbegin) + 1;
            if (cbegin == 0) break;
        }
    return '';
    }

function delCookie (name) {
    var expireNow = new Date();
    document.cookie = name + "=" +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT" +  "; path=/";
    }

//*******************************************************
//     Populate nodeArray with all tree nodes
//*******************************************************

function createNodeArray() {

//alert("creating");

	nodeArrayX  = new Array();
	i = 0

	nodeArrayX = document.getElementsByTagName('img');
	
	nodeXEOF = (nodeArrayX.length - 1);

	for (pointer = 0 ; pointer <= nodeXEOF ; pointer++)	
	{
		imgID = nodeArrayX[pointer].id;

		if (imgID.indexOf('img') == 0){
			nodeArray[i] = nodeArrayX[pointer];
			//alert(nodeArray[i].id);
			i++;
		}
	}

nodeEOF = (nodeArray.length - 1);
//alert(nodeArray[0].src);
//alert(nodeEOF);
}