
function console_htmlEntities(texto)
{
	//by Micox - elmicoxcodes.blogspot.com - www.ievolutionweb.com
	var i, carac, letra, novo='';
	for(i=0; i < texto.length; i++) {
		carac = texto[i].charCodeAt(0);
		if( (carac > 47 && carac < 58) || (carac > 62 && carac < 127) ) {
			//se for numero ou letra normal
			novo += texto[i];
		} else {
			novo += "&#" + texto[i].charCodeAt(0) + ";";
		}
	}
	return novo;
}

function console_create_window(url_prefix)
{
	var consolewindow = window.open('', 'errorConsole', 'resizable=yes,scrollbars=yes,directories=no,location=no,menubar=yes,status=yes,toolbar=yes');

	consolewindow.document.open();
	
	consolewindow.document.writeln('<html>\n' +
'		<head>\n' +
'			<title>Debug Console</title>\n' +
'			<link href="' + url_prefix + '/css/errorConsole.css" rel="stylesheet" type="text/css" />\n' +
'			<script type="text/javascript" src="' + url_prefix + '/js/errorConsole.js"><' + '/script>\n' +
'			</head>\n' + 
'		<body>\n');
	return(consolewindow);
}

function console_add_message(consolewindow, error)
{
	consolewindow.document.writeln(
'			<div class="Item">\n' +
'				<h1 onclick="toggle_next_by_class(this.firstChild, \'Details\', this);">' +
					'<span class="Toggle">+</span>\n' +
'					<span class="Time">' + error['Time'] + '</span>\n' +
'					<span class="Memory">' + error['Memory'] + '</span>' +
'					<span class="Level ' + error['Level'] + '">' + error['Level'] + '</span>\n' +
(error['Count']?'<span class="Count">' + error['Count'] + ' times:</span>\n':'') +
'					<span class="Message">' + console_htmlEntities(error['Message']) + '</span>\n' +
'					<span class="File">' + console_htmlEntities(error['File']) + '</span>\n' +
'					<span class="Line">' + error['Line'] + '</span></h1>\n' +
'				<div class="Details">\n' +
'					<h2 onclick="toggle_next_by_class(this.firstChild, \'Context\', this);">' + 
						'<span class="Toggle">-</span>\n' +
'						Context</h2>\n' +
'					<div class="Context" style="display: block;">' +
console_htmlEntities(error['Context']) +
'</div>\n' +
'					<h2 onclick="toggle_next_by_class(this.firstChild, \'Backtraces\', this);">' +
						'<span class="Toggle">+</span>\n' +
'						Backtrace</h2>\n' +
'					<div class="Backtraces">\n' +
		'');

	var j;
	for(j in error['Backtrace']) {
		var bt = error['Backtrace'][j];
		if(typeof bt != 'object') {
			continue;
		}
		
		if(bt['ContextCode']) {
			consolewindow.document.writeln(
'						<div class="Backtrace">\n' +
'							<h3 class="expandable" onclick="toggle_next_by_class(this.firstChild, \'BacktraceCode\', this);">' +
								'<span class="Toggle">+</span>\n' +
'								<span class="Function">' + console_htmlEntities(bt['Function']) + '</span>\n' +
'								<span class="File">' + bt['File'] + '</span>\n' +
'								<span class="Line">' + bt['Line'] + '</span>\n' +
'							</h3>\n' +
'							<div class="BacktraceCode">' +
				'');

			var k;
			for(k in bt['ContextCode']) {
				if(typeof bt['ContextCode'][k] != 'string') {
					continue;
				}
				consolewindow.document.writeln(
(k == bt['Line'] ? '<strong>' : '') +
k + ': ' + console_htmlEntities(bt['ContextCode'][k]) + '' +
(k == bt['Line'] ? '</strong>' : '') +
					'');
			}

			consolewindow.document.writeln(
'							</div>\n' +
'						</div>\n' +
				'');
		} else {
			consolewindow.document.writeln(
'						<div class="Backtrace">\n' +
'							<h3>' +
'								<span class="Function">' + console_htmlEntities(bt['Function']) + '</span>\n' +
'								<span class="File">' + bt['File'] + '</span>\n' +
'								<span class="Line">' + bt['Line'] + '</span>\n' +
'							</h3>\n' +
'						</div>\n' +
				'');
		}

	}

	consolewindow.document.writeln(
'						</div>\n' +
'					</div>\n' +
'				</div>\n' +
		'');
}

function console_end(consolewindow)
{
	consolewindow.document.writeln('</body>' + '</html>');
	consolewindow.document.close();
}

