function PHPeventCharCode(e) {
   if (e.charCode) {
     return e.charCode;
   } else {
     var key = e.keyCode;
     var ch;
     if ((key>=96)&&(key<=105)) {
       ch = key - 48;
     } else {
       var t = String.fromCharCode(key);
       if (key == 222) t="'";
       if (key == 186) t=";";
       if (key == 188) t=",";
       if (key == 190) t=".";
       if (key == 191) t="/";
       if (e.shiftKey) {
         if (t=='`') t='~';
         if (t=='1') t='!';
         if (t=='2') t='@';
         if (t=='3') t='#';
         if (t=='4') t='$';
         if (t=='5') t='%';
         if (t=='6') t='^';
         if (t=='7') t='&';
         if (t=='8') t='*';
         if (t=='9') t='(';
         if (t=='0') t=')';
         if (t=='-') t='_';
         if (t=='=') t='+';
         if (t=="'") t='"';
         if (t=='.') t='>';
         if (t==',') t='<';
       }
       ch = t.charCodeAt(0);
     }
     return ch;
   }
}
function PHPcancelEvent (e) {
  if (document.all) {
    e.cancelBubble = true;
    e.returnValue = false;
  } else {
    e.preventDefault();
    e.stopPropagation();
  }
}
function PHPisGoodArrow(key, ch) {
  return (key==9) || (key==8) || (key==46) ||
    (key==37) || (key==39);
}
function PHPisGoodAlpha(key, ch) {
  return PHPisGoodArrow(key, ch) || (key==32)
    || ((ch>=65)&&(ch<=90)) || ((ch>=97)&&(ch<=122))
    || (ch==46) || (ch==39)
    || (ch>1000);
}
function PHPisGoodNumber(key, ch) {
  return PHPisGoodArrow(key, ch)
    || ((ch>=48)&&(ch<=57));
}
function PHPisGoodHexNumber(key, ch) {
  return PHPisGoodArrow(key, ch)
    || ((ch>=48)&&(ch<=57)) ||
    ((ch >= 97) && (ch <= 102)) ||
    ((ch >= 65) && (ch <= 70));
}
function PHPisGoodExtra(key, ch) {
  return ch == 32;
}
function PHPisGoodAlphaNumber(key, ch) {
  return PHPisGoodAlpha(key, ch) ||
    PHPisGoodNumber(key, ch) ||
    PHPisGoodExtra(key, ch);
}
function PHPallowAlphas(e, dn)
{
   if (dn == (!document.all)) return;
   if (e.ctrlKey) return;
   var key = e.keyCode;
   var ch = PHPeventCharCode(e);
   if (!PHPisGoodAlpha(key, ch))
     PHPcancelEvent(e);
}
function PHPallowNumbers(e, dn)
{
   if (dn == (!document.all)) return;
   if (e.ctrlKey) return;
   var key = e.keyCode;
   var ch = PHPeventCharCode(e);
   if (!PHPisGoodNumber(key, ch))
     PHPcancelEvent(e);
}
function PHPallowHexNumbers(e, dn)
{
   if (dn == (!document.all)) return;
   if (e.ctrlKey) return;
   var key = e.keyCode;
   var ch = PHPeventCharCode(e);
   if (!PHPisGoodHexNumber(key, ch))
     PHPcancelEvent(e);
}
function PHPallowAlphaNumbers(e, dn)
{
   if (dn == (!document.all)) return;
   if (e.ctrlKey) return;
   var key = e.keyCode;
   var ch = PHPeventCharCode(e);
   if (!PHPisGoodAlphaNumber(key, ch))
     PHPcancelEvent(e);
}
<?php
  }
  global $validatorRe;
  global $validatorNext;
  global $validatorFocus;
  global $validatorRequired;
  if ($validatorRe or $validatorNext or
    $validatorFocus or (!empty($validatorRequired)))
  {
?>
function PHPtrim(s) {
  var re = /^ +| +$/g;
  return s.replace(re, '');
}
<?php }
if ($validatorNext) {
?>
function PHPvalidateJump(obj, e, pat, ctrl) {
  var regEx = RegExp(pat);
  var val = PHPtrim(obj.value);
  if ((e.keyCode != 8) && (e.keyCode != 9) && (e.keyCode != 16)
     && (e.keyCode != 37) && (e.keyCode != 39) && (e.keyCode != 46)
     && val.match(regEx))
  {
    document.getElementById(ctrl).focus();
  }
}
<?php }
if ($validatorFocus) {
?>
function PHPvalidateFocus(obj, pat) {
  var regEx = RegExp(pat);
  var val = PHPtrim(obj.value);
  if (!val.match(regEx))
    obj.value = '';
}
<?php }
if (!empty($validatorRequired)) {
?>
function PHPverifyExtra(testRes, fld, val, pat, fldName, mes) {
  var obj = document.getElementById(fld);
  var regEx = RegExp(pat);
  if (val.match(regEx)) {
    var mesEx = RegExp('("")');
    var addMes = mes.replace(mesEx, '"'+fldName+'"');
    if (testRes.mes != '')
      testRes.mes += '\n';
    testRes.mes += addMes;
    if (!(testRes.toFocus))
      testRes.toFocus = obj;
  }
}
function PHPverifyRequired(testRes, fld, fldName, opt, validFmt,
  valuePat, valueRepl, minLen, maxLen, minVal, maxVal)
{
  var obj = document.getElementById(fld);
  var rawVal = PHPtrim(obj.value);
  var val;
  if (valuePat != '') {
    var regEx = RegExp(valuePat);
    val = rawVal.replace(regEx, valueRepl);
  } else {
    val = rawVal;
  }
  if (val != '')
    testRes.hasData = true;
  var addMes = '';
  if (opt && (val == '')) return;
  if (val == '') {
    addMes = fldName+' is required';
  }
  if ((addMes == '') && (validFmt != '')) {
    var regEx = RegExp(validFmt);
    if (!rawVal.match(regEx))
      addMes = 'Formatting of '+fldName+' is invalid';
  }
  if ((addMes == '') && (minLen > 0)) {
    if (val.length < minLen) {
      if (minLen == maxLen) {
        addMes = fldName+' should be exactly '+minLen+' symbols long';
      } else {
        addMes = fldName+' should be at least '+minLen+' symbols long';
      }
    }
  }
  if ((addMes == '') && (maxLen > 0)) {
    if (val.length > maxLen) {
      if (minLen == maxLen) {
        addMes = fldName+' should be exactly '+maxLen+' symbols long';
      } else {
        addMes = fldName+' should be '+maxLen+' symbols long or shorter';
      }
    }
  }
  if ((addMes == '') && (minVal != -1)) {
    if (val < minVal) {
      if (maxVal != -1) {
        addMes = fldName+' should be between '+minVal+' and '+maxVal;
      } else {
        addMes = fldName+' should be at least '+minVal;
      }
    }
  }
  if ((addMes == '') && (maxVal != -1)) {
    if (val > maxVal) {
      if (minVal != -1) {
        addMes = fldName+' should be between '+minVal+' and '+maxVal;
      } else {
        addMes = fldName+' should be less then '+maxVal;
      }
    }
  }
  if (addMes != '') {
    if (testRes.mes != '')
      testRes.mes += '\n';
    testRes.mes += addMes;
    if (!(testRes.toFocus))
      testRes.toFocus = obj;
    return '';
  }
  return val;
}
function PHPverifySelection (testRes, fldName, sels) {
  var good = false;
  var i;
  var f = 0;
  for (i = 0; i<sels.length; i++) {
    if (!f) f = document.getElementById(sels[i]);
    if (document.getElementById(sels[i]).checked)
      good = true;
  }
  if (good)
    testRes.hasData = true;
  if ((!good) && f) {
    if (testRes.mes != '')
      testRes.mes += '\n';
    testRes.mes += 'Please specify if '+fldName;
    if (!(testRes.toFocus))
      testRes.toFocus = f;
  }
}
function PHPverifyList (testRes, fld, fldName, bad) {
  var f = document.getElementById(fld);
  if ((f.selectedIndex < 0) || (f.options[f.selectedIndex].value == bad)) {
    if (testRes.mes != '')
      testRes.mes += '\n';
    testRes.mes += 'Please select '+fldName;
    if (!(testRes.toFocus))
      testRes.toFocus = f;
  } else
    testRes.hasData = true;
}
<?php }
if ($validatorRe) {
?>
function PHPValidateValueRe(obj, re) {
  var i;
  var val = PHPtrim(obj.value);
  var valNew = val;
  var regEx;
  for (i = 0; i < re.length; i += 2) {
    regEx = RegExp(re[i]);
    valNew = valNew.replace(regEx, re[i+1]);
  }
  if (valNew != val)
    obj.value = valNew;
}
<?php
  }
  if (!empty($validatorRequired))
  {
?>
function PHPvalidateForm(e, emptyOK) {
  var testRes = new Object();
  testRes.mes = '';
  testRes.toFocus = 0;
  testRes.hasData = false;
<?php
if (!empty($validatorRequired)) {
  echo "  var doTest;\n";
  foreach ($validatorRequired as $r) {
    if (!empty($r->conditions)) {
      echo "  doTest = true;\n";
      $cnt = 0;
      foreach ($r->conditions as $cf => $ct) {
        $cnt++;
        echo "  var c$cnt = document.getElementById('$cf');\n";
        if ($ct != '')
          echo "  if (!($ct)) doTest = false;\n";
      }
      echo "  if (doTest) ";
    }
    if ($r->tp == 'Req') {
      if (!empty($r->extras))
        echo "var theVal=";
      echo "PHPverifyRequired(testRes, '$r->fld', '$r->name', ";
      echo   "$r->optional, '$r->validFormat', '$r->valuePat', '$r->valueRepl', ";
      echo   "$r->minLength, $r->maxLength, ";
      echo   "$r->minValue, $r->maxValue);\n";
      if (!empty($r->extras)) {
        echo "if (theVal) {\n";
        foreach ($r->extras as $extPat => $extMes)
          echo "  PHPverifyExtra(testRes, '$r->fld', theVal, '$extPat', '$r->name', '$extMes');\n";
        echo "}\n";
      }
    }
    if ($r->tp == 'Sel') {
      echo "PHPverifySelection(testRes, '$r->name', Array(";
      $fst = true;
      foreach ($r->selections as $sel) {
        if (!$fst) echo ', ';
        $fst = false;
        echo "'$sel'";
      }
      echo "));";
    }
    if ($r->tp == 'Opt') {
      echo "PHPverifyList(testRes, '$r->fld', '$r->name', '$r->badOption');\n";
    }
  }
}
?>
  if (document.all) {
    e.cancelBubble = true;
    e.returnValue = false;
  } else {
    e.preventDefault();
    e.stopPropagation();
  }
  if (testRes.toFocus && (testRes.hasData || !emptyOK)) {
    window.alert(testRes.mes);
    testRes.toFocus.focus();
  } else {
    document.forms[0].submit();
  }
}

