
//********** SITE AND HISTOLOGY CODE **********
//function lyr_hide(lyr_id1, lyr_id2){
//	document.getElementById(lyr_id1).style.visibility = "hidden";
//	document.getElementById(lyr_id2).style.visibility = "hidden";
//}

//SUBMIT BUTTON: CHECK ANSWERS.
function chk_site_his(arr_txt, ans1, ans2, ob_pic){

var input1 = arr_txt[0].value;
var input2 = arr_txt[1].value;

	if(input1 == "" || input2 == ""){
	alert("Make sure both text fields are filled.");
	}else{

		if(input1 == ans1 && input2 == ans2){
		ob_pic.innerHTML="Correct";
		ob_pic.className="ansCorrect"
		}else{
		ob_pic.innerHTML="Incorrect";
		ob_pic.className="ansIncorrect"
		}

	}

}

// RESET BUTTON: CLEAR THINGS UP.
function reset_site_his(arr_txt, ob_pic, id_lyr){
	clear_txts(arr_txt);
	restore_img(ob_pic);
	hide_lyr(id_lyr);
}


//********** SS2K **********

//Check Answers.

function chk_ss2k(rd, x, ob_pic){
	var ob_pic = $(ob_pic); //Retrieve the user input string.

	opt = -1;
	for(i=0; i<rd.length; i++){
  	if(rd[i].checked){
  	opt = i;
  	}
	}

	if(opt == -1){
  	alert('No selection made.');
  	return false;
	}

	if(opt == x){
		ob_pic.innerHTML="Correct";
		ob_pic.className="ansCorrect";
  	}else{
		ob_pic.innerHTML="Incorrect";
		ob_pic.className="ansIncorrect";
  	}
}


//Clean Things Up.

function reset_ss2k(arr_rd, ob_pic, id_lyr){
	reset_rd_btn(arr_rd);
	hide_lyr(id_lyr);
	restore_img(ob_pic);
}

// RESET RADIO BUTTONS
function reset_rd_btn(arr_rd){
for(i=0; i<arr_rd.length; i++){
 	arr_rd[i].checked=false;
	}
}


//********** TNM 6 **********


// CHECK TNM 6 ANSWERS
function chk_tnm6(ob_txt, str, ob_pic){

var ob_pic = $(ob_pic); //Retrieve the user input string.
var input = ob_txt.value; //Retrieve the user input string.
var reg = new RegExp(); //Create an instance of reg object.
reg_patn = reg.compile(str, "i"); // Form a reg pattern with the passed string: case insensitive.

//get rid of two reg anchors (^ and $) in the passed string, turning the string into the standard format.
var end_chr_index = str.length - 2;
var standard_input = str.substr(1, end_chr_index);

	if(input == ""){
		alert("No input.");
	} else {
		a_match = input.match(reg_patn); //Match the user input with the compiled reg pattern.
		if(a_match){
			ob_pic.innerHTML="Correct";
			ob_pic.className="ansCorrect";
			//ob_txt.value = standard_input; // Replace the input of the text field with the reformated string.
		}else{
			ob_pic.innerHTML="Incorrect";
			ob_pic.className="ansIncorrect";
		}
	}
}


//********** COLLABORATIVE STAGE **********

function ck_cs(txt, answer, pic){
	if(txt.value == ""){
		alert("No input in the text field.");
		}else{
	if(txt.value == answer){
		document.images[pic].src="images/quiz_feedback_correct.gif";
	}else{
		document.images[pic].src="images/quiz_feedback_incorrect.gif";
	}
	}
}

function discuss_cs(txt_area, discussion){
	txt_area.value = discussion ;
}

function reset_cs(ob_txt, ob_txt_area, str_pic){
	ob_txt.value = "";
	ob_txt_area.value = "";
	document.images[str_pic].src="images/quiz_feedback_placeholder.gif";
}

//********** GENERAL OPERATIONS **********

// Image Mouse-Over Effect
function img_change(img_num, img_src){
document.images[img_num].src=img_src;
}

// Printable Popup
function nw_printable(f_src) {
        win01=window.open(f_src,"new_win03","toolbar=no,directories=no,menubar=yes,scrollbars=yes,width=644,height=500");
}

// Show Discussion Panel.
function show_lyr(id_lyr){
document.getElementById(id_lyr).style.visibility = "visible";
}

// Hide a single layer.
function hide_lyr(id_lyr){
	$(id_lyr).hide();
}

// Restore a single image.
function restore_img(ob_pic){
	ob_pic.innerHTML="";
}

// Restore a group of images
function restore_imgs(arr_pics){
for (var i=0; i<arr_pics.length; i++){
	arr_pics[i].src = "";
	}
}

