/***Core Functions ************************************************/
/******************************************************************/

function xGetElementById(e,doc){
	doc=((doc==null)?document:doc);
	if(typeof(e)=='string'){
		if(doc.getElementById)
			e=doc.getElementById(e);
		else if(doc.all)
				e=doc.all[e];
		else
			e=null;
	}
	return e;
}

function xAttachEvent(elem,evtStr,fxn){
	evtStr=evtStr.toLowerCase();
	if(elem.addEventListener){
		elem.addEventListener(evtStr,fxn,false);
	}else if(elem.attachEvent){
		elem.attachEvent("on"+evtStr,fxn);
	}
}

function getWinObj(objStr){
	var winObj=null;
	var tries=0,maxTries=500;

	var findWinObj=function(win,objStr){
		if(typeof(win)!='undefined'?typeof(win[objStr])!='undefined':false){
			if(win[objStr]!=null){
				return win[objStr];
			}
		}
		if(win.frames.length>0){
			for(var i=0;i<win.frames.length;i++){
				if(typeof(win.frames[i])!='undefined'?typeof(win.frames[i][objStr])!='undefined':false){
					if(win[objStr]!=null){
						return win.frames[i][objStr];
					}
				}
			}
		}
		return null;
	};

	while((tries<maxTries)&&(winObj==null)){
		winObj=findWinObj(window,objStr);
		if((winObj==null)&&(typeof(window.parent)!='undefined')){
			winObj=findWinObj(window.parent,objStr);
		}
		if((winObj==null)&&(typeof(window.top)!='undefined')){
			winObj=findWinObj(window.top,objStr);
		}
		if((winObj==null)&&(typeof(window.opener)!='undefined')){
			if((window.opener!=null)&&(!window.opener.closed)){
				winObj=findWinObj(window.opener,objStr);
			}
		}
		tries++;
	}
	return winObj;
}

/*** Debugger Console *********************************************/
/******************************************************************/

function DebugConsole(title){
	this.startTime=new Date();
	this.messages=new Array();
	this.scrollLock=false;
	this.title=title;
	this.registerWindow(window);
}

DebugConsole.prototype.registerWindow=function(win){
	var dbgc_ptr=this;
	xAttachEvent(((win.addEventListener)?win:win.document),"keydown",function(evt){dbgc_ptr.activateEvtWrapper(evt,dbgc_ptr);});
}

DebugConsole.prototype.debugMsg=function(msg,classid){
	this.messages[this.messages.length]=new Array(this.date2timeStamp(new Date()),msg,((classid==null)?0:classid));
	this.printMsg(this.messages[this.messages.length-1]);
}

DebugConsole.prototype.debugMsgValue=function(n,v,classid){
	this.debugMsg(n+":="+v,classid);
}

DebugConsole.prototype.date2timeStamp=function(dte){
	var hh=dte.getHours();
	var mm=dte.getMinutes();
	mm=((mm<10)?"0":"")+mm;
	var ss=dte.getSeconds();
	ss=((ss<10)?"0":"")+ss;
	var mss=dte.getMilliseconds();
	mss=((mss<100)?"0":"")+mss;
	mss=((mss<10)?"0":"")+mss;
	return hh+":"+mm+":"+ss+"."+mss;
}

DebugConsole.prototype.displayDebugWin=function(){
	try{
		if((!this.debugWin)||(this.debugWin.closed)){
			this.debugWin=window.open("","DebugPanel","width=500,height=300,location=no,menubar=yes,status=no,toolbar=no,scrollbars=yes,resizable=yes");
			this.debugWin.document.open();
			this.debugWin.document.write("<head>");
			this.debugWin.document.write("<title>"+this.title+"</title>");
			this.debugWin.document.write("<style>body{font-family:verdana,helvetica,sans-serif;font-size:10px;}table{font-family:verdana,helvetica,sans-serif;font-size:10px;}</style>");
			this.debugWin.document.write("</head>");
			this.debugWin.document.write("<body></body>");
			this.debugWin.document.close();
			this.clearConsole();
			var dbgc_ptr=this;
			xAttachEvent(((this.debugWin.addEventListener)?this.debugWin:this.debugWin.document),"keydown",function(evt){dbgc_ptr.keydownEvtWrapper(evt,dbgc_ptr);});
		}else{
			try{
				this.debugWin.focus();
			}catch(e){}
		}
	}catch(e){
		alert("Please disable any pop-up block utilites for this application and try again.");
	}
}

