var nFindAPITries = 0;
var API = null;
var maxTries = 500;
var APIVersion = "";

// The ScanForAPI() function searches for an object named API_1484_11
// in the window that is passed into the function. If the object is
// found a reference to the object is returned to the calling function.
// If the instance is found the SCO now has a handle to the LMS
// provided API Instance. The function searches a maximum number
// of parents of the current window. If no object is found the
// function returns a null reference. This function also reassigns a
// value to the win parameter passed in, based on the number of
// parents. At the end of the function call, the win variable will be
// set to the upper most parent in the chain of parents.
function ScanForAPI(win)
{
	while ((win.API_1484_11 == null) && (win.parent != null)
	&& (win.parent != win))
	{
		nFindAPITries++;
		if (nFindAPITries > maxTries)
		{
			return null;
		}
		win = win.parent;
	}
	return win.API_1484_11;
}
// The GetAPI() function begins the process of searching for the LMS
// provided API Instance. The function takes in a parameter that
// represents the current window. The function is built to search in a
// specific order and stop when the LMS provided API Instance is found.
// The function begins by searching the current window’s parent, if the
// current window has a parent. If the API Instance is not found, the
// function then checks to see if there are any opener windows. If
// the window has an opener, the function begins to look for the
// API Instance in the opener window.
function GetAPI(win)
{
		if ((win.parent != null) && (win.parent != win))
		{
			API = ScanForAPI(win.parent);
		}
		if ((API == null) && (win.opener != null))
		{
			API = ScanForAPI(win.opener);
		}
		if (API != null)
		{
			APIVersion = API.version;
		}
}


function scorm_extiendecero(x)
{
	var s=x.toString();
	if (s.length==1)
	{
		return "0"+s;
	}
	return s;
}

function scorm_puntuacion_pantalla(id,min,max,score)
{
//	alert("id="+id+"\nmin="+min+"\nmax="+max+"\nscore="+score);
	var session_time=parseInt(((new Date()).getTime()-scorm_sessionstart)/1000);
	if(API!=null)
	{
		var i;
		var objcount=parseInt(API.GetValue("cmi.objectives._count"));
		API.SetValue("cmi.session_time","PT"+session_time+"S");
		for(i=0;i<objcount;i++)
		{
			var objid=API.GetValue("cmi.objectives."+i+".id");
			if(objid==id)
			{
//				alert("Objetivo existente");
				break;
			}
			
		}
		if(i==objcount)
		{
//			alert("Creando objetivo");
			API.SetValue("cmi.objectives."+objcount+".id",id)
		}
//		alert("id="+id+"\ni="+i);
		if(min!=max)
		{
			API.SetValue("cmi.objectives."+i+".score.min",min)
			API.SetValue("cmi.objectives."+i+".score.max",max)
			API.SetValue("cmi.objectives."+i+".score.raw",score)
			API.SetValue("cmi.objectives."+i+".score.scaled",-1+2*(score-min)/(max-min));
		}
		API.SetValue("cmi.objectives."+i+".completion_status","completed");
	}
}


function scorm_fin()
{
//	alert("Fin");
	var session_time=parseInt(((new Date()).getTime()-scorm_sessionstart)/1000);
	if(API!=null)
	{
//		alert("session_time="+session_time);
		API.SetValue("cmi.session_time","PT"+session_time+"S");
		API.SetValue("cmi.completion_status","completed");
		API.SetValue("cmi.objectives.0.completion_status","completed");
		API.Terminate("");
		API=null;
		APIVersion="";
	}
}


function scorm_inicio()
{
//	alert("Inicio");
	var today=new Date();
	scorm_sessionstart=today.getTime()
	if(API!=null)
	{
//		alert("Inicio OK");
		API.Initialize("");
		API.SetValue("cmi.completion_status","incomplete");
		API.SetValue("cmi.completion_status","incomplete");
		var objcount=parseInt(API.GetValue("cmi.objectives._count"));
		if(!objcount)
		{
			API.SetValue("cmi.objectives.0.id","objetivoPrimario");
			API.SetValue("cmi.objectives.0.completion_status","incomplete");
		}
	}
}


GetAPI(window);

