var msie = (navigator.userAgent.match(/msie/i)) ? 1 : 0;
var firefox = (navigator.userAgent.match(/firefox/i)) ? 1 : 0;
var netscape = (navigator.userAgent.match(/netscape/i)) ? 1 : 0;

// sample: onkeypress="return chkKey('address',event)"

function obj() { return document.getElementById(arguments[0]); };

function chkKey( inputType, ev, formObj ) {
  ev = ev || event;
  var srcElement = ev.srcElement || ev.target;
  var thisValue = srcElement.value || "";

  // 8[backspace]
  // [a -z] 97 - 122; [A - Z] 65 - 90; [0 - 9] 48 - 57
  // 33[!], 34["], 35[\#], 36[\$], 37[%], 38[&], 39['], 40[(], 41[)], 42[*], 43[+], 44[,], 45[-], 46[.], 47[/], 58[:], 59[;], 60[<], 61[=], 62[>], 63[?], 64[\@], 95[_]


  //letters LETTERS numbers backspace
  var a = [8,32,46,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,97,98,99,100,101,102,103,104,105,106,107,108,109,100,110,111,112,113,114,115,116,117,118,119,120,121,122];
  //numbers backspace
  var b = [8,46,48,49,50,51,52,53,54,55,56,57];
  //special charecters as denoted above
  var c = [8,33,35,36,37,38,39,40,41,42,43,44,45,46,47,58,59,60,61,62,63,64,95];

  var d = [];
  var allOk = false;
  if (msie) {
    var keyPressed = window.event.keyCode.toString();
  }
  if (!msie) {
    var keyPressed = ev.which.toString();
  }

  if (inputType.toString() == 2) { // ...
    c = [38,44,45,46,47,58,59,95];
    d = [a,b,c];
  } else if (inputType == 5) {
    if (keyPressed == 32) {
      return false;
    }
    d = [a,b];
  } else if (inputType.toString().match(/\b3\b|\bzip\b/i)) { // ZIP
    if (!thisValue.match(/-/) && thisValue.length > 4) {
      srcElement.value = thisValue+"-";
    }
    if (thisValue.match(/-/)) {
      if (thisValue.length > 9) {
        return false;
      }
    }
    d = [b];
  } else if (inputType.toString().match(/\b4\b|\baddress\b/i)) { // ADDRESS
    c = [35,38,44,45,46,47,58,59];
    d = [a,b,c];
  } else if (inputType.toString().match(/\b1\b|\b8\b|\bcleanL\b/i)) { // clean letters
    d = [a];
  } else if (inputType.toString().match(/\b7\b|cleanLN/i)) { // clean letters and numbers
    d = [a,b];
  } else if (inputType.toString().match(/cleanN/i)) { // clean numbers
    c = [48,49,50,51,52,53,54,55,56,57];
    d = [c];
  } else if (inputType.toString().match(/\b6\b|productname/i)) { // EX: PRODUCT NAME
    c = [45,47,95];
    d = [a,b,c];
  } else if (inputType.toString().match(/\b9\b|\bdate\b/i)) { // DATE
    if (thisValue.length == 2) srcElement.value = thisValue + "-";
    if (thisValue.match(/-/) && (thisValue.length == 5)) srcElement.value = thisValue + "-";
    //if (thisValue.match(/-/) && (thisValue.length > 11)) return false;

    c = [45,47];
    d = [b,c];
  } else if (inputType.match(/address2/i)) { // ADDRESS (no possible splitter)
    c = [35,38,44,45,46,47,59];
    d = [a,b,c];
  } else if (inputType.match(/Image/i)) { // IMAGE NAME
    if (srcElement.value.match(/\./)) {
      if (keyPressed == 46) return false;
    }
    c = [46,95];
    d = [a,b,c];
  } else if (inputType.match(/state/i)) { // STATE
    if (srcElement.value.length > 1) return false;
    d = [a];
  } else if (inputType.match(/price|long/i)) { // MONEY or decimals
    if (srcElement.value.match(/\./)) {
      if (keyPressed == 46) return false;
    }
    c = [46];
    d = [b,c];
  } else if (inputType.match(/email/i)) { // EMAIL
    c = [45,46,64,95];
    if (keyPressed == 32) return true;
    if (thisValue.match(/\@/) && (keyPressed == 64)) return false;
    d = [a,b,c];
  } else if (inputType.match(/phone/i)) { // PHONE
    if (thisValue.length == 3) srcElement.value = thisValue + "-";
    if (thisValue.match(/-/) && (thisValue.length == 7)) srcElement.value = thisValue + "-";
    if (thisValue.match(/-/) && (thisValue.length > 11)) return false;
    d = [b];
  } else if (inputType.toString().match(/intiger/i)) {
    d = [b];
  } else if (inputType.toString().match(/4ss|deptcode/i)) {
    d = [b];
    if (srcElement.value.length > 3) return false;
  } else if (inputType.toString().match(/trainingno/i)) {
    d = [b];
    if (srcElement.value.length > 4) return false;
  } else if (inputType.match(/textareasafe/i)) {
    c = [13,33,35,37,38,40,41,42,43,44,45,46,47,58,59,60,61,62,63,64,95];
    d = [a,b,c];
  }  else if (inputType.match(/funitureTextarea/i)) {
    c = [44,46];
    d = [a,b,c];
  } else if (inputType.match(/punchlist_textareasafe/i)) {
    c = [13,33,40,41,42,43,44,45,46,59,60,61,62,63,95];
    d = [a,b,c];
  } else if (inputType.match(/safe/i)) {
    c = [33,35,37,38,40,41,42,43,44,45,46,47,58,59,60,61,62,63,64,95];
    d = [a,b,c];
  } else if (inputType.match(/fabric/i)) { //fabric codes (letters|numbers|\-)
    c = [45,189];
    d = [a,b,c];
  }

  for (var i = 0; i < d.length; i++) {
    for (var j = 0; j < d[i].length; j++) {
      if (keyPressed.match(RegExp('\\b' + d[i][j] + '\\b'))) {
        return true;
      }
    }
  }
  return allOk;
}

