globalLogVar = new Array();
lastTime = 0;


function displayMsg(msg, log)
{
	return false; // comment this line out to re-enable debugging.
	var container = document.getElementById("ajaxResponse");
	if (!container)
	{
		container = document.createElement("DIV");
		container.id = "ajaxResponse";
		container.style.position = 'absolute';
		container.style.visibility = 'visible';
		container.style.overflow = 'scroll';
		container.style.top = 0 + "px";
		container.style.left = 0 + "px";
		container.style.zIndex = 5;
		container.style.height = "200px";
		container.style.width = "500px";
		container.style.backgroundColor = "#EEE";
		container.style.borderWidth = "2px";
		container.style.borderColor = "black";
		container.style.borderStyle = "solid";
		container.style.padding = "3px";
		container.style.whiteSpace = "pre";
		container.style.fontFamily = "verdana";
		container.style.fontSize = "10px";
		container.style.opacity = .9;
		document.body.appendChild(container);
	}
	
	var newMsgNode = document.createTextNode("\n" + msg + " \n");
	
	if (log || displayMsg.arguments.length == 1)
	{
		//container.insertBefore(newMsgNode, container.firstChild);
		container.firstChild ? container.insertBefore(newMsgNode, container.firstChild) : container.appendChild(newMsgNode);
		var brTag = document.createElement("hr");
		container.insertBefore(brTag, container.firstChild);
	}
	else
	{
		container.firstChild ? container.replaceChild(newMsgNode, container.firstChild) : container.appendChild(newMsgNode);
	}
}

function getDOM(obj)
{
	var propList = "\n";
	var tabMultiplier = getDOM.arguments.length > 1 ? getDOM.arguments[1] : 0;
	var tabs = "";
	for (i = 0; i < tabMultiplier; i++)
	{
		tabs += "\t";
	}
	 
	for (prop in obj)
	{
		try
		{
			if (typeof obj[prop] == "function")
			{
				continue;
			}
			propList += "\n" + tabs + prop + ": " + obj[prop];
		}
		catch(e)
		{
			propList += "\n" + tabs + prop + ": " + "Cannot read\n";
			continue;
		}
		
		if (obj[prop] == null)
		{
			continue;
		}
		
		if (getDOM.arguments.length > 1)
		{
			if (typeof obj[prop] == "object" && !obj[prop] instanceof String)
			{
				propList += getDOM(obj[prop], (getDOM.arguments[1] + 1));
			}
		}
	}
	propList += "\n";
	return propList;
}

function writeDOM(obj)
{
	if (writeDOM.arguments.length > 1)
	{
		displayMsg(getDOM(obj, 0));
	}
	else
	{
		displayMsg(getDOM(obj));
	}
}


function parseCaller(yourFunction)
{
	var yourCaller = yourFunction.caller;
	if (!yourCaller)
	{
		return "";
	}
	
	var string = yourCaller.toString().substring(9, yourFunction.caller.toString().indexOf("("));
	if (string == "" && yourCaller.funcName)
	{
		string = yourCaller.funcName;
	}
	var yourFunctionsArgs = new Array();
	for (argIndex = 0; argIndex < yourCaller.arguments.length; argIndex++)
	{
		if (typeof yourCaller.arguments[argIndex] == "undefined")
		{
			yourFunctionsArgs[yourFunctionsArgs.length] = "undefined";
			continue;
		}
		
		if (!yourCaller.arguments[argIndex])
		{
			continue;
		}
		
		if (yourCaller.arguments[argIndex].tagName)
		{
			yourFunctionsArgs[yourFunctionsArgs.length] = yourCaller.arguments[argIndex].tagName + "." + yourCaller.arguments[argIndex].id;
			continue;
		}
		
		yourFunctionsArgs[yourFunctionsArgs.length] = yourCaller.arguments[argIndex];
		
	}
	var argsString = yourFunctionsArgs.join(", ");
	string += "(" + argsString + ")";
	return string;
}

function trace(func)
{
	var count = 1;
	if (!func)
	{
		func = trace.caller;
	}
	//var traceResults = func.toString().substring(9, func.toString().indexOf("("));
	var traceResults = parseCaller(trace);
	traceResults += "(";
	var myArgs = new Array();
	for (var i = 0; i < func.arguments.length; i++)
	{
		if (func.arguments[i] == null)
		{
			var arg = "null or invalid reference";
		}
		else
		{
			switch (typeof func.arguments[i])
			{
				case "object":
					if (typeof func.arguments[i].id != "undefined")
					{
						var arg = "object." + func.arguments[i].id;
					}
					else if (func.arguments[i].type)
					{
						var arg = "object." + func.arguments[i] + "." + func.arguments[i].type; 
					}
					else if (func.arguments[i].toDateString)
					{
						var arg = "dateObj['" + func.arguments[i].toLocaleString() + "']";
					}
					else
					{
						var arg = func.arguments[i];
					}
						
				break;
				
				case "undefined":
				case "null":
					var arg = "undefined";
				break;
				
				case "string":
					var arg = "'" + func.arguments[i] + "'";
				break;
				
				case "number":
				case "boolean":
				default:
					var arg = func.arguments[i];
				break;	
			}
		}
			
		
		myArgs[myArgs.length] = arg;
	}
	traceResults += myArgs.join(", ");
	traceResults += ")";
	traceResults += "\n[" + count + "]" + parseCaller(func);
	if (func.caller)
	{
		while (func.caller.caller)
		{
			count++;
			func = func.caller;
			traceResults += "\n[" + count + "]" + parseCaller(func) + "\n";
		}
	}
		
	if (typeof traceResults == "undefined" || traceResults == "undefined")
	{
		traceResults = "Trace Failed!\n\n" + func;
	}
	displayMsg(traceResults);
}



function logTime(msg)
{
	var now = new Date();
	var index = now.getTime();
	
	if (logTime.caller)
	{
		var callerName = parseCaller(logTime.caller);
	}
	else
	{
		var callerName = "main()";
	}
	
	while (typeof globalLogVar[index] != "undefined")
	{
		index++;
	}
	
	globalLogVar[index] = callerName + " - " + msg;
}


function displayLog()
{
	//return false;
	var logContainer = document.createElement("DIV");
	
	var log = "";
	var timeStart = 0;
	var timeDiff = 0;
	var lastTime = 0;
	for (timeIndex in globalLogVar)
	{
		if (typeof globalLogVar[timeIndex] == "function")
		{
			continue;
		}
		var logEntryTag = document.createElement("DIV");
		logEntryTag.style.whitespace = "nowrap";
		if (timeStart == 0)
		{
			timeStart = timeIndex;
		}
		
		if (lastTime == 0)
		{
			lastTime = timeIndex;
		}
		
		var timeDiff = timeIndex - timeStart;
		var timeTaken = timeIndex - lastTime;
		var lastTime = timeIndex;
		var log = "\n" + timeDiff + ": "  + globalLogVar[timeIndex] + " (" + timeTaken + ")";
		var logEntry = document.createTextNode(log);
		logEntryTag.appendChild(logEntry);
		logContainer.appendChild(logEntryTag);
	}
	
	displayMsg("LOG ENTRIES");
	container = document.getElementById("ajaxResponse");
	container.innerHTML = "";
	container.appendChild(logContainer);
}
