   /**
    * 職訓局-客製函式庫
    *
    * PHP 4.3.9+, MySQL 4.0.10+, Apache 1.3.33+
    *
    * LICENSE: 本程式原始碼檔案，為旭聯科技股份有限公司所有。非經旭聯書面授權
    * 則嚴禁抄襲、篡改、散布、公開部份或全部內容。若取得旭聯書面授權書，亦得遵
    * 照書中所限制之使用範圍使用之，否則仍以侵權論究。
    *
    * @package     WM3
    * @author      Hubert <watermark999@sun.net.tw>
    * @copyright   2000-2007 SunNet Tech. INC.
    * @version     CVS: $Id: $
    * @link        http://demo.learn.com.tw/1000110138/index.html
    * @since       2007-04-11
    */

    /**
     * Trim
     *
     * function _Trim()
     * @param string   val		string
     * return result
     */
	function _Trim(val) {
		var re1 = /^[\s]+/ig, re2 = /[\s]+$/ig;
		var res = '';
		if (typeof(val) != 'undefined'){
			res = val.replace(re1, '');
			res = res.replace(re2, '');
		}
		return res;
	}

	/**
     * get this node's value
     *
     * function getNodeValue(node, tagname)
     * @parm	Node
     * @parm	Tag name
     * @return 	Node's value
     */
	function getNodeValue(node, tagname) {
		var nodes = null;
		if ((typeof(node) != "object") || (node == null)) return '-1';
		nodes = node.getElementsByTagName(tagname);
		if ((nodes == null) || (nodes.length <= 0)) return '-2';
		return (nodes[0].hasChildNodes()) ? nodes[0].firstChild.data : '';
	}

	/**
     * check if this user exist
     *
     * function chkUserExist(user)
     * @parm	username
     * @return 	true|false
     */
	function chkUserExist(user){
		if (user == '' || user == null) return false;
		var txt = "<manifest><ticket>" + ticket + "</ticket>";
		txt += "<user>" + user + "</user>";
		txt += "</manifest>";
		xmlVars.loadXML(txt);
		xmlHttp.open("POST", "/lib/custom/CO_chkuserexist.php", false);
		xmlHttp.send(xmlVars);
		xmlVars.loadXML(xmlHttp.responseText);
		ticket = getNodeValue(xmlVars, "ticket");
		var rslt = getNodeValue(xmlVars, "rslt");
		return (parseInt(rslt) > 0?true:false);
	}

	/**
     * put data into assigned place
     *
     * function _putValue(nid, contents, whr, ptns)
     * @parm	object id
     * @parm	put content
     * @parm	where
     * @parm	RegExp
     * @return  none
     */
	function _putValue(nid, contents, whr, ptns) {
		var val = null;
		if (typeof(nid) == 'object'){
			var node = nid;
		} else {
			switch (whr){
				case 'radio':
					var node = document.getElementsByName(nid);
					break;
				default:
					var node = document.getElementById(nid);
					break;
			}
		}
		if (node != null) {
			switch (whr){
				case 'value':
					node.value = contents;
					break;
				case 'html':
					node.innerHTML = contents;
					break;
				case 'disable':
					node.disabled = contents;
					break;
				case 'check':
					node.checked = contents;
					break;
				case 'visibility':
					node.style.visibility = contents;
					break;
				case 'display':
					node.style.display = contents;
					break;
				case 'radio':
					for(var i = 0; i < node.length; i++){
			    		if (node[i].value == contents){
			    			node[i].checked = true;
			    			if (node[i].nextSibling && ptns.toLowerCase() != 'n'){
			    				node[i].nextSibling.style.color = '#FF0000';
			    				node[i].nextSibling.style.fontWeight = 'Bold';
			    			}
			    		}else{
			    			node[i].checked = false;
			    			if (node[i].nextSibling && ptns.toLowerCase() != 'n'){
			    				node[i].nextSibling.style.color = '#000000';
			    				node[i].nextSibling.style.fontWeight = 'normal';
			    			}
			    		}
			    	}
					break;
				case 'checkbox':
					var nodes = node.getElementsByTagName('input');
					var re = new RegExp(ptns, 'i');
					for(var i = 0; i < nodes.length; i++){
			    		if (nodes[i].type == 'checkbox' && re.test(nodes[i].id)){
			    			var re1 = new RegExp(nodes[i].value, 'i');
			    			if (contents.search(re1) != -1){
			    				nodes[i].checked = true;
			    			}else{
			    				nodes[i].checked = false;
			    			}
			            }
			    	}
					break;
				case 'select':
					if (node.options.length > 0){
						for (var i = 0; i < node.options.length; i++){
							val = node.options[i].value;
							if (contents == val){
								node.options[i].selected = true;
							}
						}
					}
					break;

				case 'onclick':
					eval('node.onclick = function(){ ' + contents + ' }');
					break;

				case 'style':
					if (ptns != ''){
						eval('node.style.' + ptns + "='" + contents + "'");
					}
					break;
			}
		}
	}

	/**
     * get data from assigned place
     *
     * function _getValue(nid, whr, ptns)
     * @parm	object id
     * @parm	where
     * @parm	RegExp
     * @return  none
     */
	function _getValue(nid, whr, ptns){
		var val = '';
		if (typeof(nid) == 'object'){
			var node = nid;
		} else {
			switch (whr){
				case 'radio':
					var node = document.getElementsByName(nid);
					break;
				case 'radio1':
					var node = document.getElementsByName(nid);
					break;
				default:
					var node = document.getElementById(nid);
					break;
			}
		}
		if (node != null) {
			switch (whr){
				case 'value':
					val = node.value;
					break;
				case 'html':
					val = node.innerHTML;
					break;
				case 'tag':
					val = node.tagName.toLowerCase();
					break;
				case 'radio':
					for(var i = 0; i < node.length; i++){
			    		if (node[i].checked){
			    			if (ptns != ''){
			    				val = eval('node[i].' + ptns);
			    			}else{
			    				val = node[i].value;
			    			}
			    		}
			    	}
					break;
				case 'radio1':
					for(var i = 0; i < node.length; i++){
			    		if (node[i].checked){
			    			if (node[i].nextSibling != null && node[i].nextSibling.tagName.toUpperCase() == 'LABEL')
			    			if (node[i].nextSibling.firstChild != null) val = node[i].nextSibling.firstChild.nodeValue;
			    		}
			    	}
					break;
				case 'checkbox':
					var nodes = node.getElementsByTagName('input');
					var re = new RegExp(ptns, 'i');
					if (nodes != null){
						for(var i = 0; i < nodes.length; i++){
				    		if (nodes[i].type == 'checkbox' && re.test(nodes[i].id)){
				    			if (nodes[i].checked){
				    				val += nodes[i].value + '::';
				    			}
				            }
				    	}
			    	}
					break;
				case 'select':
					if (ptns != ''){
						val = eval('node.options[node.selectedIndex].' + ptns);
					}else{
						val = node.options[node.selectedIndex].value;
					}
					break;
			}
		}
		return val;
	}

	/**
     * display object
     *
     * function _dispObj(nid, w2do)
     * @parm	object id
     * @parm	what to do
     * @return  none
     */
	function _dispObj(nid, w2do){
		var obj = (typeof(nid)=='object' ? nid : document.getElementById(nid));
		if (obj == null) return false;
		obj.style.display = w2do;
	}

	/**
     * Show error
     *
     * function showErr(oid, msg)
     * @parm	object id
     * @parm	message
     * @return  none
     */
	function showErr(oid, msg){
		var obj = document.getElementById(oid);
		if (obj == null) {
			document.write(msg);
			return false;
		}
		for (var i = obj.rows.length - 1; i > 0; i--) {
			obj.deleteRow(i);
		}
		if (obj.rows[0] != null && obj.rows[0].cells[0] != null){
			for (var j = obj.rows[0].cells.length - 1; j > 0; j--) obj.rows[0].deleteCell(j);
			obj.rows[0].style.display = 'block';
			obj.rows[0].cells[0].width = '100%';
			obj.rows[0].cells[0].style.color = '#FF0000';
			obj.rows[0].cells[0].align = 'center';
			obj.rows[0].cells[0].innerHTML = msg;
		}
	}
	
	/**
     * Popup window
     *
     * function _Login(urls, scwt, scht)
     * @parm	link url
     * @parm	the width of popup window
     * @parm	the height of popup window
     * @return  none
     */
	var loginDailog = null;
	function _Login(urls, scwt, scht) {
		if (urls == '' || urls == null || typeof(urls) == 'undefined') urls = 'about:blank';
		if (scwt == '' || scwt == null || typeof(scwt) == 'undefined') scwt = 360;
		if (scht == '' || scht == null || typeof(scht) == 'undefined') scht = 360;
		if ((loginDailog != null) && !loginDailog.closed) {
			loginDailog.focus();
		} else {
			var rnd = Math.ceil(Math.random() * 100000);
			var wL = (screen.width - scwt) / 2;
			var wT = (screen.height - scht) / 2;
			loginDailog = window.open(urls, "win" + rnd, "top=" + wT + ",left=" + wL + ",width=" + scwt + ",height=" + scht + ",toolbar=0,location=0,status=0,menubar=0,directories=0,resizable=1,scrollbars=1");
		}
	}

	/**
     * Check identity number
     *
     * function _checkID(code)
     * @parm	identity number
     * @return  boolean
     */
	function _checkID(code){
		var SumCH = 0;
		var code = code.toUpperCase();
		if (!(parseInt(code.substr(1, 1)) == 1 || parseInt(code.substr(1, 1)) == 2)) return false;
		switch (code.substr(0, 1)){
			case "A": SumCH = 1;  break;  /***台北市(A)***/
			case "B": SumCH = 10; break;  /***台中市(B)***/
			case "C": SumCH = 19; break;  /***基隆市(C)***/
			case "D": SumCH = 28; break;  /***台南市(D)***/
			case "E": SumCH = 37; break;  /***高雄市(E)***/
			case "F": SumCH = 46; break;  /***台北縣(F)***/
			case "G": SumCH = 55; break;  /***宜蘭縣(G)***/
			case "H": SumCH = 64; break;  /***桃園縣(H)***/
			case "I": SumCH = 39; break;  /***嘉義市(I)***/
			case "J": SumCH = 73; break;  /***新竹縣(J)***/
			case "K": SumCH = 82; break;  /***苗栗縣(K)***/
			case "L": SumCH = 2;  break;  /***台中縣(L)***/
			case "M": SumCH = 11; break;  /***南投縣(M)***/
			case "N": SumCH = 20; break;  /***彰化縣(N)***/
			case "O": SumCH = 48; break;  /***新竹市(O)***/
			case "P": SumCH = 29; break;  /***雲林縣(P)***/
			case "Q": SumCH = 38; break;  /***嘉義縣(Q)***/
			case "R": SumCH = 47; break;  /***台南縣(R)***/
			case "S": SumCH = 56; break;  /***高雄縣(S)***/
			case "T": SumCH = 65; break;  /***屏東縣(T)***/
			case "U": SumCH = 74; break;  /***花蓮縣(U)***/
			case "V": SumCH = 83; break;  /***台東縣(V)***/
			case "W": SumCH = 21; break;  /***金門縣(W)***/
			case "X": SumCH = 3;  break;  /***澎湖縣(X)***/
			case "Y": SumCH = 12; break;  /***陽明山(Y)***/
			case "Z": SumCH = 30; break;  /***連江縣(Z)***/
		}
		for (var i = 8; i >= 1; i--){
			SumCH += (parseInt(code.substr(9 - i, 1))) * i;
		}
		var CH = parseInt(code.substr(9, 1));
	    if (10 - (SumCH % 10) == CH){
	    	return true;
	    }else{
	        return false;
	    }
	}

	function _doAction(action, number)
	{
		if ((typeof(xmlHttp) != "object") || (xmlHttp == null)) xmlHttp = XmlHttp.create();
		if ((typeof(xmlVars) != "object") || (xmlVars == null)) xmlVars = XmlDocument.create();
		var pageno = _getValue('pageno', 'select', '');
		var pagerow = _getValue('pagerow', 'select', '');
		var txt = '<manifest>';
		txt += '<action>' + action + '</action>';
		txt += '<number>' + number + '</number>';
		txt += '<pageno>' + pageno + '</pageno>';
		txt += '<pagerow>' + pagerow + '</pagerow>';
		txt += '</manifest>';
		
		xmlVars.loadXML(txt);
		xmlHttp.open("POST", "/lib/evta_replcontent.php", false);
		xmlHttp.send(xmlVars);
		xmlVars.loadXML(xmlHttp.responseText);
		//document.write(xmlHttp.responseText);
		//document.write(xmlHttp.responseText);
		//if (action == 'listMuseum') alert(txt+"\n"+xmlHttp.responseText);
		switch(action)
		{
			case 'goHomepage':
				_putValue('evta_toolmap', toolmap1, 'html', '');
				break;
			default:
				_putValue('evta_toolmap', toolmap2, 'html', '');
				break;
		}
		_putValue('evta_content', getNodeValue(xmlVars, "rslt"), 'html', '');
	}

	function chooseVocation()
	{
		if (_getValue('co_voc','select','') == 'Voc_Others')
		{
			_putValue('co_vocothers','block','display','');
		}
		else
		{
			_putValue('co_vocothers','none','display','');
			_putValue('co_vocothers','','value','');
		}
	}
