/**
 * debugFunctions.js
 *
 * This file contains a collection of debug functions for javascript.
 *
 * This source file is subject to version 2.1 of the GNU Lesser
 * General Public License (LPGL), found in the file LICENSE that is
 * included with this package, and is also available at
 * http://www.gnu.org/copyleft/lesser.html.
 * @package     Javascript
 *
 * @author      Dieter Raber <dieter@dieterraber.net>
 * @copyright   2004-12-27
 * @version     1.0
 * @license     http://www.gnu.org/copyleft/lesser.html
 *
 */

/**
 * TOC
 *
 * - getObjectProperties
 * - print_ob
 * - alert_ob
 * - window_ob
 *
 * - format_r
 * - print_r
 * - alert_r
 * - window_r
 */

var $recursiveCount = 0;

/**
 * showProps
 *
 * Shows the properties of an given object
 *
 * object object
 * return array
 *
 * Use print_ob(), alert_ob() or window_ob() to display the result
 */
function getObjectProperties($item, $showFunctions, $extraStr) {
	if (!$showFunctions) { $showFunctions = false; }

	var $retVal = '';
	try {
		if (!$extraStr) { $extraStr = ''; }

		for (var $prop in $item) {
			if (!$showFunctions && $item[$prop].constructor == Function) { continue; }

			var $retValArr = '';
			if (typeof($item[$prop]) == 'object' && $recursiveCount != 10) {
				$recursiveCount++;
				$retValArr += "  { \n"+ getObjectProperties($item[$prop], $showFunctions, $extraStr + "\t") + $extraStr + "}\n";
			}
			$retVal += $extraStr + $prop + ' => ' + $item[$prop] + $retValArr;
			$retVal += "\n";
		}
	} catch ($exptn) {}
	return $retVal;
}




/**
 * showProps
 *
 * Shows the properties of an given object
 *
 * object object
 * return array
 *
 * Use print_ob(), alert_ob() or window_ob() to display the result
 */
function getUserFunctions() {
	var $retVal = '';
	for (var $prop in document) {
		if (document[$prop].constructor == Function) {
			$retVal += $prop + ' => ' + document[$prop] + "\n";
		}
	}
	return document;
}

/**
 * print_ob(), alert_ob(), window_ob()
 *
 * These three functions are different ways to display the result of getObjectProperties()
 * print_ob uses document.write and can hence only be called onload
 * alert_ob displays the result in an alert box
 * window_ob opens a popup window and writes the results there
 */
function alert_ob($expr, $showFunctions) {
	$recursiveCount = 0;
	alert(getObjectProperties($expr, $showFunctions))
}

function window_ob($expr, $showFunctions) {
	$recursiveCount = 0;
	var $win = window.open('', 'format','width=400,height=300,left=50,top=50,status,menubar,scrollbars,resizable');
	$win.document.open();
	$win.document.write('<pre>' + getObjectProperties($expr, $showFunctions) + '</pre>');
	$win.document.close()
	$win.focus();
}

function print_ob($expr, $divElm, $showFunctions) {
	$recursiveCount = 0
	if (typeof($divElm) == 'object') {
		$divElm.innerHTML += '<pre>' + getObjectProperties($expr, $showFunctions) + '</pre><br>';
	} else {
		document.write('<pre>' + getObjectProperties($expr, $showFunctions) + '</pre>');
	}
}


/**
 * format_r
 *
 * Returns human-readable information about a variable
 *
 * format_r() returns information about a variable in a way that's readable by humans.
 * If given a string, integer or float, the value itself will be printed.
 * If given an array, values will be presented in a format that shows keys and elements.
 *
 * example:
 *   test = new Array ('foo', 'bar', 'foobar')
 *   format_r(test)
 *   returns: Array
 *            {
 *                 [0] => foo
 *                 [1] => bar
 *                 [3] => foobar
 *            }
 *
 */
function format_r($expr) {
	var $dim    = 0;
	var $padVal = '\xA0\xA0\xA0\xA0\xA0';
	var $retVal = '';

	switch(typeof $expr) {
		case 'string':
		case 'number':
			$retVal = $expr;
		break;

		case 'object':
			$retVal = 'Array\n{\n' + outputFormat($expr, $dim) + '\n}';
		break;

		default:
			$retVal = false;
		break;
	}

	//Inline function
	function pad($dim) {
		var $padding = '';
		for ($i = 0; $i < $dim; $i++) {
			$padding += $padVal;
		}
		return $padding;
	}

	//Inline function
	function outputFormat($expr, $dim) {
		var $retVal = '';

		for (var $key in $expr) {
			if (typeof $expr[$key] == 'object' && $expr[$key].constructor == Array) {
				$retVal += $padVal + pad($dim) + '[' + $key + '] => Array\n'
						 + $padVal + pad($dim) + '{\n'
						 + outputFormat($expr[$key], $dim + 1) + $padVal + pad($dim) + '}\n';
			} else if ($expr[$key].constructor == Function) {
				continue;
			} else {
				$retVal = $retVal + $padVal + pad($dim) + '[' + $key + '] => ' + $expr[$key] + '\n';
			}
		}
		return $retVal;
	}
	return $retVal;
}

/**
 * print_r(), alert_r(), window_r()
 *
 * These three functions are just different ways to display the result of format_r()
 * print_r uses document.write and can hence only be called onload
 * alert_r displays the result in an alert box
 * window_r opens a popup window and writes the results there
 */
function alert_r($expr) {
	alert(format_r($expr))
}

function window_r($expr) {
	var $win = window.open('', 'format','width=400,height=300,left=50,top=50,status,menubar,scrollbars,resizable');
	$win.document.open();
	$win.document.write('<pre>' + format_r($expr) + '</pre>');
	$win.document.close()
	$win.focus();
}

function print_r($expr, $divElm) {
	if (typeof($divElm) == 'object') {
		$divElm.innerHTML += '<pre>' + format_r($expr) + '</pre><br>';
	} else {
		document.write('<pre>' + format_r($expr) + '</pre>');
	}
}

/**
 * Opent een popup om een bug te rapporteren
 * @param String module De module
 * @param String page De pagina
 * @param String request De aanroep GET | POST
 */
function reportBug(module, page, request) {
	window.open('?m=Bug&module='+module+'&page='+page+'&request='+request, 'bugWindow', 'location=no, toolbar=no, menubar=no, status=no, resizable=yes, width=400, height=750, left=300, top=150');
}
