setOnload();

/* Check for old onLoad, and if one exists, run this after it */
function setOnload()
{	
	if(!document.getElementById){return false;}
    var oldOnload = window.onload;

    if (typeof(window.onload) != "function")
    {
	   window.onload = createContents;
    }
    else
    {
        window.onload = function()
        {
	   	    oldOnload();
		    createContents();
	    }
    }
}

/* Create contents for page */
function createContents()
{
	// check to see that the browser supports the getElementById method
	if(!document.getElementById){return false;}
    // Check for configuration variables
    if (typeof( contentsTag ) === "undefined")
    {
        var contentsTag = 'h2';
    }
    
    if (typeof( contentsContainer ) === "undefined")
    {
        var contentsContainer = 'subnav';
    }

    // Get array
    var contentArray = document.getElementsByTagName( contentsTag );
    
    // Set id's
    for (var i = 0; i < contentArray.length; i++)
    {
        contentArray[i].id = 'contents_' + (i + 1);
    }
    
    // Create contents
    var containerList = document.getElementById( contentsContainer );
    
    for (var i = 0; i < contentArray.length; i++)
    {
        // Strip non-breaking spaces and other converted characters
        var newHTML = contentArray[i].innerHTML;
        newHTML = newHTML.replace( /(\&nbsp\;)/g, '' );
        newHTML = newHTML.replace( /\&lt\;/g, '<' );
        newHTML = newHTML.replace( /\&gt\;/g, '>' );
        newHTML = newHTML.replace( /\&quot\;/g, '"' );
        newHTML = newHTML.replace( /\&\#039\;/g, '\'' );
        newHTML = newHTML.replace( /\&amp\;/g, '&' );

        contentArray[i].innerHTML = newHTML;
        
        // Check title out
        if (contentArray[i].innerHTML.match( /^(\s)*$/ ))
        {
            continue;
        }

        var contentsItem = document.createElement( 'li' );
        var contentsItemLink = document.createElement( 'a' );
        var contentsItemLinkText = document.createTextNode( newHTML );
        
        contentsItemLink.href = '#' + contentArray[i].id;
        
        contentsItemLink.appendChild( contentsItemLinkText );
    	contentsItem.appendChild( contentsItemLink );
        containerList.appendChild( contentsItem );
    }
}
