/* ----------------------------------------------------------------------------------------
File Name: Exercise_Init_Functions.js
'	The exercise interface is being shown.
'	Handles the "Check" and "Right answer", by calling the functions that handle these events in 
'	the right handler page.
'	"Clear" is being handled here generically.

	Written By:  Yonit Rusho
	Date:	     18 March 2003

 ----------------------------------------------------------------------------------------*/

var array_feedback
var first_time_loadingXML	  = true;
var wasChecked				  = false;	//For the right answer after check
var boolAfterCheck			  = false;	//For the Clear wrong answers after check
var boolAfterRightAnswer	  = false;	
var strUserAnswers			  = new Array()
var boolDistractorSelected	  = false;
var LastExerciseID;

/******************************************************************************************
 Function ShowExercise()

	Parameters: 

	Returns: ---
	
	Description: This function is being called when the XSLDOM Object finishes being loaded.
	First it add data from the Help XML	and data about the default_filter.
	Next it combines the XSL and the XML together and the tool is being shown.
	
	Written by: Yonit Rusho
	Date:		18 March 2003

	Modified by, on: 
*****************************************************************************************/
function ShowExercise()
{
	var state = oExerciseXsl.readyState;
	
	if(state == 4) //If dataIsComplete
	{
		var XSL_type = oExerciseXml.documentElement.selectSingleNode('.').getAttribute('type');

		//Don't allow feedback (check) for this exercise.
		var disableCheck = false;
		feedback = oExerciseXml.selectSingleNode("//Exercise/Context").getAttribute("feedback")
		
		if(feedback == 0)//Don't allow feedback (check) for this exercise.
			disableCheck = true;				
		else
			disableCheck = false;	
		//------------------------------------------------------------------------------------------
		
		if(first_time_loadingXML)
		{
			//Add the path to images, sounds and randomDistractors flag.y
			root			= oExerciseXml.documentElement;
			newAtt			= oExerciseXml.createAttribute("soundPath");
			namedNodeMap	= root.attributes;
			namedNodeMap.setNamedItem(newAtt);
			oExerciseXml.documentElement.selectSingleNode('.').setAttribute("soundPath",soundPath)
			
			newAtt			= oExerciseXml.createAttribute("imagePath");
			namedNodeMap	= root.attributes;
			namedNodeMap.setNamedItem(newAtt);
			oExerciseXml.documentElement.selectSingleNode('.').setAttribute("imagePath",imagePath)

			/*newAtt			= oExerciseXml.createAttribute("randomDists");
			namedNodeMap	= root.attributes;
			namedNodeMap.setNamedItem(newAtt);
			oExerciseXml.documentElement.selectSingleNode('.').setAttribute("randomDists",blRandomDistractors)
			*/
			//----------------------------  Instructions  ----------------------------
			
			//Create an "Instructions" tag in the XML DOM object.
			root			= oExerciseXml.documentElement;
			newElem			= oExerciseXml.createElement("Instructions");
			root.selectSingleNode("//Exercise").appendChild(newElem);
			
			//Insert CDATA in it:
			var InstructionsText = "", counter = 0;
			arr_instructionsAllLanguages = oInstructionsXml.selectNodes("//Instructions/Logic[@type = '" + XSL_type.toUpperCase() + "']")
			
			for(i=0; i < arr_instructionsAllLanguages.length; i++)
			{
				if(arr_instructionsAllLanguages[i].getAttribute("language") == userLanguage)
					InstructionsText = arr_instructionsAllLanguages[i].text
				else
					counter++
			}
			
			counter2 = 0;
			if(counter == arr_instructionsAllLanguages.length)//if this language doesn't exist
				for(i=0; i < arr_instructionsAllLanguages.length; i++)
					if(arr_instructionsAllLanguages[i].getAttribute("language") == defaultLanguage)
					{
						InstructionsText = arr_instructionsAllLanguages[i].text
						break;
					}
					else
						counter2++;
			
			if(counter2 == arr_instructionsAllLanguages.length)//if this default language doesn't exist
				for(i=0; i < arr_instructionsAllLanguages.length; i++)
					if(arr_instructionsAllLanguages[i].getAttribute("language") == SecondDefaultLanguage)
					{
						InstructionsText = arr_instructionsAllLanguages[i].text
						break;
					}					
						
			CDATASection	= oExerciseXml.createCDATASection(InstructionsText);
			newElem.appendChild(CDATASection);
			//alert(oExerciseXml.xml)
			
			//-------------------	Localization	---------
			//Create a "Localization" tag in the XML DOM object.
			if(oLocalizationXml.selectSingleNode("//Translations/" + userLanguage + "/ExerciseComponent"))//if this language exists
				LocalizationEl = oLocalizationXml.selectSingleNode("//Translations/" + userLanguage + "/ExerciseComponent").xml
			else
				LocalizationEl = oLocalizationXml.selectSingleNode("//Translations/" + defaultLanguage + "/ExerciseComponent").xml
			
			var xmlDom = new ActiveXObject("Microsoft.XMLDOM");
			xmlDom.loadXML(LocalizationEl)
			
			oExerciseXml.selectSingleNode("//Exercise").appendChild(xmlDom.documentElement);
			//alert(oExerciseXml.xml)
			
			first_time_loadingXML = false;
		}
		
		//----------------------------
		
		//reset
		document.frames("ifr_ExerciseInterface").document.close()
		document.frames("ifr_ExerciseInterface").document.write("")
		//------------------------------------------------------------------------------------------
		//enable Check and Right Answer buttons only after the exercise has being loaded.

		if(!disableCheck && XSL_type != "CST" && XSL_type != "TYT")//Enable Check and Right Answer (except from: DMS, SR1, DR1, TYT).
		{
			enable_RightAnswer_Check_btns(1)
		}
		
		else if(XSL_type == "TYT")//Enable only Right Answer and leave Check disabled.
			enable_RightAnswer_Check_btns(2)
		
		else if(XSL_type == "CST")//Enable only Check and leave Right Answer disabled.
			enable_RightAnswer_Check_btns(3)
		
		//Deside on SoundHandling source.
		if(XSL_type == "DR1")
		{
			//Get required soundHandling script page.
			document.all.SoundHandling.src = soundHandlingPath + "SoundHandling_DR1.js"
		}
		else
			document.all.SoundHandling.src = soundHandling_DR1Path + "SoundHandling.js"
		
		if(document.all.btn_Clear)
			document.all.btn_Clear.disabled = false
		if(document.all.btn_Next)
			document.all.btn_Next.disabled = false
		
		tmp_ClearUserWrongAnswersAfterCheck = ClearUserWrongAnswersAfterCheck;
		if(XSL_type == "CST" || XSL_type == "TYT") //There is no right/ wrong answer, so ClearUserWrongAnswersAfterCheck is not implemented
			ClearUserWrongAnswersAfterCheck = false;
		else //leave it as is.
			ClearUserWrongAnswersAfterCheck = tmp_ClearUserWrongAnswersAfterCheck;
		
		wasChecked = false //to know that the user didn't click on "Check".
		
		// Fill the Exercise IFrame with the result of the transform
		document.frames("ifr_ExerciseInterface").document.write(oExerciseXml.transformNode(oExerciseXsl))
		
		//Set loalization variables and set tooltips with these values.
		initToolTips(XSL_type.toUpperCase())
		
		if(blRandomDistractors)
			fnRandomEngine()
		
	}
}



