

//<![CDATA[

<!--

// BrowserCheck Object


function BrowserCheck() {
	var b = navigator.appName;
	this.mac = (navigator.appVersion.indexOf('Mac') != -1);
	if (b=="Netscape") this.b = 'ns';
	else if (b=="Microsoft Internet Explorer") this.b = 'ie';
	else this.b = b;
	this.ns = (this.b == 'ns');
	this.ie = (this.b == 'ie');
	this.version = navigator.appVersion;
	if (this.b == 'ie'){
		var x = this.version.indexOf('MSIE ') + 5;
		var vnum = '';

		while ((this.version.charAt(x) > -1)&&(this.version.charAt(x) < 10)){
			vnum += this.version.charAt(x);
			x++;
		}
		this.v = parseInt(vnum);
	}
	else{
		this.v = parseInt(this.version);
	}
	if (this.ns&&(this.v == 5)){
		this.v = 6;
	}
	this.min = (this.v > 5);
}

is = new BrowserCheck();

if ((is.min == false)||(is.ie5mac)){
	alert('Your browser can\'t handle this page. You need NS6+ or IE6+ on Windows, or NS6+ on Mac.');
}


function PageDim(){
//Get the page width and height
	this.W = 600;
	this.H = 400;
	if (is.ns) this.W = window.innerWidth;
	if (is.ie) this.W = document.body.clientWidth;
	if (is.ns) this.H = window.innerHeight;
	if (is.ie) this.H = document.body.clientHeight;
}

var pg = null;

function SuppressBackspace(e){ 
	 if (is.ns) {
		thisKey = e.keyCode;
	}
	else {
		thisKey = window.event.keyCode;
	}

	var Suppress = false;

	if (thisKey == 8) {
		Suppress = true;
	}

	if (Suppress == true){
		if (is.ie){
			window.event.returnValue = false;	
			window.event.cancelBubble = true;
		}
		else{
			e.preventDefault();
		}
	}
}

var NavLightColor = '#7f7fb2';
var NavShadeColor = '#000033';
var NavBarColor = '#000066';
var FuncLightColor = '#ffffff';
var FuncShadeColor = '#7f7f7f';
var ExBGColor = '#ffffff';

function NavBtnOver(Btn){
	Btn.style.padding = '1px 3px 3px 1px';
	Btn.style.borderLeftColor = NavLightColor;
	Btn.style.borderTopColor = NavLightColor;
	Btn.style.borderRightColor = NavShadeColor;
	Btn.style.borderBottomColor = NavShadeColor;
}

function NavBtnOut(Btn){
	Btn.style.borderColor = NavBarColor;
	Btn.style.padding = '2px 2px 2px 2px';
}

function NavBtnDown(Btn){
	Btn.style.borderLeftColor = NavShadeColor;
	Btn.style.borderTopColor = NavShadeColor;
	Btn.style.borderRightColor = NavLightColor;
	Btn.style.borderBottomColor = NavLightColor;
	Btn.style.padding = '3px 1px 1px 3px';
}

function FuncBtnOver(Btn){
	Btn.style.padding = '2px 7px 4px 5px';
	Btn.style.borderLeftColor = FuncLightColor;
	Btn.style.borderTopColor = FuncLightColor;
	Btn.style.borderRightColor = FuncShadeColor;
	Btn.style.borderBottomColor = FuncShadeColor;
}

function FuncBtnOut(Btn){
	Btn.style.borderColor = '#ffffff';
	Btn.style.padding = '3px 6px 3px 6px';
}

function FuncBtnDown(Btn){
	Btn.style.borderLeftColor = FuncShadeColor;
	Btn.style.borderTopColor = FuncShadeColor;


	Btn.style.borderRightColor = FuncLightColor;
	Btn.style.borderBottomColor = FuncLightColor;
	Btn.style.padding = '4px 5px 2px 7px';
}

var topZ = 100;

var CorrectIndicator = ':-)';
var IncorrectIndicator = 'X';
var YourScoreIs = 'Your score is ';
var CorrectResponse = 'Correct! Well done.';
var IncorrectResponse = 'Sorry! Try again. Incorrect matches have been removed.';
var TotalUnfixedLeftItems = 0;
var TotCorrectChoices = 0;
var Penalties = 0;
var DefaultResponse = '[strDefaultResponse]';
var ReadingURL = '[ReadingURL]';
var TempReading = '';
var StartTime = (new Date()).toLocaleString();
var HPNStartTime = (new Date()).getTime();
var Finished = false;
var SubmissionTimeout = 30000;
var Score = 0;



var TableOpener = '<form name="QForm">';
TableOpener += '<table border="0">';

