
function setUpPage(){
	randomorder = randomize(questionArray.length);
	AnswerKey = new Array("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");
	Answerorder = new Array(questionArray.length);
	for(suindex = 0;suindex<questionArray.length;suindex++){
		document.write("<span class='sentencematching'>"  + (suindex+1) + ".</span>");
		document.write("<input size=1 MAXLENGTH=1 name=ans" + (suindex+1) + ">");
		document.write("<span><img src='bits/transparent.gif' name='im" + (suindex +1) + "'></span>");
		document.write("<span class='exercise_text'>" + questionArray[suindex][0] + "</span>");
		document.write("<span class='upper_alpha'>" + AnswerKey[suindex] + "</span><span class='exercise_text'>"  + questionArray[randomorder[suindex]][1] + 
"</span></span><br>"); Answerorder[randomorder[suindex]]=suindex; 
            		
	}
	document.write("<div class='exercise_buttons'> <input onClick=checkScore() type=button value='Score' name='button'> <input type=button value='See answers' onClick='reveal()' name='button2'> <input type='reset' onClick='again()' name='reset' value='Start again'></div>");
	answerArray = new Array(document.form1.ans1,document.form1.ans2,document.form1.ans3,document.form1.ans4,document.form1.ans5,document.form1.ans6,document.form1.ans7,document.form1.ans8);
	imageArray = new Array(document.images.im1,document.images.im2,document.images.im3,document.images.im4,document.images.im5,document.images.im6,document.images.im7,document.images.im8);
}

//answerArray = new Array(document.form1.ans1,document.form1.ans2,document.form1.ans3,document.form1.ans4,document.form1.ans5,document.form1.ans6,document.form1.ans7,document.form1.ans8);

function checkScore() {
answerArray = new Array(document.form1.ans1,document.form1.ans2,document.form1.ans3,document.form1.ans4,document.form1.ans5,document.form1.ans6,document.form1.ans7,document.form1.ans8);
var imageArray = new Array(document.images.im1,document.images.im2,document.images.im3,document.images.im4,document.images.im5,document.images.im6,document.images.im7,document.images.im8);
	if(cheat){
		alert("Please press Start again to have another go.")
	}else{
		var score = 0;
		for(csindex = 0;csindex<questionArray.length;csindex++){
			//alert(answerArray[csindex].value + " : " + AnswerKey[Answerorder[csindex]] + " : " + Answerorder[csindex]) ;
			if (answerArray[csindex].value.toUpperCase() == AnswerKey[Answerorder[csindex]]) {
				imageArray[csindex].src="bits/tick.gif";
				score++;
			} else {
				imageArray[csindex].src="bits/cross.gif";
			}
		}
		alert("Your score is "+score+" / " + questionArray.length);
	}
}




function reveal() {
var answerArray = new Array(document.form1.ans1,document.form1.ans2,document.form1.ans3,document.form1.ans4,document.form1.ans5,document.form1.ans6,document.form1.ans7,document.form1.ans8);
var imageArray = new Array(document.images.im1,document.images.im2,document.images.im3,document.images.im4,document.images.im5,document.images.im6,document.images.im7,document.images.im8);
	for(revindex = 0;revindex<questionArray.length;revindex++){
		answerArray[revindex].value = AnswerKey[Answerorder[revindex]];
		imageArray[revindex].src="bits/transparent.gif";
	}
	cheat = true;
}
function again() {
var answerArray = new Array(document.form1.ans1,document.form1.ans2,document.form1.ans3,document.form1.ans4,document.form1.ans5,document.form1.ans6,document.form1.ans7,document.form1.ans8);
var imageArray = new Array(document.images.im1,document.images.im2,document.images.im3,document.images.im4,document.images.im5,document.images.im6,document.images.im7,document.images.im8);
	cheat = false;
	for(agindex = 0;agindex<questionArray.length;agindex++){
		answerArray[agindex].value = "";
		imageArray[agindex].src="bits/transparent.gif";
	}

}
function randomize(Alength){
	var tempA = new Array(Alength);
	var outArray = new Array(Alength);
	for(i=0;i<Alength;i++){
		tempA[i] = i;	
	}
	for(i=0;i<Alength;i++){
		randindex = Math.floor((Math.random()*tempA.length));
		outArray[i] = tempA[randindex];
		tempB = tempA.slice(0,randindex);
		tempC = tempA.slice(randindex+1,tempA.length)
		tempA = tempB.concat(tempC);
	}
	return outArray;
	
}
setUpPage();
