Q: Javascript & Scorm 1.2 in Moodle 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 ::
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 > 0; i--){
        s2 = "%" + i;
        if (s.indexOf(s2) > -1){
            re.compile(s2,"g")
            s = s.replace(re, arguments[i]);
        }
    }
    return s
}
function FindAPI(win) {
    while ((win.API == null) && (win.parent != null) && (win.parent != win)) {
        g_nFindAPITries ++;
        if (g_nFindAPITries > 500) {
            AlertUserOfAPIError(g_strAPITooDeep);
            return null;
        }
        win = win.parent;
    }
    return win.API;
}
function APIOK() {
    return ((typeof(g_objAPI)!= "undefined") && (g_objAPI != null))
}
function SCOInitialize() {
var err = true;
    if (!g_bInitDone) 
        {


        if ((window.parent) && (window.parent != window)){
            g_objAPI = FindAPI(window.parent)
        }
        if ((g_objAPI == null) && (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()) && (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
}

and this is in body html
<body bgcolor="#ffffff" onload="SCOInitialize()" onunload="SCOFinish()" onbeforeunload="SCOFinish()" >

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

A: I would focus your debugging around the g_bSetCompletedAutomatically variable. Set a watch on it and see if the code is calling SCOSetStatusCompleted() when you exit the course. Also, without seeing what the original value of g_bSetCompletedAutomatically or the code for SCOSetStatusCompleted() in the samples you provided, it is really hard to give you any more direction. But that appears where you are setting the lesson_status to completed as I do not see any other code that sets it completed any where else in your code.
For the issue when the course is complete, the other thing you might want to look at is in your SCOInitialize() as that is the only place I see your code setting the lesson_status. You need to see if lesson_mode is returning browse and lesson_status is returning not attempted to see if that piece of code is what is setting the course to incomplete. Also, when a course is in browse mode, the RTE and the LMS should ignore and SetValue calls anyway.
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")
        }
    }

