
/*
// function that will display the feedback for a given choice made by clicking 
// on it's respective option button
function chooseOption() {
	var radios = document.getElementsByTagName('input');
	// iterate over buttons. If not checked, hide the respective div
	for(var i = 0; i < radios.length; i++) {
		if(radios[i].checked) {
			document.getElementById('P_FB' + radios[i].value).style.display = "block";
			// did they check the correct answer? ie. is there a 't' in the 
			// radio button's value tag?
			if(radios[i].value.match(/t/)) {
				document.getElementById('P_FB' + radios[i].value).className = "";
			} else {
				document.getElementById('P_FB' + radios[i].value).className = "p_fb_incorrect";
			}
		} else {
			document.getElementById('P_FB' + radios[i].value).style.display = "none";
		}	
	}
}
*/

var longTime = 10000;

// New swap script for show/hide feedback - old script is retained above for reference.
var current_id = "";
function swapLayers(id) {
	if(id != current_id) {
		document.getElementById("HELP_FB").innerHTML = document.getElementById(id).innerHTML;
		if(document.getElementById("SWAP_"+id) != null) {
			document.getElementById("SWAP_"+id).className = "choice_clicked";
		}
		if(document.getElementById("SWAP_"+current_id) != null) {
			document.getElementById("SWAP_"+current_id).className = "choice";
		}
		current_id = id;
	}
	if(onPracticePage){
    	saveTYResponse();
    }
}




// This will be called on the unload function of a test yourself window. It'll
// save the users response to the current page in a little delicious cookie named
// HMMty
function saveTYResponse() {
	// First, we grab the question number from the p_wrapper div (thomas will cook this out)
	// We need a question number because there is no guarantee that a user won't answer
	// the questions in a non-sequential order. So, when saving an answer, you
	// need to also save the question it corresponds to explicitly. 
	var questionDiv = document.getElementById('HELP').parentNode;
	var questionNumber = questionDiv.id;
	questionNumber = parseInt(questionNumber.substring(questionNumber.indexOf('_')+1));
	
	// if it's the first question, we are going to erase the contents of the 
	// cookie, as presumably the user is either taking the test for the first
	// time, or retaking it. 
	
	//SCRATCH that: we're recording the first response, and not allowing them 
	//to "re-take" the quiz

	/******* Modified for HMM10 ty cookie edits 1120 ************/
	ty_cookie_name = "HMMty"+topicIDNum;
	var tyCookie = getCookie(ty_cookie_name);
	/******* End Modified for HMM10 ty cookie edits 1120 ***********************/

	if(questionNumber == 1) {
		if(!REVIEWING) {
			if(tyCookie == null || "" == tyCookie){
				setCookie('HMMty',"",longTime);
			}
		}
	} 

	
	// get the answer number that the user chose
	var answerNumber = current_id.substring(current_id.indexOf("_")+3);
	
	// save the users response in the form, "questionNumber:answerNumber|"
	// if the cookie already contains an answer for that question, modify
	// the tyCookie to reflect the new changes.
	// Store just the first response

  // 1.19.08 MODIFICATION: CHANGE THE IF CONDITION TO REMOVE NULL
  // ORIGINAL PROTASIS READ 
  /* if(tyCookie == null || "" == tyCookie)  */
  // ADDED CONDITIONAL SEARCH OF STRING BEFORE CONTINUING WITH PROTASIS

  cookie_string_value = tyCookie.toString();
  if(cookie_string_value == "null" || "" == tyCookie)

	{
		tyCookie = pageInfo["topicID"] + ":" + questionNumber + ":" + answerNumber + "|"; 
	} else {
		var responseList = tyCookie.split("|");
		var alreadyAnswered = false;
		for (i=0;i < responseList.length-1;i++) {
			var responsePair = responseList[i].split(":");
			var responseTopic = responsePair[0];
			if (responseTopic == pageInfo["topicID"]){
				var responseQuestion = responsePair[1];
				if (responseQuestion == questionNumber){
					alreadyAnswered = true;
				}
			}
		}
		if (!alreadyAnswered){
			tyCookie += pageInfo["topicID"] + ":" + questionNumber + ":" + answerNumber + "|";
		}
	}
	
	//}

		/******* Modified for HMM10 ty cookie edits 1120 ************/
	setCookie(ty_cookie_name,tyCookie,longTime);
	
	/******** End modified for HMM10 ty cookie edits 1120********/	
	
}


