// Licensed Materials - Property of IBM
//
// IBM Cognos Products: pps
//
// (C) Copyright IBM Corp. 2005, 2017
//
// US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
// Debug Library (optional)

function Debugger () {

	window.onerror = function (msg,url,line){ alert("Line " + line + ": " + msg); return true; };

	//Member assignment...
	this.debugWin = open("", "debugWindow", "directories=no,hotkeys=no,innerHeight=500,innerWidth=300,location=no,menubar=yes,screenX=0,screenY=0,scrollbars=yes,resizeable=yes,status=no,titlebar=no,toolbar=no");
	this.maxDebugLength = 100;
	this.startTime = new Date();
	this.level = 1;
	if (typeof pdbSettingLevel == "number")
		this.level = pdbSettingLevel;

	this.dumpObjects = false;
	if (typeof pdbSettingObjectDump == "boolean")
		this.dumpObjects = pdbSettingObjectDump;

	this.appendOutput = false;
	if (typeof pdbSettingAppendOutput == "boolean")
		this.appendOutput = pdbSettingAppendOutput;

	this.fontSize = 1;
	if (typeof pdbSettingFontSize == "number")
		this.fontSize = pdbSettingFontSize;

	//Function assignment...
	this.out = pdbPrintMsg;
	this.dump = pdbDumpObject;
	this.dumpArgs = pdbDumpArguments;
	this.getObjectName = pdbGetObjectName;
	this.getFunctionName = pdbGetFunctionName;
	this.checkHide = pdbCheckHide;


	if (!this.appendOutput)
	{
		this.debugWin.document.close();
		this.debugWin.document.open();
	}

	if (!this.debugWin.document.title || this.debugWin.document.closed)
	{
		this.debugWin.document.write("<HTML><HEAD><TITLE>PPWeb Debug</TITLE></HEAD><BODY>");
		this.debugWin.document.write("<FONT SIZE=" + this.fontSize + "><B>*** Start ***</B><BR><UL><LI>");
	}
	else
	{
		this.debugWin.document.write("</UL><BR><B>*** Start ***</B><BR><UL><LI>");
	}		
}

//DON'T call ANY of these directly...
//call pdb.[func name] (see above)

function pdbPrintMsg() {

//  this is a variable argument function called through pdb.out()
//  it is used to write to the debug window
//	suitable calls:
//
// 	 pdb.out("This is a message")	>> simply prints the message...
//	 pdb.out(arguments);		>> "call stack" type of message...identifies the calling func and its arguments
//	 pdb.out(arguments, "Another message") or pdb.out("Another message", arguments);
//	 pdb.out(object);	>>	calls the toString method of the object
//	 pdb.out(property);	>>	prints the property name and value;

//	 Also, if the last argument is a number, it is interpreted as the "message level".
//		if logging level >= message level
//			print message
//		else
//			skip it
//	 If the message level is omitted, it is assumed to be 1 (ie. "terse" message...always present)

	var msg = "";	
	var indent = false;
	var numArgs = arguments.length;
	if (numArgs <= 0) {
		return;
	}

	if (typeof arguments[numArgs-1] == "number") {
		if (this.level < arguments[numArgs-1])
			return;
	}

	for (var i = 0; i < numArgs; i++)
	{
		var arg = arguments[i];
		var argType = typeof arg;
		
		if (argType	== "string") 
		{
			msg += arg + "<br>";
		}
		
		else if (argType == "object")
		{
			if (typeof arg.callee != "undefined" && typeof arg.length != "undefined")
			{
				msg += this.getFunctionName(arg.callee) + " ( ";
				if (arg.length > 0)
					msg += this.dumpArgs(arg);
				msg += " ) ";
			}
			else
			{
				msg = arg.toString();
			}
		}
		else if (argType == "number" && (i+1 != numArgs)) 
		{
			msg += "\n" + arg.toString();
		}

		if ( i+2 < numArgs || ( i+1 == numArgs && argType != "number") )
		{
			if (!indent)
			{
				msg += "<UL>";
				indent = true;
			}
			msg += "<LI>";
		}
	}
	
	if (indent)
		msg += "</LI></UL>";

	var newTime = new Date();
	var timeOut = newTime.getHours() + ":" + newTime.getMinutes() + "." + newTime.getSeconds() + ":" + newTime.getMilliseconds();
	if (msg) {
		this.debugWin.document.write(timeOut + ": " + msg + "\n<LI>");
	//	this.debugWin.scrollBy(0,100);
	}

	line = msg;
	if (line.length > this.maxDebugLength)
		line = "..." + line.substring(line.length - this.maxDebugLength);
}

function pdbDumpArguments (args) {
	var result = "";

	for (var i = 0; i < args.length; i++)
	{
		var arg = args[i];
		var argType = typeof arg;
		
		if (argType == "string" || argType == "number")
		{
			result += arg;
		}
		else if (argType == "object")
		{
			if (this.dumpObjects)
			{
				result += this.dump(arg);
			}
			else
				result += arg;
		}
		else
		{
			result += "<unknown function parameter>";
		}

		if (i+1 < args.length)
			result += ", ";
	}

	return result;
}

function pdbDumpObject (obj) {

	var result = this.getObjectName( obj ) + " { <br>";

	for (var prop in obj)
	{
		result += "variable: " + prop + ", value: " + obj[prop] + "<br>";
	}

	return result;
}

function pdbGetObjectName (obj) {

	if (typeof obj.constructor == "undefined")
		return "Object";

	return this.getFunctionName( obj.constructor );
}

function pdbGetFunctionName (func) {
	var funcBody = func.toString();
	var funcName = funcBody.substring(funcBody.indexOf(" "), funcBody.indexOf("{") );
	funcName = funcName.substring(0,funcName.indexOf("("));

	return funcName;
}


function pdbCheckHide() {
	for (n in _menus) {
		menu = _menus[n];
		if (menu._active) {
			var str = menu._id + ": Still Active !!";
			debug(str);
			menu._active = false;
		}
	}
}