// Name: check.js
// Author: EBJET.COM
// Version: 1.0
// Last Update: 2001-01-08
// Description: Valid check for JavaScript
// Usage: <script type=text/javascript src=/check.js></script>
// Function Listing:
//  function chkdate(yearStr, monthStr, dayStr)
//  function chkdatestr(checkStr)
//  function chkemail(checkStr)
//  function chkfloat(checkStr)
//  function chkinteger(checkStr)
//  function chklength(checkStr)
//  function chkname(checkStr)
//  function chknegative(checkStr)
//  function chknostring(checkStr, forbidStr)
//  function chknumber(checkStr)
//  function chkpasswd(checkStr)
//  function chkphone(checkStr)
//  function chkquot(checkStr)
//  function chksafe(checkStr)
//  function chkspace(checkStr)
//  function chkstring(checkStr, checkOK)
//  function trim(w)
//  function trimform(TheForm)


//ㄧ计chkdate
//ざ残浪琩琌猭ら戳
//把计弧璶浪琩才﹃るら
//  false:ぃ琌  true:琌
function chkdate(yearStr, monthStr, dayStr) {
  var checkOK = "1234567890";
  if ( !chkstring(yearStr, checkOK) ||
       !chkstring(monthStr, checkOK) ||
       !chkstring(dayStr, checkOK) )
    return(false);

  testday = new Date();
  testday.setFullYear(yearStr, monthStr-1, dayStr);
  var tmpy = testday.getFullYear();
  var tmpm = testday.getMonth() + 1;
  var tmpd = testday.getDate();
  if (tmpy == yearStr && tmpm == monthStr && tmpd == dayStr) {
    return(true);
  } else {
    return(false);
  }
}


//ㄧ计chkdateStr
//ざ残浪琩琌猭ら戳
//把计弧璶浪琩才﹃YYYY-MM-DD
//  false:ぃ琌  true:琌
function chkdatestr(checkStr) {
  var tmpy = "";
  var tmpm = "";
  var tmpd = "";
  var checkCode = 0;
  for (i=0; i<checkStr.length ;i++) {
    ch = checkStr.charAt(i);
    if (ch == '-') checkCode++;
    if (checkCode > 2) return(false);
    else if (checkCode == 0 && ch != '-') tmpy += ch;
    else if (checkCode == 1 && ch != '-') tmpm += ch;
    else if (checkCode == 2 && ch != '-') tmpd += ch;
  }
  if (chknumber(tmpy) && tmpy.length == 2) {
    if (tmpy > 70) tmpy = "19" + tmpy;
    else tmpy = "20" + tmpy;
  }
  return(chkdate(tmpy, tmpm, tmpd));
}


//ㄧ 计 chkemail
//ざ残浪琩琌猭Email Address
//把计弧璶浪琩才﹃
//  false:ぃ琌  true:琌
//喷砏玥ぃ.┪@秨繷㎝/┪挡Юぃ1@*@(*.)*
function chkemail(checkStr) {
  var checkOK = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@._-";
  var allValid = false;
  var checkCode = 0;
  for (i = 0; i < checkStr.length; i++) {
    ch = checkStr.charAt(i);
    if (ch == "@") {
      if (checkCode == 0 && i > 0) {
        checkCode = 1;
      } else {
        break;
      }
    }
    if (ch == ".") {
      if (i == 0 || i == checkStr.length - 1) {
        break;
      } else if (checkStr.charAt(i+1) == '.') {
        break;
      } else if (checkCode == 0) {
        if (checkStr.charAt(i+1) == '@') {
          break;
        }
      } else if (checkCode == 1) {
        if (checkStr.charAt(i-1) == '@') {
          break;
        } else {
          checkCode = 2;
        }
      }
    }
    chValid = false;
    for (j = 0; j < checkOK.length; j++) {
      if (ch == checkOK.charAt(j)) {
        chValid = true;
        break;
      }
    }
    if (!chValid) break;
    if (i == checkStr.length - 1 && checkCode == 2) {
      allValid = true;
      break;
    }
  }
  return(allValid);
}


//ㄧ 计 chkfloat
//ざ残浪琩琌计
//把计弧璶浪琩才﹃
//  false:ぃ琌  true:琌
function chkfloat(checkStr) {
  if (chknumber(checkStr) && checkStr.indexOf(".") >= 0) {
    return(true);
  } else {
    return(false);
  }
}


//ㄧ 计 chkinteger
//ざ残浪琩琌计
//把计弧璶浪琩才﹃
//  false:ぃ琌  true:琌
function chkinteger(checkStr) {
  var checkOK = "0123456789+-";
  var allValid = true;
  for (i = 0; i < checkStr.length; i++) {
    ch = checkStr.charAt(i);
    if (checkOK.indexOf(ch) == -1) {
      allValid = false;
      break;
    }
    if ((ch == '+' || ch == '-') && i > 0) {
      allValid = false;
      break;
    }
  }
  return(allValid);
}


//ㄧ 计 chklength
//ざ残浪琩才﹃
//把计弧璶浪琩才﹃
//  竊
function chklength(checkStr) {
  var n = 0;
  for(i=0; i<checkStr.length; i++) {
    chcode = checkStr.charCodeAt(i);
    if (chcode >=0  && chcode <= 255) {
      n++;
    } else {
      n += 2;
    }
  }
  return(n);
}


//ㄧ 计 chkname
//ざ残浪琩琌才嘿璶―
//把计弧璶浪琩才﹃
//  false:ぃ琌  true:琌
function chkname(checkStr) {
  var forbidStr = "0123456789`~!@#$%^&*()_-+=|\\{}[];:,<>?/\"";
  return(!chknostring(checkStr, forbidStr));
}


