/**
 * handle keys pressed, to fake acceskeys and who knows what more!
 */
var emod;
var alt;      
var function_map = new Array(); 
var condition_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) {
  function_map[key] = function_name;
  if (condition_function == null) {
    condition_function = defaultConditionFunction;
  }
  condition_map[key] = condition_function;
}

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 (window[function_map[e.keyCode]] && window[condition_map[e.keyCode]]) {
    if (window[condition_map[e.keyCode]](e)) {
      window[function_map[e.keyCode]]();
      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]);
  } 
}
