title: Javascript & Scorm 1.2 in Moodle
body: <p>I am publish html from flash which is flash + scorm 1.2
   my html contain .swf as player
  .swf player will have menu and student navigate pages from this swf, so the status of the course should be completed when the student reach last page,,, but what happened with me?
  when 1st time student visit the course and close it >> the status of lesson is incomplete which is correct,,,, but at the 2nd time when open the course the status changed to completed and the third time change to incomplete ..4th time change to complete ,, i cant understand what exactly happened this is the javascript functions i use ::</p>
  
  <pre><code>var g_nFindAPITries = 0;
  var g_objAPI = null;
  var g_bInitDone = false;
  var g_bFinishDone = false;
  var g_bSCOBrowse = false;
  var g_dtmInitialized = new Date(); // will be adjusted after initialize
  var g_bMasteryScoreInitialized = false;
  function AlertUserOfAPIError(strText) {
      if (g_bShowApiErrors) {
          var s = strText + "\n\n" + g_strDisableErrorMsgs;
          if (!confirm(s)){
              g_bShowApiErrors = false
          }
      }
  }
  
  function ExpandString(s){
      var re = new RegExp("%","g")
      for (i = arguments.length-1; i &gt; 0; i--){
          s2 = "%" + i;
          if (s.indexOf(s2) &gt; -1){
              re.compile(s2,"g")
              s = s.replace(re, arguments[i]);
          }
      }
      return s
  }
  function FindAPI(win) {
      while ((win.API == null) &amp;&amp; (win.parent != null) &amp;&amp; (win.parent != win)) {
          g_nFindAPITries ++;
          if (g_nFindAPITries &gt; 500) {
              AlertUserOfAPIError(g_strAPITooDeep);
              return null;
          }
          win = win.parent;
      }
      return win.API;
  }
  function APIOK() {
      return ((typeof(g_objAPI)!= "undefined") &amp;&amp; (g_objAPI != null))
  }
  function SCOInitialize() {
  var err = true;
      if (!g_bInitDone) 
          {
  
  
          if ((window.parent) &amp;&amp; (window.parent != window)){
              g_objAPI = FindAPI(window.parent)
          }
          if ((g_objAPI == null) &amp;&amp; (window.opener != null))  {
              g_objAPI = FindAPI(window.opener)
          }
          if (!APIOK()) {
              AlertUserOfAPIError(g_strAPINotFound);
              err = false
          } 
                  else
                  {
              err = g_objAPI.LMSInitialize("");
              if (err == "true") 
                           {
                  g_bSCOBrowse = (g_objAPI.LMSGetValue("cmi.core.lesson_mode") == "browse");
                  if (!g_bSCOBrowse)
                                     {
                      if (g_objAPI.LMSGetValue("cmi.core.lesson_status") == "not attempted")
                                             {
                          err = g_objAPI.LMSSetValue("cmi.core.lesson_status","incomplete")
                         }
                     }
              }
                          else 
                          {
                  AlertUserOfAPIError(g_strAPIInitFailed)
              }
          }
            if (typeof(SCOInitData) != "undefined") 
                     {
              // The SCOInitData function can be defined in another script of the SCO
              SCOInitData()
             }
          g_dtmInitialized = new Date();
      }
      g_bInitDone = true;
  
      return (err + "") // Force type to string
  }
  function SCOFinish() {
      if ((APIOK()) &amp;&amp; (g_bFinishDone == false)) {
          SCOReportSessionTime()
          if (g_bSetCompletedAutomatically){
              SCOSetStatusCompleted();
          }
          if (typeof(SCOSaveData) != "undefined"){
              // The SCOSaveData function can be defined in another script of the SCO
              SCOSaveData();
          }
          g_bFinishDone = (g_objAPI.LMSFinish("") == "true");
      }
      return (g_bFinishDone + "" ) // Force type to string
  }
  // Call these catcher functions rather than trying to call the API adapter directly
  function SCOGetValue(nam)           {return ((APIOK())?g_objAPI.LMSGetValue(nam.toString()):"")}
  function SCOCommit()                    {return ((APIOK())?g_objAPI.LMSCommit(""):"false")}
  function SCOGetLastError()      {return ((APIOK())?g_objAPI.LMSGetLastError():"-1")}
  function SCOGetErrorString(n)   {return ((APIOK())?g_objAPI.LMSGetErrorString(n):"No API")}
  function SCOGetDiagnostic(p)    {return ((APIOK())?g_objAPI.LMSGetDiagnostic(p):"No API")}
  //LMSSetValue is implemented with more complex data management logic below
  var g_bMinScoreAcquired = false;
  var g_bMaxScoreAcquired = false;
  // Special logic begins here
  function SCOSetValue(nam,val){
      var err = "";
      if (!APIOK()){
              AlertUserOfAPIError(g_strAPISetError + "\n" + nam + "\n" + val);
              err = "false"
      } else if (nam == "cmi.core.score.raw") err = privReportRawScore(val)
      if (err == ""){
              err = g_objAPI.LMSSetValue(nam,val.toString() + "");
              if (err != "true") return err
      }
      if (nam == "cmi.core.score.min"){
          g_bMinScoreAcquired = true;
          g_nSCO_ScoreMin = val
      }
      else if (nam == "cmi.core.score.max"){
          g_bMaxScoreAcquired = true;
          g_nSCO_ScoreMax = val
      }
      return err
  }
  </code></pre>
  
  <p>and this is in body html</p>
  
  <pre><code>&lt;body bgcolor="#ffffff" onload="SCOInitialize()" onunload="SCOFinish()" onbeforeunload="SCOFinish()" &gt;
  </code></pre>
  
  <p>so why its swap time after time incomplete and completed and i am try to set value for 
  cmi.core.lesson_status but it doesn't work</p>