// Copyright Oxford University Press 2005
//SRBD
var startagainmessage ="You haven't answered the questions. Please select 'Start again'." 
var seeanswerscheck = 0
var extension= '/images/elt/adultlist/exercise_'
var blank='transparent.gif'
var tick='tick.gif'
var cross='cross.gif'
var linkidArray = new Array()
var errorcheckArray= new Array()
var triesArray = new Array()
var correctscore=0
var errortries=0
var founderrors=0
var clearword=0
var foundmessage="You have already found the message for this sentence!"
//loopthrough and make an array of variables to check against
//get errorArray then get answer code which is position in Array+theNumber
for (errorindex=0; errorindex<errorArray.length; errorindex++){
	errorcheckArray[errorindex]= (errorindex+ "_" +errorArray[errorindex])
	}
var questions = new Array (question0, question1, question2, question3, question4, question5)	
//write the exercise into the page

setUpPage();

function setUpPage(){
	
	var i=0
	var linkword=0	
	document.write("<form name ='form1' id='form1'>");
	document.write("<ol id='errorcorrection'>");
	//loop through arrays
	for (qwrite=0;qwrite<questions.length;qwrite++){
		document.write("<li>")
		//loopthrough each word and put it in a link
		for (i=0;i<questions[qwrite].length;i++){
		//put a link around with a call to the check function+unique id which should be ie question1_2
		//question1_2 can later be passed to the check function to equal the correct word or not
		linkidArray[i] = (qwrite +"_"+ i);
		linkword = questions[qwrite][i];
	document.write(" <a href='#' id='link_" + linkidArray[i] + "' onclick=checkthelink('" + linkidArray[i] + "','" + qwrite +"')>" + linkword + "</a> ");
		}
		
		document.write(" <img src='" + extension + blank +"' name='mark" + qwrite +"' width='18' height='20' \/>")
		//end the list item then put a text box on it
		//the text box needs a name - questions[qwrite] should be question1, 2, etc.
		document.write("<br/> <input type='text' name='question" + qwrite + "' id='question" + qwrite + "' /> <img src='" + extension + blank +"' name='correctionmark" + qwrite +"' width='18' height='20' \/> </li>");
	}
	document.write("</ol>");
	document.write(" <fieldset><legend>How many mistakes have I found? </legend> <label>Mistakes found: <input type='text' name='errorsfound' value='0' class='scorebox'> </label> <label>Attempts: <input type='text' name='attempts' id='attempts' value='0' class='scorebox'/> </label> </fieldset> &nbsp; <fieldset><legend>How many mistakes have I corrected?</legend><input onclick='checkthetext()' type='button' value='Check' name='button' /> <input type='text' name='errorscorrected' value='0' class='scorebox'> </fieldset>")
	
	document.write("<div class='exercise_buttons'> <input type='button' value='See answers' onclick='seeanswers()' name='button2'> <input type='reset' onclick='clearit()' name='reset' value='Start again'></div></form>");
	
}


function checkthelink(identity,qnumber){
	//identity is the linkspecific id
	//qnumber is the same for all links of a question - and should be the same as the name of the text box
	
	
    if (triesArray[qnumber]>0){
	alert(foundmessage)
	} 
	
	else {
	errortries++;
	document.form1.attempts.value=errortries ;
	
	for (scanindex=0; scanindex<errorcheckArray.length; scanindex++)	

	if (identity==errorcheckArray[scanindex]){
	var changeword = "link_" + identity
	document.getElementById(changeword).style.backgroundColor="#ff3399"
	document.getElementById(changeword).style.color="#fff"
	var markit= eval("document.form1.mark" + qnumber)
	markit.src= extension + tick;
		
	//gets the textbox
	var thisbox= eval("document.form1.question"+ qnumber)
	//gets the question number
	var thetext= eval("question"+ qnumber)
		
	//gets the index of the error within the array
	var theword= errorArray[qnumber]
		
	//gets the wrong word		
	var thistext= thetext[theword]
	thisbox.value=thistext
		
	// set the value of the question to more than 0 to stop repeat attempts
	triesArray[qnumber] =1
		
	//increase the number of found errors
	founderrors++;
		
	document.form1.errorsfound.value=founderrors;
		
	//break from the loop if the correct word has been clicked
	break;
		}
		
	if (identity!=errorcheckArray[scanindex]){
		var markit= eval("document.form1.mark"+qnumber)
		markit.src=extension + cross;
		
	}
	
	}

}
	

function checkthetext(){
	// get the value of the text boxes
	
	if (seeanswerscheck>0){
		alert(startagainmessage)
		} else{
	for (checkindex=0; checkindex<correctionArray.length; checkindex++){
		
		var checkthisbox= eval("document.form1.question"+ checkindex)
		var correctionmarkcheck= eval("document.form1.correctionmark" + checkindex)
		
		//is the value of the box the same as the equivalent word in the correctionArry?
		
		if (checkthisbox.value==correctionArray[checkindex]){
			correctscore++
			correctionmarkcheck.src=extension+tick
			
			var correctboxid="question" + checkindex
		document.getElementById(correctboxid).style.backgroundColor=""
		}
		else {
			
			correctionmarkcheck.src=extension+cross
			var boxid="question" + checkindex
		document.getElementById(boxid).style.backgroundColor="#ff3399"
			}
		
		}
		document.form1.errorscorrected.value=correctscore;
	correctscore=0
}
}

//this clears the 
function clearit(){
	for (clearindex=0; clearindex<errorArray.length; clearindex++){
		
		//var clearthisbox= eval("document.form1.question"+ clearindex)
		//clearthisbox.value="";
		
		//this clears the graphics for the marks
		var clearmarks=eval("document.form1.mark"+ clearindex)
		clearmarks.src=extension + blank
		
		var clearcorrectionmarkcheck= eval("document.form1.correctionmark" + clearindex)
		clearcorrectionmarkcheck.src=extension + blank
		
		var clearword = "link_" + clearindex + "_" + errorArray[clearindex]
		document.getElementById(clearword).style.backgroundColor=""
		document.getElementById(clearword).style.color=""
		
		var clearboxid="question" + clearindex
		document.getElementById(clearboxid).style.backgroundColor=""
		
		triesArray[clearindex]="0"
		
		
		}
		//clear the scores
		errortries=0
		founderrors=0
		correctscore=0
		seeanswerscheck=0
	}



function seeanswers(){
	clearit();
	for (seeindex=0; seeindex<correctionArray.length; seeindex++){
		
		var seethisbox= eval("document.form1.question" + seeindex)
		seethisbox.value=correctionArray[seeindex];
		
		var boxstyle="question" + seeindex
		document.getElementById(boxstyle).style.backgroundColor=""
		
		var thisword = "link_" + seeindex + "_" + errorArray[seeindex]
		document.getElementById(thisword).style.backgroundColor="#ff3399"
		document.getElementById(thisword).style.color="#fff"
		}
		
	seeanswerscheck=1
	}

