/* Copyright © 2001 PureWorks, Inc.  All Rights Reserved. */



/*

The following functions are available to AICCize this content to work in 
any LMS.  Each of these functions can be implemented in any way you wish, so
long as you adhere to the following standards:

-all module level variables must have the prefix AICC_
-all function names must begin with the prefix AICC_
-all existing functions must be present and implement the specified interface and return values


you also have access to a function called WriteToDebug that will write debug information to 
the JavaConsole
*/



var objAICCStandard = new Object;
	
objAICCStandard.Initialize = AICC_Initialize;
objAICCStandard.Finish = AICC_Finish;
objAICCStandard.CommitData = AICC_Commit;
objAICCStandard.SetBookmark = AICC_SetBookmark;
objAICCStandard.GetBookmark = AICC_GetBookmark;
objAICCStandard.GetLastError = AICC_GetLastError;
objAICCStandard.GetErrorDescription = AICC_GetErrorDescription;

var AICC_Error = new String();
var AICC_ErrorDescription = new String();
var AICC_Version = new String();
var AICC_GetParamCommand = new String();
var AICC_PutParamCommand = new String();
var AICC_ExitAUCommand = new String();

AICC_Version = "1.0";
AICC_GetParamCommand = "GETPARAM";
AICC_PutParamCommand = "PUTPARAM";
AICC_ExitAUCommand = "EXITAU";

// AICC level 1 compliance variables.
var AICC_Student_ID = new String();
var AICC_Student_Name = new String();
var AICC_SOutput_file = new String();
var AICC_Path = new String();
var AICC_Lesson_Location = new String();
var AICC_Lesson_Status = new String();
var AICC_Score = new String();
var AICC_Credit = new String();
var AICC_Time = new String();
var AICC_Core_Lesson = new String();
var AICC_Core_Vendor = new String();
var AICC_Mastery_Score = new String();

AICC_Student_ID = "";
AICC_Student_Name = "";
AICC_Output_file = "";
AICC_Path = "";
AICC_Lesson_Location = "";
AICC_Lesson_Status = "";
AICC_Score = "";
AICC_Credit = "";
AICC_Time = "";
AICC_Core_Lesson = "";
AICC_Core_Vendor = "";
AICC_Mastery_Score = "";
	
/*
Initialize is called when the content is loaded.
Purpose: perform any initialization necessary
Parameters: none
Return value: boolean indicating success or failure
*/	
function AICC_Initialize(){
	WriteToDebug("AICC_Initialize");
	return AICC_GetParam();
}



/*
Finish is called from 3 places
  1. When the page containing the content is unloaded
  2. When the user elects to "Save and Exit"
  3. When the content is complete and the user clicks "OK"
Purpose: perform any task necessary when the user exits
Parameters: 
	blnReachedEnd (boolean) true = user reached the last slide
							false = user did not reach the last slide
	
	strExitType (string, defined by constant exit type) 
				EXIT_TYPE_UNLOAD = "UNLOAD" - this value is used in Finish scenerio 1
				EXIT_TYPE_PUNT = "PUNT" - this value is used in Finish scenerio 2
				EXIT_TYPE_FINISHED = "FINISHED" - this value is used in Finish scenerio 3

Return value: boolean indicating success or failure
*/
function AICC_Finish(blnReachedEnd, strExitType){

	WriteToDebug("AICC_Finish");

	WriteToDebug("Exit type = " + strExitType);	
	
	if (strExitType == EXIT_TYPE_FINISHED)
	{	
		if (AICC_PutParam(true)){
			AICC_ExitAU();
			return true;
		}
		else{
			return false;
		}
	}
	else
	{
		AICC_ExitAU();
		return true;
	}
}


/*
SetBookmark is called periodically throughout the content
Purpose: Save the user's current location in the content
Parameters: strBookmark (String < 500 characters)
Return value: boolean indicating success or failure
*/
function AICC_SetBookmark(strBookmark){

	WriteToDebug("AICC_SetBookmark");
	
	AICC_Lesson_Location = strBookmark;
	
	return AICC_PutParam(false);
}



/*
GetBookmark is called when the content loads
Purpose: Retrieve the user's last location in the content
Parameters: none
Return value: String < 500 characters, that was set in the last call to SetBookmark
*/
function AICC_GetBookmark(){

	WriteToDebug("AICC_GetBookmark");	
	
	return AICC_Lesson_Location;
}


/*
GetLastError is called periodically, usually after another call
Purpose: Determine if an error has occured
Parameters: none
Return value: String representing an error number that should be recorded for debug, "0" indicates no error
*/
function AICC_GetLastError(){

	WriteToDebug("AICC_GetLastError");	
	
	return AICC_Error;
}


