/**
 * handle keys pressed, to fake acceskeys and who knows what more!
 */
var emod;
var alt;      
var function_map = new Array(); 
var SHFT = 16;
var TAB = 9;
var ESC = 27;
var KEYUP = 38;
var KEYDN = 40;
var ENTER = 13;
var defaultConditionFunction = 'alt';

function addKeyMap(key, function_name, condition_function, id) {
  function_map[key] = {};
  function_map[key].function_name = function_name;
  if (condition_function == null) {
    condition_function = "alt";
  }
  function_map[key].condition_function = condition_function;
  function_map[key].id = id;
}

function KPsubmit(){
  try { 
    if (formname){
      // move focus to other element to trigger onChange event
      for (e in document.forms[formname].elements) {
        try {
         e.focus();
         break;
        } catch (e) {
        }
      }
      document.forms[formname].eol_applied.value = 1;
      eolsubmit(document.forms[formname]);
    }
  } catch(e) { 
    return;
  }   
}

function getKeyCode(ev) {
  if (ev) { //Moz
    return ev.keyCode;
  }
  if (window.event) { //IE
    return window.event.keyCode;
  }
}

function handleKeys(e){
  if (emod == "IE4+") {
    e = window.event;   
  }
  if (typeof e == "undefined" || e == null){
    return true; 
  }
  // try to execute function from function_map
  if (function_map[e.keyCode] != null) {
    var obj = function_map[e.keyCode];
    if (window[obj.condition_function](e)) {
      window[obj.function_name](obj.id);
      return false;
    }
  }
  // if I can't handle this, perhaps I have a parent who can
  if (self != top) { // this needed, because the top window is it's own parent
    if (window.parent.handleKeys) {
      return window.parent.handleKeys(e);
    }
  }
  return true;
}

function alt(e) {
  return (e.altKey || e.keyCode == '18');
}

function ctrl(e) {
  return (e.ctrlKey || e.keyCode == '17');
}

function onloadKP(e){
  emod = (e) ? (e.eventPhase) ? "W3C" : "NN4" : (window.event) ? "IE4+" : "unknown";
  if (emod == "NN4") {
    document.captureEvents(Event.KEYDOWN); 
  }
  document.onkeydown = handleKeys;
  return true;
}

function submitOnEnter(e) {
  if (emod == "IE4+") {
    e = window.event;   
  }  
  if (e.keyCode == '13') {
    eolsubmit(document.forms[formname]);
    return false;
  }
  return true;
}

function doOnclickForElement(id) {
  var el = document.getElementById(id);
  if (el) {
    return el.onclick();
  }
}

if (typeof(addLoadEvent) == 'function') {
  addLoadEvent(onloadKP);
} else {
  window.onload = onloadKP;
}
//addLoadEvent(function(){addKeyMap(83,"KPsubmit");});


