//<![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.version = navigator.appVersion
	this.v = parseInt(this.version)
	this.ns = (this.b=="ns" && this.v>=5)
	this.ns5 = (this.b=="ns" && this.v==5)
	this.ns6 = (this.b=="ns" && this.v==5)
	this.ie = (this.b=="ie" && this.v>=4)
	this.ie4 = (this.version.indexOf('MSIE 4')>0)
	this.ie5 = (this.version.indexOf('MSIE 5')>0)
	if (this.mac) this.ie = this.ie5
	this.ie5mac = (this.ie5 && this.mac);
	this.min = (this.ns||this.ie)
}

is = new BrowserCheck();

if ((is.min == false)||(is.ie5mac)){
	alert('Your browser can\'t handle this page. You need NS6 or IE5 on Windows, or NS6 on Mac.');
//	history.back();
}

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;

//Hide the feedback window if showing
	if (thisKey == 13){
		if (document.getElementById('FeedbackDiv').style.display != 'none'){
			HideFeedback();
			Suppress = true;
		}
	}

	if (thisKey == 8) {
		Suppress = true;
	}

	if (Suppress == true){
		if (is.ie){
			window.event.returnValue = false;	
			window.event.cancelBubble = true;
		}
		else{
			e.preventDefault();
		}
	}
}

var NavLightColor = '#fff7bf';
var NavShadeColor = '#7f7740';
var NavBarColor = '#FFFFCC';
var FuncLightColor = '#ffbf7f';
var FuncShadeColor = '#7f4000';
var ExBGColor = '#FFFFCC';

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 = '#FFFFCC';
	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 = 'Right.';
var IncorrectIndicator = 'Wrong.';
var YourScoreIs = 'Your score: ';
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 TableOpener = '<form name="QForm">';
TableOpener += '<table border="0">';

var IRow = '<tr><td><span class="LeftItem">';
IRow += '[strLeftItem]<\/span>&nbsp;&nbsp;&nbsp;&nbsp;<\/td>';
IRow += '<td><span class="RightItem">';
IRow += '[strRightItem]<\/span><\/td>';
IRow += '<td>&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/5) + 'px';
	FDiv.style.width = Math.floor(pg.W/5) + 'px';
//Set the top again in case the window has been resized
	if (is.ns){
		FDiv.style.top = Math.floor(pg.H/5) + window.pageYOffset + 'px';
	}
	else{
		FDiv.style.top = Math.floor(pg.H/5) + 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';


	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';
			}
		}
	}
}

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="??? ">??? <\/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(){
	if (is.ns){
		window.addEventListener('keypress',SuppressBackspace,false);
	}
	else{
		document.attachEvent('onkeydown',SuppressBackspace);
		window.attachEvent('onkeydown',SuppressBackspace);
	}



	PreloadImages();


//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;
}