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;
    }
    if(keyPressed == 45 && srcElement.value != ''){
      return false;
    }
    c = [45,46];
    d = [b,c];
  } else if (inputType.match(/pricesigned/i)) { // MONEY or decimals
    if (srcElement.value.match(/\./)) {
      if (keyPressed == 46) return false;
    }
    c = [45,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;
}


/**
* InputFilter.js: unobtrusive filtering of keystrokes for <input> tags
*
* This module finds all <input type="text"> elements in the document that
* have a nonstandard attribute named "allowed". It registers an onkeypress
* event handler for any such element to restrict the user's input so that
* only characters that appear in the value of the allowed attribute may be
* entered. If the <input> element also has an attribute named "messageid",
* the value of that attribute is taken to be the id of another document
* element. If the user types a character that is not allowed, the messageid
* element is made visible. If the user types a character that is allowed,
* the messageid element is hidden. This message id element is intended to
* offer an explanation to the user of why her keystroke was rejected. It
* should typically be styled with CSS so that it is initially invisible.
*
* Here is some sample HTML that uses this module.
* Zipcode:
* <input id="zip" type="text" allowed="0123456789" messageid="zipwarn">
* <span id="zipwarn" style="color:red;visibility:hidden">Digits only</span>
*
* In browsers such as IE, which do not support addEventListener( ), the
* keypress handler registered by this module overwrites any keypress handler
* defined in HTML.
*
* This module is purely unobtrusive: it does not define any symbols in
* the global namespace.
*/

(function( ) { // The entire module is within an anonymous function
  // When the document finishes loading, call the init( ) function below
  //addEvent( window, "load", init );
  if( window.addEventListener ){
    window.addEventListener( 'load', init, false );
  } else if( window.attachEvent ){
    window.attachEvent( 'onload', init );
  }


  // Find all the <input> tags we need to register an event handler on
  function init( ) {
    var inputtags = [], tmp = [];

    for(var j = 0; j < 2; j++){
      if(j == 0) tmp = document.getElementsByTagName("input");
      if(j == 1) tmp = document.getElementsByName('textarea');
      for(var i = 0; i < tmp.length; i++){
        inputtags.push(tmp[i]);
      }
    }

    var specialGroups = {
      ':letters;' : 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
      ':upperCase;': 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
      ':lowercase;': 'abcdefghijklmnopqrstuvwxyz',
      ':numbers;': '0123456789',
      ':email;': 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789._-@',
      ':phone;': '0123456789-() '
    }

    var spe_pat = /:(?=[^;]+;)/;

    for(var i = 0 ; i < inputtags.length; i++) { // Loop through all tags
      var tag = inputtags[i];
      if(tag.type != "text") continue; // We only want text fields
      if(tag.onkeypress != null) continue;
      var allowed = tag.getAttribute("allowed");
      if (!allowed) continue; // And only if they have an allowed attr
      alert("HERE");
      if(spe_pat.test(allowed)){
        for(var group in specialGroups){ allowed = allowed.replace(RegExp(group, 'ig'), specialGroups[group]); }
        tag.setAttribute('allowed', allowed);
      }

      // Register our event handler function on this input tag
      if (tag.addEventListener){
        tag.addEventListener("keypress", filter, false);
      } else {
        tag.onkeypress = filter;// We don't use attachEvent because it does not invoke the handler function with the correct value of the this keyword.
      }
    }
  }

  // This is the keypress handler that filters the user's input
  function filter(event) {
    // Get the event object and character code in a portable way
    var e = event || window.event; // Key event object
    var code = e.charCode || e.keyCode; // What key was pressed

    // If this keystroke is a function key of any kind, do not filter it
    if (e.charCode == 0) return true; // Function key (Firefox only)
    if (e.ctrlKey || e.altKey) return true; // Ctrl or Alt held down
    if(code == 13 && this.getAttribute('enterKeyFunction')){
      if(window[this.getAttribute('enterKeyFunction')] && typeof(window[this.getAttribute('enterKeyFunction')]) == 'function'){
        (this.getAttribute('enterKeyFunction'))();
      }
    }

    if (code < 32) return true; // ASCII control character
    // Now look up information we need from this input element
    var allowed = this.getAttribute("allowed"); // Legal chars
    var messageElement = null; // Message to hide/show
    var messageid = this.getAttribute("messageid"); // Message id, if any
    if (messageid) // If there is a message id, get the element
    messageElement = document.getElementById(messageid);
    var c = String.fromCharCode(code); // Convert the character code to a character
    if (allowed.indexOf(c) != -1) {// See if the character is in the set of allowed characters
      if (messageElement) messageElement.style.visibility = "hidden";// If c is a legal character, hide the message, if any
      return true; // And accept the character
    }
    else {
      if (messageElement) messageElement.style.visibility = "visible";// If c is not in the set of allowed characters, display message
      if (e.preventDefault) e.preventDefault( );// And reject this keypress event
      if (e.returnValue) e.returnValue = false;
      return false;
    }
  }
})( );
