// the calling page must contain a form called login
// it must have the following hidden fields
//   buffer   = buffer to store keypress
//   barcode  = stores the barcode scanned
// these fields are optional
//   username = default focus puts here
//   password = user can key in the password here
// these fields are only required when debugging
//   aski     = ascii code of keypress
//   shft     = shift state (whether shift was pressed)
//   fiel     = the field having the focus
//   dbug     = whether debug is on or not
//   tmng     = timing, how fast was the keystroke (ms)
//
// the user can scan more than one barcode, the last one will be used
// when the barcode is scanned, the focus can be anywhere on the page
// if the focus is on a text field, the barcode will be removed
//
// if debug fields not present on form, must set dbug=false, or script won't work



window.onload = onloadH;

// to differentiate between different browsers
  var emod;
// to store time values
  var tm1 =   0; // time previous char was received
  var tm2 =   0; // current time
  var tm3 = 100; // maximum time between keystrokes, milliseconds
                 // if slower than this, it didn't come from a barcode scanner
                 // 100ms/char is about 120 words per min, a very fast typist
                 // the rest of us can reach this for only a few char
// debug yes/no
  // var dbug = true;
  var dbug = false;



function onloadH(e) {

  // to differentiate between different browsers
    emod = (e) ? (e.eventPhase) ? "W3C" : "NN4" : (window.event) ? "IE4+" : "unknown";
    if (emod == "NN4") {
      document.captureEvents(Event.KEYDOWN);
      }

  // process keypresses
    document.onkeypress = checkCode;

  return true;

}



function checkCode(e) {
  // do necessary pre-processing for different browsers (compatibility)
  // then call checkBar() to process further

  switch (emod) {

    case "IE4+":
      e = window.event;
      // to do later
      k = e.keyCode;
      sh = event.shiftKey;
      t = e.srcElement;
      break;
    case "NN4":
      // to do later
      k = e.which;
      sh = e.modifier >= 4;
      t = e.target; // don't know if this is the right DOM object for NN4
      break;
    case "W3C":
      k = e.which;
      sh = e.shiftKey;
      t = e.target;
      break;

  }

  if ( checkBar( k, sh, t ) ) {
    k = false;  // checkBar returns true when the terminator character is received
                // block this character from appearing on the form
    // if you want the form to auto-submit when a barcode is scanned, do it here
    document.login.submit();
  }

  return k;

}



function checkBar( k, sh, t ) {
  // keypress received, save it to the buffer
  // if timing between keystrokes is too long, char was typed, not scanned, clear buffer
  // on terminator character received, copy buffer to barcode field
  //
  // returns true when the terminator character is received
  // returns false at other times

  // temporary Date object, allows you to acccess getTime()
    var tm = new Date();

  // check the time now
    tm2 = tm.getTime();

  if ( dbug ) {
    document.login.aski.value = k;
    document.login.shft.value = sh;
    document.login.fiel.value = t.name;
    document.login.tmng.value = ( tm2 - tm1 ) + "";
  }

  // this variable is set to true, when the end marker is received
  // at all other times, it is false
  // this tells the calling function to block the last character from appearing on the form
    bar = false;

  // if you scan with a barcode scanner, it should be fast
  // check if the time difference is within limits
    if ( ( tm1 == 0 ) || ( ( tm2 - tm1 ) <= tm3 ) ) {
      // timing is within limits, check if key received is the terminator char
        if ( k == 13 ) {
          // this is the terminator charactor, copy buffer to barcode field
            document.login.barcode.value = document.login.buffer.value;
          // clear buffer
            document.login.buffer.value = "";
          // find which field had the focus, remove the barcode from the end of that field
          // this needs more work, you need to check that the field is a text/textarea
          // also need to check if input hit maxlen of field, so don't remove more than entered
            document.login.elements[t.name].value = document.login.elements[t.name].value.substr( 0, document.login.elements[t.name].value.length - document.login.barcode.value.length );
          // this says the barcode was successfully received
            bar = true;
        }
    } else {
      // not first char, and outside limits; so clear buffer
        document.login.buffer.value = "";
    }
  // if this is a normal character, pust it into the buffer
    if ( ( k >= 32 ) && ( k <=127 ) ) {
      document.login.buffer.value = document.login.buffer.value + String.fromCharCode( k );
    }
  // old time is new time (prep for next iteration)
    tm1 = tm2;

  return bar;
}



// end
