/*
* Construct the quiz array from the category based database lookup
* Start the quiz.
*/
function initializeQuiz()
{
	
	for(var i=0; i<questions.length; i++)
	{
		questions[i] = i;
	}

	for(var i=0; i<questions.length*5; i++)
	{
		var index1 = Math.round(Math.random()*(questions.length-1));
		var index2 = Math.round(Math.random()*(questions.length-1));
		var temp = questions[index1];
		questions[index1] = questions[index2];
		questions[index2] = temp;
		
	}
	response = document.getElementById("response");
	question = document.getElementById("question");
	
	showNextQuestion();
}

/*
* Display the next question and a number of possible answers.
*/
function showNextQuestion()
{
	document.getElementById("quizInfo").innerHTML = "Question " + (currentQuestion+1) + " of " + (questions.length);

	if(currentQuestion == questions.length)
	{
		endQuiz();
		return;
	}
	
	var correctAnswerIndex = Math.round(Math.random()*(svarmuligheder-1));
	for(var i=0; i<svarmuligheder; i++)
	{
		var j = 0;
		
		if(i == correctAnswerIndex)
		{
			j = questions[currentQuestion];
		}
		else
		{
			var check;
			do
			{
				check = true;
				j = Math.round(Math.random()*(ordArray.length-1));
				if(j == questions[currentQuestion])
					check = false;
				for(var k=0; k<i; k++)
				{
					if(j == svarindex[k])
						check = false;
				}
			} while(!check);
		}
		svarindex[i] = j;

		if(retning == 0 && tegnsaet == 1)
		{
			//set tool tip to the kana version
			document.getElementById("button"+i).title = convert(ordArray[j][2]);	
		}
		else
		{
			//set tool tip to the romanji version
			document.getElementById("button"+i).title = ordArray[j][retning];	
		}

		//if english question and japanse answer and hiragana is displayed then:
		if(retning == 0 && tegnsaet == 2)
		{
			document.getElementById("button"+i).innerHTML = ordArray[j][2];			
		}
		else
		{
			document.getElementById("button"+i).innerHTML = ordArray[j][retning];

		}
		
	}
	
	if(retning == 1 && tegnsaet == 1)
	{
		//set tool tip to the kana version
		question.title = convert(ordArray[questions[currentQuestion]][2]);
	}
	else
	{
		//set tool tip to the romanji version
		question.title = ordArray[questions[currentQuestion]][1-retning];
	}
	
	//if japanese question and english answers and hiragana is displayed then:
	if(retning == 1 && tegnsaet == 2)
	{
		question.innerHTML = ordArray[questions[currentQuestion]][2];
	}
	else
	{
		question.innerHTML = ordArray[questions[currentQuestion]][1-retning];	
	}

	//reset response field
	response.innerHTML = "";
}

/*
* Display the endquiz div tag and set the total score 
*/
function endQuiz()
{

	document.getElementById("quiz").style.display = "none";
	document.getElementById("endquiz").style.display = "";
	document.getElementById("score").innerHTML = "You answered " + Math.round(100.0*ordArray.length/questions.length) + "% correct";  
}

/*
* Verify if the selected answer is correct. 
* If so, then go to the next question, if not display the correct answer.
*/
function validateAnswer(answerNumber)
{
	var answer = svarindex[answerNumber];
	if(answer == questions[currentQuestion])
	{
		currentQuestion++;
		showNextQuestion();
	}
	else
	{
		//Set tool tip on response to the romanji version:
		response.title = "Wrong answer. The correct answer is: " + ordArray[questions[currentQuestion]][retning];
	
		//if japanese answers and kana character set then:
		if(retning == 0 && tegnsaet == 2)
		{
			response.innerHTML = "Wrong answer. The correct answer is: " + ordArray[questions[currentQuestion]][2];
		}
		else
		{
			response.innerHTML = "Wrong answer. The correct answer is: " + ordArray[questions[currentQuestion]][retning];
		}
		var a = new Array(1);
		a[0] = questions[currentQuestion];
		questions = questions.concat(a);
	}
}

/*
* Converts the display of kana from std. text to japanese letters
*/
function convert(s)
{
	var p=document.getElementById("convert");
	p.innerHTML = s;
	return p.innerText;
}

/*
* Populates the quiz length field in the quiz selection page depending on the selected quiz subject.
*/
function populateLengthField()
{
	quizlengthfield = document.getElementById("quizlength");
	
	var optionFields = document.getElementById("ordgruppe").options;
	var quizinterval = new Number(document.getElementById("quizinterval").value);
	quizinterval = quizinterval.valueOf();

	var selectedSubjects = new Array();
	var j=0;
	for(var i=0; i<optionFields.length; i++)
	{
		if(optionFields[i].selected == true)
		{
			selectedSubjects[j++] = optionFields[i].text;
		}
	}
	
	if(selectedSubjects.length == 1)
	{
		//Enable the quiz length field
		quizlengthfield.disabled = false;

		var subject = selectedSubjects[0];
		
		//Determine the number of words defined for the subject.
		//var re = /\d+/;
		var re = /\((\d+)\)/;
		var wordsinsubject = (re.exec(subject))[1]; //get match without paranteses matches are (number), number . I don't know how to get rid of the first match.
		
		//Make options for quizlength field with "quizinterval" word intervals.
		var length = quizlengthfield.options.length;
		for(var i=length-1; i>=0; i--)
		{
			quizlengthfield.remove(i);
		}

		var option = document.createElement("OPTION");
		option.text="All";
		option.value="All";
		try
		{	//mozilla
			quizlengthfield.add(option,null);
		}
		catch(ex)
		{	//IE
			quizlengthfield.add(option);
		}
		var startindex=0;
		var slutindex=0;

		for(var i=0; i<Math.floor(wordsinsubject/quizinterval); i++)
		{
			var option = document.createElement("OPTION");
			startindex = (i*quizinterval+1);
			slutindex =  i*quizinterval + quizinterval;
			option.text= startindex+" - "+slutindex;
			option.value=startindex-1;
			try
			{
				quizlengthfield.add(option, null);
			}
			catch(ex)
			{
				quizlengthfield.add(option);
			}
		}
		if(i != 0 && i*quizinterval < wordsinsubject)
		{
			var option = document.createElement("OPTION");
			option.text=(i*quizinterval+1)+" - "+ wordsinsubject;
			option.value=i*quizinterval;
			try
			{
				quizlengthfield.add(option, null);
			}
			catch(ex)
			{
				quizlengthfield.add(option);
			}

		}
	}
	else
	{
		quizlengthfield.disabled = true;
	}		
}

