var encryptedanswer;
var correctanswer;
var answerstring = "";
var numberofdefineoneglobalvariabletries = 0;

function setencryptedanswer(inputfromdocument)
{
    encryptedanswer = inputfromdocument;
    var decryptedanswer = decrypt(encryptedanswer);
    //alert(decryptedanswer);
    var next=decryptedanswer;
    var ans = new Array();
    var letteranswer = new Array();
    answerstring = "";
    letteranswer[0] = 'A';
    letteranswer[1] = 'B';
    letteranswer[2] = 'C';
    letteranswer[3] = 'D';
    letteranswer[4] = 'E';
    
    for (i=9, j=0; i>-1; i--, j++)
    {
       prev = next; 
       next = next % Math.pow(5,i);       
       ans[j] = (prev-next)/Math.pow(5,i);
       answerstring += letteranswer[ans[j]];
    }
    
    //alert(answerstring);
    
}

function setencryptedanswer12(inputfromdocument)
{
    answerstring = inputfromdocument;
    
}

function defineoneglobalvariable()
{
   //alert ('defining global variables for one question');
   
   if (numberofdefineoneglobalvariabletries>20) {return;}
   
   var loaded = false;
   loaded = top.allframes_loaded();
   
   if (!loaded) 
   {
      //alert('not loaded');
      numberofdefineoneglobalvariabletries++;
      setTimeout('defineoneglobalvariable()',200);
      return;
   }
   numberofdefineoneglobalvariabletries = 0;
   //now okay to do the function
   
   radioset = new Array();
   radioset[0] = top.buttonsandcontentsframeset.contentsframe.document.forms[0].q1;
}

function setcorrectanswer(inputfromdocument)
{
   correctanswer = inputfromdocument;
//   alert(correctanswer);
}

var numberofdefineglobalvariablestries = 0;

function defineglobalvariables()

//Called when page loads (onload="blahblah")
//Eventually these variables will have to be a 2D array
//if try to make globals before page loads then get junk since forms 
//don't exist yet. probably could define radiosets in checkans. they
//really don't need to be pre-done although i suppose it saves time.

{ 

  //alert('defining global variables');
  
  if (numberofdefineglobalvariablestries>20) {return;}
   
   var loaded = false;
   loaded = top.allframes_loaded();
   
   if (!loaded) 
   {  
      //alert('not loaded');
      numberofdefineglobalvariablestries++;
      setTimeout('defineglobalvariables()',200);
      return;
   }
   numberofdefineglobalvariablestries = 0;
   //now okay to do the function
  
  radioset = new Array();
  
  radioset[0] = top.buttonsandcontentsframeset.contentsframe.document.forms[0].q1;
  radioset[1] = top.buttonsandcontentsframeset.contentsframe.document.forms[0].q2;
  radioset[2] = top.buttonsandcontentsframeset.contentsframe.document.forms[0].q3;
  radioset[3] = top.buttonsandcontentsframeset.contentsframe.document.forms[0].q4;
  radioset[4] = top.buttonsandcontentsframeset.contentsframe.document.forms[0].q5;
  radioset[5] = top.buttonsandcontentsframeset.contentsframe.document.forms[0].q6;
  radioset[6] = top.buttonsandcontentsframeset.contentsframe.document.forms[0].q7;
  radioset[7] = top.buttonsandcontentsframeset.contentsframe.document.forms[0].q8;
  radioset[8] = top.buttonsandcontentsframeset.contentsframe.document.forms[0].q9;
  radioset[9] = top.buttonsandcontentsframeset.contentsframe.document.forms[0].q10;
  
}   

function defineglobalvariables12()

//Called when page loads (onload="blahblah")
//Eventually these variables will have to be a 2D array
//if try to make globals before page loads then get junk since forms 
//don't exist yet. probably could define radiosets in checkans. they
//really don't need to be pre-done although i suppose it saves time.

