/* Copyright © 2001 PureWorks, Inc.  All Rights Reserved. */


var SCORM_LOGOUT = "logout"
var SCORM_SUSPEND = "suspend"
var SCORM_NORMAL_EXIT = ""
var SCORM_PASSED = "passed"
var SCORM_COMPLETED = "completed"
var SCORM_BROWSED = "browsed"
var SCORM_INCOMPLETE = "incomplete"
var SCORM_CREDIT = "credit"
var SCORM_NO_CREDIT = "no-credit"
var SCORM_BROWSE = "browse"
var SCORM_NORMAL = "normal"
var SCORM_REVIEW = "review"
var SCORM_NO_ERROR = "0"


var objSCORMStandard = new Object;
	
objSCORMStandard.Initialize = SCORM_Initialize;
objSCORMStandard.Finish = SCORM_Finish;
objSCORMStandard.CommitData = SCORM_Commit;
objSCORMStandard.SetBookmark = SCORM_SetBookmark;
objSCORMStandard.GetBookmark = SCORM_GetBookmark;
objSCORMStandard.GetLastError = SCORM_GetLastError;
objSCORMStandard.GetErrorDescription = SCORM_GetErrorDescription;
	

var SCORM_findAPITries = 0;
var SCORM_objAPI;


function SCORM_Initialize(){
	
	var intError;
	var blnResult;
	
	SCORM_objAPI = SCORM_getAPI();

	if (typeof(SCORM_objAPI) == "undefined"){
		WriteToDebug("Unable to acquire SCORM API:")
		WriteToDebug("SCORM_objAPI=" + typeof(SCORM_objAPI));
		
		alert("Error - unable to acquire LMS API, content may not play properly and results may not be recorded.  Please contact technical support.");
		return false;
	}
	
	SCORM_objAPI.LMSInitialize("");
	intError = SCORM_objAPI.LMSGetLastError();
	intError = intError + "";	
	
	if (intError != SCORM_NO_ERROR){
		WriteToDebug ("Error calling LMSInitialize:");
		WriteToDebug ("              intError=" + intError);
		WriteToDebug ("              ErrorString=" + SCORM_objAPI.LMSGetErrorString(intError));
		WriteToDebug ("              Diagnostic=" + SCORM_objAPI.LMSGetDiagnostic(intError));
		
		alert("Error initializing communications with LMS");
		return false;
	}	

	if (SCORM_IsContentInBrowseMode()){
		blnResult = SCORM_CallLMSSetValue("cmi.core.lesson_status", SCORM_BROWSED);
	}
	else{
		blnResult = SCORM_CallLMSSetValue("cmi.core.lesson_status", SCORM_INCOMPLETE);
	}
	
	return true;
}



function SCORM_Finish(blnReachedEnd, strExitType){

	var strStatusAfterCompletion;
	var intError;
	
	if (blnReachedEnd){
			
		strStatusAfterCompletion = SCORM_GetCompletionStatus();
		
		SCORM_CallLMSSetValue("cmi.core.lesson_status", strStatusAfterCompletion);
	}

	SCORM_CallLMSSetValue("cmi.core.exit", SCORM_TranslateExitTypeToSCORM(strExitType));
	SCORM_Commit();
			
	SCORM_objAPI.LMSFinish("");
				
	intError = SCORM_objAPI.LMSGetLastError()
	intError = intError + "";
	
	if (intError != SCORM_NO_ERROR){
		WriteToDebug ("Error calling LMSFinish:");
		WriteToDebug ("              intError=" + intError);
		WriteToDebug ("              ErrorString=" + SCORM_objAPI.LMSGetErrorString(intError));
		WriteToDebug ("              Diagnostic=" + SCORM_objAPI.LMSGetDiagnostic(intError));
		return false;
	}
	
	return true;

}


function SCORM_SetBookmark(strBookmark){
	SCORM_CallLMSSetValue("cmi.core.lesson_location", strBookmark);
}

function SCORM_GetBookmark(){
	return SCORM_CallLMSGetValue("cmi.core.lesson_location");
}

function SCORM_GetLastError(){
	return SCORM_objAPI.LMSGetLastError() + ""
}

function SCORM_GetErrorDescription(){
	return SCORM_objAPI.LMSGetErrorString(SCORM_objAPI.LMSGetLastError()) + "\n" & SCORM_objAPI.LMSGetDiagnostic();
}