DebugConsole.prototype.toggleScrollLock=function(){
	this.scrollLock=!this.scrollLock;
	try{
		var elem=xGetElemtentById('scrlck',this.debugWin.document);
		elem.innerText=((this.scrollLock)?"on":"off");
		this.debugWin.document.getElementById("scrlck").innerHTML=((this.scrollLock)?"on":"off");
	}catch(e){}
}

DebugConsole.prototype.clearConsole=function(classId){
	this.acClassId=classId;
	var consoleHTML="";
	consoleHTML+="<div>Technomedia International, Inc. Copyright 2004-2007</div>";
	consoleHTML+="<div style=\"color:gray;\">"+new Date()+"</div>";
	consoleHTML+="<div id=\"instrBlock\" style=\"color:blue;padding:10px;\">";
		consoleHTML+="<div>[a] - set trace for all messages</div>";
		consoleHTML+="<div>[0-9] - set trace level"+((classId==null)?"":" &lt;"+((classId==-1)?"all":classId)+"&gt;")+"</div>";
		consoleHTML+="<div>[p] - scrollLock &lt;<span id=\"scrlck\">"+((this.scrollLock)?"on":"off")+"</span>&gt;</div>";
		consoleHTML+="<div>[c] - clear console</div>";
	consoleHTML+="</div>";
	consoleHTML+="<hr/>";
	consoleHTML+="<table><tbody></tbody></table>";
	try{
		this.debugWin.document.getElementsByTagName("body").item(0).innerHTML=consoleHTML;
	}catch(e){}
}

DebugConsole.prototype.activateMessages=function(classId){
	this.clearConsole(classId);
	for(var i=0;i<this.messages.length;i++){
		this.printMsg(this.messages[i]);
	}
}

DebugConsole.prototype.printMsg=function(arrMsg){
	if((this.debugWin)&&(!this.debugWin.closed)){
		if((this.acClassId==arrMsg[2])||(this.acClassId==-1)){
			var classFormat="0"
			try{
				var d=this.debugWin.document;
				var tableElem=d.getElementsByTagName("tbody").item(0);
				var newRow=d.createElement("tr");
				var newCol=d.createElement("td");
				newCol.vAlign="top";
				newCol.noWrap="noWrap";
				var txtNode=d.createTextNode(arrMsg[0]+"["+(classFormat.substr(0,classFormat.length-(""+arrMsg[2]).length))+arrMsg[2]+"]");
				newCol.appendChild(txtNode);
				newRow.appendChild(newCol);
				var newCol=d.createElement("td");
				var txtNode=d.createTextNode(arrMsg[1]);
				newCol.appendChild(txtNode);
				newRow.appendChild(newCol);
				tableElem.appendChild(newRow);
				if(!this.scrollLock){
					newRow.scrollIntoView(true);
				}
			}catch(e){}
		}
	}
}

DebugConsole.prototype.activateEvtWrapper=function(evt,dbgc_ptr){
	if(!evt){
		evt=window.event;
	}
	if((evt.keyCode==68)&&(evt.ctrlKey)&&(evt.altKey)){
		try{
			dbgc_ptr.displayDebugWin();
		}catch(e){}
	}
}

DebugConsole.prototype.keydownEvtWrapper=function(evt,dbgc_ptr){
	if(!evt){
		evt=window.event;
	}
	switch(evt.keyCode){
		case 67:
			dbgc_ptr.clearConsole();
			break;
		case 65:
			dbgc_ptr.activateMessages(-1);
			break;
		case 80:
			dbgc_ptr.toggleScrollLock();
			break;
		case 116:
			evt.cancelBubble=true;
			break;
		default:
			if((evt.keyCode>=48)&&(evt.keyCode<=57)){
				dbgc_ptr.activateMessages(evt.keyCode-48);
			}
	}
	dbgc_ptr.debugWin.focus();
}