/*
================================================================================================================

DEFINITIONS

================================================================================================================
*/

/*

LMSGetValue():
			Retrieves a data model element's value from the LMS
LMSSetValue():
			Writes a value for a data model element to the LMS
LMSCommit():
			May be called after any values are set to ensure that the data is persisted
cmi.core.lesson_location:
			The data element that describes the user's last saved location
cmi.core.lesson_status:
			The data element that describes the user's last saved lesson status
			"incomplete" | "completed"
cmi.suspend_data:
			A free-for-all (a 4096 character string)

*/

/*
================================================================================================================

STARTUP

================================================================================================================
*/


/*

initCMI()
---------

*/

function initCMI() {
	trace("initCMI()");
	
	/*
	
	INITIALIZE THE LMS...
	
	*/
	
	doLMSInitialize();

	/*
	
	CHECK OUR MODULE STATUS...
	
	*/
	
	var sStatus = GetStatus();
	
	trace("sStatus = "+sStatus);
	
	if (sStatus != "completed") {
		SetStatus("incomplete");
		bTrackProgress = true;
	}

	/*
	
	CHECK IF WE'VE SAVED A LOCATION TO RESUME WITH...
	
	*/
	
	var sLocation = GetLocation();
	
	trace("sLocation = "+sLocation);
	
	if (sLocation != "") {
		nChapter = sLocation.split("_")[0];
		nSubChapter = sLocation.split("_")[1];
		bResumeOnStartup = true;
	}
	
}

/*
================================================================================================================

PROGRESS

================================================================================================================
*/

/*

SetProgress()
-------------

*/

function SetProgress(sProgress) {
	
	trace("SetProgress("+sProgress+")");
	
	doLMSSetValue("cmi.suspend_data", sProgress);
	
	doLMSCommit();
	
}

/*

GetProgress()
-------------

*/

function GetProgress() {
	
	trace("GetProgress()");
	
	return doLMSGetValue("cmi.suspend_data");
	
}

/*
================================================================================================================

STATUS

================================================================================================================
*/

/*

SetStatus()
-----------

*/

function SetStatus(sStatus) {
	
	trace("SetStatus("+sStatus+")");
	
	doLMSSetValue("cmi.core.lesson_status", sStatus);
	
	doLMSCommit();
	
}

/*

GetStatus()
-----------

*/

function GetStatus() {
	
	trace("GetStatus()");
	
	return doLMSGetValue("cmi.core.lesson_status");
	
}

/*

MarkCompleted()
---------------

*/

function MarkCompleted() {
	
	trace("MarkCompleted()");
	
	SetStatus("completed");
	
}

/*
================================================================================================================

LOCATION

================================================================================================================
*/


/*

SetLocation()
-------------

*/

function SetLocation(sLocation) {
	
	trace("SetLocation("+sLocation+")");
	
	doLMSSetValue("cmi.core.lesson_location", sLocation);
	
	doLMSCommit();
	
}

/*

GetLocation()
-------------

*/

function GetLocation() {
	
	trace("GetLocation()");
	
	return doLMSGetValue("cmi.core.lesson_location");
	
}

/*
================================================================================================================

SAVE AND EXIT

================================================================================================================
*/

/*

SaveAndExit()
-------------

*/

function SaveAndExit(nChapter, nSubChapter) {
	
	trace("SaveAndExit("+nChapter+", "+nSubChapter+")");
	
	var sLocation = nChapter+"_"+(nSubChapter == null ? "0" : nSubChapter);
	
	SetLocation(sLocation);
	
	Exit();
}

/*

ExitWithoutSaving()
-------------------

*/

function ExitWithoutSaving() {
	
	trace("ExitWithoutSaving()");
	
	SetLocation("");
	
	Exit();
	
}

/*

Exit()
------

*/

function Exit() {
	
	trace("Exit()");
	
	doLMSFinish();
	
	var sLocation = "http://www.westpac.com.au/";
	
	if (window.opener != null) {
		window.opener.location.href = sLocation;
	} else {
		location.href = sLocation;
	}
	
	window.close();
	
}

/*

The following function is the way the Exit() function
worked before the whole popup thing was introduced...

NO POPUP!

*/

function Exit_() {
	
	trace("Exit()");
	
	doLMSFinish();
	
	location = "http://www.westpac.com.au/";
	
}