/*************************************************************************
 ** Copyright Knowledge Window Inc. 2002
 ** 
 ** This is the wrap JavaScript for AICC HACP based course LMS 
 **  communications.
 **
 ** created: 12/1/2000
 ** Modified: 5/20/2002 - Andy Li
 **
 ** Revision: 5/20/2002 - support AICC 3.5 HACP communication
 **
 **
 ** note: 
 **     In order to make iFrame to contain the text/plain file sent by LMS,
 **	the following registry may need to be added:
 **     HKEY_Current_User\Software\Windows\Internet Setting\IsTextPlainHonored=1
 **     (DWORD)
 **
 ** $TODO: This set of function shall be moved into LMS_API.inc
 **
 ** Owner: Andy Li
 **
 **
 ***************************************************************************/
 
 
 /****************************************************************************
  ** AICC required data
  ** [Core]
  **	Lesson_Location
  **	Lesson_Status
  ** 	Score
  ** 	Time
  ****************************************************************************/
  
  
 
 var m_straiccdata	= "";
 var m_strError		= "";
 var m_strErrorText	= "";
 var m_strVersion	= "1.0";  //default to AICC spec 1.0
 var m_strLessonLocation= "";
 var m_strLessonStatus  = "";
 var m_strScore         = "";
 var m_strTime          = "";
 
/**************************************************************************
 **
 ** Function to get parameter from LMS by calling the catcher
 **
 **************************************************************************/
function GetParam(){
	traceLog("Aicc - GetParam()" );
	window.frames("receiver").document.frames("GetParamFrame").document.all("GetParam").action=m_gstrAiccUrl;
	window.frames("receiver").document.frames("GetParamFrame").document.all("GetParam").session_id.value=m_gstrAiccSid;
	window.frames("receiver").document.frames("GetParamFrame").document.all("GetParam").version.value=1.0;
    	window.frames("receiver").document.frames("GetParamFrame").document.all("GetParam").aicc_data.value="";
   	window.frames("receiver").document.frames("GetParamFrame").document.forms("GetParam").submit();
   	setTimeout("parseParamValue()", HTTP_WAITING_TIME);
   	
 }

/***************************************************************************
 **
 ** function is called by LMS_API, LMSInitialize
 **
 ** $ TODO: will be depricated and move to LMS_API
 **
 ***************************************************************************/
function getAICCData()
{
	return m_straiccdata;
}


/*************************************************************************
 ** 
 ** Function to parse the returned LMS values
 **
 ** This function needs to be called after receiving http response
 **
 **
 ** AICC 3.5 Specification:   aicc_data must be the last of part of return
 **
 *************************************************************************/
function parseParamValue()
{
	traceLog ("Entering parseParamValue()");
   	m_strGetAICCData = window.frames("receiver").document.frames("AICCDataFrame").document.body.innerText;
	traceLog ("Get AICC Data: " + m_strGetAICCData);
	
	if ( m_strGetAICCData != "") 
	{
		m_strGetAICCData = m_strGetAICCData.toLowerCase();
		aicc_index = m_strGetAICCData.indexOf ("aicc_data");
		m_straiccdata = m_strGetAICCData.substring(aicc_index, m_strGetAICCData.length);
		m_strGetAICCData = m_strGetAICCData.substring(0, aicc_index-1);
		
		valArray = m_strGetAICCData.split(/\n/);
		for(var x=0; x < valArray.length;x++)
		{
			if(valArray[x].indexOf("error") > -1 && valArray[x].indexOf("error_text")==-1)
				m_strError = getValue(valArray[x]);
			if(valArray[x].indexOf("error_text") > -1)
				m_strErrorText = getValue(valArray[x]);
			if(valArray[x].indexOf("version") > -1)
				m_strVersion = getValue(valArray[x]);
		}
		
	}

	//if no return value from LMS at all
	if(m_strError == "")
		showErrorMsg("Failed to connect to LMS server", null);
	
	if ( m_strError != 0 )
	{
		showErrorMsg ("Content failed to connect to LMS. \n" + "Error: " + m_strErrorText, null);
	}

}