var IRow = '<tr><td valign="middle"><span class="LeftItem">';
IRow += '[strLeftItem]</span>&nbsp;&nbsp;&nbsp;&nbsp;</td>';
IRow += '<td valign="middle" ><span class="RightItem">';
IRow += '[strRightItem]</span></td>';
IRow += '<td valign="middle">&nbsp;&nbsp;&nbsp;<span class="CorrectMark">[Mark]</span>';
IRow += '</td></tr>';

TableCloser = '</table></form>';

var DropDownList = '';
var Exercise = '';

function Shuffle(InArray){
	Temp = new Array();
	var Len = InArray.length;

	var j = Len;

	for (var i=0; i<Len; i++){
		Temp[i] = InArray[i];
	}

	for (i=0; i<Len; i++){
		Num = Math.floor(j  *  Math.random());
		InArray[i] = Temp[Num];

		for (var k=Num; k < j; k++) {
			Temp[k] = Temp[k+1];
		}
		j--;
	}
	return InArray;
}


function WriteFeedback(Feedback){
	var Output = Feedback + '<br /><br />';
	document.getElementById('FeedbackContent').innerHTML = Output;
	var FDiv = document.getElementById('FeedbackDiv');
//Calculate and set its left setting
	topZ++;
	FDiv.style.zIndex = topZ;

//Position the feedback div
	pg = new PageDim();
	FDiv.style.left = Math.floor(pg.W/3) + 'px';
	FDiv.style.width = Math.floor(pg.W/3) + 'px';
//Set the top again in case the window has been resized
	if (is.ns){
		FDiv.style.top = Math.floor(pg.H/3) + window.pageYOffset + 'px';
	}
	else{
		FDiv.style.top = Math.floor(pg.H/3) + document.body.scrollTop + 'px';
	}

//Special for IE bug -- hide all the form elements that will show through the popup
	if (is.ie){
		for (var i=0; i<document.all.length; i++){
			if (document.all[i].id.substring(0,3) == 'sel'){
				document.all[i].style.display = 'none';
			}
		}
	}
	FDiv.style.display = 'block';
	document.getElementById('FeedbackOKButton').focus();


	RefreshImages();

}

function HideFeedback(){
	document.getElementById('FeedbackContent').innerHTML = '';
	document.getElementById('FeedbackDiv').style.display='none';

//Special for IE bug -- redisplay all the form elements hidden in WriteFeedback()
	if (is.ie){
		for (var i=0; i<document.all.length; i++){
			if (document.all[i].id.substring(0,3) == 'sel'){

				document.all[i].style.display = 'inline';
			}
		}
	}
	FocusAButton();
	if (Finished == true){
		Finish();
	}
}

function FocusAButton(){
	if (document.getElementById('CheckButton1') != null){
		document.getElementById('CheckButton1').focus();
	}
	else{
		if (document.getElementById('CheckButton2') != null){
			document.getElementById('CheckButton2').focus();
		}
		else{
			document.getElementsByTagName('button')[0].focus();
		}
	}
}

function WriteToInstructions(Feedback) {
	Feedback = '<span class="FeedbackText">' + Feedback + '</span>';
	document.getElementById('InstructionsDiv').innerHTML = Feedback;

	RefreshImages();

}

function BuildSelector(){
	DropDownList = '<select id="sel[INum]" name="sel[INum]">';
	DropDownList += '<option value="Match It - Fruit and Veg ">Match It - Fruit and Veg </option>';
	for (var i=0; i<RItems.length; i++){
		DropDownList += '<option value="' + EscapeDoubleQuotes(RItems[i][0]) + '">' + RItems[i][0] + '</option>';
	}
	DropDownList += '</select>';
}



Imgs = new Array();

function PreloadImages(){
	var a = PreloadImages.arguments;
	for (var i=0; i<a.length; i++){
		Imgs[i] = new Image();
		Imgs[i].src = a[i];
	}
}

function RefreshImages(){
	for (var i=0; i<document.images.length; i++){
		if (document.images[i].name.substring(0,6) != 'NavBar'){
			document.images[i].src = document.images[i].src;
		}
	}
}


