<!--SCOFunctions.js-->

/*******************************************************************************
**
** Concurrent Technologies Corporation (CTC) grants you ("Licensee") a non-
** exclusive, royalty free, license to use, modify and redistribute this
** software in source and binary code form, provided that i) this copyright
** notice and license appear on all copies of the software; and ii) Licensee
** does not utilize the software in a manner which is disparaging to CTC.
**
** This software is provided "AS IS," without a warranty of any kind.  ALL
** EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
** IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-
** INFRINGEMENT, ARE HEREBY EXCLUDED.  CTC AND ITS LICENSORS SHALL NOT BE LIABLE
** FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
** DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES.  IN NO EVENT WILL CTC  OR ITS
** LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
** INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
** CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
** OR INABILITY TO USE SOFTWARE, EVEN IF CTC  HAS BEEN ADVISED OF THE
** POSSIBILITY OF SUCH DAMAGES.
**
*******************************************************************************/
var startDate;
var exitPageStatus;
var userName="";
var masteryScore =80;//default for initialisation
var test ="this is tes var";
var lastVisitedfromLMS="";
var lastVisitedToLMS="";
var statusfromLMS="";
var completedTopicsStrFromLMS="";
var completedTopicsStrToLMS="";
var statusToLMS="incomplete";
var scoreToLMS=0;
var totalTimeFromLMS = "00:00:00";



//This function is called when the page is laoded.In this function we have to set the values to the flash movieClip

function preInitialize(){
	loadPage();
	
	

 }
 


function loadPage()
{
	result = LMSInitialize();
	statusfromLMS = getStatus();
	//alert("status" +  statusfromLMS);
	if (statusfromLMS == "not attempted"){
	// the student is now attempting the lesson
		LMSSetValue( "cmi.core.lesson_status", "incomplete" );
	}
 lastVisitedfromLMS= getBookmark();
 totalTimeFromLMS = getTotalTime();
masteryScore = getMasteryScore() ;
//completedTopicsStrFromLMS = getSuspendData();
   userName = getUserName();

   
   if(userName!=null)
   	userName  = repalceComma(userName);

   exitPageStatus = false;
    startTimer();

// preset the variables that go to LMs to the one that came from LMS
 lastVisitedToLMS =   lastVisitedfromLMS;
    statusToLMS = statusfromLMS;

}



function doQuit()
{
	
 	computeTime();
 	setBookmark(lastVisitedToLMS);
 	setScore(scoreToLMS);

	//if(statusToLMS != "completed"){
	setStatus(statusToLMS);
	//}
		
 	exitPageStatus = true;
 	  
 	LMSCommit();

	// NOTE: LMSFinish will unload the current AU.  All processing
	// relative to the current page must be performed prior
	// to calling LMSFinish.   

	LMSFinish();   
}

function unloadPage()
{
/*******************************************************************************
** The purpose of this function is to handle cases where the current AU may be 
** unloaded via some user action other than using the navigation controls 
** embedded in the content.   This function will be called every time an AU
** is unloaded.  If the user has caused the page to be unloaded through the
** preferred AU control mechanisms, the value of the "exitPageStatus" var
** will be true so we'll just allow the page to be unloaded.   If the value
** of "exitPageStatus" is false, we know the user caused to the page to be
** unloaded through use of some other mechanism... most likely the back
** button on the browser.  We'll handle this situation the same way we 
** would handle a "quit" - as in the user pressing the AU's quit button.
*******************************************************************************/

	if (exitPageStatus != true)
	{
		doQuit();
		
	}

	// NOTE:  don't return anything that resembles a javascript
	//		  string from this function or IE will take the
	//		  liberty of displaying a confirm message box.
	
}

function setBookmark(lvStr) {
	//alert("In setBookmark");
	LMSSetValue("cmi.core.lesson_location", lvStr );
}

function getBookmark() {
	return LMSGetValue("cmi.core.lesson_location");
}


function getUserName() {
	return LMSGetValue("cmi.core.student_name");
}

function getSuspendData(){
	//return LMSGetValue("cmi.suspend_data");
	return LMSGetValue("cmi.core.lesson_location");
}

function setSuspendData(str){
	LMSSetValue("cmi.core.lesson_location", completedTopicsStrToLMS );
}
function setStatus(status) {
	//alert("In setBookmark");
	LMSSetValue("cmi.core.lesson_status", status );
}

function getStatus() {
	return LMSGetValue("cmi.core.lesson_status");
}
function setScore(score) {
	
	LMSSetValue("cmi.core.score.raw", score + "" );
}

function getScore() {
	return LMSGetValue("cmi.core.score.raw");
}
function getTotalTime() {
	
	return LMSGetValue("cmi.core.total_time");
}
function getMasteryScore() {
	
	return LMSGetValue("cmi.student_data.mastery_score");
}



function repalceComma(str){
	
	nameArray = new Array();
	nameArray = str.split(",");
	count= 1;
	nameStr = nameArray[0] ; 
	while(count < nameArray.length){
  		nameStr = nameStr + " " + nameArray[count++] ;
  	}
  	return nameStr;
}	

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.0";
   }

   LMSSetValue( "cmi.core.session_time", formattedTime );
}
  
/*******************************************************************************
** 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;
}
   

