

//<![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 may not be able to handle this page. If you have problems try the printable version.');
}

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;
var InGap = false;

function SuppressBackspace(e){ 
	if (InGap == true){
		return;
	}
	 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 = '#ddddf6';
var FuncShadeColor = '#5d5d77';
var ExBGColor = '#bbbbee';

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 = '#bbbbee';
	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 CurrentBox = -1;
var Feedback = '';
var CluesAcross = 'Across:';
var CluesDown = 'Down:';
var Correct = 'Correct! Well done.';
var Incorrect = 'Some of your answers are incorrect. Incorrect squares have been blanked out.'; 
var GiveHint = 'A correct letter has been added.';
var YourScoreIs = 'Your score is ';
var BuiltGrid = '';
var BuiltExercise = '';
var Penalties = 0;
var FirstShow = true;
var ReadingURL = '[ReadingURL]';
var TempReading = '';
var Score = 0;

var CurrBoxElement = null;
var StartTime = (new Date()).toLocaleString();
var HPNStartTime = (new Date()).getTime();
var Finished = false;
var SubmissionTimeout = 30000;



var GridOpener = '<table border="1" style="margin: auto;" cellpadding="0" cellspacing="0">';
var GridRowOpener = '<tr>';
var GridRowCloser = '</tr>';

var BlankCell = '<td bgcolor="#000000" width="30" height="30">&nbsp;</td>';
var LetterOnlyCell = '<td bgcolor="#ffffff" width="30" height="30" align="center" valign="middle"><span class="GridLetter">[Letter]</span></td>';
var NumLetterCell = '<td bgcolor="#ffffff" style="cursor: pointer; cursor: hand;" width="30" height="30" align="center" valign="top" onclick="ShowClue([ClueNum],[x],[y])"><sup><a href="javascript:DoNothing()" onclick="ShowClue([ClueNum],[x],[y])">[ClueNum]</a></sup>&nbsp;<span class="GridLetter">[Letter]</span></td>';

var GridCloser = '</table></center><br />';

var GetAnswerOpener = '<form name="AnswerForm" action="" onSubmit="return false;">';
GetAnswerOpener += '<table border="0" align="center" cellpadding="2" cellspacing="2">';


var GetAnswer = '<tr><td valign="top"><span class="ClueText">[ThisClue]</span></td>';
GetAnswer += '<td valign="top"><input type="text" class="AnswerBox" name="[BoxName]" size="[AnsLength]" maxlength="[AnsLength]" onfocus="InGap=true;" /></td>';

GetAnswer += '<td valign="top"><button class="FunctionButton"  onfocus="FuncBtnOver(this)" onmouseover="FuncBtnOver(this)" onblur="FuncBtnOut(this)" onmouseout="FuncBtnOut(this)" onmousedown="FuncBtnDown(this)" onmouseup="FuncBtnOver(this)" onclick="EnterAnswer(document.AnswerForm.[BoxName].value,[boolAcross],[AnsLength],[x],[y])">';
GetAnswer += '&nbsp;Enter&nbsp;</button></td>';


GetAnswer += '<td valign="top"><button class="FunctionButton"  onfocus="FuncBtnOver(this)" onmouseover="FuncBtnOver(this)" onblur="FuncBtnOut(this)" onmouseout="FuncBtnOut(this)" onmousedown="FuncBtnDown(this)" onmouseup="FuncBtnOver(this)"  onclick="ShowHint([boolAcross],[x],[y],\'document.AnswerForm.[BoxName]\')">';
GetAnswer += '&nbsp;Hint&nbsp;</span></td>';


GetAnswer += '</tr>';

var GetAnswerCloser = '</table></form>';

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';
	}
	FDiv.style.display = 'block';
	document.getElementById('FeedbackOKButton').focus();



	InGap = false;
}

function HideFeedback(){
	document.getElementById('FeedbackContent').innerHTML = '';
	document.getElementById('FeedbackDiv').style.display='none';
	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;

}

function WriteClue(ClueText) {
	document.getElementById('ClueDiv').innerHTML = ClueText;

}




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





	BuildExercise();
	DisplayExercise();


}

