/***************************************************************************
*   Copyright (C) 2006, Paul Lutus                                        *
*                                                                         *
*   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 2 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, write to the                         *
*   Free Software Foundation, Inc.,                                       *
*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
***************************************************************************/

window.onerror = handleError; // tell javaScript about error handler

docLocation = unescape(location); // decode escaped characters

//
// this is a required hack to get MSIE to transfer variables locally
//
if(navigator.appName.toLowerCase().substring(0,9) == "microsoft") {
	if(docLocation.indexOf("file:///") >= 0) {
		docLocation = docLocation.substring(8);
	}
}

var i = docLocation.lastIndexOf("?");

if(i >= 0) {
	objectString = docLocation.substring(i+1);
	docLocation = docLocation.substring(0,i);
}
else {
	objectString = "window";
}

var tableBg = "bgcolor=#e3e3ca";
var tableBorder = "bgcolor=#004080";
var head = "Tree Position: " + objectString;

var title = wrapTag("JavaScript Object Tree Browser &mdash; Copyright &copy; 1999, P. Lutus","B");
title = wrapTag(title,"P");

var subtitle = wrapTag("Use your browser's \"back\" button to move back up the tree.","I");
subtitle = wrapTag(subtitle,"P");

var note = wrapTag("MSIE may crash when certain links are clicked. Netscape never does.","I");
note = wrapTag(note,"P");

var ss = head;

function handleError() // the error-handling function
{
	var un = wrapTag("(unavailable)","CENTER");
	ss += makeTableRow(un,un,"TD",tableBg);
	ss = endFormat(ss);
	document.write(ss);
	return true; // required
}



// "wrapTag" is an economical way to make HTML tags

function wrapTag(str,taglbl,mods)
{
	var r = "<" + taglbl; // start building tag
	if(wrapTag.arguments.length > 2) {
		r += " " + mods; // add tag modifiers
	}
	return r + ">" + str + "</" + taglbl + ">"; // finish tag
}

function makeTableRow(ld,rd,tag,tableBg) // make a 2-column row
{
	var row = wrapTag(ld,tag,tableBg) + wrapTag(rd,tag,tableBg);
	return wrapTag(row,"TR") + "\n";
}

// "srchRepl" globally replaces one string with another within a string
// this doesn't require regExp so it works with all browsers

function srchRepl(src,a,b) // search for string "a", replace with string "b"
{
	var dest = "";
	while ((i = src.indexOf(a)) >= 0) {
		dest += src.substring(0,i) + b; // replace with b
		src = src.substring(i + a.length); // skip over a
	}
	dest += src; // default action
	return(dest);
}

// this function lists an object's members

function showObject(xns,tableBg) {
	var x = getObject(xns); // isolate object of interest
	var s = "";
	for (var n in x) {
		var cx = x[n]; // child of x
		var rd = srchRepl("" + cx,">","&gt;"); // escape any tag delimiters
		rd = srchRepl(rd,"<","&lt;");
		var ld = xns + "." + n;
		if((typeof(cx) == "object") && (cx != null)) { // make link to child object
			ld = wrapTag(ld,"A","HREF=" + docLocation + "?" + ld);
		}
		if((rd.indexOf("\n") >= 0) || (rd.indexOf("\r") >= 0)) {
			rd = wrapTag(rd,"PRE"); // multiline format
		}
		s += makeTableRow(ld,rd + "&nbsp;","TD",tableBg);
	}
	return s;
}

// use getObject instead of eval -- in MSIE, eval can't walk the entire object tree

function getObject(str) { // walk tree to isolate object
	var x = window; // default object root
	var op = "";
	do {
		var i = str.indexOf(".");
		if(i > 0) {
			op = str.substr(0,i);
			str = str.substring(i+1);
		}
		else {
			op = str;
			str = "";
		}
		x = x[op]; // replace object with its child
	}
	while(i > 0);
	return x;
}

function endFormat(s) // cope with nasty errors
{
	// inner table
	s = wrapTag(s,"TABLE","border=1 cellpadding=4 cellspacing=2 width=95% " + tableBorder);
	
	// outer table
	s = wrapTag(s,"TD");
	s = wrapTag(s,"TR");
	s = wrapTag(s,"TABLE","border=1 cellpadding=0 cellspacing=0 width=95% " + tableBorder);
	
	// title & subtitle
	s = title + subtitle + note + s;
	return wrapTag(s,"CENTER");
}



function show_it() {

document.write(wrapTag(head,"TITLE"));

ss = wrapTag(ss,"B");
ss = wrapTag(ss,"TD","colspan=2 align=center " + tableBg);
ss = wrapTag(ss,"TR");

ss += makeTableRow("Name","Value","TH",tableBg);

ss += showObject(objectString, tableBg); // usual source of errors

ss = endFormat(ss);
document.write(ss);

document.close();
}