////////////////////////////////////////////////////////////
// 說明 : 檢查輸入字串長度
// 作著 : maduka Bor
// 日期 : 2002/10/18
////////////////////////////////////////////////////////////		
function checkStrlen(InField,ChkLen,Info) {
  if (InField.value.length > 0 && InField.value.length < ChkLen) {
   alert(Info + "輸入長度必須至少為" + ChkLen + "碼!");
   InField.focus();
   InField.select();
   return false;
  }
  return true;
}
////////////////////////////////////////////////////////////
// 說明 : 檢查是否為空值
// 作著 : maduka Bor
// 日期 : 2002/10/18
////////////////////////////////////////////////////////////	
function checknull(InField,Info) {
  	if (InField.value.length == 0) {
  			if(typeof message != 'undefined') alert(message);
     		else alert(Info + " field cannot be left blank!");
      	InField.focus();
      	InField.select();
      	return false;
  	}
  	return true;
}

////////////////////////////////////////////////////////////
// 說明 : 檢查是否為數字型態
// 作著 : maduka Bor
// 日期 : 2002/10/18
////////////////////////////////////////////////////////////	
function checknum(InField,Info){
  	if (isNaN(InField.value)) {
      	alert(Info + "必須為數字！");
      	InField.focus();
      	InField.select();
      	return false;
  	}
  	return true;
}	
////////////////////////////////////////////////////////////
// 說明 : 檢查是否為日期型態(YYYY/MM/DD)
// 作著 : maduka Bor
// 日期 : 2002/10/18
////////////////////////////////////////////////////////////	
function checkdate(InField,Info) {

var str_i = InField.value;

var str_p ="YYYY/MM/DD";

p_element = new Array(3);
i_element = new Array(3);
reg_year  = /yyyy|yyy|yy|rr/i;
reg_ycnt  = /y|r/gi;
arr_mon   = ["","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
reg_mon   = /Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec/i;
reg_month = /mon/i;
reg_s     = /\-|\/|\,|\./;
reg_ss    = /\-|\/|\,|\./g;

if (InField.value.length == 0) {
	return true;
}
// 判斷是否為8碼	
if (InField.value.length==8)
{
	if (isNaN(InField.value)) {
		//Window.status = '輸入YYYY(西元年)/MM/DD';
	} else {
		datY = str_i.substr(0,4);
		datM = str_i.substr(4,2);
		datD = str_i.substr(6,2);
		datYMD = datY + '/' +  datM + '/' + datD;
		str_i = datYMD;
	}
}
if (str_p.match(reg_month)){
    if (!str_i.match(reg_mon)) {
        alert(Info + "格式錯誤，應為 YYYY(西元年)/MM/DD !");
        InField.focus();
        InField.select();
        return false;
    }else{
        for (i=1;i<=12;i++){
            if (str_i.match(new RegExp(arr_mon[i],"i"))){
                str_i = str_i.replace(new RegExp(arr_mon[i],"i"),i);
            }
        }
    }
}
// find seperate
if (str_p.match(reg_s)){
    sep  = str_p.match(reg_s);
    seps = new RegExp("\\"+sep,"g");
    if (str_i.match("\\"+sep) && str_i.match(seps).length == 2){
        p_element=str_p.split(sep);
        i_element=str_i.split(sep);
    }else{
        alert(Info + "格式錯誤，應為 YYYY(西元年)/MM/DD !");
        InField.focus();
        InField.select();
        return false;
    }
// no seperate
}else{
    var cnt =0 ,arr_cnt =0;
    while (arr_cnt < 3){
        p_s = str_p.substr(cnt,1);
        p_l = str_p.match(new RegExp(p_s,"gi")).length;
        p_element[arr_cnt] = str_p.substr(cnt,p_l);
        i_element[arr_cnt] = str_i.substr(cnt,p_l);
        arr_cnt = arr_cnt + 1;
        cnt = cnt + p_l;
    }
}
        for (i=0;i<=2;i++){
            if (p_element[i].match(/y|r/i)){
                var yy = i_element[i];
                var reg_y = "\\b\\d{2,"+p_element[i].match(reg_ycnt).length+"}\\b";}
            if (p_element[i].match(/m/i)){
                if (i_element[i].length<2){
                    var mm = "0"+i_element[i];
                }else{
                    var mm=i_element[i];
                }}
            if (p_element[i].match(/d/i)){
                if (i_element[i].length<2){
                    var dd = "0"+i_element[i];
                }else{
                    var dd=i_element[i];
                }}
        }


	if (yy.match(reg_y) && (mm>0 && mm<13) && (dd>0 && dd<32)){
    		if ((mm==4||mm==6||mm==9||mm==11) && dd > 30) {
        		alert("該月僅有 30 天 !")
        		InField.focus();
        		InField.select();
        		return false;
    		}else if (mm==2) {
        		if ((parseInt(yy)+1911) % 4 > 0 && dd > 28) {
            		alert("該月僅有 28 天 !");
            		InField.focus();
            		InField.select();
            		return false;
        		} else if ((parseInt(yy)+1911) % 4 == 0 && dd > 29) {
            		alert("該月僅有 29 天 !");
            		InField.focus();
            		InField.select();
            		return false;
        		}
    		}else if (dd > 31){
        		alert("該月僅有 31 天 !");
        		InField.focus();
        		InField.select();
        		return false;
    		}
				arrDate = str_i.split("/");
				if (arrDate[1].length == 1) {
					arrDate[1] = '0' + arrDate[1];
				}
				if (arrDate[2].length == 1) {
					arrDate[2] = '0' + arrDate[2];
				}				
				str_i = arrDate[0] + '/' + arrDate[1] + '/' + arrDate[2];
    		InField.value = str_i;
    		return true;
	}else{
    		alert(Info + "格式錯誤，應為 YYYY(西元年)/MM/DD !");
    		InField.focus();
    		InField.select();
    		return false;
	}
}	

////////////////////////////////////////////////////////////
// 說明 : 檢查 E-mail 格式
// 作著 : maduka Bor
// 日期 : 2002/10/18
////////////////////////////////////////////////////////////	
function checkemail(InField,Info) {
	var email = InField.value;
	invalidChars = " /:,;"
	if (email != "") {
		for (i=0; i<invalidChars.length; i++) {
			badChar = invalidChars.charAt(i)
			if (email.indexOf(badChar,0) != -1) {
				alert(Info + "E-mail 格式錯誤!");
	      InField.focus();
	      InField.select();				
				return false;
			}
		}
		atPos = email.indexOf("@",1)
		if (atPos == -1) {
			alert(Info + "E-mail 格式錯誤!");
	    InField.focus();
	    InField.select();				
			return false;
		}
		if (email.indexOf("@",atPos+1) != -1) {
			alert(Info + "E-mail 格式錯誤!");
	    InField.focus();
	    InField.select();	
			return false;
		}
		periodPos = email.indexOf(".",atPos)
		if (periodPos == -1) {
			alert(Info + "E-mail 格式錯誤!");
	    InField.focus();
	    InField.select();	
			return false;
		}
		if (periodPos+3 > email.length)	{
			alert(Info + "E-mail 格式錯誤!");
	    InField.focus();
	    InField.select();	
			return false;
		}
	}	
	return true;
}
////////////////////////////////////////////////////////////
// 說明 : 檢查身份證字號
// 作著 : maduka Bor
// 日期 : 2002/10/18
////////////////////////////////////////////////////////////	
function check_personid(InField,Info) {
  	if (InField.value.length != 0) {
	var L1, D1, D2, D3, D4, D5, D6, D7, D8, D9;
	var X1, X2;	
	var Y;
	var X;
	var t= "ABCDEFGHJKLMNPQRSTUVXYWZIO";

	L1 = InField.value.substring(0,1);
	D = InField.value.substring(2,10);
	D1 = parseInt(InField.value.substring(1,2));
	D2 = parseInt(InField.value.substring(2,3));
	D3 = parseInt(InField.value.substring(3,4));
	D4 = parseInt(InField.value.substring(4,5));
	D5 = parseInt(InField.value.substring(5,6));
	D6 = parseInt(InField.value.substring(6,7));
	D7 = parseInt(InField.value.substring(7,8));
	D8 = parseInt(InField.value.substring(8,9));	
	D9 = parseInt(InField.value.substring(9,10));	
	c = t.indexOf(L1.toUpperCase());
	
	if (isNaN(D)) {
    alert(Info + '身份證格式錯誤，第二至第十個字應為數字 !');
    InField.select();
    InField.focus();	
		return false;		
	}
	if((InField.value.length != 10) || (c<0)) {
    alert(Info + '身份證格式錯誤，第一個字應為字母 !');
    InField.select();
    InField.focus();	
		return false;	
	}
	X = parseInt( c+ 10);
	X1 = Math.floor(parseInt(X) / 10);
	X2 = X - X1 * 10;	
	
	Y = parseInt(X1 + 9 * X2 + 8 * D1 + 7 * D2 + 6 * D3 + 5 * D4 + 4 * D5 + 3 * D6 + 2 * D7 + D8 + D9);

  if ((Y % 10) != 0) {
    alert(Info + '身份證格式錯誤 !');
    InField.select();
    InField.focus();
    return false;
  }
}
  //alert('身份證格式正確 !');  
	
	return true;
}

////////////////////////////////////////////////////////////
// 說明 : 檢查統一編號
// 作著 : maduka Bor
// 日期 : 2002/10/18
////////////////////////////////////////////////////////////	
function check_compid(InField,Info) {
  	if (InField.value.length != 0) {
	var D1, D2, D3, D4, D5, D6, D7, D8;
	var C1, C2, C3, C4, a1, b1, a2, b2, a3, b3, a4, b4;	
	var Y;
	var a5;

  // (a)
	D1 = parseInt(InField.value.substring(0,1));
	D2 = parseInt(InField.value.substring(1,2));
	D3 = parseInt(InField.value.substring(2,3));
	D4 = parseInt(InField.value.substring(3,4));
	D5 = parseInt(InField.value.substring(4,5));
	D6 = parseInt(InField.value.substring(5,6));
	D7 = parseInt(InField.value.substring(6,7));
	D8 = parseInt(InField.value.substring(7,8));

	// (b)							
  C1 = parseInt(D1 * 1);
  C2 = parseInt(D3 * 1);
  C3 = parseInt(D5 * 1);
  C4 = parseInt(D8 * 1);
  a1 = Math.floor(parseInt(D2 * 2) / 10);
  b1 = parseInt(D2 * 2) - a1 * 10; 
  a2 = Math.floor(parseInt(D4 * 2) / 10);
  b2 = parseInt(D4 * 2) - a2 * 10;
  a3 = Math.floor(parseInt(D6 * 2) / 10);
  b3 = parseInt(D6 * 2) - a3 * 10;
  a4 = Math.floor(parseInt(D7 * 4) / 10);
  b4 = parseInt(D7 * 4) - a4 * 10;
          
  // (c)
  if (D7 != 7) {
  	Y = parseInt(C1 + a1 + b1 + C2 + a2 + b2 + C3 + a3 + b3 + C4 + a4 + b4);
  } else {	
    a5 = Math.floor(parseInt(a4 + b4) / 10);
    Y = parseInt(C1 + a1 + b1 + C2 + a2 + b2 + C3 + a3 + b3 + C4 + a5) ;
  }
  if ((Y % 10) != 0) {
    alert(Info + '統一編號格式錯誤 !');
    InField.select();
    InField.focus();
    return false;
  }
}
  //alert(Info + '統一編號格式正確 !');  
	return true;
}
////////////////////////////////////////////////////////////
// 說明 : 檢查字串長度是不是大於所設定之長度
// 作著 : Robby Lee
// 日期 : 2001/06/21
////////////////////////////////////////////////////////////
function checkCMaxStrlen(InField, I_MaxLen,Info) {
 	var intStrLen = 0;
	for (var i = 0; i < InField.value.length; i++) {
		num = InField.value.charCodeAt(i);
		if (num < 0) {
		  intStrLen += 2;
		} else if (num > 127) {
		  intStrLen += 2;
		} else {
		intStrLen++;
		}
	}
	if (intStrLen == 0) {
		return true;
	} else if (intStrLen > I_MaxLen) {
  	alert(Info + "輸入長度太大! 本欄位最大為 " + I_MaxLen + "字!");
    InField.focus();
    InField.select();		
		return false;
	} else {
		return true;
	} 	
}

////////////////////////////////////////////////////////////
// 說明 : 檢查CHECKBOX欄位是否未選取
// 作著 : Charly Chang
// 日期 : 2005/08/11
////////////////////////////////////////////////////////////
function isRdoChk(obj,Info){
	//alert(obj.length);

	var blnChk = false;
	if (obj.value != null)		//只有一個選項時
	{
//		if (obj.checked){
			obj.checked=true;
			blnChk = true;
//		}

	} else {					//二個以上選項時

		for (var i = 0;i < obj.length ;i++ )
		{
			if (obj[i].checked)
			{
				blnChk = true;
				break;
			}

		}

	}

	if (!blnChk){
		alert(Info + "至少要勾選一項");
	}


	return blnChk;
}

////////////////////////////////////////////////////////////
// 說明 : 檢查CHECKBOX欄位是否僅選取一項
// 作著 : Charly Chang
// 日期 : 2006/05/17
////////////////////////////////////////////////////////////
function isRdoSelChk(obj,Info){
	var blnChk = true;
	var j=0;

	if (obj.value == null)		//只有一個選項時
	{
		for (var i = 0;i < obj.length ;i++ )
		{
			if (obj[i].checked)
			{
				j++
		}

		}
		if (j != 1)
		{
			alert(Info + "僅能勾選一項");
			blnChk=false;
		}
	}

	return blnChk;
}


////////////////////////////////////////////////////////////
// 說明 : 檢查Select欄位是否未選取
// 作著 : Charly Chang
// 日期 : 2007/11/27
////////////////////////////////////////////////////////////	
function checkCombo(InField,Info) {
  	if (InField.value == "") {
  			if(typeof message != 'undefined') alert(message);
     		else alert(Info + " field cannot be left blank!");
      	return false;
  	}
  	return true;
}