function BuildExercise(){
	var OutString = '';
	var Temp = '';
	var GridStuff = GridOpener;	

	for (var i=0; i<L.length; i++){
		GridStuff += GridRowOpener;
		for (var j=0; j<L[i].length; j++){
//If the cell is blank, add a black cell
			if (L[i][j] == ''){
				GridStuff += BlankCell;
			}
//else there's a letter there
			else{
//If there's a clue number, add it
				if (C[i][j] > 0){
					Temp = NumLetterCell;
					Temp = ReplaceStuff('[ClueNum]', C[i][j], Temp);
//Add location info
					Temp = ReplaceStuff('[x]', i, Temp);
					Temp = ReplaceStuff('[y]', j, Temp);
				}
//Otherwise add a blank cell
				else{
					Temp = LetterOnlyCell;
				}

//If the cell is already answered, add the letter from the Guess array
				if (G[i][j] != ''){
					Temp = ReplaceStuff('[Letter]', G[i][j], Temp);
				}
//Otherwise, put in a non-breaking space
				else{
					Temp = ReplaceStuff('[Letter]', '&nbsp;', Temp);
				}
				GridStuff += Temp;
			}
		}
		GridStuff += GridRowCloser;
	}

	GridStuff += GridCloser;

//Put the final pieces together
	OutString = GridStuff;
	BuiltExercise = OutString;
}

function DisplayExercise(){
//Build the output string
	var OutString = '';
	OutString += BuiltExercise;

//Write it to the main div
	document.getElementById('GridDiv').innerHTML = OutString;


}

function BuildClue(ClueNum,x,y,Across){
	var OutString = '';
	if (Across == true){
		if (ClueNum >= A.length){
			return '';
		}	
		if (A[ClueNum] == null){
			return '';
		}
		
		OutString = GetAnswer;
		OutString = ReplaceStuff('[ThisClue]', ClueNum + '. ' + A[ClueNum], OutString);
		OutString = ReplaceStuff('[BoxName]', 'BA'+ClueNum, OutString);
//Find out how long the answer is
		AnsLength = 0;
		i = x;
		j = y;
		while (j<L[i].length){
			if (L[i][j] != ''){
				AnsLength++;
			}
			else{
				break;
			}
			j++;
		}
		OutString = ReplaceStuff('[AnsLength]', AnsLength, OutString);
		OutString = ReplaceStuff('[boolAcross]', 'true', OutString);
	}
	else{
		if (ClueNum >= D.length){
			return '';
		}	
		if (D[ClueNum] == null){
			return '';
		}
		
		OutString = GetAnswer;
		OutString = ReplaceStuff('[ThisClue]', ClueNum + '. ' + D[ClueNum], OutString);
		OutString = ReplaceStuff('[BoxName]', 'BD'+ClueNum, OutString);
//Find out how long the answer is
		AnsLength = 0;
		i = x;
		j = y;
		while (i<L.length){
			if (L[i][j] != ''){
				AnsLength++;
			}
			else{
				break;
			}
			i++;
		}
		OutString = ReplaceStuff('[AnsLength]', AnsLength, OutString);
		OutString = ReplaceStuff('[boolAcross]', 'false', OutString);
	}
	OutString = ReplaceStuff('[x]', x ,OutString);
	OutString = ReplaceStuff('[y]', y, OutString);
	return OutString;
}

