//Pacific Life Quiz Logic

var currentQuiz;
var currentQuestion;
var currentMode;
var userAnswer;
var numberCorrect;
var quizBodyId;
var userIsCorrect;
var userAnswerArray = new Array();

cNoChoice = -1;
cMaxRadioButton = 4;

function initQuiz(Quiz)
{
	document.title = cTitle[Quiz];
	currentQuiz = Quiz;
	currentMode = cIntroMode;
	configurePage();	
}

function configurePage()
{
	var s;
	switch (currentMode) 
	{
		case cIntroMode: 
		{
			document.getElementById("QuizText").innerHTML = cIntroText[currentQuiz];
			showRadioButtons(0);
			document.getElementById("QuizButtonBack").style.display="none";
			document.getElementById("QuizButtonNext").value="Continue >>";
			break;
		};
		case cQuestionMode: 
		{
			s = '<b>Question ' + currentQuestion + ' (of ' + cMaxQuestion[currentQuiz] + ')<BR></b>';
			s = s + '<p>' + cQuestion[currentQuiz][currentQuestion];
			document.getElementById("QuizText").innerHTML = s;

			showRadioButtons(cMaxChoice[currentQuiz][currentQuestion]); 

			document.getElementById("QuizButtonBack").style.display="inline";
			document.getElementById("QuizButtonBack").value="<< Back";
	
			document.getElementById("QuizButtonNext").value="Continue >>";
 			break;
		};
		case cAnswerMode: 
		{
			s = '<b>Question ' + currentQuestion + ' (of ' + cMaxQuestion[currentQuiz] + ')<BR><BR></b>';
			if (userIsCorrect) 
			{
				s = s + 'Right! ';			
			}
			else
			{
				s = s + 'Sorry. ';			
			}
			
			s = s + 'The correct answer is: <BR>';
			
			s = s + IndentLine(7)+ getQuestion(cCorrectAnswer[currentQuiz][currentQuestion]);
			s = s + cAnswer[currentQuiz][currentQuestion];
			document.getElementById("QuizText").innerHTML = s;

			showRadioButtons(0); 

			document.getElementById("QuizButtonBack").style.display="inline";
			document.getElementById("QuizButtonBack").value="<< Back";
	
			document.getElementById("QuizButtonNext").value="Continue >>";
			break;
		};
		case cFinalResultMode: 
		{
			s = '<BR>You answered ' + numberCorrect + ' question';
			if (numberCorrect != 1)
			{
				s = s + 's';
			}
			s = s + ' correctly for a score of ' + getPercentCorrect();
			s = s + '<BR><BR>';
			s = s + cFinalText[currentQuiz];
			document.getElementById("QuizText").innerHTML = s;

			showRadioButtons(0);

			document.getElementById("QuizButtonBack").style.display="none";
			document.getElementById("QuizButtonNext").value="Try Again >>";
			break;
		};
	}
	scroll(0,0);
}

function showRadioButtons(maxShowButton)
{
	for (var j = 1; j <= cMaxRadioButton; j++)
	{
		if (j <= maxShowButton)
		{ 
			document.getElementById("radioButton" + j).style.display="inline";
			document.getElementById("radioButton" + j).checked=false;
			document.getElementById("radioText" + j).style.display= "inline";
			document.getElementById("radioText" + j).innerHTML = getQuestion(j);

		}
		else
		{ 
			document.getElementById("radioButton" + j).style.display="none";
			document.getElementById("radioText" + j).style.display= "none";

		}
	}
}

function buttonClickBack()
{
	switch (currentMode) 
	{
		case cQuestionMode: 
		{
			if (currentQuestion == 1)
			{
				currentMode = cIntroMode;
			}
			else
			{
				currentQuestion = currentQuestion - 1;
			}
			break;		
		};
		case cAnswerMode: 
		{
			currentMode = cQuestionMode;
			break;		
		};
	}
	configurePage(); 
}

function buttonClickNext()
{
	switch (currentMode) 
	{
		case cIntroMode: 
		{
			currentMode = cQuestionMode;
			currentQuestion = 1;
			userAnswer = cNoChoice;
			break;
		};
		case cFinalResultMode:
		{
			currentMode = cIntroMode;
			break;
		};
		case cQuestionMode: 
		{
			if (userAnswer == cNoChoice)
			{
				alert("Please make a selection before continuing."); 
			}
			else
			{
				currentMode = cAnswerMode;
				userIsCorrect = (userAnswer == cCorrectAnswer[currentQuiz][currentQuestion]);
				userAnswerArray[currentQuestion] = userAnswer;
			}
			break;		
		};
		case cAnswerMode: 
		{
			if (currentQuestion == cMaxQuestion[currentQuiz])
			{
				currentMode = cFinalResultMode;
				getNumberCorrect();
			}
			else
			{
				currentMode = cQuestionMode;
				currentQuestion = currentQuestion + 1;
				userAnswer = cNoChoice;
			}
			break;		
		};
	}
	configurePage(); 
}

function getNumberCorrect()
{
	numberCorrect = 0;
	for (var j = 1; j <= cMaxQuestion[currentQuiz]; j++)
	{
		if (userAnswerArray[j] == cCorrectAnswer[currentQuiz][j])
		{
			numberCorrect = numberCorrect + 1;
		}
	}
	return numberCorrect;
}

function getNumberCorrect()
{
	numberCorrect = 0;
	for (var j = 1; j <= cMaxQuestion[currentQuiz]; j++)
	{
		if (userAnswerArray[j] == cCorrectAnswer[currentQuiz][j])
		{
			numberCorrect = numberCorrect + 1;
		}
	}
	return numberCorrect;
}

function radioClick(btn)
{
	userAnswer = btn;
}

function getQuestion(choice)
{
	var s="";
	s = cQuestionLetter[choice] + ') ' + cChoice[currentQuiz][currentQuestion][choice];
	if (s.search(/<Table/i)<0)
	{
	      s = s + "<BR><BR>";
	}
	return s;
}

function getPercentCorrect()
{
	return Math.round(numberCorrect / cMaxQuestion[currentQuiz] * 100) + '%.';
}

function IndentLine(num)
{
	var s;
	s = "<BR>";
	for (var j = 1; j <= num; j++)
	{
		s = s + "&nbsp";
	}
	return s;
}
