/**
 *  ==================================================
 *  SoftChalk Lesson Builder
 *  Copyright 2003-2004 SoftChalk LLC
 *  All Rights Reserved.
 *
 *  ==================================================
 *  For additional information, please contact
 *  http://www.softchalk.com
 *  help@softchalk.com
 */

var my_score = 0;
var totalQ = 0;
for (var i = 1; i <= (scoreQ.length); i++) {
  if (scoreQ[i] == "yes") {
	  totalQ++;
	}
}

//"no" -> form and question number (which are the same)
//"mc_Items" -> how many mc items there are - value should be 0 for short answer, 2 for true-false
//"q_type" -> 1 for multiple choice, 2 for true-false, 3 for multiple answer, 4 for short answer
function check_q(no, mc_items, q_type) {
	var checkedButton = "";
	var correct = "no";
	get_answers = new Array();
	fieldno = eval("document.f" + no + ".q" + no);
	imageno = eval("document.check" + no);
	which_array = eval("right_answers" + no);
	
  if (which_array.length == 1) {  // if single right answer for multiple choice and short answer
		if (q_type != 4) { //if not short answer...
			for (var i = 0; i <= (mc_items-1); i++) {
				if (fieldno[i].checked == true) {  //for multiple choice
					checkedButton = fieldno[i].value;
     		}
    	}
		}
		else {
    	checkedButton = fieldno.value;  //for short answer type
   	}
   	
    if (checkedButton.toUpperCase() == which_array[0].toUpperCase()) {  //if answer is right...
      feedback = eval("feedbackRight" + no);
      correct = "yes";
      if (q_done[no] == false) {
        my_score++;
      }
       q_done[no] = true;
       if (feed[no] == true) {
           //imageno.src = "check.gif";
       }
    }
    else {
      feedback = eval("feedbackWrong" + no);
      if (eval("showCorrect" + no) == "yes") {
        feedback = feedback + '<br><br>The correct response: ' + which_array[0] + '.';
      }
      correct = "no";
      if (document.getElementById && (eval("feedbackHighlight" + no) != "none")) {
      	document.getElementById(eval("feedbackHighlight" + no)).style.color =' red';
        highlighted = eval("feedbackHighlight" + no);
        feedback = feedback + "<br><br>A section of the text which may be helful in understanding the correct response is highlighted in red."
      }
      if (feed[no] == true) {
           //imageno.src = "wrong.gif";
       }
    }
	}
   
  if (which_array.length>1) {  // if more than one right answer (short answer)
   	if (q_type == 4) { //if short answer...
    	correct = "no"; //unless answer is found in array...
    	for (var i = 0; i <= (which_array.length-1); i++) {
     		if (fieldno.value.toUpperCase() == which_array[i].toUpperCase()) {
       		feedback = eval("feedbackRight" + no);
       		correct = "yes";
       		if (q_done[no] == false) {
         		my_score++;
       		}
        	if (feed[no] == true) {
          	//imageno.src = "check.gif";
       		}
         	q_done[no] = true;
       		break;
     		}
    	}
    	
    	if (correct == "no") {
      	feedback = eval("feedbackWrong" + no);
      	if (eval("showCorrect" + no) == "yes") {
        	feedback = feedback + '<br><br>The correct response(s): ' + which_array + '.';
      	}
      	if (document.getElementById && (eval("feedbackHighlight" + no) != "none")) {
        	document.getElementById(eval("feedbackHighlight" + no)).style.color = 'red';
        	highlighted = eval("feedbackHighlight" + no);
        	feedback = feedback + "<br><br>A section of the text which may be helful in understanding the correct response is highlighted in red.";
      	}
      	if (feed[no] == true) {
        	//imageno.src = "wrong.gif";
       	}
    	}
    }
	}
	
	if (q_type==3) { //if multiple answer type...
  	for (var i = 0; i <= (mc_items-1); i++) {
      if (fieldno[i].checked == true) {
        checkedButton = fieldno[i].value;
        my_answers = which_array[0];
        get_answers[get_answers.length] = checkedButton;
        if (my_answers.indexOf(checkedButton) != -1) {
          correct = "partial";
        }
      }
    }
    if (get_answers == my_answers) {
      feedback = eval("feedbackRight" + no);
      correct = "yes";
      if (q_done[no] == false) {
        my_score++;
      }
      if (feed[no] == true) {
        //imageno.src = "check.gif";
      }
      q_done[no] = true;
    }
    else {
      if (correct == "partial")  {
        feedback = eval("feedbackPartial" + no);
      }
      else {
      	feedback = eval("feedbackWrong" + no);
      }
      correct = "no";
  	}
    if (correct == "no") {
      if (eval("showCorrect"+no) == "yes") {
        feedback = feedback + '<br><br>The correct response(s): ' + which_array + '.';
      }
      if (document.getElementById && (eval("feedbackHighlight" + no) != "none")) {
        document.getElementById(eval("feedbackHighlight" + no)).style.color = 'red';
        highlighted = eval("feedbackHighlight" + no);
        feedback = feedback + "<br><br>A section of the text which may be helful in understanding the correct response is highlighted in red.";
      }
      if (feed[no] == true) {
        //imageno.src = "wrong.gif";
      }
    }
  }
  
	if (feed[no] == true) {
    myWin(feedback, correct);
    if (correct == "yes") {
      toggletable(no);
    }
    feed[no] = false;
	}
  
	checkedButton = "";  //resetting values
  correct = "";
  get_answers = "";
  feedback = "";
}