function ShowClue(ClueNum,x,y){
	var OutString = '';
	var Temp = '';
	var ThisClue = '';
	var AnsLength = 0;
	var i;
	var j;
	CurrBoxElement = ClueNum;
	if (A[ClueNum] != null){
		ThisClue = GetAnswer;
		Temp = 'Across: ' + ClueNum + '. ' + A[ClueNum];
		ThisClue = ReplaceStuff('[ThisClue]', Temp, ThisClue);		
		OutString += ThisClue;
		OutString = ReplaceStuff('[BoxName]', 'BA'+ClueNum, OutString);
//Find out how long the answer is
		AnsLength = 0;
		i = x;
		j = y;
		while (j<L[i].length){
			if (L[i][j] != ''){
				AnsLength++;
			}
			else{
				break;
			}
			j++;
		}
		OutString = ReplaceStuff('[AnsLength]', AnsLength, OutString);
		OutString = ReplaceStuff('[boolAcross]', 'true', OutString);
	}
	if (D[ClueNum] != null){
		ThisClue = GetAnswer;
		Temp = 'Down: ' + ClueNum + '. ' + D[ClueNum];
		ThisClue = ReplaceStuff('[ThisClue]', Temp, ThisClue);
		OutString += ThisClue;
		OutString = ReplaceStuff('[BoxName]', 'BD'+ClueNum, OutString);
//Find out how long the answer is
		AnsLength = 0;
		i = x;
		j = y;
		while (i<L.length){
			if (L[i][j] != ''){
				AnsLength++;
			}
			else{
				break;
			}
			i++;
		}
		OutString = ReplaceStuff('[AnsLength]', AnsLength, OutString);
		OutString = ReplaceStuff('[boolAcross]', 'false', OutString);
	}
	OutString = GetAnswerOpener + OutString + GetAnswerCloser;
	OutString = ReplaceStuff('[x]', x ,OutString);
	OutString = ReplaceStuff('[y]', y, OutString);
	WriteClue(OutString); 
	if (eval('document.AnswerForm.BA' + ClueNum) != null){
		eval('document.AnswerForm.BA' + ClueNum).focus();
	}
	else{
		if (eval('document.AnswerForm.BD' + ClueNum) != null){
			eval('document.AnswerForm.BD' + ClueNum).focus();
		}
	}
}

function EnterAnswer(Guess,Across,AnsLength,x,y){
	Guess = Guess.toUpperCase();

	var i=x;
	var j=y;
	var Letter = 0;
	while (Letter < AnsLength){
		if (Letter < Guess.length){
			G[i][j] = Guess.charAt(Letter);
		}
		if (Across == true){
			j++;
		}
		else{
			i++;
		}
		Letter++;
	}

	BuildExercise();
	DisplayExercise();
	InGap = false;
	CurrBox = null;
}

function ShowHint(Across,x,y,BoxName){
	var i=x;
	var j=y;
	var OutString = '';
	if (Across==true){
		while (j<L[i].length){
			if (L[i][j] != ''){
				OutString += L[i][j];
				if (G[i][j] != L[i][j]){
					G[i][j] = L[i][j];
					Penalties++;
					break;
				}
			}
			else{
				break;
			}
		j++;
		}
	}
	else{
		while (i<L.length){
			if (L[i][j] != ''){
				OutString += L[i][j];
				if (G[i][j] != L[i][j]){
					G[i][j] = L[i][j];
					Penalties++;
					break;
				}
			}
			else{
				break;
			}
		i++;
		}
	}
	if (eval(BoxName) != null){
		eval(BoxName).value = OutString;
	}
}

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

L = new Array();
L[0] = new Array('','','','','','','','','','','','','G','','','');
L[1] = new Array('','','','','','','M','I','R','K','W','O','O','D','','');
L[2] = new Array('R','U','N','E','S','','O','','','','','','L','','','');
L[3] = new Array('','','','S','','','U','','','','B','I','L','B','O','');
L[4] = new Array('','','','G','','','N','','','','','','U','','','');
L[5] = new Array('','','L','A','K','E','T','O','W','N','','S','M','A','U','G');
L[6] = new Array('G','','','R','','','A','','O','','','','','','','');
L[7] = new Array('L','','H','O','B','B','I','T','O','N','','','G','','','R');
L[8] = new Array('A','','','T','','','N','','D','','','','A','','','I');
L[9] = new Array('M','','','H','','','','','E','L','R','O','N','D','','V');
L[10] = new Array('D','','','','T','R','O','L','L','','','','D','','','E');
L[11] = new Array('R','','','','H','','','','V','','','B','A','L','I','N');
L[12] = new Array('I','','B','E','O','R','N','','E','','','','L','','','D');
L[13] = new Array('N','','','','R','','','','S','','','','F','','','E');
L[14] = new Array('G','O','B','L','I','N','','','','','','','','','','L');
L[15] = new Array('','','','','N','','','R','A','V','E','N','H','I','L','L');


