//**********檢查身分證字號****************//
var local=new Array(34)
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'

function checkid(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{
    return true;
  }
}
function lengtherr(id){
  if(id.length<10)
    return 1
  else 
    return 0
}
function firstlettererr(id){   //第一碼字元正確性檢測
  var fl=id.substring(0,1)   //取得第一碼字元值給fl變數
  var haserr=1   //建立錯誤發生旗標,預設是有錯誤
  for(i=10;i<=33;i++){   //從第一碼的識別庫中找尋是否有該字母
     if(local[i]!=fl)   //如果沒有就往下一個找
       continue
     else{   
   //如果有的話就把錯誤碼設為0表示沒有錯誤發生,因為第一碼已在識別庫中了
       haserr=0
       break
     }
  }
  if(haserr==1)   //如果錯誤發生旗標為1表示第一個字元並未出現在識別庫中
    return 1   //就回覆錯誤為true
  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<=33;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))
}
//**********Email****************//
function emailCheck (emailStr) {
	/* The following pattern is used to check if the entered e-mail address
	   fits the user@domain format.  It also is used to separate the username
	   from the domain. */
var emailPat=/^(.+)@(.+)$/
	/* The following string represents the pattern for matching all special
	   characters.  We don't want to allow special characters in the address. 
	   These characters include ( ) < > @ , ; : \ " . [ ]    */
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	/* The following string represents the range of characters allowed in a 
	   username or domainname.  It really states which chars aren't allowed. */
var validChars="\[^\\s" + specialChars + "\]"
	/* The following pattern applies if the "user" is a quoted string (in
	   which case, there are no rules about which characters are allowed
	   and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
	   is a legal e-mail address. */
var quotedUser="(\"[^\"]*\")"
	/* The following pattern applies for domains that are IP addresses,
	   rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
	   e-mail address. NOTE: The square brackets are required. */
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	/* The following string represents an atom (basically a series of
	   non-special characters.) */
var atom=validChars + '+'
	/* The following string represents one word in the typical username.
	   For example, in john.doe@somewhere.com, john and doe are words.
	   Basically, a word is either an atom or quoted string. */
var word="(" + atom + "|" + quotedUser + ")"
	// The following pattern describes the structure of the user
var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	/* The following pattern describes the structure of a normal symbolic
	   domain, as opposed to ipDomainPat, shown above. */
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")


	/* Begin with the coarse pattern to simply break up user@domain into
	   different pieces that are easy to analyze. */
var matchArray=emailStr.match(emailPat)
if (matchArray==null) {
	  /* Too many/few @'s or something; basically, this address doesn't
	     even fit the general mould of a valid e-mail address. */
	alert("Email 不正確 (請檢查 @ 和 .'s)")
	return false
}
var user=matchArray[1]
var domain=matchArray[2]

	// See if "user" is valid 
if (user.match(userPat)==null) {
    // user is not valid
    alert("Email 使用者名稱不正確.")
    return false
}

	/* if the e-mail address is at an IP address (as opposed to a symbolic
	   host name) make sure the IP address is valid. */
var IPArray=domain.match(ipDomainPat)
if (IPArray!=null) {
    // this is an IP address
	  for (var i=1;i<=4;i++) {
	    if (IPArray[i]>255) {
	        alert("IP address 不正確!")
		return false
	    }
    }
    return true
}

	// Domain is symbolic name
var domainArray=domain.match(domainPat)
if (domainArray==null) {
	alert("Domain name 不正確.")
    return false
}

	/* domain name seems valid, but now make sure that it ends in a
	   three-letter word (like com, edu, gov) or a two-letter word,
	   representing country (uk, nl), and that there's a hostname preceding 
	   the domain or country. */

	/* Now we need to break up the domain to get a count of how many atoms
	   it consists of. */
var atomPat=new RegExp(atom,"g")
var domArr=domain.match(atomPat)
var len=domArr.length
if (domArr[domArr.length-1].length<2 || 
    domArr[domArr.length-1].length>3) {
   // the address must end in a two letter or three letter word.
   alert("EMail address 最後必須為二至三碼的字元.")
   return false
}

// Make sure there's a host name preceding the domain.
if (len<2) {
   var errStr="EMail 不正確!"
   alert(errStr)
   return false
}

return true;
}

//**********檢查日期****************//
var now = new Date();
var today = new Date(now.getYear(),now.getMonth(),now.getDate());
//**********檢查數字(不允許輸入小數點)****************//
function check_Int(checkfield,fieldname,isEmptyOk)
{
    newString = "";    
    count = 0; 
    checkString=checkfield.value;        

	if (isEmptyOk){
		if (checkString=="")return true;
	}
	
	if (!check_empty(checkfield,fieldname))return false;
		for (i = 0; i < checkString.length; i++){
	        ch = checkString.substring(i, i+1);
	        if (ch >= "0" && ch <= "9")newString += ch;
	}
	
	if (checkString != newString){
	   alert(fieldname+"要是數字喔!");
		 checkfield.focus();
	    return false ;
	}else return true ;
}

