// JavaScript Document

//this function grabs all h tags and generates a linked table of contents into a specified div tag
//this version iterates through a variable to create anchor tags, except in the case of "Annexes," where the name attribute is the letter of the Annex

function toc() 

{
	//establish variables
	var toc_text = ' ';
	var newtext =' ';
	var h2count = 0;
	var h3count = 0;
	var h4count = 0;
	var h5count = 0;
	var anchcount = 1;
	var arr;	
		
	//get all header tags	
	var h2s = document.getElementsByTagName('h2');
	var h3s = document.getElementsByTagName('h3');
	var h4s = document.getElementsByTagName('h4');
	var h5s = document.getElementsByTagName('h5');
	
	
	//create array of all nodes in main text area
	arr=document.getElementById('iddrs_main_content').childNodes;
	
	//step through the nodes and return the text in all h tags
	for (i=0;i<arr.length;i++) {

		if (arr[i].nodeType == 1) {    //check only element nodes

			switch (arr[i].nodeName) {
		
				case "H2" :
				content = h2s[h2count].innerHTML;
		
				if (content.substring( 0 , 5) == "Annex") {				//check if it's an Annex
					var anch = content.charAt(6).toLowerCase(); 	
					
					if (content.charAt(8).match(/\d+/g)) {					//if Annex is subdivided, grab the digit
						anch = anch + content.charAt(8);
						}
			
					newtext = '<p><a href="#' + anch + '"><strong>' + content + '</strong></a></p>';
					h2s[h2count].innerHTML = content.anchor(anch);			//insert content back into h tag, adding anchor
					}
					
				else {
					newtext = '<p><a href="#' + anchcount + '"><strong>' + content + '</strong></a></p>';
					h2s[h2count].innerHTML = content.anchor(anchcount);		//insert content back into h tag, adding anchor
					}
				
				toc_text = toc_text + newtext;  						 	//add text to current toc text
							
				
				h2count++;
				anchcount++;
				break;
			
			
			
				case "H3" :
				content = h3s[h3count].innerHTML;
				
				testsample = content.substring( 0 , 3);					//test to see if first character is a digit (if so, we'll make it bold)
				
				test = testsample.substr( 0 , 1).match(/\d+/g);			
				
				test2 = testsample.substr( 0 , 1).match(/\D+/g);		//if the first character is a letter, what is it?
				
				if (test2) {
					var test3 = testsample.substring( 1, 2 ).match(/\./g);		//is there a decimal?
					if (test3) {
						digit = testsample.substring(2);						//what is the digit after the decimal?
						}
					}
					
				if (test) {
					var test1a = testsample.substr( 1 , 1).match(/\d+/g);		//check to see if there is a second digit in the number
					var test1b = testsample.substr( 2 , 1).match(/\d+/g);
					}
					
				
				part1 = content.substring( 0 , 2);
				part2 = content.substring( 2 );
				
				if(test1a || test1b) { 
					part1 = content.substring( 0 , 3);
					part2 = content.substring( 3);
					}
				
				
				if (test) {
					newtext = '<p><a href="#' + anchcount + '"><strong>' + part1 + '</strong>' + part2 + '</p>';
					}
					
				
				else {
					if (test3) {
						var annex_letter=test2.toString().toLowerCase();
						newtext = '<p><a href="#' + annex_letter + digit + '"><strong>' + part1 + '</strong>' + part2 + '</p>';
						}
					else {
						newtext = '<p><a href="#' + anchcount + '">' + content + '</p>';
						}
					}
				
				toc_text = toc_text + newtext;
				
				if (test3) {
					h3s[h3count].innerHTML = content.anchor(annex_letter + digit);	
					}
				else {
					h3s[h3count].innerHTML = content.anchor(anchcount);			//insert content back into h tag, adding anchor
					}
				
				h3count++;
				anchcount++;
				break;		
				
				
				
				
				
				case "H4" :
				content = h4s[h4count].innerHTML;
				
				testsample = content.substring( 0 , 3);
				test=testsample.match(/\d+/g);
				
				
				if (test) {
					test1a = testsample.substring( 1 , 2).match(/\d+/g);		//check to see if there is a second digit in the number
					test1b = testsample.substring( 2 , 3).match(/\d+/g);		//check to see if there is a second digit after the .
					}
			
				part1 = content.substring( 0 , 3);
				part2 = content.substring (3);	
				
				if (test1a || test1b) {
					part1 = content.substring( 0 , 5);
					part2 = content.substring (5);	
					}
				
				
				if (test) {
					newtext = '<p class="indent"><a href="#' + anchcount + '"><strong>' + part1 + '</strong>' + part2 + '</p>';
					}
				else {
					newtext = '<p class="indent"><a href="#' + anchcount + '">' + content + '</p>';
					}
				
				toc_text = toc_text + newtext;
				h4s[h4count].innerHTML = content.anchor(anchcount);			//insert content back into h tag, adding anchor
				
				h4count++;
				anchcount++;
				break;	
			
			
			
				case "H5" :
				content = h5s[h5count].innerHTML;
				
				testsample = content.substring( 0 , 2);
				test=testsample.match(/\d+/g);
				
				if (test) {
					test1a = testsample.substring( 1 , 2).match(/\d+/g);		//check to see if there is a second digit in the number
					}
				
				
				part1 = content.substring( 0 , 6);
				part2 = content.substring (6);
				
				if (test1a) {
					part1 = content.substring( 0 , 7);
					part2 = content.substring (7);	
					}

				
				
				if (test) {
					newtext = '<p class="indent2"><a href="#' + anchcount + '"><strong>' + part1 + '</strong>' + part2 + '</p>';
					}
				else {
					newtext = '<p class="indent"><a href="#' + anchcount + '">' + content + '</p>';
					}
				
				toc_text = toc_text + newtext;
				h5s[h5count].innerHTML = content.anchor(anchcount);			//insert content back into h tag, adding anchor
				
				h5count++;
				anchcount++;
				break;	
			
			}
			
		}	
	}	

	document.getElementById('toc_list').innerHTML = toc_text;			//insert content into toc content div
	document.getElementById('toc').style.visibility = 'visible';		//make toc container div visible

}
