var API = null;
var findAPITries = 0;
var dbg = false ;

function findAPI(win){
    try{
       while ( (win.API == null) && (win.parent != null) && (win.parent != win) ){	
          findAPITries++;
          if (findAPITries > 7) {
             return null;
          }
          win = win.parent;
       }
       return win.API;
    }catch(e){
        return null;
    }
}

function getAPI(){
   var theAPI = findAPI(parent.window);
   if ( (theAPI == null) && (parent.window.opener != null) && (typeof(parent.window.opener) != "undefined") ){
      theAPI = findAPI(parent.window.opener);
   }
   return theAPI;
}


function doLMSInitialize(){
  API = getAPI();
  if (dbg==true) {
    alert("doLMSInitialize="+API);
  }
  return ""+API;
}


function scorm_LMSinit(){
  if( API == null ){
    API = getAPI();
  }
  var strStatus;
  API.LMSInitialize("");
  strStatus = API.LMSGetLastError();
  if (dbg==true) {
    alert("scorm_LMSinit="+strStatus);
  }
  return ""+strStatus;
}

function doLMSGetValue(name){
  if( API == null ){
    API = getAPI();
  }
  var value = API.LMSGetValue(name);
  var errCode = API.LMSGetLastError().toString();
  if (dbg==true) {
    alert("doLMSGetValue("+name+")="+value+", errCode="+errCode);
  }
  if (errCode != 0) {
    return "";
  } else {
    return ""+value.toString();
  }
}



function doLMSSetValue(name,value) {
  if( API == null ){
    API = getAPI();
  }
  var strStatus;
  API.LMSSetValue(name,value);
  strStatus = API.LMSGetLastError();
  if (dbg==true) {
    alert("doLMSSetValue("+name+","+value+")="+strStatus);
  }
  return ""+strStatus;
}

function doLMSFinish(){
  if( API == null ){
    API = getAPI();
  }
  var strStatus;
  API.LMSFinish("");
  strStatus = API.LMSGetLastError();
  if (dbg==true) {
    alert("doLMSFinish="+strStatus);
  }
  return ""+strStatus;
}