/* buildTOC.js - build a table of contents using DOM methods
   Peter Johnson - peter.johnson@southcentral.edu
   Written:  11/01/08
*/

function buildTOC( )
{
    // Check that the browser supports the methods used, if not, simply leave
	if (!document.getElementById || !document.createElement || !document.appendChild) return false;
	
	//alert("inside buildTOC( )");
	document.write("<div id='toc'><b>Table of Contents</b><hr />")
	// get all the h header elements from the web page
	var headerList = document.getElementsByTagName("h2");
	
	// loop through and display each one
	for (var x=0; x<headerList.length; x++)
	{
		// display the text for each h3 header
		//document.write(headerList[x].textContent + "<br />")
		// add a unique id to each link, first three characters + an id number
		// these will be used as anchors
		var thisIDString = headerList[x].textContent.substring(0,3) + x;
		headerList[x].setAttribute("id", thisIDString.toLowerCase());
		// create a link to each of these anchors:  <a href='idNumber'>contents of h2 tag</a>
		document.write("<a href='#");
		// the id string
		document.write(thisIDString.toLowerCase());
		// close the opening a tag
		document.write("'>");
		// display hyperlink text on the screen
		document.write(headerList[x].textContent);
		// close tag </a>
		document.write("</a><br />");	
	}
	document.write("\n<!-- End of ID=Table of Contents -->\n</div>");	
}
