﻿// maxLength: 限制長度
// targetObj: 檢查的欄位
// displayObj: 顯示剩餘的字數
function calculate_wordLength(maxLimit, targetObj, displayObj) {
    // 計算目前輸入的字串長度
    var charcnt = countLength($("#" + targetObj).val());

    if (charcnt > maxLimit) {
        $("#" + targetObj).val(substr($("#" + targetObj).val(), maxLimit));
    }
    else {
        $("#" + displayObj).text(maxLimit - charcnt);
    }
}

function countLength(stringToCount) {
    //計算有幾個全型字、中文字...
    var c = stringToCount.match(/[^ -~]/g);
    return stringToCount.length + (c ? c.length : 0);
}

function substr(str, len) {
    if (!str || !len) {
        return '';
    }

    var a = 0;
    var i = 0;
    var temp = '';

    for (i = 0; i < str.length; i++) {
        if (str.charCodeAt(i) > 255) {
            a += 2;
        }
        else {
            a++;
        }

        if (a > len) {
            return temp;
        }

        temp += str.charAt(i);
    }

    return str;
}

// Error Msg
function CommonErrMsg(vMsgCode, vField, vArgu1) {
    var vMSG = '';

    switch (vMsgCode) {
        case 1:
            vMSG = document.getElementById("CommonErrMsg1").value;
            vMSG = vMSG.replace('{0}', vField);
            break;
        case 2:
            vMSG = document.getElementById("CommonErrMsg2").value;
            break;
        case 3:
            vMSG = document.getElementById("CommonErrMsg3").value;
            vMSG = vMSG.replace('{0}', vField);
            break;
        case 4:
            vMSG = document.getElementById("CommonErrMsg4").value;
            vMSG = vMSG.replace('{0}', vField);
            break;
        case 5:
            vMSG = document.getElementById("CommonErrMsg5").value;
            vMSG = vMSG.replace('{0}', vField);
            vMSG = vMSG.replace('{1}', vArgu1);
            break;
        case 6:
            vMSG = document.getElementById("CommonErrMsg6").value;
            vMSG = vMSG.replace('{0}', vField);
            break;
        case 7:
            vMSG = document.getElementById("CommonErrMsg7").value;
            vMSG = vMSG.replace('{0}', vField);
            break;
       //Gary   Add  20100520    獲取密碼不合法提示信息
        case 8:
            vMSG = document.getElementById("CommonErrMsg8").value;
            vMSG = vMSG.replace('{0}', vField);
            break;
        default: vMSG = 'Unknown Msg Code.[' + vField+']';
    }
    alert(vMSG);
    return true;
}

//判斷日期格式(YYYYMMDD)
function chkDate(str)
{
    if (str.length != 8 )
    {
        return false;
    }

    if (str.match(/^(\d{4})(\d{2})(\d{2})$/) == null)
    {
        return false;
    }
    var sYear = eval(RegExp.$1);
    var sMonth = eval(RegExp.$2) - 1;
    var sDay = eval(RegExp.$3);
    if (sYear < "2000")
    {
        sYear = sYear + 1900;
    }
    var da = new Date(sYear, sMonth, sDay);
    if (sMonth == da.getMonth() && sDay == da.getDate())
        return true;
    else
        return false;
}

// Checking function
function ChkGroup() {
    var reg;
    this.EmptyCheck = function(Label, vControl) {
      if(vControl!=null)
      {
        if (vControl.value == "") { vControl.focus(); return CommonErrMsg(6, Label); }
      }
    };
    this.CharNumberCheck = function(Label, vControl) {
        reg = /\W/g;
        if (reg.test(vControl.value)) { vControl.focus(); return CommonErrMsg(7, Label); }
    };
    this.EmailCheck = function(Label, vControl) {
        reg = /\w[\w.-]+@[\w-]+(\.\w{2,})+/g;
        if (!reg.test(vControl.value)) { vControl.focus(); return CommonErrMsg(2, Label); }
    };
    //检测密码长度是否合法
    this.PassCheck = function(Label, vControl) {
        //reg = /^[a-zA-Z0-9]{7,}$/; Gary Modify 20100707
    if (vControl.value.length >= 7) {
            reg = /^[0-9a-zA-Z]*([a-zA-Z]+[0-9]+|[0-9]+[a-zA-Z]+)[0-9a-zA-Z]*$/;
            if (!reg.test(vControl.value)) { vControl.focus(); return CommonErrMsg(8, Label); }
        }
        else { vControl.focus(); return CommonErrMsg(8, Label); }
    };
    this.NumberCheck = function(Label, vControl) {
        reg = /[^0-9]/g;
        if (reg.test(vControl.value)) { vControl.focus(); return CommonErrMsg(3, Label); }
    }
    this.LengthCheck = function(Label, vControl, vlength) {
        var str;
        var len = 0;
        var nLength = 0;
        if (vlength != undefined) { nLength = vlength; }
        else if (vControl.getAttribute('maxlength') != undefined) { nLength = vControl.getAttribute('maxlength'); }
        else if (vControl.value.length != undefined) { nLength = vControl.value.length; }
        else { return CommonErrMsg(0, Label); }
        //        str = vControl.value;
        //        for (var i = 0; i < str.length; i++) {
        //            len += str.charCodeAt(i) > 0xff ? 2 : 1;
        //        }
        if (vControl.length != undefined) { len = vControl.length; }
        else if (vControl.value.length != undefined) { len = vControl.value.length; }
        else { return CommonErrMsg(0, Label); }
        
        if (len > nLength) { vControl.focus(); return CommonErrMsg(5, Label, nLength); }
    }
    this.DateCheck = function(Label, vControl, Format)
    {
        if (!chkDate(vControl.value.replace(Format, "").replace(Format, "")))
        {
            vControl.focus(); return CommonErrMsg(1, Label);
        }
    }
    this.OtherCheck = function(Label, vControl) {
        // 尚未實作
        vControl.focus(); return CommonErrMsg(4, Label);
    }
}
