caseXmlTag   = 1; // 0:大小寫不變 1:小寫字母 2:大寫字母
isCompactXML = 0; // 0:完整XML 1:精簡XML 2:絕對精簡XML

str2htmlAy = new Array (
	["&",     "#amp;"],
	["#amp;", "&amp;"],
	["<",     "&lt;"],
	[">",     "&gt;"],
	[" ",     "&nbsp;"]
);

isIE  = (document.all) ? 1 : 0;
isIE6 = (isIE) ? ((navigator.userAgent.toLowerCase().indexOf('msie 6')!=-1) ? 1 : 0) : 0;
isIE7 = (isIE) ? ((navigator.userAgent.toLowerCase().indexOf('msie 7')!=-1) ? 1 : 0) : 0;
isFF2 = (isIE) ? 0 : ((navigator.userAgent.toLowerCase().indexOf('firefox/2')!=-1) ? 1 : 0);
isFF3 = (isIE) ? 0 : ((navigator.userAgent.toLowerCase().indexOf('firefox/3')!=-1) ? 1 : 0);

//==================================================
// 字串置換。
//
window.strReplace_ = function (s, f, t) { // function begin
	while (s.indexOf(f) >= 0) s = s.replace(f, t);
	return s;
} // function end

//==================================================
// 將 \r\n 轉為 <!--crlf--> // <crlf/>
//
window.trimCRLF_ = function (s) { // function begin
	s = strReplace_(s, '\r', '');
	s = strReplace_(s, '\n', '<!--crlf-->'); // s = strReplace_(s, '\n', '<crlf/>');
	return s;
} // function end

//==================================================
// 將字串轉換 str2htmlAy 所列的特殊字元為 html code。
//
window.str2html_ = function (s) { // function begin
	s = strReplace_(s, "\r", "");

	var a = s.split('\n');
	var n = a.length;
	s = "";
	for (var i=0; i<n; i++) {
		s += a[i];
		if (i < n-1)
		if (a[i].charAt(a[i].length-1) == '+')
			s += "''+";
		else if (a[i].charAt(a[i].length-1) == ';')
			s += ';;';
		else if (a[i].charAt(a[i].length-1) == '>')
			if (a[i].indexOf('<script ')==0 && a[i].indexOf('</script>')<0)
				s += 'var crlf=1;';
			else
				s += '<!--crlf-->'; // s += '<crlf/>';
		else
			s += '<br/>';
	}

	for (var i=0; i<str2htmlAy.length; i++) s = strReplace_(s, str2htmlAy[i][0], str2htmlAy[i][1]);

	return s;
} // function end

//==================================================
//  將 html code 轉換 str2htmlAy 所列的特殊字元為字串。
//
window.html2str_ = function (s) { // function begin
	if (!s) return "";
	//for (var i=str2htmlAy.length-1; i>=0; i--)
		//s = strReplace_(s, str2htmlAy[i][1], str2htmlAy[i][0]);
	s = strReplace_(s, "\r", "");
	s = strReplace_(s, "\n", "");
	s = strReplace_(s, "<br/>",   ((isIE) ? "\r\n" : "\n"));
	s = strReplace_(s, "<!--crlf-->", ((isIE) ? "\r\n" : "\n")); // s = strReplace_(s, "<crlf/>", ((isIE) ? "\r\n" : "\n"));
	s = strReplace_(s, "var crlf=1;", ((isIE) ? "\r\n" : "\n"));
	s = strReplace_(s, ";;;",     ((isIE) ? ";\r\n" : ";\n"));
	s = strReplace_(s, "+''+",    ((isIE) ? "+\r\n" : "+\n"));
	if (s == " ") s = "";
	return s;
} // function end

//==================================================
// 回報 em 是否有 child？
//
window.hasChild_ = function (em) { // function begin
	var childAy = em.childNodes;
	for (var i=0; i<childAy.length; i++)
		if (childAy[i].nodeType == 1) return true;
	return false;
} // function end

//==================================================
// 讀取 node 的 text value
//
window.getText_ = function (dom) { // function begin
	if (dom.nodeType == 3) return dom.data;
	var childAy = dom.childNodes;
	for (var i=0; i<childAy.length; i++) if (childAy[i].nodeType == 3)
		return childAy[i].data;
	return "";
} // function end

//==================================================
// 同 document.getElementById
//
window.emid_ = function (idname) { // function begin
	return document.getElementById(idname);
} // function end

//==================================================
// 顯示 idname 的元件
//
window.disp_ = function (idname) { // function begin
	var idAy = idname.split(',');
	for (var i=0; i<idAy.length; i++)
	if (emid_(idAy[i])) emid_(idAy[i]).style.display = '';
} // function end

//==================================================
// 隱藏 idname 的元件
//
window.hide_ = function (idname) { // function begin
	var idAy = idname.split(',');
	for (var i=0; i<idAy.length; i++)
	if (emid_(idAy[i])) emid_(idAy[i]).style.display = 'none';
} // function end

window.ShowForm_ = function (w, h, url, name) { // function begin
	var WinW = w, WinH = h;
	var ScrW = window.screen.availWidth;
	var ScrH = window.screen.availHeight;
	var x = (ScrW - WinW) / 2;
	var y = (ScrH - WinH) / 2;
	var w = window.open(url, name, "scrollbars=yes,left="+x+",top="+y+",width="+WinW+",height="+WinH);
	w.focus();
} // function end

window.setAllChkbox_ = function (is1, InputName) { // function begin
	if (!InputName) InputName = 'TransXmlID[]';
	var InputAy = document.getElementsByTagName("input");
	for (var i=0; i < InputAy.length; i++)
	if (InputAy[i].type=="checkbox" && InputAy[i].name==InputName)
		InputAy[i].checked = is1;
} // function end