//**************檢查數字************//
function check_Float(objNum,objStr){
	if(objNum.value != ""){
		if(!Math.sin(objNum.value)){
			alert(objStr + "必須是數字喔!");
			objNum.focus();
			return false;
		}else{
			return true;
		}
	}else{
		return true;
	}
}

//**********檢查空白****************//
function check_empty(checkfield,fieldname)
{
    if(checkfield.value==""){
    	alert(fieldname+"要輸入喔!");
    	checkfield.focus();
    	return false ;
    }
    else return true ;
    
}

	//=======檢查文字欄位不能有特殊字元=======
	function CheckData(text1)
	{	
		for(var i=1;i<=text1.value.length;i++)
		{
			if ((text1.value.substring(i-1,i) != ".")
		      && (!((text1.value.substring(i-1,i) >= "0") && (text1.value.substring(i-1,i) <= "9")))
			  && (!((text1.value.substring(i-1,i) >= "@") && (text1.value.substring(i-1,i) <= "Z"))) 
			  && (!((text1.value.substring(i-1,i) >= "a") && (text1.value.substring(i-1,i) <= "z")))
			  && (!((text1.value.substring(i-1,i) == "-") || (text1.value.substring(i-1,i) == "_"))))
		    	var flag=false;         
		}
		if(flag == false){		  
			alert('Input format error! Please Input again!');
			return(false);
		}else{
			return(true);
		}	
	}

	//=======檢查文字欄位只能輸入a-z;A-Z;0-9; =======
	function CheckDataLogin(text1)
	{	
		for(var i=1;i<=text1.value.length;i++)
		{
			if ((text1.value.substring(i-1,i) != ".")
		      && (!((text1.value.substring(i-1,i) >= "0") && (text1.value.substring(i-1,i) <= "9")))
			  && (!((text1.value.substring(i-1,i) >= "@") && (text1.value.substring(i-1,i) <= "Z"))) 
			  && (!((text1.value.substring(i-1,i) >= "a") && (text1.value.substring(i-1,i) <= "z"))))
		    	var flag=false;         
		}
		if(flag == false){		  
			alert('Input format error! Please Input again!');
			return(false);
		}else{
			return(true);
		}	
	}
		//=======檢查文字欄位只能輸入a-z;A-Z;0-9;和空白 =======
	function CheckDataSearch(text1)
	{	
		for(var i=1;i<=text1.value.length;i++)
		{
			if ((text1.value.substring(i-1,i) != ".")
		      && (!((text1.value.substring(i-1,i) >= "0") && (text1.value.substring(i-1,i) <= "9")))
			  && (!((text1.value.substring(i-1,i) >= "A") && (text1.value.substring(i-1,i) <= "Z"))) 
			  && (!((text1.value.substring(i-1,i) >= "a") && (text1.value.substring(i-1,i) <= "z")))
			  && (!((text1.value.substring(i-1,i) == " "))))
		    	var flag=false;         
		}
		if(flag == false){		  
			alert('Input format error! Please Input again!');
			return(false);
		}else{
			return(true);
		}	
	}
	//=======檢查文字欄位不能有單引號=======
  function CheckData1(text2)
  {
    if(text2.value.indexOf("'") != "-1"){
    	alert('輸入值不能有單引號,請重新輸入!!');
    	return false ;
    }
    else {
    	return true ;
    }
  }

//**********檢查密碼****************//
 function checkpwd(pwd1, pwd2){
	if( pwd1.length==0 )
    		return "請輸入登入密碼\n"; 
    if( pwd1.length < 4 ){
    		return "密碼必須大於四個字元!\n";
    	}else{
    	  var cmp="~!@#$%^&*()-_+=|\}{[]:;<>,.?/'"; 
       var cmpChar;
       error=false;
       for(var i=0; i<pwd1.length; i++){
         cmpChar=pwd1.substring(i,i+1)
              if(cmp.indexOf(cmpChar)>0){
                 error=true;
                 i=pwd1.length;
             }
      }
     if(error == true)
       return "密碼必須是文數字!\n";   
   }	
    if( pwd2.length==0)
    		return "請輸入確定密碼!\n";
    if( pwd1 != pwd2) 
    		return "確定密碼必須與登入密碼符合\n ";
    return "";
}

