var findAPITries=1;
var API = null;
var	timerID = null
var	timerRunning = false
var	startDate
var	startSecs
var timeValue
var lessonStatus=false;
var Bookmark_location="undefined";
var SetMasteryScore=80;
var LMSStatus;
//-----------------------------------------------------------------------
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.core.lesson_location")
			//alert("tempPage "+tempPage);
			//alert("lesson_status "+get_val("cmi.core.lesson_status"));
			var tmp = tempPage.split("^");			
			//if(tempPage != ""){
			if(Number(tmp[0])>=0){
			//alert("tempPage "+tempPage)
			//tempPage=Number(tempPage)
				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( "" );		
	}
	
};
//-----------------------------------------------------------------------
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( "" );
	//alert ("inside "+gname+ " "+ret1)
	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()
{
//if(callUnload == true){	
	//alert(lessonStatus);
		API = GetAPI();
			
		if( API != null )
		{
		
			var ret;
			var code;
			var diag;
			
			//alert("parent.next "+parent.nextCount)
			//alert("Bookmark_location in finish: "+Bookmark_location);
			set_val("cmi.core.lesson_location",Bookmark_location);
			//alert(get_val("cmi.core.lesson_location"))
			sTime=stopclock();
			sTime=String(sTime)
			set_val("cmi.core.session_time",sTime);
			//if(lessonStatus){
				
			 //set_val("cmi.core.lesson_status","completed");
			//}
			commit();
			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()

{

return Bookmark_location

}

function set_location(setbookmark)
{
Bookmark_location=setbookmark;
set_val("cmi.core.lesson_location",Bookmark_location);
commit();
//alert("Bookmark_location: "+Bookmark_location);
}

function fnSetLastPage(lastpge) {

lessonStatus= lastpge

}
function sendScore(score) {	
	//var stat = (score >= SetMasteryScore? "passed" : "failed");
	var stat = (score >= SetMasteryScore? "completed" : "incomplete");
	//alert("score:"+score+" status: "+stat);
	set_val("cmi.core.score.raw", score);
	//set_val("cmi.student_data.mastery_score",score);
	set_val("cmi.core.lesson_status", stat);
	commit();
	//alert("score: "+score);
}
function fnGetData() {
	//var LMSStatus = get_val("cmi.core.lesson_status");
	return Bookmark_location;
}
function setScore(score, bookmark) {	
	var stat = (score >= SetMasteryScore? "passed" : "failed");
	//var stat = (score >= SetMasteryScore? "completed" : "incomplete");
	//alert("score:"+score+" status: "+stat);
	set_val("cmi.core.score.raw", score);
	//set_val("cmi.student_data.mastery_score",score);
	set_val("cmi.core.lesson_status", stat);
	Bookmark_location=bookmark;	
	commit();
	//alert("score: "+score+" & bookmark: "+bookmark);
}
function setCompletion(lStatus, bookmark) {	
	set_val("cmi.core.lesson_status", lStatus);
	Bookmark_location=bookmark;	
	commit();
	//alert("score: "+score+" & bookmark: "+bookmark);
}