﻿// 檢查是否輸入資料
function isDataNull(theObj, msg) {
    if (theObj.value == "") {
        alert("請注意,這個欄位 \"" + msg + "\" 空白,請輸入資料 !");
        theObj.focus();
        return true;
    }
    return false;
}
//檢查電話
function isPhoneNoValided(theObj, msg) {
    var num = theObj.value;
    if (num.length == 0) return true;
    var valid = "1234567890-()";
    if ( (num.length < 7) || (num.length > 20) ){
        alert(msg + "輸入錯誤 !");
        theObj.focus();
        return false;
    }
    for (var i = 0 ; i < num.length; i++) {
        if ( valid.indexOf(num.charAt(i)) ==  -1) {
            alert(msg + "輸入錯誤 !");
            theObj.focus();
            return false;
        }
    }
    return true;
}
// 檢查 E-mail
function isEmailValided(theObj, msg) {
    var message = "很抱歉,您所輸入的 email 不符合格式,請再確認 email 是否正確 !";
    var email = theObj.value;
    if (email.length == 0) return true;
    var len=email.length;
    for(var i=0;i<len;i++) {
        var c=email.charAt(i);
        //電子郵件地址只能是數字,英文字母及'-','_','.','@'等符號,其他的符號都不能使用
        if( !((c>="A"&&c<="Z")||(c>="a"&&c<="z")||(c>="0"&&c<="9")||(c=="-")||(c=="_")||(c==".")||(c=="@")) ){
            alert(message);
            theObj.focus();
            return false;
        }
    }
   if((len<8)||(len>50)){         //輸入之電子郵件地址小於8碼或大於50碼
      alert(message);  theObj.focus(); return false;
   }else if((email.indexOf("@")==-1)||(email.indexOf("@")==0)||(email.indexOf("@")==(len-1))){       //沒有或第一個或最後一個字為@
      alert(message);  theObj.focus(); return false;
   }else if((email.indexOf("@")!=-1)&&(email.substring(email.indexOf("@")+1,len).indexOf("@")!=-1)){ //有兩個@
      alert(message);  theObj.focus(); return false;
   }else if((email.indexOf(".")==-1)||(email.indexOf(".")==0)||(email.lastIndexOf(".")==(len-1))){   //沒有或第一個或最後一個字為.
      alert(message);  theObj.focus(); return false;
   }else if((email.indexOf("@.")!=-1)||(email.indexOf(".@")!=-1)||(email.indexOf("..")!=-1)){        //. 與 @ 或 .連在一起
      alert(message);  theObj.focus(); return false;
   }
   return true;
}
//檢查是否為數值
function chkNum(theObj,msg){
    if (theObj.value.length == 0) return true;
    if(isNaN(theObj.value)){
    	alert(msg+"必須輸入數值 !");
    	theObj.focus();
    	return false;
    }
    return true;
}
//檢查是否為整數值
function chkInt(theObj,msg){
    if (theObj.value.length == 0) { return true; }
    if(chkNum(theObj,msg)==false) {
        return false;
    }else if(theObj.value.indexOf('.')!=-1){
        alert(msg+"必須輸入整數 !");
        theObj.focus();
        return false;
    }
    return true;
}
function chkMinInt(theObj,num,msg) {
    if (theObj.value.length == 0) { return true; }
    if (!chkInt(theObj, msg)) { return false; }
    if (parseInt(theObj.value, 10) < parseInt(num, 10)) {
        alert(msg+"不得小於 " + num + " !");
        theObj.focus();
        return false;
    }
    return true;
}
function chkMaxInt(theObj,num,msg) {
    if (theObj.value.length == 0) { return true; }
    if (!chkInt(theObj, msg)) { return false; }
    if (parseInt(theObj.value, 10) > parseInt(num, 10)) {
        alert(msg+"不得大於 " + num + " !");
        theObj.focus();
        return false;
    }
    return true;
}
function chkMinMaxInt(theObj, minnum, maxnum, msg) {
    if (!chkMinInt(theObj, minnum, msg)) { return false ;
    } else if (!chkMaxInt(theObj, maxnum, msg)) {  return false;
    }
    return true;
}
//檢查是否符合代號之規定,代號只能輸入英文字母,數字  . - _  不符則回傳 false
function chkCode(theObj, msg){
    var str = theObj.value;
    var len = str.length;
    if (len == 0) return true;
    var flag = 0;
    for(var i = 0; i < len; i++){
        var no=str.charCodeAt(i);
        if((no>122)||(no<45)){
            alert(msg+"輸入錯誤 !");  theObj.focus(); return false;
        }else if((no>57)&&(no<65)){
            alert(msg+"輸入錯誤 !");  theObj.focus(); return false;
        }else if(no==91){
            alert(msg+"輸入錯誤 !");  theObj.focus(); return false;
        }else if((no>92)&&(no<97)){
            alert(msg+"輸入錯誤 !");  theObj.focus(); return false;
        }else{
            flag++;
        }
    }
    if(flag==len){
        return true;
    }else{
        alert(msg+"輸入錯誤 !");  theObj.focus(); return false;
    }
}
//檢查欄位長度之範圍
function chkMinMaxLen(theObj,minLen,maxLen,msg){
   if(!chkMinLen(theObj,minLen,msg)){ return false;
   }else if(!chkMaxLen(theObj,maxLen,msg)){ return false;
   }
   return true;
}
//檢查欄位之最小長度
function chkMinLen(theObj,minLen,msg){
   if(theObj.value.length < minLen){
      //alert(msg + "不得少於 " + minLen + " 個字 !"); theObj.focus(); return false;
      alert("很抱歉,您所輸入 \"" + msg + "\" 這個數字過少,請更正 !"); theObj.focus(); return false;
   }
   return true;
}
//檢查欄位之最大長度
function chkMaxLen(theObj,maxLen,msg){
   if(theObj.value.length > maxLen){
      alert("很抱歉,您所輸入 \"" + msg + "\" 這個欄位超過數字限制,請刪除部分文字 !"); theObj.focus(); return false;
   }
   return true;
}
//比較兩日期之前後
function compareDate(Y1,M1,D1,msg1,Y2,M2,D2,msg2){
    msg = msg2 + " 應在 " + msg1 + " 之後 !";
    if(Y1>Y2){
        alert(msg); return false;
    }else if(Y1==Y2){
       if(M1>M2){
          alert(msg); return false;
       }else if(M1==M2){
          if(D1>D2){
             alert(msg); return false;
          }
       }
   }
   return true;
}
// 檢查 checkbox 欄位是否勾選
function isCheckboxSelected(theObj) {
    var cbLen = theObj.length;
    if (cbLen > 1) {
        for (var i = 0; i < cbLen; i++) {
            if (theObj[i].checked == true) return true;
        }
        return false;
    } else {
        return theObj.checked;
    }
}

