// Images
var answerMarker;
var feedbackCorrectMarker;
var feedbackIncorrectMarker;
var feedbackStudentAnswerMarker = null;
var selectedMarker;
var m_startMarker; // marker before multiple choice item was selected.

// images directory
var imgDirURL = "/jupiter/images/";

// new answer button images
var notSelected = imgDirURL + "nosel.gif";
var notSelectedGray = imgDirURL + "nosel2.gif";
var notSelectedRight = imgDirURL + "noselrt.gif";
var notSelectedRightGray = imgDirURL + "noselrt2.gif";
var selected = imgDirURL + "sel.gif";
var selectedRight = imgDirURL + "selright.gif";
var selectedRightGray = imgDirURL + "selrt2.gif";
var selectedWrongGray = imgDirURL + "selwrg2.gif";
var selectedWrong = imgDirURL + "selwrong.gif";

// multiple choice question layouts
var horizontalUnderLayout = "Horizontal under question";
var verticalUnderLayout = "Vertical under question";
var twoColsUnderLayout = "Two columns under question";
var twoColsRightLayout = "Two columns right of question";
var defaultLayout = verticalUnderLayout;


// Constructor for single Multiple Choice answer
function MultipleChoiceAnswer (msg) {

	//var msgArray = decrypt(msg);
	this.answerText = msg[0];
	this.isCorrect = msg[1];
	this.answerFeedbackText = msg[2];

}

// Constructor for single Multiple Choice activity item
function MultipleChoiceTestItem (questionText, answerArray, feedbackCorrect, feedbackIncorrect, left, top, width, height, fontFace, fontStyle, fontSize, fontColor, layout, questionID, metadata, example) {

	this.type = "multiple choice";
	this.questionText = questionText;
	this.answerArray = answerArray;
	this.feedbackCorrect = feedbackCorrect;
	this.feedbackIncorrect = feedbackIncorrect;
	this.left = left;
	this.top = top;
	this.height = height;
	this.width = width;
	this.fontFace = fontFace;
	this.fontStyle = fontStyle;
	this.fontSize = fontSize;
	this.fontColor = fontColor;

	// The  *** (!xxx) ? "" : xxx" ***  conditionals are for older activity files than don't pass in these new parameters.
	this.layout = (!layout) ? defaultLayout : layout;
	this.questionID = (!questionID) ? "" : questionID;
	this.metadata = (!metadata) ? "" : metadata;
	this.example = (!example) ? false : example;
}


function showMultipleChoiceSelection(itemIndex, answerIndex) {

	// To make buttons respond to mouseOver and mouseOut
	multipleChoiceButton = "multipleChoiceButton_" + itemIndex + "_" + answerIndex;

	// If student has already received feedback and activity is in evaluation mode, don't let them change their answers
	if ((feedbackShown) && (evaluationMode == "Evaluation")) return;
	if (evaluationMode == "Evaluation" && feedbackMode == "Immediate" && studentResponseArray[itemIndex] != null) return;

	// Cumulative mode
	if (feedbackMode == "Cumulative")  {
	
		if (NN4) {
			var activityItemId = "activityItem" + itemIndex;
			m_startMarker = document.bodyDiv.document.layers[activityItemId].document.images[multipleChoiceButton].src;
			document.bodyDiv.document.layers[activityItemId].document.images[multipleChoiceButton].src = selected;
		}
		else {
			m_startMarker = getObj(multipleChoiceButton).src;
			getObj(multipleChoiceButton).src = selected;
		}
	}

	else if (evaluationMode == "Evaluation")  {
		if (studentResponseArray[itemIndex] == null) {
			if (NN4) {
				var activityItemId = "activityItem" + itemIndex;
				m_startMarker = document.bodyDiv.document.layers[activityItemId].document.images[multipleChoiceButton].src;
				document.bodyDiv.document.layers[activityItemId].document.images[multipleChoiceButton].src = selected;
			}
			else
			{
				m_startMarker = getObj(multipleChoiceButton).src;
				getObj(multipleChoiceButton).src = selected;
			}
		}
	}
	
	else if (evaluationMode == "Practice")  {
		if (!activityCompletedArray[itemIndex]) {
			if (NN4) {
				var activityItemId = "activityItem" + itemIndex;
				m_startMarker = document.bodyDiv.document.layers[activityItemId].document.images[multipleChoiceButton].src;
				document.bodyDiv.document.layers[activityItemId].document.images[multipleChoiceButton].src = selected;
			}
			else
			{
				m_startMarker = getObj(multipleChoiceButton).src;
				getObj(multipleChoiceButton).src = selected;
			}
		}
	}
	

}

