
var debugWin;
var debugFilePath=codesFilesUrl+"debug/debug.htm"
var runtimeClientMode="DEBUG";
var tempLogsStr="",tempErrorsStr=""
var debugWinOpened=false;

function openDebugWin(){
	try{
		if (typeof window.showModalDialog != 'undefined'){
		    var debugWinFeatures = "dialogHeight:442px;\
								    dialogWidth:507px;\
								    dialogTop:10px;\
								    dialogLeft:400px;\
								    center:no;\
								    status:no;\
								    scroll:no;\
								    resizable:no;";
    		debugWin=window.showModelessDialog(debugFilePath,self,debugWinFeatures);
		}
		else{
			//netscape.security.PrivilegeManager.enablePrivilege('UniversalBrowserWrite');
			//var params = 'a|b|c';
			var width = "507";
			var height= "442";
			var sWinFeat = "width=" +width+ ",height=" +height+ ","
			sWinFeat += "screenY="+((screen.availHeight-height)/2)+","; 
            sWinFeat += "screenX="+((screen.availWidth-width)/2)+","; 

		    debugWin=window.open(debugFilePath, 'debugWin',  sWinFeat +'chrome,centerscreen=YES,dependent=YES, dialog=YES,modal=NO,resizable=YES,scrollbars=NO, location=0,status=0,menubar=0,toolbar=0');
		}
	}catch(e){
	    alert(e)
		runtimeClientMode="USER";
	}
}
function dbgL(msg){
	if(runtimeClientMode!="DEBUG") return;
	try{
		if(!debugWinOpened)
			tempLogsStr+=("-"+msg+"<br>");
		else
			debugWin.document.all['logs_cont'].innerHTML+=("-"+msg+"<br>");
	}
	catch(e){}
}
function dbgE(msg){
	if(runtimeClientMode!="DEBUG") return;
	try{
		if(!debugWinOpened)
			tempErrorsStr+=("-"+msg+"<br>");
		else
			debugWin.document.all['errors_cont'].innerHTML+=("-"+msg+"<br>");
	}
	catch(e){}
}
/*
//document.addEventListener("keyup", aaa, false);
document.attachEvent("onkeyup", aaa);
function aaa() { //F9
alert(111)
	if(event.keyCode==120) openDebugWin();
}
*/
document.onkeydown = function(e) { //F9
	var code;
	if (!e) var e = window.event
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
	//alert(code)
	if(code==120) openDebugWin();
}
/*****************************************************************************/
//for the click on the debug div
var siteTreeDivArr=[];
function handleDebugDiv(obj){
	if(obj.style.borderStyle=='outset' || obj.style.borderStyle=='groove'){
		obj.style.borderStyle='inset'
		obj.style.backgroundColor = "#DFEC42"
		obj.innerHTML =_handleDebugDiv(obj,0)
		//obj.innerHTML =siteTreeDivArr[obj.id]
	}
	else{
		obj.style.borderStyle='outset'
		obj.style.backgroundColor = "yellow"
		obj.innerHTML =obj.innerHTML.replace(/\&lt;/gi,"<").replace(/\&gt;/gi,">").replace(/((\<br s="1"\>)?(&nbsp;&nbsp;&nbsp;)*)/gi,"")
	}
}
var msg=[]
function _handleDebugDiv(obj,depth){
	var str = ""
	var _f=false
	var _nodeName = ""
	var tabStr="";
	for(var j=0;j<depth;j++){
		tabStr+="&nbsp;&nbsp;&nbsp;"
	}

	var nodeList = obj.childNodes;
	if (nodeList!=null) {
		for (var i=0; i<nodeList.length; i++){ 
			if(nodeList[i].nodeName!="#text"){
				if(nodeList[i].nodeName.toLowerCase()=="br")
					str+= "<br>"
				else{
					/*
						msg[msg.length] = "<td><hr>" + nodeList[i].nodeName +"<br>"
						var msg2=[]
						for(var c in nodeList[i]){
							msg2[msg2.length]="<br> "+ c +":" + nodeList[i][c] 
						}
						msg[msg.length]=msg2.join() + "</td>"
					*/
					str+=((depth>0)?"<br s='1'>":"") +tabStr+(_f?tabStr:"")+ "&lt;" +nodeList[i].nodeName+"&gt;"
					if(nodeList[i].canHaveChildren){
						if(nodeList[i].hasChildNodes()){
							str+=_handleDebugDiv(nodeList[i],depth+1)+"<br s='1'>" +tabStr
						}
						_f=false
						str+="&lt;/" +nodeList[i].nodeName+ "&gt;"
					}
					else{
						_f=true
					}
				}
			}
			else{
				str+="<br s='1'>" +tabStr+(_f?tabStr:"")+ nodeList[i].nodeValue
			}
		}		
		//msg.sort()
		//document.write("<table><tr>" + msg.join() + "</tr></table>")
	}
	return(str)
}
/*****************************************************************************/
var currentTab=0

function _handleDebugDivNEW(obj){
	var str = ""
	var _pop=false
	function _getTab(){
		var tabStr="";
		for(var j=0;j<currentTab;j++){
			tabStr+="&nbsp;&nbsp;&nbsp;"
		}
		return tabStr
	}
	var nodeList = obj.childNodes;
	if (nodeList==null) return
	var stack=new Array()
	for (var i=0; i<nodeList.length; i++){ 
		if(nodeList[i].nodeName!="#text"){
			
			if(nodeList[i].canHaveChildren){
				//print real node:
				currentTab++
				str+="<br>" +_getTab()+ "&lt;" +nodeList[i].nodeName+"&gt;"
				if(nodeList[i].hasChildNodes()){
					str+=_handleDebugDiv(nodeList[i],depth+1)
				}
				currentTab--
				str+="<br>" +_getTab()+"&lt;/" +nodeList[i].nodeName+ "&gt;"
			}
			else{
				//print unreal node:
				if(nodeList[i].nodeName.indexOf("/")==-1){
					if(!_pop) currentTab++
					_pop=false
					stack.push(nodeList[i].nodeName)
					str+="<br>" + _getTab()+"&lt;" +nodeList[i].nodeName+"&gt;"
				}
				else{
					_pop=true
					stack.pop()
					if(("/"+stack[stack.length-1])==nodeList[i].nodeName) currentTab--
					str+="<br>" +_getTab()+ "&lt;" +nodeList[i].nodeName+"&gt;"
				}
				
			}
		}
		else{
			//print text
			//str+="<br>" + nodeList[i].nodeValue
		}
	}
	
	return(str)
}
