
var common_isTraceEnabled; //XX not = false;
var common_postRun;

//******* private *********

var trace_win = "client_trace"; //XX
var gTraceMsg; //XX use buffer to append? ; !! don't initialize here



function $(id) {
	try {
		return document.getElementById(id);
	} catch(ex) {
		return null;
	}
}


function hideControl(id) {
	try {
		var ctrl = $(id);
		
		if (ctrl == null) return;
		// ctrl.style.display = "none";
		//$("LabelMaintenance").style.display = "none";
		ctrl.style.visibility = "hidden";
	} catch(ex) {
	}
}


function showControl(id) {
	try {
		var ctrl = $(id);
		
		if (ctrl == null) return;
		//ctrl.style.display = "block";
		ctrl.style.visibility = "visible";
	} catch(ex) {
	}
}


function removeItem(id) {
	try {
		var ctrl = $(id);
		
		if (ctrl == null) return;
		ctrl.style.display = "none";
	} catch(ex) {
	}
}

function restoreItem(id) {
	try {
		var ctrl = $(id);
		
		if (ctrl == null) return;
		ctrl.style.display = "block";
	} catch(ex) {
	}
}

function restoreItemInline(id) {
	try {
		var ctrl = $(id);
		
		if (ctrl == null) return;
		ctrl.style.display = "inline-block";
	} catch(ex) {
	}
}


var gHeadSwitch = true;

//XX pathguide2
function clickHeadSwitch(target) {
	//trace("clickHeadSwitch", "switch", gHeadSwitch);
try {	
	if (gHeadSwitch) {
		gHeadSwitch = false;
		removeItem(target);
		$('pathguide2').style.top = "3px";
		$('pagebody').style.top = "25px";
		$('headswitch').title = "打开主菜单";
		floatbar_run(9);
	}
	else {
		gHeadSwitch = true;
		restoreItem(target);
		$('pathguide2').style.top = "135px";
		$('pagebody').style.top = "160px";
		$(target).style.top = "-5px";
		$('headswitch').title = "合上主菜单";
		$('sideBar').style.top = "5px";
		floatbar_run(26);
	}
} catch(ex) {} 
}


function setContent(id, text) {
	try {
		if (text == "") return;
		if (id == "") {
			traceBasic("setContent", "目标 id 为空");
			return; 
		}
		$(id).innerHTML = text;  	
	} catch(ex) {
		//!! trace_win does not exist
		var obj = $(trace_win);
		if (obj != null) {			
			obj.innerHTML = composeTrace("[setContent] " + ex + 			" id:" + id + " " + text.substring(0, 5));
		} else {
			closeTrace();
		}
	}
}


function ZXCommon_appendContent(id, text) {
	try {
		//traceRun("ZXCommon_appendContent");
		//trace("ZXCommon_appendContent", "id", id);
		//trace("ZXCommon_appendContent", "text", text);

		if (text == "") return;
		if (id == "") {
			traceBasic("appendContent", "目标 id 为空");
			return; 
		}
		var old = $(id).innerHTML;
		//trace("ZXCommon_appendContent", "old", old);
		$(id).innerHTML = old + text;  	
	} catch(ex) {
		traceEx("ZXCommon_appendContent", ex)
	}
}


//********* 

/*	TRACE 
	
**********/


function openTrace() {
	common_isTraceEnabled = true;
}

function openTrace_f(flag) {
	common_isTraceEnabled = flag;
}

function closeTrace() {
	common_isTraceEnabled = false;	
}


function composeTrace(msg) {
	if (msg == "" || msg == null) return;
	
	var dt = new Date().toLocaleTimeString() + " ";
	if (gTraceMsg == null) gTraceMsg = "Trace start<br />";
	gTraceMsg += dt + msg + "<br />";
	
	return gTraceMsg;
}


function traceBasic(where, msg) {
	try {
		if (!common_isTraceEnabled) return;
		
		$(trace_win).style.display = "block";
		
		if (msg == null) msg = ""; //?? init		
		setContent(trace_win, composeTrace("[" + where + "] " + msg));
	} catch(ex) {
		setContent(trace_win, composeTrace("[traceBasic] " + ex));		
	}
}

function traceRun(where) {
	traceBasic(where, "Run...");
}

function traceEx(where, ex) {
	traceBasic(where + ".exception", ex);
}

function trace(where, key, val) {
	traceBasic(where, key + ": " + val);
}

