function IsNumeric(strToCheck)
  {

  var i;
  var VaildWords         = '0123456789'
  var intStrLen          = 0;
  var blnNonNumericFound = false;

  intStrLen = strToCheck.length;

  for ( i=0 ; i < intStrLen ; i++ )
  {
    if ( VaildWords.indexOf( strToCheck.charAt(i) ) == -1 )
    {  
      blnNonNumericFound = true;
      break;
    }
  }

  return( !blnNonNumericFound );
  }




function IsValidDate(DateObject)
  {

  var strYear;  	
  var strMonth;
  var strDay;
  	
  var aryElements;
  var strDateString;
  var blnValid = false;


  strDateString = DateObject.value;
   
  if (strDateString != '')
    {
    
    aryElements = strDateString.split('/');

    if (aryElements.length == 3)
      {
      	
      // 檢查年份
      strYear = aryElements[0];

      if ( (strYear.length == 4) && IsNumeric(strYear) )
        {

        // 檢查月份
        strMonth = aryElements[1];
        if ( (strMonth.length < 2) && IsNumeric(strMonth) )
          strMonth = '0' + strMonth;
          
        if ( (strMonth.length == 2) && ( (strMonth >= '01') && (strMonth <= '12') ) )
          {
          	
          // 檢查日期
          strDay = aryElements[2];
          if ( (strDay.length < 2) && IsNumeric(strMonth) )
            strDay = '0' + strDay;
            
          if ( (strDay.length == 2) && ( (strDay >= '01') && (strDay <= '31') ) )
            {
            blnValid = true;
            DateObject.value = strYear + '/' + strMonth + '/' + strDay;
            }

          }

        }

      }
    
    }
   else
     blnValid = true
       
   return(blnValid);
    
  }



function IsValidBan(strToCheck)
  {
  var i;
  var basic = "12121241";
  var intValidLen = 8;
  var blnValidBan;
  var data = 0;
  var xx   = 0;
  var x    = 0;
  var y    = 0;

  if ( strToCheck.length == 0 )
    blnValidBan = true
  else if ( ( strToCheck.length == intValidLen ) && IsNumeric(strToCheck) )
    {
    for ( i=0 ; i < strToCheck.length ; i++ ) {
    if ((i==6)&&(strToCheck.charAt(6)==7))
      continue;
    else
      {
      xx = strToCheck.charAt(i) * basic.charAt(i);
      x  = xx % 10;
      data = data + (xx-x) / 10 + x; }
      }
    x = data % 10;
    y = ( data + 1 ) % 10;

    if (((x==0)||(y==0))&&(data!=0)&&(strToCheck.charAt(6)==7))
      blnValidBan = true;
    else if ((x==0)&&(data!=0)&&(strToCheck.charAt(6)!=7))
      blnValidBan = true;
    else
      blnValidBan = false;
    }
  else
    blnValidBan = false;

  return( blnValidBan );
  }




function IsValidSID(s)
  {
  var c, n, i;
  var t= "ABCDEFGHJKLMNPQRSTUVXYWZIO";
  
  c= s.substring(0,1);
  c= t.indexOf(c.toUpperCase());
  if((s.length!= 10) || (c<0)) return false;
  
  n= parseInt(c/10)+ c%10*9+ 1;
  for(i=1; i<9; i++) n= n+ parseInt(s.substring(i,i+1))* (9-i);
  n= (10- (n% 10))% 10;
  if(n!= parseInt(s.substring(9,10))) return false;
  
  return true;
  }




function IsValidSID_old(strToCheck)
  {
  check:
    {
    if (strToCheck.length != 10)
      break check;

    var no=strToCheck.split("");
    no[0]=no[0].toUpperCase();
    if ((no[0]<"A") || (no[0]>"Z"))
      break check;
    if ((no[1]<"1") || (no[1]>"2"))
      break check;
    for (var i=1;i<10;i++)
      {
      if ((no[i]<"0" || no[i]>"9"))
        break check;
      else
        no[i]=Number(no[i]);
      }

    no[0]=no[0].charCodeAt(0);

    if (no[0]<73)
      no[0]-=55;
    else if (no[0]==73)
      no[0]=34;
    else if (no[0]<79)
      no[0]-=56;
    else if (no[0]==79)
      no[0]=35;
    else if (no[0]<87)
      no[0]-=57;
    else if (no[0]==87)
      no[0]=32;
    else if (no[0<90])
      no[0]-=58;
    else no[0]=33;

    var sum=Math.floor((no[0]/10))+(no[0]%10)*9;
    for (var i=1;i<9;i++)
      sum+=no[i]*(9-i);

    sum+=no[9];

    if (sum%10 != 0)
      break check;

    return(true);
    }
  return(false);
  }




function IsValidEmail(strToCheck)
  {
  if ( strToCheck.length == 0 )
    return(true);

  re = /(.+)\@+([\,\w\.-]+)/i;
  found = strToCheck.match(re);
  if (! found)
    return(false);
  else
    {
    dot_array=found[2].split(/[\,\.]+/);
    if ( dot_array.length < 2)
      return(false);
    else
      {
      found[1]=found[1].replace( /^\s+/, '');
      found[1]=found[1].replace( /\s+$/, '');
      if(found[1].match(/\s+/))
        return(false);
      }
    }
  return(true);   
  }




function LengthB(strText)
  {
  var intTextLenB = 0;
  var intTextLen = strText.length;

  for (var i=0 ; i<intTextLen ; i++)
    {
    intCharCode = strText.charCodeAt(i);

    if ( ( intCharCode > 0 ) && ( intCharCode < 128 ) )
      intTextLenB += 1;
    else
      intTextLenB += 2;        
    }

  return(intTextLenB);
  }
