// JavaScript Document

var startTime;
var startHour;
var startMinutes;
var startSeconds;
var nowTime;
var nowHour;
var nowMinutes;
var nowSeconds;
var thisSessionHour;
var thisSessionMinutes;
var thisSessionSeconds;
var lessSeconds;
var thisHr;
var thisMn;
var thisSc;
var timeSpan;

/*****************************************************************************/
function Initialize() {
	// make initialize call
	doLMSInitialize();
	
	// read the current time on the computer clock when the page is opened
	startTime = new Date();
	startHour = startTime.getHours();
	startMinutes = startTime.getMinutes();
	startSeconds = startTime.getSeconds();

	// set completion status to complete
	SetComplete();
}

/*****************************************************************************/
function Terminate() {
	
	// now get the current date and time on the computer clock
   nowTime = new Date();
   nowHour = nowTime.getHours();
   nowMinutes = nowTime.getMinutes();
   nowSeconds = nowTime.getSeconds();
	
	updateSessionTime();
	
	//doLMSSetValue("cmi.core.session_time","05:03:02")
	
	doLMSFinish();
}

/******************************************************************************/
function SetComplete (){
	doLMSSetValue( "cmi.core.lesson_status", "completed" );
}

/******************************************************************************/

function updateSessionTime() {
   //calculate hours
   /*if the minutes when the course was closed is lower than the minutes when it was 
   opened, at least one hour has elapsed that was not accounted for in the hours
   calculation. Therefore we need to reduce the nowHour time by 1 hour, so that when 
   we add on 24 to make the number positive, we account for this*/
   if (nowMinutes < startMinutes) {
	   nowHour = nowHour - 1;
   }
   thisSessionHour = nowHour - startHour;
   if (thisSessionHour < 0) {
	   thisSessionHour = thisSessionHour + 24;
   }
   
   //calculate minutes
   /*if the seconds when the course was closed is lower than the seconds when it was 
   opened, at least one minute has elapsed that was not accounted for in the minutes
   calculation. Therefore we need to reduce the nowMinutes time by 1 minute, so that when 
   we add on 60 to make the number positive, we account for this*/
   if (nowSeconds < startSeconds) {
	   nowMinutes = nowMinutes - 1;
   }
   thisSessionMinutes = nowMinutes - startMinutes;
   if (thisSessionMinutes < 0) {
	   thisSessionMinutes = thisSessionMinutes + 60;
   }
   
   //calculate seconds
   /*if the seconds when the course was closed is lower than the seconds when it was 
   opened, we need to make sure that we show the correct amount of seconds as the minue
   has changed. To do this we see if the seconds now is lower than when the course was
   opened, and if so, subtract the total seconds from 60.*/
   if (nowSeconds <= startSeconds) {
	   lessSeconds = startSeconds - nowSeconds;
	   thisSessionSeconds = 60 - lessSeconds;
   } else {
	   thisSessionSeconds = nowSeconds - startSeconds;
   }
   
   /*thisSession times will be added on to current session times stored in Moodle so no
   need to create a total elapsed time from the existing stored time + thisSession times*/
   if (thisSessionHour < 10) thisSessionHour = "0" + thisSessionHour;
   if (thisSessionMinutes < 10) thisSessionMinutes = "0" + thisSessionMinutes;
   if (thisSessionSeconds < 10) thisSessionSeconds = "0" + thisSessionSeconds;
   
   //convert thisSession integers to strings
   thisHr = thisSessionHour.toString();
   thisMn = thisSessionMinutes.toString();
   thisSc = thisSessionSeconds.toString();
   
   // prepare the CMITimespan string
   timeSpan = thisHr+":"+thisMn+":"+thisSc;
   doLMSSetValue("cmi.core.session_time",timeSpan);
} 