function toc() {
	if(document.getElementById) {
		var nodecol=document.getElementById('cm_pagecontent').childNodes;
		var nodelng=nodecol.length;
		var nodecnt='';
		var toc='';
		var jump='';
		var nest=tocndx=0;
		for(var i=1;i<nodelng;i++){
			if(nodecol[i].tagName=='H1'||nodecol[i].tagName=='H2'||nodecol[i].tagName=='H3'||nodecol[i].tagName=='H4') {
				nest=parseInt(nodecol[i].tagName.substr(1));
				nodecnt=nodecol[i].innerHTML;
				jump=nodecnt.replace(/(<([^>]+)>)/ig,"");
				jump=purify(jump);
				for(var j=3;j<=nest;j++) toc += '<ul>';
				toc += '\n<li><a href="#jump_' + jump + '">' + nodecnt.replace(/(<([^>]+)>)/ig,"") + '</a></li>\n';
				for(var j=3	;j<=nest;j++) toc+='</ul>';
				// New bit
				var tmp = document.createElement('a');
				tmp.id = 'jump_' + jump;
				nodecol[i].parentNode.insertBefore(tmp,nodecol[i].nextSibling);
				// Old bit - Commented
				//nodecol[i].innerHTML = '<a id="jump_' + jump + '"></a>' + nodecnt;
				tocndx++;
			}
		}
		if(tocndx>1) {
		document.getElementById('toc').innerHTML = '<div id="toccnt">\n<h4>Table of Contents</h4>\n'+toc+'\n</div>';
		document.getElementById('toc').className = 'mmhide_toc';
		}
		if (document.location.hash.length >= 6) {
			var new_pos = document.location.hash.substr(1,document.location.hash.length);
			if ( document.getElementById(new_pos) != null) {
				window.location.hash = '#' + new_pos;
			}
		}
	}
}

function purify(rawText) {
	if (!rawText) { return false; }
	var cleanText = rawText.toLowerCase();
	cleanText = cleanText.replace(/\s+/g,'');
	cleanText = cleanText.replace(/[^a-z0-9\s]/g,'');
	if (cleanText.length == 0 || cleanText.match(/^\s+$/) != null) { 
		return false; 
	}
	return cleanText
}