var startDate;
var startDate2;
var LMSInitialized;
var exitPageStatus;

function loadPage()
{
   var LMSInitialized = doLMSInitialize();
   var status = doLMSGetValue( "cmi.core.lesson_status" );
   if (status == "not attempted")
   {
	  doLMSSetValue( "cmi.core.lesson_status", "incomplete" );
   }
   exitPageStatus = false;
   suspendData = "";
   startTimer();
}

function startTimer()
{
   startDate = new Date().getTime();
   startDate2 = new Date().getTime();
}

function computeTime()
{
   if ( startDate != 0 )
   {
      var currentDate = new Date().getTime();
      var elapsedSeconds = ( (currentDate - startDate) / 1000 );
      var formattedTime = convertTotalSeconds( elapsedSeconds );
   }
   else
   {
      formattedTime = "00:00:00.0";
   }
   doLMSSetValue( "cmi.core.session_time", formattedTime );
}

function doQuit()
{
   doLMSSetValue( "cmi.core.exit", "suspend" );
   computeTime();
   exitPageStatus = true;  
   var result;
   result = doLMSCommit();
   result = doLMSFinish();
}

/*******************************************************************************
** 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;

   return rtnVal;
}

function choice(d,o,nav) {
  var totalInteractions = doLMSGetValue("cmi.interactions._count");
  doLMSSetValue("cmi.interactions."+totalInteractions+".id",1);
  doLMSSetValue("cmi.interactions."+totalInteractions+".type","choice");
  doLMSSetValue("cmi.interactions."+totalInteractions+".correct_responses.0.pattern",question_response(d));
  doLMSSetValue("cmi.interactions."+totalInteractions+".student_response",o);
  if (o==question_response(d)) {
    doLMSSetValue("cmi.interactions."+totalInteractions+".result","correct");
  } else {
    doLMSSetValue("cmi.interactions."+totalInteractions+".result","wrong");
  }
  var suspendData = doLMSGetValue("cmi.suspend_data");   
  suspendData += '{"D":"'+d+'","I":"'+totalInteractions+'","N":"'+nav+'"}"';
  doLMSSetValue("cmi.suspend_data",suspendData);
  doLMSCommit();  
}

function completar() {
	if (exitPageStatus != true)
	{
    var status = doLMSGetValue( "cmi.core.lesson_status" );
    if (status != "completed")
    {		  
	   	 doLMSSetValue("cmi.core.lesson_status","completed");
	  }	      
    doLMSSetValue('nav.event','continue');
    var coreSCOLocation = doLMSGetValue("cmi.core.lesson_location");
    window.location = coreSCOLocation;
  }
} 

function retornar(d,o,nav) {
  choice(d,o,nav);
}

function prosseguir(d,o,nav) {
  choice(d,o,nav);
	if (exitPageStatus != true)
	{
    var status = doLMSGetValue( "cmi.core.lesson_status" );
    if (status != "completed")
    {		   
	   	 doLMSSetValue("cmi.core.lesson_status","completed");
	  }	      
    doLMSSetValue('nav.event','continue');
    var coreSCOLocation = doLMSGetValue("cmi.core.lesson_location");
    window.location = coreSCOLocation;
  }      
} 

function finalizar_caso() {
	if (exitPageStatus != true)
	{
    var status = doLMSGetValue( "cmi.core.lesson_status" );
    if (status != "completed")
    {		
       doLMSSetValue("cmi.core.score.raw","100");    
	   	 doLMSSetValue("cmi.core.lesson_status","completed");
	  }	      
    doQuit();
  }    
}

function question_response(d) {
  var questionResponse = new Array("NA","b","a","a","b");
  return questionResponse[d];
}

function question_feedback(d,o) {
  var resp = "";  
  var questionFeedback;
  if (o=="a") questionFeedback = new Array("NA","F0001","F0004","F0007","F0010");
  if (o=="b") questionFeedback = new Array("NA","F0002","F0005","F0008","F0011");
  if (o=="c") questionFeedback = new Array("NA","F0003","F0006","F0009","F0012");
  resp = questionFeedback[d];
  return resp;
}

function question_feedback_anchor(d,o) {
  var resp = "";  
  var questionFeedback;
  if (o=="a") questionFeedback = new Array("","","","","","","","");
  if (o=="b") questionFeedback = new Array("","","","","","","","");
  if (o=="c") questionFeedback = new Array("","","","","","","","");
  resp = questionFeedback[d];
  return resp;
}