function MM_openBrWindow(theURL,winName,features) { //v2.0
var my_popup=window.open(theURL,winName,features);my_popup.focus();
}

function checkemail(email){
	var str=email;
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	if (filter.test(str))
		return true;
	else{
		return false;
	}
}


function no_modify()
{
	var el, els, e, f = 0, form, forms = document.getElementsByTagName('form');
	while (form = forms.item(f++))
	{
		e = 0; els = form.getElementsByTagName('input');
		while (el = els.item(e++)) {
			//if (el.readOnly) {
				el.readOnly=true;
				el.className = 'readonly';
			//}
		}
	}
}

function check_time_ISO_8601_format(S) {
  return /^([01]?[0-9]|[2][0-3])(:[0-5][0-9])?$/.test(S) 
}

function ReadISO8601dateE(Q) { // adaptable for other layouts
  if (Q.search(/^\d+-\d\d-\d\d$/)!=0) { return -2 } // bad format
  var T = Q.split('/'), Y = +T[0], M = +T[1], D = +T[2]
  return D>0 && (D<=[,31,28,31,30,31,30,31,31,30,31,30,31][M] ||
    D==29 && Y%4==0 && (Y%100!=0 || Y%400==0) ) ? T : -1 }

//圖書館登記證之驗證方式
//只要第一碼為英文字,2-3可英文可數字,其餘可為數字,共12碼
function cidCheck(num) {
//錯誤return ture
   a1 = num.substring(0, 1);
   if (a1 == '.') {	//外國讀者
   	return false;		
   }
   a1 = num.substring(0, 3);
   if (a1 == 'tep' || a1 == 'ref') {	//臨時証
      tepnum = num.substring(3, 12);
      if (chknum(tepnum)==false) {
      	   return true;		
      }   
      return false;		
   }
   
   return !IDcheck(num);		//身份証
   
/*   	
   patten=/^[.-z][0-z]{9}[0-9]{2}$/;
   if(!patten.test(num)){
	return true;
   }

   a1 = num.substring(0, 1);
   tepnum = num.substring(3, 12);
   if (chknum(tepnum)==false) {
   	return true;		
   }
*/

}


//數字檢查
function numberCheck(v)
{
  var checkOK = "0123456789-.";
  var checkStr = v;
  var allValid = true;
  var decPoints = 0;
  var allNum = "";
  if(checkStr == "") return(false);
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
    if (ch == ".")
    {
      allNum += ".";
      decPoints++;
    }
    else
      allNum += ch;
  }
  if (!allValid)
  {
    return false;
  }

  if (decPoints > 1)
  {
    return false;
  }
  return true;
}

/*

(a) 中華民國身份證字號編碼原則  
 
1.身份證統一編號共計有10位，其中第一位為英文字母，
  後共有九個數字；而最後一位數字為檢查碼( Check Digit ) ，
  表示如下表：
┌─┬─┬─┬─┬─┬─┬─┬─┬─┬─┐
│L1│D1│D2│D3│D4│D5│D6│D7│D8│D9│
└─┴─┴─┴─┴─┴─┴─┴─┴─┴─┘
↑ ↑                                 ↑
│ └── 1：表示男性                 └─ 檢查碼
│        2：表示女性
│
└ 英文字母：代表各縣市

字母 L1 對照表如下，須找出其代號 2 位：
┌──┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┐
│字母│Ａ│Ｂ│Ｃ│Ｄ│Ｅ│Ｆ│Ｇ│Ｈ│Ｊ│Ｋ│Ｌ│Ｍ│Ｎ│
├──┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┤
│代號│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│
└──┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┘
令其十位數為 X1 ，個位數為 X2 ；( 如Ａ：X1=1 , X2=0 )

3.依其公式計算結果：
Ｙ=  X1 + 9*X2 + 8*D1 + 7*D2 + 6*D3 + 5*D4 
    + 4*D5 + 3*D6 + 2*D7+ 1*D8 + D9

4.如Ｙ能被 10 整除，則表示該身份證號碼為正確，否則為錯誤。


*/


var local=new Array(36)
local[10]='A'
local[11]='B'
local[12]='C'
local[13]='D'
local[14]='E'
local[15]='F'
local[16]='G'
local[17]='H'
local[18]='J'
local[19]='K'
local[20]='L'
local[21]='M'
local[22]='N'
local[23]='P'
local[24]='Q'
local[25]='R'
local[26]='S'
local[27]='T'
local[28]='U'
local[29]='V'
local[32]='W'
local[30]='X'
local[31]='Y'
local[33]='Z'
local[34]='I'
local[35]='O'

