/*
-------------------------------------------------------------------------------
JavaScript Debug Window functions

Author:		aleonard
Date:		2007.04.11
Description: Various methods for the JavaScript Debug Window
-------------------------------------------------------------------------------
*/
var useJavascriptDebugger = false;
var _dragEL = null;
var _resizeEL = null;
var _resizeHandle = null;
var _history = new Array();
var _iCurrent = 0;
var _X = 0;
var _Y = 0;

/*
-------------------------------------------------------------------------------
Author:			aleonard
Date:			2007.04.11
Description:	function used to write to the debug output window
Parameters:
	strTestName		Descriptive text for the output, will be bold folowed by a colon
	strOutput		Text to be displayed in the output window
-------------------------------------------------------------------------------
*/
function javascriptDebugWrite(strTestName, strOutput)
{
	if (useJavascriptDebugger != true)
	{
		return;
	}
	objDebugWindow = document.getElementById(javascriptDebugWindowID);
	objOutputWindow = document.getElementById(javascriptDebugOutputID);
	//Make sure we have a string value
	strOutput = strOutput.toString();
	//Show the DebugWindow
	objDebugWindow.style.display = 'block';
	objDebugWindow.style.visibility = 'visible';
	//Add to the output
	if(objOutputWindow.innerHTML != '')
	{
		objOutputWindow.innerHTML += '<br/>';
	}
	if (strOutput.indexOf('{Error}') > -1)
	{
		strOutput = '<br/><span style="color:#C00000;padding-left:24px"><b>Error: </b>' + strOutput.replace(/{Error}/g,'') + '</span>';
	}
	else if (strOutput.indexOf('{Help}') > -1)
	{
		strOutput = '<br/><div style="color:#00C000;padding-left:24px">' + strOutput.replace(/{Help}/g,'').replace(/\n/g,'<br/>') + '</span>';
	}
	else
	{
		strOutput = '<span style="color:#0000C0">' + strOutput.replace(/</g,'&lt;').replace(/>/g,'&gt;').replace(/\n/g,'<br/>') + '</span>';
	}	
	
	objOutputWindow.innerHTML += '<b>' + strTestName + ':</b> ' + strOutput;
	objOutputWindow.scrollTop = objOutputWindow.scrollHeight;
}

/*
-------------------------------------------------------------------------------
Author:			aleonard
Date:			2007.04.11
Description:	Clears the output window and closes the debug window
-------------------------------------------------------------------------------
*/
function javascriptDebugEnd()
{
	objDebugWindow = document.getElementById(javascriptDebugWindowID);
	//Clear the output
	javascriptDebugClear();
	//Hide the DebugWindow
	objDebugWindow.style.display = 'none';
	objDebugWindow.style.visibility = 'hidden';
	window.event.cancelBubble = true;
}

/*
-------------------------------------------------------------------------------
Author:			aleonard
Date:			2007.04.11
Description:	Clears the output window
-------------------------------------------------------------------------------
*/
function javascriptDebugClear()
{
	objOutputWindow = document.getElementById(javascriptDebugOutputID);
	//Clear the output
	objOutputWindow.innerHTML = '';
	window.event.cancelBubble = true;
}

/*
-------------------------------------------------------------------------------
Author:			aleonard
Date:			2007.04.11
Description:	Toggles between word-wrap and no wrap
Parameters:
	oEL			Reference to the Wrap button
-------------------------------------------------------------------------------
*/
function javascriptDebugToggleWrap(oEL)
{
	objOutputWindow = document.getElementById(javascriptDebugOutputID);
	if(objOutputWindow.style.whiteSpace == 'nowrap')
	{
		objOutputWindow.style.whiteSpace = 'normal';
		oEL.innerText = 'No Wrap';
	}
	else
	{
		objOutputWindow.style.whiteSpace = 'nowrap';
		oEL.innerText = 'Wrap';
	}
	window.event.cancelBubble = true;
}

/*
-------------------------------------------------------------------------------
Author:			aleonard
Date:			2007.04.11
Description:	Copies the contents of the output window to the clipboard (IE only)
-------------------------------------------------------------------------------
*/
function javascriptDebugCopy()
{
	objOutputWindow = document.getElementById(javascriptDebugOutputID);
	window.clipboardData.setData('text',objOutputWindow.innerText);
	window.event.cancelBubble = true;
}

