// fill in the question text and options

// fill in the question text
for(qcounter=1;qcounter<=number_of_questions;qcounter++){
	thisquestionstart = eval('q' + qcounter + 'start_of_sentence');
	thisquestionend = eval('q' + qcounter + 'end_of_sentence');
	thiscorrectanswer = eval('q' + qcounter + 'correct_answer');
	if(document.getElementById('q' + qcounter + 'a')) document.getElementById('q' + qcounter + 'a').innerHTML = thisquestionstart; // start of sentence
	if(document.getElementById('q' + qcounter + 'b')) document.getElementById('q' + qcounter + 'b').innerHTML = thisquestionend; // end of sentence
	
	// fill in the options
	if(multiplechoiceoptions[1]){ // if there is a single list of options that go in all drop-down boxes
		for(optioncounter=1;optioncounter<multiplechoiceoptions.length;optioncounter++){
			document.f1['q' + qcounter].options[optioncounter].text = multiplechoiceoptions[optioncounter];
			if(thiscorrectanswer == multiplechoiceoptions[optioncounter]){
				if(document.f1['q' + qcounter]) document.f1['q' + qcounter].options[optioncounter].value = 1;
			} // end if
		} // end for optioncounter
	} // end if there is a single list of options that go in all drop-down boxes
	else{ // populate the drop-down boxes one-by-one
		numberofoptions = 1; // reset
		for(onebyonecounter=1;onebyonecounter<=12;onebyonecounter++){ // goes through options
			thiswronganswertext = eval('q' + qcounter + 'wrong_answer' + onebyonecounter);
			if(thiswronganswertext){ // an option exists
				document.f1['q' + qcounter].options[onebyonecounter].text = thiswronganswertext;
				numberofoptions++;
			} // end if thiswronganswertext exists
			//else window.status = 'Q' + qcounter + ' has no options.';
			//window.status += 'Q' + qcounter + ' numberofoptions ' + numberofoptions ;
		} // end for onebyonecounter
		// generate random number
	
		thisRandomNum = Math.random();
		thisRandomMultiplied = thisRandomNum * numberofoptions;
		thisRandomInteger = Math.floor(thisRandomMultiplied);
		if(thisRandomInteger < 1) thisRandomInteger = 1; // make sure its not zero

		// put the random option in holding
		holding = document.f1['q' + qcounter].options[thisRandomInteger].text
	
		// replace it with the correct answer
		document.f1['q' + qcounter].options[thisRandomInteger].text = thiscorrectanswer;
		
		// set the value to 1 to show the answer is correct
		document.f1['q' + qcounter].options[thisRandomInteger].value = 1;		
	
		// add the holding option to the end, if available
		document.f1['q' + qcounter].options[numberofoptions].text = holding;
		
	} // end else
	
} // end qcounter

var feedback4wrongansStart = 'Sorry, <i>';
var feedback4wrongansMiddle = '</i> is not correct. ';
var feedback4wrongansEnd = ' Please try again.';

// populate array of feedback for each question with not done yet
var feedbacktext = new Array(21);
for(fbcounter=0;fbcounter<feedbacktext.length;fbcounter++){
	feedbacktext[fbcounter] = 'You have not answered this question yet.';
} // end for fbcounter


function processhoice(qnum,thisScore,thisText){
	thisTip = eval('q' + qnum + 'tip');
	if(scores[qnum] == 0) scores[qnum] = parseInt(thisScore); // only change if previously unanswered or wrong
	if(thisScore == 1){ // correct answer
		feedbacktext[qnum] = 'Correct, well done, <i>' + thisText + '</i> is the right answer.';
	} // end if
	else{ // wrong answer
		feedbacktext[qnum] = feedback4wrongansStart;
		feedbacktext[qnum] += thisText + feedback4wrongansMiddle;
		if(thisTip) feedbacktext[qnum] += ' Tip: ' + thisTip;
		feedbacktext[qnum] += feedback4wrongansEnd;
	} // end else
	showallanswers(); // updates it if feedback showing
	countscore();
} // end fn processhoice

var scores = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
var totalscore = 0;
function countscore(){
	totalscore = 0; // reset
	for(scorecounter=0;scorecounter<scores.length;scorecounter++){
		totalscore = totalscore + scores[scorecounter];
	} // end for scorecounter
	if(document.f1.scorebox) document.f1.scorebox.value = totalscore;
	if(document.f1.scorebox && number_of_questions) document.f1.scorebox.value += ' \/ ' + number_of_questions;
} // end fn countscore	

var allfeedbacklist = '';
function showallanswers(){
	allfeedbacklist = '';
	for(allanswercounter=1;allanswercounter<=number_of_questions;allanswercounter++){
		if(document.f1['q' + allanswercounter]) allfeedbacklist += '<br>(' + allanswercounter + ')&nbsp;' + feedbacktext[allanswercounter];
	} // end for allanswercounter
	if(answersshowing == 'yes') document.getElementById('fb').innerHTML = allfeedbacklist;
	countscore();
} // end fn showallanswers

var answersshowing = 'no';
function showallanswersbuttonclick(){
	window.scrollBy(0,200);
	answersshowing = 'yes';
	showallanswers();
} // end fn showallanswersbuttonclick

for(uddbxcounter=1;uddbxcounter<21;uddbxcounter++){ // hide unused drop-down boxes
	if(uddbxcounter > number_of_questions){ 
		if(document.getElementById('q' + uddbxcounter + 'dd')) document.getElementById('q' + uddbxcounter + 'dd').style.visibility = "hidden";
	} // end if
} // end for uddbxcounter

if(questionbackgroundcolour) document.getElementById('questionarea').style.background = questionbackgroundcolour;

document.f1.reset();