// Adam Fox
// www.monkeyfruit.co.uk
// Copyright © monkeyfruit limited
var _alert=false,_Debug=false,_NoError=0,_GeneralException=101,_ServerBusy=102,_InvalidArgumentError=201,_ElementCannotHaveChildren=202,_ElementIsNotAnArray=203,_NotInitialized=301,_NotImplementedError=401,_InvalidSetValue=402,_ElementIsReadOnly=403,_ElementIsWriteOnly=404,_IncorrectDataType=405,apiHandle=null,API=null,findAPITries=0;
function doLMSInitialize(){var a=getAPIHandle();if(a==null){_alert&&alert("Unable to locate the LMS's API Implementation.\nLMSInitialize was not successful.");return"false"}a=a.LMSInitialize("");if(a.toString()!="true")var b=ErrorHandler();return a.toString()}
function doLMSFinish(){var a=getAPIHandle();if(a==null){_alert&&alert("Unable to locate the LMS's API Implementation.\nLMSFinish was not successful.");return"false"}else{a=a.LMSFinish("");if(a.toString()!="true")var b=ErrorHandler()}return a.toString()}
function doLMSGetValue(a){var b=getAPIHandle();if(b==null){_alert&&alert("Unable to locate the LMS's API Implementation.\nLMSGetValue was not successful.");return""}else{var c=b.LMSGetValue(a),d=b.LMSGetLastError().toString();if(d!=_NoError){b=b.LMSGetErrorString(d);_alert&&alert("LMSGetValue("+a+") failed. \n"+b);return""}else return c.toString()}}
function doLMSSetValue(a,b){var c=getAPIHandle();if(c==null)_alert&&alert("Unable to locate the LMS's API Implementation.\nLMSSetValue was not successful.");else{a=c.LMSSetValue(a,b);if(!a||a.toString()!="true")a=ErrorHandler()}}
function doLMSCommit(){var a=getAPIHandle();if(a==null){_alert&&alert("Unable to locate the LMS's API Implementation.\nLMSCommit was not successful.");return"false"}else{a=a.LMSCommit("");if(typeof a+""=="undefined")a="";if(a!="true")var b=ErrorHandler()}return a.toString()}function doLMSGetLastError(){var a=getAPIHandle();if(a==null){_alert&&alert("Unable to locate the LMS's API Implementation.\nLMSGetLastError was not successful.");return _GeneralError}return a.LMSGetLastError().toString()}
function doLMSGetErrorString(a){var b=getAPIHandle();b==null&&_alert&&alert("Unable to locate the LMS's API Implementation.\nLMSGetErrorString was not successful.");return b.LMSGetErrorString(a).toString()}function doLMSGetDiagnostic(a){var b=getAPIHandle();b==null&&_alert&&alert("Unable to locate the LMS's API Implementation.\nLMSGetDiagnostic was not successful.");return b.LMSGetDiagnostic(a).toString()}
function LMSIsInitialized(){var a=getAPIHandle();if(a==null){_alert&&alert("Unable to locate the LMS's API Implementation.\nLMSIsInitialized() failed.");return false}else{var b=a.LMSGetValue("cmi.core.student_name");a=a.LMSGetLastError().toString();return a==_NotInitialized?false:true}}
function ErrorHandler(){var a=getAPIHandle();if(a==null)_alert&&alert("Unable to locate the LMS's API Implementation.\nCannot determine LMS error code.");else{var b=a.LMSGetLastError().toString();if(b!=_NoError){var c=a.LMSGetErrorString(b);if(_Debug==true){c+="\n";c+=a.LMSGetDiagnostic(null)}_alert&&alert(c)}return b}}function getAPIHandle(){if(apiHandle==null)apiHandle=getAPI();return apiHandle}
function findAPI(a){for(;a.API==null&&a.parent!=null&&a.parent!=a;){findAPITries++;if(findAPITries>7){alert("Error finding API -- too deeply nested.");return null}a=a.parent;if(a.API)break;if(a.opener&&a.opener.API){a=a.opener;break}}return a.API}function getAPI(){var a=findAPI(window);if(a==null&&window.top.opener!=null&&typeof window.top.opener!="undefined")a=findAPI(window.top.opener);a==null&&_alert&&alert("Unable to find an API adapter");return a};