
var objTimer=null;
var StartTime=0;
var SecondsPassed=0;
var StrLength=0;
var gradeout="";


function timer() {
  var dtNow_ = new Date();

  SecondsPassed = dtNow_.getTime();
  SecondsPassed = SecondsPassed / 1000;
  SecondsPassed = Math.round(SecondsPassed - StartTime);
  StrLength = document.frmType.txtTop_.value.length;
  StrLength = StrLength + document.frmType.txtTypeHere_.value.length;
 
  gradeout="Characters: " + StrLength;
  gradeout += "  Seconds Passed:" + SecondsPassed; 
  
  gradeout += "  Word per minute:" + Math.round((StrLength / SecondsPassed)/ (6 / 60));
  document.frmType.txtStats_.value= + SecondsPassed +" Seconds";
}

function startTimer() {
  StartTime = parseInt((new Date).getTime()/1000);
  SecondsPassed=0;
  endTimer();
  objTimer = setInterval("timer();",1000);
}

function endTimer() {
  SecondsPassed = 0;
  if (objTimer != null) {
    clearInterval(objTimer);
    objTimer = null;
  }
}


function validateTyping(e) {

  var isNN = (navigator.appName.indexOf("Netscape")!=-1);

  if (isNN) {
    sChar = String.fromCharCode(e.which);
  } else {
    sChar = String.fromCharCode(event.keyCode);
  }

  var iTypedLength =  document.frmType.txtTypeHere_.value.length;
  var sInitialValue = document.frmType.txtBottom_.value.substring(0,iTypedLength);
  var sTestChunk =    document.frmType.txtTypeHere_.value.substring(0,iTypedLength) + sChar;
  var sCorrectChunk = document.frmType.txtBottom_.value.substring(0,iTypedLength+1);

  
  if (sTestChunk != sCorrectChunk) {
    document.frmType.txtTypeHere_.value=sInitialValue;
  } else {
    document.frmType.txtTypeHere_.value=sCorrectChunk;
  }
  if (escape(document.frmType.txtBottom_.value.charAt(iTypedLength))=="%0D") {
    document.frmType.txtTop_.value+= "\n" +
    document.frmType.txtBottom_.value.substring(0,iTypedLength);
    document.frmType.txtBottom_.value=
    document.frmType.txtBottom_.value.substring(iTypedLength+2,document.frmType.txtBottom_.value.length);
    document.frmType.txtTypeHere_.value="";
  }
  
  //There should never be a return character at the end.
  if (escape(document.frmType.txtTypeHere_.value.charAt(iTypedLength-1))=="%0A") {
    // Remove return character.
    document.frmType.txtTypeHere_.value=
       document.frmType.txtTypeHere_.value.substring(0,iTypedLength-2);
  }
}