function trim(str) {
res="";
for(var i=0; i< str.length; i++) {
if (str.charAt(i) != " " && str.charAt(i) != "　") {
res +=str.charAt(i);
}
}
return res;
}//trim()

var bName = navigator.appName;
nc = (bName == "Netscape") ? true : false;
ie = (bName == "Microsoft Internet Explorer") ? true : false;
function Check_num() {
	if(nc) document.onkeypress = keyDown;
	else if ((event.keyCode < 48 || event.keyCode > 57) && event.keyCode != 13 && event.keyCode != 46) event.returnValue = false;
}
function keyDown(e){
	var nkey=e.which;
	if (nkey >= 48 && nkey <= 57 || nkey==46 || nkey==13 || nkey==8 || nkey==0) return true;
	else return false;
}
function Cls_event(){
	document.onkeypress = "";
}

/*Email*/
function Checkmail(objName,lange) {
	var msg;
	var obj = objName;
	var c = obj.value.charAt(0);
	if (lange=="e") msg="Email format is incorrect, please review your entry!";
	else msg="電子郵件格式錯誤!";

	if (!((c>="a" && c<="z") || (c>="0" && c<="9") || (c>="A" && c<="Z"))) {
		alert(msg);
		obj.focus();
		return(false);
	}
	var c = obj.value.charAt(obj.value.length-1);
	if (!((c>="a" && c<="z") || (c>="0" && c<="9") || (c>="A" && c<="Z"))) {
		alert(msg);
		obj.focus();
		return(false);
	}
	if ((obj.value.indexOf("@")=="-1") || (obj.value.indexOf(".")=="-1")) {
		alert(msg);
		obj.focus();
		return(false);
	}
	return(true);
}

//判斷身份證
function firstlettererr(id){ 
	var fl=id.value.substr(0,1); 
	var T="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var smp=id.value.substr(0,1) 
	if (T.indexOf(smp) == -1) { return false; } else { return true; } 
} 

function checkid(id) { 
	if(id.value.length==10) return true;
	else return false;
} 

function chfastid(id) { 
	var c = id.value.charAt(0); 
	if(c<"A" || c> "Z") return false;
	else return true; 
} 

function ch12fastid(id) { 
	var c = id.value.charAt(1); 
	if(c!="1" && c!="2") return false;
	else return true; 
} 

function idmanber(id) { //=====後九碼為數字// 
	var bmp; 
	var d="0123456789"; 
	var bab=id.value.length-1; 
	for (var i=1;i<=bab;i++){ 
		bmp=id.value.substr(i,1) 
		if (d.indexOf(bmp) == -1) { return false; } 
	} 
	if(id.value != "0") return true; 
	else return false;
} 

function idchackok(id) { //規則// 
	var alph = new Array("A","B","C","D","E","F","G","H","J","K","L","M","N","P","Q","R","S","T","U","V","X","Y","W","Z","I","O"); 
	var num = new Array("10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35"); 
	var n=0; 
	for(i=0;i<alph.length;i++) 
	if(id.value.charAt(0)==alph[i]) 
	n=i; 
	var tot1 = parseFloat(num[n].charAt(0)) + (parseFloat(num[n].charAt(1)) * 9); 
	var tot2 = 0; 
	for(i=1;i<id.value.length-1;i++) 
	tot2 = tot2 + parseFloat(id.value.charAt(i))*(9-i); 
	var tot3 = parseFloat(id.value.charAt(9)); 
	var tot4 = tot1 + tot2 + tot3; 
	if((tot4 % 10)!=0) return false; 
	else return true;
} 


function idChech(id){ 
	if(checkid(id) == false){ 
		alert("您的身份證字號位數不對!!"); 
		return false; 
	} 
	if(chfastid(id) == false){ 
		alert("您的身分證字號第一碼必須是大寫的英文字母 !"); 
		return false; 
	} 
	if(ch12fastid(id) == false){ 
		alert("您的身分證字號第二碼有問題 !"); 
		return false; 
	} 
	if(firstlettererr(id) == false){ 
		alert("您的身份證字號錯誤!!"); 
		return false; 
	} 
	if(idmanber(id) == false){ 
		alert("您的身份證字號後9碼應為數字!!"); 
		return false; 
	} 
	if(idchackok(id) == false){ 
		alert("您的身分證字號有問題 !"); 
		return false; 
	} 
	return true;
} 

