/// <history>
/// <change date="2000-05-22" author="Darrin Morgan">Original Version</change>
/// </history>

/// <summary>
/// When this javascript routine is called it opens a new popup 
/// window and writes to this (using GetPropertyHtml) all the properties
/// and their values that the named object exhibits.  This is used
/// for debugging and enables us to examine objects.
/// <summary>
/// <param name="obj">The object whose properties are to be enumerated</param>
/*public*/ function Inspect( obj, expand )
{
    var win = window.open("","InspectWindow","left=10,top=10,height="+(screen.availHeight-50)+",width=550,status=no,resizable=yes,scrollbars=yes");

    var head = '<html>\n' 
		+ '<head>\n'
		+ '<style type="text\JavaScript">\n'
        + 'td { font-family: verdana; font-size: 8pt; }\n'
        + 'a { font-family: verdana; font-size: 8pt; text-decoration: none; }\n'
        + 'h1 { font-family: verdana; font-size: 16pt; }\n'
        + 'body { margin: 0px; }\n'
        + '</style>\n';
        + '</head>\n'
        + '<body>\n';
    var body = GetPropertyHtml(obj, expand);
   
    win.document.write( head )
    win.document.write( body );
    win.document.write( "</body>\n</html>" );

	InjectCode( win );

    win.document.close();
}


/// <summary>
/// This javascript routine is used to build up an HTML table
/// containing all the properties that an object exhibits.
/// </summary>
/// <remarks>
/// The original idea for this came from an example at the web
/// site http://developer.iplanet.com/docs/examples/javascript.html
/// </remarks>
/// <param name="obj">The object whose properties are to be enumerated</param>
/*private*/ function GetPropertyHtml( obj, expand )
{ 
	var properties = "";
	var lineColor = "whitesmoke";
	var textValue = "";

	var objectId = ""
	try	{ objectId = "{" + obj.id + "}"; }
	catch(ex) { objectId = "<b style='color: red'>" + ex + "</b>"; }

	properties += '<table cellpadding="2" cellspacing="0" border="0" width="100%">\n';
	properties += '<tr valign="center">\n';
	properties += '<td bgcolor="#9CCEFF" colspan="4" width="100%"><h1>&nbsp;'
		+ '(' + typeof(obj) + ') ' + objectId + '</h1></td>\n';
	properties += '</tr>\n';

	// if parameter is an object, we need to enumerate its properties.
	if ( typeof(obj) == 'object' )
	{
		properties += '<tr bgcolor="whitesmoke">\n';
		properties += '<td align="right"><b>No</b></td>\n';
		properties += '<td align="right"><b>Property</b></td>\n';
		properties += '<td><b>Type</b></td>\n';
		properties += '<td><b>Value</b></td>\n';
		properties += '</tr>\n';

		// sort the property names into ascending order.
		var names = "";
		try
		{
			for (var propName in obj) names += ',' + propName;
		}
		catch(ex) { properties += "<tr><td colspan=3>" + ex.message + "</td></tr>"}
		var nameList = names.split(',');
		if (names.length == 0) 
		{
		}
		else
		{		
		names = names.substr(1);
		nameList.sort();

		// enumerate each property and build the html document.
		for (var propIndex=0; propIndex<nameList.length; propIndex++)
		{
			// get name of property.
			var propName = nameList[propIndex];

			// decide what color to make the line.
			lineColor = (lineColor == 'white') ? 'whitesmoke' : 'white';

			// try to get value of property.
            try
            {
				// get value of current property name.
				var prop = obj[propName];

				// ensure that HTML tags are not acted on.
				textValue = "" + prop;
				textValue = textValue.replace(new RegExp("<",'g'),"&lt;")
				
				// for object variables, build a string containing object properties.
				if (expand && typeof(prop)=="object" && prop != null)
				{
					var newNames = "";
					var regex = new RegExp("<",'g');
					for (var newpropName in prop) 
					{
						newNames += "<tr><td>" + newpropName + ": </td><td><nobr>";

						// attempt to get name of property.
						try
						{
							// if an object we can't do much else.
							if (typeof(prop[newpropName])=="object")
							{
								newNames += "[Object]";
							}
							else // if not an object we can try to show value.
							{
								var newPropvalue = prop[newpropName];
								if (newPropvalue.indexOf("<") != -1) newPropvalue = newPropvalue.replace(regex,"&lt;")
								newNames += typeof(prop[newpropName]) + " | " + newPropvalue;
							}
						}
						// if cannot get property value, assign exception object instead.
						catch(ex)
						{
							newNames += "<span style='color: red'>" + ex + "</span>";
						}
						newNames += "</nobr></td></tr>";
					}

					// if object has properties, assign table of property values to cell.
					if (newNames != "")
					{
						textValue = ""
							+ "\n\t\t<span style=\"color: blue; cursor: hand;\" onclick=\"ToggleProperty("+propIndex+")\">" + textValue + "</span>"
							+ "\n\t\t<span id=\"spanProp" + propIndex + "\" style=\"display: none\">" 
							+ "<table border='0' cellspacing='0' cellpadding='0'>" + newNames + "</table>" + "</span>";							
					}
				}
			}
			// catch any errors raised from assignment
            catch(ex)
            {
				var newNames = "";
				for (var newpropName in ex) newNames += "<br/>" + newpropName + ": " + ex[newpropName];
				textValue = "<span style=\"color:red\"><b>" + ex + "</b>" + newNames;
            }

            properties += '<tr bgcolor="' + lineColor + '">\n';
            properties += '\t<td width="05%" align="right" valign="top">' + propIndex + '</td>\n';
            properties += '\t<td width="15%" align="right" valign="top">' + propName + '&nbsp;</td>\n';
			properties += '\t<td width="15%" valign="top">' + "(" + typeof(obj[propName]) + ")" + '</td>\n';
			properties += '\t<td width="60%" valign="top">' + textValue + '</td>\n';
            properties += '</tr>\n';
		}
        }
    }
    else
    {
        properties += '<tr bgcolor="' + lineColor + '">\n';
        properties += '\t<td width="06%" align="right">==&nbsp;</td>\n';
        properties += '\t<td width="33%" align="right">==&nbsp;</td>\n';
        properties += '\t<td width="33%" align="right">&nbsp;</td>\n';
        properties += '\t<td width="66%">&nbsp;' + obj + '</td>\n';
        properties += '</tr>\n';
    }

    properties += '</table>\n';
    properties += '<div style="background-color:#9CCEFF; font-size: 7px;" />&nbsp;</div>\n';

    return properties;
}

/// <summary>
/// The window being opened has it's own javascript routines that 
/// are executed when the user clicks on elements in the table. 
/// This function writes the required javascript routines into
/// the html used to populate the window.
/// <summary>
/// <param name="win">Window object into which the javascript should be written.</param>
/* private */ function InjectCode( win )
{
	var code = "\n<script>\n";
	code += "function ToggleProperty( propId )\n"
	code += "{\n"
	code += "	var prop = document.getElementById('spanProp' + propId);\n"
	code += "	prop.style.display = (prop.style.display=='') ? 'none' : ''\n"
	code += "}\n"
	code += "</script>\n";
	
	win.document.write( code );
}