// this script is copyright PL Collett 2000

var zyq = false;  // debug flag

var qId=1;
var arrAnsId = new Array(100);
var choices = new Array(8);
choices[0] = "A";
choices[1] = "B";
choices[2] = "C";
choices[3] = "D";
var nlChar = String.fromCharCode(10);
var score=0;   // cumulative total


var numSchemes=0;

mScheme = new Array(50);


// various browser detection stuff
// Note: On IE5, these return 4, so use is_ie5up to detect IE5.
var agt=navigator.userAgent.toLowerCase();
var is_major = parseInt(navigator.appVersion);
var is_minor = parseFloat(navigator.appVersion);
var is_nav  = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1)
            && (agt.indexOf('compatible') == -1) );
var is_ie   = (agt.indexOf("msie") != -1);
var is_nav5 = (is_nav && (is_major == 5));
var is_ie5  = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.0")!=-1) );




function markingScheme(lower, upper, comment){
 this.comment = comment;
 this.lower = lower;
 this.upper = upper;
}

function setCommentOnScores(lower, upper, comment){
  mScheme[numSchemes++] = new markingScheme(lower, upper, comment);
  }  // end setCommentOnScores



function usrAnswer(rdo){
  /*
   * Description: This function displays the clicked radio
   *              button on the window status line.
   * Arguments:
   *   rdo - the button which was pressed.
   */
  if (rdo.checked)
    window.status = rdo.name + " you selected " + rdo.value;
  }  // end rdoEvent



function addRadioButton(nam, qtxt, valId)
{
document.write("<INPUT TYPE='radio' NAME='"+nam+"' VALUE='");
document.write(valId);
document.write("' onClick='usrAnswer(this)'>");
document.write(qtxt+"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
  }  // end addRadioButton



function addQuestion(qtxt, ansId, opt1, opt2, opt3, opt4)
{
var rdButNam;

arrAnsId[qId] = ansId;

document.write("<HR>"+qId+".&nbsp;&nbsp;"+qtxt+"<BR>");

rdButNam = "q" + qId;
addRadioButton(rdButNam,opt1,choices[0]);
addRadioButton(rdButNam,opt2,choices[1]);
addRadioButton(rdButNam,opt3,choices[2]);
addRadioButton(rdButNam,opt4,choices[3]);
document.write("<BR>");

qId++;
  }  // end addQuestion




function preAmble()
{
document.write("<table width='100%' cellpadding=5 cellspacing-5><tr><td>");
document.write("<font size='-1'>");
document.write("Choose the best options then check your answers using the <a href='#mark'>button</a> at the end");
document.write("</font>");
document.write("<FORM NAME='quiz' id='quiz'>");
  }  // end preamble




function checkAnswers()
{
var zxz=0;
var foundIt=false;
var comment = "no comment";

	  // window.alert ("Called CheckAnswers()");
score=0;
	  // window.alert ("about to call getscore()");
	 if (document.forms[0]==null) {
	  window.alert ("BAD things are happening here - scarper now!!!");
	 } else {
	  // window.alert ("about to call getscore()+quiz.q1[2] is not checked");
	 }
if (zyq == false) getScore();  // call the synthesised function (put in earlier by writeMarkingFunc())


//if (score==1) quiz.marks.value = " Huh! Given a monkey a typewriter lately?";
//if (score==0) quiz.marks.value = " Try another subject!";

	 // window.alert ("About to find the right comment");
while ((zxz<numSchemes) && (foundIt == false)) {
   if ((score >= mScheme[zxz].lower) && (score <= mScheme[zxz].upper)) {
      foundIt = true;
	 // window.alert ("found the right comment");
      // quiz.marks.value = score + "/" + (qId-1) + " correct, " + mScheme[zxz].comment;
      comment = score + "/" + (qId-1) + " correct, " + mScheme[zxz].comment;
      getAnswerArea().appendChild(makeP(comment));
   }
   zxz++;  
}

  }  // end checkAnswers

function makeP (text) {
	var pElem, textNode;
	pElem = document.createElement("p"); // lowercase for XHTML
	textNode = document.createTextNode(text);
	pElem.appendChild (textNode);
	return pElem;
}

function getAnswerArea() {
	// Workaround for bug 38763:
	if ( !is_nav5 && document.getElementById ) { // if HTML document
		return document.getElementById("rsltarea");
	} else {
	var rv = null;
						
	var divlist = document.getElementsByTagName("div");
	 // window.alert ("Number of div items=" + divlist.length);
	for (var i = 0; i < divlist.length; i++) {
		// window.alert ("At item number " + i);
		// window.alert( divlist.item(i) );
		if ( divlist.item(i).getAttribute("id") == "rsltarea" ) {
			rv = divlist.item(i);
		}
	}
	// dump(rv + "\n"); // gives [object HTMLDivElement] in XHTML
	return rv;
	}
}

function postAmble()
{
document.write("<hr><a name='mark'></a>");
writeMarkingFunc();
document.write("<INPUT TYPE='button' NAME='pbList' VALUE='please check answers now' onClick='checkAnswers()'>");
document.write("<br>");
// document.write("<INPUT TYPE='text' NAME='marks' VALUE='None' size=60></FORM>");
document.write("</FORM>");
document.write("<div id='rsltarea' name='rsltarea'>Score: </div>");
document.write("</td></tr></table>");
}  // end postAmble


function getScore2()
{
if (quiz.q1[arrAnsId[1]].checked) score++;
if (quiz.q2[arrAnsId[2]].checked) score++;
} 



function writeMarkingFunc()
{
var zvz=1;
if (zyq == true) {
    document.write("<PRE>");
} else {
    document.write("<script language='JavaScript'>"+nlChar);
}

document.write("function getScore() {"+nlChar);
document.write("var zvz = 1;"+nlChar);

for (zvz=1;zvz<qId;zvz++) {
  // document.write("if (quiz.q"+zvz+"[arrAnsId[zvz]].checked) score++;"+nlChar);
  document.write("if (document.forms[0].q"+zvz+"[arrAnsId[zvz]].checked) score++;"+nlChar);
  document.write("zvz++;"+nlChar);
}

//if (quiz.q1[arrAnsId[zvz++]].checked) score++;
//if (quiz.q2[arrAnsId[zvz++]].checked) score++;

document.write(nlChar+"}"+nlChar);

   if (zyq == true) {
    document.write("</PRE>"+nlChar);
   } else {
    document.write("</script>"+nlChar);
   }

}  // end writeMarkingFunc



