/*
#################################################
##
##  DH JScript 2.0
##
#################################################
##
##  Coding by 侯志成 2004/2/18
##  MSN : damonhou@hotmail.com
##  E-mail : damon.h@yahoo.com.tw
##
#################################################
#################################################
##
##  清除字串頭尾的空白字元
##
#################################################
*/
function Trim(variable) {
  return variable.replace(/^\s+|\s+$/g, "")
}
/*
#################################################
#################################################
##
##  清除字串開頭的空白字元
##
#################################################
*/
function LTrim(variable) {
  return variable.replace(/^\s+/g, "")
}
/*
#################################################
#################################################
##
##  清除字串結尾的空白字元
##
#################################################
*/
function RTrim(variable) {
  return variable.replace(/\s+$/g, "")
}
/*
#################################################
#################################################
##
##  取字串左邊幾個字元
##
#################################################
*/
function Left(variable, amount) {
  return variable.substr(0, amount)
}
/*
#################################################
#################################################
##
##  取字串右邊幾個字元
##
#################################################
*/
function Right(variable, amount) {
  return variable.substr(variable.length-amount, amount)
}
/*
#################################################
#################################################
##
##  將字串中的字元顛倒排列順序
##
#################################################
*/
function strReverse(variable) {
  var v = ""
  for (i=variable.length; i-->0;) v += variable.charAt(i)
  return v
}
/*
#################################################
#################################################
##
##  傳回特定長度的重複字元字串
##
#################################################
*/
function string(amount, variable) {
  var v = variable
  for (i=amount; i-->1;) v += variable
  return v
}
/*
#################################################
#################################################
##
##  傳回格式化的數字
##
#################################################

formatNumber(v1[, v2[, v3[, v4[, v5]]]])

參數說明：
  v1：欲被格式化的運算式
  v2：此數值表示有多少小數位數
  v3：小數點前是否 "顯示前導零"
  v4：負數值是否帶有括號
  v5：數字是否以 "數位群組符號" 來分隔
*/
function formatNumber(v1, v2, v3, v4, v5) {
  var v = v1.toString(), v2 = v2==null ? 2 : v2
  if (isNaN(v2) && v2 != null) {
    throw Error("\"小數點位數\" 的值必須是數字！")
  } else if (v2 != 0) {
    if (v.match(/\./g)) {
      // 假如 v = "-123.456" 且 v2 = 2 則 v = Math.round("-12345" + "." + "6").toString() = "-12346"
      v = Math.round(v.slice(0, v.indexOf(".")+v2+1).replace(/\./g, "") +
          "." + v.charAt(v.indexOf(".")+v2+1)).toString()
      // 承上例則 v = "-123" + "." + "46" = "-123.46"
      v = Left(v, v.length-v2) + "." + Right(v, v2)
    } else {
      v = v + "." + string(v2, "0")
    }
  }
  if (v3 == 0) {
    v = v.replace(/^0/g, "").replace(/^\-0/g, "-")
  } else if (v3 != 0 && v3 != null && v3 != -1) {
    throw Error("\"是否顯示前導零\" 的參數必須是 -1 或 0 ！")
  }
  if (v4 == -1) {
    v = v.match(/^\-/g) ? "("+v+")" : v
  } else if (v4 != -1 && v4 != null && v4 != 0) {
    throw Error("\"負數是否帶有括弧\" 的參數必須是 -1 或 0 ！")
  }
  if (v5 == -1 || v5 == null) {
    var t = "", s = v.split("."), s0 = s[0].replace(/\)$/g, "")// 若有設定 "負數帶有括弧" 則將結尾的括弧先移除
    // 將逗號一一插入
    for (i=s0.length+1; i-->1;) t = (s0.length-i+1)%3==0 ? ","+s0.charAt(i-1)+t : s0.charAt(i-1)+t
    v = s.length==2 ? t+"."+s[1] : t
    // 若有設定 "負數帶有括弧" 則將結尾的括弧補上
    v = v.match(/^\((.*)[^\)]$/g) ? v+")" : v
    // 移除第一個逗號﹙例如 -,123,456.789 移除變成 -123,456.789﹚
    v = v.replace(/^,/g, "").replace(/^\-,/g, "-").replace(/^\(,/g, "(").replace(/^\(\-,/g, "(-")
  } else if (v5 != -1 && v5 != null && v5 != 0) {
    throw Error("\"是否以數位群組符號來分隔\" 的參數必須是 -1 或 0 ！")
  }
  return v
}
/*
#################################################
#################################################
##
##  表單驗證模組
##
#################################################
##
##  使用範例：
##
##  <form name="form" method="post" action="#">
##    姓　　名：<input type="text" name="name"><br>
##    帳　　號：<input type="text" name="user"><br>
##    密　　碼：<input type="password" name="pass"><br>
##    電子信箱：<input type="text" name="mail"><br>
##    信用卡號：<input type="text" name="card"><br>
##    身分證號：<input type="text" name="pid"><br>
##    統一編號：<input type="text" name="cid"><br><br>
##    <input type="button" value="送出" onClick="subform()">
##  </form>
##  <script lane=jscript src="dh.js"></script>
##  <script lane=jscript>
##  function subform() {
##    if (dhValueCheck("name", "姓名")) {
##    } else if (dhValueCheck("user", "帳號", "", 1)) {
##    } else if (dhValueCheck("pass", "密碼", 8, 1)) {
##    } else if (dhMailCheck("mail")) {
##    } else if (dhCardCheck("card")) {
##    } else if (dhPIDCheck("pid")) {
##    } else if (dhCIDCheck("cid")) {
##    } else {
##      document.form.submit()
##    }
##  }
##  </script>
#################################################
##
##  帳號密碼驗證
##
##===============================================
##
##  dhValueCheck(inputId, inputMsg, maxLength, isCharCheck)
##
##  參數說明：
##        inputId：為欄位的 NAME 或 ID 值
##       inputMsg：為該欄位中文敘述
##      maxLength：最大允許的字元數目
##    isCharCheck：若設定為 true 或 1 則
##  　　　　　      只容許英文字母及數字
##  　　　　  　    否則只判斷是否為空值
##
#################################################
*/
function dhValueCheck(inputId, inputMsg, maxLength, isCharCheck) {
  var d = document.all[inputId], v = d.value, f = d.select() + "break"
  if (v == "") {
    return alert("Please input " + inputMsg + "！") + f
  } else if (maxLength != "" && v.length > maxLength) {
    return alert("「" + inputMsg + "」" + " 最多 " + maxLength + " 個字元！") + f
  } else if ((isCharCheck == true || isCharCheck == 1) && v.match(/[\W]/g)) {
    return alert("「" + inputMsg + "」" + " 必須是英文字母、數字或 _！") + f
  }
}
/*
#################################################
##
##  電子郵件地址驗證
##
##===============================================
##
##  dhMailCheck(inputId)
##
##  參數說明：
##    inputId：為欄位的 NAME 或 ID 值
##
#################################################
*/
function dhMailCheck(inputId) {
  var d = document.all[inputId], v = d.value, f = d.select() + "break"
  if (v == "") {
    return alert("Please input E-mail！") + f
  } else if (!v.match(/@(.*)\./g) || v.match(/@\.|^@|\.$|@(.*)@|\.\.|[^\w\-\._@]/g)) {
    return alert("E-mail is wrong！") + f
  }
}
/*
#################################################
##
##  信用卡號碼驗證
##
##===============================================
##
##  dhCardCheck(inputId)
##
##  參數說明：
##    inputId：為欄位的 NAME 或 ID 值
##
#################################################
*/
// 簡易版〈僅驗證 16 碼規格的信用卡〉-- 原創
function dhSCardCheck (inputId) {
  var d = document.all[inputId], v = d.value, f = d.select() + "break", t = 0
  if (v == "") {
    return alert("請輸入信用卡號碼！") + f
  } else if (!eval(/^\d{16}$/g).test(v)) {
    return alert("信用卡號碼錯誤！") + f
  } else {
    for (i=0;i<15;i++) t = i%2>0 ? t+v.charAt(i)*1 : (v.charAt(i)<5 ? t+v.charAt(i)*2 : t+v.charAt(i)*2-10+1)//i=15; i-->0;
    if (10-t%10 != v.charAt(15)) return alert("信用卡號碼錯誤！") + f
  }
}

// 完整版〈支援 15 種信用卡〉-- 修改自 Mod 10 credit card number validator (2002/09/13)
function dhCardCheck(inputId) {
  var d = document.all[inputId], v = d.value, f = d.select() + "break"
  var pass = 0, sum = 0, len = v.length, s = 0
  if (v == "") {
    return alert("請輸入信用卡號碼！") + f
  } else if (len<13 || len>19) {
    return alert("信用卡號碼錯誤！") + f
  } else {
    for (i=1; i<len; i++) {
      s = parseInt(v.charAt(len-(i+1)))
      if (i%2 == 1) {
        s *= 2
        if (s.toString().length == 2) s = (parseInt(s.toString().charAt(0)) + parseInt(s.toString().charAt(1)))
      }
      sum += s
    }
    sum += parseInt(v.charAt(len-1))
    if (sum%10 == 0) {
      if (v.match(/^2014|^2149/)) {
        pass = len==15 ? 1 : 0// ENROUTE
      } else if (v.match(/^389/)) {
        pass = len==14 ? 1 : 0// CARTE BLANCHE
      } else if (v.match(/^34|^37/)) {
        pass = len==13 || len==15 ? 1 : 0// AMEX
      } else if (v.match(/^3528|^3529|^353|^354|^355|^356|^357|^358|^2131|^1800/)) {
        pass = len==15 || len==16 ? 1 : 0// JCB
      } else if (v.match(/^300|^301|^302|^303|^304|^305|^36|^380|^381|^382|^383|^384|^385|^386|^387|^388/)) {
        pass = len==14 ? 1 : 0// DINERS CLUB
      } else if (v.match(/^405501|^405502|^405503|^405504|^405550|^405551|^405552|^405553|^405554|^415928|^424604|^424604|^427533|^4288|^443085|^4484|^4485|^4486|^4715|^4716|^4804/)) {
        pass = len==16 ? 1 : 0// VISA PURCHASING
      } else if (v.match(/^490300|^490301|^49031|^49032|^490330|^490331|^490332|^490333|^490334|^49034|^49035|^49036|^49037|^49038|^49039|^49040|^490419|^490451|^490459|^490467|^490475|^490476|^490477|^490478|^4905|^491103|^491104|^491105|^491106|^491107|^491108|^491109|^49111|^49112|^49113|^49114|^49115|^49116|^491170|^491171|^491172|^491173|^491183|^491184|^491185|^491186|^491187|^491188|^491189|^49119|^4928|^4987/)) {
        pass = len==16 ? 1 : 0// VISA_ATM
      } else if (v.match(/^413733|^413734|^413735|^413736|^413737|^4462|^453978|^453979|^454313|^454313|^454432|^454433|^454434|^454435|^454742|^456725|^456726|^456727|^456728|^456729|^45673|^456740|^456741|^456742|^456743|^456744|^456745|^46583|^46584|^46585|^46586|^46587|^484409|^484410|^49096|^49097|^492181|^492182|^498824/)) {
        pass = len==16 ? 1 : 0// DELTA
      } else if (v.match(/^450875|^484406|^484407|^484408|^484411|^484412|^484413|^484414|^484415|^484416|^484417|^484418|^484419|^48442|^48443|^48444|^484450|^484451|^484452|^484453|^484454|^484455|^49173|^49174|^49175|^491880/)) {
        pass = len==16 ? 1 : 0// ELECTRON
      } else if (v.match(/^490302|^490303|^490304|^490305|^490306|^490307|^490308|^490309|^490335|^490336|^490337|^490338|^490339|^491101|^491102|^491174|^491175|^491176|^491177|^491178|^491179|^491180|^491181|^491182|^4936|^564182|^63330|^63331|^63332|^63333|^63334|^6759/)) {
        pass = len==16 || len==18 || len==19 ? 1 : 0// SWITCH
      } else if (v.match(/^4/)) {
        pass = len==13 || len==16 ? 1 : 0// VISA
      } else if (v.match(/^51|^52|^53|^54|^55/)) {
        pass = len==16 ? 1 : 0// "MASTERCARD"
      } else if (v.match(/^50|^56|^57|^58|^6/)) {
        pass = 1// "MAESTRO"
      } else if (v.match(/^60/)) {
        pass = len==16 ? 1 : 0// DISCOVER
      } else if (v.match(/^63345|^63346|^63347|^63348|^63349|^6767/)) {
        pass = len==16 || len==18 || len==19 ? 1 : 0// SOLO
      }
      if (pass == 0) return alert("信用卡號碼錯誤！") + f
    } else {
      return alert("信用卡號碼錯誤！") + f
    }
  }
}
/*
#################################################
##
##  身分證字號驗證
##
##===============================================
##
##  dhPIDCheck(inputId)
##
##  參數說明：
##    inputId：為欄位的 NAME 或 ID 值
##
#################################################
*/
function dhPIDCheck(inputId) {
  var d = document.all[inputId], v = d.value, f = d.select() + "break"
  var c = ("0123456789abcdefghjklmnpqrstuvxywzio").indexOf(v.charAt(0).toLowerCase()).toString()
  if (v == "") {
    return alert("請輸入身分證字號！") + f
  } else if (!v.match(/^[a-zA-Z]\d{9}$/g) || v.match(/^.[^12]|^..[7-9]/g) ||
             (c.charAt(0)*1 + c.charAt(1)*9 + v.charAt(1)*8 + v.charAt(2)*7 + v.charAt(3)*6 + v.charAt(4)*5 +
             v.charAt(5)*4 + v.charAt(6)*3 + v.charAt(7)*2 + v.charAt(8)*1 + v.charAt(9)*1) % 10 > 0) {
    return alert("身分證字號錯誤！") + f
  }
}
/*
#################################################
##
##  統一編號驗證
##
##===============================================
##
##  dhCIDCheck(inputId)
##
##  參數說明：
##    inputId：為欄位的 NAME 或 ID 值
##
#################################################
*/
function dhCIDCheck(inputId) {
  var d = document.all[inputId], v = d.value, f = d.select() + "break"
  var a = "12121241", c = 0
  if (v == "") {
    return alert("請輸入統一編號！") + f
  } else {
    for (i=8 ;i-->0;) {
      t = (a.charAt(i)*v.charAt(i)).toString()
      c += t>9 ? t.charAt(0)*1+t.charAt(1)*1 : t*1
    }
    if (!v.match(/^\d{8}$/g) || (c%10 > 0 && (v.charAt(6)==7 ? c+1 : c)%10 > 0)) return alert("統一編號錯誤！") + f
  }
}
/*
#################################################
#################################################
##
##  傳回特定長度的重複字元字串
##
#################################################
*/