//身份證號碼檢驗
function IDcheck(id){
id=id.toUpperCase()
if(lengtherr(id)){
  //alert('輸入的字號長度有誤！')
  return false;
}else if(firstlettererr(id)){
  //alert('身份證第一碼查無此英文字母:'+id.substring(0,1))
  return false;
}else if(numerr(id)){
  //alert('輸入的身份證後九碼應為數字！')
  return false;
}else if(checkerr(id)){
  alert('您輸入的身份證檢查碼有誤！')
  return false;
}else{
  //alert('您輸入的身份證字號完全正確！')
  return true;
}
}
function lengtherr(id){
if(id.length<10)
return 1
else 
return 0
}
function firstlettererr(id){
var fl=id.substring(0,1)
var haserr=1
for(i=10;i<=35;i++){
if(local[i]!=fl)
continue
else{ 
haserr=0
break
}
}
if(haserr==1)
return 1
else
return 0
}function numerr(id){
var haserr=0
for(i=1;i<=9;i++){
if(parseInt(id.substring(i,i+1))>0 || id.substring(i,i+1)=='0')
continue
else{
haserr=1
break}
}
if(haserr==1)
return 1
else
return 0
}
function checkerr(id){
	var se=new Array(10)
	var we=0
	var checkcode=0
	for(i=10;i<=35;i++){
		if(local[i]==id.substring(0,1)){
			se[0]=parseInt((i+'0').substring(0,1))
			se[1]=parseInt((i+'0').substring(1,2))
			break
		} 
	}
	for(i=1;i<=9;i++){
		se[i+1]=parseInt(id.substring(i,i+1))
	}
	for(i=0;i<=10;i++){
		if(i==0)
			we=we+se[i]
		else
			we=we+(se[i]*(10-i))
	} 
	checkcode=((10-mod(we,10))+'0').substring(0,1)
	if(checkcode!=id.substring(9,10))
		return 1
	else
		return 0
}
function mod(a,b){
	var r
	r=Math.round(a/b)
	if((b*r)>a)
	r-=1
	return (a-(b*r))
}

//回傳英文字+中文字的字數
function strLengthCheck(txtboxobj)
{
	var str=txtboxobj.value,max=str.length,addascii=0,addbig5=0;
	
	for(var i=0;i<max; i++) {
		var codes=str.charCodeAt(i);
		if(codes>=0 && codes<=127) {
			addascii++;
		}
		else {
			addbig5+=2;
		}
	}
	//回傳英文字+中文字的字數
	return addascii+addbig5;
}

//檢查文字中是否有中文字
function strBig5Check(txt)
{
	var str=txt,max=str.length,addascii=0,addbig5=0;
	
	for(var i=0;i<max; i++) {
		var codes=str.charCodeAt(i);
		if(codes>=0 && codes<=127) {
			addascii++;
		}
		else {
			addbig5+=2;
		}
	}
	if(addbig5>0) return true;
	if(addascii>0) return false;
}

/*
==================================================================
LTrim(string) : Returns a copy of a string without leading spaces.
==================================================================
*/
function LTrim(str)
/*
   PURPOSE: Remove leading blanks from our string.
   IN: str - the string we want to LTrim
*/
{
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(0)) != -1) {
      // We have a string with leading blank(s)...

      var j=0, i = s.length;

      // Iterate from the far left of string until we
      // don't have any more whitespace...
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;

      // Get the substring from the first non-whitespace
      // character to the end of the string...
      s = s.substring(j, i);
   }
   return s;
}

/*
==================================================================
RTrim(string) : Returns a copy of a string without trailing spaces.
==================================================================
*/
function RTrim(str)
/*
   PURPOSE: Remove trailing blanks from our string.
   IN: str - the string we want to RTrim

*/
{
   // We don't want to trip JUST spaces, but also tabs,
   // line feeds, etc.  Add anything else you want to
   // "trim" here in Whitespace
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      // We have a string with trailing blank(s)...

      var i = s.length - 1;       // Get length of string

      // Iterate from the far right of string until we
      // don't have any more whitespace...
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;


      // Get the substring from the front of the string to
      // where the last non-whitespace character is...
      s = s.substring(0, i+1);
   }

   return s;
}

/*
=============================================================
Trim(string) : Returns a copy of a string without leading or trailing spaces
=============================================================
*/
function Trim(str)
/*
   PURPOSE: Remove trailing and leading blanks from our string.
   IN: str - the string we want to Trim

   RETVAL: A Trimmed string!
*/
{
   return RTrim(LTrim(str));
}

function replaceSubstring(inputString, fromString, toString) {
   // Goes through the inputString and replaces every occurrence of fromString with toString
   var temp = inputString;
   if (fromString == "") {
      return inputString;
   }
   if (toString.indexOf(fromString) == -1) { // If the string being replaced is not a part of the replacement string (normal situation)
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } else { // String being replaced is part of replacement string (like "+" being replaced with "++") - prevent an infinite loop
      var midStrings = new Array("~", "`", "_", "^", "#");
      var midStringLen = 1;
      var midString = "";
      // Find a string that doesn't exist in the inputString to be used
      // as an "inbetween" string
      while (midString == "") {
         for (var i=0; i < midStrings.length; i++) {
            var tempMidString = "";
            for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
            if (fromString.indexOf(tempMidString) == -1) {
               midString = tempMidString;
               i = midStrings.length + 1;
            }
         }
      } // Keep on going until we build an "inbetween" string that doesn't exist
      // Now go through and do two replaces - first, replace the "fromString" with the "inbetween" string
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + midString + toTheRight;
      }
      // Next, replace the "inbetween" string with the "toString"
      while (temp.indexOf(midString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(midString));
         var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } // Ends the check to see if the string being replaced is part of the replacement string or not
   return temp; // Send the updated string back to the user
} // Ends the "replaceSubstring" function