{ 

  //alert('defining global variables');
  
  if (numberofdefineglobalvariablestries>20) {return;}
   
   var loaded = false;
   loaded = top.allframes_loaded();
   
   if (!loaded) 
   {  
      //alert('not loaded');
      numberofdefineglobalvariablestries++;
      setTimeout('defineglobalvariables12()',200);
      return;
   }
   numberofdefineglobalvariablestries = 0;
   //now okay to do the function
  
  radioset = new Array();
  
  radioset[0] = top.buttonsandcontentsframeset.contentsframe.document.forms[0].q1;
  radioset[1] = top.buttonsandcontentsframeset.contentsframe.document.forms[0].q2;
  radioset[2] = top.buttonsandcontentsframeset.contentsframe.document.forms[0].q3;
  radioset[3] = top.buttonsandcontentsframeset.contentsframe.document.forms[0].q4;
  radioset[4] = top.buttonsandcontentsframeset.contentsframe.document.forms[0].q5;
  radioset[5] = top.buttonsandcontentsframeset.contentsframe.document.forms[0].q6;
  radioset[6] = top.buttonsandcontentsframeset.contentsframe.document.forms[0].q7;
  radioset[7] = top.buttonsandcontentsframeset.contentsframe.document.forms[0].q8;
  radioset[8] = top.buttonsandcontentsframeset.contentsframe.document.forms[0].q9;
  radioset[9] = top.buttonsandcontentsframeset.contentsframe.document.forms[0].q10;
  radioset[10] = top.buttonsandcontentsframeset.contentsframe.document.forms[0].q11;
  radioset[11] = top.buttonsandcontentsframeset.contentsframe.document.forms[0].q12;
  
}   

function radioselectedvalue(anyradioset)

//The radio buttons don't have this built in.
//So I had to write a function to figure out which
//choice the user had made and return its value.
//The radio buttons don't seem to be regarded as
//a single entity so there is no "selectedvalue" property.
//the set of radio buttons is regarded by the computer
//as an array but the whole array has no properties, only
//the individual elements of the array have any properties.

{  
   var chosenindex = -1;
//   var selected = false;
   
   for (var index=0; index < anyradioset.length; index++)
   {
      if (anyradioset[index].checked == true) 
      {
         chosenindex = index; 
 //        selected = true;
      }
   }
   
   if (chosenindex == 0) return "A";
   if (chosenindex == 1) return "B";
   if (chosenindex == 2) return "C";
   if (chosenindex == 3) return "D";
   if (chosenindex == 4) return "E";
   if (chosenindex == -1) return "-";
}

function checkoneans() 
{
 // alert('in checkoneans now');
  if (!top.allframes_loaded()) {alert('Wait a moment and try again. Some files are still loading'); return;}
  if (guessanimationid) {stopguessing();}
  if (processresponseid) {stopprocessing();}
  
  var usersanswer;
 
  var documentref = top.buttonsandcontentsframeset.buttonsframe.document;
   

//the radio values are string whether you quote them in the html file or not
//parseInt converts a string to an integer.
 
 //alert('about to set usersanswer');
 
      usersanswer=radioselectedvalue(radioset[0]);

//alert('usersanswer set');
   
   //alert(usersanswer); two forms in instructions now
   
   if (anyblankquestions()) 
   {
      documentref.getElementById('output').value = " Please choose an answer. ";
      return;
   }
   
  // alert('about to check');
   
   if (usersanswer == "A") 
   {
     processresponse(" Incorrect. ETS has a special program for monsters with disabilities.");
   }
   
   if (usersanswer == "B") 
   {
       processresponse(" Don't kid yourself; EVERYONE takes the SAT. ");
   }
   
   if (usersanswer == "C") 
   {
       processresponse(" Correct! The author has a Ph.D. in nuclear physics so of course he picked C.");
   }
   
   if (usersanswer == "D") 
   {
       processresponse(" Test-taking isn't fun, but there's no need for violence.");
   }
   
   if (usersanswer == "E") 
   {
      processresponse(" Gotcha! It's an ETS trick: On hard questions, if it seems obvious it's wrong.");
   }  
}

function checkans()
{  

//   alert('in checkans');
   if (radioset.length == 10)
   {
      checktenans();
   }
   else if (radioset.length == 12)
   { 
      check12ans();
   }
}

