﻿//純數字
function checkPhone(invoiceNumber) {
    var regularExpression = /^[0-9]+$/;

    return invoiceNumber.match(regularExpression);
}
//字母+數字
function checkAccount(invoiceNumber) {
    var regularExpression = /^[a-zA-Z0-9]+$/;

    return invoiceNumber.match(regularExpression);
}

//字母+數字+"-"
function checkAccount(invoiceNumber) {
    var regularExpression = /^[a-zA-Z0-9.-]+$/;

    return invoiceNumber.match(regularExpression);
}

//mail格式
function checkmail(invoiceNumber) {
    var regularExpression = /^[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;

    return invoiceNumber.match(regularExpression);
}
//身分證格式
function checkTwID(id) {
    //建立字母分數陣列(A~Z)  
    var city = new Array(
         1, 10, 19, 28, 37, 46, 55, 64, 39, 73, 82, 2, 11,
        20, 48, 29, 38, 47, 56, 65, 74, 83, 21, 3, 12, 30
    )
    id = id.toUpperCase();
    // 使用「正規表達式」檢驗格式  
    if (id.search(/^[A-Z](1|2)\d{8}$/i) == -1) {
        //alert('基本格式錯誤');  
        return false;
    } else {
        //將字串分割為陣列(IE必需這麼做才不會出錯)  
        id = id.split('');
        //計算總分  
        var total = city[id[0].charCodeAt(0) - 65];
        for (var i = 1; i <= 8; i++) {
            total += eval(id[i]) * (9 - i);
        }
        //補上檢查碼(最後一碼)  
        total += eval(id[9]);
        //檢查比對碼(餘數應為0);  
        return ((total % 10 == 0));
    }
}
//有效日期
function checkdate(date) {
    var check = false
    if (date == "") {
        check = true;
    } else if (parseInt(date.split("/")[0]) >= 1 && (parseInt(date.split("/")[1]) >= 1 && parseInt(date.split("/")[1]) <= 12) && parseInt(date.split("/")[2]) >= 1) {
        switch (parseInt(date.split("/")[1])) {
            case 1:
                if (parseInt(date.split("/")[2]) > 31) {
                    check = true;
                } break;
            case 2:
                if (parseInt(date.split("/")[0]) % 4 != 0 && parseInt(date.split("/")[2]) > 28) {
                    check = true;
                } else if (parseInt(date.split("/")[2]) > 29) {
                    check = true;
                } break;
            case 3:
                if (parseInt(date.split("/")[2]) > 31) {
                    check = true;
                } break;
            case 4:
                if (parseInt(date.split("/")[2]) > 30) {
                    check = true;
                } break;
            case 5:
                if (parseInt(date.split("/")[2]) > 31) {
                    check = true;
                } break;
            case 6:
                if (parseInt(date.split("/")[2]) > 30) {
                    check = true;
                } break;
            case 7:
                if (parseInt(date.split("/")[2]) > 31) {
                    check = true;
                } break;
            case 8:
                if (parseInt(date.split("/")[2]) > 31) {
                    check = true;
                } break;
            case 9:
                if (parseInt(date.split("/")[2]) > 30) {
                    check = true;
                } break;
            case 10:
                if (parseInt(date.split("/")[2]) > 31) {
                    check = true;
                } break;
            case 11:
                if (parseInt(date.split("/")[2]) > 30) {
                    check = true;
                } break;
            case 12:
                if (parseInt(date.split("/")[2]) > 31) {
                    check = true;
                } break;
        }
    }
    return check
}
//上線欄位驗證錯誤訊息
function showErrorMessage(Target, msg) {
    $(Target).html(msg);
}

//Add validator 後者日期不可早於前者日期
$.validator.methods.dateBoth = function(value, element, param) {
    return this.optional(element) || !(!(value.match(/^\d{4}[\/-]\d{1,2}[\/-]\d{1,2}$/) == null || $("#" + param).val().match(/^\d{4}[\/-]\d{1,2}[\/-]\d{1,2}$/) == null) && Date.parse(value) > Date.parse($("#" + param).val()));
};

//Add validator 後者數字不可大於前者數字
$.validator.methods.numberBoth = function(value, element, param) {
return this.optional(element) || !(!(value.match(/^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/) == null || $("#" + param).val().match(/^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/) == null) && parseFloat(value) < parseFloat($("#" + param).val()));
};

//Add validator 數字總合100
$.validator.methods.SumBoth = function(value, element, param) {
    return this.optional(element) || !(!(value.match(/^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/) == null || $("#" + param).val().match(/^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/) == null) && parseFloat(value) + parseFloat($("#" + param).val()) != 100);
};

//Add validator 限字母+數字
$.validator.methods.AlphaNum = function(value, element, param) {
    return this.optional(element) || checkAccount(value) != null;
};

//Add validator 國內公司專長驗證
$.validator.methods.Skill = function(value, element, param) {
    var check = true;
    var i = 1;
    $("textarea[name=CounselObject]").each(function() {
        if (($(this).val() == "" && $("#CounselContent" + i).val() != "") || ($(this).val() != "" && $("#CounselContent" + i).val() == "")) check = false;
        i++;
    });
    return check;
};