/*
-------------------------------------------------------------------------------
Author:			aleonard
Date:			2007.04.11
Description:	Executes a javascript command entered on the 'command line'
				Also, adds the command to an array so the up and down arrows
				can be used to navigate previously entered commands
Parameters:
	oEL			Reference to the textbox
-------------------------------------------------------------------------------
*/
function javascriptDebugCommand(oEL)
{

	window.event.cancelBubble = true;
	window.event.returnValue = false;

	switch (window.event.keyCode)
	{
		case 13:
			var strResult = new String();
			var strValue = new String(oEL.value.toString().toLowerCase());
			_iCurrent = _history.push(oEL.value);
			switch (true)
			{
				case (strValue == 'exit'):
				case (strValue == 'close'):
					javascriptDebugEnd();
					break;
				case (strValue == 'cls'):
				case (strValue == 'clear'):
					javascriptDebugClear();
					break;
				case (strValue == 'copy'):
					javascriptDebugCopy();
					break;
				case (strValue == 'list dom'):
				case (strValue == 'listdom'):
					javascriptDebugListDOM();
					break;
				case (strValue.indexOf('list children of ') > -1):
					var strId = strValue.substring('list children of '.length, strValue.length);
					javascriptDebugListChildren(strId);
					break;
				case (strValue == 'help'):
					javascriptDebugListHelp();
					break;
				case (strValue == 'list history'):
				case (strValue == 'list commands'):
				case (strValue == 'list cmd'):
					javascriptDebugListHistory();
					break;
				default:
					try
					{
						strResult = eval(oEL.value).toString();
					}
					catch (e)
					{
						strResult = '{Error}' + e.message + ' [' + e.number + ']';
					}
					javascriptDebugWrite('> ' + strValue,strResult.replace(/</g,'&lt;').replace(/>/g,'&gt;'));
			}
			oEL.value = '';
			break;
		case 38: //up arrow
			if (_iCurrent > 0)
			{
				_iCurrent -= 1;
				oEL.value = _history[_iCurrent];
			}
			break;
		case 40: //down arrow
			if (_iCurrent < (_history.length - 1))
			{
				_iCurrent += 1;
				oEL.value = _history[_iCurrent];
			}
			break;
		case 27: //escape
			oEL.value = '';
			_iCurrent = _history.length;
			break;
	}
}

/*
-------------------------------------------------------------------------------
Author:			aleonard
Date:			2007.04.11
Description:	Begins the dragging of the debug window
-------------------------------------------------------------------------------
*/
function javascriptDebugStartDrag()
{
	_dragEL = document.getElementById(javascriptDebugWindowID);
	document.attachEvent('onmousemove',javascriptDebugMove);
	document.attachEvent('onmouseup',javascriptDebugEndDrag);
	_X = window.event.clientX;
	_Y = window.event.clientY;
}

/*
-------------------------------------------------------------------------------
Author:			aleonard
Date:			2007.04.11
Description:	Ends the dragging of the debug window
-------------------------------------------------------------------------------
*/
function javascriptDebugEndDrag()
{
	_dragEL = null;
	document.detachEvent('onmousemove',javascriptDebugMove);
	document.detachEvent('onmouseup',javascriptDebugEndDrag);
}

/*
-------------------------------------------------------------------------------
Author:			aleonard
Date:			2007.04.11
Description:	Handles the moving of the debug window while dragging
-------------------------------------------------------------------------------
*/
function javascriptDebugMove()
{
	if (_dragEL != 'undefined' && _dragEL != null)
	{
		_dragEL.style.pixelTop = _dragEL.style.pixelTop + _dragEL.scrollTop + (window.event.clientY - _Y);
		_dragEL.style.pixelLeft = _dragEL.style.pixelLeft + _dragEL.scrollLeft + (window.event.clientX - _X);
		_X = window.event.clientX;
		_Y = window.event.clientY;
		window.event.cancelBubble = true;
		window.event.returnValue = false;
	}
}

/*
-------------------------------------------------------------------------------
Author:			aleonard
Date:			2007.04.11
Description:	Begins the resizing of the debug window
Parameters:
	oEL			Reference to the resize handle
-------------------------------------------------------------------------------
*/
function javascriptDebugStartResize(oEL)
{
	_resizeEL = document.getElementById(javascriptDebugWindowID);
	_resizeHandle = oEL;
	document.attachEvent('onmousemove',javascriptDebugResize);
	document.attachEvent('onmouseup',javascriptDebugEndResize);
	_X = window.event.clientX;
	_Y = window.event.clientY;
}

/*
-------------------------------------------------------------------------------
Author:			aleonard
Date:			2007.04.11
Description:	Ends the resizing of the debug window
-------------------------------------------------------------------------------
*/
function javascriptDebugEndResize()
{
	_resizeEL = null;
	_resizeHandle = null;
	document.detachEvent('onmousemove',javascriptDebugResize);
	document.detachEvent('onmouseup',javascriptDebugEndResize);
}

