
var courseType = window.top.courseType1;
var findAPITries=1;
var API = null;
var course_status=""
var lessonStatus =""
var API_Extended = null; // Set extended API to null
var completionStatus_lms = "";



//function to check the fpi
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;

   }
   API_Extended = win.API_Extended; // Find extended API
   return win.API;
   
}

//function to get the fpi
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 to initialise the sco
function initSco()
{

            
		API = GetAPI();
		
		if( API != null )
		{
		
			var ret;
			var code;
			var diag;
			API.LMSInitialize("");
			//start calculating the session time
			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");
			}
			
						
			//course_status=String(get_val("cmi.core.lesson_location"));
			
			course_status=String(get_val("cmi.suspend_data"));
			//alert("course_status is "+ course_status);
			
			lessonStatus = String(get_val("cmi.core.lesson_status"));
	
			code = API.LMSGetLastError();
			ret = API.LMSGetErrorString( code );
			diag = API.LMSGetDiagnostic( "" );	
			//set_val("cmi.core.lesson_status","completed");

		}
	
	
}

//function to set the values of the variables in the LMS
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 to get the values of the variables from the LMS
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 to update the values of the variables in the LMS
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 to finish the LMS communication
function finish()
{
        
		API = GetAPI();
		
		if( API != null )
		{
		
			var ret;
			var code;
			var diag;
			
			//set_val("cmi.core.lesson_location",course_status);
			set_val("cmi.suspend_data",course_status);			

			if((completionStatus_lms.length>0)&&(completionStatus_lms!="incomplete"))
			{
				set_val("cmi.core.lesson_status",completionStatus_lms);		
				  //alert ( " completionStatus  "  +     completionStatus)	
				set_score(100)

			}


			//stop calculating the time 
			sTime=String(stopclock());
			//alert("Session time is "+sTime);
			//set the time taken in the current session to the LMS5a
			set_val("cmi.core.session_time",sTime);

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

		}

};


function stime()
{
if(	API_Extended != null)
	{
		
		sfailTime=String(stopclock1());
		set_val("cmi.core.session_time",sfailTime);
		//alert("Session time is sfailTime "+sfailTime);
	}


}

//this function is to retrieve the bookmark stored in the LMS
function fnGetBookMark()
{
	return course_status
}

//function to set the bookmark, set the course status and score
function set_location(bookmarkStr,completionStatus)
{

	course_status = bookmarkStr;

set_val("cmi.suspend_data",course_status);
	completionStatus_lms = completionStatus;
	

	//added by pravin  - commit after each and every call
	//commit();

}

//function to set the bookmark, set the course status and score
function set_score(score)
{
	//alert("score::"+score)
	set_val("cmi.core.score.raw",score);
if(score>=75)
{
	completionStatus="completed";
	set_val("cmi.core.lesson_status",completionStatus);
}
else

{
	completionStatus="incomplete";
	set_val("cmi.core.lesson_status",completionStatus);
}

	
}

//this function is to stop calculating the session time
function stopclock()
{

	if(timerRunning){
		clearTimeout(timerID)
		timerRunning = false
	return timeValue

}

}

//this function is to stop calculating the session time
function stopclock1()
{

	if(timerRunning){
		clearTimeout(timerID)
		//timerRunning = false
	return timeValue

}

}


//-----------------------------------------------------------------------
//this function is to start calculating the session time
function startclock()
{

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

	
}
//-----------------------------------------------------------------------
//this function is used to calculate the time
function showtime(){
	

	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
}

//this function exits the course after invoking finish call properly through extended api call
function fnExit()
{

if(	API_Extended != null)
	{
		stime();
		var can_nav_exit = API_Extended.GetNavCommandStatus("exit");
		if(can_nav_exit == true)
		{
			API_Extended.SetNavCommand("exit");
		}
	}else{
		top.close();
	}	
}


//Additionally added function by Srinivas to mark the course complete specifically on Click the Course Completion Button.

function markStatus(completionStatus)
{

	set_val("cmi.core.lesson_status",completionStatus);
	//alert ("  completionStatus  " +completionStatus )
	
}


function fnCompStatus()
{
	return  lessonStatus;
}
function fnGetStudentName()	
{
		//alert(get_val("cmi.core.student_name"))
		var studentName_lms = get_val("cmi.core.student_name");
		//var studentName_lms = "Srinivas"
		return studentName_lms;
}


function fnGetStudentId()	
{
		//alert(get_val("cmi.core.student_name"))
		var studentName_lms = get_val("cmi.core.student_id");
		//var studentName_lms = "813"
		return studentName_lms;
}