/*******************************************************************************/
//william.boccassi@allos.it
/*******************************************************************************/
var exitPageStatus;
var startDate;
var book;
var visited;
//loadPage
function loadPage(){
	LMSInitialize();
	if (apiHandle!=null){
		//CORE
		var student_id = LMSGetValue( "cmi.core.student_id" );
		var student_name = LMSGetValue( "cmi.core.student_name" );
		var lesson_status = LMSGetValue( "cmi.core.lesson_status" );
		var lesson_location = LMSGetValue( "cmi.core.lesson_location");
		var total_time = LMSGetValue( "cmi.core.total_time" );
		var suspend_data = LMSGetValue( "cmi.suspend_data" );
		book=lesson_location;
		visited=suspend_data;
		var debug_str='';
		startDate = new Date().getTime();
			if (lesson_status == "not attempted"){
				LMSSetValue( "cmi.core.lesson_status", "incomplete" );
			}
		exitPageStatus = false;
		
		if (_Debug == true){
			debug_str += 'cmi.core.student_id=';
			debug_str += student_id;
			debug_str += '\n';
			debug_str += 'cmi.core.student_name=';
			debug_str += student_name;
			debug_str += '\n';
			debug_str += 'cmi.core.lesson_status=';
			debug_str += lesson_status;
			debug_str += '\n';
			debug_str += 'cmi.core.lesson_location=';
			debug_str += lesson_location;
			debug_str += '\n';
			debug_str += 'cmi.core.total_time=';
			debug_str += total_time;
			debug_str += '\n';
			debug_str += 'cmi.core.suspend_data=';
			debug_str += suspend_data;
			//alert(debug_str);
		 }
	}	
}
//doQuit
function doQuit(){
	if (apiHandle!=null){
		preload.style.display = 'block';
		var currentDate = new Date().getTime();
    	var elapsedMills = currentDate - startDate;
    	var formattedTime = convertTotalMills( elapsedMills );
		LMSSetValue( "cmi.core.session_time", formattedTime );
		LMSSetValue( "cmi.core.exit", "logout" );
		exitPageStatus = true;
		LMSCommit();
		LMSFinish();

	} else {
	exitPageStatus = true;
	window.close();
	}	
}
//UnloadPage
function unloadPage(){
	if (apiHandle!=null){
		if (exitPageStatus != true){
			doQuit();
		}
	}
}	
//Conevert Total Mills
function convertTotalMills(ts) {
  var Sec  = 0;
  var Min  = 0;
  var Hour = 0;
  while( ts >= 3600000 ) {
    Hour += 1;
    ts -= 360000;
  }
  while( ts >= 60000 ){
    Min += 1;
    ts -= 60000;
  }
  while ( ts >= 1000 ){
    Sec += 1;
    ts -= 1000;
  }
  if (Hour < 10) Hour = "0"+Hour;
  if (Min < 10) Min = "0"+Min;
  if (Sec < 10) Sec = "0"+Sec;
  var rtnVal = Hour+":"+Min+":"+Sec;
  return rtnVal;
}
//SetCompelete
function setComplete(){
	LMSSetValue( "cmi.core.lesson_status", "completed" );
}
//SetBook
function setBook(book){
	LMSSetValue( "cmi.core.lesson_location", book );
}
//SetVisited
function setVisited(visited){
	LMSSetValue( "cmi.suspend_data", visited );
}
//GetBookmark
function getBookmark(){
	main.setVariable("book",book);
}
//GetVisited
function getVisited(){
	main.setVariable("visited",visited);
}
//ResetAll
function resetAll(){
	LMSSetValue( "cmi.core.lesson_status", "incomplete" );
	LMSSetValue( "cmi.core.lesson_location", "" );
	LMSSetValue( "cmi.suspend_data", "" );
}
//TEMP FUNCTION (non fa nulla)--IN ATTESA SWF DEFINITIVO--
function Pagina(url){
	
}
