
/****************************************************************/
/* Function: ChangeScore()                                      */
/* Purpose : Update the screen with the completed test score    */
/* Input   : numRight - integer of number of questions correct  */
/*        	totalQuestions - total number of questions on test  */
/*                                                              */
/****************************************************************/
function ChangeScore(numRight, totalQuestions) {
   var scoreP = document.getElementById("instructions1");
   var status = 'passed'; 
   
   if ( (totalQuestions - numRight) > 3) {
		status = 'failed';
   }
   scoreP.firstChild.nodeValue='You got ' + numRight + ' questions correct out of ' + 
	(totalQuestions - 1) + ' - you have ' + status + ' this test.';
}  // ChangeScore()


// rey 
function dynamiccontentDOM(elementid,content){
	if (document.getElementById){
	rng = document.createRange();
	el = document.getElementById(elementid);
	rng.setStartBefore(el);
	htmlFrag = rng.createContextualFragment(content);

	while (el.hasChildNodes())
		el.removeChild(el.lastChild);
		el.appendChild(htmlFrag);
	}
}

/****************************************************************/
/* Function: ScoreTest()                                        */
/* Purpose : Master test scoring routine.  Adds up number of    */
/*   questions wrong and displays wrong choices & right choices */
/*   by manipulating the DOM for the page.                      */
/* Input   : numRight - integer of number of questions correct  */
/*        	totalQuestions - total number of questions on test  */
/*                                                              */
/****************************************************************/
function ScoreTest() {
	// set an array of the question names to use later
	for (var i = 1; i <= totalQuestions; i++) {
		questionNamesArray[i] = 'q' + i;
	}
	
	// break the answer string into an array
	for (var i = 0; i < rightAnswersStr.length; i++) {
		varName = questionNamesArray[i+1];
		rightAnswers[varName] = rightAnswersStr.charAt(i);
	}
	
	
	// get your answers and pop them into another array
	for (var i = 1; i < totalQuestions; i++) {
		var qFullName = "document.form1." + questionNamesArray[i];
		var qName = questionNamesArray[i];
		var numOptions = eval(qFullName + ".length");

		for (var radioValue=0; radioValue < numOptions; radioValue++) {
			if (eval(qFullName + '[' + radioValue + '].checked')) {
				formVarName = qFullName + '[' + radioValue + '].value';
				yourAnswers[qName] = eval(formVarName);
				
				// check answer
				// alert(qName);
				// alert('Right answer for this is ' + rightAnswers[qName] + 
					//' your answer was: ' + yourAnswers[qName]);
				if (yourAnswers[qName] == rightAnswers[qName]) {
					totalScore++;
				}
			}
		}
	
	}
	
	
	inputs=document.getElementsByTagName('p');
	for (i=0; i< inputs.length; i++) {
		// alert (inputs[i].className);
		if (inputs[i].className == 'option') {
			inputs[i].className = 'wrong';
		} else if (inputs[i].className == 'optionright') {
			inputs[i].className = 'right';
		} else if (inputs[i].className == 'referto') {
			inputs[i].className = 'referto2';
		}
	}

   var scoreP = document.getElementById("answers");
   scoreP.firstChild.nodeValue=' Answers';

	scoreP = document.getElementById("introduction1");
	scoreP.parentNode.removeChild(scoreP);
	
   // dynamiccontentDOM('introduction1', '<p>&nbsp;</p>');
   dynamiccontentDOM('instructions2', "<p>The correct answer for each question is " + 
   	"highlighted in <span class='right'>green</span>.  Incorrect answers are in <span class='wrong'>" + 
   	"red.</span>" + "The answer you chose is the highlighted radio button. " + 
   	"The relevant page in the <i>Driver\'s Manual</i> " + 
	"is noted below each question.</p>");

	
	ChangeScore(totalScore, totalQuestions);
		
} // ScoreTest()
