selectedborder = '2px solid #f00';
unselectedborder = '2px solid #36f';
crtGap = -1;
gap = new Array();
hints = new Array(0,0,0,0,0);
scores = new Array(0,0,0,0,0);

function init(){
	var i;
	for(i=0; i<=4; i++){
		if(document.getElementById('gap' + i) != null){
			document.getElementById('gap' + i).style.border = unselectedborder;
		}
	}
}
function track(gapnum){
	var i;
	crtGap = gapnum;
	for(i=0; i<=4; i++){
		if(document.getElementById('gap' + i) != null){
			document.getElementById('gap' + i).style.border = unselectedborder;
		}
	}
	document.getElementById('gap' + gapnum).style.border = selectedborder;
}
function typeChar(newchar){
	if(!(crtGap>=0 && crtGap<=4)){
		alert('Please first select a box to insert the ' + newchar + ' character!');
		return;
	}
	if(document.getElementById('gap' + crtGap) != null){
		document.getElementById('gap' + crtGap).value = document.getElementById('gap' + crtGap).value + newchar;
	}
	document.getElementById('gap' + crtGap).focus();
}
function trim(str){
	var i, str;
	if (str.length) {
		while (str.charAt(str.length - 1) == ' '){
			str = str.substring(0, str.length - 1);
		}
		while (str.charAt(0) == ' '){
			str = str.substring(1, str.length);
		}
		while ((i = str.indexOf('  ')) != -1) {
			str = str.substring(0, i) + str.substring(i+1, str.length)
		}
		return str;
	}else{
		return '';
	}
}
function setAnswer(numGap){
	if(document.getElementById('gap' + numGap) != null){
		document.getElementById('wrap' + numGap).innerHTML = gap[numGap];
		document.getElementById('wrap' + numGap).style.fontWeight='bold';
		document.getElementById('wrap' + numGap).style.color='blue';
		gap[numGap]='';
	}
}
function getHint(){
	var crtWord, okWord, lowCrtWord, lowOkWord, hintWord;
	if(!(crtGap>=0 && crtGap<=4)){
		alert('Please select a box for a hint.');
		return;
	}
	if(document.getElementById('gap' + crtGap) == null){
		return;
	}
	crtWord = (document.getElementById('gap' + crtGap).value = trim(document.getElementById('gap' + crtGap).value));
	okWord = gap[crtGap];
	lowCrtWord = crtWord.toLowerCase(); //To see if a word is OK, compare lower case (makes the test case insensitive)
	lowOkWord = okWord.toLowerCase();
	if(lowCrtWord == lowOkWord){
		setAnswer(crtGap);
		hints[crtGap]++;
		scores[crtGap] =20*(lowOkWord.length-hints[crtGap])/lowOkWord.length;
		if(scores[crtGap]<0){
			scores[crtGap]=0;
		}
		crtGap = -1;
		init();
		alert('Your word was right!');
	}else{
		i=0;
		hintWord = '';
		while(lowCrtWord.charAt(i) == lowOkWord.charAt(i)){
			hintWord += okWord.charAt(i);
			i++;
		}
		if(hintWord.length<okWord.length){
			hintWord += okWord.charAt(i);
			while(okWord.charAt(i)==' '){ //Just in case the next character to add from the solution is a space, add another character...
				i++;
				hintWord += okWord.charAt(i);
			}
			document.getElementById('gap' + crtGap).value = hintWord;
			alert('A new letter has been added!');
			hints[crtGap]++;
		}else{
			setAnswer(crtGap);
			hints[crtGap]++;
			scores[crtGap] =20*(lowOkWord.length-hints[crtGap])/lowOkWord.length;
			crtGap = -1;
			init();
			alert('The beginning of your guess matched the solution.');
		}
	}
	if((crtGap!=-1) && document.getElementById('gap' + crtGap)){
		document.getElementById('gap' + crtGap).focus();
	}
}
function checkAnswers(){
	var nbnewok, nbnotok, score, crtWord, lowCrtWord, lowOkWord, i, msg;
	nbnewok=0; //New good answers
	nbnotok=0; //Answers still not ok
	score=0;
	for(i=0; i<5; i++){
		if(gap[i] != ''){ //Answer still not checked
			if(document.getElementById('gap' + i) != null){
				crtWord = (document.getElementById('gap' + i).value = trim(document.getElementById('gap' + i).value));
				lowCrtWord = crtWord.toLowerCase(); //To see if a word is OK, compare lower case (makes the test case insensitive)
				lowOkWord = gap[i].toLowerCase();
				if(lowCrtWord==lowOkWord){
					setAnswer(i);
					if(crtGap==i){
						crtGap=-1;
						init();
					}
					scores[i] =20*(lowOkWord.length-hints[i])/lowOkWord.length;
					if(scores[i]<0){
						scores[i]=0;
					}
					nbnewok++;
				}else{
					nbnotok++;
					hints[i]++;
				}
			}else{
				alert('Internal Error!');
				return;
			}
		}
		score += scores[i];
	}
	if(nbnotok){
		msg = '';
		if(nbnewok){
			msg = 'You have ' + nbnewok + ' new right answer' + (nbnewok>1?'s':'') + '.' + "\n";
		}
		msg += nbnotok + ' answer' + (nbnotok>1?'s are':' is') + ' still wrong!';
	}else{
		msg1 = 'Congratulations, all your answers are correct.'
		msg2 = 'Your score is ' + Math.floor(score) + '%!';
		msg = msg1 + "\n" + msg2;
		if(document.getElementById('buttonbar')){
			document.getElementById('buttonbar').innerHTML = '<div style="margin: 15px 0 10px; font-weight: bold; border: 2px solid green; background-color: #ffc; width: 400px;">' + msg1 + '<br />' + msg2;
		}else{
			alert("Can't be found!");
		}
	}
	alert(msg);
	if((crtGap!=-1) && document.getElementById('gap' + crtGap)){
		document.getElementById('gap' + crtGap).focus();
	}
}