// Base Functions

var score = 0;
var current_time = max_time;
var timer;

function CheckWord() {
	input = UTIL_GetElementById("quiz_input");
	text = input.value.toLowerCase();

		for (i = 0; i < pokemon.length; i++) {
			id = "item"+i;
			obj = UTIL_GetElementById(id);
			if (pokemon[i] == text && obj.style.display != "") {
				obj.style.display = "";
				score++;
				UTIL_ChangeInnerHTML( "quiz_score", score );
				input.value = "";
				if (score >= pokemon.length) {
					EndQuiz();
				}
				return;
			}
		}
}

function CheckWordIncremental() {
	input = UTIL_GetElementById("quiz_input");
	text = input.value.toLowerCase();
		id = "item"+score;
		obj = UTIL_GetElementById(id);
		if (pokemon[score] == text && obj.style.display != "") {
			obj.style.display = "";
			score++;
			UTIL_ChangeInnerHTML( "quiz_score", score );
			input.value = "";
			if (score >= pokemon.length) {
				EndQuiz();
			}
			return;
		}
}

function StartQuiz() {
	obj = UTIL_GetElementById("quiz_input");
	obj.style.display = "";
	obj.readOnly = false;
	obj.value = "";
	obj.focus();

	obj = UTIL_GetElementById("quiz_giveup");
	obj.style.display = "";

	obj = UTIL_GetElementById("quiz_start");
	obj.style.display = "none";
	UpdateTime();

}

function GiveUp() {
	for (i = 0; i < pokemon.length; i++) {
		id = "item"+i;
		obj = UTIL_GetElementById(id);
		if (obj.style.display != "") {
			obj.style.display = "";
			UTIL_GetElementById("cell" + i).className = "missing";
		}
	}
	EndQuiz();
}

function EndQuiz() {
	clearTimeout(timer);

	obj = UTIL_GetElementById("quiz_input");
	obj.style.display = "none";

	obj = UTIL_GetElementById("quiz_giveup");
	obj.style.display = "none";

	UTIL_ChangeInnerHTML( "quiz_results", "You missed " + (pokemon.length - score) + " " + GetAmnesiaObject(pokemon.length - score) + "!");
	switch (score) {
		case 0:
			UTIL_ChangeInnerHTML( "quiz_summary", "Wow...fail. You got absolutely nothing. Better study the Psydex again!");
			break;
		case pokemon.length:
			UTIL_ChangeInnerHTML( "quiz_summary", "You've named them all! Try another quiz and see how you do.");
			break;
		default:
			UTIL_ChangeInnerHTML( "quiz_summary", "Looks like you missed some... better luck next time.");

	}

	UTIL_ChangeInnerHTML( "quiz_final", "Final Result:");
	remaining = max_time - current_time;
	min = Math.floor(remaining / 60);
	sec = (remaining % 60);
	UTIL_ChangeInnerHTML( "quiz_time", FormatTime(current_time) );
	UTIL_ChangeInnerHTML( "quiz_verdict", "You named " + score + " " + GetAmnesiaObject(score) + " in " +min+ " minute"+(min == "1" ? "" : "s")+", "+sec+" second"+(sec == "01" ? "" : "s")+"" );

}

function UpdateTime() {
	UTIL_ChangeInnerHTML( "quiz_time", FormatTime(current_time) );
	if (current_time <= 0) {
		GiveUp();
		return;
	}
	current_time--;
	timer = setTimeout( "UpdateTime()", 1000 );
}

function FormatTime(time) {
	time_string = Math.floor(time / 60) + ":" + ((time % 60) < 10 ? "0" : "") + (time % 60)
	return time_string;
}