  /***************************                               ***********************************/
 /***************************  INTERNAL FUNCTIONS AND DATA  ***********************************/
/***************************                               ***********************************/

var LMS = new Object();
LMS.today          = SCORMdate(new Date());
LMS.interactionCnt = 0;
LMS.lesson_id      = "lesson2"; // Developer defined id - must be uniqe for this learning object

function join( arr, separator )
{
   res = "";

   for( i = 0; i < arr.length; i++ )
   {
      if( i )  res += separator;
      if( arr[ i ] <= 9 ) res += "0";
      res += arr[ i ];
   }

   return res;
}

function SCORMdate(date)  {  return join([ date.getFullYear(), date.getMonth()+1, date.getDate()],    "/" );}
function SCORMtime(time)  {  return join([ time.getHours(),    time.getMinutes(), time.getSeconds()], ":" );}
function secsToTime( sec ) { return join([ Math.floor( sec/3600 ), Math.floor((sec%3600)/60), sec%60], ":");}

function addInteraction(id,type,correct,response,result,latency)
{
   var cnt = LMS.interactionCnt;
   var now = SCORMtime( new Date());
   var interaction = "cmi.interactions." + cnt;
   var weight = 1; // We normally don't weight our exercises

   if( API )
   {
      /*if( cnt == 0 ) // Need only set these values once
      {
         API.LMSSetValue( "cmi.interactions.lesson_id", LMS.lesson_id);
         API.LMSSetValue( "cmi.interactions.date",      LMS.today );
      }*/

      API.LMSSetValue( interaction + ".id",                          id       );
      API.LMSSetValue( interaction + ".time",                        now      );
      API.LMSSetValue( interaction + ".type",                        type     );
      API.LMSSetValue( interaction + ".correct_responses.0.pattern", correct  );
      API.LMSSetValue( interaction + ".weighting",                   weight   );
      API.LMSSetValue( interaction + ".student_response",            response );
      API.LMSSetValue( interaction + ".result",                      result   );
      API.LMSSetValue( interaction + ".latency",                     latency  );

      API.LMSCommit("");
   }
/*else
{
alert("noLMS");
}*/

   LMS.interactionCnt++;
}

// returns the LMS API object (may be null if not found)
function FindAPIinWin(win)
{  
   if (win.API != null) 
      return win.API;  
   for( var i = 0; i < win.frames.length; i++)
	  if( API = FindAPIinWin( win.frames[ i ])) return API;
}

function findAPI()
{

	// 28.11.02
	// First search the parent
	// Oracle support
	API = FindAPIinWin(this.parent);

	
	//alert( API == null );


	if( API == null )
	{
		// obtain the LMS API
		API = FindAPIinWin(window.top);
	}

	if (API == null && window.opener != null) 
	   API = FindAPIinWin(window.opener);

	if (API == null) 
	{
	   //alert("Error: Could not find API");
	}
	
	return API;

}


  /**************************                             **********************/
 /**************************  PUBLIC FUNCTIONS AND DATA  **********************/
/**************************                             **********************/

function initialize()
{

   if( API != null)
   {

      API.LMSInitialize("");

      lessonStatus = API.LMSGetValue( "cmi.core.lesson_status" );

      if( lessonStatus[0] != "p" && lessonStatus[0] != "c" && lessonStatus[0] != "f" )
      {
         API.LMSSetValue( "cmi.core.lesson_status", "incomplete" );
         API.LMSCommit("");
      }
   }
 
   timeOfInit = new Date();
}


function courseCompleted()
{

   if( API )
   {
      lessonStatus = API.LMSGetValue( "cmi.core.lesson_status" );

      if( lessonStatus[0] != "p" && lessonStatus[0] != "c" )
      {

          //API.LMSSetValue( "cmi.core.lesson_status", "attempted" );
      	  API.LMSSetValue( "cmi.core.lesson_status", "completed" );
     	  API.LMSCommit("");
      }
   }
}



function setCompletion( value )
{
   if( API )
   {
      lessonStatus = API.LMSGetValue( "cmi.core.lesson_status" );

      // Do not alter if user has completed or passed allready
      if( lessonStatus[0] != "p" && lessonStatus[0] != "c" )
      {
	  //alert("Status = " + value );
      	  API.LMSSetValue( "cmi.core.lesson_status", value );
      	  API.LMSCommit( "" );
      }
   }
}


function setScore( value )
{

   if( API )
   {
      lessonStatus = API.LMSGetValue( "cmi.core.lesson_status" );

      // Do not alter if user has completed or passed session
      if( lessonStatus[0] != "p" && lessonStatus[0] != "c" )
      {
      	  API.LMSSetValue( "cmi.core.score.raw", value );
      	  API.LMSCommit( "" );
      }
   }

}


function setScoreAndCompletion( value, completion )
{

   if( API )
   {
      lessonStatus = API.LMSGetValue( "cmi.core.lesson_status" );

      // Do not alter if user has completed or passed session
      if( lessonStatus[0] != "p" && lessonStatus[0] != "c" )
      {
      	  API.LMSSetValue( "cmi.core.score.raw", value );
      	  API.LMSCommit( "" );

      	  API.LMSSetValue( "cmi.core.lesson_status", completion );
      	  API.LMSCommit( "" );
      }
   }

}


function finish()
{
   if( API )
   {
     timeOfCompletion = new Date();
     timeUsed = secsToTime( Math.floor(( timeOfCompletion.getTime() - timeOfInit.getTime())/1000 ));

     //alert( "time used: " + timeUsed)
   
     //alert( 'calling LMSFinish' );

     //courseCompleted();


     //lessonStatus = API.LMSGetValue( "cmi.core.lesson_status" );

     //if( lessonStatus[0] != "p" && lessonStatus[0] != "c" )
     //{
	  API.LMSSetValue( "cmi.core.session_time", timeUsed );
    	  API.LMSCommit(""); 
     //}

     API.LMSFinish("");
   }
}

// Example:
// addMultipleChoice( "ex1", "a,b", "a,c","wrong","0:0:12" );
function addMultipleChoice(id,correct,response,result,latency)
{ addInteraction(id,"choice",correct,response,result,latency);}

// Example:
// addSlider( "ex1", "", "3","","0:0:12" );
// addSlider( "ex2", "choice 3", "choice 2","wrong","0:0:12" );
function addSlider(id,correct,response,result,latency)
{   addInteraction(id,"likert",correct,response,result,latency);}

// Example:
// addFreetext( "ex3", "Platon", "Platon","correct","0:0:12" );
function addFreetext(id,correct,response,result,latency)
{
//alert( "addInteraction(" + id + ",'fill-in',"+ correct+ ","+ response+ ","+ result+ ","+ latency+ ")" );   
addInteraction(id,"fill-in",correct,response,result,latency);
}

// Example:
// addDragDrop( "ex4", "{a.b,e.f,c.b}", "a.b,e.f,c.f","wrong","0:0:12" );
function addDragDrop(id,correct,response,result,latency)
{   addInteraction(id,"matching",correct,response,result,latency);}



