var win;
//window.moveTo(1024,0);
window.moveTo(0,0);
var userName;

//**************** error checking *************
var start_attempts = 0;
var finish_attempts = 0;
var num_attempts = 10;
var time_attempts = 200;
var err_lms_failure = "Failed to communicate with the LMS. Your score will not be recorded.";
var err_desc ="";
var err_desc_0="The content was able to disconnect from\r\n the LMS as per SCORM 1.1 specifications\r\n         LMSFinish function successful";
var err_desc_101="The content was not able to disconnect from the LMS as per SCORM 1.1 specifications\r\n\r\n                 Error 101 - General Exception. LMSFinish function failed";
var err_desc_102="The content was not able to disconnect from the LMS as per SCORM 1.1 specifications\r\n\r\n                 Error 102 - The server was busy. LMSFinish function failed";
var err_desc_unk="The content was not able to disconnect from the LMS as per SCORM 1.1 specifications\r\n\r\n                 Error unknown or not supported. LMSFinish function failed";
//*********************************************

function init() {
	doback();
	loadPages();	
}

function loadPages() 
{ 
	loadPage();

	if (catBound) 
	{
		start_attempts = 0;
		startContent();
	} 
	else 
	{
		if (start_attempts<num_attempts) 
		{
			start_attempts++
			setTimeout("loadPages()",time_attempts);
		} 
		else 
		{
			start_attempts = 0;
			startContent();
//			alert (err_lms_failure);
		}
	}
}

function startContent() {
	win=window.open('frameset.html','winone',"width=730,height=530,toolbar=no,menubar=no,status=no,scrollbars=no,resizable=no,top=0,left=0,fullscreen=no");
}

function doback() {
	win=window.open('loading.html','winone',"width=730,height=530,toolbar=no,menubar=no,status=no,scrollbars=no,resizable=no,top=0,left=0,fullscreen=no");
}

function focusWindow() {
	window.moveTo(0,0);
}

function finish() {
	LMSFinish()
	var err=LMSGetLastError() 
	switch(err) {
		case 0:
			//LMSFinish successful
			err_desc=err_desc_0;
			return 1; //successful
        
		case 101:
			//General Exception.
			finish_attempts++;
			err_desc=err_desc_101;
			return 2;
          
		case 102:
			//Server is busy.
			finish_attempts++;
			err_desc=err_desc_102;
			return 2;            

		default:
			//Unsupported error.
			err_desc=err_desc_unk;
			return 3; //unknown error
	}  
	return 4;//exceeded allowed attempts 
}

function check_finish()
{
	if (catBound)
	{
	   var err_check=finish();
	
	   if (err_check=1)
	   {
	       //alert (err_desc) //successful
	   }
	
	   else if (err_check=2)
	   {
	
	       if (finish_attempts<num_attempts)
	       {
	           setTimeout("check_finish()",time_attempts);
	       } 
	       else
	       {  
	       alert (err_desc) //error code
	       }
	   }
	     
	   else if (err_check=3)
	   {
	       alert (err_desc) //unsupported error
	   }
	
	   else 
	   {
	       alert (err_desc) //successful - too many attempts
	   }
	}

}

function set_score(score)
{
	if (catBound)
	{
		LMSSetValue("cmi.core.score.raw", score);
	}
}