
barcodescan_active = false;
barcodescan_timeout_id = null;
barcodescan_input = '';

function barcodescan(code) {
}

function barcodescan_stopEvent(event) {
  if (event.stopPropagation) event.stopPropagation(event);
  if (event.preventDefault) event.preventDefault(event);
  return false;
}

function barcodescan_isInitKey(event) {
  if (
    event.ctrlKey &&
    event.altKey && (
      event.charCode == 32  ||
      event.keyCode  == 32  ||
      event.charCode == 160 ||
      event.keyCode  == 160 ||
      event.charCode == 48  ||
      event.keyCode  == 48
    )
  ) return true;
  else return false;
}

function barcodescan_isTermKey(event) {
  if (
    event.keyCode  == 32 ||
    event.charCode == 32 ||
    event.keyCode  == 13 ||
    event.charCode == 13 ||
    event.keyCode  == 10 ||
    event.charCode == 10
  ) return true;
  else return false;
}

function barcodescan_timeout() {
  barcodescan_active = false;
}

function barcodescan_init() {
    barcodescan_input = '';
    barcodescan_active = true;
    //barcodescan_timeout_id = window.setTimeout('barcodescan_timeout()', 1000);
}

function barcodescan_term() {
  window.clearTimeout(barcodescan_timeout_id);
  barcodescan_timeout_id = null;
  barcodescan_active = false;
}

function barcodescan_onkeydown(event) {
  if (!event) event = window.event;
  if (barcodescan_isInitKey(event)) {
    barcodescan_init();
    return barcodescan_stopEvent(event);
  }
  return true;
}

function barcodescan_onkeypress(event) {
  if (!event) event = window.event;
  if (barcodescan_isInitKey(event)) {
    barcodescan_init();
    return barcodescan_stopEvent(event);
  }
  if (barcodescan_active) {
    if (barcodescan_isTermKey(event)) {
      barcodescan_term();
      barcodescan(barcodescan_input);
    } else if (event.charCode == 96 || event.keyCode == 96) {
      // Workaround for Bug in Firefox 3, which sends charCode 96 after Ctrl-Alt-Space
      // do nothing
    } else if (event.charCode > 0) {
      barcodescan_input += String.fromCharCode(event.charCode);
    } else if (event.keyCode > 0) {
      barcodescan_input += String.fromCharCode(event.keyCode);
    }
    return barcodescan_stopEvent(event);
  }
  return true;
}

document.onkeydown = barcodescan_onkeydown;
document.onkeypress = barcodescan_onkeypress;

/*
barcodescan_active = false;
barcodescan_timeout_id = null;
barcodescan_element = null;
function barcodescan_success(barcode) {
}
function barcodescan_reset() {
  window.clearTimeout(barcodescan_timeout_id);
  barcodescan_timeout_id = null;
  barcodescan_active = false;
  barcodescan_element.value = '';
}
function barcodescan_start() {
  window.clearTimeout(barcodescan_timeout_id);
  barcodescan_timeout_id = window.setTimeout('barcodescan_reset()', 2000);
  barcodescan_active = true;
  barcodescan_element.value = '';
  barcodescan_element.focus();
}
window.onload = function() {
  barcodescan_element = document.getElementById('barcode_field');
  window.onkeyup =  // MSIE workaround
  window.onkeypress = function(event) {
    if (!event) event = window.event;  // MSIE workaround
    if (event.ctrlKey && event.altKey && (event.charCode == 32 || event.keyCode == 32 || event.charCode == 160 || event.keyCode == 160)) {
      if (event.preventDefault) event.preventDefault();
      barcodescan_start();
    }
  };
  barcodescan_element.onkeypress = function(event) {
    var c;
    if (!event) event = window.event;  // MSIE workaround
    if (barcodescan_active && (event.keyCode == 13 || event.charCode == 13 || event.keyCode == 10 || event.charCode == 10)) {
      if (event.preventDefault) event.preventDefault();
      barcodescan_success(barcodescan_element.value);
      barcodescan_reset();
    }
  };
  barcodescan_element.onkeyup = function(event) {
    if (!barcodescan_active) barcodescan_reset();
  };
  barcodescan_element.onblur = function(event) {
    barcodescan_reset();
  };
  barcodescan_reset();
};
*/
