/* ----------------------------------------------------------------------------------------
File Name: UserAnswerFunctions.js

	This file holds functions that are being called from the outside 
	and general functions. They handle both Save State and Display State modes.
	
	Written By:  Yonit Rusho
	Date:	     15 June 2003

 ----------------------------------------------------------------------------------------*/


/******************************************************************************************
	Description:
		This function calles functions from the logic's script page handler
		and global functions.
		It must be called before the Display State mode, when the interface still
		holds the user's answer.
	Returns: arrUserAnswer as a String, that the Display State mode will display.
*****************************************************************************************/
function UserAnswerEngine_SaveState()
{
	var arrUserAnswer = new Array()
	var strUserAnswer; //holds the String with the Objects' variables.
	
	CreateObject()
	
	arrUserAnswer = InitObjects_Arraying() //With values based on the user's answer.
	
	strUserAnswer = ConvertToString(arrUserAnswer)
	
	//alert("Return: " + strUserAnswer)
	
	return strUserAnswer
}


/******************************************************************************************
	Parameters: arrUserAnswer- The String that had been created by the Save State mode.
	
	Description:
		This function calles functions from the logic's script page handler
		and global functions.
		Must be called after the Save State mode, so strUserAnswer will
		hold the user's answer to be displayed.
*****************************************************************************************/
function UserAnswerEngine_DisplayState(strUserAnswer)
{
	//alert("Get: " + strUserAnswer)
	
	var arrUserAnswer = new Array()
	
	arrUserAnswer = ConvertFromString(strUserAnswer)
	
	CreateObject()
	
	InitObjects_DisplayUserAnswer(arrUserAnswer)
}




//*************************************************************************/
//*************************************************************************/

//-------- GLOBAL VARIABLES -----------------------
var objUserAnswer;


//-------- GLOBAL FUNCTIONS ---------------------

/******************************************************
Description: This function creates one global instance of the Object.
*******************************************************/
function CreateObject()
{
	var strXmlType = oExerciseXml.documentElement.selectSingleNode('.').getAttribute('type')
	strXmlType = strXmlType.toUpperCase()
	
	//JScript Inheritance
	eval("objUserAnswer_" + strXmlType + ".prototype = new clsUserAnswer('" + strXmlType + "')")
	
	//Initialize Object
	eval("objUserAnswer = new objUserAnswer_" + strXmlType + "()")
}


/******************************************************
Description: This function gets an Array, converts it to one String and returns it.
*******************************************************/
function ConvertToString(arrUserAnswer)
{
	var strUserAnswer;
	
	strUserAnswer = arrUserAnswer.join("|");
	
	return strUserAnswer
}


/******************************************************
Description: This function gets a String, converts it back to an Array and returns it.
*******************************************************/
function ConvertFromString(strUserAnswer)
{
	arrConvertedFromString = strUserAnswer.split("|")

	return 	arrConvertedFromString
}


/******************************************************
Description: This function gets 2 Arrays: an empty one and one with Strings.
			 It puts Objects with data from arrUserAnswer's Strings 
			 in arrInstances Array and returns it. 
			 This function is being called Handlers pages.
*******************************************************/
function InitFromArray(arrInstances, arrUserAnswer)
{
	var intCountChecked = 0;
	
	for(i=1; i<arrUserAnswer.length; i++)//first is the logic type, so it starts from 1.
	{
		zugi = i%2;
		if(zugi != 0)
		{
			CreateObject()
			arrInstances[intCountChecked] = objUserAnswer;         //put an uninitialized instance in the array.
			arrInstances[intCountChecked].setAll(arrUserAnswer[i],arrUserAnswer[i+1]);//Object
			intCountChecked ++;
		}
	}
	
	return arrInstances
}
