
dotOverNow = "";
dotOverPrevious = "";
lastDotClicked = "";
clicked = 0;

//for IE pre 5.5 versions, define push method for arrays.
function array_push(element) {
	this[this.length]=element;
}
Array.prototype.push=array_push;

// Randomize the order of the questions, if flag in self-test file says to do so.
if (randomizeQuestions==true) {
	for (j=0; j<Q.length; j++) {
		tempQArray = new Array(); //holds questions while they're being rearranged.
		tempRightArray = new Array(); //holds pointer to right answers while they're being rearranged.
		tempAnswerArray = new Array(); //holds answers while they're being rearranged.	
		tempReasonArray = new Array(); //holds reasons while they're being rearranged.	
		do {
			randomItem=Math.floor(Math.random()*Q.length);  //choose random question from original question array.
			tempQArray.push(Q[randomItem]);  //store chosen question in temp array.
			tempRightArray.push(Right[randomItem]);  //store pointer to right answer in temp array.
			tempAnswerArray.push(A[randomItem]);  //store answers and reasons in temp arrays.
			tempReasonArray.push(R[randomItem]);

			//next block removes questions, right answer identifiers, answers, and reasons from original arrays; could be more easily done with splice method 
			for (m=randomItem; m<Q.length; m++) {
				Q[m]=Q[m+1]; 
				Right[m]=Right[m+1]; 
				A[m]=A[m+1]; 
				R[m]=R[m+1]; 
			}
			Q.length--;
			Right.length--;
			A.length--;
			R.length--;
//			Q.splice(randomItem,1); //remove chosen question from original array.
//			Right.splice(randomItem,1); //remove pointer to right answer from original array.
//			A.splice(randomItem,1); //remove answers and reasons from original arrays.
//			R.splice(randomItem,1);

		} while (Q.length > 0);
		Q = tempQArray; //copy temp quesions into original arrays.
		Right = tempRightArray; //copy pointers to right answers into original arrays.
		A = tempAnswerArray; //copy temp answers and reasons into original arrays.
		R = tempReasonArray;
	}	
}

// Randomize the order of the answers within each question
	for (j=0; j<Q.length; j++) {
		tempAnswerArray = new Array();  //holds answers while they're being rearranged.
		tempReasonArray = new Array();  //holds reasons while they're being rearranged.
		rightAnswer=A[j][Right[j]] //remember which answer is right so we can update Right pointer below.
		do {
			randomItem=Math.floor(Math.random()*A[j].length);  //choose random answer from original answer array.
			tempAnswerArray.push(A[j][randomItem]);  //store random answer and corresponding reason in temp arrays.
			tempReasonArray.push(R[j][randomItem]);
			if (A[j][randomItem] == rightAnswer) {
				Right[j] = (tempAnswerArray.length)-1;  //if this was the right answer, update Right array to point to its new position
			}

			//next block removes chosen answer and corresponding reason from original arrays; could be more easily done with splice method 
			for (m=randomItem; m<A[j].length; m++) {
				A[j][m]=A[j][m+1]; 
				R[j][m]=R[j][m+1]; 
			}
			A[j].length--;
			R[j].length--;
//			A[j].splice(randomItem,1); 
//			R[j].splice(randomItem,1);

		} while (A[j].length > 0);
		A[j] = tempAnswerArray; //copy temp answers and reasons into original arrays.
		R[j] = tempReasonArray;
	}	

function rollover_image(dotName, width, height) {
        img_prefix = dotName;
        this.rolloff_img = new Image (width, height);
        this.rolloff_img.src = "../../images/blueDot.gif";
        this.rollover_img = new Image (width, height);
        this.rollover_img.src = "../../images/yellowDot.gif";
}

function make_rollover_image(dotName, width, height) {
        rollover[dotName] = new rollover_image (dotName, width, height);
}

function rollover(dotName) {
        dotOverNow = dotName;
        if (dotOverNow != dotOverPrevious) {
                question.document[dotOverNow].src = rollover[dotOverNow].rollover_img.src;
                if (dotOverPrevious != "") {
                        question.document.images[dotOverPrevious].src = rollover[dotOverPrevious].rolloff_img.src;
                }
                if ((lastDotClicked != "") && (lastDotClicked != dotOverNow)) {
                        question.document.images[lastDotClicked].src = rollover[lastDotClicked].rolloff_img.src;
                }
                dotOverPrevious = dotOverNow;
        }
}

function doClick(dotName){
        lastDotClicked = dotName;
		questionClicked = dotName.slice(3,dotName.indexOf("_"));
		answerClicked = dotName.charAt(dotName.indexOf("_")+1);
		feedback = "<font face = \"verdana,arial,sans-serif\">";
		if (Right[questionClicked] == answerClicked) {
			feedback += "<IMG SRC='../../images/right.gif' align='top'> ";
		} else {
			feedback += "<IMG SRC='../../images/wrong.gif' align='top'> ";
		}
		feedback += R[questionClicked][answerClicked];
		reason.document.write(feedback);
		reason.document.close()
}

function rolloff() {
        if (lastDotClicked != ""){
                question.document.images[lastDotClicked].src = rollover[lastDotClicked].rollover_img.src;
        }
        if ((dotOverNow != "") && (dotOverNow != lastDotClicked)) {
                question.document.images[dotOverNow].src = rollover[dotOverNow].rolloff_img.src;
        }
        dotOverNow = "";
        dotOverPrevious = "";
}

for (j = 0; j < Q.length; j++) {
	for (i = 0; i < A[j].length; i++) {
		make_rollover_image ("dot"+j+"_"+i, 14,14);
	}
}

questionText="<html><head><title>Self-Test Questions</title></head><body>";
questionText+="<b>Self-Test Questions: "+PageTitle+"</b><br />";
questionText+="<hr>";

for (j=0; j<Q.length; j++) {
	questionText+="<table width='100%'><tbody>";
	questionText+="<tr>";
	questionText+="<td vAlign=top align=middle width='5%'>";
	questionText+="<table cellPadding=5 border=0>";
	questionText+="<tbody>";
	questionText+="<tr>";
	questionText+="<td bgColor=#cccccc><b>"+(j+1)+"</b> </td></tr></tbody></table></td>";
	questionText+="<td> "+Q[j]+"<br><br>";

	questionText+="<table border=0>";
	questionText+="<tbody>";

	//present each answer
	for (i=0; i<A[j].length; i++) {
		questionText+="<TR>";
		questionText+="<TD vAlign=top> ";
		questionText+="<A href=\"javascript:void(0)\" onmouseover=\"parent.rollover('dot"+j+"_"+i+"'); return true\" onclick=\"parent.doClick('dot"+j+"_"+i+"'); return true;\" onmouseout=parent.rolloff()>";
		questionText+="<IMG alt='' src='../../images/blueDot.gif' border=0 name=dot"+j+"_"+i+"></A> </TD>";
		questionText+="<TD>"+A[j][i]+"</TD>";
		questionText+="</TR>";
	}

	questionText+="</tbody></table>";
	questionText+="</td></tr></tbody></table>";
	questionText+="<hr>";
}
questionText+="</body></html>";
