<!--
//
//*****************************************************************************************
//*****************************************************************************************
// Commandes et fonctions Javascript pour Flash/SCORM
//*****************************************************************************************
//*****************************************************************************************
//
//*****************************************************************************************
// Script pour faire fonctionner fscommand avec Internet Explorer
//*****************************************************************************************
//
if (navigator.appName && navigator.appName.indexOf("Microsoft") != -1 && navigator.userAgent.indexOf("Windows") != -1 && navigator.userAgent.indexOf("Windows 3.1") == -1) {
	document.write('<script language=\"VBScript\"\>\n');
	document.write('On Error Resume Next\n');
	document.write('Sub index_FSCommand(ByVal command, ByVal args)\n');
	document.write('	Call index_DoFSCommand(command, args)\n');
	document.write('End Sub\n');
	document.write('</script\>\n');
}
//
//*****************************************************************************************
// Initialisation des variables
//*****************************************************************************************
//
// Change to true to show error messages
var g_bShowApiErrors = false;
//
// Error strings (in message boxes if g_bShowApiErrors is true)
var g_strAPINotFound = "Management system interface not found.";
var g_strAPITooDeep = "Cannot find API - too deeply nested.";
var g_strAPIInitFailed = "Found API but LMSInitialize failed.";
var g_strAPISetError = "Trying to set value but API not available.";
var g_strFSAPIError = 'LMS API adapter returned error code: "%1"\nWhen FScommand called API.%2\nwith "%3"';
var g_strDisableErrorMsgs = "Select cancel to disable future warnings.";
//
// This value is normally given by the LMS, but in case it is not, this is the default value to use to determine passed/failed.
// Set this null if the Flash actionscript uses its own method to determine passed/fail, otherwise set to a value from 0 to 1
// inclusive (may be a floating point value, e.g "0.75").
var g_SCO_MasteryScore = null;
//
var g_nSCO_ScoreMin = 0;
var g_nSCO_ScoreMax = 100;
var g_bMinScoreAcquired = false;
var g_bMaxScoreAcquired = false;
//
// Per SCORM specification, the LMS provided mastery score, if any, will override the SCO in interpreting whether the score
// should be interpreted when the pass/fail status is determined. The template tries to obtain the mastery score, and if it
// is available, to set the status to passed or failed accordingly when the SCO sends a score. The LMS may not actually make
// the determination until the SCO has been terminated. Default value for this flag is true. Set it to false if don't want to
// predict how the LMS will set pass/fail status based on the mastery score (the LMS will win in the end anyway).
var g_bInterpretMasteryScore = true;
//
// API interface initialization and catcher functions
var g_nFindAPITries = 0;
var g_objAPI = null;
var g_bInitDone = false;
var g_bFinishDone = false;
var	g_bSCOBrowse = false;
var g_dtmInitialized = new Date();
var g_bMasteryScoreInitialized = false;
//
// Detect Internet Explorer
var g_bIsInternetExplorer = navigator.appName.indexOf("Microsoft") != -1;
//
//*****************************************************************************************
// Fonctions
//*****************************************************************************************
//
function AlertUserOfAPIError(strText) {
	if (g_bShowApiErrors) {
		var s = strText + "\n\n" + g_strDisableErrorMsgs;
		if (!confirm(s)) {
			g_bShowApiErrors = false;
		}
	}
}
function ExpandString(s) {
	var re = new RegExp("%","g");
	for (i = arguments.length-1; i > 0; i--) {
		s2 = "%" + i;
		if (s.indexOf(s2) > -1) {
			re.compile(s2,"g");
			s = s.replace(re, arguments[i]);
		}
	}
	return s;
}
function FindAPI(win) {
	while ((win.API == null) && (win.parent != null) && (win.parent != win)) {
		g_nFindAPITries ++;
		if (g_nFindAPITries > 500) {
			AlertUserOfAPIError(g_strAPITooDeep);
			return null;
		}
		win = win.parent;
	}
	return win.API;
}
function APIOK() {
	return ((typeof(g_objAPI)!= "undefined") && (g_objAPI != null));
}
function SCOInitialize() {
	var err = true;
	if (!g_bInitDone) {
		if ((window.parent) && (window.parent != window)) {
			g_objAPI = FindAPI(window.parent);
		}
		if ((g_objAPI == null) && (window.opener != null)) {
			g_objAPI = FindAPI(window.opener);
		}
		if (!APIOK()) {
			AlertUserOfAPIError(g_strAPINotFound);
			err = false;
		} else {
			err = g_objAPI.LMSInitialize("");
			if (err == "true") {
				g_bSCOBrowse = (g_objAPI.LMSGetValue("cmi.core.lesson_mode") == "browse");
				if (!g_bSCOBrowse) {
					if (g_objAPI.LMSGetValue("cmi.core.lesson_status") == "not attempted") {
						err = g_objAPI.LMSSetValue("cmi.core.lesson_status","incomplete");
					}
				}
			} else {
				AlertUserOfAPIError(g_strAPIInitFailed);
			}
		}
		if (typeof(SCOInitData) != "undefined") {
			// The SCOInitData function can be defined in another script of the SCO
			SCOInitData();
		}
		g_dtmInitialized = new Date();
	}
	g_bInitDone = true;
	return (err + "");
}
function SCOFinish() {
	if ((APIOK()) && (g_bFinishDone == false)) {
		SCOReportSessionTime();
		if (typeof(SCOSaveData) != "undefined") {
			SCOSaveData();
		}
		g_bFinishDone = (g_objAPI.LMSFinish("") == "true");
	}
	return (g_bFinishDone + "" );
}
//
// Call these catcher functions rather than trying to call the API adapter directly
function SCOGetValue(nam) {
	return ((APIOK())?g_objAPI.LMSGetValue(nam.toString()):"");
}
function SCOCommit() {
	return ((APIOK())?g_objAPI.LMSCommit(""):"false");
}
function SCOGetLastError() {
	return ((APIOK())?g_objAPI.LMSGetLastError():"-1");
}
function SCOGetErrorString(n) {
	return ((APIOK())?g_objAPI.LMSGetErrorString(n):"No API");
}
function SCOGetDiagnostic(p) {
	return ((APIOK())?g_objAPI.LMSGetDiagnostic(p):"No API");
}
function SCOSetValue(nam,val) {
	var err = "";
	if (!APIOK()) {
		AlertUserOfAPIError(g_strAPISetError + "\n" + nam + "\n" + val);
		err = "false";
	} else if (nam == "cmi.core.score.raw") {
		err = privReportRawScore(val);
	}
	if (err == "") {
		err = g_objAPI.LMSSetValue(nam,val.toString() + "");
		if (err != "true") {
			return err;
		}
	}
	if (nam == "cmi.core.score.min") {
		g_bMinScoreAcquired = true;
		g_nSCO_ScoreMin = val;
	}
	else if (nam == "cmi.core.score.max") {
		g_bMaxScoreAcquired = true;
		g_nSCO_ScoreMax = val;
	}
	return err;
}
function privReportRawScore(nRaw) {
	if (isNaN(nRaw)) {
		return "false";
	}
	if (!g_bMinScoreAcquired) {
		if (g_objAPI.LMSSetValue("cmi.core.score.min",g_nSCO_ScoreMin+"")!= "true") {
			return "false";
		}
	}
	if (!g_bMaxScoreAcquired) {
		if (g_objAPI.LMSSetValue("cmi.core.score.max",g_nSCO_ScoreMax+"")!= "true") {
			return "false";
		}
	}
	if (g_objAPI.LMSSetValue("cmi.core.score.raw", nRaw)!= "true") {
		return "false";
	}
	g_bMinScoreAcquired = false;
	g_bMaxScoreAcquired = false;
	if (!g_bMasteryScoreInitialized) {
		var nMasteryScore = parseInt(SCOGetValue("cmi.student_data.mastery_score"),10);
		if (!isNaN(nMasteryScore)) {
			g_SCO_MasteryScore = nMasteryScore;
		}
	}
  	if ((g_bInterpretMasteryScore)&&(!isNaN(g_SCO_MasteryScore))) {
    	var stat = (nRaw >= g_SCO_MasteryScore? "passed" : "failed");
    	if (SCOSetValue("cmi.core.lesson_status",stat) != "true") {
			return "false";
		}
  	}
  	return "true";
}
//
// Convert duration from milliseconds to 0000:00:00.00 format
function MillisecondsToCMIDuration(n) {
	var hms = "";
	var dtm = new Date();	dtm.setTime(n);
	var h = "000" + Math.floor(n / 3600000);
	var m = "0" + dtm.getMinutes();
	var s = "0" + dtm.getSeconds();
	var cs = "0" + Math.round(dtm.getMilliseconds() / 10);
	hms = h.substr(h.length-4)+":"+m.substr(m.length-2)+":";
	hms += s.substr(s.length-2)+"."+cs.substr(cs.length-2);
	return hms;
}
//
// SCOReportSessionTime is called automatically by this script, but you may call it at any time also from the SCO
function SCOReportSessionTime() {
	var dtm = new Date();
	var n = dtm.getTime() - g_dtmInitialized.getTime();
	return SCOSetValue("cmi.core.session_time",MillisecondsToCMIDuration(n));
}
//
// Since only the designer of a SCO knows what completed means, another script of the SCO may call this function to set completed
// status. The function checks that the SCO is not in browse mode, and avoids clobbering a "passed" or "failed" status since they
// imply "completed".
function SCOSetStatusCompleted() {
	var stat = SCOGetValue("cmi.core.lesson_status");
	if (SCOGetValue("cmi.core.lesson_mode") != "browse") {
		if ((stat!="completed") && (stat != "passed") && (stat != "failed")) {
			return SCOSetValue("cmi.core.lesson_status","completed");
		}
	} else {
		return "false";
	}
}
//
// Objective management logic
function SCOSetObjectiveData(id, elem, v) {
	var result = "false";
	var i = SCOGetObjectiveIndex(id);
	if (isNaN(i)) {
		i = parseInt(SCOGetValue("cmi.objectives._count"));
		if (isNaN(i)) {
			i = 0;
		}
		if (SCOSetValue("cmi.objectives." + i + ".id", id) == "true") {
			result = SCOSetValue("cmi.objectives." + i + "." + elem, v);
		}
	} else {
		result = SCOSetValue("cmi.objectives." + i + "." + elem, v);
		if (result != "true") {
			// Maybe this LMS accepts only journaling entries
			i = parseInt(SCOGetValue("cmi.objectives._count"));
			if (!isNaN(i)) {
				if (SCOSetValue("cmi.objectives." + i + ".id", id) == "true") {
					result = SCOSetValue("cmi.objectives." + i + "." + elem, v);
				}
			}
		}
	}
	return result;
}
function SCOGetObjectiveData(id, elem) {
	var i = SCOGetObjectiveIndex(id);
	if (!isNaN(i)) {
		return SCOGetValue("cmi.objectives." + i + "."+elem);
	}
	return "";
}
function SCOGetObjectiveIndex(id) {
	var i = -1;
	var nCount = parseInt(SCOGetValue("cmi.objectives._count"));
	if (!isNaN(nCount)) {
		//walk backward in case LMS does journaling
		for (i = nCount-1; i >= 0; i--) { 
			if (SCOGetValue("cmi.objectives." + i + ".id") == id) {
				return i;
			}
		}
	}
	return NaN;
}
//
//*****************************************************************************************
// fscommand en provenance de Flash
//*****************************************************************************************
//
// nom_DoFSCommand --> Le nom doit être le même que celui de la capsule Flash tel qu'identifié dans les tags
// <object id="abc"> et <embed src="abc">
function index_DoFSCommand(command, args) {
	var SCORMObj = g_bIsInternetExplorer ? index : document.index;
	var myArgs = new String(args);
	var cmd = new String(command);
	var v = "";
	var err = "true";
	var arg1, arg2, n, s, i;
	var sep = myArgs.indexOf(",");
	if (sep > -1) {
		arg1 = myArgs.substr(0, sep);
		arg2 = myArgs.substr(sep+1);
	} else {
		arg1 = myArgs;
	}
	if (!APIOK()) {
		if (cmd == "LMSInitialize") {
			SCOInitialize();
		} else {
			return;
		}
	}
	if (cmd.substring(0,3) == "LMS") {
		if (cmd == "LMSSetValue") {
			err = SCOSetValue(arg1,arg2);
		} else if (cmd == "LMSFinish") {
			// Handled automatically by the template, but the movie may call it earlier
			err = SCOFinish();
		} else if (cmd == "LMSCommit") {
			err = SCOCommit();
		} else if (cmd == "LMSFlush") {
			// LMSFlush is not defined in SCORM and calling it causes test suite error
		} else if ((arg2) && (arg2.length > 0)) {
			if (cmd == "LMSGetValue") {
				SCORMObj.SetVariable(arg2, SCOGetValue(arg1));
			} else if (cmd == "LMSGetLastError") {
				SCORMObj.SetVariable(arg2, SCOGetLastError(arg1));
			} else if (cmd == "LMSGetErrorString") {
				SCORMObj.SetVariable(arg2, SCOGetLastError(arg1));
			} else if (cmd == "LMSGetDiagnostic") {
				SCORMObj.SetVariable(arg2, SCOGetDiagnostic(arg1));
			} else {
				// for unknown LMSGetxxxx extension
				v = eval('g_objAPI.' + cmd + '(\"' + arg1 + '\")');
				SCORMObj.SetVariable(arg2,v);
			}
		} else if (cmd.substring(0,3) == "LMSGet") {
			err = "-2: No Flash variable specified";
		}
		// End of handling "LMSxxx" cmds
		//
	} else {
		// Unknown commands may be invoking an API extension If commands comes with a 2nd argument, assume a value is expected
		// otherwise assume it is just a cmd.
		if (eval('g_objAPI.' + cmd)) {
			v = eval('g_objAPI.' + cmd + '(\"' + arg1 + '\")');
			if ((arg2) && (arg2.length > 0)) {
				SCORMObj.SetVariable(arg2,v);
			} else {
				err = v;
			}
		} else {
			err = "false";
		}
	}
	// End of command translation and processing
	//
	// Handling detected errors, such LMS error returns
	if ((g_bShowApiErrors) && (err != "true")) {
		AlertUserOfAPIError(ExpandString(g_strFSAPIError, err, cmd, args));
	}
	return err;
}
//-->