// 檢查 E-mail 前台
function isEmailValidedf(theObj) {
    var email = theObj.value;
    if (email.length == 0) return true;
    var len=email.length;
    for(var i=0;i<len;i++) {
        var c=email.charAt(i);
        //電子郵件地址只能是數字,英文字母及'-','_','.','@'等符號,其他的符號都不能使用
        if( !((c>="A"&&c<="Z")||(c>="a"&&c<="z")||(c>="0"&&c<="9")||(c=="-")||(c=="_")||(c==".")||(c=="@")) ){

            theObj.focus();
            return false;
        }
    }
   if((len<8)||(len>50)){         //輸入之電子郵件地址小於8碼或大於50碼
        theObj.focus(); return false;
   }else if((email.indexOf("@")==-1)||(email.indexOf("@")==0)||(email.indexOf("@")==(len-1))){       //沒有或第一個或最後一個字為@
        theObj.focus(); return false;
   }else if((email.indexOf("@")!=-1)&&(email.substring(email.indexOf("@")+1,len).indexOf("@")!=-1)){ //有兩個@
        theObj.focus(); return false;
   }else if((email.indexOf(".")==-1)||(email.indexOf(".")==0)||(email.lastIndexOf(".")==(len-1))){   //沒有或第一個或最後一個字為.
        theObj.focus(); return false;
   }else if((email.indexOf("@.")!=-1)||(email.indexOf(".@")!=-1)||(email.indexOf("..")!=-1)){        //. 與 @ 或 .連在一起
        theObj.focus(); return false;
   }
   return true;
}
//檢查輸入字  是否合法  (0-9，a-z，A-Z)
function checkLawChar( str ) {
      for ( var i = 0 ; i < str.length ; i++ ) {
          var c = str.substr( i, 1 );
          if ( (c < '0' || c > '9') && (c < 'a' || c > 'z') && (c < 'A' || c > 'Z') ) {
               return false;
          }
      }
      return true;
}