/*
GetErrorDescription is called after a call to GetLastError that indicates an error
Purpose: Get additional information about the error for debug
Parameters: none
Return value: String containing information about the last error
*/
function AICC_GetErrorDescription(){

	WriteToDebug("AICC_GetErrorDescription");	
	
	return AICC_ErrorDescription;
}



/*
Commit is called periodically 
Purpose: Make sure that any bookmarks are permanently stored in the LMS
Parameters: none
Return Value: boolean indicating success or failure
*/
function AICC_Commit(){

	WriteToDebug("AICC_Commit");	
	
	return true;
}



/*
AICC_GetParam must be the first call and may be called periodically throughout the content.
Purpose: Get information from the LMS to initialize the content.
Parameters: none
Return value: boolean indicating success or failure
*/
function AICC_GetParam(){

	WriteToDebug("AICC_GetParam");

	var strSessionID = new String();
	var strURL = new String();
	var strGetParamData = new String();
	var objGetParam = new Object();
	
	// get Session ID and the post URL
	strSessionID = unescape(GetQueryStringValue("aicc_sid", window.location.search));
	strURL = unescape(GetQueryStringValue("aicc_url", window.document.location.search));
	
	// build the form
	ClearForm();
	AddFormElement("command",AICC_GetParamCommand);
	AddFormElement("version",AICC_Version);
	AddFormElement("session_id",strSessionID);
	AddFormElement("aicc_data","");
	
	// submit the form
	strGetParamData = SubmitForm(window.document.location.host, strURL);
	objGetParam = AICC_ParseAICCData(strGetParamData);

	// validate the response from the LMS
	if (AICC_ValidateLMSResponse(objGetParam))
	{
		// set all parameters returned from GetParam
		AICC_SetAllParams(objGetParam);
		return true;
	}	
	else
		return false;
	
	
} // end AICC_GetParam



/*
AICC_PutParam is called periodically throughout the content.
Purpose: Put or send information to the LMS about the progress of the content.
Parameters: blnFinish (indicates content should be recorded as completed)
Return value: boolean indicating success or failure
*/
function AICC_PutParam(blnFinish){

	WriteToDebug("AICC_PutParam");
	
	var strAICC_Data = new String();
	var strCRLF = new String();
	var strSessionID = new String();
	var strFinish = new String();
	var strURL = new String();
	var strPutParamData = new String();
	var objPutParam = new Object();
		
	strAICC_Data = "";
	strCRLF = "\r\n";
	strPutParamData = "";
	strStatus = "I";
	
	// When the content is complete and the user clicks "OK", set the status to complete (C).  
	// Else, set to the default, Incomplete(I).
	if (blnFinish)
		strStatus = "C";
		
	// build the AICC data variable
	strAICC_Data = "[Core]";
	strAICC_Data = strAICC_Data + strCRLF + "Lesson_Location=" + AICC_Lesson_Location;
	strAICC_Data = strAICC_Data + strCRLF + "lesson_status=" + strStatus;
	strAICC_Data = strAICC_Data + strCRLF + "score=";
	strAICC_Data = strAICC_Data + strCRLF + "time=" + AICC_Time;
	strAICC_Data = strAICC_Data + strCRLF + "[Core_Lesson]" + strCRLF;

	// get Session ID and the post URL
	strSessionID = unescape(GetQueryStringValue("aicc_sid", window.location.search));
	strURL = unescape(GetQueryStringValue("aicc_url", window.document.location.search));
	
	// build the form
	ClearForm();
	AddFormElement("command",AICC_PutParamCommand);
	AddFormElement("version",AICC_Version);
	AddFormElement("session_id",strSessionID);
	AddFormElement("aicc_data",strAICC_Data);
	WriteToDebug("Data Sent in PutParam = " + strAICC_Data);

	// submit the form
	strPutParamData = SubmitForm(window.document.location.host, strURL);
	objPutParam = AICC_ParseAICCData(strPutParamData);
	
	// validate the response from the LMS
	return AICC_ValidateLMSResponse(objPutParam);
} // end AICC_PutParam

/*
AICC_ExitAU is the last call made from the content.
Purpose: Signals to the LMS that the content is exiting.
Parameters: none
Return value: boolean indicating success or failure
*/
function AICC_ExitAU()
{
	var strSessionID = new String();
	var strURL = new String();
	var strExitAUData = new String();
		
	strAICC_Data = "";
	strCRLF = "\r\n";
	strExitAUData = "";
	
	// get Session ID and the post URL
	strSessionID = unescape(GetQueryStringValue("aicc_sid", window.location.search));
	strURL = unescape(GetQueryStringValue("aicc_url", window.document.location.search));
	
	// build the form
	ClearForm();
	AddFormElement("command",AICC_ExitAUCommand);
	AddFormElement("version",AICC_Version);
	AddFormElement("session_id",strSessionID);

	// submit the form
	strExitAUData = SubmitForm(window.document.location.host, strURL);

	// Always return true.
	return true;

} // end AICC_ExitAU