// returns the index of the correct answer for a multiple choice question
function getCorrectMultipleChoiceAnswerIndex(activityItem) {

	var correctAnswerIndex = 0;
	var answerArray = activityItem.answerArray;

	
	for (var i = 0; i < answerArray.length; i++) {
	//alert(answerArray[i].isCorrect);
		if (answerArray[i].isCorrect == "y") {
			
			correctAnswerIndex = i;
		}
	}

	return correctAnswerIndex;

}

function hideMultipleChoiceSelection(itemIndex, answerIndex) {
	var correctAnswerIndex = getCorrectMultipleChoiceAnswerIndex(testItemArray[itemIndex]);
	var studentAnswerIndex = studentResponseArray[itemIndex];
		
		
	// To make buttons respond to mouseOver and mouseOut
	multipleChoiceButton = "multipleChoiceButton_" + itemIndex + "_" + answerIndex;

	// If student has already received feedback and activity is in evaluation mode, don't let them change their answers
	if ((feedbackShown) && (evaluationMode == "Evaluation")) return;
	if (evaluationMode == "Evaluation" && feedbackMode == "Immediate" && studentResponseArray[itemIndex] != null) return;

	// Evaluation End mode
	// If the question has not already been answered, unselect the button.
	if (feedbackMode == "Cumulative")  {

			if (NN4) {
				var activityItemId = "activityItem" + itemIndex;
				document.bodyDiv.document.layers[activityItemId].document.images[multipleChoiceButton].src = m_startMarker;
			}
			else
				getObj(multipleChoiceButton).src = m_startMarker;
				
	}

	// Evaluation mode
	// If the question has not already been answered, unselect the button.
	else if (evaluationMode == "Evaluation")  {
		if (studentResponseArray[itemIndex] == null) {

			if (NN4) {
				var activityItemId = "activityItem" + itemIndex;
				document.bodyDiv.document.layers[activityItemId].document.images[multipleChoiceButton].src = m_startMarker;
			}
			else
				getObj(multipleChoiceButton).src = m_startMarker;
		}
	}
	
	// Explore mode
	//If the question has not already been answered, 
	// or, if answer was not answered correctly, unselect the button.
	else if (evaluationMode == "Practice") {
		if (!activityCompletedArray[itemIndex]) {
			if (NN4) {
				var activityItemId = "activityItem" + itemIndex;
				document.bodyDiv.document.layers[activityItemId].document.images[multipleChoiceButton].src = m_startMarker;
			}
			else {
				getObj(multipleChoiceButton).src = m_startMarker;
			}
		}
	}
}


