﻿var emailReg = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
var dateRegEx = /^((((1[6-9]|[2-9]\d){1}\d{2})[\/](0?[13578]|1[02])[\/](0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d){1}\d{2})[\/](0?[13456789]|1[012])[\/](0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d){1}\d{2})[\/]0?2[\/](0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d){1}(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00)[\/]0?2[\/]29))$/;
var ipReg = /^((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})$/;
var pswdReg = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?!.*\s).{6,8}$/;
//var pswdRegS = /(?=^.{6,8}$)((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$/;

function blockEnter() {
    if (window.event.keyCode == 13) {
        event.returnValue = false;
        event.cancel = true;
    }
}

var whichButton;
function changeRed() {
    window.event.srcElement.style.color = "red";
    whichButton = window.event.srcElement.id;
}
function changeBack() {
    window.event.srcElement.style.color = "#41509D";
    whichButton = "";
}

//使用IMOKisNaN()以防止全形字的阿拉伯數字
function imokIsNaN(inputString) {
    if (isNaN(inputString)) {
        return true;
    }
    else {
        for (var i = 0; i < inputString.length; i++) {
            //45 is the unicode value of the minus
            //46 is the unicode value of the Period character
            if (inputString.charCodeAt(i) != 46 && inputString.charCodeAt(i) != 45) {
                //48 is the unicode/ascii value of zero, 57 is the uncode/ascii value of nine.
                if (inputString.charCodeAt(i) < 48 || inputString.charCodeAt(i) > 57) {
                    return true;
                }
            }
        }
        return false; //meaning the inputString is indeed a number
    }
}

function curbBackSpace(e) {
    if (e.keyCode == 8) window.event.keyCode = 0;
}


function checkId(theId) {
    if (theId.length != 10) {
        return false;
    }

    if (theId.substr(1, 1) == "1" || theId.substr(1, 1) == "2") {
        //if the second letter is 1 or 2, means the input is memPid

        var fstId = theId.substr(0, 1).toUpperCase();
        if (fstId > "Z" || fstId < "A") {
            return false;
        }

        var idNo = 0;

        if (fstId <= "H" && fstId >= "A") {
            idNo = fstId.charCodeAt() - "A".charCodeAt() + 10;
        }

        if (fstId <= "N" && fstId >= "J") {
            idNo = fstId.charCodeAt() - "J".charCodeAt() + 18;
        }

        if (fstId <= "V" && fstId >= "P") {
            idNo = fstId.charCodeAt() - "P".charCodeAt() + 23;
        }

        if (fstId <= "Y" && fstId >= "X") {
            idNo = fstId.charCodeAt() - "X".charCodeAt() + 30;
        }

        if (fstId == "W") {
            idNo = 32;
        }

        if (fstId == "Z") {
            idNo = 33;
        }

        if (fstId == "I") {
            idNo = 34;
        }

        if (fstId == "O") {
            idNo = 35;
        }

        var idPhase = idNo + theId.substr(1, 8); //注意這裡原先使用substring(1,9), 其實等於substr(1,8)
        var theBase = "1987654321";
        var RstNo = 0;

        for (var i = 0; i < idPhase.length; i++) {
            RstNo = RstNo + (parseInt(idPhase.charAt(i)) * parseInt(theBase.charAt(i))) % 10;
        }

        if (((10 - (RstNo % 10)) % 10) == parseInt(theId.substr(9, 1))) {
            return true;
        }
        else {
            return false;
        }
    }
    else {
        //if the second letter is not 1 or 2, means the input is foreign resident ID
        if (isNaN(theId.substr(2, 8)) || (theId.substr(0, 1) < "A" || theId.substr(0, 1) > "Z") || (theId.substr(1, 1) < "A" || theId.substr(1, 1) > "Z")) {
            return false;
        }

        var head = "ABCDEFGHJKLMNPQRSTUVXYWZIO";
        var newId = (head.indexOf(theId.substr(0, 1)) + 10) + '' + ((head.indexOf(theId.substr(1, 1)) + 10) % 10) + '' + theId.substr(2, 8);
        var s = parseInt(newId.substr(0, 1)) +
		    parseInt(newId.substr(1, 1)) * 9 +
		    parseInt(newId.substr(2, 1)) * 8 +
		    parseInt(newId.substr(3, 1)) * 7 +
		    parseInt(newId.substr(4, 1)) * 6 +
		    parseInt(newId.substr(5, 1)) * 5 +
		    parseInt(newId.substr(6, 1)) * 4 +
		    parseInt(newId.substr(7, 1)) * 3 +
		    parseInt(newId.substr(8, 1)) * 2 +
		    parseInt(newId.substr(9, 1)) +
		    parseInt(newId.substr(10, 1));
        //判斷是否可整除
        if ((s % 10) != 0) {
            return false;
        }
        //統一證號碼正確
        return true;
    }
}
