//sco.js  for CDC 
//Healthsoft Learning Management System compliant Mock Code 
//Client-side Javascript nominal implementation (no launch LMS)

//SCO HANDLING
//NOTE: this function should ONLY be passed an empty string 
function LMSInitialize(str) {
	if (Initialized) return "true";
	else { // we can test for some condition like login
		if ("yes" == "yes") { 
			//SCOwin.focus(); // use only if actual LMS launch was performed
			Initialized = true;
			return "true";
		} else {
			Initialized = false;
			return "false";
		}
	}
}

//NOTE: this function should ONLY be passed an empty string - LMSFinish("")
function LMSFinish(str) {
	if (!Initialized)  return "false";
	Initialized = false;
	return "true";
}


//DATA HANDLING

//Acceptable parameters are:
//	datamodel.group.element - standard data call
//	datamodel._version - returns version
//	datamodel.group._children - return comma-deliniated list of children available
//	datamodel.group._count - returns # of children
function LMSGetValue(element) { 
	return "";
}

//This function should return true if data was successfully saved, else false
//Value must be a string that can be converted into the data type required	
function LMSSetValue(element, value) {
	if (!Initialized)   return "false";
	return "true";
}
//NOTE: this function should ONLY be passed an empty string 
//this function should commit any variables that were declared by the LMSSetValue
function LMSCommit(str) {
	if (!Initialized)   return "false";
	return "true";
}


//ERROR HANDLING

//returns an error code
function LMSGetLastError() {
	return "0";
}

//returns error description
function LMSGetErrorString(errornumber) {
	return "";
}

//returns vendor specific error message info
function LMSGetDiagnostic(errornumber) {
	return "";
}

//end LMS functions

//API object constructor
function apiObj() {
   this.LMSInitialize = LMSInitialize;
   this.LMSFinish = LMSFinish;
	this.LMSGetValue = LMSGetValue;
	this.LMSSetValue = LMSSetValue
	this.LMSCommit = LMSCommit;
	this.LMSGetLastError = LMSGetLastError;
	this.LMSGetErrorString = LMSGetErrorString;
	this.LMSGetDiagnostic = LMSGetDiagnostic;
}

//var Initialized = false;
var Initialized = true; //this line 
//this is the object that is located by findAPI function in sco.js
var API = new apiObj();
/***********************************************************************************/
//sco.js
//Healthsoft Sharable Content Object (SCO) locating functions - Jesse Brett - 3/4/02
//Client-side Javascript implementation
/* FUNCTIONS
	exitSCO()
	getAPI()
	errorReport(errorcode,LMSerror)
	saveLocation(loc)
	convertTotalSeconds(ts)
	convertBack(str)
	convertMilSeconds(n,scope)
	
	
*/
/* VARIABLES
	findAPITries
	API	
*/
function exitSCO(st,ct) {
	if (API == null) {
		window.close();
	} else {
		var my_time = parseInt(ct) - parseInt(st);
		if(!isNaN(my_time)){
			API.LMSSetValue("cmi.core.session_time", convertMilSeconds(my_time));
		}
		API.LMSSetValue("cmi.core.exit", "suspend");
		API.LMSSetValue("cmi.core.lesson_status", "incomplete");		
		API.LMSFinish("");
		window.close();
	}
}


//API-LOCATING FUNCTIONS

var findAPITries = 0;

function findAPI(win) {
	while ((win.API == null)&&(win.parent != null)&&(win.parent != win)) {
		findAPITries++;
		if (findAPITries > 7) {
			return null;
		}
		win = win.parent;
	}
	return win.API;
}

function getAPI() {
	var theAPI = findAPI(window);
	if ((theAPI == null)&&(window.opener != null)&&(typeof(window.opener) != "undefined")) {
		theAPI = findAPI(window.opener);
	}
	if (theAPI == null || theAPI == "undefined") {
		return null;
	}
	return theAPI;
}

function errorReport(errorcode,LMSerror) {
	switch (errorcode) {
		case 1:
			document.location.replace("../../lms/error.asp?errorcode="+errorcode);
			break;
		case 2:
			document.location.replace("../../lms/error.asp?errorcode="+errorcode);
			break;
		case 3:
			document.location.replace("../../lms/error.asp?errorcode="+errorcode+"&LMSerror="+LMSerror);
			break;
		default:
			document.location.replace("../../lms/error.asp?errorcode="+errorcode);
		break;
	}
}
function saveLocation(loc,st,ct) {
	if (API == null) {
		errorReport(1);
	} else {
		var retval = API.LMSSetValue("cmi.core.lesson_location", loc);
		if (retval == "false") {
			var errorval = API.LMSGetLastError();
			errorReport(3,errorval);
		}
		var my_time = parseInt(ct) - parseInt(st);
		if(!isNaN(my_time)){
			API.LMSSetValue("cmi.core.session_time", convertMilSeconds(my_time));
		}
		API.LMSCommit("");
	}
}
/*******************************************************************************
** 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) { //original utility
   if (isNaN(parseInt(ts))) ts = 0;
   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
   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>0) 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;
   return rtnVal;
}
function convertBack(str) {
	var secs = "";
	var tmpAry = str.split(":");
	if(tmpAry.length == 3){
		secs = 0;
		secs += parseInt(tmpAry[0]) * 60 * 60;
		secs += parseInt(tmpAry[1]) * 60;
		secs += parseFloat(tmpAry[2]);
	}
   return secs;
}
function convertMilSeconds(n,scope) { //converts mseconds to time str hh:mm:ss.mil
   if (isNaN(n) || n == 0) var rtnVal = "00:00:00.000";
   else {
	var ms = n % 1000; //ms left after whole seconds(1000ms)
	n -= ms;
	ms = parseInt(ms);
	var sec = n % 60000;  //ms left after whole minites(60000ms)
	n -= sec;
	var min = n % 3600000;  //ms left after whole hours(3600000ms)
	n -= min;
	var hrs = n % 86400000;
	n -= hrs;
	var days = n;
	if(days != 0) days = n/86400000;
	if(hrs != 0) hrs = hrs/3600000;
	if(min != 0) min = min/60000;
	if(sec != 0) sec = sec/1000;
	var strHrs = new String(hrs);
	var strMin = new String(min);
	var strSec = new String(sec);
	var strMs = new String(ms);
	if (strHrs.length < 2) strHrs = "0"+strHrs;
	if (strMin.length < 2) strMin = "0"+strMin;
	if (strSec.length < 2) strSec = "0"+strSec;
	if (strMs.length < 2) strMs = "0"+strMs;
	if (strMs.length < 3) strMs = "0"+strMs;
	var rtnVal = strHrs+":"+strMin+":"+strSec+"."+strMs;
   }
   return rtnVal;
}
function convertBackToMil(str) {//converts time str back to mseconds
	var secs = 0;
	secs = secs + parseInt(str.substring(0, str.indexOf(":"))) * 60 * 60;
	str = str.substring(str.indexOf(":")+1);
	secs = secs + parseInt(str.substring(0, str.indexOf(":"))) * 60;
	str = str.substring(str.indexOf(":")+1);
	if (str.indexOf(".") != -1) { 
		secs = secs + parseInt(str.substring(0, str.indexOf(".")));
		str = str.substring(str.indexOf(".")+1);
		var msecs = secs * 1000;
		msecs = msecs + parseInt(str);
	} else {
		secs = secs + parseInt(str);
		var msecs = secs * 1000;
	}
   return msecs;
}
var API = getAPI();
