var findAPITries=1;
var API = null;
var course_status=""
var lessonStatus =""
var API_Extended = null; // Set extended API to null
//updated for avoiding hand cursor and flash plugin active message in browser by Navin 28th June'07 stats here
document.write("<HTML><HEAD><meta http-equiv=Content-Type content=text/html;  charset=ISO-8859-1\"><TITLE>:: Pioneer ::</TITLE>");
		document.write("<scr");
		document.write("ipt language=\"javascript\">initSco();</scr");
		document.write("ipt>");

		document.write("</HEAD><BODY marginheight='0' marginwidth='0' leftmargin='0' topmargin='0' bgcolor=\"#D5D5D5\" onUnLoad=\"finish()\"><table align =\"center\" cellpadding=\"0\" cellspacing=\"0\" height =\"100 %\"  width =\"100 %\" ><tr><td align=\"center\" valign=\"middle\"><OBJECT classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0\" WIDTH=\"1002\" HEIGHT=\"620\" id=\"flashMovie\" ALIGN=\"center\"> <PARAM NAME=movie VALUE=\"shell.swf?BookMark_data="+fnGetBookMark()+"&course_stat="+fnCompStatus()+"\"> <PARAM NAME=quality VALUE=high> <PARAM NAME=menu VALUE=false> <PARAM NAME=loop VALUE=false><PARAM NAME=bgcolor VALUE=#19512A> <EMBED src=\"shell.swf?BookMark_data="+fnGetBookMark()+"&course_stat="+fnCompStatus()+"\" quality=high bgcolor=#19512A  WIDTH=\"1002\" HEIGHT=\"620\" NAME=\"flashMovie\" ALIGN=\"center\" TYPE=\"application/x-shockwave-flash\" PLUGINSPAGE=\"http://www.macromedia.com/go/getflashplayer\"></EMBED></OBJECT></td></tr></table></BODY></HTML>");

//Code ends here


//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"));
			lessonStatus = String(get_val("cmi.core.lesson_status"));
			code = API.LMSGetLastError();
			ret = API.LMSGetErrorString( code );
			diag = API.LMSGetDiagnostic( "" );	

		}
	
	
}

//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);

						//stop calculating the time 
			sTime=String(stopclock());
			//alert("Session time is "+sTime);
			//set the time taken in the current session to the LMS
			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.core.lesson_location",course_status);
set_val("cmi.suspend_data",course_status);

	if((completionStatus.length>0)&&(completionStatus!="incomplete"))
	{
 		  set_val("cmi.core.lesson_status",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)
{
	
	set_val("cmi.core.score.raw",score);

	
}

//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)
{
	//alert(completionStatus);
	set_val("cmi.core.lesson_status",completionStatus);
	
}

function fnCompStatus(){

return  lessonStatus;
}

