var startDate;
var lmsExitCalled;
var lessonStatus;		// SCORM 1.2 ONLY
var completionStatus;		// SCORM 2004 ONLY
var successStatus;		// SCORM 2004 ONLY
var mode;


var MODE_PARAM   = (scormVer == "1.2") ? "cmi.core.lesson_mode"   : "cmi.mode";
var DATA_PARAM   = (scormVer == "1.2") ? "cmi.suspend_data"       : "cmi.suspend_data";
var TIME_PARAM   = (scormVer == "1.2") ? "cmi.core.session_time"  : "cmi.session_time";
var EXIT_PARAM   = (scormVer == "1.2") ? "cmi.core.exit"          : "cmi.exit";


function LMSInitialize() {
	var result;

	result          = doSCORMInitialize();

	lmsExitCalled   = false;
	startDate       = new Date().getTime();
	mode            = doSCORMGetValue(MODE_PARAM);
	LMSGetLessonStatus();

	if ((lessonStatus == "not attempted") || (completionStatus == "not attempted") || (completionStatus == "unknown")) {
		//  The student is now attempting the lesson
		LMSSetLessonStatus("incomplete");
	}

}


function LMSChangeModule(url) {
	if (lmsExitCalled) {
		return;
	}
	var result;

	// Reinitialize Exit to blank
	doSCORMSetValue(EXIT_PARAM, "");

	LMSExit("");
}

function LMSExit(status) {
	if (lmsExitCalled) {
		return;
	}
	var result;

	result = LMSSetSessionTime();
	if ((status != null) && (status != undefined) && (status != "")) {
		result = LMSSetLessonStatus(status);
	}

	doSCORMSetValue(EXIT_PARAM, "");

	result = doSCORMCommit();
	result = doSCORMFinish();

	lmsExitCalled = true;
//	self.close();
}


function LMSGetParameters(args) {
	if (lmsExitCalled) {
		return;
	}
	return doSCORMGetValue(DATA_PARAM);
}

function LMSStoreParameters(args) {
	if (lmsExitCalled) {
		return;
	}
	doSCORMSetValue(DATA_PARAM, args);
}

function LMSGetLessonStatus() {
	if (lmsExitCalled) {
		return;
	}
	if (scormVer == "2004") {
		completionStatus = doSCORMGetValue("cmi.completion_status");
		successStatus    = doSCORMGetValue("cmi.success_status");
	}
	else {
		lessonStatus = doSCORMGetValue("cmi.core.lesson_status");
	}
}

function LMSSetLessonStatus(status) {
	if (lmsExitCalled) {
		return;
	}
	if ( (mode == "review")  ||  (mode == "browse") ) {
		return;
	}

	if (scormVer == "2004") {
		if ((status == "passed") || (status == "failed")) {
			if (status == successStatus) {
				return;
			}
			result = doSCORMSetValue("cmi.success_status", status);
			successStatus = status;
		}
		else if ((status == "completed") || (status == "incomplete")) {
			completionStatus = doSCORMGetValue("cmi.completion_status");
			if (status == completionStatus) {
				return;
			}
			if (completionStatus != "completed")
			{
				result = doSCORMSetValue("cmi.completion_status", status);
				completionStatus = status;
			}
		}
	}
	else {
		if ((status == "completed") || (status == "passed")) {
			status = "completed";
		}
		else if ((status == "incomplete") || (status == "failed")) {
			status = "incomplete";
		}
		else {
		}
		if (status == lessonStatus) {
			return;
		}
		result = doSCORMSetValue("cmi.core.lesson_status", status);
		lessonStatus = status;
	}
}


function LMSMarkModuleComplete() {
	if (lmsExitCalled) {
		return;
	}
	var result;

//REMOVED THIS BECAUSE MODULE != SCO WHEN MULTIPLE MODULES ARE PACKAGED AS ONE SCO!!!
//	LMSSetLessonStatus("passed");
	LMSSetSessionTime();
	result = doSCORMCommit();
}


function LMSSetSessionTime() {
	if (lmsExitCalled) {
		return;
	}
	var formattedTime = "00:00:00.0";

	if (startDate != 0) {
		var currentDate    = new Date().getTime();
		var elapsedSeconds = ( (currentDate - startDate) / 1000 );
		var formattedTime  = convertTotalSeconds( elapsedSeconds );
	}

	doSCORMSetValue(TIME_PARAM, formattedTime);
}


/*******************************************************************************
** this function will convert seconds into hours, minutes, and seconds in
** CMITimespan type format - HHHH:MM:SS.SS (Hours has a max of 4 digits &
** Min of 2 digits
*******************************************************************************/
function convertTotalSeconds(ts) {
	var sec = (ts % 60);

	ts     -= sec;
	var tmp = (ts % 3600);  //# of seconds in the total # of minutes
	ts     -= tmp;              //# of seconds in the total # of hours

	// convert seconds to conform to CMITimespan type (e.g. SS.00)
	sec = Math.round(sec*100)/100;

	var strSec         = new String(sec);
	var strWholeSec    = strSec;
	var strFractionSec = "";

	if (strSec.indexOf(".") != -1) {
		strWholeSec    =  strSec.substring(0, strSec.indexOf("."));
		strFractionSec = strSec.substring(strSec.indexOf(".")+1, strSec.length);
	}

	if (strWholeSec.length < 2) {
		strWholeSec = "0" + strWholeSec;
	}
	strSec = strWholeSec;

	if (strFractionSec.length) {
		strSec = strSec+ "." + strFractionSec;
	}


	if ((ts % 3600) != 0 )
		var hour = 0;
	else
		var hour = (ts / 3600);

	if ( (tmp % 60) != 0 )
		var min = 0;
	else
		var min = (tmp / 60);

	if ((new String(hour)).length < 2)
		hour = "0"+hour;

	if ((new String(min)).length < 2)
		min = "0"+min;

	var rtnVal = hour+":"+min+":"+strSec;
	if (scormVer == "2004") {
		rtnVal = "PT" + hour + "H" + min + "M" + strSec + "S";
	}

	return rtnVal;
}