// format one multiple choice question vertically
function formatMultipleChoiceItem(itemIndex, itemData) {

	var itemHTML;
	var feedbackMarker;
	var clickResponse = "";
	var multipleChoiceButton;
	var headerHTML = getActivityItemHeader(itemIndex);
	var anchorHTML = "<A name=\"activityItemAnchor" + itemIndex + "\"></A>";
	var theStyle="normal",theWeight="normal";	

	// check if this multi-choice item is a non-functioning example
	var isExample = testItemArray[itemIndex].example;


	footerHTML = "</DIV>";
	if (itemData.fontStyle != null) {
		if(itemData.fontStyle.indexOf("Bold")!=-1)
		{
			theWeight="Bold";
		}
		if(itemData.fontStyle.indexOf("Italic")!=-1)
		{
			theStyle="Italic";
		}
	}

	var theFontColorStyle = "";
	if (itemData.fontColor)
		theFontColorStyle = "; color:" + itemData.fontColor;

	var theQuestionLayout = itemData.layout;

	itemHTML = headerHTML + anchorHTML;

	itemHTML += "<TABLE border=0 width=" + itemData.width + ">";
	itemHTML += "<TR>";
	itemHTML += "<TD VALIGN=\"top\">";
	itemHTML += "<FONT STYLE=\"font-family:" + itemData.fontFace + "; font-size:" + itemData.fontSize + "px; font-style:" + theStyle + "; font-weight:" + theWeight + theFontColorStyle + "\">";
	itemHTML += getItemDisplayNumber(itemIndex) + itemData.questionText;
	itemHTML += "</FONT>";
	itemHTML += "</TD>";

	if (theQuestionLayout != twoColsRightLayout)
	{
		itemHTML += "</TR>\n";
		itemHTML += "<TR>\n";
	}
	else
	{
		itemHTML += "<TD><img src=\"/jupiter/images/invisible.gif\" border=0 width=\"10\" height=\"10\"></TD>\n";
	}

	itemHTML += "<TD VALIGN=\"top\">\n";
	itemHTML += "<TABLE border=0  CELLSPACING=\"5\">\n";

	// Display answers
	var answerIndex;
	for (answerIndex = 0; answerIndex < itemData.answerArray.length; answerIndex++)
	{
		// To make buttons respond to mouseOver and mouseOut
		multipleChoiceButton = "multipleChoiceButton_" + itemIndex + "_" + answerIndex;

		// Make answers clickable and use default marker.
		clickResponse = "<A HREF=\"javascript:checkMultipleChoiceAnswer(" + itemIndex + "," + answerIndex +  ")\" onMouseOver='showMultipleChoiceSelection(" + itemIndex + "," + answerIndex +  ")' onMouseOut='hideMultipleChoiceSelection(" + itemIndex + "," + answerIndex +  ")'>";

		if ( isExample && itemData.answerArray[answerIndex].isCorrect == "y" ) {
			feedbackMarker = "<img src = \"" + notSelectedRight + "\" name = " + multipleChoiceButton + " id = " + multipleChoiceButton + " width = 24 height=24 border=0>";
		}else {
			feedbackMarker = "<img src = \"" + notSelected + "\" name = " + multipleChoiceButton + " id = " + multipleChoiceButton + " width = 24 height=24 border=0>";
		}
		
		if (theQuestionLayout == horizontalUnderLayout)
		{
			if (answerIndex == 0)
				itemHTML += "<TR><TD VALIGN=\"top\">";
			else
				itemHTML += "<TD VALIGN=\"top\">";
		}
		else if (theQuestionLayout == twoColsUnderLayout || theQuestionLayout == twoColsRightLayout)
		{
			if (answerIndex % 2 == 0)
				itemHTML += "<TR><TD VALIGN=\"top\">";
			else
				itemHTML += "<TD VALIGN=\"top\">";
		}
		else
		{
			itemHTML += "<TR><TD VALIGN=\"top\">";
		}

		if (!isExample)
			itemHTML += clickResponse;

		itemHTML += feedbackMarker;

		if (!isExample)
			itemHTML += "</A>";

		itemHTML += "</TD>";
		itemHTML += "<TD VALIGN=\"top\">";
		itemHTML += "<FONT STYLE=\"font-family:" + itemData.fontFace + "; font-size:" + itemData.fontSize + "px; font-style:" + theStyle + "; font-weight:" + theWeight + theFontColorStyle + "\">";
		itemHTML += itemData.answerArray[answerIndex].answerText;
		itemHTML += "</FONT>";

		if (theQuestionLayout == horizontalUnderLayout)
		{
			itemHTML += "</TD><TD><img src=\"/jupiter/images/invisible.gif\" border=0 width=\"10\" height=\"10\"></TD>\n";
		}
		else if (theQuestionLayout == twoColsUnderLayout || theQuestionLayout == twoColsRightLayout)
		{
			if (answerIndex % 2 == 0)
				itemHTML += "</TD><TD><img src=\"/jupiter/images/invisible.gif\" border=0 width=\"10\" height=\"10\"></TD>\n";
			else
				itemHTML += "</TD></TR>\n";
		}
		else
		{
			itemHTML += "</TD></TR>\n";
		}
	}

	if (theQuestionLayout == horizontalUnderLayout)
	{
		itemHTML += "</TR>\n";
	}
	else if (theQuestionLayout == twoColsUnderLayout || theQuestionLayout == twoColsRightLayout)
	{
		if (answerIndex % 2 == 0)
			itemHTML += "<TD></TD><TD></TD></TR>\n";
	}

	if ( MAC && IE && browserVersion == 5 )
	{
		itemHTML += "</TABLE></TABLE>";
	}
	else
	{
		itemHTML += "</TABLE>";
	}

	itemHTML += "</TD>\n";
	itemHTML += "</TABLE>\n" + footerHTML;

//	alert(itemHTML);

	return itemHTML;
}