function StartUp(){
//IE bug avoidance -- remove bottom navbar
	if (is.ie){
		if (document.getElementById('BottomNavBar') != null){
			document.getElementById('TheBody').removeChild(document.getElementById('BottomNavBar'));
		}
	}

	if (is.ie){
		document.attachEvent('onkeydown',SuppressBackspace);
		window.attachEvent('onkeydown',SuppressBackspace);
	}
	else if (is.ns){
		window.addEventListener('keypress',SuppressBackspace,false);
	}





	PreloadImages('../../IMAGES/Vocab/Food/Fruit/apple.gif','../../IMAGES/Vocab/Food/Fruit/banana.gif','../../IMAGES/Vocab/Food/Vegetables/aubergine.gif','../../IMAGES/Vocab/Food/Vegetables/redpepper.gif','../../IMAGES/Vocab/Food/Fruit/pineapple.gif','../../IMAGES/Vocab/Food/Fruit/pear.gif','../../IMAGES/Vocab/Food/Vegetables/corn.gif','../../IMAGES/Vocab/Food/Vegetables/mushroom.gif','../../IMAGES/Vocab/Food/Fruit/strawberries.gif','../../IMAGES/Vocab/Food/Fruit/grapes.gif');


//Create the right-item/distractor array, and shuffle it
	var DuplicateItem = false;
	for (var i=0; i<I.length; i++){
//If it's not fixed
		if (I[i][2] < 1){
			if (I[i][0].length > 0){
				TotalUnfixedLeftItems++;
			}
			DuplicateItem = false;
//and it's not already in the array
			for (var j=0; j<RItems.length; j++){
				if (I[i][1] == RItems[j][0]){
					DuplicateItem = true;
					RItems[j][1][RItems[j][1].length] = i;
				}
			}
//add it to the array
			if (DuplicateItem == false){
				RItems[RItems.length] = new Array();
				RItems[RItems.length-1][0] = I[i][1];
				RItems[RItems.length-1][1] = new Array();
				RItems[RItems.length-1][1][0] = i;
			}
		}
	}
	RItems = Shuffle(RItems);

//Now tell the I array items which of the selector items matches to it
	for (i=0; i<RItems.length; i++){
		for (j=0; j<RItems[i][1].length; j++){
			I[RItems[i][1][j]][3] = i;
		}
	}

//Now use the RItems array to build the drop-down list selector
	BuildSelector();	

//Create arrays
	CreateStatusArrays();



//Build and show the exercise
	BuildExercise();
	DisplayExercise(Exercise);
}


function BuildExercise(){
	var TempRow = '';
	Exercise = '';
	for (var i=0; i<I.length; i++){
//if there's a left item -- ie it's not a distractor
		if (I[i][0].length > 0){
			TempRow = IRow;
			TempRow = ReplaceStuff('[strLeftItem]', I[i][0], TempRow);
//if not fixed and not yet answered correctly
			if ((I[i][2] < 1)&&(Status[i][0] < 1)){
				TempRow = ReplaceStuff('[strRightItem]', DropDownList, TempRow);
			}
			else{
				TempRow = ReplaceStuff('[strRightItem]', I[i][1], TempRow);
			}
//if it's not fixed, and it's been attempted, mark it appropriately
			if ((I[i][2] < 1)&&(Status[i][1] > 0)){
				if (Status[i][0] < 1){
					TempRow = ReplaceStuff('[Mark]', IncorrectIndicator, TempRow);
				}
				else{
					TempRow = ReplaceStuff('[Mark]', CorrectIndicator, TempRow);
				}
			}
			else{
				TempRow = ReplaceStuff('[Mark]', '', TempRow);
			}
			TempRow = ReplaceStuff('[INum]', i, TempRow);
			Exercise += TempRow;
		}
	}

//Make it into a table
	Exercise = TableOpener + Exercise + TableCloser;
}

