var findAPITries=1;
var API = null;
var	timerID = null
var	timerRunning = false
var	startDate
var	startSecs
var timeValue
var lessonStatus=false;
var SendCompletionStatus = false;
var Bookmark_location=""
var SetMasteryScore=80;

//-----------------------------------------------------------------------
/*
function myFlash_DoFSCommand(command, args) 
 {
 //alert("Fs command"+command);
 switch(command){

 case "BookMark":
	set_location(args)
 break;
 case "score":
	score(args)
 break; 
case "Exit":
	fnCourseClose();
break;
case "openpdf": 
	OpenPDFDocuments(args);
break;
case "CourseCompletion":
	fnLessonCompleted(args);
break;
}
*/
//-----------------------------------------------------------------------
function FindAPI(win)
{
   while ((win.API == null) && (win.parent != null) && (win.parent != win))
   {
      findAPITries++;
      // Note: 7 is an arbitrary number, but should be more than sufficient
      if (findAPITries > 7) 
      {
        parent.status = "Error finding API -- too deeply nested.";
         return null;
      }
      
      win = win.parent;

   }
   return win.API;
}
//-----------------------------------------------------------------------
function GetAPI()
{

   var API = FindAPI(window);
   if ((API == null) && (window.opener != null) && (typeof(window.opener) != "undefined"))
   {
      API = FindAPI(window.opener);
   }
   if (API == null)
   {
     parent.status = "Unable to find an API adapter";
   }

   return API
}
//-----------------------------------------------------------------------
function initSco()

{
		API = GetAPI();
		if( API != null )
		{
			var ret;
			var code;
			var diag;
			API.LMSInitialize("");
			startclock();
			
			set_val("cmi.core.score.max",100);
			set_val("cmi.core.score.min",0);

			var status = get_val("cmi.core.lesson_status")
			if (status == "not attempted")
			{
			  // the student is now attempting the lesson
			  set_val("cmi.core.lesson_status","incomplete");
			}

			tempPage=get_val("cmi.suspend_data")
				
			Bookmark_location=tempPage;	

			code = API.LMSGetLastError();
			ret = API.LMSGetErrorString( code );
			diag = API.LMSGetDiagnostic( "" );	

		}
	
}
function set_val( gname,gvalue )
{
	API = GetAPI();
	if( API != null )
	{
		var ret;
		var code;
		var diag;		

		API.LMSSetValue( gname, gvalue );		

		code = API.LMSGetLastError();
		ret = API.LMSGetErrorString( code );	
		diag = API.LMSGetDiagnostic( "" );		
		commit();
		
	}
	
};
//-----------------------------------------------------------------------
function get_val( gname )
{	
	API = GetAPI();
	if( API != null )
	{
	
	var ret1,ret2;
		var code;
		var diag;
				
		ret1 = API.LMSGetValue( gname );		
		code = API.LMSGetLastError();
		ret2 = API.LMSGetErrorString( code );
		diag = API.LMSGetDiagnostic( "" );	
		return ret1;		
		
	}
	
};
//-----------------------------------------------------------------------
function commit()
{	
	API = GetAPI();
	if( API != null )
	{
		var ret = "";
		var code;
		var diag;
		
		API.LMSCommit("");
		code = API.LMSGetLastError();
		ret = API.LMSGetErrorString( code );
		diag = API.LMSGetDiagnostic( "" );			
	}
	
};
//-----------------------------------------------------------------------
function finish()
{
		API = GetAPI();
			
		if( API != null )
		{
		
			var ret;
			var code;
			var diag;			
			
			set_val("cmi.suspend_data",Bookmark_location)			
			sTime=stopclock();
			sTime=String(sTime)
			set_val("cmi.core.session_time",sTime);
			
			ret = API.LMSFinish("");
			code = API.LMSGetLastError();
			ret = API.LMSGetErrorString( code );
			diag = API.LMSGetDiagnostic( "" );
		}	
};

function stopclock()
{

	if(timerRunning){
		clearTimeout(timerID)
		timerRunning = false
		return timeValue
}
}
//-----------------------------------------------------------------------
function startclock()
{

	startDate = new Date()
	startSecs = (startDate.getHours()*60*60) + (startDate.getMinutes()*60) 
						+ startDate.getSeconds()

	stopclock()
		showtime()
	
}
//-----------------------------------------------------------------------
function showtime()
{
	// this doesn't work correctly at midnight...

	var now = new Date()
	var nowSecs = (now.getHours()*60*60) + (now.getMinutes()*60) + now.getSeconds()
	var elapsedSecs = nowSecs - startSecs;

	var hours = Math.floor( elapsedSecs / 3600 )
	elapsedSecs = elapsedSecs - (hours*3600)

	var minutes = 	Math.floor( elapsedSecs / 60 )
	elapsedSecs = elapsedSecs - (minutes*60)

	var seconds = elapsedSecs

	timeValue = "" + hours
		if(hours<10){
		timeValue = "0" + hours
	}
	timeValue  += ((minutes < 10) ? ":0" : ":") + minutes
	timeValue  += ((seconds < 10) ? ":0" : ":") + seconds

		// Update display
	timerID = setTimeout("showtime()",1000)
	timerRunning = true
}

function fnGetBookMark()
{
	//alert("fnGetBookMark"+Bookmark_location);
	return Bookmark_location;
}



function set_location(setbookmark)
{
	Bookmark_location=setbookmark;
	set_val("cmi.suspend_data",Bookmark_location);
	//alert("fnGetBookMark"+Bookmark_location);
}

//Set the score
function score(score)
{
	
	set_val("cmi.core.score.raw",score);	
	if(score>=SetMasteryScore)
	{	
	 set_val("cmi.core.lesson_status","passed");

	}else{

		 set_val("cmi.core.lesson_status","failed");
	
	}
}


function fnLessonCompleted(param)
{
	if(SendCompletionStatus == false)
	{
		set_val("cmi.core.lesson_status","completed");
		SendCompletionStatus = true;
	}		
}



function fnCourseClose()
{
	var confirm;	
	confirm=window.confirm('This will close the E-learning module. Do you want to exit the Course?');	
	if(confirm)
	{		
		window.close();		
	}
}



//open the pdf documents
function OpenPDFDocuments(param)
{
	var win = "Cigna_eProNav_PDF";   
    var openWin=window.open(param,win)
}


//open the HTML Page
function OpenHTMLPage(param)
{	 
    var openWin=window.open(param,"")
}
