//alert('in passwordentry.js');

var encryptedpassword;

function setencryptedpassword(inputfromdocument)
{

// the document passwordentry.html which loads into the questions
// frame when the user clicks "enter password" above the navigation
// frame must run this function and must send the encrypted password
// to the function so that the function knows the encrypted password
// I will also need a tool so that I can set type in a 7 digit number
// and get out the encrypted version and then edit passwordentry.html
// with the encrypted version in the onload part that calls this function

 //alert('encryptedpassword is ' + inputfromdocument);
   encryptedpassword = inputfromdocument; //a seven digit number
}

function initializeinputboxes()
{

   //call this function when passwordentry.html loads 
   
//   document.passwordform.passwordattemptbox.value = "";
//   document.passwordform.resultbox.value = ""
  
//  alert(document.passwordform.username); //concerned that if it isn't there get an error
    // alert(!top.samplenextbuttonon); //'false' and false aren't the same.
     
  if (!top.samplenextbuttonon) 
  {
   //   alert('putting cursor in username box');
      document.passwordform.username.focus();
  }
}


function hasBeenValidated()
{
  
  setsigninflag();
  accessgranteddisplay(); 
  activatelinks();
  
}


function encryptandcheck()
{

   var passwordattempt;
   var encryptedattempt;
   var correctnumberofdigits = 7; 

   passwordattempt = document.passwordform.passwordattemptbox.value; 

   if (!alldigits(passwordattempt,correctnumberofdigits)) 
   {
      initializeinputboxes();
      alert("the password is " + correctnumberofdigits + " digits (0-9)"); 
      return; 
   }

   encryptedattempt = encrypt(passwordattempt); 

   if (encryptedattempt == encryptedpassword)
   {
      accessgranteddisplay(); 
      setTimeout('countdowntoaccess();',1000); 
      setTimeout('activatelinks();',500); 
      setsigninflag();
      //top.precacheremainingimages(); //causes problems book has precache happening before the page loads
   }
   else
   {
      document.passwordform.resultbox.value = "No match. Access denied. Sorry.";
      document.passwordform.passwordattemptbox.focus();
   }
   
}


function accessgranteddisplay()
{
   //document.getElementById('accessgranted').style.display = 'block';
}


var countdowntime = 5;

function countdowntoaccess()
{
   accessgrantedstring = "1st section loads in " + countdowntime + " seconds.";
   document.passwordform.resultbox.value = accessgrantedstring;
   if (countdowntime == 0) {loadalgebraskillspage(); return;}
   setTimeout('countdowntime--; countdowntoaccess();',1000);
}

function alldigits(anystring,therightnumber)
{  
   //Check that string is all numbers

   var anystring;
   var valid;
   
   if (anystring.length != therightnumber) {return false;}
   
   for (index=0; index<anystring.length; index++)
   {
      valid = false; //assume character at given index is nfg
      
      if (anystring.charAt(index) == "1" || anystring.charAt(index) == "6") {valid=true;}     
      if (anystring.charAt(index) == "2" || anystring.charAt(index) == "7") {valid=true;}
      if (anystring.charAt(index) == "3" || anystring.charAt(index) == "8") {valid=true;}
      if (anystring.charAt(index) == "4" || anystring.charAt(index) == "9") {valid=true;}
      if (anystring.charAt(index) == "5" || anystring.charAt(index) == "0") {valid=true;}
      
      if (!valid) {return false;} //if no digit found return and invalidate
   }
   
   return true; //if get out of loop okay then return with valid flag
   
}

function encrypt(anynumber)
{  

   var step1, step2, encryptedresult;

   step1 =           Math.pow(anynumber,2) % 25060027;
   step2 =           Math.pow(step1,2) % 25060027;
   encryptedresult = (step2 * anynumber) % 25060027;

   return encryptedresult;   
}


function resetFrame()
{
  top.buttonsandcontentsframeset.contentsframe.location = "mainpagelinks/passwordentry.php";
}


