/*==============================================================================
Author:     John Gardner
Date:       September 2004

Note that some of the techniques here have been picked up from Chapter 9 of 
"JavaScript and DHTML Cookbook", by Danny Goodman, published by O'Reilly.

==============================================================================*/

function hotKeys (event) {

  // Get details of the event dependent upon browser
  event = (event) ? event : ((window.event) ? event : null);
  
  // We have found the event.
  if (event) {   
    
    // Hotkeys require that either the control key or the alt key is being held down
    if (event.ctrlKey || event.altKey || event.metaKey) {
    
      // Pick up the Unicode value of the character of the depressed key.
      var charCode = (event.charCode) ? event.charCode : ((event.which) ? event.which : event.keyCode);
      
      // Convert Unicode character to its lowercase ASCII equivalent
      var myChar = String.fromCharCode (charCode).toLowerCase();
      
      // Convert it back into uppercase if the shift key is being held down
      if (event.shiftKey) {myChar = myChar.toUpperCase();}
          
      // Now scan through the user-defined array to see if character has been defined.
      for (var i = 0; i < keyActions.length; i++) {
         
        // See if the next array element contains the Hotkey character
        if (keyActions[i].character == myChar) { 
      
          // Yes - pick up the action from the table
          var action;
            
          // If the action is a hyperlink, create JavaScript instruction in an anonymous function
          if (keyActions[i].actionType.toLowerCase() == "link") {
            action = new Function ('location.href  ="' + keyActions[i].param + '"');
          }
            
          // If the action is JavaScript, embed it in an anonymous function
          else if (keyActions[i].actionType.toLowerCase()  == "code") {
            action = new Function (keyActions[i].param);
          }
            
          // Error - unrecognised action.
          else {
            alert ('Hotkey Function Error: Action should be "link" or "code"');
            break;
          }
           
          // At last perform the required action from within an anonymous function.
          action ();
         
          // Hotkey actioned - exit from the for loop.
          break;
        }
      }
    }
  }
}
/*============================================================================*/