var marked_row = new Array;
function PMA_markRowsInit() {
    // for every table row ...
	var rows = document.getElementsByTagName('tr');
	
	for ( var i = 0; i < rows.length; i++ ) {
	    // ... with the class 'odd' or 'hidden' ...
		if ( 'odd' != rows[i].className && 'hidden' != rows[i].className && rows[i].className.search(' marked') < 0 ) {
		    continue;
		}
        // ... to highlight the row on mouseover ...
	    if ( navigator.appName == 'Microsoft Internet Explorer' ) {
	        // but only for IE, other browsers are handled by :hover in css
			rows[i].onmouseover = function() {
			    this.className += ' hover';
			}
			rows[i].onmouseout = function() {
			    this.className = this.className.replace( ' hover', '' );
			}
	    }
        // ... and to mark the row on click ...
		rows[i].onmousedown = function() {
		  var unique_id;
            var checkbox;

            checkbox = this.getElementsByTagName( 'input' )[0];
            if ( checkbox && checkbox.type == 'checkbox' ) {
                unique_id = checkbox.name + checkbox.value;
            } else if ( this.id.length > 0 ) {
                unique_id = this.id;
            } else {
		        return;
		    }

            if ( typeof(marked_row[unique_id]) == 'undefined' || !marked_row[unique_id] ) {
                marked_row[unique_id] = true;
            } else {
                marked_row[unique_id] = false;
            }
            if (checkbox.checked==true) marked_row[unique_id] = false;

            if ( marked_row[unique_id] ) {
			    this.className += ' marked';
            } else {
			    this.className = this.className.replace(' marked', '');
            }

            if ( checkbox && checkbox.disabled == false ) {
                //checkbox.checked = marked_row[unique_id];
                checkbox.checked = !checkbox.checked;
            }
		}

		// ... and disable label ...
		var labeltag = rows[i].getElementsByTagName('label')[0];
		if ( labeltag ) {
		    labeltag.onclick = function() {
		        return false;
		    }
	    }
	    // .. and checkbox clicks
		var checkbox = rows[i].getElementsByTagName('input')[0];
		if ( checkbox ) {
		    checkbox.onclick = function() {
		        // opera does not recognize return false;
		        this.checked = ! this.checked;
		    }
	    }
	}
}
window.onload=PMA_markRowsInit;

function markAllRows( container_id ) {
	var rows = document.getElementById(container_id).getElementsByTagName('tr');
	var unique_id;
	var checkbox;

	for ( var i = 0; i < rows.length; i++ ) {

	if ( 'odd' != rows[i].className && 'hidden' != rows[i].className ) {
	    continue;
	}
        checkbox = rows[i].getElementsByTagName( 'input' )[0];

        if ( checkbox && checkbox.type == 'checkbox' ) {
            unique_id = checkbox.name + checkbox.value;
            
            if ( checkbox.disabled == false ) {
                checkbox.checked = true;
                if ( typeof(marked_row[unique_id]) == 'undefined' || !marked_row[unique_id] ) {
                    rows[i].className += ' marked';
                    marked_row[unique_id] = true;
                }
            }
	    }
	}

	return true;
}

/**
 * marks all rows and selects its first checkbox inside the given element
 * the given element is usaly a table or a div containing the table or tables
 *
 * @param    container    DOM element
 */
function unMarkAllRows( container_id ) {
	var rows = document.getElementById(container_id).getElementsByTagName('tr');
	var unique_id;
	var checkbox;

	for ( var i = 0; i < rows.length; i++ ) {

        checkbox = rows[i].getElementsByTagName( 'input' )[0];

        if ( checkbox && checkbox.type == 'checkbox' ) {
            unique_id = checkbox.name + checkbox.value;
            checkbox.checked = false;
            rows[i].className = rows[i].className.replace(' marked', '');
            marked_row[unique_id] = false;
        }
	}

	return true;
}
