// $Id: debug.js,v 1.9 2004/09/29 10:39:21 cscharf Exp $
// $Date: 2004/09/29 10:39:21 $
// $Author: cscharf $

// -- Debugging Window --

// Global flag to use to enable/disable error messages.
var DEBUGGING_ENABLED = false;

var debugWindow;

// Creates a pop-up window and draws messages into it. If the window already
// exists it will reuse it.
//
// functionName: Header of the message block.
// error: Text of the messag block.
// isImportant: Optional parameter which will highlight the block when set to
//  true.
function debug( functionName, error, isImportant )
	{
	if( !DEBUGGING_ENABLED )
		return;

	// Test to see if the debug window exists and if we have permission to
	// write into it. We lose access rights when the URL changes and have
	// to recreate the window.
	try
		{
		var accessTest = debugWindow.document.documentElement;
		delete accessTest;
		}
	catch( exception )
		{
		debugWindow = null;
		}

	if( !debugWindow )
		{
		debugWindow = window.open( null, 'debugWindow', 'width=500, height=650, scrollbars=yes, resizable=yes' );

		var debugDocument = debugWindow.document;
		with( debugDocument )
			{
			open( );
			writeln( '<html>' );
			writeln( '<head>' );
			writeln( '<title>Javascript Debug Log</title>' );
			writeln( '<style type="text/css">' );
			writeln( 'body { background-color: gray; }' );
			writeln( 'h1 { width: 100%; border: dashed silver 3px; padding: 10px; color: black; background-color: red; overflow: hidden; }' );
			writeln( 'div { width: 100%; clear: both; border: solid silver 1px; padding: 5px; background-color: white; }' );
			writeln( 'h2 { width: 100%; border: solid black 3px; padding: 10px; color: black; background-color: silver; overflow: hidden; }' );
			writeln( 'p { width: 100%; border: solid black 1px; padding: 10px; color: black; background-color: white; overflow: scroll; }' );
			writeln( 'input.clear { width: 50%; float: left; border: outset silver 2px; padding: 1px; color: black; background-color: silver; }' );
			writeln( 'input.close { width: 50%; float: right; border: outset silver 2px; padding: 1px; color: black; background-color: silver; }' );
			writeln( '</style>' );
			writeln( '</head>' );
			writeln( '<body>' );
			writeln( '<h1>' );
			writeln( 'Debug log for: ' );
			writeln( document.location );
			writeln( '</h1>' );
			writeln( '<input type="button" class="clear" value="Clear Window" onclick="document.getElementById( \'errorLocation\' ).innerHTML = \'\';"/>' );
			writeln( '<input type="button" class="close" value="Close Window" onclick="window.close( );"/>' );
			writeln( '<div id="errorLocation"></div>' );
			writeln( '<input type="button" class="clear" value="Clear Window" onclick="document.getElementById( \'errorLocation\' ).innerHTML = \'\';"/>' );
			writeln( '<input type="button" class="close" value="Close Window" onclick="window.close( );"/>' );
			writeln( '</body>' );
			writeln( '</html>' );
			close( );
			}
		}

	if( isImportant )
		{
		var headerText = debugWindow.document.createTextNode( ' === ' + functionName + ' === ' );
		var headerNode = debugWindow.document.createElement( 'em' );
		headerNode.appendChild( headerText );
		}
	else
		var headerNode = debugWindow.document.createTextNode( functionName );
	var messageHeader = debugWindow.document.createElement( 'h2' );
	messageHeader.appendChild( headerNode );
	debugWindow.document.getElementById( 'errorLocation' ).appendChild( messageHeader );

	var messageBody = debugWindow.document.createElement( 'pre' );
	messageBody.appendChild( debugWindow.document.createTextNode( error ) );
	// This would be safer, but it's harder to read. Maybe just escape </pre>
	// tags?
	//messageBody.appendChild( debugWindow.document.createTextNode( encodeHTML( error ) ) );
	var codeTag = debugWindow.document.createElement( 'code' );
	codeTag.appendChild( messageBody );
	var pTag = debugWindow.document.createElement( 'p' );
	pTag.appendChild( codeTag );
	debugWindow.document.getElementById( 'errorLocation' ).appendChild( pTag );

	// This gets really annoying with event handlers.
	if( isImportant )
		debugWindow.focus( );
	}

// Registers a global error catching event handler. This will catch all
// javascript errors that the window generates. If this function returns false
// the error will be displayed as normal. If it returns true the browser will
// silently drop it.
window.onerror = function( message, location, lineNumber )
	{
	debug( location + ' at line ' + lineNumber, message );
	return !DEBUGGING_ENABLED;
	}