//********************* 檢查日期**************************//
function check_select (year,month,date){   	
 	yy = parseInt(year.options.value); 
 	mm = parseInt(month.options.value);
 	dd = parseInt(date.options.value)
 	var sel =month.selectedIndex; 
  var seld =date.selectedIndex;  	
  if (((mm == 4) || (mm == 6) || (mm == 9) || (mm == 11)) && (dd == 31)){
  	alert("日期格式不對, 請重新確認");
  	return false;
  }else if(mm == 2){
  	if (((yy%4==0) && ((yy%400==0) || (yy%100!=0))) && ((dd == 30) || (dd == 31))){
  		alert("日期格式不對, 請重新確認");
  		return false;
  	}else if((yy%4!=0) && (dd == 29) || (dd == 30) || (dd == 31)){
  		alert("日期格式不對, 請重新確認");
  		return false;
  	}else{
  		return true;
  	}
  }else{
  	return true;
	}
}	

//********** 結束日期必須在開始日期之後的檢查
	function CheckDate()	{
		MyObj = eval(document.thisform);
		if(MyObj.start_date.value!=""){
			var enddate = new Date(MyObj.end_date.value)
			var startdate = new Date(MyObj.start_date.value)
	
			if(enddate<=startdate){
				alert("結束日期必須在開始日期之後!\n請重新選擇結束日期");
				MyObj.end_date.focus();
				return false;
			}
			else	return true;
		}	
	
	}

//********** 結束日期必須在開始日期之後的檢查(改良版)
	function CheckDate1(date1,date2,errstr)	{
		if(date1.value!=""){
			var enddate = new Date(date2.value)
			var startdate = new Date(date1.value)
	
			if(enddate<=startdate){
				alert(errstr);
				date2.focus();
				return false;
			}
			else	return true;
		}	
	
	}

//********** 結束日期必須在開始日期之後的檢查(改良版)
	function CheckDate2(date1,date2,errstr)	{
		if(date1.value!=""){
			var enddate = new Date(date2.value)
			var startdate = new Date(date1.value)
	
			if(enddate < startdate){
				alert(errstr);
				//date2.focus();
				return false;
			}
			else{
				return true;
			}
		}	else{
			return false;
		}
	
	}

			//=======檢查電話欄位只能輸入0-9; ( ) - + =======
	function CheckPhoneData(text1)
	{	
		for(var i=1;i<=text1.value.length;i++)
		{
			if ((!((text1.value.substring(i-1,i) >= "0") && (text1.value.substring(i-1,i) <= "9")))
			  && ((text1.value.substring(i-1,i) != "-") && (text1.value.substring(i-1,i) != "+")) 
			  && ((text1.value.substring(i-1,i) != "(") && (text1.value.substring(i-1,i) != ")"))
			  )
		    	var flag=false;         
		}
		if(flag == false){		  
			alert('Input format error! Please Input again!');
			text1.focus();
			return(false);
		}else{
			return(true);
		}	
	}
	
				//=======檢查檔案欄位只能輸入0-9;a-z;A-Z;與底線 =======
	function CheckFileName(text1)
	{	
		for(var i=1;i<=text1.value.length;i++)
		{
			if ((!((text1.value.substring(i-1,i) >= "0") && (text1.value.substring(i-1,i) <= "9")))
			  && ((text1.value.substring(i-1,i) != "-") && (text1.value.substring(i-1,i) != "+")) 
			  && (!((text1.value.substring(i-1,i) >= "A") && (text1.value.substring(i-1,i) <= "Z"))) 
			  && (!((text1.value.substring(i-1,i) >= "a") && (text1.value.substring(i-1,i) <= "z")))
			  && (!((text1.value.substring(i-1,i) == "_"))))
		    	var flag=false;         
		}
		if(flag == false){		  
			alert('Input format error! Please Input again!');
			text1.focus();
			return(false);
		}else{
			return(true);
		}	
	}
	
	//===== 產生密碼getPassword()

	function getRandomNum(lbound, ubound) {
		return (Math.floor(Math.random() * (ubound - lbound)) + lbound);
	}

	function getRandomChar() {
		var numberChars = "0123456789";
		var lowerChars = "abcdefghijklmnopqrstuvwxyz";
		var upperChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		var charSet;
		charSet += numberChars;
		charSet += lowerChars;
		charSet += upperChars;
		return charSet.charAt(getRandomNum(0, charSet.length));
	}

	function getPassword() {
		var rc = "";
		for (var idx = 0; idx < 6; ++idx) {
		rc = rc + getRandomChar();
		}
		return rc;
	}
	//=====================================================
	function byte_length(st) 
	{

   var result = 0;
   for (var i = 0 ; i < st.length ; i++) {
      if ( st.charCodeAt(i) >= 128 ) {
         result += 2;
      } else {
         result++;
      }
   }
   return result;
}	