
var counter=0;

function generate_TOC(arrTocs)
{
  if(arrTocs.length>0)
  {
	  for(var j = 0; j<arrTocs.length;j++)
	  {
		var parent = document.getElementById(arrTocs[j].tocid);
		var shownumber = arrTocs[j].shownumbering;
		var showheader = arrTocs[j].showheader;
		var showanchor = arrTocs[j].showanchor;
		
		if(typeof parent != "undefined")
		{	
			if(typeof shownumber != "undefined" && shownumber == "yes")
			{
			   generateNumericTOC(parent,showheader);
			}
			else
			{
				generateSimpleTOC(parent,showheader);
				if(typeof showanchor != "undefined" && showanchor == "yes")
				{
					
				   generateAnchorTOC(parent,showheader);
				}
			}
			
		}
	  }
  }
}

function H_getText(el) {
  var text = "";
  for (var i = el.firstChild; i != null; i = i.nextSibling) {
    if (i.nodeType == 3 /* Node.TEXT_NODE, IE doesn't speak constants */)
      text += i.data;
    else if (i.firstChild != null)
      text += H_getText(i);
  }
  return text;
}
function TOC_EL(el, text, level) {
  this.element = el;
  this.text = text;
  this.level = level;
}
function getHeadlines(el) {
	
  var l = new Array;
  var rx = /[hH]([1-6])/;
  // internal recursive function that scans the DOM tree
  var rec = function (el) {
    for (var i = el.firstChild; i != null; i = i.nextSibling) {
      if (i.nodeType == 1 /* Node.ELEMENT_NODE */) {
        if (rx.exec(i.tagName))
          l[l.length] = new TOC_EL(i, H_getText(i), parseInt(RegExp.$1));
        rec(i);
      }
    }
  }
  rec(el);
  return l;
}
function getAnchors(el) {
	
  var l = new Array;
  var rx = /[aA]/;
  // internal recursive function that scans the DOM tree
  var rec = function (el) {
    for (var i = el.firstChild; i != null; i = i.nextSibling) {
	   if(typeof i.tagName != "undefined")
	   {
		  if ((i.nodeType == 1) && (i.tagName == "A")) 
		  {
			  if(typeof i.name !="undefined" && i.name != "")
			  {
				l[l.length] = new TOC_EL(i, i.name, 1);
			  }
		  }
		  rec(i);
		}
    }
  }
  rec(el);
  return l;
}

function generateSimpleTOC(parent,showheader) {
 
	if(typeof parent != "undefined")
	{
	  
	  var hs = getHeadlines(document.getElementsByTagName("body")[0]);
	  var outerDiv = document.createElement("div");
	  outerDiv.className = "showtable";
	  outerDiv.id = parent.id+"TopTable";
	  for (var i = 0; i < hs.length; ++i) {
		if(showheader == "yes")
		{
			if(i == 0)
			{
				var divTitle = document.createElement("div");
				var hr = document.createElement("hr");
				var a =  document.createElement("a");
				a.id=parent.id+"ShowHide";
				a.setAttribute("href","javascript:hideTable('"+a.id+"','"+outerDiv.id+"')");
		   		a.innerHTML = "Hide";
				divTitle.appendChild(document.createTextNode("Contents["));
				divTitle.appendChild(a);
				divTitle.appendChild(document.createTextNode("]"));
				
				divTitle.style.textAlign="left";
				divTitle.style.color="#003366";
				divTitle.className = "tocheader";
				parent.appendChild(divTitle);
				parent.appendChild(hr);
			}
		}
		var hi = hs[i];
		var d = document.createElement("div");
		if (hi.element.id == "")
		  hi.element.id = "gen" + i;
		var a = document.createElement("a");
		a.href = "#" + hi.element.id;
		a.appendChild(document.createTextNode(hi.text));
		d.appendChild(a);
		d.className = "level" + hi.level;
		outerDiv.appendChild(d);
	  }
	   parent.appendChild(outerDiv);
	 
	}
		
	
}

function generateAnchorTOC(parent,showheader) {
	if(typeof parent != "undefined")
	{
	  var hs = getAnchors(document.getElementsByTagName("body")[0]);
	  for (var i = 0; i < hs.length; ++i) {
		var hi = hs[i];
		var d = document.createElement("div");
		var a = document.createElement("a");
		a.href = "#" + hi.text;
		a.appendChild(document.createTextNode(hi.text));
		d.appendChild(a);
		d.className = "level" + hi.level;
		parent.appendChild(d);
	  }
	}
		
}

function generateNumericTOC(parent,showheader) 
{
    // Recursively traverse the body of the document, looking for sections
    // sections marked with <h1>, <h2>, ... tags, and use them to create 
    // the TOC by adding rows to the table
	if(typeof parent != "undefined")
	{
		// Initialize an array that keeps track of section numbers
		var sectionNumbers = [0,0,0,0,0,0];
		var bodyObj =document.body; 
		var outerDiv = document.createElement("div");
		outerDiv.className = "showtable";
		outerDiv.id = parent.id+"TopTable";
		counter = 0;
		addSections(bodyObj,parent,sectionNumbers,showheader,outerDiv);
		parent.appendChild(outerDiv);
	}
	 
 
    
}

