/********************************************************************************
* Filename		: NewScripts													*
* Date			: 6-Sep-09													  	*
* Author		: Harman Dhillon, www.harmandhillon.com							*
* Description	: This JS file contains new code for dynamic functionalities 	*
*				  introduced for the StudySmarter website						*
* Version	Date		Author			Comments							  	*
* 1.0		6-Sep-09	Harman Dhillon  Initial file created				  	*
*********************************************************************************/
/********************************************************************************
* Function		: toggleDisplay													*
* Description	: Toggles the style=display value between 'none' and 'block' 	*
*				  for a given div element 										*
* Arguments		:										  				  		*
*	divID		: The 'id' of the div element which needs to be toggled			*
*********************************************************************************/
function toggleDisplay(divID){
	var ans_DIV = document.getElementById(divID);

	if (ans_DIV.style.display == "none") ans_DIV.style.display = "block";
	else if (ans_DIV.style.display == "block") ans_DIV.style.display = "none";
}

/********************************************************************************
* Function		: trim															*
* Description	: Trims the leading and training white space characters for the *
*				  passed string													*
* Arguments		:										  				  		*
*	str			: The input string to be trimmed								*
* Return		: The trimmed string											*
*********************************************************************************/
function trim(str)
{
	return str.replace(/^\s+|\s+$/g,'');
}


/********************************************************************************
* Function		: ans_pop														*
* Description	: This function can be used to have a multiple choice questions *
*				  (MCQ) with response for each question. This code is the 		*
*				  engine (logic) for running the dynamic response part. To 		*
*				  drive this engine, the data set needs to be also defined (an 	*
*				  associative array) - Answers[RadioName]. Here Answers is the 	*
*				  name of the array (should always be named 'Answers'), and 	*
*				  RadioName is the value of the radio button clicked. For 		*
*				  example check the following link:								*
*					http://www.studentservices.uwa.edu.au/ss/learning/ahss		*
* Arguments		:										  				  		*
*	parentform	: The Parent form of the radio button selected					*
*********************************************************************************/
function ans_pop(parentform) {
    for (var i=0; i<parentform.RadioName.length; i++) {
        if (parentform.RadioName[i].checked == true) {
        	parentform.Response.value = Answers[parentform.RadioName[i].alt];
        } 
     }
 }

/********************************************************************************
* Function		: validateAns													*
* Description	: This function should be used wherever self correcting quiz is *
*				  required. This code is the engine (logic) for running the 	*
*				  dynamic response part. To drive this engine, the data set 	*
*				  needs to be also defined - 4 arrays as follows:				*
*					AnsTryCount - storing the count of number of attempts made 	*
*						for each question										*
*					AnsKeywords - Associative array strong the keywords for 	*
*						correct answer											*
*					Feedback_Correct - Associative array storing feedback to be *
*						given is answer entered is correct.						*
*					Feedback_Wrong - Associative array storing feedback to be 	*
*						given is answer entered is wrong.						*
*																				*
*				  For a running example, please check the following link:		*
*  http://www.studentservices.uwa.edu.au/ss/learning/law/analysing_the_question	*
*																				*
* Arguments		:										  				  		*
*	parentform	: textarea with the answer given by the user					*
*********************************************************************************/
function validateAns(textbox){
	var match =false;
	
	// Increment the ans try counter
	if (!AnsTryCount[textbox.name])AnsTryCount[textbox.name] = 0; // declare if the variable doesnt exist
	AnsTryCount[textbox.name]++; // increment by one

	// Check if the answer matches the input string
	for (var i=0; i<AnsKeywords[textbox.name].length && (!match); i++){
		if (trim(textbox.value.toLowerCase()) == AnsKeywords[textbox.name][i].toLowerCase()) match = true; 	
	}
	
	// If there is a match, OR the user is trying for the 3rd time, show the correct answer
	if (match|| AnsTryCount[textbox.name]>2){
		var result = "";
		// If the user still got it wrong, let him know his answer is incorrect
		if (!match) 
			result = "Your answer is not correct. The correct answer is : <br/>";
		
		// show the correct answer feedback
		if (Feedback_Correct[textbox.name]){
			result += "<span style=\"color:green\">" + Feedback_Correct[textbox.name]+ "</span>";
		}
		else{
			// Correct answer feedback is not defined, show the default feedback
			result += "<span style=\"color:green\">Correct Answer</span>";
		}
	}
	else{
		var result = "";

		// Throw the incorrect answer feedback
		if (Feedback_Wrong[textbox.name]){
			result = "<span style=\"color:red\">" + Feedback_Wrong[textbox.name]+ "</span>";
		}
		else{
			// Incorrect answer feedback is not defined, show the default feedback
			result = "<span style=\"color:red\">Incorrect Answer. Please try again.</span>";
		}
	}

	// Print the feedback
	document.getElementById("Feedback_"+textbox.name).innerHTML = result; 
}