function check12ans()
{
if (!top.allframes_loaded()) {alert('Wait a moment and try again. Some files are still loading'); return;}
   if (guessanimationid) {stopguessing();}
   if (processresponseid) {stopprocessing();}
   
   var attemptstring = "";
   var resultstring = "";

   var documentref = top.buttonsandcontentsframeset.buttonsframe.document;
   var skipped = false;
   var numberincorrect = 0;
   var numbercorrect = 0;
   
   var correctstring = "";
   var incorrectstring = "";
   
   for (var index=0; index<12; index++)
   {
      attemptstring += radioselectedvalue(radioset[index]);
   }

   for (var index=0; index<12; index++)
   {
      if (attemptstring.charAt(index) == "-") 
      {
         resultstring += "- "; 
         skipped = true;
      }
      else if (attemptstring.charAt(index) == answerstring.charAt(index)) 
      {
         correctstring += " " + (index+1);
         resultstring += "Y "; 
         numbercorrect++;
      }
      else 
      {
         incorrectstring += " " + (index+1);
         resultstring += "N "; 
         numberincorrect++;
      }
   }
   
   if (correctstring == "") {correctstring = " none";}
   if (incorrectstring == "") {incorrectstring = " none";}
   
   //no room for the word question # with 12 questions
   if (numbercorrect == 1) {correctstring = " #" + correctstring;}
   if (numberincorrect == 1) {incorrectstring = " #" + incorrectstring;}
   
   if (top.navigationframe.difficultylevel == 3)
   {
      if (skipped) 
      {
         documentref.getElementById('output').value = ' At level 3 you must answer all questions.';
      }
      else if (numberincorrect == 0)
      {
         documentref.getElementById('output').value = ' You were PERFECT on level 3. Congratulations! ';
      }
      else
      {
         documentref.getElementById('output').value = " You got at least one wrong. Find the incorrect answer(s). Build your accuracy.";
      }
   }
   
   else if (top.navigationframe.difficultylevel == 2)
   {
      if (skipped) 
      {
         documentref.getElementById('output').value = ' At level 2 you must answer all questions.';
      }
      else if (numberincorrect == 0) 
      {
         documentref.getElementById('output').value = ' You were PERFECT on level 2. Could it be time to try level 3?'
      }
      else if (numberincorrect == 1)
      {
         documentref.getElementById('output').value = ' You got only one wrong. Find the one incorrect answer. Build your accuracy.'
      }
      else
      {
         documentref.getElementById('output').value = ' You have ' + numbercorrect + ' correct answers. Find the incorrect answers. Build your accuracy.'
      }
   }
   
   else // (top.navigationframe.difficultylevel == 1)
   {
      documentref.getElementById('output').value = " " + resultstring + " || Correct:" + correctstring + ". || Incorrect:" + incorrectstring + ".";
   }
   
}