// Empty a Group of Text Fields
function clear_txts(arr_txt){
	for (var i=0; i<arr_txt.length; i++){
		arr_txt[i].value = "";
	}
	arr_txt[0].focus();
}

// TOGGLE TWO DISCUSSION LAYERS
function lyr_show_hide(lyr_id1, lyr_id2){
	document.getElementById(lyr_id1).style.visibility = "visible";
	document.getElementById(lyr_id2).style.visibility = "hidden";
}

//AUTO TAB
function tab_next(ob_txt, event, len, next_txt) {
var txt_length=0;
	if (event == "down") {
		txt_length=ob_txt.value.length;
		}
	else if (event == "up") {
		if (ob_txt.value.length != txt_length) {
			txt_length=ob_txt.value.length;
			if (txt_length == len) {
				next_txt.focus();
			}
		}
	}
}








/* ******************* IVAN'S *************************************** */

//CHECK SINGLE INPUT TEXT FIELD QUESTIONS

function chk_txt(answerElement, correctAnswer, feedbackElement){

	var feedbackElement = $(feedbackElement); //Retrieve the user input string.
	var answer = $(answerElement).value; //Retrieve the user input string.

	// Validate, standardize (trim, collapse multiple white spaces)
	answer=answer.replace(/\s/g,' ').replace(/  +/g,' ').replace(/^ | $/g,'');
	$(answerElement).value=answer;

	correctAnswer = correctAnswer.replace(/([^\\])\./g,'$1\\.');
	var regex = new RegExp('^'+correctAnswer+'$','i'); //Create an instance of reg object.

	//get rid of two reg anchors (^ and $) in the passed string, turning the string into the standard format.
	if(answer == ""){
		alert("No input.");
	} else {
		if( regex.test(answer) ){
			feedbackElement.innerHTML="Correct";
			toggleClassName(feedbackElement,"ansCorrect");
			//ob_txt.value = standard_input; // Replace the input of the text field with the reformated string.
		}else{
			feedbackElement.innerHTML="Incorrect";
			toggleClassName(feedbackElement,"ansIncorrect");
		}
		feedbackElement.focus();
	}
}

function hide_chk_txt(answerElement, correctAnswer, feedbackElement, quesSuffix){
  chk_txt(answerElement, correctAnswer, feedbackElement);
  $('cktohide'.concat(quesSuffix)).hide();
  $('resettohide'.concat(quesSuffix)).show();
}
function hide_reset_txt(formId, discussId, quesSuffix){
  reset_txt(formId, discussId);
  $('resettohide'.concat(quesSuffix)).hide();
  $('cktohide'.concat(quesSuffix)).show();
  $('hideanshide'.concat(quesSuffix)).hide();
  $('showanshide'.concat(quesSuffix)).show();
}
function hide_showans(quesID, quesSuffix){
  $('showanshide'.concat(quesSuffix)).hide();
  $('hideanshide'.concat(quesSuffix)).show();
  $(quesID).toggle();
}
function hide_hideans(quesID, quesSuffix){
  $('hideanshide'.concat(quesSuffix)).hide();
  $('showanshide'.concat(quesSuffix)).show();
  $(quesID).toggle();
}
function tnm_reset(formId, discussId){
	$(discussId).hide();
}
//RESET TEXT FIELD QUESTIONS

// Sample reset function call: reset_txt('fm', 'lyr_tnm6_c')
function reset_txt(formId, discussId){
	$(discussId).hide();
	$$('#'+formId+' a').each( function (o) { o.update(''); toggleClassName(o); o.hide(); } );
}

function resetAll(formId, discussId, summaryId ){
	if ( ! summaryId ) summaryId = 'summary';
	$$('#'+formId+' a').each( function (o) { o.update(''); toggleClassName(o); o.hide(); } );
	if ( $(discussId) ) $(discussId).hide();
	if ( $(summaryId) )	{
		$(summaryId).hide();
		$$('#'+summaryId+' ul')[0].update();
	}
  $('resettohide_fm').hide();
  $('cktohide_fm').show();
  $('hideanshide_fm').hide();
  $('showanshide_fm').show();
}