/*
-------------------------------------------------------------------------------
Author:			aleonard
Date:			2007.04.11
Description:	Handles the resizing of the debug window and other elements
-------------------------------------------------------------------------------
*/
function javascriptDebugResize()
{
	if (_resizeEL != 'undefined' && _resizeEL != null)
	{
		if (_resizeEL.style.pixelHeight == 0)
		{
			_resizeEL.style.pixelHeight = _resizeHandle.style.pixelTop + 22;
			_resizeEL.style.pixelWidth = _resizeHandle.style.pixelLeft + 18;
		}
		var intNewHeight = _resizeEL.style.pixelHeight + (window.event.clientY - _Y);
		var intNewWidth = _resizeEL.style.pixelWidth + (window.event.clientX - _X);
		if (intNewHeight < 200)
		{
			intNewHeight = 200;
		}
		if (intNewWidth < 400)
		{
			intNewWidth = 400;
		}
		_resizeEL.style.height = intNewHeight;
		_resizeEL.style.width = intNewWidth;
		_resizeEL.children(1).style.width = (intNewWidth - 5) + 'px';
		_resizeEL.children(2).style.height = (intNewHeight - 75) + 'px';
		_resizeEL.children(4).style.width = (intNewWidth - 45) + 'px';
		_resizeHandle.style.top = (intNewHeight - 22) + 'px';
		_resizeHandle.style.left = (intNewWidth - 18) + 'px';
		_X = window.event.clientX;
		_Y = window.event.clientY;
		window.event.cancelBubble = true;
		window.event.returnValue = false;
	}
}

function javascriptDebugListDOM()
{
	var arr = new Array();
	
	for (var i = 0; i < document.all.length; i++)
	{
		arr.push('<' + document.all(i).tagName.toString() + ' id="' + document.all(i).id + '">');
	}
	
	javascriptDebugWrite("DOM","\n" + arr.join("\n"));
}

function javascriptDebugListHistory()
{
	javascriptDebugWrite("Command History","\n" + _history.join("\n"));
}

function javascriptDebugListChildren(strId)
{
	var arr = new Array();

	try
	{
		var obj = eval(strId);
	}
	catch (e)
	{
		var obj = document.getElementById(strId);
	}

	for (var i = 0; i < obj.all.length; i++)
	{
		arr.push('<' + obj.all(i).tagName.toString() + ' id="' + obj.all(i).id + '">');
	}
	
	javascriptDebugWrite("Children of " + strId,"\n" + arr.join("\n"));
}

function javascriptDebugListHelp()
{
	var arr = new Array();
	
	arr.push('<tr><td style="white-space:nowrap;vertical-align:top">clear</td><td>Clears the output window</td></tr>');
	arr.push('<tr><td style="white-space:nowrap;vertical-align:top">close</td><td>Clears the output window and closes the debug window</td></tr>');
	arr.push('<tr><td style="white-space:nowrap;vertical-align:top">cls</td><td>Clears the output window</td></tr>');
	arr.push('<tr><td style="white-space:nowrap;vertical-align:top">copy</td><td>Copies the contents of the output window to the clipboard</td></tr>');
	arr.push('<tr><td style="white-space:nowrap;vertical-align:top">exit</td><td>Clears the output window and closes the debug window</td></tr>');
	arr.push('<tr><td style="white-space:nowrap;vertical-align:top">help</td><td>This help list</td></tr>');
	arr.push('<tr><td style="white-space:nowrap;vertical-align:top">list children of <i>obj</i></td><td>Lists the tags and ids of elements that are children of the object specified by <i>obj</i> (i.e., \'list children of document\' is the same as list dom)</td></tr>');
	arr.push('<tr><td style="white-space:nowrap;vertical-align:top">list commands</td><td>Lists the command history</td></tr>');
	arr.push('<tr><td style="white-space:nowrap;vertical-align:top">list cmd</td><td>Lists the command history</td></tr>');
	arr.push('<tr><td style="white-space:nowrap;vertical-align:top">list dom</td><td>Lists the tags and ids of the elements in the document</td></tr>');
	arr.push('<tr><td style="white-space:nowrap;vertical-align:top">list history</td><td>Lists the command history</td></tr>');
		
	javascriptDebugWrite("Help","{Help}<table border=0 cellpadding=2 cellspacing=0>" + arr.join("") + "</table>");
}