function checktenans() 
{
   if (!top.allframes_loaded()) {alert('Wait a moment and try again. Some files are still loading'); return;}
   if (guessanimationid) {stopguessing();}
   if (processresponseid) {stopprocessing();}
   
   
  // var answer = new Array();
   var attemptstring = "";
   var resultstring = "";
 //  var attempt1_10 = 0;
 //  var encryptedattempt1_10 = 0;
 //  var sectionnumber, encryptedanswer1_10;
   var documentref = top.buttonsandcontentsframeset.buttonsframe.document;
   var skipped = false;
   var numberincorrect = 0;
   var numbercorrect = 0;
   
   var correctstring = "";
   var incorrectstring = "";

//the answers need to be put into mainscript.js as globals and this script running
//in the contentsframe can ask the main page for the encrypted answer using a getencryptedanswer
//function. WILL DO LATER.

//the radio values are string whether you quote them in the html file or not
//parseInt converts a string to an integer. but the index is just an number so no need for parseint
   
   for (var index=0; index<10; index++)
   {
      attemptstring += radioselectedvalue(radioset[index]);
      //alert(attemptstring);
   }
   
  // if (anyblankquestions()) 
   //{
   //   documentref.getElementById('output').value = ' Please answer all 10 questions.'; 
   //   return;
  // }

   //alert(attemptstring);
   //alert(answerstring);

   for (var index=0; index<10; index++)
   {
      if (attemptstring.charAt(index) == "-") 
      {
         resultstring += "- "; 
         skipped = true;
      }
      else if (attemptstring.charAt(index) == answerstring.charAt(index)) 
      {
         correctstring += " " + (index+1);
         resultstring += "Y "; 
         numbercorrect++;
      }
      else 
      {
         incorrectstring += " " + (index+1);
         resultstring += "N "; 
         numberincorrect++;
      }
      
      
      //alert(resultstring);
   }
   
   if (correctstring == "") {correctstring = " none";}
   if (incorrectstring == "") {incorrectstring = " none";}
   
   if (numbercorrect == 1) {correctstring = " Question #" + correctstring;}
   if (numberincorrect == 1) {incorrectstring = " Question #" + incorrectstring;}
   
   if (top.navigationframe.difficultylevel == 3)
   {
      if (skipped) 
      {
         documentref.getElementById('output').value = ' At level 3 you must answer all questions.';
      }
      else if (numberincorrect == 0)
      {
         documentref.getElementById('output').value = ' You got 10 out of 10 on level 3. Congratulations! ';
      }
      else
      {
         documentref.getElementById('output').value = " You got at least one wrong. Find the incorrect answer(s). Build your accuracy.";
      }
   }
   
   else if (top.navigationframe.difficultylevel == 2)
   {
      if (skipped) 
      {
         documentref.getElementById('output').value = ' At level 2 you must answer all questions.';
      }
      else if (numberincorrect == 0) 
      {
         documentref.getElementById('output').value = ' You got 10 out of 10 on level 2. Could it be time to try level 3?'
      }
      else if (numberincorrect == 1)
      {
         documentref.getElementById('output').value = ' You got 9 out of 10 correct. Find the one incorrect answer. Build your accuracy.'
      }
      else
      {
         documentref.getElementById('output').value = ' You got ' + numbercorrect + ' out of 10 correct. Find the incorrect answers. Build your accuracy.'
      }
   }
   
   else // (top.navigationframe.difficultylevel == 1)
   {
      documentref.getElementById('output').value = " " + resultstring + " || Correct:" + correctstring + ". || Incorrect:" + incorrectstring + ".";
   }
   

//Now we convert a 10 letter string to a number. There are five possible letters
//a-e. If you wanted your number representing the string to start at zero and
//end at the number of possible 10 letter strings you would use 0-4 to represent
//a-e and an exponent of 5 starting with 5^9 and going down to 5^0. However, I don't
//want to start at zero. I want the smallest number to be pretty big so that the
//encyption is never obvious. Zero, for example would just encrypt to zero. So I just
//use 1-5 to represent a-e and the same base 5 from 5^9 down to 5^0. My numbers don't
//start at zero and there is some skipping. But it doesn't matter. I still get a 
//unique 7 or 8 digit number for every set of 10 answers and this number has a unique
//encrypted value. but if you are allowed to use zero you can get the unencrypted number
//in more than one way for example 7015760 = 3244001020 and also 3243445515
   
//   for (index=0, exponent=9; index<10; index++,exponent--)
//   {
//      attempt1_10 += Math.pow(5,exponent)*answer[index]
//   }

//Now encrypt the attempt . . .
   
//   encryptedattempt1_10 = encrypt(attempt1_10);
   
//   encryptedanswer1_10 = encryptedanswer;  //global encryptedanswer got set when document loaded

// . . . and compare the encrypted attempt with the encrypted answer. The answer
// was put into an encryption program and encrypted version determined. The encrypted
//answer was then given to this program (just by typing it into the code). Because
//of the guaranteed one to one correspondence of the RSA encryption algorithm, if
//the encrypted attempt is the same as the encrypted answer, then the attempt must be
//the correct answer. 
   
//   if (encryptedattempt1_10 == encryptedanswer1_10) 
//   {
//      documentref.getElementById('output').value = ' Congratulations! You have chosen wisely.';
//   }
//   else 
//   {
//      documentref.getElementById('output').value = ' At least one is wrong. Maybe more. Keep trying. ';
//   }         
//

}

function checkifblank(anyradioset)
{  
   var blank = true;
   
   for (var index=0; index < anyradioset.length; index++)
   {
      if (anyradioset[index].checked == true) 
         {blank = false; }
   }
   
   if (blank == true) 
      { return true; }
   else
      { return false; }                          
}


function anyblankquestions()
{  
   var ans = new Array();
   
   for (var index=0; index<radioset.length; index++)
   {
      ans[index]=checkifblank(radioset[index]);
      if (ans[index]==true) {return true;}
   }
   
   return false;         
}

function encrypt(anyattempt)

//This is the *** encryption algorithm using n=25060027 and an encryption 
//exponent of 5. To avoid numbers that are too large, I square the number,
//take the modulus, square it again, take the mod again, then multiply by the
//original number and take the mod once more. This is the same as taking the
//number to the fifth power and then taking the mod. Javascript seems to be
//okay with 16 or 17 digit numbers. Of course, someone who knows how to 
//decrypt the *** algorithm could decrypt all the answers and have a complete
//set of answers. However, the person would have to read about ***, figure
//out how it works and then do the fairly nasty math necessary to decrypt. The
//decryption exponent (not given here for obvious reasons) is much larger than 
//5 and can only be found by someone who knows the algorithm. This was probably
//the most fun part of the whole project. 

