// ========================== Begin Debug Section ==========================================
// Debug stuff
var debugEnabled = false ;
var debugWindow = null;
var pNode =null;
var markColor = "red";
var normalColor = "lime";
var lineCount = 0;
var maxLineCount = 1000;			// will start from 0 again, after hitting maxium.

function enableDebug() {

	if (debugEnabled && !debugWindow.closed) return ;

	debugEnabled = true ;
	//alert("ebable debugging");
	var features = "width=" + screen.width *2/7 + "," +
				"height=" + screen.height*9/10 + "," +
				"top=0," +
				"left=" + ((screen.width*5/7) - 10) + "," +
				"resizable=yes," +
				"scrollbars=yes," +
				"alwaysRaised=yes";

	debugWindow = window.open("", "debug", features);

	//var d = debugWindow.document;
	debugWindow.document.writeln("<HEAD><TITLE>Javascript Debugger</TITLE></HEAD>");
	debugWindow.document.writeln('<BODY style="background-color: black; color:' + normalColor + '" >');
//	debugWindow.document.writeln('<P id="debug_p" style="font-family: courier; font-size: 12px; ' +
//		'margin: 0px">' +
//		'**** Start Script Debugging ****<br>');
	debugWindow.document.writeln("</body></html>");
	pNode = debugWindow.document.createElement("P");
	pNode.setAttribute("id", "debug_p");
	pNode.style.margin = "0px";
	pNode.style.fontSize = "12px";
	pNode.style.fontFamily = "courier";
	pNode.appendChild(debugWindow.document.createTextNode("**** Start Script Debugging ****"));
	debugWindow.document.body.appendChild(pNode);

	// move the calling window
	//window.moveTo(0, 0);
	//window.resizeTo(screen.width*5/7 - 10, screen.height*9/10);

}

function disableDebug() {

	if (!debugEnabled) return;

	debugEnabled = false;
	debugWindow.close();
}

function debug(msg, mark) {
	if (debugEnabled) {
		if (debugWindow.closed) {
			enableDebug();
		}
		debugWindow.scrollBy(0,100);
		//if (pNode == null) pNode = debugWindow.document.getElementById("debug_p");
		var tNode = debugWindow.document.createTextNode(lineCount + ":" + msg);
		if (++lineCount >= maxLineCount) lineCount = 0 ;
		var newPara = pNode.cloneNode(false);
		newPara.appendChild(tNode);
		if (mark == true) {
			newPara.style.color = markColor;
		} else {
			if (mark != undefined) newPara.style.color = mark;
		}
		debugWindow.document.body.appendChild(newPara);
	}
}
// ========================== End Debug Section =========================================