//表單驗証
var formValidate = {
    /**
     * 檢查是否為空字串
     * @param obj
     * @param title
     * @return true:非空字串 false:空字串
     */
    checkEmpty: function(obj, title) {
        if (obj.value == "") {
            alert(" Please type in e-mail by correct form. ");
            obj.focus();
            return false;
        } else {
            return true;
        }
    },
    
    /**
     * 檢查 Email 格式
     * @param obj
     * @param title
     * @return true:格式正確 false:格式錯誤
     */
    checkEmail: function(obj, title) {
        var regularExpression = /^[^\s]+@[^\s]+\.[^\s]{2,3}$/;
        if (!regularExpression.test(obj.value)) {
            alert(title+" Please type in e-mail by correct form. ");
            obj.focus();
            return false;
        } else {
            return true;
        }
    },
    
    /**
     * 檢查是否數字
     * @param obj
     * @param title
     * @return true:為數字 false:不為數字
     */
    checkIsNumber: function(obj, title) {
        if (isNaN(obj.value)) {
            alert(title+" 只能輸入數字 ");
            obj.focus();
            return false;
        } else {
            return true;
        }
    },
    
    /**
     * 檢查字串長度
     * 利用 getbyteStringLen() 可以中文字算成 3
     * @param obj
     * @param title
     * @param limit 限制長度
     * @return true:長度合法 false:長度超過限制
     */
    checkLength: function(obj, title, limit) {
        var strLen = getbyteStringLen(obj.value);
        if (strLen > limit) {
            alert(title+" 長度超過限制!!");
            obj.focus();
            return false;
        } else {
            return true;
        }
    }
}

/**
 * 取得字串長度 (可區分中文字)
 * @param strVal 輸入字串
 */
function getbyteStringLen(strVal) {
    nLen = 0;
    for (i=0; i<strVal.length; i++) {
        if (strVal.charCodeAt (i) > 255)
            nLen+= 3;
        else
            nLen++;
    }
    return nLen;
}