/**************************************************************************
 **
 ** Function to put parameter by calling LMS catcher
 **
 ** Refer to AICC CMI 3.5 specification for the data format.
 **
 ***************************************************************************/
function PutParam(ptype,parg,paction){
	
	traceLog ("Entering PutParam(" + ptype + "," + parg + "," + paction + ")");

	var error	="error=0"			+ "\r\n";
	var error_text	="error_text=Successful"	+ "\r\n";
	var version	="version="+ m_strVersion 	+ "\r\n";
	var core	="";


	if (ptype=="score")
		t_score=parg;
	if (ptype=="status")
		t_status=parg;

	if (t_score=="")
		t_score=0;
	if (t_status=="")
		t_status="complete";
		
 	
 	if (ptype == "aicc_data")
 	{
 		core = parg;
 	} else
 	{
		core ="aicc_data=[core]"		+ "\r\n";
		core = core + "Lesson_Location=End"	+ "\r\n";
		core = core + "Lesson_Status="+ t_status+ "\r\n";
		core = core + "Score="+t_score	   +"\r\n";
		core = core + "TIME="+getClock();
	}

	
	aicc_data=error+error_text+version+core;
	traceLog ("Message:" + aicc_data);
	
	if (paction==1){
		window.frames("receiver").document.frames("PutParamFrame").document.all("PutParam").action=m_gstrAiccUrl;
		window.frames("receiver").document.frames("PutParamFrame").document.all("PutParam").session_id.value= m_gstrAiccSid;
		window.frames("receiver").document.frames("PutParamFrame").document.all("PutParam").version.value=1.0;
		window.frames("receiver").document.frames("PutParamFrame").document.all("PutParam").aicc_data.value=aicc_data;
		window.frames("receiver").document.frames("PutParamFrame").document.forms("PutParam").submit();
	};
}


/***************************************************************************
 **
 ** Mandatory function for exiting AU.
 **
 ***************************************************************************/
function ExitAU(){
	traceLog("Entering ExitAU()");
	window.frames("receiver").document.frames("ExitAUFrame").document.all("ExitAU").action=m_gstrAiccUrl;
	window.frames("receiver").document.frames("ExitAUFrame").document.all("ExitAU").session_id.value= escape(m_gstrAiccSid);
	window.frames("receiver").document.frames("ExitAUFrame").document.all("ExitAU").version.value= escape("1.0");
	window.frames("receiver").document.frames("ExitAUFrame").document.forms("ExitAU").submit();
}

/*******************************************************************************************
 **
 ** Function to generate AICC format current time
 **
 ** return: 	current time in HH:MM:SS format
 **
 ** Deprated: 5/20/2002
 **
 ********************************************************************************************/
function getClock()
{
	var time = new Date()
	var hours = time.getHours()
	var minutes = time.getMinutes()
	var seconds = time.getSeconds()
	var clock = hours
	clock += ((minutes < 10) ? ":0" : ":") + minutes
	clock += ((seconds < 10) ? ":0" : ":") + seconds
	return clock;
}

/*******************************************************************************************
 **
 ** Function to find the value of the name/value pair, delimited by "="
 **
 ** return: 	value in string
 **             value may contain empty space, so needs to be trimmed before return.
 **
 ********************************************************************************************/

function getValue (pStr)
{
	traceLog ("Entering getValue( " + pStr + ")");
	eqIndex = pStr.indexOf("=");
	return trim(pStr.substring(eqIndex+1,pStr.length));
}

/***************************************************************************
 ** 
 ** Function to trim the string
 **
 ***************************************************************************/
function trim(str)
{
	if(str == "")
		return "";
	 var blankFound = true;
	 while(blankFound)
	{
		if(str.lastIndexOf(" ") == (str.length-1))
		{
			str = str.substring(0,str.length-1);
		}
		else if(str.indexOf(" ") == 0)
		{
			str = str.substring(1,str.length);
		}
		else
			blankFound = false;
	}
	return str;
 }