var startDate;
var mode;
var scormEntry;
var lessonStatus;		// SCORM 1.2 ONLY
var completionStatus;		// SCORM 2004 ONLY
var successStatus;		// SCORM 2004 ONLY


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";
var DATA_PARAM   = (scormVer == "1.2") ? "cmi.suspend_data"       : "cmi.location";
var ENTRY_PARAM  = (scormVer == "1.2") ? "cmi.entry"              : "cmi.entry";  // VERIFY 1.2 PARAM

function getObjSuccessStatusFlash(tgtObjID) {
	var stat = getObjSuccessStatus(tgtObjID);
	setFlashVar("_level0." + tgtObjID + "Status", stat);
}

function getObjCompletionStatusFlash(tgtObjID) {
	var stat = getObjCompletionStatus(tgtObjID);
	setFlashVar("_level0." + tgtObjID + "Status", stat);
}

function getSCOCompletionStatusFlash() {
	var stat = getSCOCompletionStatus();
	setFlashVar("_level0.scoCompletionStatus", stat);
}

function setFlashVar(varName, val) {
	var mov  =  InternetExplorer ? CarneyContentShell : document.CarneyContentShell;

	mov.SetVariable(varName, val);
	mov.SetVariable("_level0.getSCORMIsReady", 1);
}


function getSCOSuccessStatus() {
	return (scormVer == "2004") ? successStatus : lessonStatus;
}

function setSCOSuccessStatus(status) {
	if ((mode == "review")  ||  (mode == "browse") ) {
		return;
	}

	if (scormVer == "2004") {
		//  status should be either "passed" or "failed"
		if (status == successStatus) {
			return;
		}
		//alert("setting SCO success status to " + status);
		result = doSCORMSetValue("cmi.success_status", status);
		successStatus = status;
	}
	else {
// REVISIT ARE OTHER VALUES ALLOWED???
		//  status should be either "completed" or "incomplete"
		if (status == lessonStatus) {
			return;
		}
		result = doSCORMSetValue("cmi.core.lesson_status", status);
		lessonStatus = status;
	}
}



function getSCOCompletionStatus() {
	return (scormVer == "2004") ? completionStatus : lessonStatus;
}

function setSCOCompletionStatus(status) {
	if ((mode == "review")  ||  (mode == "browse") ) {
		return;
	}
	if (scormVer == "2004") {
		//  status should be either "completed" or "incomplete"

		if (status == completionStatus) {
			return;
		}
		if (completionStatus != "completed") {
			//alert("setting SCO completion status to " + status);
			result = doSCORMSetValue("cmi.completion_status", status);
			completionStatus = status;
		}
	}
	else {
		setSCOSuccessStatus(status);
	}
}

function getObjSuccessStatus(tgtObjID) {
	var objTag = getTagForObjID(tgtObjID);
	//alert("getting success status for " + objTag);
    return doSCORMGetValue(objTag + ".success_status");
}

function setObjSuccessStatus(tgtObjID, status) {
	//  status should be one of: passed, failed

	var objTag = getTagForObjID(tgtObjID);
	//alert("setting " + objTag + ".success_status to " + status);
    doSCORMSetValue(objTag + ".success_status", status);
}

function getObjCompletionStatus(tgtObjID) {
	var objTag = getTagForObjID(tgtObjID);
    return doSCORMGetValue(objTag + ".completion_status");
}

function setObjCompletionStatus(tgtObjID, status) {
	//  Status should be one of: "completed", "incomplete", "not attempted"

	var objTag = getTagForObjID(tgtObjID);
	//alert("setting " + objTag + ".completion_status to " + status);
    doSCORMSetValue(objTag + ".completion_status", status);
}


function getObjScoreScaled(tgtObjID) {
	var objTag = getTagForObjID(tgtObjID);
	return doSCORMGetValue(objTag + ".score.scaled");
}

function setObjScoreScaled(tgtObjID, score) {
	//  score should be nbr between 0 and 1 inclusive
	var objTag = getTagForObjID(tgtObjID);
	doSCORMSetValue(objTag + ".score.scaled", score);
}



function LMSInitialize() {
	var result  = doSCORMInitialize();

	startDate   = new Date().getTime();
	mode        = doSCORMGetValue(MODE_PARAM);

	if (scormVer == "2004") {
		completionStatus = doSCORMGetValue("cmi.completion_status");
		successStatus    = doSCORMGetValue("cmi.success_status");
	}
	else {
		lessonStatus = doSCORMGetValue("cmi.core.lesson_status");
	}

	scormEntry = doSCORMGetValue(ENTRY_PARAM);

	if ((lessonStatus == "not attempted") || (completionStatus == "not attempted") || (completionStatus == "unknown")) {
		setSCOCompletionStatus("incomplete");
	}

}

function exitSCO(navRequest, exitParam) {
	//REVISIT NEED TO MAKE SURE THESE ARE CONSISTENT

	//  Valid SCORM 2004 values for adl.nav.request are:
	//
	//          continue
	//          previous
	//          choice
	//          exit
	//          exitAll
	//          abandon
	//          abandonAll
	//          suspendAll
	//          _none_


	if ((navRequest != undefined) && (navRequest != "")) {
		doSCORMSetValue("adl.nav.request", navRequest);
	}

	//  Valid SCORM 2004 values for EXIT_PARAM are:
	//
	//          suspend
	//          timeout
	//          normal
	//          ""

	if (exitParam == undefined) {
		exitParam = "suspend";
	}

	doSCORMSetValue(EXIT_PARAM, exitParam);

	var result;

	result = LMSSetSessionTime();
	result = doSCORMCommit();

	doSCORMTerminate();
}



function LMSGetParameters(args) {
	return doSCORMGetValue(DATA_PARAM);
}

function LMSStoreParameters(args) {
	doSCORMSetValue(DATA_PARAM, args);
}

function LMSSetSessionTime() {
	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);
}


function getTagForObjID(tgtObjID) {
	var childCount = doSCORMGetValue("cmi.objectives._count");

	for (var i = 0; i < childCount; i++) {
		var tempObjID = doSCORMGetValue("cmi.objectives." + i + ".id");
		if (tempObjID == tgtObjID) {
			return "cmi.objectives." + i;
		}
	}
	return "";
}



/*******************************************************************************
** 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;
}


