var Ccounter = 0;
var Mcounter = 1;
var firstNode = 0;
var lastNode = 0;
var emptyCat = new Array(); var ec = 1; //track empty categories
var currCat = 1, newCat;

function dopreload()
{
	var the_images = new Array('ftv2blank.gif','ftv2lastnode.gif','ftv2mlastnode.gif','ftv2mnode.gif','ftv2node.gif','ftv2plastnode.gif','ftv2pnode.gif','ftv2vertline.gif','ftv2pfirstnode.gif','ftv2mfirstnode.gif');
	preloadImages(the_images);
}

function preloadImages(the_images_array) 
{
	for(loop = 0; loop < the_images_array.length; loop++)
	{
   		var an_image = new Image();
		an_image.src = the_images_array[loop];
	}
}

function openTree(caption)
{
	document.write('<font face="Tahoma,Arial,Helvetica,Sans Serif" size="2">');
	document.write('<div style="margin-left:0; display:;">');
	document.write('<table cellpadding=0 cellspacing=0><tr>');
	document.write('<td valign=top><img src="ftv2mfirstnode.gif" ID=MI-1 onClick="expandCollapseClick();">');
	document.write('<img ID=FI-1 src="foldere2.gif"></td>');
	document.write('<td valign=top><span ID=MT-1><font size="-1"><b>');
	document.write(caption);
	document.write('</a></b></font></span></td>');
	document.writeln('</tr></table>');
}

function closeTree()
{
	document.writeln ('</DIV>\n</FONT>');
}

// This function handles to onClick event for expanding/closing
// nodes of the tree.
function expandCollapseClick ()
{
	var parentID;
	var child;
	var parentImage;
	var otherImage;

	parentID = window.event.srcElement.id;
	var imageNumber = parentID.substr(3);

	// used to detect which catagory is being clicked.
	if(imageNumber != '1')
		if(currCat != '1' && imageNumber != currCat){
			expandMenu("MI-" + currCat);
	}

	if(parentID.charAt (0) == 'M' && !emptyCat[parentID.substr(3)])
	{
		child = document.all ('C-' + imageNumber);
		parentImage = document.all ('MI-' + imageNumber);
		if (child.style.display == 'none') // hidden
		{
			otherImage = document.all ('FI-' + imageNumber);
			otherImage.src = 'foldere2.gif';	
			child.style.display = '';

			// used only if tree root is not clicked.
			if(imageNumber != '1') currCat = imageNumber;

			/* use last minus sign when appropiate - determine which type of node */
			
			switch(parentID) {
				case 'MI-1' :
					parentImage.src = 'ftv2mfirstnode.gif';
					break;
				case ('MI-' + Mcounter) :
					parentImage.src = 'ftv2mlastnode.gif';			
					break;
				default : 
					parentImage.src = 'ftv2mnode.gif';
					break;
			}
				
		}
		else
		{
			otherImage = document.all ('FI-' + imageNumber);
			otherImage.src = 'folderc2.gif';

			// used only if tree root is not clicked.
			if(imageNumber != '1') currCat = '1';
		
			child.style.display = 'none';
			switch(parentID) {
				case 'MI-1' :
					parentImage.src = 'ftv2pfirstnode.gif';
					break;
				case ('MI-' + Mcounter) :
					parentImage.src = 'ftv2plastnode.gif';			
					break;
				default : 
					parentImage.src = 'ftv2pnode.gif';
					break;
			}
		}
	}
}

// This function gets called from a script tag at the bottom of
// your html to expand the menu upon page opening.
function expandMenu(cat)
{
	var parentID;
	var child;
	var parentImage;
	var otherImage;

	// Get First level desired to expand. You want to specify
	// Top level (MI-1) when calling this function.
	parentID = cat.substr(0);

	if (parentID.charAt(0) == 'M' && !emptyCat[parentID.substr(3)])
	{
		child = document.all ('C' + parentID.substr (2));
		parentImage = document.all ('MI' + parentID.substr (2));
		if (child.style.display == 'none') // hidden
		{
			otherImage = document.all ('FI' + parentID.substr (2));
			otherImage.src = 'foldere2.gif';
			child.style.display = '';
			/* use last minus sign when appropiate - determine which type of node */			
			switch(parentID) {
				case 'MI-1' :
					parentImage.src = 'ftv2mfirstnode.gif';
					break;
				case ('MI-' + Mcounter) :
					parentImage.src = 'ftv2mlastnode.gif';			
					break;
				default : 
					parentImage.src = 'ftv2mnode.gif';
					break;
			}
		}
		else
		{
			otherImage = document.all ('FI' + parentID.substr (2));
			otherImage.src = 'folderc2.gif';
			child.style.display = 'none';
			switch(parentID) {
				case 'MI-1' :
					parentImage.src = 'ftv2pfirstnode.gif';
					break;
				case ('MI-' + Mcounter) :
					parentImage.src = 'ftv2plastnode.gif';			
					break;
				default : 
					parentImage.src = 'ftv2pnode.gif';
					break;
			}
		}
	}
}

