 /*******************************************************************************
Author : 顏宏育 Roy Yan

Create Date : 2004/2/15

History

Note ---------------------------------------------------------------------------
Name : Roy VaeCheck
Version : 1.0
*******************************************************************************/
var _str_err_log = new Array();
var _err_count = 0;
//------------------------------------------------------------------------------
String.prototype.Blength = function() { 
  var arr = this.match(/[^\x00-\xff]/ig); 
  return arr == null ? this.length : this.length + arr.length; 
}
//------------------------------------------------------------------------------
function _IsEmpty(CheckVar)
{
  if (CheckVar == "")
  {
    return true;
  }
  return false;
}
//------------------------------------------------------------------------------
function _SetParErr(strErr)
{
  this.str_err_log[this.err_count] = "Check "+strErr+" error";
  this.err_count++;
}
//------------------------------------------------------------------------------
function _SetErrLog(strErr)
{
  this.str_err_log[this.err_count] = strErr;
  this.err_count++;
}
//------------------------------------------------------------------------------
function _GetErr()
{
  return this.str_err_log;
}
//------------------------------------------------------------------------------
function _ErrCount()
{
  return this.err_count;
}
//------------------------------------------------------------------------------
function _DataCheck(intVar,FieldName,MinVar,MaxVar,CheckType,AcpNull)
{ // BEGIN function IntC
  
  if (MinVar > MaxVar)
  {
    this.SetParErr("IntCheck");
    return false;
  }
  
  if (AcpNull == false && _IsEmpty(intVar))
  {
    this.SetErrLog("「"+FieldName+"」は必需記入項目です");
    return false;
  }
  
  if(MinVar == 0 && MaxVar == 0)
  {
    return true;
  }
  
  if ((intVar <= MaxVar && intVar >= MinVar)
      || (AcpNull==true && _IsEmpty(intVar)))
  {
    return true;
  }
  else if (intVar > MaxVar || intVar < MinVar)
  {
    if (CheckType=="int")
    {
      this.SetErrLog("「"+FieldName+"」"+MinVar+" ~ "+MaxVar+" 以内でなくてはいけません");
    }
    else if (CheckType=="str")
    {
      if (intVar > MaxVar)
      {
        this.SetErrLog("「"+FieldName+"」文字数は "+MaxVar+" 以内でなくては");
      }
      else
      {
        this.SetErrLog("「"+FieldName+"」文字数は "+MinVar+" 以上でなくては");
      }
      
    }
    
    return false;
  }
  this.SetErrLog("「"+FieldName+"」エラーが発生しました");
  return false;
}
//------------------------------------------------------------------------------
function _GetErrList(strType)
{
  var strTemp = "";
  switch (strType)
  {
    case "string":
      if (this.ErrCount()>0)
      {
        for (i=0;i<this.ErrCount();i++)
        {
          strTemp += "● "+this.str_err_log[i]+"  \r\n";
        }
        return strTemp;
      }
    case "html":
      if (this.ErrCount()>0)
      {
        if (this.ErrCount()>0)
        {
          strTemp = "<ul>\r\n";
          for (i=0;i<this.ErrCount();i++)
          {
            strTemp += "<li>"+this.str_err_log[i]+"</li>\r\n";
          }
          strTemp += "</ul>\r\n";
          return strTemp;
        }
      }
      break;
  }
  this.ReSetCheck();
  //return "strType參數錯誤";
}
//------------------------------------------------------------------------------
//檢查數值
function _IntCheck(intVar,FieldName,MinVar,MaxVar,AcpNull)
{
  return this.DataCheck(intVar,FieldName,MinVar,MaxVar,"int",AcpNull);
}
//------------------------------------------------------------------------------
//檢查字串
function _StrCheck(strVar,FieldName,MinVar,MaxVar,AcpNull)
{
  intVar = strVar.Blength();
  return this.DataCheck(intVar,FieldName,MinVar,MaxVar,"str",AcpNull);
}
//------------------------------------------------------------------------------
//檢查日期 - 數值格式
function _DateCheck(strYear,strMonth,strDay,FieldName,AcpNull)
{
    if (AcpNull == true &&
       (_IsEmpty(strYear) || _IsEmpty(strMonth) || _IsEmpty(strDay)))
    {
      return true;
    }
    
    var dateTemp;
    var strDate = strYear + "/" + strMonth + "/" + strDay;
    var dateTemp = new Date(strDate);
    var y=dateTemp.getFullYear();
    var m=dateTemp.getMonth()+1;
    var d=dateTemp.getDate();
    var strDateTemp = y + "/" + m + "/" + d;
    
    if (strDateTemp != strDate)
    {
      this.SetErrLog("「"+FieldName+"」ご入力された日付に誤りがあります"); 
      return false;     
    }
    return true; 
}
//------------------------------------------------------------------------------
//檢查日期 - 字串格式
function _DateCheckStr(strDate,FieldName,strSplit,AcpNull)
{
    if (AcpNull == true && _IsEmpty(strDate))
    {
      return true;
    }
    
    if (strSplit != "") strDate.replace("/",strSplit);
    
    if (strDate == "")
    {
      this.SetErrLog("「"+FieldName+"」ご入力された日付、またはフォーマットに誤りがありま");  
      return false;
    }
    var dateTemp;
    var dateTemp = new Date(strDate);
    var y=dateTemp.getFullYear();
    var m=dateTemp.getMonth()+1;
    var d=dateTemp.getDate();
    var strDateTemp = y + "/" + m + "/" + d;
    
    if (strDateTemp != strDate)
    {
      this.SetErrLog("「"+FieldName+"」ご入力された日付、またはフォーマットに誤りがありま");  
      return false;     
    }
    return true; 
}
//------------------------------------------------------------------------------
//檢查e-Mail格式
function _emailCheck (emailStr,FieldName,AcpNull)
{
  if (AcpNull == true && _IsEmpty(emailStr))
  {
    return true;
  }
  else if (AcpNull == false && _IsEmpty(emailStr))
  {
    this.SetErrLog("「"+FieldName+"」必需記入項目");  
    return false; 
  }
  
  var regexp=/^[a-zA-Z0-9_]{2,25}(.*)[@][a-zA-z0-9-]{1,100}[.]([a-zA-z0-9-]{2,10}|[a-zA-z0-9-]{2,10}[.][a-zA-z0-9-]{1,10})$/;
  var Result = regexp.test(emailStr);
  
  if (!Result)
  {
    this.SetErrLog("「"+FieldName+"」のエラーが発生しました");  
    return false;     
  }
  return true;
}
//------------------------------------------------------------------------------
//檢查台灣身份證
function _TWIDCheck(strID,AcpNull)
{
  if (AcpNull == true && _IsEmpty(strID))
  {
    return true;
  }
  
  if (AcpNull == false && _IsEmpty(strID))
  {
    this.SetErrLog("「TWID」は必需記入項目です");
    return false;
  }
  
  if(strID.Blength() < 10)
  {
    this.SetErrLog("「TWID」のエラーが発生しました");
    return false;
  }
  else if(strID.substr(1,9)=="000000000")
  {
    this.SetErrLog("「TWID」のエラーが発生しました");
    return false;
  }
  
  AreaCode = "ABCDEFGHJKLMNPQRSTUVXYWZIO";
  AreaNum = new Array(10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35);
  
  strID = strID.toUpperCase();
  
  var strArea ="";
  var iArea=0;
  
  for(i=0;i<AreaCode.length;i++)
  {
    if(AreaCode.charAt(i) == strID.charAt(0))
    {
      iArea = AreaNum[i];
      break;
    }
  }
  
  if(iArea!=0)
  {
    strID = iArea.toString() + strID.substr(1,9).toString();
  }
  
  IDValue = strID.charAt(0) * 1 + strID.charAt(1) * 9 + strID.charAt(2) * 8 +
            strID.charAt(3) * 7 + strID.charAt(4) * 6 + strID.charAt(5) * 5 + 
            strID.charAt(6) * 4 + strID.charAt(7) * 3 + strID.charAt(8) * 2 +
            strID.charAt(9) * 1 + strID.charAt(10) * 1;
  Result = IDValue % 10;
  
  if(Result == 0)
  {
    return true;
  }
  else
  {
    this.SetErrLog("「TWID」のエラーが発生しました");
  }
  return true;
}
//------------------------------------------------------------------------------
//檢查手機號碼
function _MPhoneCheck(str,FieldName,AcpNull)
{
  if (AcpNull == true && _IsEmpty(str))
  {
    return true;
  }
  else if (AcpNull == false && _IsEmpty(str))
  {
    this.SetErrLog("「"+FieldName+"」必需記入項目");  
    return false; 
  }
  
  //0xxx-xxxxxx
  var regexp= /^[0][0-9]{3}-[0-9]{6}$/;
  var Result = regexp.test(str);
  
  if (!Result)
  {
    this.SetErrLog("「"+FieldName+"」のエラーが発生しました");  
    return false;     
  }
  
  return true;
}
//------------------------------------------------------------------------------
//檢查電話號碼
function _PhoneCheck(str,FieldName,AcpNull)
{
  if (AcpNull == true && _IsEmpty(str))
  {
    return true;
  }
  else if (AcpNull == false && _IsEmpty(str))
  {
    this.SetErrLog("「"+FieldName+"」必需記入項目");  
    return false; 
  }
  
  // 0xx-xxxxxx or 0x-xxxxxxxx or 0x-xxxxxxx
  var regexp =  /(^[0][0-9]-[0-9]{8}$)|(^[0][0-9]{2}-[0-9]{6}$)|(^[0][0-9]-[0-9]{7})/;
  Result = regexp.test(str);
  
  if (!Result)
  {
    this.SetErrLog("「"+FieldName+"」のエラーが発生しました");  
    return false;     
  }
  return true;
}
//------------------------------------------------------------------------------
//重新設定物件
function _ReSetCheck()
{
  this.err_count = 0;
  this.str_err_log = null;
  this.str_err_log = new Array();
}
//------------------------------------------------------------------------------
//建構子
function _VarCheck()
{
  this.str_err_log = new Array();
  this.err_count = 0;
}
//------------------------------------------------------------------------------
//定義Class
function VarCheck()
{
  //constructor
  this.VarCheck = _VarCheck;
  //property
  this.err_count = _err_count;
  this.str_err_log = _str_err_log;
  //method
  this.GetErrList = _GetErrList;
  this.DataCheck = _DataCheck;
  this.IntCheck = _IntCheck;
  this.StrCheck = _StrCheck;
  this.DateCheck = _DateCheck;
  this.DateCheckStr = _DateCheckStr;
  this.SetErrLog = _SetErrLog;
  this.GetErr = _GetErr;
  this.ErrCount = _ErrCount;
  this.emailCheck = _emailCheck;
  this.TWIDCheck = _TWIDCheck;
  this.MPhoneCheck = _MPhoneCheck;
  this.PhoneCheck = _PhoneCheck;
  this.ReSetCheck = _ReSetCheck;
}