function deactivateLinks()
{
   top.navigationframe.document.getElementById('logoutDiv').style.display = "none";
   top.navigationframe.document.getElementById('samplelink').style.display = "block";
   top.navigationframe.document.getElementById('3buttons').style.display="block";

   top.navigationframe.document.getElementById('algebrawarmuplink').style.display = "none";  
   top.navigationframe.document.getElementById('manipulationlink').style.display = "none"; 
   top.navigationframe.document.getElementById('fractionslink').style.display = "none"; 
   //top.navigationframe.document.getElementById('fractionslink').style.color = "blue"; 
   top.navigationframe.document.getElementById('functionslink').style.display = "none"; 
   top.navigationframe.document.getElementById('wordproblemslink').style.display = "none"; 
   top.navigationframe.document.getElementById('laplink').style.display = "none"; 
   top.navigationframe.document.getElementById('areaslink').style.display = "none"; 
   //top.navigationframe.document.getElementById('areaslink').style.color = "blue"; 
   top.navigationframe.document.getElementById('triangleslink').style.display = "none"; 
   top.navigationframe.document.getElementById('paslink').style.display = "none"; 
   top.navigationframe.document.getElementById('unitspavlink').style.display = "none"; 
   top.navigationframe.document.getElementById('unitsalgebralink').style.display = "none"; 
   top.navigationframe.document.getElementById('statisticswarmuplink').style.display = "none"; 
   top.navigationframe.document.getElementById('averageslink').style.display = "none"; 
   top.navigationframe.document.getElementById('probabilitylink').style.display = "none"; 
   top.navigationframe.document.getElementById('mmmlink').style.display = "none"; 
   top.navigationframe.document.getElementById('divisibilitylink').style.display = "none"; 
   top.navigationframe.document.getElementById('characteristicslink').style.display = "none"; 
   top.navigationframe.document.getElementById('remainderslink').style.display = "none"; 
   top.navigationframe.document.getElementById('digitologylink').style.display = "none"; 
   top.navigationframe.document.getElementById('chartinglink').style.display = "none"; 
   top.navigationframe.document.getElementById('thinkinglink').style.display = "none"; 
   top.navigationframe.document.getElementById('countinglink').style.display = "none"; 
   
   top.navigationframe.document.getElementById('algebrawarmupteaser').style.display = "block";  
   top.navigationframe.document.getElementById('manipulationteaser').style.display = "block"; 
   top.navigationframe.document.getElementById('fractionsteaser').style.display = "block"; 
   top.navigationframe.document.getElementById('functionsteaser').style.display = "block"; 
   top.navigationframe.document.getElementById('wordproblemsteaser').style.display = "block"; 
   top.navigationframe.document.getElementById('lapteaser').style.display = "block"; 
   top.navigationframe.document.getElementById('areasteaser').style.display = "block"; 
   top.navigationframe.document.getElementById('trianglesteaser').style.display = "block"; 
   top.navigationframe.document.getElementById('pasteaser').style.display = "block"; 
   top.navigationframe.document.getElementById('unitspavteaser').style.display = "block"; 
   top.navigationframe.document.getElementById('unitsalgebrateaser').style.display = "block"; 
   top.navigationframe.document.getElementById('statisticswarmupteaser').style.display = "block"; 
   top.navigationframe.document.getElementById('averagesteaser').style.display = "block"; 
   top.navigationframe.document.getElementById('probabilityteaser').style.display = "block"; 
   top.navigationframe.document.getElementById('mmmteaser').style.display = "block"; 
   top.navigationframe.document.getElementById('divisibilityteaser').style.display = "block"; 
   top.navigationframe.document.getElementById('characteristicsteaser').style.display = "block"; 
   top.navigationframe.document.getElementById('remaindersteaser').style.display = "block"; 
   top.navigationframe.document.getElementById('digitologyteaser').style.display = "block"; 
   top.navigationframe.document.getElementById('chartingteaser').style.display = "block"; 
   top.navigationframe.document.getElementById('thinkingteaser').style.display = "block"; 
   top.navigationframe.document.getElementById('countingteaser').style.display = "block";
}