// this function will take as input a question number. It will investigate a 
// string encoded in the form ("questionNumber:answerNumber_answerCorrectness|")* and return the 
// corresponding answerNumber_answerCorrectness for a given questionNumber. If there isn't a 
// response recorded for that question, the function will return -1.
function extractResponse(questionNumber, stringToSearch) {
	var questionLookup = pageInfo["topicID"] + ":" + questionNumber + ":";
	if(stringToSearch == null) { return -1; }
	var index = stringToSearch.indexOf(questionLookup)
	if(index != - 1) {
		index += questionLookup.length;
		var result = "";
		while(stringToSearch.charAt(index) != "|") {
			result += stringToSearch.charAt(index);
			index++
		}
		if(result == "") { return -1; } 
		else { return result; }
	} else {
		return index;
	}
}

function hideAllAnswers() {
	var allPtags = document.getElementsByTagName('p');
	for(var i  = 0; i < allPtags.length; i++) {
		if(allPtags[i].className == 'a_answer') {
			allPtags[i].style.display = 'none';
		}
	}
}

function displayResults() {

	/******* Modified for HMM10 ty cookie edits 1120 ************/
	ty_cookie_name = "HMMty"+topicIDNum;
	var tyCookie = getCookie(ty_cookie_name);
	/****** End Modified for HMM10 ty cookie edits 1120 **************/

	var numCorrect = 0;
	if(tyCookie != null && "" != tyCookie) {
		howManyQuestions = (tyCookie.split("|").length);
		for(var i = 0; i < howManyQuestions; i++) {
			var eachResponse = extractResponse(i,tyCookie);
			if(eachResponse != -1) {
				if (eachResponse.charAt(eachResponse.length -1) == 'c') {
					numCorrect++;
				}
			}

		}
	}
	// output num_correct_answers/num_total_answers
	document.getElementById('scoreRatio').innerHTML += " " + numCorrect + "/" + numQuestions;
	for(var i = 1; i <= numQuestions; i++) {
		
		var qRev = document.getElementById("questionReview_" + i);

		var response = extractResponse(i,tyCookie);

		if(qRev != null) {
			var questionPTags = qRev.getElementsByTagName('div');
			for(var j = 0; j < questionPTags.length; j++) {
				if(questionPTags[j].id.indexOf("a_") != -1 || questionPTags[j].id.indexOf("a_feedback") != -1) {
					if(questionPTags[j].id.indexOf(response) == -1) {
						questionPTags[j].style.display = "none";
					}
				}
			}
		}
		
		if(response != -1) { 
			document.getElementById('a_choice_' + i).style.display = "block";
			document.getElementById('a_didnot_' + i).style.display = "none";
			if(response.split("_")[1] == 'c') {
				qRev.className = 'correct';
				document.getElementById('caption_' + i).innerHTML = correct;
			} else {
				qRev.className = 'incorrect';
				document.getElementById('caption_' + i).innerHTML = incorrect;
			}
		} else {
			document.getElementById('a_choice_' + i).style.display = "none";
			document.getElementById('a_didnot_' + i).style.display = "block";
			qRev.className = 'no_answer';
			document.getElementById('caption_' + i).innerHTML = no_answer;
		}
	}	
}	


/*
var cur_lyr;	// holds id of currently visible layer
function swapLayers(id) {
  if (cur_lyr) hideLayer(cur_lyr);
  showLayer(id);
  cur_lyr = id;
}

function showLayer(id) {
  var lyr = getElemRefs(id);
  if (lyr && lyr.css) lyr.css.display = "block";
}

function hideLayer(id) {
  var lyr = getElemRefs(id);
  if (lyr && lyr.css) lyr.css.display = "none";
}

function getElemRefs(id) {
	var el = (document.getElementById)? document.getElementById(id): (document.all)? document.all[id]: (document.layers)? document.layers[id]: null;
	if (el) el.css = (el.style)? el.style: el;
	return el;
}
*/