// Title: tree.js
// Last update: 29 Oct 09

var start=0;
var stop=(rows-1);
var depth=1;
var next1 = "0"
var next2 = "0"

var indent = new Array(5)
	{
	indent[0] = '<img src="' + myURL + subWeb +'/images/tree/indent0.png" >'		
	indent[1] = '<img src="' + myURL + subWeb +'/images/tree/indent1.png" >'
	indent[2] = '<img src="' + myURL + subWeb +'/images/tree/indent2.png" >'
	indent[3] = '<img src="' + myURL + subWeb +'/images/tree/indent3.png" >'
	indent[4] = '<img src="' + myURL + subWeb +'/images/tree/indent4.png" >'
	indent[5] = '<img src="' + myURL + subWeb +'/images/tree/indent5.png" >'
	indent[6] = '<img src="' + myURL + subWeb +'/images/tree/indent6.png" >'
	indent[7] = '<img src="' + myURL + subWeb +'/images/tree/indent7.png" >'
	indent[8] = '<img src="' + myURL + subWeb +'/images/tree/indent8.png" >'
	indent[9] = '<img src="' + myURL + subWeb +'/images/tree/indent9.png" >'
	}
		
function expand(row)
	{	
	myTree[row][oState]= "Y";
	if ( myTree[row][oChild] != "D")
		{	
		myTree[row][oChildren]= "Y";
		}
	else
		{
		myTree[row][oChildren]= "N";
		}
	
	depth = parseInt(myTree[row][oDepth]);

  	for (i = row+1; i < rows; i++)	
		{	
		if ( parseInt(myTree[i][oDepth]) <= depth )
			{
			break;
			}
		else if ( parseInt(myTree[i][oDepth]) == depth+1 )
			{
			myTree[i][oState]= "Y";
			myTree[i][oChildren]= "N";
			}
		else
			{
			myTree[i][oState]= "N";
			myTree[i][oChildren]= "N";
			}		
		}
	treeDisplay();	
	};

function contract(row)
	{
	myTree[row][oChildren]= "N";
	depth = parseInt(myTree[row][oDepth]);
	
  	for (i = row+1; i < rows; i++)	
		{	
		if ( parseInt(myTree[i][oDepth]) <= depth )
			{break;}
		myTree[i][oState]= "N";
		myTree[i][oChildren]= "N";
		}
	treeDisplay();
	};
	
function openAll()
	{
	start = 0;
	stop = rows -1; 

  	for (i = start; i < stop; i++)	
		{		
		myTree[i][oState]= "Y";
		if ( myTree[i][oChild] != "D")
			{	
			myTree[i][oChildren]= "Y";
			}
		else
			{
			myTree[i][oChildren]= "N";
			}
		}
	
	next1 = "0";
	next2 = tops-1;

	treeDisplay();
	}

function closeAll()
	{
	start = 0;
	stop = rows -1; 
	
  	for (i = start; i < stop; i++)	
		{	
		if ( parseInt(myTree[i][oDepth]) > 0 )
			{myTree[i][oState]= "N";}
			{myTree[i][oChildren]= "N";}
		}
	next1 = "0"
	next2 = "2"
	treeDisplay();
	}
				
function openTo()
	{
	depth = (document.openLevel.level.value-1);

	next2 = "0"	

	if ( depth >= 9 )
		return;
		
	next2 = document.openLevel.level.value

  	for (i = 0; i < rows; i++)	
		{
		if ( parseInt(myTree[i][oDepth]) < depth )
			{
			myTree[i][oState]= "Y";
			if ( myTree[i][oChild] != "D")
				{	
				myTree[i][oChildren]= "Y";
				}
			else
				{
				myTree[i][oChildren]= "N";
				}	
			}
		else if ( parseInt(myTree[i][oDepth]) == depth )
			{
			myTree[i][oState]= "Y";
			myTree[i][oChildren]= "N";		
			}
		else
			{
			myTree[i][oState]= "N";
			}
		}
	treeDisplay();
	}
	
function section()
	{
	section0 = parseInt(document.topSection.x.value);
	
		next1 = "0"
	
	if ( section0 >= tops-1 )
		return;
	
	if ( section0 < 1 )
		{
		start=0;
		next=0;
		stop=(rows-1);
		}
	else
		{	
		next1 = document.topSection.x.value;
		start = parseInt(topSection[section0-1]);
		stop = parseInt(topSection[section0]);
		}
	
	treeDisplay();
	}

function treeDisplay()
	{
	var treeDiv=document.getElementById('myTree')
	
	saveState();
		
	str = '<p>'
	
	str += '<input type="image" src="' + myURL + subWeb +'/images/tree/plus.bmp" onClick=openAll()>'
	str += '<a href="javascript:openAll()"> Open/All</a> <br>'
		
	str += '<input type="image" src="' + myURL + subWeb +'/images/tree/minus.bmp" onClick=closeAll();>'
	str += '<a href="javascript:closeAll()"> Close/All</a> <br><br>'
	
	str += '<form name="topSection">'
	str += '<input TYPE="radio" NAME="test" VALUE="test" onClick="section();">'
	str += '<input TYPE="TEXT" NAME="x" VALUE="' + next1  + '" maxlength="1" SIZE="1" class=mybox> Section</form>'

	str += '<form name="openLevel">'
	str += '<input TYPE="radio" NAME="Open" VALUE="Open" onClick="openTo();">'
	str += '<input TYPE="TEXT" NAME="level" VALUE="' + next2  + '" maxlength="1" SIZE="1" class=mybox> Depth</form><br>'

	for (i = parseFloat(start); i != parseFloat(stop); i++)	
		{
		if ( myTree[i][oState]== "Y" )
			{
			depth = myTree[i][oDepth];	
			if ( depth != 0 )
				{
				str += '<img src="' + myURL + subWeb +'/images/tree/leaf.png">'
				str += indent[depth]
				}
			
			if (( myTree[i][oType]!= "L" ) && ( myTree[i][oType]!= "E" ))
				{
				if ( myTree[i][oChildren]== "Y" )
					{
					str += '<input name="test" type=image src="' + myURL 
					str += subWeb +'/images/tree/minus.bmp" onClick=contract('+i+');>'
					}	
				else
					{
					str += '<input name="test" type=image src="' + myURL
					str += subWeb +'/images/tree/plus.bmp" onClick=expand('+i+');>'
					}	
				}
			else
				{
				str += indent[2]
				}
			
			str += '<a href="' + myURL + "website2/html/" + myTree[i][oTag] + '.' + myTree[i][oLabel] + '.html">';
			
			
			if ( depth == 0)
				{
				str += '<b><u>' + myTree[i][oLabel] + '</u></b></a> <br>';		
				}
			else if (depth == 1)
				{
				if ( myTree[i][oChildren]== "Y" )
					{
					str += '<b>' + myTree[i][oLabel] + '</b></a> <br>';
					}
				else
					{
					str += myTree[i][oLabel] + '</a> <br>';
					}
				}
			else if (depth == 2)
				{
				if ( myTree[i][oChildren]== "Y" )
					{
					str += '<u><i>' + myTree[i][oLabel] + '</i></u></a> <br>';
					}
				else
					{
					str += myTree[i][oLabel] + '</a> <br>';
					}
				}
			else
				{
				str += myTree[i][oLabel] + '</a> <br>';
				}
			}
		}
	str += '</p>';
	treeDiv.innerHTML = str;	
	} 

//----------------------------------------------------------
// entry function
// ---------------------------------------------------------
function treeMenu()
	{
	if ((stateData = readCookie("treeCookie")) != null)
		{
		loadState();
		}
	treeDisplay();
	}