function hint(no) {
	my_hint = eval("hint" + no);
  my_hint_highlight = eval("hintHighlight" + no);
  myWin('<b>Hint</b><br><br>' + my_hint);
  if (document.getElementById && (my_hint_highlight != 'none')) {
    document.getElementById(my_hint_highlight).style.color = 'red';
    highlighted = my_hint_highlight;
  }
}

function clear_highlight(no, mc_items) {
	imageno = eval("document.check" + no);
	//imageno.src = "spacer.gif";
	if (highlighted != "none") {
  	if(document.getElementById) {document.getElementById(highlighted).style.color = 'black';}
  }
  fieldno = eval("document.f" + no + ".q" + no);
  for (var i = 0; i <= (mc_items-1); i++) {
    if (fieldno[i].checked == true) {
      fieldno[i].checked = false;
    }
  }
  if (fieldno.value){fieldno.value = "";}
}

function toggletable(which) {  //hides or displays quiz question
  temp = document.getElementById('t' + which);
  if (tableToggle_array[which] == true) {
    tableToggle_array[which] = false;
    temp.style.position = 'absolute';
    temp.style.visibility = 'hidden';
    temp.style.display= 'none';
  }
  else {
    tableToggle_array[which] = true;
    temp.style.position = 'relative';
    temp.style.visibility = 'visible';
    temp.style.display= 'block';
  }
}
 
function myWin(stuff, correct) {
   winSpecs = 'width=400,height=200,resizable=yes,scrollbars=yes'; 
   win = window.open ("",'pic', winSpecs);
   win.document.clear();
   win.document.open();
   win.document.write("<html><head><title>Feedback</title></head><body>");
   win.document.write(stuff);
   if (correct == "yes") {
     win.document.write("<script>onLoad=setTimeout('window.close()',2000)</script>");
   }
   win.document.write("<br><br><div align=center><form><input type=button value='Close this window' onclick='window.close()'</form></body></html>");
   win.document.close();
   win.focus();
}

function getScore(sendInfo) {
  percent = Math.round((my_score / totalQ) * 100);
  if (totalQ == 1) {
	  myWin('<b>Your Score</b><br><br>Out of '+ totalQ + ' question,<br>you have ' + my_score + ' right for a score of ' + percent + '%.' + sendInfo);
  }
  else {
	  myWin('<b>Your Score</b><br><br>Out of '+ totalQ + ' questions,<br>you have ' + my_score + ' right for a score of ' + percent + '%.' + sendInfo);
  }
}

function mailIt(form) {
  percent = Math.round((my_score / q_item) * 100);
  var recipient = document.f0.recipient.value;
  var subject = "Lesson score";
  var userInfo = "";
  form.action += recipient;
  form.action += "?subject=" + subject;
  userInfo += "Page Title:  " + document.title + "\n";
  userInfo += "Student name:  " + document.f0.student_name.value + "\n";
  userInfo += "Score:  " + percent + "%\n";
  userInfo += "Mailed From: " + document.location + "\n\n";
  form.mailBody.value = userInfo;
  return true;
}