C = new Array();
C[0] = new Array(0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0);
C[1] = new Array(0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0);
C[2] = new Array(3,0,0,4,0,0,0,0,0,0,0,0,0,0,0,0);
C[3] = new Array(0,0,0,0,0,0,0,0,0,0,5,0,0,0,0,0);
C[4] = new Array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
C[5] = new Array(0,0,6,0,0,0,0,0,7,0,0,8,0,0,0,0);
C[6] = new Array(9,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
C[7] = new Array(0,0,10,0,0,0,0,0,0,0,0,0,11,0,0,12);
C[8] = new Array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
C[9] = new Array(0,0,0,0,0,0,0,0,13,0,0,0,0,0,0,0);
C[10] = new Array(0,0,0,0,14,0,0,0,0,0,0,0,0,0,0,0);
C[11] = new Array(0,0,0,0,0,0,0,0,0,0,0,15,0,0,0,0);
C[12] = new Array(0,0,16,0,0,0,0,0,0,0,0,0,0,0,0,0);
C[13] = new Array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
C[14] = new Array(17,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
C[15] = new Array(0,0,0,0,0,0,0,18,0,0,0,0,0,0,0,0);


G = new Array();
G[0] = new Array('','','','','','','','','','','','','','','','');
G[1] = new Array('','','','','','','','','','','','','','','','');
G[2] = new Array('','','','','','','','','','','','','','','','');
G[3] = new Array('','','','','','','','','','','','','','','','');
G[4] = new Array('','','','','','','','','','','','','','','','');
G[5] = new Array('','','','','','','','','','','','','','','','');
G[6] = new Array('','','','','','','','','','','','','','','','');
G[7] = new Array('','','','','','','','','','','','','','','','');
G[8] = new Array('','','','','','','','','','','','','','','','');
G[9] = new Array('','','','','','','','','','','','','','','','');
G[10] = new Array('','','','','','','','','','','','','','','','');
G[11] = new Array('','','','','','','','','','','','','','','','');
G[12] = new Array('','','','','','','','','','','','','','','','');
G[13] = new Array('','','','','','','','','','','','','','','','');
G[14] = new Array('','','','','','','','','','','','','','','','');
G[15] = new Array('','','','','','','','','','','','','','','','');


A = new Array();
A[2]='Space station king makes love to 500 in forest';
A[3]='Flee from the west and north with old letters?';
A[5]='He\'s a blob I confused with the main character';
A[6]='This noisy trading post would not wake student in disarray';
A[8]='The chiefest of calamities re-worked as mug';
A[10]='BO in broth, not right for small town';
A[13]='The finder of the moon letters is a student/drone mix';
A[14]='Go for a walk, but not south, to find a hungry monster';
A[15]='This dwarf was a sort of confused old blister';
A[16]='A skin-changer in part maybe, or not.';
A[17]='An inhabitant of the Misty Mountains may be boiling, but not I!';
A[18]='Confused? I\'ll have the navy for look-out post';


D = new Array();
D[1]='Precious creature in Mogul student confusion?';
D[2]='The lonely maintain, without artificial intelligence, the Open University is inside';
D[4]='That place\'s old name was so Gareth!';
D[7]='Head west with two rings, dig? Southern folk who imprisoned the dwarves';
D[9]='Staring fiercely around, the doctor showed the elven blade';
D[11]='One thousand and a \'alf? Wizard!';
D[12]='Having driven, turned to old measure to reveal the Last Homely House';
D[14]='North one staggered to find the leader of the dwarves';



function CheckAnswers(){

	var AllCorrect = true;
	var TotLetters = 0;
	var CorrectLetters = 0;

//Check each letter
	for (var i=0; i<L.length; i++){
		for (var j=0; j<L[i].length; j++){
			if (L[i][j] != ''){
				TotLetters++;
				if (G[i][j] != L[i][j]){
					G[i][j] = '';
					AllCorrect = false;
				}
				else{
					CorrectLetters++;
				}
			}
		}
	}

	Score = Math.floor(((CorrectLetters-Penalties) * 100)/TotLetters);

//Compile the output
	var Output = '';

	if (AllCorrect == true){
		WriteClue('&nbsp;');
		Output = Correct + '<br />';
	}

	Output += YourScoreIs + ' ' + Score + '%.<br />';
	if (AllCorrect == false){
		Output += Incorrect;
		Penalties++;
	}
	BuildExercise();
	WriteFeedback(Output);
	WriteToInstructions(Output);


		
	
	if (AllCorrect == true){

		Finished = true;
		setTimeout('Finish()', SubmissionTimeout);
		return;
	}

	DisplayExercise();
}







function DoNothing(){}

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();
	}
}

//-->

//]]>