// This function creates the link.
function makeLink (category, urlDir, target)
{
	var categoryToDisplay, targetstr;

	if (category.length > 12)
	{
		categoryToDisplay = category.substring (0, 50);
	}
	else
	{
		categoryToDisplay = category;
	}

	HTMLstr='<font size="-1"><A HREF="' + urlDir + '" TITLE="Open &#145;' + category + '&#146;." class="Corp3"';
	targetstr = target + ""; // Explicitely change target to string

	if (targetstr != "") // Test to see if target is empty, if not empty add the target attribute to href tag
	{
		HTMLstr += 'target="' + target +'">' + categoryToDisplay + '</A></font>';
	}
	else // else leave tag alone.
	{
		HTMLstr += '>' + categoryToDisplay + '</A></font>';
	}
	return HTMLstr;
}

// This function creates the end nodes under any given category.
function makeNode (name, URLpath, target)
{
	++lastNode; // increment to track last node of node division.
	document.writeln ('<table CELLPADDING=0 CELLSPACING=0><tr><td valign=top><img ID=img1-' + lastNode + ' src="ftv2vertline.gif"><img ID=img2-' + lastNode + ' src="ftv2node.gif"><img src="doc.gif"></td><td width="100" valign=middle>' + makeLink(name, URLpath, target) + '</td></tr></table>');
}

// This function creates the category.
function makeCategory (name, URLpath, target)
{
	Mcounter++;
	document.writeln ('<table CELLPADDING=0 CELLSPACING=0><tr><td valign=top><img src="ftv2pnode.gif" ID=MI-' + Mcounter + '  onClick=expandCollapseClick()><img ID=FI-' + Mcounter + ' src="folderc2.gif"></td><td valign=top><SPAN ID=MT-' + Mcounter + '>' + makeLink (name, URLpath, target) + '</SPAN></td></tr></table>');
}

// This function creates the opening div tag for hiding and
// showing with the expandCollapseClick function.
function openDiv ()
{
	emptyCat[ec] = false;
	Ccounter++;
	//Changed margin from 15 to 24 for new images
	document.writeln ('<DIV ID=C-' + Ccounter + ' STYLE="margin-left:24; display:None;">');
}
function openNodeDiv ()
{
	firstNode = lastNode + 1; // remember first node of node division.
	++Ccounter;
	document.writeln ('<DIV ID=C-' + Ccounter + ' STYLE="margin-left:0; display:None;">');
}

// This function closes the div tag.
function closeDiv ()
{
	var image;

	document.writeln ('</DIV>');

	// Choose correct image for last catagory
	image = document.all('MI-' + Mcounter);

	// if last catagory is empty no plus sign else plus sign
	if(emptyCat[ec])
		image.src = 'ftv2lastnode.gif';
	else
		image.src = 'ftv2plastnode.gif';

	// blank last catagory's node's first image(vertical line).
	for(i = firstNode; i <= lastNode; ++i)
	{
		image = document.all('img1-' + i);
		//Added width and height for proper alignment - should be OK  
		image.width = '24';
		image.height = '0';
		image.src = 'ftv2blank.gif';
	}
}
function closeNodeDiv ()
{
	var image;

	document.writeln('</DIV>');
	
	// if catagory empty(no nodes) then no plus sign else set last node image
	if(firstNode == lastNode + 1){
		emptyCat[++ec] = true;
		image = document.all('MI-' + ec);
		image.src = 'ftv2node.gif';
	}
	else{
		emptyCat[++ec] = false;
		image = document.all('img2-' + lastNode);
		image.src = 'ftv2lastnode.gif';
	}
}

// This command calls the function created in the head of your
// document. If you do not want users to see your directory
// structure, you will need to put that function here. I moved
// it outside this file and into the head of the html file
// to manipulate the tree using ASP.
// showDirectoryTree();
