/**
 *  ==================================================
 *  SoftChalk LessonBuilder
 *  Copyright 2003-2008 SoftChalk LLC
 *  All Rights Reserved.
 *
 *  http://www.softchalk.com
 *  ==================================================
 *  File date: March 20, 2008
 */


 /**
 *	From lesson.js file:
 *
 *  tableToggle_array=new Array();
 *  feed=new Array();
 *  f_right=new Array();
 *  f_wrong=new Array();
 *  f_show_correct=new Array();
 *  f_done=new Array();
 *  f_partial=new Array();
 *
 *  scoreQ[]	indicates whether to include question in scoring
 *  q_done[]	indicates whether the question has been attempted to be answered
 *
 *	"q_num"        form and question number (which are the same)
 *	"mc_items"  how many mc or matching items there are - value should be 0 for short answer, 2 for true-false
 *
 *	"q_type"
 *
 *  MULTIPLE_CHOICE = 1;
 *  TRUE_FALSE = 2;
 *  MULTIPLE_ANSWER = 3;
 *  SHORT_ANSWER = 4;
 *  MATCHING = 5;
 *  ORDERING = 6;
 *
 * 	ACTIVITY = 7;
 *
 *
 *  **********************************************
 *
 *	In lesson html files that have no quizpoppers:
 *
 *  scoreQ[], and my_status are defined in header.
 *
 *
 *  **********************************************
 *
 *	In lesson html files that have quizpoppers:
 *
 *  my_status is defined in header
 *  to prevent error msgbox if student
 *  quits lesson without answering any questions.
 *
 *
 *  **********************************************
 *
 */


 /*
  *************************************************************
  * non-scoring version for non-forced frames
  *************************************************************
  */



var scorm = false					//unless overriden by scorm.js

var CLOSE_THIS_WINDOW = "<a href='javascript:window.close()'>" +
												"<img src='close_feedback.png' border='0' align='right' alt='" + qfLangCloseAlt + "'>" +
												"</a><br><br>";

var INLINE_ID_NAMES = new Array("feed","f_done"); //inline div ids


var skip = new Array();						//monitors if question has already been answered correctly
var this_q_points = new Array();	//each question's scored points




/**
 *  Called when "Check Answer" button is clicked
 */