/******************************************************************************************
 Function EventsHandler(btn_type)

	Parameters: 
		number btn_type: 1->Check ; 2->Right answer

	Returns: ---
	
	Description: This function is being called when the "Check", "Clear" or " Right answer" are clicked.
	Calls the functions that handle these events in the right handler page.
	"Clear" is partly being handled here generically.
		
	Written by: Yonit Rusho
	Date:		27 May 2002

	Modified by, on: 
*****************************************************************************************/
function EventsHandler(btn_type)
{
	if(btn_type == 1) // Check
	{
		var XSL_type = oExerciseXml.documentElement.selectSingleNode('.').getAttribute('type')
		boolAfterRightAnswer = false;
		
		if(ClearUserWrongAnswersAfterCheck)
		{
			//Save user's answer before Check. The String will be used after the Clear to show the user's right answers.
			strUserAnswers = UserAnswerEngine_SaveState();
			boolAfterCheck		= true;
			boolDistractorSelected	= false;//Another button was selected after the distractor
		}
		
		array_feedback = CheckExercise() //There is a function in that name in every logic handler page.
		
		if(RighAnswerAfterCheck && XSL_type != "CST")//Enable RightAnswer if this logic type has a rightAnswer functionality
		{
			wasChecked = true; //to know that the user clicked on "Check".
			ShowRightAnswerButton(1)//Show RightAnswer image/button
		}
		//alert(array_feedback)
	}
	
	else if(btn_type == 2) // Right answer
	{
		boolAfterRightAnswer = true;
		
		if(ClearUserWrongAnswersAfterCheck)
		{
			boolDistractorSelected	= false;//Another button was selected after the distractor
			boolAfterCheck = false; //to know that the user clicked on "Right answer".
		}
		
		if((RighAnswerAfterCheck && wasChecked) || (!RighAnswerAfterCheck))
			RightanswerExercise()
	}
	
	else if(btn_type == 3) // Clear answer
	{
		boolAfterRightAnswer = false;
		
		if(RighAnswerAfterCheck)
		{
			wasChecked = false; //to know that the user didn't click on "Check".
			ShowRightAnswerButton(2)//Hide RightAnswer image/button
		}
		ClearExercise()
	}
}


