var findAPITries=1;
var API = null;

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) 
      {
         top.window.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)
   {
      top.window.status = "Unable to find an API adapter";
   }
   return API
}


function initSco()
{		
	API = GetAPI();
	if( API != null )
	{
		var ret;
		var code;
		var diag;
		API.LMSInitialize("");
		code = API.LMSGetLastError();
		ret = API.LMSGetErrorString( code );
		diag = API.LMSGetDiagnostic( "" );	
		
	}
}

function set_val( gname,gvalue )
{
	
	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 )
{	
	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()
{
	
	GetAPI();
	if( API != null )
	{
		var ret = "";
		var code;
		var diag;
		
		API.LMSCommit("");
		code = API.LMSGetLastError();
		ret = API.LMSGetErrorString( code );
		diag = API.LMSGetDiagnostic( "" );		
	}
	
};

function finish()
{	
	GetAPI();
	if( API != null )
	{
		var ret;
		var code;
		var diag;
		
		ret = API.LMSFinish("");
		code = API.LMSGetLastError();
		ret = API.LMSGetErrorString( code );
		diag = API.LMSGetDiagnostic( "" );
		
	}
	
};

function score(score)
{
	set_val("cmi.student_data.mastery_score",score);
}