/*
AICC_ParseAICCData is called from GetParam and PutParam.
Purpose: Extracts parameters returned from GetParam and PutParam.
Parameters: strAICCData (raw data returned from LMS)
Return value: objAICCData (object of name/value pairs)
*/
function AICC_ParseAICCData(strAICCData) 
{
	WriteToDebug("AICC_ParseAICCData");

	strAICCData = new String(strAICCData);

	var strCRLF = "\n";
	var pairs = strAICCData.split(strCRLF);
		
	var objAICCData = new Object();
	for (var i = 0; i<pairs.length; i++) 
	{
		var pos = pairs[i].indexOf("=");
		if (pos>-1) 
		{
			var argname = pairs[i].substring(0, pos).toLowerCase();
			var value = unescape(pairs[i].substring(pos+1));
			
			// strip carriage returns from end of value 
			if (value.charCodeAt(value.length - 1) == 13)
				value = value.slice(0,value.length - 1)
			
			objAICCData[argname] = value;		
			WriteToDebug(argname + " = " + value);
		}
	}
	
	return objAICCData;
} // end AICC_ParseAICCData

/*
AICC_ValidateLMSResponse is called from GetParam and PutParam.
Purpose: Determines if the response from the LMS.
Parameters: objAICCData (object of name/value pairs)
Return value: boolean indicating success or failure
*/
function AICC_ValidateLMSResponse(objAICCData)
{
	WriteToDebug("AICC_ValidateLMSResponse");
	
	// validate the error code
	if(objAICCData["error"] == null)
	{
		AICC_Error = "40000";
		
		AICC_ErrorDescription = "No error code returned from LMS.";
		
		WriteToDebug("Failed response.  No error code returned from LMS.");
		
		return false;
	}
	
	// check for failed response
	if(objAICCData["error"] != "0")
	{
		AICC_Error = objAICCData["error"];
		
		WriteToDebug("Failed response.  Error = " + AICC_Error);
		
		if(objAICCData["error_text"] != null)
			AICC_ErrorDescription = "";
		else
			AICC_ErrorDescription = objAICCData["error_text"];

		return false;
	}
	
	WriteToDebug("Successful response.  Error=0");
	
	// successful return
	AICC_Error = objAICCData["error"];
	
	AICC_ErrorDescription = "";
	
	return true;

} // end AICC_ValidateLMSResponse


/*
AICC_SetAllParams is called from GetParam.
Purpose: Sets global AICC variables returned from GetParam.
Parameters: objAICCData (object of name/value pairs)
Return value: boolean indicating success or failure
*/
function AICC_SetAllParams(objGetParam)
{

	WriteToDebug("AICC_SetAllParams");

	/*	Most of these params are not used.
		Set for future use if necessary. */		

	if(objGetParam["student_id"] == null)
		AICC_Student_ID = "";
	else
		AICC_Student_ID = objGetParam["student_id"];

	if(objGetParam["student_name"] == null)
		AICC_Student_Name = "";
	else
		AICC_Student_Name = objGetParam["student_name"];

	if(objGetParam["output_file"] == null)
		AICC_Output_file = "";
	else
		AICC_Output_file = objGetParam["output_file"];

	if(objGetParam["lesson_location"] == null)
		AICC_Lesson_Location = "";
	else
		AICC_Lesson_Location = objGetParam["lesson_location"];

	if(objGetParam["lesson_status"] == null)
		AICC_Lesson_Status = "";
	else
		AICC_Lesson_Status = objGetParam["lesson_status"];

	if(objGetParam["score"] == null)
		AICC_Score = "";
	else
		AICC_Score = objGetParam["score"];

	if(objGetParam["credit"] == null)
		AICC_Credit = "";
	else
		AICC_Credit = objGetParam["credit"];
	
	// default time if no value returned.
	if(objGetParam["time"] == null)
		AICC_Time = "00:00:00";		
	else
	{
		AICC_Time = objGetParam["time"];
		if (AICC_Time == "")
			AICC_Time = "00:00:00";	
	}

	if(objGetParam["core_lesson"] == null)
		AICC_Core_Lesson = "";
	else
		AICC_Core_Lesson = objGetParam["core_lesson"];
		
	if(objGetParam["core_vendor"] == null)
		AICC_Core_Vendor = "";
	else
		AICC_Core_Vendor = objGetParam["core_vendor"];

	if(objGetParam["mastery_score"] == null)
		AICC_Mastery_Score = "";
	else
		AICC_Mastery_Score = objGetParam["mastery_score"];				
	
	return true;
} // AICC_SetAllParams

// End Standard-AICC