//ㄧ 计 chknegative
//ざ残浪琩琌璽计
//把计弧璶浪琩才﹃
//  false:ぃ琌  true:琌
function chknegative(checkStr) {
  if (chknumber(checkStr) && checkStr.charAt(0) == '-') {
    return(true);
  } else {
    return(false);
  }
}


//ㄧ 计 chknostring
//ざ残浪琩琌獶猭才
//把计弧璶浪琩才﹃猭才﹃栋
//  false:ぃ琌  true:琌
function chknostring(checkStr, forbidStr) {
  var allValid = false;
  if (typeof(checkStr) != "string" || typeof(forbidStr) != "string") return(false);
  for (i = 0; i < checkStr.length; i++) {
    ch = checkStr.charAt(i);
    if (forbidStr.indexOf(ch) >= 0) {
      allValid = true;
      break;
    }
  }
  return(allValid);
}


//ㄧ 计 chknumber
//ざ残浪琩琌计
//把计弧璶浪琩才﹃
//  false:ぃ琌  true:琌
function chknumber(checkStr) {
  var checkOK = "0123456789.+-";
  var allValid = true;
  var checkCode = 0;
  for (i = 0; i < checkStr.length; i++) {
    ch = checkStr.charAt(i);
    if (checkOK.indexOf(ch) == -1) {
      allValid = false;
      break;
    }
    if ((ch == '+' || ch == '-') && i > 0) {
      allValid = false;
      break;
    }
    if (ch == '.') {
      checkCode += 1;
      if (checkCode > 1) {
        allValid = false;
        break;
      }
    }
  }
  return(allValid);
}


//ㄧ 计 chkpasswd
//ざ残浪琩琌才璶―
//把计弧璶浪琩才﹃
//  false:ぃ琌  true:琌
function chkpasswd(checkStr) {
  var checkOK ="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-.";
  return(chkstring(checkStr, checkOK));
}


//ㄧ 计 checkphone
//ざ残浪琩琌筿杠腹絏
//把计弧璶浪琩才﹃
//  false:ぃ琌  true:琌
function chkphone(checkStr) {
  var checkOK = "0123456789-()# ,;:";
  return(chkstring(checkStr, checkOK));
}


//ㄧ 计 chkquot
//ざ残浪琩琌Τま腹虫ま腹㎝/┪蛮ま腹
//把计弧璶浪琩才﹃
//  false:ぃ琌  true:琌
function chkquot(checkStr) {
  var allValid = false;
  for (i = 0; i < checkStr.length; i++) {
    ch = checkStr.charAt(i);
    if (ch == "'" || ch == '"') {
      allValid = true;
      break;
    }
  }
  return(allValid);
}


//ㄧ 计 chksafe
//ざ残浪琩琌Τ&;`'\"|*?~<>^()[]{}$\n\r
//把计弧璶浪琩才﹃
//  false:ぃ琌  true:琌
function chksafe(checkStr) {
  var allValid = true;
  var forbidStr = new Array ("&", ";", "`", "'", "\"", "|", "*", "?", "~", "<", ">", "^", "(", ")", "[", "]", "{", "}", "$", "\n", "\r");
  m = forbidStr.length;
  n = checkStr.length;
  for (i=0; i<m; i++) {
    for (j=0; j<n; j++) {
      ch1 = checkStr.charAt(j);
      ch2 = forbidStr[i];
      if (ch1 == ch2) {
        allValid = false;
        break;
      }
    }
  }
  return(allValid);
}


//ㄧ 计 chkspace
//ざ残浪琩琌Τ
//把计弧璶浪琩才﹃
//  false:ぃ琌  true:琌
function chkspace(checkStr) {
  var allValid = false;
  for (i = 0; i < checkStr.length; i++) {
    ch = checkStr.charAt(i);
    if (ch == " " || ch == "") {
      allValid = true;
      break;
    }
  }
  return(allValid);
}


//ㄧ 计 chkstring
//ざ残浪琩琌场猭
//把计弧璶浪琩才﹃猭才﹃栋
//  false:ぃ琌  true:琌
function chkstring(checkStr, checkOK) {
  var allValid = true;
  if (typeof(checkStr) != "string" || typeof(checkOK) != "string") return(false);
  for (i = 0; i < checkStr.length; i++) {
    ch = checkStr.charAt(i);
    if (checkOK.indexOf(ch) == -1) {
      allValid = false;
      break;
    }
  }
  return(allValid);
}


//ㄧ 计 trim
//ざ残埃ㄢ狠才腹㎝〓┪à
//把计弧璶矪瞶才﹃
//  矪瞶才﹃
function trim(w) {
  while (w.length>0 && (w.substr(0,1)==' ' || w.substr(0,1)=='')) w=w.substr(1);
  while (w.length>0 && (w.substr(w.length-1)==' ' || w.substr(w.length-1)=='')) w=w.substr(0,w.length-1);
  return(w);
}


//ㄧ 计 trimform
//ざ残癸虫ず┮Τtext摸暗trim巨
//把计弧璶矪瞶虫
//  false:ア毖  true:Θ
function trimform(TheForm) {
  if (typeof(TheForm) != "object") return(false);
  for (var i=0; i<TheForm.elements.length; i++) {
    var e = TheForm.elements[i];
    if (e.type == 'text') {
      e.value = trim(e.value);
    }
  }
  return(true);
}