function activatelinks()
{
   top.navigationframe.document.getElementById('logoutDiv').style.display = "block";
   top.navigationframe.document.getElementById('samplelink').style.display = "none";
   top.navigationframe.document.getElementById('3buttons').style.display="none";

   top.navigationframe.document.getElementById('algebrawarmuplink').style.display = "block";  
   top.navigationframe.document.getElementById('manipulationlink').style.display = "block"; 
   top.navigationframe.document.getElementById('fractionslink').style.display = "block"; 
   top.navigationframe.document.getElementById('functionslink').style.display = "block"; 
   top.navigationframe.document.getElementById('wordproblemslink').style.display = "block"; 
   top.navigationframe.document.getElementById('laplink').style.display = "block"; 
   top.navigationframe.document.getElementById('areaslink').style.display = "block"; 
   top.navigationframe.document.getElementById('triangleslink').style.display = "block"; 
   top.navigationframe.document.getElementById('paslink').style.display = "block"; 
   top.navigationframe.document.getElementById('unitspavlink').style.display = "block"; 
   top.navigationframe.document.getElementById('unitsalgebralink').style.display = "block"; 
   top.navigationframe.document.getElementById('statisticswarmuplink').style.display = "block"; 
   top.navigationframe.document.getElementById('averageslink').style.display = "block"; 
   top.navigationframe.document.getElementById('probabilitylink').style.display = "block"; 
   top.navigationframe.document.getElementById('mmmlink').style.display = "block"; 
   top.navigationframe.document.getElementById('divisibilitylink').style.display = "block"; 
   top.navigationframe.document.getElementById('characteristicslink').style.display = "block"; 
   top.navigationframe.document.getElementById('remainderslink').style.display = "block"; 
   top.navigationframe.document.getElementById('digitologylink').style.display = "block"; 
   top.navigationframe.document.getElementById('chartinglink').style.display = "block"; 
   top.navigationframe.document.getElementById('thinkinglink').style.display = "block"; 
   top.navigationframe.document.getElementById('countinglink').style.display = "block"; 
   
   top.navigationframe.document.getElementById('algebrawarmuplink').style.color = "blue";  
   top.navigationframe.document.getElementById('manipulationlink').style.color = "blue"; 
   top.navigationframe.document.getElementById('fractionslink').style.color = "blue"; 
   top.navigationframe.document.getElementById('functionslink').style.color = "blue"; 
   top.navigationframe.document.getElementById('wordproblemslink').style.color = "blue"; 
   top.navigationframe.document.getElementById('laplink').style.color = "blue"; 
   top.navigationframe.document.getElementById('areaslink').style.color = "blue"; 
   top.navigationframe.document.getElementById('triangleslink').style.color = "blue"; 
   top.navigationframe.document.getElementById('paslink').style.color = "blue"; 
   top.navigationframe.document.getElementById('unitspavlink').style.color = "blue"; 
   top.navigationframe.document.getElementById('unitsalgebralink').style.color = "blue"; 
   top.navigationframe.document.getElementById('statisticswarmuplink').style.color = "blue"; 
   top.navigationframe.document.getElementById('averageslink').style.color = "blue"; 
   top.navigationframe.document.getElementById('probabilitylink').style.color = "blue"; 
   top.navigationframe.document.getElementById('mmmlink').style.color = "blue"; 
   top.navigationframe.document.getElementById('divisibilitylink').style.color = "blue"; 
   top.navigationframe.document.getElementById('characteristicslink').style.color = "blue"; 
   top.navigationframe.document.getElementById('remainderslink').style.color = "blue"; 
   top.navigationframe.document.getElementById('digitologylink').style.color = "blue"; 
   top.navigationframe.document.getElementById('chartinglink').style.color = "blue"; 
   top.navigationframe.document.getElementById('thinkinglink').style.color = "blue"; 
   top.navigationframe.document.getElementById('countinglink').style.color = "blue"; 
   
   top.navigationframe.document.getElementById('algebrawarmupteaser').style.display = "none";  
   top.navigationframe.document.getElementById('manipulationteaser').style.display = "none"; 
   top.navigationframe.document.getElementById('fractionsteaser').style.display = "none"; 
   top.navigationframe.document.getElementById('functionsteaser').style.display = "none"; 
   top.navigationframe.document.getElementById('wordproblemsteaser').style.display = "none"; 
   top.navigationframe.document.getElementById('lapteaser').style.display = "none"; 
   top.navigationframe.document.getElementById('areasteaser').style.display = "none"; 
   top.navigationframe.document.getElementById('trianglesteaser').style.display = "none"; 
   top.navigationframe.document.getElementById('pasteaser').style.display = "none"; 
   top.navigationframe.document.getElementById('unitspavteaser').style.display = "none"; 
   top.navigationframe.document.getElementById('unitsalgebrateaser').style.display = "none"; 
   top.navigationframe.document.getElementById('statisticswarmupteaser').style.display = "none"; 
   top.navigationframe.document.getElementById('averagesteaser').style.display = "none"; 
   top.navigationframe.document.getElementById('probabilityteaser').style.display = "none"; 
   top.navigationframe.document.getElementById('mmmteaser').style.display = "none"; 
   top.navigationframe.document.getElementById('divisibilityteaser').style.display = "none"; 
   top.navigationframe.document.getElementById('characteristicsteaser').style.display = "none"; 
   top.navigationframe.document.getElementById('remaindersteaser').style.display = "none"; 
   top.navigationframe.document.getElementById('digitologyteaser').style.display = "none"; 
   top.navigationframe.document.getElementById('chartingteaser').style.display = "none"; 
   top.navigationframe.document.getElementById('thinkingteaser').style.display = "none"; 
   top.navigationframe.document.getElementById('countingteaser').style.display = "none"; 
   
}

function loadalgebraskillspage()
{
   top.buttonsandcontentsframeset.contentsframe.location.href = "../algebra/algebraskills.html"; 
}

function setsigninflag()
{ 
   top.samplenextbuttonon = true;
}

function isreturnkey(evt)
 {
    evt = (evt) ? evt : (window.event) ? window.event : "";

    var whichkey;

    if (evt)
    {
       whichkey = (evt.which) ? evt.which : evt.keyCode;
    }

    return (whichkey == 13); //if it's the return key, then return true

 }
