/* 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 */
questions = new Array();
questionnum = -1;
possibleanswers = new Array();
answers = new Array();
explanations = new Array();

comments = new Array();
scores = new Array();

function AddComment(minscore, comment) {
	var n = comments.length;
	comments[n] = comment;
	scores[n] = minscore;
}

function Question(question){
	questionnum++;
	questions[questionnum] = question;
	possibleanswers[questionnum] = new Array();
}

function Explanation(explanation) {
	if(explanations[questionnum]) {
	  explanations[questionnum] += explanation;
	} else {
	  explanations[questionnum] = explanation;
	}
}

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();

// Confirm box

confirmbox = "<HTML><HEAD><TITLE>Results</TITLE></HEAD><BODY BGCOLOR=\"#DFF9B0\"><FONT FACE=\"Arial, Verdana\">";
confirmbox += "<P>Your score was "+percent+"%.</P><P>";

for(var i=0; i<comments.length; i++) {
  if(percent >= scores[i]) {
  	confirmbox += comments[i];
  	break;
  }
}

confirmbox += "</P><P>Would you like to see explanations of the answers?</P><FORM ACTION=\"\" onSubmit=\"return false;\"><CENTER>";
confirmbox += "<INPUT TYPE=button VALUE=OK onClick=\"window.opener.explainAnswers();\"> <INPUT TYPE=button VALUE=\"Do it again\" onClick=\"self.close();\">";
confirmbox += "</CENTER></FORM></FONT></BODY></HTML>";
win = window.open("","win", "height=400,width=620,scrollbars=yes,resizable=yes");
win.document.write(confirmbox);

}

//if(confirm("Your score was "+percent+"%. Would you like to see the answers explained?")) {
function explainAnswers() {
  str = "<HTML><HEAD><TITLE>Results</TITLE></HEAD><BODY BGCOLOR=\"#DFF9B0\"><FONT FACE=\"Arial, Verdana\">";
  str += "<P>You got "+percent+"% correct.</P>";
  
  for(i=0;i<questions.length;i++) {
  if(results[i] == undefined) {
    results[i] = "(No answer was selected)";
  }
    str += "<IMG SRC=\"answer_"+(results[i] == answers[i])+".gif\" ALIGN=left>";
    str += "<B>Question #"+(i+1)+": "+questions[i]+"</B><BR>";
    str += "You answered <B>"+results[i]+"</B><BR>";
    str += "The correct answer is <B>"+answers[i]+"</B><BR>";
    if(explanations[i]) {
      str += "" + explanations[i] + "<BR>";
    }
    str += "<BR>";
  }

  str += "<FORM ACTION=\"\" onSubmit=\"return false;\"><CENTER><INPUT TYPE=button VALUE=OK onClick=\"self.close();\"></CENTER></FORM></FONT></BODY></HTML>";
  win.document.close();
  win.document.write(str);
}
