// JavaScript Document
/*
Author: Jim Michaels <jmichae3@yahoo.com>
Abstract: show HTML document node tree using Javascript
Create Date:  Nov 11, 2009
Current Date: Nov 16, 2009
Version 1.0


Copyright 2009 Jim Michaels

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

//in the FF browser, you generally get 1's and 3's (elements and text).
var showtreeStrStruct="";
var arrNodeTypes=Array("0:unknownElementType","1:ELEMENT_NODE","2:ATTRIBUTE_NODE","3:TEXT_NODE","4:CDATA_SECTION_NODE","5:ENTITY_REFERENCE_NODE","6:ENTITY_NODE","7:PROCESSING_INSTRUCTION_NODE","8:COMMENT_NODE","9:DOCUMENT_NODE","10:DOCUMENT_TYPE_NODE","11:DOCUMENT_FRAGMENT_NODE","12:NOTATION_NODE");
/*
showNodeTree - given an element object, shows a web page depicting the tree of document elements
also can show other nodes, such as text, CDATA, comment, etc.
el - base of tree node to start from.  can be an element node.  for example, document.getELementById("blah")
level - should be 0 (zero), the starting level number.
maxrecursionlevel - maximum recursion depth before you get an [formerly] alert box (or a ton of them). this corresponds to about twice the the number of nested levels of the tags in your document.  100 is usually a safe number to use.
showtextnodes - true gives you text nodes.  false eliminates them.
showothernodes - true gives you all the other odes other than element and text.  false eliminates these.  normally these nodes don't even exist for your average use, like something within the body tags.
*/
function showNodeTree(el, level, maxrecursionlevel, showtextnodes, showothernodes) {
	// at this point, el should be a UL tag.
	var elCurrent, elNextChild;
	if (level>maxrecursionlevel) {
		//alert("menu nested too deep (>100 levels).  either increase levelCount from 100 or consider using a smaller page");
		return;
	}
	if (0==level) {
		showtreeStrStruct="<html><head><title>structure</title></head><body><ul>";
	}
	if (null==el) {
		return;
	}
	//has children.  first child of first child may be a UL tag, or it will probably be null.
		
	//show current node
	if (el.tagName) {
		showtreeStrStruct+="<li>"+arrNodeTypes[el.nodeType]+"="+el.tagName+"\n";
	} else if (showtextnodes && el.nodeType==3) {
		showtreeStrStruct+="<li>"+arrNodeTypes[el.nodeType]+"="+el.nodeValue+"\n";
	} else if (showothernodes) {
		showtreeStrStruct+="<li>"+arrNodeTypes[el.nodeType]+"\n";
	}
	
	//process siblings
	var x;
	for (x=0; x < el.childNodes.length; x++) {
		//handle recursion
		if (null != el.childNodes[x] && undefined != el.childNodes[x]) {
			//child found, recurse
			//the following 3 lines are debug code
			showtreeStrStruct+=", childNodes["+x+"]";
			showtreeStrStruct+="<ul>\n"; //will this really work or just generate a bunch of garbage?
			showNodeTree(el.childNodes[x], level+1, maxrecursionlevel, showtextnodes, showothernodes); //recurse into child node
			showtreeStrStruct+="</ul>\n";
		}
	}
	if (0==level) {
		showtreeStrStruct += "</ul></body></html>";
		   // Output the new document in a new window.
		var w = window.open("", "tree");
		w.document.open();
		w.document.write(showtreeStrStruct);
		w.document.close();
	}
}
/*
function showtree(el, level, maxrecursionlevel, showtextnodes) {
	// at this point, el should be a UL tag.
	var elCurrent, elNextChild;
	if (level>maxrecursionlevel) {
		alert("menu nested too deep (>100 levels).  either increase levelCount from 100 or consider using a smaller page");
		return;
	}
	if (0==level) {
		showtreeStrStruct="<html><head><title>structure</title></head><body><ul>";
	}
	if (null==el) {
		return;
	}
	//has children.  first child of first child may be a UL tag, or it will probably be null.
	for (elCurrent = el; null != elCurrent && undefined != elCurrent; elCurrent = elCurrent.nextSibling) {
		//process siblings
		elNextChild = elCurrent.childNodes[0];
		
		//show current node
		if (elCurrent.tagName) {
			showtreeStrStruct+="<li>"+arrNodeTypes[elCurrent.nodeType]+"="+elCurrent.tagName+"\n";
		} else if (showtextnodes && elCurrent.nodeType==3) {
			showtreeStrStruct+="<li>"+arrNodeTypes[elCurrent.nodeType]+"="+elCurrent.nodeValue+"\n";
		} else {
			showtreeStrStruct+="<li>"+arrNodeTypes[elCurrent.nodeType]+"\n";
		}
		
		//handle recursion
		if (null != elNextChild && undefined != elNextChild) {
			//child found, recurse
			//the following 3 lines are debug code
			showtreeStrStruct+="<ul>\n"; //will this really work or just generate a bunch of garbage?
			showtree(elNextChild, level+1, maxrecursionlevel, showtextnodes); //recurse into child node
			showtreeStrStruct+="</ul>\n";
		}
	}
	if (0==level) {
		showtreeStrStruct += "</ul></body></html>";
		   // Output the new document in a new window.
		var w = window.open("", "tree");
		w.document.open();
		w.document.write(showtreeStrStruct);
		w.document.close();
	}
}
*/