// Display the correct answer in a multiple choice activity item.
function showMultipleChoiceCorrectAnswer(itemData, itemIndex, studentAnswerIndex) {
	var correctAnswerIndex = getCorrectMultipleChoiceAnswerIndex(itemData);
	
	for (var answerIndex = 0; answerIndex < itemData.answerArray.length; answerIndex++) {
			
		multipleChoiceButton = "multipleChoiceButton_" + itemIndex + "_" + answerIndex;
		//alert(multipleChoiceButton);

		if (answerIndex == correctAnswerIndex)
		{
			if (answerIndex == studentAnswerIndex)
				feedbackMarker = selectedRight;
			else
				feedbackMarker = notSelectedRight;
		}
		else
		{
			if (answerIndex == studentAnswerIndex)
				feedbackMarker = selectedWrong;
			else
				feedbackMarker = notSelected;
		}


		if (NN4) {
			var activityItemId = "activityItem" + itemIndex;
			document.bodyDiv.document.layers[activityItemId].document.images[multipleChoiceButton].src = feedbackMarker;
		}
		else
			getObj(multipleChoiceButton).src = feedbackMarker;
	
	}
		
}


// check multiple choice answer for correctness
function checkMultipleChoiceAnswer(itemIndex, studentAnswerIndex) {
	var itemData = testItemArray[itemIndex];
	var correctAnswerIndex = getCorrectMultipleChoiceAnswerIndex(testItemArray[itemIndex]);
	var feedbackMarker;
	var correctFeedbackText = "";
	var incorrectFeedbackText = "";
	var exploreIncorrectFeedbackText = "";
	var multipleChoiceButton;
		

	// Get feedback messages, if they are not blank.
	if (itemData.feedbackCorrect != "") {
		correctFeedbackText = itemData.feedbackCorrect;
	}
	
	if (itemData.feedbackIncorrect != "") {
		incorrectFeedbackText = itemData.feedbackIncorrect;
	}

	// Cumulative Mode
	if (feedbackMode == "Cumulative" || feedbackMode == "No feedback") {
	
		// If student has already received feedback and activity is in evaluation mode, don't let them change their answers
		if ((feedbackShown) && (evaluationMode == "Evaluation")) return;
		
		// Record student response but do not update navigation header bar
		recordStudentResponse(itemIndex, " " + studentAnswerIndex);
		

		// If in explore mode, grey out answers only if answer was correct, and display correct answer
		for (var answerIndex = 0; answerIndex < itemData.answerArray.length; answerIndex++) {
			
			multipleChoiceButton = "multipleChoiceButton_" + itemIndex + "_" + answerIndex;

			if (studentAnswerIndex == answerIndex)
			{
				feedbackMarker = selected;
				m_startMarker = selected;
			}
			else
			{
				feedbackMarker = notSelected;
			}

			if (NN4) {
				var activityItemId = "activityItem" + itemIndex;
				document.bodyDiv.document.layers[activityItemId].document.images[multipleChoiceButton].src = feedbackMarker;
			}
			else
				getObj(multipleChoiceButton).src = feedbackMarker;
	
		}	
		updateNavigationBar(itemIndex);

	}


	// Evaluate Mode
	if (evaluationMode == "Evaluation" && feedbackMode == "Immediate") {	

		// Record student response and update navigation header bar
		if (studentResponseArray[itemIndex] == null) {
			recordStudentResponse(itemIndex, " " + studentAnswerIndex);
		}
		else
		{
//			if (testItemArray.length == getTestItemsAttempted())
//				showCurrentScore();
			return;
		}
		
		// show feedback
		if ((studentAnswerIndex == correctAnswerIndex)) {
			studentScore++
			if ( correctFeedbackText == null || correctFeedbackText == "" ) {
				if ( itemData.answerArray[correctAnswerIndex].answerFeedbackText == "" )
					showFeedback("correct", "Correct!", "Multichoice");
				else
					showFeedback("correct", itemData.answerArray[correctAnswerIndex].answerFeedbackText, "Multichoice");
			} else {
				showFeedback("correct", correctFeedbackText, "Multichoice");
			}
		}
		
		else {
			if ( incorrectFeedbackText == null || incorrectFeedbackText == "" ) {
				showFeedback("incorrect", itemData.answerArray[studentAnswerIndex].answerFeedbackText, "Multichoice");
			} else {
				showFeedback("incorrect", incorrectFeedbackText, "Multichoice");
			}		
		}
		
	
		// If student has answered question:
		// If in evaluate mode, grey out answers, and display correct answer
		showMultipleChoiceCorrectAnswer(itemData, itemIndex, studentAnswerIndex);

//		if (testItemArray.length == getTestItemsAttempted())
//			showCurrentScore();

		updateNavigationBar(itemIndex);
	}
	
	// Practice Mode		
	else if (evaluationMode == "Practice") {
		// Record student response and update navigation header bar
		if (studentResponseArray[itemIndex] == null) {

			if ((studentAnswerIndex == correctAnswerIndex)) {
				studentScore++;
			}
			recordStudentResponse(itemIndex, " " + studentAnswerIndex);
		}
		
		// show feedback
		if ((studentAnswerIndex == correctAnswerIndex)) {
			if ( correctFeedbackText == null || correctFeedbackText == "" ) {
				if ( itemData.answerArray[correctAnswerIndex].answerFeedbackText == "" )
					showFeedback("correct", "Correct!");
				else
					showFeedback("correct", itemData.answerArray[correctAnswerIndex].answerFeedbackText);
			} else {
				showFeedback("correct", correctFeedbackText);
			}
			activityCompletedArray[itemIndex] = true;
		}
		else {
			if ( incorrectFeedbackText == null || incorrectFeedbackText == "" ) {
				showFeedback("incorrect", itemData.answerArray[studentAnswerIndex].answerFeedbackText);
			} else {
				showFeedback("incorrect", incorrectFeedbackText);
			}
		}
		
	
		// If student has answered question:
		// If in explore mode, grey out answers only if answer was correct, and display correct answer
		for (var answerIndex = 0; answerIndex < itemData.answerArray.length; answerIndex++) {
			
			multipleChoiceButton = "multipleChoiceButton_" + itemIndex + "_" + answerIndex;
			//alert(multipleChoiceButton);
			

			if (studentAnswerIndex == answerIndex) {
					if (answerIndex == correctAnswerIndex)
					{
						feedbackMarker = selectedRight;
						m_startMarker = selectedRight;
					}
					else
					{
						feedbackMarker = selectedWrong;
						m_startMarker = selectedWrong;
					}
			}			
			else {

				feedbackMarker = notSelected;
			}

			if (NN4) {
				var activityItemId = "activityItem" + itemIndex;
				document.bodyDiv.document.layers[activityItemId].document.images[multipleChoiceButton].src = feedbackMarker;
			}
			else
				getObj(multipleChoiceButton).src = feedbackMarker;
	
		}
		updateNavigationBar(itemIndex);
	}
	
}