function check_q(q_num, mc_items, q_type, allow_retry) {
	if (!q_done[q_num]) {
	  q_done[q_num] = true;
	}
	else if (!allow_retry) {
		myWin(qfLangBeenAnswered, "", q_num, q_type, "");
		return;
	}

	var correct = "no";
	var feedback = "";
	var student_answer = "";
	var fieldno = eval("document.f" + q_num + ".q" + q_num); // input name
	var imageno = eval("document.check" + q_num);
	var right_answers = eval("right_answers" + q_num);



  if (q_type == 1 || q_type == 2) {											// MULTIPLE_CHOICE, TRUE_FALSE
		var checkedButton = "";

		for (var i = 0; i < mc_items; i++) {
			if (fieldno[i].checked) {
				checkedButton = fieldno[i].value;
				break;
			}
		}

    student_answer = checkedButton;

    if (checkedButton.toUpperCase() == right_answers[0].toUpperCase()) {
    	correct = "yes";
    	feedback = eval("feedbackRight" + q_num);
    }
    else {
    	feedback = eval("feedbackWrong" + q_num);
      if (eval("showCorrect" + q_num) == "yes") {
        //feedback += "<br><span style=\"font-size: 90%;\">The correct response: " + right_answers[0] + ".";
        feedback += "<br><span style=\"font-size: 90%;\">" + qfLangFBmctf + " " + right_answers[0];
      }
		}
	}
	else if (q_type == 3) {																// MULTIPLE_ANSWER
		var get_answers = new Array();
		var correctly_matched = new Array();

  	for (var i = 0; i < mc_items; i++) {
      if (fieldno[i].checked) {
        get_answers[get_answers.length] = fieldno[i].value;
      }
    }

    var answers_array = right_answers[0].split(",");
    for (var i = 0; i < get_answers.length; i++) {
      for (var j = 0; j < answers_array.length; j++) {
	      if (get_answers[i] == answers_array[j]) {
		      correctly_matched[correctly_matched.length] = get_answers[i];
	      }
      }
    }

    if (correctly_matched.length == answers_array.length) {
    	if (get_answers.length > correctly_matched.length) {
		  	correct = "partial";
		  	//feedback = eval("feedbackPartial" + q_num) + "<br><span style=\"font-size: 90%;\">Only these answers are correct: " + correctly_matched;
		  	feedback = eval("feedbackPartial" + q_num) + "<br><span style=\"font-size: 90%;\">" + qfLangFBmaOnly + " " + correctly_matched;
		  }
		  else {
	    	correct = "yes";
	    	feedback = eval("feedbackRight" + q_num);
    	}
    }
    else if (correctly_matched.length > 0) {
    	correct = "partial";
	    //feedback = eval("feedbackPartial" + q_num) + "<br><span style=\"font-size: 90%;\">Correctly answered: " + correctly_matched;
	    feedback = eval("feedbackPartial" + q_num) + "<br><span style=\"font-size: 90%;\">" + qfLangFBmaPartial + " " + correctly_matched;
    }
    else {
    	feedback = eval("feedbackWrong" + q_num);
			if (eval("showCorrect" + q_num) == "yes") {
        //feedback += "<br><span style=\"font-size: 90%;\">The correct response(s): " + right_answers + ".";
        feedback += "<br><span style=\"font-size: 90%;\">" + qfLangFBmasaWrong + " " + right_answers;
      }
    }

    student_answer = get_answers;
  }
	else if (q_type == 4) {																// SHORT_ANSWER
  	student_answer = fieldno.value;

		var upper = student_answer.toUpperCase();
  	for (var i = 0; i < right_answers.length; i++) {
   		if (upper == right_answers[i].toUpperCase()) {
     		correct = "yes";
     		break;
			}
   	}

   	if (correct == "yes") {
 			feedback = eval("feedbackRight" + q_num);
   	}
  	else {
    	feedback = eval("feedbackWrong" + q_num);
    	if (eval("showCorrect" + q_num) == "yes") {
        //feedback += "<br><span style=\"font-size: 90%;\">The correct response(s): " + right_answers + ".";
        feedback += "<br><span style=\"font-size: 90%;\">" + qfLangFBmasaWrong + " " + right_answers;
    	}
  	}
	}
  else {																								// MATCHING, ORDERING
		var correctly_matched = new Array();
    var correct_answers = new Array();
    var student_answers = new Array();

  	for (var i = 0; i < mc_items; i++) {
  		var option_num = i + 1;
  	  fieldno = eval("document.f" + q_num + ".q" + q_num + "_" + option_num);
  	  var s_index = fieldno.options.selectedIndex;
  	  var ra_index = right_answers[i];

  	  student_answers[i] = fieldno.options[s_index].value;
  	  correct_answers[i] = " " + option_num + " = " + fieldno.options[ra_index].value;

      if(s_index == ra_index) {
        correctly_matched[correctly_matched.length] = option_num;
      }
    }

//    var term = "matched";
//    var term2 = "matchings";
//    if (q_type == 6) {
//	  	term = "ordered";
//			term2 = "order";
//	  }

    if (correctly_matched.length == right_answers.length){
    	correct = "yes";
    	//feedback = eval("feedbackRight" + q_num) + "<br>All items correctly " + term + "!";
    	feedback = eval("feedbackRight" + q_num) + "<br>" + qfLangFBmatorRight;
    }
    else if (correctly_matched.length > 0) {
    	correct = "partial";
      //feedback = eval("feedbackPartial" + q_num) + "<br><span style=\"font-size: 90%;\">Correctly " + term + ": " + correctly_matched;
      feedback = eval("feedbackPartial" + q_num) + "<br><span style=\"font-size: 90%;\">" + qfLangFBmatorPartial + " " + correctly_matched;
    }
    else {
      feedback = eval("feedbackWrong" + q_num);
      if (eval("showCorrect" + q_num) == "yes") {
      	//feedback += "<br><span style=\"font-size: 90%;\">The correct " + term2 +": " + correct_answers + ".</span>";
      	feedback += "<br><span style=\"font-size: 90%;\">" + qfLangFBmatorWrong + " " + correct_answers + "</span>";
      }
    }

    student_answer = student_answers;
  }

	// set the icon and this_q_points
	if (correct == "yes") {
		imageno.src = "check.png";
	}
	else {
		imageno.src = "wrong.png";
	}

	myWin(feedback, correct, q_num, q_type, student_answer);
}


