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 ENTRY_PARAM  = (scormVer == "1.2") ? "cmi.core.entry"         : "cmi.entry";  // VERIFY 1.2 PARAM

function getObjSuccessStatusFlash(tgtObjID) {
	var stat = getObjSuccessStatus(tgtObjID);
	setFlashVar(tgtObjID + "Status", stat);
}

function getObjCompletionStatusFlash(tgtObjID) {
	var stat = getObjCompletionStatus(tgtObjID);
	setFlashVar(tgtObjID + "Status", stat);
}

function getSCOCompletionStatusFlash() {
	var stat = getSCOCompletionStatus();
	setFlashVar("scoCompletionStatus", stat);
}

function setFlashVar(varName, val) {
	var mov  =  getCarneyContentShell();

	mov.setParam(varName, val);
	mov.setParam("getSCORMIsReady", 1);
}


function getSCOSuccessStatus() {
	if (parameterMode != "LMS")
		return;
	return (scormVer == "2004") ? successStatus : lessonStatus;
}

function setSCOSuccessStatus(status) {
	if (parameterMode != "LMS")
		return;
	if ((mode == "review")  ||  (mode == "browse") ) {
		return;
	}

	if (scormVer == "2004") {
		//  status should be either "passed" ,"failed" or "unknown";
		if (status == successStatus) {
			return;
		}
		result = doSCORMSetValue("cmi.success_status", status);
		successStatus = status;
	}
	else {

		if (status == lessonStatus) {
			return;
		}
		result = doSCORMSetValue("cmi.core.lesson_status", status);
		lessonStatus = status;
		
	}
}



function getSCOCompletionStatus() {
	if (parameterMode != "LMS")
		return;
	return (scormVer == "2004") ? completionStatus : lessonStatus;
}

function setSCOCompletionStatus(status) {
	if (parameterMode != "LMS")
		return;
	if ((mode == "review")  ||  (mode == "browse") ) {
		return;
	}
	if (scormVer == "2004") {
		
		//  status should be either "completed" or "incomplete"

		if (status == completionStatus) {
			return;
		}
		if (completionStatus != "completed") {
			
			if((successStatus == "") || (successStatus == "null") || (successStatus == "unknown")){
				setSCOSuccessStatus("passed")
			}

			result = doSCORMSetValue("cmi.completion_status", status);
			completionStatus = status;
		}
	}
	else {
		setSCOSuccessStatus(status);
	}
}

function getObjSuccessStatus(tgtObjID) {
	if (parameterMode != "LMS")
		return;
	var objTag = getTagForObjID(tgtObjID);
	//alert("getting success status for " + objTag);
    return doSCORMGetValue(objTag + ".success_status");
}

function setObjSuccessStatus(tgtObjID, status) {
	if (parameterMode != "LMS")
		return;
	//  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) {
	if (parameterMode != "LMS")
		return;
	var objTag = getTagForObjID(tgtObjID);
    return doSCORMGetValue(objTag + ".completion_status");
}

function setObjCompletionStatus(tgtObjID, status) {
	if (parameterMode != "LMS")
		return;
	//  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) {
	if (parameterMode != "LMS")
		return;
	var objTag = getTagForObjID(tgtObjID);
	return doSCORMGetValue(objTag + ".score.scaled");
}

function setObjScoreScaled(tgtObjID, score) {
	if (parameterMode != "LMS")
		return;
	//  score should be nbr between 0 and 1 inclusive
	var objTag = getTagForObjID(tgtObjID);
	doSCORMSetValue(objTag + ".score.scaled", score);
}



function LMSInitialize() {
	if (parameterMode != "LMS")
		return;
	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) {
	
	if (scormVer == "2004"){
		
		if (exitParam == undefined) {
			exitParam = "suspend";
		}
		doSCORMSetValue(EXIT_PARAM, exitParam);
		
	}
	
	if(scormVer == "1.2"){
		if (exitParam == undefined) {
			exitParam = "suspend";
		}
		doSCORMSetValue(EXIT_PARAM, exitParam);
	}
	
	
	var result;

	result = LMSSetSessionTime();
	result = doSCORMCommit();

	doSCORMTerminate();
}



function LMSGetParameters(args) {
	if (parameterMode != "LMS")
		return;
	return doSCORMGetValue(DATA_PARAM);
}

function LMSStoreParameters(args) {
	doSCORMSetValue(DATA_PARAM, args);
}

function LMSSetSessionTime() {
	if (parameterMode != "LMS")
		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);
}


function getTagForObjID(tgtObjID) {
	if (parameterMode != "LMS")
		return;
	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;
}