// AICC HACP javascript for when the course & lms are in seperate domains - 1 way communication

var RHdocHREF, RHaiccSID, RHaiccURL, RHaiccDATA, exitAfter;
exitAfter = false;

//alert("EH 29/03/07 - AICC_hacp")

function loadPage()
{
	startTimer();
	//alert("EH 29/03/07 - LoadPage called")
	//call getParam after 4 seconds to allow aiccForm frame to load.
	window.setTimeout("getParam()",4000);
}

if (location.search != parent.location.search) location.search = parent.location.search;

function dataReady()
{
	//This function is called when the aiccData Frame loads
	if (exitAfter)
	{
		exitAU();
		exitAfter = false;
	}

}

function dataGone()
{
	//alert("Bamm!");
	//This function is called when the aiccData Frame UNloads
	//if (parent.data.location.href.indexOf("aiccForm.html") == -1)
	//window.setTimeout("parent.data.location.href='aiccForm.html'",2000);
}

function exitAU()
{
	//alert("EH 29/03/07 - ExitAU called")
	parent.data.document.forms[0].action = parent.RHaiccURL;
	parent.data.document.forms[0].aicc_data.value = "";
	parent.data.document.forms[0].command.value = "ExitAU";
	parent.data.document.forms[0].session_id.value = parent.RHaiccSID;
	parent.data.document.forms[0].submit();
}

function getParam()
{
	//alert("EH 29/03/07 - GetParam called")
	parent.data.document.forms[0].action = parent.RHaiccURL;
	parent.data.document.forms[0].aicc_data.value = "";
	parent.data.document.forms[0].command.value = "getparam";
	parent.data.document.forms[0].session_id.value = parent.RHaiccSID;
	parent.data.document.forms[0].submit();
}


function recordProgress(progress,score,exit)
{
	//alert("EH 29/03/07 - record Progress")
	parent.data.document.forms[0].action = parent.RHaiccURL;

	var outputaiccDATA  = "";
	
	outputaiccDATA += "%5BCORE%5D%0D%0A";
	outputaiccDATA += "time%3D" + computeTime() + "%0D%0A";
	outputaiccDATA += "lesson_location%3D" + score + "%0D%0A";
	var adjustedProgress;
	if (progress.toLowerCase().indexOf("passed") > -1 || progress.toLowerCase() == "complete" ) adjustedProgress = "Complete";
	else adjustedProgress = "Incomplete";
	outputaiccDATA += "lesson_status%3D" + adjustedProgress + "%0D%0A";
	
	var displayedScore;
	
	if (parseInt(score) >= 80) displayedScore = 100;
	else displayedScore=0;
	outputaiccDATA += "score%3D" + displayedScore + "%0D%0A";	
	//alert("record progress:" + unescape(outputaiccDATA));
	parent.data.document.forms[0].aicc_data.value = unescape(outputaiccDATA);
	parent.data.document.forms[0].command.value = "putparam";
	parent.data.document.forms[0].session_id.value = parent.RHaiccSID;
	parent.data.document.forms[0].submit();
	
	if (exit==1) window.setTimeout("exitAU()",3000);
	
}

function closeLO()
{
	top.close();
}

function startTimer()
{
   startDate = 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";
   }

   return formattedTime;
}

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;
}