/*=============================================================================
 * Product        : Bayesian Keystroke Dynamics StandAlone 2.0
 *                  BKDS-2.0
 * Version        : 2.0 
 * Date           : Thu, 18 Jun 2009 09:25:23 +0200 
 * File           : keystroke.js
 * 
 *=============================================================================
 *        (c) Copyright 2009, ProBayes SAS  -  all rights reserved
 *                       http://www.probayes.com/
 *=============================================================================
 *
 *------------------------- Description ---------------------------------------
 *    javascript function for determine the typed sequences for the input
 *   
 *-----------------------------------------------------------------------------
*/

/*global variable*/
var keystrokeEventArray=new Array ();
/*verify that the form is not empty*/
function checkForm(numero) {
with(window.document.forms[numero])
  {for (var i=0; i< elements.length; i++){
    if (elements[i].type=="text" && elements[i].value.length==0) return false;
    if (elements[i].type=="password" && elements[i].value.length==0) return false;
    }
  }
  return true;
}
/*initialize global variables*/
function load (){
  for (var i=0 ; i <window.document.forms[0].elements.length; i++)
    keystrokeEventArray[i]=new Array ();
}

/*register the date when one key is pressed*/
function keypress (inputIndex){
  if ( document.forms[0].elements[inputIndex].value.length == keystrokeEventArray[inputIndex].length) 
    keystrokeEventArray[inputIndex][document.forms[0].elements[inputIndex].value.length] =new Date();
}

/*check that the length of the typed word is egal to the size of event array*/
function check (inputIndex,e){
  var touche=(window.Event)?e.which:e.keyCode;
  /*reset the input when keys (left, right, up, down) are pressed*/
  if (touche>36 && touche<41 )
    {keystrokeEventArray[inputIndex]=new Array (); document.forms[0].elements[inputIndex].value="";}
  /*reset the input when the lengths are different*/
  if (document.forms[0].elements[inputIndex].value.length != keystrokeEventArray[inputIndex].length) 
    {keystrokeEventArray[inputIndex]=new Array (); document.forms[0].elements[inputIndex].value=""; }
}


/*register the typed sequences in the hidden input*/
function validate (inputIndex, outputIndex, delimiter){
document.forms[0].elements[outputIndex].value="";
for (var i= 0; i<(document.forms[0].elements[inputIndex].value.length)-1;i++)
   {
     var msecs=keystrokeEventArray[inputIndex][i+1].getTime()-keystrokeEventArray[inputIndex][i].getTime();
     if (msecs > 1000) msecs=1000;
     document.forms[0].elements[outputIndex].value=document.forms[0].elements[outputIndex].value+msecs + delimiter;
  }
}