function SCORM_Commit(){
	var intError
	
	SCORM_objAPI.LMSCommit("")
	
	intError = SCORM_objAPI.LMSGetLastError()
	intError = intError + "";
	
	if (intError != SCORM_NO_ERROR){
		WriteToDebug ("Error calling LMSCommit:");
		WriteToDebug ("              intError=" + intError);
		WriteToDebug ("              ErrorString=" + SCORM_objAPI.LMSGetErrorString(intError));
		WriteToDebug ("              Diagnostic=" + SCORM_objAPI.LMSGetDiagnostic(intError));
		return false;
	}
	
	return true;
}


function SCORM_TranslateExitTypeToSCORM(strExitType){
	
	switch (strExitType){
		case EXIT_TYPE_PUNT:
			return SCORM_SUSPEND;
		break;
		case EXIT_TYPE_UNLOAD:
			return SCORM_NORMAL_EXIT;
		break;
		case EXIT_TYPE_FINISHED:
			return SCORM_LOGOUT;
		break;
	}
}


function SCORM_GetCompletionStatus(){
		
	var strCredit
	
	if (SCORM_IsContentInBrowseMode()){
		return SCORM_BROWSED;
	}
	else{
		strCredit = SCORM_CallLMSGetValue("cmi.core.credit");
		
		WriteToDebug("SCORM_GetCompletionStatus,  cmi.core.credit=" + strCredit);
	
		if (strCredit == SCORM_CREDIT){
			return SCORM_PASSED;
		}
		else{
			return SCORM_COMPLETED;
		}
	}
	
}


//note: in all functions that interact with API, we concact any returned strings with "" to convert
//the value to an string type, do this b/c many API's will return a Java String instead of a Javascript
//string

function SCORM_CallLMSSetValue(strElement, strValue){
	
	var intError
	
	SCORM_objAPI.LMSSetValue(strElement, strValue)
	
	intError = SCORM_objAPI.LMSGetLastError()
	intError = intError + "";
	
	if (intError != SCORM_NO_ERROR){
		WriteToDebug ("Error calling LMSSetValue:");
		WriteToDebug ("              strElement=" + strElement);
		WriteToDebug ("              strValue=" + strValue);
		WriteToDebug ("              intError=" + intError);
		WriteToDebug ("              ErrorString=" + SCORM_objAPI.LMSGetErrorString(intError));
		WriteToDebug ("              Diagnostic=" + SCORM_objAPI.LMSGetDiagnostic(intError));
		return false;
	}
	return true;
}

function SCORM_CallLMSGetValue(strElement){

	var intError
	var strResult
	
	strResult = SCORM_objAPI.LMSGetValue(strElement) + ""
	
	intError = SCORM_objAPI.LMSGetLastError()
	intError = intError + "";

	if (intError != SCORM_NO_ERROR){	
		WriteToDebug ("Error calling LMSGetValue:");
		WriteToDebug ("              strElement=" + strElement);
		WriteToDebug ("              intError=" + intError);
		WriteToDebug ("              ErrorString=" + SCORM_objAPI.LMSGetErrorString(intError));
		WriteToDebug ("              Diagnostic=" + SCORM_objAPI.LMSGetDiagnostic(intError));
	}
	
	return strResult;
	
}


function SCORM_IsContentInBrowseMode(){
	
	var strLessonMode
	
	strLessonMode = SCORM_CallLMSGetValue("cmi.core.lesson_mode");
	
	WriteToDebug("SCORM_IsContentInBrowseMode,  cmi.core.lesson_mode=" + strLessonMode);
	
	if (strLessonMode == SCORM_BROWSE){
		return true;
	}
	else{
		return false;
	}
}


function SCORM_findAPI(win)
{
   WriteToDebug("in find API")
   while ( ((win.API == null) || typeof(win.API) == "undefined" ) && (win.parent != null) && (win.parent != win))
   {
      WriteToDebug("Trying parent #" + SCORM_findAPITries + "  window name=" + win.name)
      SCORM_findAPITries++;
      // Note: 7 is an arbitrary number, but should be more than sufficient
      if (SCORM_findAPITries > 7) 
      {
		 WriteToDebug("Error finding API -- too deeply nested.")
         return null;
      }
      
      win = win.parent;

   }
   return win.API;
}

function SCORM_getAPI()
{
   var theAPI = SCORM_findAPI(window);
   if ((theAPI == null) && (window.opener != null) && (typeof(window.opener) != "undefined"))
   {
      theAPI = findAPI(window.opener);
   }
   if (theAPI == null || typeof(theAPI) == "undefined")
   {
      WriteToDebug("Unable to find an API adapter")
   }
   return theAPI;
}