window.chkEmpty_ = function (id, msgEmpty) { // function begin
	var idAy = id.split(','), notEmpty=0;
	for (var i=0; i<idAy.length; i++) {
		id = document.getElementById(idAy[i]);
		if (id) notEmpty = notEmpty || (id.value!="");
	}
	if (!notEmpty) {
		alert(msgEmpty); document.getElementById(idAy[0]).focus(); return false;
	}
	return document.getElementById(idAy[0]);
} // function end

//==================================================
// 取 ctrl 物件在網頁裡頭的實際位置 x, y
//
window.getCtrlXY_ = function (ctrl) { // function begin
	var sx = ctrl.offsetLeft,  sy = ctrl.offsetTop;
	var sw = ctrl.offsetWidth, sh = ctrl.offsetHeight;
	do {
		ctrl = ctrl.offsetParent;
		if (ctrl) {
			sx	+= ctrl.offsetLeft;
			sy += ctrl.offsetTop;
		}
	} while(ctrl && ctrl.tagName!="BODY");
	return {x:sx, y:sy, w:sw, h:sh};
} // function end

//==================================================
// 取 document 資訊
//
window.getDocumentInfo_ = function () { // function begin
	if (document.all) {
		if (document.documentElement.clientHeight) { // for 更笨ie7
			var docW = document.documentElement.clientWidth;
			var docH = document.documentElement.clientHeight;
		}
		else { // for 笨ie6
			var docW = document.body.clientWidth;
			var docH = document.body.clientHeight;
		}
		var scrollTop  = document.documentElement.scrollTop;
		var scrollLeft = document.documentElement.scrollLeft;
	}
	else {
		var docW = window.innerWidth;
		var docH = window.innerHeight;
		var scrollTop  = window.pageYOffset;
		var scrollLeft = window.pageXOffset;
	}
	return {w:docW, h:docH, scrollLeft:scrollLeft, scrollTop:scrollTop};
} // function end

//==================================================
// 更新驗證碼
//
window.renewChkCode_ = function () { // function begin
	var d = new Date();
	if (document.getElementById('imgChkCode')) {
		document.getElementById('imgChkCode').src = wdbPath+'/chkcode.php?rseed='+d.getTime();
		if (document.getElementById('chkcode')) document.getElementById('chkcode').value = '';
	}
} // function end

//==================================================
// 設定 CSS
//
window.setCSS_ = function (css_s, css_name) { // function begin

	if (css_name) {
		var styleAy = document.getElementsByTagName("head")[0].getElementsByTagName("style");
		for (var i=0; i<styleAy.length; i++)
		if (styleAy[i].getAttribute('name')==css_name)
			document.getElementsByTagName("head")[0].removeChild(styleAy[i]);
	}

	var css_style = document.createElement('style');
	css_style.setAttribute('type', 'text/css');
	if (css_name) css_style.setAttribute('name', css_name);
	if (css_style.styleSheet)
		css_style.styleSheet.cssText = css_s;
	else
		css_style.appendChild(document.createTextNode(css_s));
	document.getElementsByTagName("head")[0].appendChild(css_style);
} // function end

//==================================================
// 取 scroll 資訊
//
window.getScrollInfo_ = function (doc) { // function begin
	if (!doc) doc = document;
	var t, l, w, h;
	if (doc.documentElement && doc.documentElement.scrollTop) {
		t = doc.documentElement.scrollTop;
		l = doc.documentElement.scrollLeft;
		w = doc.documentElement.scrollWidth;
		h = doc.documentElement.scrollHeight;
	} else if (doc.body) {
		t = doc.body.scrollTop;
		l = doc.body.scrollLeft;
		w = doc.body.scrollWidth;
		h = doc.body.scrollHeight;
	}
	return { top:t, left:l, width:w, height:h };
} // function end

//==================================================
// 取 IFrame 裡頭 document 元件
//
window.getIFrameDocument_ = function (idname) { // function begin
	var iframe = document.getElementById(idname);
	var doc = iframe.contentWindow || iframe.contentDocument;
	if (doc.document) doc = doc.document;
	return doc;
} // function end

//==================================================
// 依 IFrame 裡頭 document 內容調整 IFrame 高度
//
window.adjIFrameHeight_ = function (idname) { // function begin
	var doc;
	var a = getScrollInfo_(doc = getIFrameDocument_(idname));
	var h = a.height;
	if (isFF2) h += 23;
	document.getElementById(idname).style.height = h+'px';
	return doc;
} // function end

//==================================================
// 設定 Table 奇偶行背景色
// idTable=Table ID, cOdd="奇數背景", cEven="偶數行背景", cOver="滑鼠經過背景", cClick="滑鼠點擊後背景"
//
window.setTrBgColor_ = function (idTable, cOdd, cEven, cOver, cClick) { // function begin
	if (typeof(idTable)=='string') idTable = document.getElementById(idTable);
	var t = idTable.getElementsByTagName("tr"), c = '', n = -1;
	for (var i=0; i<t.length; i++) {
		if (t[i].style.display != 'none') n++;
		//n = this.sectionRowIndex;
		c = (n%2==0) ? cOdd : cEven;
		t[i].style.backgroundColor = c;
		t[i].cBG = c;
		if (cClick && cClick != '')
			t[i].onclick = function() {
				if(this.x != "1") {
					this.x = "1"; this.style.backgroundColor = cClick;
				} else {
					this.x = "0"; this.style.backgroundColor = this.cBG;
				}
			};
		if (cOver && cOver != '') {
			t[i].onmouseover = function() {
				if(this.x!="1") this.style.backgroundColor = cOver;
			};
			t[i].onmouseout = function() {
				if(this.x!="1") this.style.backgroundColor = this.cBG;
			};
		}
	}
} // function end