function chk_mc(formId, correctAnswerMap, feedbackIdPrefix, mcOptions){
	if ( typeof mcOptions == 'undefined' || ! mcOptions ) mcOptions = {};
	var formElement = $(formId);
	var answersSummary = {'u': 0, 't': 0, 'f': 0};
	var questionCount = 0;

	for ( qId in correctAnswerMap ) {
		questionCount++;
		var answerIndex = -1;
		// Determine which radio/checkbox is checked (of an answer group of radios/check's)
		for(i=0; i<formElement[qId].length; i++){
	  	if(formElement[qId][i].checked){
	  		answerIndex = i; break;
	  	}
		}

		var feedbackElement = $(feedbackIdPrefix+'_'+qId)
		if(answerIndex == -1){
	  	feedbackElement.innerHTML="Unanswered";
			toggleClassName(feedbackElement,"ansUnanswered");
			answersSummary['u']++;
		}
		else if(answerIndex == correctAnswerMap[qId]){
			feedbackElement.innerHTML="Correct";
			toggleClassName(feedbackElement,"ansCorrect");
			answersSummary['t']++;
	  }
	  else {
			feedbackElement.innerHTML="Incorrect";
			toggleClassName(feedbackElement,"ansIncorrect");
			answersSummary['f']++;
	  }
	} // end-for

	var hasSummary = false;

	if ( questionCount > 1 )
		hasSummary = summarize_mc( answersSummary, typeof(mcOptions['summaryId'])!='undefined' ? mcOptions['summaryId'] : 'summary'  );

	if ( ! hasSummary )
		window.location="#quiz";

  $('cktohide_fm').hide();
  $('resettohide_fm').show();
}

function chk_box(formId, correctAnswerMap, feedbackIdPrefix, mcOptions){
	if ( typeof mcOptions == 'undefined' || ! mcOptions ) mcOptions = {};
	var formElement = $(formId);
	var answersSummary = {'u': 0, 't': 0, 'f': 0};
	var questionCount = 0;

	for ( qId in correctAnswerMap ) {
		questionCount++;
		var answerIndex = -1;
		// Determine which radio/checkbox is checked (of an answer group of radios/check's)
		var isCorrect = ( formElement[qId].checked && correctAnswerMap[qId] ) || ( ! formElement[qId].checked && ! correctAnswerMap[qId] );


		var feedbackElement = $(feedbackIdPrefix+'_'+qId)
		if( isCorrect ){
			feedbackElement.innerHTML="Correct";
			toggleClassName(feedbackElement,"ansCorrect");
			answersSummary['t']++;
	  }
	  else {
			feedbackElement.innerHTML="Incorrect";
			toggleClassName(feedbackElement,"ansIncorrect");
			answersSummary['f']++;
	  }
	} // end-for

	var summaryId = typeof(mcOptions['summaryId']) !=  'undefined' ? mcOptions['summaryId'] : 'summary';
	if ( questionCount > 1 )
		summarize_mc( answersSummary, summaryId );
}


function chk_def(formId, correctAnswerMap, feedbackIdPrefix, mcOptions){
	if ( typeof mcOptions == 'undefined' || ! mcOptions ) mcOptions = {};
	var formElement = $(formId);
	var answersSummary = {'u': 0, 't': 0, 'f': 0};
	var questionCount = 0;

	for ( qId in correctAnswerMap ) {
		questionCount++;
		var answerIndex = -1;
		// Determine which radio/checkbox is checked (of an answer group of radios/check's)
		var isCorrect = formElement[qId].value == correctAnswerMap[qId];
		var isBlank = formElement[qId].value.search(/^\s*$/) >= 0;

		var feedbackElement = $(feedbackIdPrefix+'_'+qId)
		if( isBlank ){
	  	feedbackElement.innerHTML="Unanswered";
			toggleClassName(feedbackElement,"ansUnanswered");
			answersSummary['u']++;
		}
		else if( isCorrect ){
			feedbackElement.innerHTML="Correct";
			toggleClassName(feedbackElement,"ansCorrect");
			answersSummary['t']++;
	  }
	  else {
			feedbackElement.innerHTML="Incorrect";
			toggleClassName(feedbackElement,"ansIncorrect");
			answersSummary['f']++;
	  }
	} // end-for

	var summaryId = typeof(mcOptions['summaryId']) !=  'undefined' ? mcOptions['summaryId'] : 'summary';
	if ( questionCount > 1 )
		summarize_mc( answersSummary, summaryId );
}

function summarize_mc( answers, summaryId ) {
		var str = '';
		var summary = $(summaryId)
		if ( ! summary ) return false;
		str += '<li>Correct: '+answers['t']+'</li>';
		str += '<li>Incorrect: '+answers['f']+'</li>';
		str += '<li>Unanswered: '+answers['u']+'</li>';

		$$('#'+summaryId+' ul')[0].update(str);
		summary.show();
		window.location="#"+summaryId;
		return true;
	}


function toggleClassName(elem,toggleToClass) {
	//if ( typeof toggleToClass == 'undefined' ) toggleToClass = '';
	elem = $(elem);
	if (typeof elem.origClassName == 'undefined' )
		elem.origClassName = elem.className;

	elem.className = (toggleToClass ? toggleToClass+' ' : '') + elem.origClassName ;
	elem.className.replace(/\s+$/,'');
	elem.show();

}