{  
   var step1, step2, encryptedattempt;

   step1 = Math.pow(anyattempt,2) % 25060027;
   step2 = Math.pow(step1,2) % 25060027;
   encryptedattempt = (step2 * anyattempt) % 25060027;
   
   return encryptedattempt;
}

function decrypt(somenumber)
{
   //need to take somenumber of to the power of 20,040,013
   
   var answer = new Array();
   answer[0] = somenumber;
   answer[1] = somenumber;
   answer[2] = somenumber;
   answer[3] = somenumber;
   answer[4] = somenumber;
   answer[5] = somenumber;
   answer[6] = somenumber;
   answer[7] = somenumber;
   answer[8] = somenumber;
   answer[9] = somenumber;
   answer[10] = somenumber;
   answer[11] = somenumber;
   
   var finalanswer=1;
   
   var exp = new Array();
   exp[0] = 24;
   exp[1] = 21;
   exp[2] = 20;
   exp[3] = 16;
   exp[4] = 15;
   exp[5] = 14;
   exp[6] = 11;
   exp[7] = 8;
   exp[8] = 6;
   exp[9] = 3
   exp[10] = 2;
   exp[11] = 0;
   
   for (var j=0; j<12; j++)
   {
   
      for (var i=0; i<exp[j]; i++)
      {
         answer[j] = (answer[j] * answer[j]) % 25060027 ;
      }
   
   finalanswer = (finalanswer*answer[j]) % 25060027;
   
   }
   
   return finalanswer;
}

function clearformguess() 
{
   if (!top.allframes_loaded()) {alert('Wait a moment and try again. Some files are still loading'); return;}
   
   for (var index=0; index<radioset.length; index++)
   {
      clearradioset(radioset[index]);
   }
}

function clearform() 
{
   if (!top.allframes_loaded()) {alert('Wait a moment and try again. Some files are still loading'); return;}
   
   for (var index=0; index<radioset.length; index++)
   {
      clearradioset(radioset[index]);
   }
   top.buttonsandcontentsframeset.buttonsframe.document.getElementById('output').value = ""
}

function clearradioset(anyradioset)
{
   for (var index=0; index < anyradioset.length; index++)
   {
      anyradioset[index].checked = false; //redundant as most already false but easy
   }  
}

var guessanimationid = null;
var guessanimationcount;

function wildguess() 

//The setInterval method runs in the background calling the given function every so many
//milliseconds forever until you stop it. The guess function keeps track of how many times
//it has been called and stops the repetition after a bit. The guess function also clears
//the form and puts in random guesses. This function resets the count whenever it is called.

{  
 //  alert("in wild guess")
   guessanimationcount = 0;                           //global to keep track in guess()
   guessanimationid = setInterval("guess()",200);  //global to turn off guessing
}  

function stopguessing()
{
   clearInterval(guessanimationid);
   guessanimationid = null;
   guessanimationcount = 0;
}

function guess() 
{  
   guessanimationcount++;                                          //keep track
   if (guessanimationcount>20) 
   {
      stopguessing();
   }
      
   clearformguess();                             
   
   for (var index=0; index<radioset.length; index++)
   {
      radioset[index][pickanumber()].checked = true; //put in random guesses
   }
} 
   

function pickanumber() 
{
   var number = (Math.floor(Math.random()*5));
   return number;
}  

function getsectionnumber()
{

//This one is particularly nasty. We want to find out what section number is
//currently loaded. We use the filename ASSUMING that the filename is 
//section#.extension where # is any number (could have more than one digit)
//and extension is any extension. However the "section" must literally be 
//section of this won't work. The computer looks at the whole long filename
//plus pathname of the currently loaded page, starts and the end and finds 
//the word "section." Then it adds 7 to the returned index to find the index
//where the # supposedly starts. Then it used the substring method to get a
//string that is #.extension. Now that the string starts with a number we can
//use the parseInt method to grab the number and dump the rest of the string.

   var number, filename, locationofsection, locationofnumber, stringbeginningwithnumber;
   
   filename = window.location.href;
   
   locationofsection = filename.lastIndexOf("section");
   
      if (locationofsection == -1) 
      {
         alert("this button won't work due to a filename problem");
         return -1;
      }
   
   locationofnumber = locationofsection + 7;
   
   stringbeginningwithnumber = filename.substring(locationofnumber)
   
   number = parseInt(stringbeginningwithnumber);
   
   return number;
}