I = new Array();
I[0] = new Array();
I[0][0] = '<IMG SRC="../../IMAGES/Vocab/Food/Fruit/apple.gif" ALT="" WIDTH="47" HEIGHT="59" ALIGN="LEFT" BORDER="0">';
I[0][1] = 'apple';
I[0][2] = 1;
I[1] = new Array();
I[1][0] = '<IMG SRC="../../IMAGES/Vocab/Food/Fruit/banana.gif" ALT="" WIDTH="67" HEIGHT="73" ALIGN="LEFT" BORDER="0">';
I[1][1] = 'banana';
I[1][2] = 0;
I[2] = new Array();
I[2][0] = '<IMG SRC="../../IMAGES/Vocab/Food/Vegetables/aubergine.gif" ALT="" WIDTH="60" HEIGHT="54" ALIGN="LEFT" BORDER="0">';
I[2][1] = 'aubergine';
I[2][2] = 0;
I[3] = new Array();
I[3][0] = '<IMG SRC="../../IMAGES/Vocab/Food/Vegetables/redpepper.gif" ALT="" WIDTH="55" HEIGHT="57" ALIGN="LEFT" BORDER="0">';
I[3][1] = 'red pepper';
I[3][2] = 0;
I[4] = new Array();
I[4][0] = '<IMG SRC="../../IMAGES/Vocab/Food/Fruit/pineapple.gif" ALT="" WIDTH="52" HEIGHT="89" ALIGN="LEFT" BORDER="0">';
I[4][1] = 'pineapple';
I[4][2] = 0;
I[5] = new Array();
I[5][0] = '<IMG SRC="../../IMAGES/Vocab/Food/Fruit/pear.gif" ALT="" WIDTH="55" HEIGHT="71" ALIGN="LEFT" BORDER="0">';
I[5][1] = 'pear';
I[5][2] = 0;
I[6] = new Array();
I[6][0] = '<IMG SRC="../../IMAGES/Vocab/Food/Vegetables/corn.gif" ALT="" WIDTH="70" HEIGHT="68" ALIGN="LEFT" BORDER="0">';
I[6][1] = 'corn';
I[6][2] = 0;
I[7] = new Array();
I[7][0] = '<IMG SRC="../../IMAGES/Vocab/Food/Vegetables/mushroom.gif" ALT="" WIDTH="63" HEIGHT="58" ALIGN="LEFT" BORDER="0">';
I[7][1] = 'mushroom';
I[7][2] = 0;
I[8] = new Array();
I[8][0] = '<IMG SRC="../../IMAGES/Vocab/Food/Fruit/strawberries.gif" ALT="" WIDTH="61" HEIGHT="64" ALIGN="LEFT" BORDER="0">';
I[8][1] = 'strawberries';
I[8][2] = 0;
I[9] = new Array();
I[9][0] = '<img src="../../IMAGES/Vocab/Food/Fruit/grapes.gif" width="30" height="44" border="0"></img>';
I[9][1] = 'grapes';
I[9][2] = 0;


Status = new Array();

RItems = new Array();

function CreateStatusArrays(){
	for (var x=0; x<I.length; x++){
		Status[x] = new Array();
		Status[x][0] = 0; // Item not matched correctly yet
		Status[x][1] = 0; //Tries at this item so far
	}
}

function ReplaceStuff(Token, Replacement, InString){
	var i = InString.indexOf(Token);
	var FirstBit = '';
	var LastBit = '';
	while (i>-1){
		FirstBit = InString.substring(0, i);
		LastBit = InString.substring(i + Token.length, InString.length);	
		InString = FirstBit + Replacement + LastBit;
		i = InString.indexOf(Token);
	}
	return InString;
}

function EscapeDoubleQuotes(InString){
	var Result = '';
	for (var i=0; i<InString.length; i++){
		if (InString.charAt(i) == '"'){
			Result += '&quot;';
		}
		else{
			Result += InString.charAt(i);
		}
	}
	return Result;
}

function DisplayExercise(StuffToDisplay){
	document.getElementById('MatchDiv').innerHTML = StuffToDisplay;

	RefreshImages();

}

function GetAnswer(INum){
	var Result = -1;
	var s = eval('document.QForm.sel' + INum);
	if (s != null){
		Result = s.selectedIndex - 1;
	}
	return Result;
}

function CheckAnswers(){
	var AllDone = true;
	TotCorrectChoices = 0;
//for each item not fixed or a distractor
	for (var i=0; i<I.length; i++){
		if ((I[i][2] < 1)&&(I[i][0].length > 0)){
//if it hasn't been answered correctly yet
			if (Status[i][0] < 1){
//Add one to the number of tries for this item
				Status[i][1]++;
//Get the answer
				if (GetAnswer(i) == I[i][3]){
//The answer is correct, so set the status flag
					Status[i][0] = 1;
				}
//else the answer is wrong, so remember that
				else{
					AllDone = false;
				}				
			}
//If it's correct, count it
			if (Status[i][0] == 1){
				TotCorrectChoices++;
			}
		}
	}
//Calculate the score
	Score = Math.floor(((TotCorrectChoices-Penalties)/TotalUnfixedLeftItems)*100);
	var Feedback = '';

//Build the feedback
	if (AllDone == true){
		Feedback = CorrectResponse + '<br />' + YourScoreIs + Score + '%.';
	}
	else{
		Feedback = IncorrectResponse + '<br />' + YourScoreIs + Score + '%.';
//Penalty for incorrect check
		Penalties++;
	}

	if (AllDone == true){
		WriteToInstructions(Feedback);
	}



	BuildExercise();
	DisplayExercise(Exercise);

	if (AllDone == true){
		Finished = true;
		setTimeout('Finish()', SubmissionTimeout);
	}

//Show the feedback and rebuild the exercise
	WriteFeedback(Feedback);
}





function Finish(){
//If there's a form, fill it out and submit it
	if (document.store != null){
		Frm = document.store;
		Frm.starttime.value = HPNStartTime;
		Frm.endtime.value = (new Date()).getTime();
		Frm.mark.value = Score;
		Frm.submit();
	}
}

//-->

//]]>


