/*
 * function to handle the events which take place after a user has answered a question
 */
function answerTestYourKnowledgeQuestion(questionId)
{
	var result;
	var question, questionOptions;
	var correct=0;
	var myQuizQuestion;
	var questionIndex = questionId;
	question = this.quiz[questionId];
	questionOptions = eval("document.quizForm.question" + question.id);
	
	
	// If question has no correct answer then it must be a poll question
	if(question.answerQuestionOptionId == null)
	{
		handlePoll(question, questionId);
		return;
	}
		
	// iterate over all of the question options for the answered question
	for(var i = 0; i < questionOptions.length; i++) 
	{
		// if this questionOption is the correct answer then process by updating style and 
		// insert a call to action link
		if(this.quiz[questionIndex].answerQuestionOptionId == questionOptions[i].value)
		{
			myQuizQuestion = document.getElementById("questionOption" + this.quiz[questionIndex].questionOptions[i].id);
			if(myQuizQuestion.innerHTML.indexOf("href") == -1)
			{
				if(question.questionLink != null){
				myQuizQuestion.innerHTML += " - <span class=\"testYourKnowledgeCarat\"><a href=\"" + question.questionLink + "\">" + question.questionLinkText + " </a></span>";
				} else {
				myQuizQuestion.innerHTML += " - <span class=\"testYourKnowledgeCarat\">" + question.questionLinkText + "</span>";
				}
				myQuizQuestion.style.fontWeight = "bold";
				myQuizQuestion.style.color = "green";	
				myQuizQuestion.style.fontSize = "12px";
			}	
			
			// if the user selected this questionOption the process
			if (questionOptions[i].checked) 
			{
				result = questionOptions[i].value;
				
				// if this is the wrong answer then update the HTML to reflect it as such
				if(this.quiz[questionIndex].answerQuestionOptionId != questionOptions[i].value)
				{
					myQuizQuestion = document.getElementById("questionOption" + this.quiz[questionId].questionOptions[i].id);
					
					if(myQuizQuestion.innerHTML.indexOf("[ X ]") == -1)
							myQuizQuestion.innerHTML+= " <span style=\"color: red;\">[ X ]</span>";
					myQuizQuestion.style.fontWeight = "bold";
				}
				else
				{
					// correct answer!
					correct++;
				}
			}
		}
	}
	
	//submit this user's response - associated to the current user id
	this.TestYourKnowledgeService.saveQuestionOption(result, this.userId);
}

/**
 * This function is called when the question which a user answers has not correct answer - ie. its a poll
 * and the function will process the HTML as a poll response.
 * @param question
 * @return
 */
function handlePoll(question){
	if (document.quizForm.tmpPoll.value == 1){ // if hidden field value has been set to 1, ignore, else run
		} else

{
	var tally, percent, total;
	
	// we need to know the total number of answered polls
	total = getTotal(question);
	
	var questionOptions = eval("document.quizForm.question" + question.id);
	
	// iterate over the poll options
	for(var i = 0; i < questionOptions.length; i++) 
	{
		// get the current value for the poll option total
		tally = question.questionOptions[i].total;
		
		// if this option is the one the user selected, save and update the tally and total
		if (questionOptions[i].checked) 
		{
			this.TestYourKnowledgeService.saveQuestionOption(questionOptions[i].value, this.userId);
			tally++;
			total++;
		}
		
		// if this option has never been selected, make sure that it is not null (for math processing)
		if(tally == null)
		{
			tally = 0;
		}
		
		// calculate percent of current option responses given the total and the tally
		percent = 100 * (tally/total);
	    percent = roundTowardsZero(percent);
	     
	    // update the poll question to show the percent as well as the graphical bar
		quizQuestion = document.getElementById("questionOption" + question.questionOptions[i].id);
		quizQuestion.innerHTML+= " - " + percent + "%<div style='width:" + percent*2 + "px;'><div style='background-color:red;'>&nbsp;</div></div>";
		document.quizForm.tmpPoll.value = 1;
		document.getElementById("quizThanks").style.display = "block";

	}
}

/*
 * This function totals the responses to provide a base for poll percentage totals
 */
function getTotal(question)
{
	var total = 0;
	
	var questionOptions = question.questionOptions;
	
	for(var i = 0; i < questionOptions.length; i++) 
	{
		if(questionOptions[i].total == null)
			questionOptions[i].total = 0;
		
		total += questionOptions[i].total;
	}
	
	// fix to remove divide by zero issue
	if (total == 0)
		total = 1;
	
	return total;
}
}
/*
 * Helper function removes decimal from percentage
 */
function roundTowardsZero(x) 
{
    return (x < 0 ? -1 : 1) * Math.floor(Math.abs(x));
}