// Finally, insert the TOC into the document by replacing the node
// specified by the replace argument with the TOC subtree
//replace.parentNode.replaceChild(toc, replace);

// This method recursively traverses the tree rooted at Node n, looking
// looking for <h1> through <h6> tags, and uses the content of these tags
// to build the table of contents by adding rows to the HTML table specified
// by the toc argument. It uses the sectionNumbers array to keep track of
// the current section number.
// This function is defined inside of maketoc(  ) so that it is not
// visible from the outside. maketoc(  ) is the only function exported
// by this JavaScript module.
function addSections(n, toc, sectionNumbers,showheader,outerDiv) {
	 
	
	// Loop through all the children of n
	for(var m = n.firstChild; m != null; m = m.nextSibling) {
		// Check whether m is a heading element. It would be nice if we
		// could just use (m instanceof HTMLHeadingElement), but this is
		// not required by the specification and it does not work in IE.
		// Therefore, we must check the tagname to see if it is H1-H6.
		
		if ((m.nodeType == 1) &&  /* Node.ELEMENT_NODE */ 
		
			(m.tagName.length == 2) && (m.tagName.charAt(0) == "H")) {
			// Figure out what level heading it is
		
			var level = parseInt(m.tagName.charAt(1));

			if (!isNaN(level) && (level >= 1) && (level <= 6)) {
				
				if(showheader == "yes")
				{
					if(counter == 0)
					{
						
						var divTitle = document.createElement("div");
						var hr = document.createElement("hr");
						var a =  document.createElement("a");
						a.id=toc.id+"ShowHide";
						a.setAttribute("href","javascript:hideTable('"+a.id+"','"+outerDiv.id+"')");
						a.innerHTML = "Hide";
						divTitle.appendChild(document.createTextNode("Contents["));
						divTitle.appendChild(a);
						divTitle.appendChild(document.createTextNode("]"));
						divTitle.style.textAlign="left";
						divTitle.style.color="#003366";
						divTitle.className = "tocheader";
						toc.appendChild(divTitle);
						toc.appendChild(hr);

						
					}
				}
					counter=counter+1;
				// Increment the section number for this heading level
				sectionNumbers[level-1]++;
				// And reset all lower heading-level numbers to zero
				for(var i = level; i < 6; i++) sectionNumbers[i] = 0;
				// Now combine section numbers for all heading levels
				// to produce a section number like "2.3.1"
				var sectionNumber = "";
				for(var i = 0; i < level; i++) {
					sectionNumber += sectionNumbers[i];
					if (i < level-1) sectionNumber += ".";
				}

				// Create an anchor to mark the beginning of this section
				// This will be the target of a link we add to the TOC
				//var anchor = document.createElement("a");
				//anchor.setAttribute("name", "SECT"+sectionNumber);

				// Create a link back to the TOC and make it a
				// child of the anchor
				//var backlink = document.createElement("a");
				//backlink.setAttribute("href", "#TOC");
				//backlink.appendChild(document.createTextNode("Contents"));
				//anchor.appendChild(backlink);

				// Insert the anchor into the document right before the
				// section header
				//n.insertBefore(anchor, m);

				// Now create a link to this section. It will be added
				// to the TOC below.
				if (m.id == "")
						m.id = "gen"+counter;
				var link = document.createElement("a");
			
				link.setAttribute("href", "#" +m.id);
				// Get the heading text using a function defined below
				var sectionTitle = getTextContent(m);
				// Use the heading text as the content of the link
				link.appendChild(document.createTextNode(sectionNumber+" "+sectionTitle));

				// Create a new row for the TOC
				var div = document.createElement("div");
				
				//col1.appendChild(document.createTextNode(sectionNumber));
				// Put a link to the section in the second column
				//col2.appendChild(link);
				// Add the columns to the row, and the row to the table
				//div.appendChild(document.createTextNode(sectionNumber+" "));
				div.appendChild(link);
				div.className = "level" + level;
				outerDiv.appendChild(div);

				// Modify the section header element itself to add
				// the section number as part of the section title
				//m.insertBefore(document.createTextNode(sectionNumber+": "),m.firstChild);
			}
		}
		else {  // Otherwise, this is not a heading element, so recurse
			addSections(m, toc, sectionNumbers,showheader,outerDiv);
		}
	}
}

// This utility function traverses Node n, returning the content of
// all Text nodes found and discarding any HTML tags. This is also
// defined as a nested function, so it is private to this module.
function getTextContent(n) {
	var s = '';
	var children = n.childNodes;
	for(var i = 0; i < children.length; i++) {
		var child = children[i];
		if (child.nodeType == 3 /*Node.TEXT_NODE*/) s += child.data;
		else s += getTextContent(child);
	}
	return s;
}

function hideTable(aId,tableId)
{
	var a = document.getElementById(aId);
	var table = document.getElementById(tableId);
	table.className="hidetable";
	a.setAttribute("href","javascript:showTable('"+aId+"','"+tableId+"')");
	a.innerHTML = "Show";
}

function showTable(aId,tableId)
{
	var a = document.getElementById(aId);
	var table = document.getElementById(tableId);
	table.className="showtable";
	a.setAttribute("href","javascript:hideTable('"+aId+"','"+tableId+"')");
	a.innerHTML = "Hide";
}