/******************************************************************************************
 Function ClearExercise()
	Description: This function clears all answers.
	It's generic to all logics.
	Clear not after a Check clears everything.
	After a Check it clears only wrong answers.
*****************************************************************************************/
function ClearExercise()
{
	var type = oExerciseXml.documentElement.selectSingleNode('.').getAttribute('type');

	//REDECLERATION OF VARIABLES & ARRAYS
	if(type == "DMS" || type == "SR1")
	{
		var MPlayer_SoundSrc_DR1		= "" //holds the path to next sound to be played
		var MPlayer_SoundSrc		= "" //holds the path to next sound to be played
		var SoundCounter_next	= 0 //a global variable

		for(i=0; i < sound_El_Array.length; i++)
			sound_El_Array[i] = ""
		for(i=0; i < soundArray.length; i++)
			soundArray[i] = ""
		for(i=0; i < RecordedArray.length; i++)
			RecordedArray[i] = ""
		var Chosen_row			= 0 //holds the chosen row -> SR1
	} 
	else if(type == "DR1")
		part = "none"			//holds the chosen part of speech -> DR1

	//if(oExerciseXml.documentElement.selectSingleNode('.').getAttribute('type') == "HST")
		//HotSpot_Dictionary_obj.RemoveAll();       // Clear the dictionary.
	
	//--------   --------   --------   --------   --------   --------   -------- 
	
	//1. Delete all
	document.frames("ifr_ExerciseInterface").document.close()
	document.frames("ifr_ExerciseInterface").document.write("")

	//2. Reload
	ShowExercise()

	//3. Display user's right answers.
	if(ClearUserWrongAnswersAfterCheck && boolAfterCheck == true && !boolDistractorSelected)//After a check and not after a distractor was selected.
	{
		//alert(strUserAnswers)
		DisplayUserRightAnswers(strUserAnswers) //This function is different for each logic and is implemented in the handlers page.
		boolDistractorSelected	= false;
	}
}


// ----------------------------------------------------------------------------------------

