/***** UNIVERSAL QUIZ FUNCTIONS *****/
// Calculations based on numberQuestionsToShow
var numberHighest = totalNumberofQuestions;
var checkNumber = numberQuestionsToShow - 1;

//Right and wrong values - used later on
var right = 0;
var wrong = 0;

// Check the answer
var questionAmount = 0;

function checkAnswer(questionNum) {
questionAmount += 1;
	//Next question
	var nextQuestion = questionNum + 1;

	// get radio button results
	var radioVal = 0;
	for(i = 0; i < document.QuizArea.answerChoice.length; i++) {
		if (document.QuizArea.answerChoice[i].checked == true) 
			radioVal = document.QuizArea.answerChoice[i].value;
	}

	
	// Check results and do stuff
	var quizResults = radioVal; // Gets form value
	var searchResults = quizAnswers.search(quizResults); // performs the search
	
	if (searchResults > 0) {  //if answer is right and there is another question
		right += 1;
			if (questionAmount <= checkNumber) { // if right and still more questions
				var rightMessage = "Right!<div class=\"rightAnswer\">" + rightAnswer[questionNum] + "</div>That's " + right + " so far. <br /><br /><a href=\"javascript:showQuiz(" + nextQuestion + ");\">Go on to the next question</a>.";
				document.getElementById('quizArea').innerHTML = rightMessage;
			} else { // if answer is right and there are no more questions
				var lastMessage = "Right!<div class=\"rightAnswer\">" + rightAnswer[questionNum] + "</div>That's all for the quiz. You got " + right + " right. ";
				document.getElementById('quizArea').innerHTML = lastMessage + finalConfMessage + "<br /><br /><a href='quizzes.html'>Take more quizzes.</a>";
			}
	} else {
		wrong += 1;
			if (questionAmount <= checkNumber) { //if answer is wrong and more questions
				var wrongMessage = "Nope, sorry that is incorrect.<br /><br />The right answer is:<div class=\"rightAnswer\">" + rightAnswer[questionNum] + "</div>You've got " + right + " correct so far.<br /><br /><a href=\"javascript:showQuiz(" + nextQuestion + ");\">Go on to the next question</a>.";
				document.getElementById('quizArea').innerHTML = wrongMessage
			} else { //if answer is wrong and there are no more questions
				var wrongMessage = "Nope, sorry that is incorrect.<br /><br />The right answer is:<div class=\"rightAnswer\">" + rightAnswer[questionNum] + "</div>"		
				var lastMessage = "That's all for the quiz. You got " + right + " right. ";
				document.getElementById('quizArea').innerHTML = wrongMessage + lastMessage + finalConfMessage + "<br /><br /><a href='quizzes.html'>Take more quizzes.</a>";
			}
	} 
}


//Show quiz (and start at a random question up to numberHighest since it only shows the number of questions specified up above)
function startQuiz() {
	// To increase questions, and have a random starting place un-comment the next two lines and remove the showQuiz(0)
	//var randomnumber=Math.floor(Math.random()*numberHighest);
	//showQuiz(randomnumber);
	showQuiz(0);
}
function showQuiz(questionNum) {
	var question = questionBlock[questionNum]; // gets the question
	var answer = answerBlock[questionNum]; // gets the answer radios
	var button = "<input name=\"button\" type=\"button\" value=\"Check answer\" onclick=\"checkAnswer(" + questionNum + ");\" />" // next question button
	var step = right + wrong + 1; // answers how many questions have you gone through
	document.getElementById('quizArea').innerHTML = "<div class=\"questioncount\">Question " + step + " of " + numberQuestionsToShow + "</div><h2>" + question + "</h2><div class=\"answerBlock\">" + answer + "</div><div style=\"margin-top:10px;\">" + button + "</div>";
	
}
