/****************************************************/
/*  Title      : JavaScript Calculator              */
/*  Course     : CPAN542 Client Server              */
/*  Date       : Oct 5, 2002                        */
/*                                                  */
/*  Programmer : In Chul Koo                        */
/*  E-mail     : ickoo@rogers.com                   */
/*  Web Address: members.rogers.com/ickoo           */
/*               sparcy.humber.org/~knch0013        */
/*                                                  */
/*  Purpose : to define keyboard input keys         */
/****************************************************/
var isNav;

if(navigator.appName=="Netscape")
{ isNav=true;
  document.captureEvents(Event.KEYPRESS); 
  document.onkeypress = showKey; 
}
else
  isNav=false;

/*****************************/
/*       inputByKey          */
/*****************************/
function inputByKey(e)
{ var theKey;
  if (isNav)
    theKey=e.which;
  else
    theKey=e.keyCode;
 
  if( theKey >= 40 && theKey <= 57 ) 
  { switch(theKey)
    { case  42: inputValue(document.calculatorForm, '*');      // value: '*'
                break;
      case  44:                                                // value: ','
                break;
      case  47: inputValue(document.calculatorForm, '/');      // value: '/'
                break;
      case  50:                                                // value: '2'
      case  51:                                                // value: '3'
      case  52:                                                // value: '4'
      case  53:                                                // value: '5'
      case  54:                                                // value: '6'
      case  55: if(currMode!="bin")                            // value: '7'
                  inputValue(document.calculatorForm, String.fromCharCode(theKey));
                break;
      case  56:                                                // value: '8'
      case  57: if(currMode=="hex" || currMode==="dec")        // value: '9'
                  inputValue(document.calculatorForm, String.fromCharCode(theKey));
                break;
      default : inputValue(document.calculatorForm, String.fromCharCode(theKey));
    }
  }
  else
  { switch(theKey)
    { 
      case  24: powerOff(document.calculatorForm);               // value: '^x'
                break;                                           
      case  32: inputValue(document.calculatorForm, 'BS');       // value: 'SP' ->BS
                break;                                           
      case  88:                                                  
      case 120: if(currMode=="dec")
                  inputValue(document.calculatorForm, '^2');     // value: 'X' ->X^2
                break;                                           
      case  94: if(currMode=="dec")
                  inputValue(document.calculatorForm, '^');      // value: '^'
                break;                                           
      case  13:                                                  // value: 'enter'
      case  61: executeForm(document.calculatorForm);            // value: '='
                break;                                           
      case  27: clearAll(document.calculatorForm);               // value: 'ESC'
                break;                                           
      case  71:                                                  
      case 103: if(currMode=="dec")
                  inputValue(document.calculatorForm, 'log(');   // value: 'g' ->log(
                break;                                           
      case  76:                                                  
      case 108: if(currMode=="dec")
                  inputValue(document.calculatorForm, 'ln(');    // value: 'l' ->ln(
                break;                                           
      case  78:                                                  
      case 110: inputValue(document.calculatorForm, 'Ans');      // value: 'n' ->Ans
                break;                                           
      case  80:                                                  
      case 112: if(currMode=="dec")                              
                  inputValue(document.calculatorForm, 'pi');     // value: 'p' ->PI
                break;
      case  82:
      case 114: if(currMode=="dec")
                  inputValue(document.calculatorForm, 'sqrt(');  // value: 'r' ->sqrt
                break;
      case  83: if(currMode=="dec")
                  inputValue(document.calculatorForm, 'asin(');  // value: 'S' ->asin
                break;                                           
      case 115: if(currMode=="dec")
                  inputValue(document.calculatorForm, 'sin(');   // value: 's' ->sin
                break;                                           
      case  67: if(currMode=="dec")
                  inputValue(document.calculatorForm, 'acos(');  // value: 'C' ->cos
                break;                                           
      case  99: if(currMode=="dec")
                  inputValue(document.calculatorForm, 'cos(');   // value: 'c' ->cos
                break;                                           
      case  84: if(currMode=="dec")
                  inputValue(document.calculatorForm, 'atan(');  // value: 'T' ->atan
                break;                                           
      case 116: if(currMode=="dec")
                  inputValue(document.calculatorForm, 'tan(');   // value: 't' ->tan
                break;                                           
      case  33: selectMode(document.calculatorForm, 'hex');      // value: '!' ->select [hex]
                break;
      case  64: selectMode(document.calculatorForm, 'dec');      // value: '@' ->select [dec]
                break;
      case  35: selectMode(document.calculatorForm, 'oct');      // value: '#' ->select [oct]
                break;
      case  36: selectMode(document.calculatorForm, 'bin');      // value: '$' ->select [bin]
                break;
      case  37: if(currMode=="dec")                              // value: '%' ->selectDrg
                  selectDrg(document.calculatorForm);
                break;                                           
      case  79:
      case 111: changeSoundMode(document.calculatorForm);       // value: '0' ->sound_on_off
                break;
      default:
    }
  }
  if(currMode=="hex" && ((theKey >= 65 && theKey <= 70) || (theKey >= 97 && theKey <= 102)) )
    inputValue(document.calculatorForm, String.fromCharCode(theKey).toUpperCase());
  else if(currMode=="dec" && ( theKey == 69 || theKey == 101 ))
    inputValue(document.calculatorForm, 'e');

  if(document.title == "Scientific Calculator Main Window")
    checkTesting();
}