function toggletable(which) {                    // show/hide question
  var num = which.substring(10); //remove prefix "quizpopper"
  if (tableToggle_array[num]) {
    tableToggle_array[num] = false;
    setShow(which, false);
  }
  else {
    tableToggle_array[num] = true;
    setShow(which, true);
  }
}


function togglefeed(q_num, which, hide) {						// show/hide feedback
  my_item_value = eval(which + "[" + q_num + "]");
  if (!hide) {
    if (!my_item_value) {
      setState(q_num, which, true);
      setShow(which + q_num, false);
    }
    else {
      setState(q_num, which, false);
      setShow(which + q_num, true);
    }
	}
  else {
    for (i = 0; i < INLINE_ID_NAMES.length; i++) {
	  	setState(q_num, INLINE_ID_NAMES[i], false);
	  }
    setShow(which + q_num, false);
  }
}


function setState(q_num, which, state) {
	if (which == "feed") {feed[q_num] = state;}
	else if (which == "f_done") {f_done[q_num] = state;}
}


function setShow(elemId, show) {
	var elem = document.getElementById(elemId);
	if (show) {
		elem.style.display = 'block';
	}
	else {
		elem.style.display = 'none';
	}
}


function clearMe(q_num) {
	for (i = 0; i < INLINE_ID_NAMES.length; i++) {
  	setShow(INLINE_ID_NAMES[i] + q_num, false);
  }
}


function hint(q_num) {
  my_hint = eval("hint" + q_num);

  if (!eval('inline_feedback' + q_num)) {
    winSpecs = 'width=400,height=200,resizable=yes,scrollbars=yes';
    win = window.open ("", 'pic', winSpecs);
    win.document.open();
    win.document.clear();
    win.document.write("<html><head><title>" + qfLangHint + "</title></head><body style='font-family: Verdana, Helvetica, sans-serif;'>");
    win.document.write(CLOSE_THIS_WINDOW);
    win.document.write(my_hint);
    win.document.write("</body></html>");
    win.document.close();
    win.focus();
  }
  else {
  	clearMe(q_num);
    document.getElementById("f_done" + q_num).innerHTML = "<embed src=spacer.gif width=0 height=0>" + my_hint;
		f_done[q_num] = true;
		togglefeed(q_num, "f_done", false);
  }
}


// score info is not included
function myWin(stuff, correct, q_num, q_type, student_answer) {

	if (eval('inline_feedback' + q_num)) {																	// inline
    clearMe(q_num);

		document.getElementById("f_done" + q_num).innerHTML = "<embed src=spacer.gif width=0 height=0>" + stuff;
		f_done[q_num] = true;
		togglefeed(q_num, "f_done", false);

  }
  else {																																	// popup
  	winSpecs = 'width=400,height=200,resizable=yes,scrollbars=yes';
    win = window.open("", 'pic', winSpecs);
    win.document.open();
    win.document.clear();
    win.document.write("<html>\n<head>\n<title>" + qfLangFeedback + "</title>\n</head>\n<body style='font-family: Arial, Helvetica, sans-serif;'>\n");
    win.document.write(CLOSE_THIS_WINDOW);
    win.document.write(stuff);
    win.document.write("</body>\n</html>");
    win.document.close();
    win.focus();
  }
}
