
var InternetExplorer = navigator.appName.indexOf("Microsoft") != -1;
var StartTime, EndTime, SessionTime;
var finished=false;
var api = getAPIHandle();
var initalTime=new Date();
// Handle all the the FSCommand messages in a Flash movie
function shell_DoFSCommand(command, args) {
  var shellObj = InternetExplorer ? shell : document.shell;
  switch(command){
		case "alert":
			alert(args);
			break;
		case "getLMSData":
			if (api != null) {
				document.getElementById("shell").StopPlay();
				var data=LMSGetValue("cmi.suspend_data");
				document.getElementById("shell").SetVariable("core_lesson",data);
				data=LMSGetValue("cmi.core.student_name");
				document.getElementById("shell").SetVariable("studName",data);
				data=LMSGetValue("cmi.core.lesson_location");
				document.getElementById("shell").SetVariable("bookmark",data);
				data=LMSGetValue("cmi.core.lesson_status");
				document.getElementById("shell").SetVariable("course_status",data);
				data=LMSGetValue("cmi.core.score.raw");
				document.getElementById("shell").SetVariable("score",data);
				//data=LMSGetValue("cmi.core.score.max");
				//document.getElementById("shell").SetVariable("scoreMax",data);
				document.getElementById("shell").Play();
			}
			break;
		case "updateLMSData":
		
			if (api != null) {
				var data=document.getElementById("shell").GetVariable("core_lesson");
				//alert("core_lesson "+data);
				LMSSetValue("cmi.suspend_data",data);
				data=document.getElementById("shell").GetVariable("bookmark");
				//alert("bookmark "+data);
				LMSSetValue("cmi.core.lesson_location",data);
				data=document.getElementById("shell").GetVariable("course_status");
				//alert("course_status "+data);
				LMSSetValue("cmi.core.lesson_status",data);
				data=document.getElementById("shell").GetVariable("score");
				if(!isNaN(parseInt(data))) {
					//alert("score "+data);
					LMSSetValue("cmi.core.score.raw",data);
				}
				//data=document.getElementById("shell").GetVariable("scoreMax");
				//LMSSetValue("cmi.core.score.max",data);
				LMSCommit();
			}
			break;
		case "exitCourse":
			closeCourse();
			setTimeout("parent.close()",1000);
			break;
		case "saveAs":
			save(args);
			break;
		case "openPopup":
			tmp=args.split(";");
			path=tmp[0];
			ht=Number(tmp[1]);
			wd=Number(tmp[2]);
		 	tp = (window.screen.height-ht)/2;;
 			lf = (window.screen.width-wd)/2;
			window.open(path,'newPop','toolbar=0,scrollbar=0,height='+ht+',width='+wd+',left='+lf+',top='+tp+'');
			break;
  }
}
		
function save(xmlString){ 
	saveAs.document.open('text/html','replace'); 
	saveAs.document.write(xmlString); 
	saveAs.document.close(); 
	saveAs.focus(); 
	saveAs.document.execCommand('SaveAs',false,'Notes');
} 

// Hook for 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 shell_FSCommand(ByVal command, ByVal args)\n');
	document.write('  call shell_DoFSCommand(command, args)\n');
	document.write('end sub\n');
	document.write('</SCRIPT\> \n');
}
function startCourse(){
	document.getElementById("shell").StopPlay();
	api = getAPIHandle();
	if(api!=null) {
		finished=false;
		LMSInitialize();
		data=LMSGetValue("cmi.core.lesson_status");
		if(data=="not attempted"){
			LMSSetValue("cmi.core.lesson_status","incomplete");
		}
		document.getElementById("shell").Play();
	}else{
		document.getElementById("shell").Play();
	}
	initalTime=new Date();
}

function closeCourse(){
	
	if (api != null && !finished) {
		
		var data=document.getElementById("shell").GetVariable("core_lesson");
		//alert("core_lesson "+data);
		LMSSetValue("cmi.suspend_data",data);
		data=document.getElementById("shell").GetVariable("bookmark");
		//alert("bookmark "+data);
		LMSSetValue("cmi.core.lesson_location",data);
		data=document.getElementById("shell").GetVariable("course_status");
		//alert("course_status "+data);
		LMSSetValue("cmi.core.lesson_status",data);
		data=document.getElementById("shell").GetVariable("score");
		//alert("score "+data);
		if(!isNaN(parseInt(data))) {
			//alert("score "+data);
			LMSSetValue("cmi.core.score.raw",data);
		}
		//data=document.getElementById("shell").GetVariable("scoreMax");
		//LMSSetValue("cmi.core.score.max",data);
		doLMSFinish();
	}
}
function doLMSFinish(){
	if (api != null && !finished) {
		var timespent=gettSessionTime();
		LMSSetValue("cmi.core.session_time",timespent);
		LMSCommit();
		LMSFinish();
		finished=true;
	}
}
function MillisecondsToCMIDuration(n) {
//Convert duration from milliseconds to 0000:00:00.00 format
	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 gettSessionTime() {
	var dtm = new Date();
	var n = dtm.getTime() - initalTime.getTime();
	return MillisecondsToCMIDuration(n);
}