// file: http://www.cprince.com/courses/cs3121/media/JavaScript/utils.js
// return true if browser is IE; false otherwise.
function BrowserIsIE() {
	if (navigator.appName.indexOf("Microsoft") != -1) {
		// indexOf method returns the position of the search string within the string
		// returns -1 only when string not found
		// navigator.appName == "Microsoft Internet Explorer" for IE
		// navigator.appName == "Netscape" for other browsers I’ve tested
		return true;
	} else {
		return false;
	}
}

// e is a possible parameter passed to a document event handler
// return an event based on the argument, or a global event object
function getEvent(e) {
	// if this is Internet Explorer, we don't get passed events to 
	// handlers, and so we'll have to get it from the 
	// window event object.
	if (BrowserIsIE()) {
		e = window.event;
	}
	return e;
}

// Call this to start sending debugging output out to a new browser window
function StartDebug() {
	// Usually a "no no"-- create a new global variable to 
	// refer to our window
	// Using an empty feature list causes the features to default to no or off.
	_debugWindow = window.open("","debugWindow", "");
}

// Send the debugging output given in the string to our debug window
function Debug(s) {
	_debugWindow.document.write('<br>')
	_debugWindow.document.write(s)
}

// Send the debugging output given in the string to our debug window
function DebugNoNewline(s) {
	_debugWindow.document.write(s)
}

// A function to pretty print all of the tags in a document. I.e., print
// the tags and text of a document at indentation levels
// reflecting the structure of the document.
function PrettyPrint() {
	StartDebug()
	
	var bodyNodes = document.getElementsByTagName('body')
	for (var x=0; x < bodyNodes.length; x++) {
		PrettyPrintAux(bodyNodes[x])
	}
}

function PrettyPrintAux(root) {
	for (var x=0; x < root.childNodes.length; x++) {
		var childNode = root.childNodes[x];
		
		if (childNode.nodeType == Node.TEXT_NODE) {
			Debug("'" + childNode.nodeValue + "'")
		} else {
			Debug(childNode.nodeName)
			DebugNoNewline("<blockquote>")
			PrettyPrintAux(childNode)
			DebugNoNewline("</blockquote>")
		}
		
	}
}
