document.write('<link href="http://www.ejun.cn/mydoc/css/default.css" rel="stylesheet" type="text/css" />');
document.write('<script src="http://www.ejun.cn/counter/mystat.asp?style=no"></script>');

// JavaScript Document
function formatDoc(sheader, sfooter, n)
{
	if (!n) n = document.body;
	//sfooter="Power by www.e-jun.com 2005-2006";
	
	// 创建doucmentFragment, 将n的所有字节点暂时保存起来
	var f = document.createDocumentFragment();
	while(n.firstChild)
		f.appendChild(n.firstChild);
	
	var container = document.createElement("div");
	container.setAttribute("id", "container");
	n.appendChild(container);
	
	var containerbg = document.createElement("div");
	containerbg.setAttribute("id", "containerbg");
	container.appendChild(containerbg);
	
	var header = document.createElement("div");
	header.setAttribute("id", "header");
	containerbg.appendChild(header);
	
	var mainwrap = document.createElement("div");
	mainwrap.setAttribute("id", "mainwrap");
	containerbg.appendChild(mainwrap);
	
	var footer = document.createElement("div");
    footer.setAttribute("id", "footer");
	containerbg.appendChild(footer);
	
	if (sheader && sheader!="")
	{
	   var p = document.createElement("p");
	   p.appendChild(document.createTextNode(sheader));
	   header.appendChild(p);
	}
	
	if (sfooter && sfooter!="")
	{
	   var p = document.createElement("p");
	   p.appendChild(document.createTextNode(sfooter));
	   footer.appendChild(p);
	}
	
	var p = document.createElement("p");
	var home = document.createElement("a");
	home.href = "http://www.ejun.cn";
	home.appendChild(document.createTextNode("www.eJun.cn"));
    p.appendChild(document.createTextNode("Power By "));
    p.appendChild(home);
    p.appendChild(document.createTextNode(" 2005-2006"));
	footer.appendChild(p);
	
	var content = document.createElement("div");
	content.setAttribute("id", "content");
	mainwrap.appendChild(content);
	
	content.appendChild(f);
	
	// 创建文档目录
	var toc = document.createElement("span");
	toc.setAttribute("toc");
	insertAfter(content, toc, content.firstChild);
	
	makeToc(toc, content);

	var script = document.createElement("script");
	//script.setAttribute("type", "text/javascript");
	script.setAttribute("src", "http://www.ejun.cn/counter/mystat.asp");
	n.appendChild(script);
	
}

function insertAfter(parent, node, referenceNode) {
    if(referenceNode.nextSibling) {
        parent.insertBefore(node, referenceNode.nextSibling);
    } else {
        parent.appendChild(node);
    }
}


function makeToc(tocNode, contentNode)
{
	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)
				s += child.data;
			else
			    s += getTextContent(child);	
		
		}
		return s;	
	}
	function addSections(n, tbody, sectionNumbers)
	{				
		for(var m = n.firstChild; m != null; m = m.nextSibling)
		{		
		   
			if ((m.nodeType == 1)&&
		       (m.tagName.length==2)&&
			   (m.tagName.charAt(0)=="H"))
			{				
				var level = m.tagName.charAt(1);
				level = parseInt(level) - 1;
				  
				if (!isNaN(level)&&(level>=1)&&(level<=6))
				{
					sectionNumbers[level-1]++;
					for(var i=level;i<6;i++) sectionNumbers[i]=0;
					
					var sectionNumber = "";
					for (var i = 0; i < level; ++i)
					{
						sectionNumber += sectionNumbers[i];
						if (i < level - 1) sectionNumber += ".";
					}
					
					// 创建一个锚元素，标记该段的开端
					// 它将成为我们添加到目录里的连接的目标
					var anchor = document.createElement("a");
					anchor.setAttribute("name", "SECT" + sectionNumber);
					anchor.setAttribute("id", "SECT" + sectionNumber);
					
					var backlink = document.createElement("a");
					backlink.setAttribute("href", "#TOC");
					backlink.appendChild(document.createTextNode("返回目录"));
					anchor.appendChild(backlink);
					
					n.insertBefore(anchor, m);
					//insertAfter(n, anchor, m);
					
					var seclink = document.createElement("a");
					seclink.setAttribute("href", "#SECT" + sectionNumber);
					
					//for(a in m)
					//  alert(a);
					var sectionTitle = getTextContent(m);
    				seclink.appendChild(document.createTextNode(sectionTitle));
					
					// 为目录行创建表格行
					var row = document.createElement("tr");
					var col1 = document.createElement("td");
					var col2 = document.createElement("td");
					
					col1.setAttribute("align", "left");
					//alert(sectionNumber);
					col1.appendChild(document.createTextNode(sectionNumber));					
					col2.appendChild(seclink);
					col2.style.textIndent = (9 * level) + "pt";
					
					row.appendChild(col1);
					row.appendChild(col2);					
					tbody.appendChild(row);
					
					m.insertBefore(document.createTextNode(sectionNumber + " "), m.firstChild);	
				
				}
			
			
			}
			else
			{
				addSections(m, toc, sectionNumbers);
			}
		
		}
	
	
	}
	
	
	
	
	//创建一个<div>元素, 作为TOC树的根元素
	var toc = document.createElement("div");
	toc.setAttribute("id", "toc");
	
	//用锚元素作为TOC的开端，以便我们能连接回来
	var anchor = document.createElement("a");
	anchor.setAttribute("name", "TOC");
	anchor.setAttribute("id", "TOC");
	toc.appendChild(anchor);
	
	anchor.appendChild(document.createTextNode("文档目录"));
	
	// 创建一个Table用来存放目录
	var table = document.createElement("table");
	toc.appendChild(table);
	
	var tbody = document.createElement("tbody");
	table.appendChild(tbody);
	
	// 初始化一个数组，以跟踪段的编号
	var sectionNumbers = [0, 0, 0, 0, 0, 0];
	
	addSections(contentNode, tbody, sectionNumbers);
	
	tocNode.parentNode.replaceChild(toc, tocNode);
}
