/*JS script that I got from J. Hillier for a multiple choice quiz series 
*that currently only works properly in N4, slightly off  in IE4 in that it
*writes to the main window and not the new one, but causes an
*access violation in N3, I am hoping someone will tell me why??
*/

questions = new Array();
questionnum = -1;
possibleanswers = new Array();
answers = new Array();

/* In order, these are the characters that will be used as labels for each possible
 * answer for a question, each separated by a |. These may include HTML tags to
 * apply different formatting for choice labels if desired.
 * The default configuration allows for 26 different possible answers for each question.
 */
labels = "a.|b.|c.|d.|e.|f.|g.|h.|i.|j.|k.|l.|m.|n.|o.|p.|q.|r.|s.|t.|u.|v.|w.|x.|y.|z.";

/* NO MODIFICATIONS BELOW HERE */

function Question(question){
questionnum++;
questions[questionnum] = question;
possibleanswers[questionnum] = new Array();
}

function CorrectChoice(choice){
WrongChoice(choice);
answers[questionnum] = choice;
}

function WrongChoice(choice){
var choicenumber = possibleanswers[questionnum].length;
possibleanswers[questionnum][choicenumber] = choice;
}

function Asplit(str,seperator){
var arraya = new Array();
var first = 0;

if(str.charAt(str.length) != seperator)
  {
  str += seperator;
  }

for(var i=0;i<str.length;i++)
  {
  if(str.charAt(i) == seperator)
    {
    second = i;
 	arraya[arraya.length] = str.substring(first,second);
	first = second;
	}
  }

for(var i=0;i<arraya.length;i++)
  {
  if(arraya[i].charAt(0) == seperator)
    {
    arraya[i] = arraya[i].substring(1,arraya[i].length);
	}
  }

return arraya;
}

function check(){
radios = new Array();
results = new Array();
correct = 0;
total = questions.length;
wrong = new Array();
var thisCorrect;

for(i=0;i<total;i++)
  {
  radios[i] = document.theform.elements["num"+(i+1)];
  thisCorrect = false;

  for(d=0;d<radios[i].length;d++)
    {
    if(radios[i][d].checked == 1)
      {
      results[i] = radios[i][d].value;
      if(results[i] == answers[i])
        {
        correct++;
        thisCorrect = true;
        }
      }
    }
  if(thisCorrect == false)
    {
    wrong[wrong.length] = i;
    }
  }

percent = Math.round(correct / total * 100).toString();
str = "";

if(percent == 100)
  {
  str += "Congradulations! You got a perfect score!";
  }
else
  {
  str += "You got "+percent+"% correct. ";

  if(confirm("You got some wrong.....You can either go back and try again,.... or click below to find out how you did"))
    {
    str += "The questions answered incorrectly were:<BR><BR>";

    for(i=0;i<wrong.length;i++)
      {
      str += "<B>Question "+(wrong[i]+1)+"</B>: The correct answer is <B>"+answers[wrong[i]]+"</B><BR>";
      }
    }
  }

str += "<FORM><INPUT TYPE=button VALUE=OK onClick=\"window.close();\"></FORM>";
win = window.open("","", "height=300,width=620,scrollbars=yes,resizable=yes");
win.document.write(str);
}