/*******************************************************************************
**
** FileName: CMIWrapp.js 
**
*******************************************************************************/
// Find API methods

var findAPITries = 0;
var bDebug = false ; // true false
var bDebug2 = false ; // true false
var bDebugScore = false ; // true false
var aiccresult = "";
var bDebugFind = false ; // true falsevar bDebugHACP = false ; // true false

var nPassed = 0;
var nScored = 0;
var timerID = null ;
var timerRunning = false ;
var ntimecnt = 0;
var bappinit = false ;
var bShutdown = false;
var bDebugFrame = false;
function checkstopclock()
 {
	if (timerRunning)
		  clearTimeout(timerID) ;
	timerRunning = false;
 }

function checkstartclock(nwaittime, ntimeoutcnt, bGetHACP)
 {
	checkstopclock();
	checkappinit(nwaittime, ntimeoutcnt, bGetHACP);
 }

function checkappinit(itime, i ,bhacp)
 {
 timerRunning = true ;
 
 if (bappinit)
   {
   if (HACPAPI2 == null) // Netscape
     {
     HACPAPI2 = dataFrame.document.HACPAPI ;
     if (HACPAPI2) IsAICC = true;
     }

   KQLMSGetData () ;
   }
else
   {
	// Netscape?
	pos   = myBrowser.indexOf("Netscape")
	if (pos > -1)
      {
      if (dataFrame.document.HACPAPI != null)
         {
         HACPAPI2 = dataFrame.document.HACPAPI ;
         IsAICC = true;
         bappinit = true;
         KQLMSGetData () ;
         }
      }
   }
   
 ntimecnt++;
 
   if (ntimecnt >= i || bappinit)
     {
     checkstopclock() ;
     if (!bappinit )
       {
       IsAICC = false;
       HACPAPI2 = null ;
       }
     content.location='Coursels.htm' ;
     }
   else
     {
	  checkstopclock();
	  timerID = setTimeout ("checkappinit("+itime+", "+ i+", "+bhacp+")", itime);
	  }
 }
function findAPIWindows(win)
{
	var theAPI = null;

	// Check to see if the window (win) contains the API
	// if the window (win) does not contain the API and
	// the window (win) has a parent window and the parent window
	// is not the same as the window (win)

	while (theAPI == null)
	{

		// increment the number of findAPITries
		findAPITries++;

		if (bDebugFind)
			{
			alert("In findAPINEW ("+win.location.href+")");
			}

		// Note: 7 is an arbitrary number, but should be more than sufficient
		if (findAPITries > 7)
		{
			if (bDebugFind)
				{
				alert("Error finding API -- too deeply nested.");
				}
			return null;
		}
		theAPI = findAPIMAD (win);


		// set the variable that represents the window being
		// searched to be the parent of the current window
		// then search for the API again

	  if (win.parent == null || win.parent == win) return theAPI;

		win = win.parent;
		if (bDebugFind)
			{
			alert("Parent findAPINEW ("+win.location.href+")");
			}

	}

	return theAPI;
}


function FindAPIDOCENT()
{
    //Is it in the current window?
    if (window.document.API != null) 	return window.document.API;

	// Is it in the window's opener?
	if (window.opener != null){
	    if (window.opener.API != null){
			return window.opener.API;
	    }else{
			// look in the openers window's frames...
			if (window.opener.parent != null){
				for (i=0; i<window.opener.parent.frames.length; i++) {
					if (window.opener.parent.frames[i].API != null)
						return window.opener.parent.frames[i].API;
				}
			}
		}
	}

	// Is it in the current window's parent?
	if (window.parent != null){
		if (window.parent.API != null){
			return window.parent.API;
		}else{
			// look in the parent window's frames...
			for (i=0; i<window.parent.frames.length; i++){
				if (window.parent.frames[i].API != null)
					return window.parent.frames[i].API;
			}
		  
		}
	}

	// Is it in the current window's parent?
	if (window.parent.parent != null){
		if (window.parent.parent.API != null){
			return window.parent.parent.API;
		}else{
			// look in the parent window's frames...
			// this is probably the most likely place for it to be...
			for (i=0; i<window.parent.parent.frames.length; i++){
				if (window.parent.parent.frames[i].API != null)
					return window.parent.parent.frames[i].API;
			}
		}
	}
	
	// The API was not found
	return null;	
}



function FindAPIGOOD(win)
{
	// start by looking for the API in the current window
	if (bDebugFind)
	  {
	  alert("findAPIWindows ("+win.location.href+")");
	  }

	var theAPI = findAPIWindows(win);

	// if the API is null (could not be found in the current window)
	// and the current window has an opener window
	if ( (theAPI == null) &&
		  (win.opener != null) &&
		  (typeof(win.opener) != "undefined") )
	{
		// try to find the API in the current window's opener
		theAPI = findAPIWindows(win.opener);
	}
	// if the API has not been found
	if (theAPI == null)
	{
		// Alert the user that the API Adapter could not be found
		// alert("Unable to find an API adapter");
	}
	return theAPI;
}

function findAPIMAD(win)
{

	// Search the window hierarchy for an object named "API"
	// Look in the current window (win) and recursively look in any child frames

	if (bDebugFind)
	{
		alert("win is: "+win.location.href);
	}

	if (win.document.API != null)
	{
		if (bDebugFind)
		{
			alert("found api in this window");
		}
		return win.document.API;
	}

	if (win.length > 0)  // does the window have frames?
	{
		if (bDebugFind)
		{
			alert("looking for api in windows frames");
		}

		for (var i=0;i<win.length;i++)
		{

			if (bDebugFind)
			{
				alert("looking for api in frames["+i+"]");
			}
			var theAPI = findAPIMAD(win.frames[i]);
			if (theAPI != null)
			{
				return theAPI;
			}
		}
	}

	if (bDebugFind)
	{
		alert("didn't find api in this window (or its children)");
	}
	return null;

}

function getname()
{
  if (API != null) return API.LMSGetValue("cmi.core.student_name");
  else return "stranger";
}

function savedata(lessonlocation, score)
{
  alert("Saving data");
  // Save all the data and commit
  if (API != null)
  {
    if (lessonlocation != null) API.LMSSetValue("cmi.core.lesson_location", lessonlocation);
    API.LMSSetValue("cmi.core.score.max", 100);
    if (lessonlocation == null) score = 0;
    API.LMSSetValue("cmi.core.score.raw", score);
    API.LMSCommit("")
  }
}


function suspend(lessonlocation, suspenddata, score)
{
  if (suspenddata != null) API.LMSSetValue("cmi.suspend_data", suspenddata);
  API.LMSSetValue("cmi.core.exit", "suspend");
  if (API.LMSGetValue("cmi.core.lesson_status") != "completed") API.LMSSetValue("cmi.core.lesson_status", "incomplete");
  savedata(lessonlocation, score);
  API.LMSFinish("")
  window.close();
}function complete()
{
  if (API != null)
  {
    API.LMSSetValue("cmi.core.lesson_status", "completed");
    API.LMSSetValue("cmi.core.exit", "logout");
    API.LMSCommit("")
    API.LMSFinish("")
  }
  window.close();
}

///////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////
// AICC
//
// If we find an AICC session ID then we assume AICC complience
// Later if we find the CMIAPI we will use it.  Otherwise, we will use HACP

function AICCHACPInitialize ()
	  {
     // Load the HACP applet and return;
	  if (bDebug)
		 {
		  alert("AICCHACPInitialize");
		 }

	  // Get start time
	  startTime	= new Date();
	  if (bDebug) alert("Start time:" + startTime.getTime());

  myBrowser=navigator.appName;
 // IE?
 pos   = myBrowser.indexOf("Microsoft")
 if (pos > -1)
   {
	dataFrame.document.writeln("\<html\>\<\head\>\<script language='JavaScript' type='text/JavaScript'\>");
	dataFrame.document.writeln("function AppletInit(txtMsg)");
	dataFrame.document.writeln("{");
	dataFrame.document.writeln("parent.bappinit = true;");
 //	dataFrame.document.writeln("alert(txtMsg);");
	dataFrame.document.writeln("}\</script\>\</head>\<body OnUnload='parent.EndCourse()'\>");
 if (bDebugFrame)
   dataFrame.document.writeln("<APPLET ARCHIVE='ea_hacp.jar' CODE='ea_hacp.java' WIDTH='500' HEIGHT='500' name='HACPAPI' mayscript='1' id='HACPAPI'><param name='sid' value='" +
		aiccsid + "'>" +  "<PARAM NAME='CABBASE'  VALUE='ea_hacp.cab'>" +  "<param name='dbg' value='true'>" +  "<param name='url' value='" + aiccurl + "'>"+  "<param name='dbg' value='true'>"+"</APPLET>");
 else
   dataFrame.document.writeln("<APPLET ARCHIVE='ea_hacp.jar' CODE='ea_hacp.java' WIDTH='1' HEIGHT='1' name='HACPAPI' mayscript='1' id='HACPAPI'><param name='sid' value='" +
		aiccsid + "'>" +  "<PARAM NAME='CABBASE'  VALUE='ea_hacp.cab'>" +  "<param name='url' value='" + aiccurl + "'>"+"</APPLET>");

	dataFrame.document.writeln("\</body\>\</html\>");
   }
 else
   dataFrame.location='Coursecn.htm' ;

     if (dataFrame.document.HACPAPI != null || (IsAICC && dataFrame.document.HACPAPI == null))
          {
          //alert ("Try to connected to the HACP LMS.")
          HACPAPI2 = dataFrame.document.HACPAPI ;
          
          // We need to wait for initialization here
          // Start the clock for APPLETT init.
          ntimecnt = 0;
          checkstartclock(500,60,false) ;

          }
     else
          HACPAPI2 = null ;

     }
     
// if AICC CMI API tracking - Find and Initialize.
function AICCCMIInitialize()
{
	if( (navigator.appName == "Netscape") && (navigator.appVersion.substring(0,1) <= "4"))
	{
		if (bDebug) 
			alert("Calling without try");

		AICCCMIInitializeEx();
	}
	else{
		if (bDebug) 
			alert("Calling withtry");
			
		TryAICCCMIInitialize();
	}
}

function AICCCMIInitializeEx ()
	  {
	  // Try to locate the CMIAPI
	  if (bDebug)
		 {
		  alert("AICCCMIInitialize");
		 }

	  // Get start time
	  startTime	= new Date();
	  if (bDebug) alert("Start time:" + startTime.getTime());

	  AICCCMI = FindAPIDOCENT(window);

	  if (AICCCMI == null && top.opener != null) AICCCMI = FindAPIDOCENT(top.opener);
	  if (bDebugFind)
		 {
		 if (AICCCMI == null) alert("Could not find CMIAPI-DOCENT");
		 else  alert("Found CMIAPI-DOCENT");
		 }

	  if (AICCCMI == null)
		  {
		  if (bDebugFind) alert("looking for CMIAPI-Recursive");
		  AICCCMI = FindAPIGOOD (window);
		  if (AICCCMI == null && top.opener != null) AICCCMI = FindAPIGOOD(top.opener);
		  if (bDebugFind)
			 {
			 if (AICCCMI == null) alert("Could not find CMIAPI-Recursive");
			 else  alert("Found CMIAPI-Recursive");
			 }

		  }

	  if (AICCCMI)
		  {
		  if (bDebug) alert("CMI Initialization start.");

		  var emptyString = new String("");

		  AICCCMI.LMSInitialize (emptyString);
		  iError = AICCCMI.LMSGetLastError ();

		  if (iError != 0)
			  {
			  if (bDebug) alert("CMI Initialization error: "+AICCCMI.LMSGetErrorString (iError));
			  }
                  else
			{
				if (bDebug) alert("CMI Initialized - getting launch data.");
				buffer = AICCCMI.LMSGetValue ("cmi.launch_data");
				if (bDebug) alert(buffer);
			}
		  }
	  }

// AICCCMIInitialize () ;
function ParseLMSData (tstbuffer)
		{
      tempp = " " ;
		work  = " " ;
      tempp = tstbuffer.toUpperCase()+"";

  if (HACPAPI2 != null) // HACP Method
      {
		// Find bookmark data
 		bookmark     = "999" ;
      error 		 = "0" ;
		pos   = tempp.indexOf("LESSON_LOCATION=") ;
		if (pos > -1)
			{
			work = tstbuffer.substring(pos + 16, tempp.length) ;
			if (work.indexOf("\r") > 0)
			 work = work.substring(0, work.indexOf("\r")) ;
         bookmark = work ;
    		if (bDebugHACP) alert ("Parse - bookmark="+bookmark) ;
      	}
		if (bDebug) alert("Parse - bookmark="+bookmark);

		pos   = tempp.indexOf("ERROR=") ;
		if (pos > -1)
			{
			work = tstbuffer.substring(pos + 6, tempp.length) ;
			if (work.indexOf("\r") > 0)
			 work = work.substring(0, work.indexOf("\r")) ;
         error = work ;
         if (bDebugHACP) alert("Parse - error="+error);
      	}
		if (bDebug) alert("Parse - error="+error);
      } // if (HACPAPI2)

		// Find checkmark data
		pos   = tempp.indexOf("CHECK=");
		if (pos > -1)
			{
			work = tstbuffer.substring(pos + 6, tempp.length);
			if (work.indexOf("&") > 0)
			 work = work.substring(0, work.indexOf("&"));
         check = work ;
         if (bDebugHACP) alert("Parse - check="+check);
      	}
		if (bDebug) alert("Parse - check="+check);

		// Find mastery data
		pos   = tempp.indexOf("MASTERY=") ;
		if (pos > -1)
			{
			work = tstbuffer.substring(pos + 8, tempp.length) ;
			if (work.indexOf("&") > 0)
			 work = work.substring(0, work.indexOf("&")) ;
			mastery = work ;
      	}
		if (bDebug) alert("Parse - mastery="+mastery);

		// Find assessment data
		pos   = tempp.indexOf("ASSESS=") ;
		if (pos > -1)
			{
			work = tstbuffer.substring(pos + 7, tempp.length) ;
			if (work.indexOf("&") > 0)
			 work = work.substring(0, work.indexOf("&")) ;
			strAssess = work ;
      	}
		if (bDebug) alert("Parse - assess="+strAssess);
		}

function padNumber(num,dig)
	{
	if (dig == 2)
	  {
	  if (num < 10)
		 return "0" + num;
	  else
		 return num;
	  }
	if (dig == 4)
	  {
	  if (num < 10)
		 return "000" + num;
	  else
	  if (num < 100)
		 return "00" + num;
	  else
	  if (num < 1000)
		 return "0" + num;
	  else
		 return num;
	  }
	}

function EndCourse()
		{

      if (bShutdown) return ;
      bShutdown = true;

      if (bDebugHACP) alert("Exit course");
		if (bDebug) alert("Exit course");

		if (AICCCMI)
			 {
			 endTime	= new Date();
			 tmpTime = new Date();
			 zeroTime = new Date (70,1,0,0,0,0)
			 test = zeroTime.getTime();
			 tmpTime.setTime (zeroTime.getTime()+(endTime.getTime() - startTime.getTime()) );

			 AICCTime =	padNumber(tmpTime.getHours(),4) + ":" +
							padNumber(tmpTime.getMinutes(),2) + ":" +
							padNumber(tmpTime.getSeconds(),2);

			 AICCCMI.LMSSetValue ("cmi.core.session_time", AICCTime);
			 // AICCCMI.LMSCommit (null);
			 if (bDebug) alert("Time in course ="+AICCTime);

			 if (bDebug) alert ("Commit Course data");

			 var emptyString = new String("");

// MAD 9/7/2001 Start - added for Docent - Don't assume complete status
          lesson_status = AICCCMI.LMSGetValue("cmi.core.lesson_status") ;
			 if (lesson_status != "completed" && lesson_status != "passed")
					AICCCMI.LMSSetValue("cmi.core.lesson_status", "incomplete");

			 // Commit the data to the LMS
			 AICCCMI.LMSCommit (emptyString);

			 if (bDebug) alert (AICCCMI.LMSGetValue("cmi.core.lesson_status"));
// MAD 9/7/2001 End


			 AICCCMI.LMSFinish (emptyString);
			 }
			 
		if (HACPAPI2 != null)
	      {
          KQLMSWrite () ;
          aiccresult = HACPAPI2.CallFunction("ExitAU", "") ;
          } // if (HACPAPI2)
          
		}

////////////////////////////////////////////////////////////////////////////
// LMS: Write data to LMS using AICCCMI.
function KQLMSWrite ()
  {
  if (AICCCMI) // CMI Method
		{
		AICCCMI.LMSSetValue ("cmi.core.lesson_location", bookmark);

		AICCCMI.LMSSetValue ("cmi.suspend_data", "check="+check+"&"+"mastery="+mastery+"&"+"assess="+strAssess);

		if (bDebug2) alert ("Commit Course data");

		// Commit the data to the LMS
		var emptyString = new String("");

		AICCCMI.LMSCommit (emptyString);
		} // if (AICCCMI)
		
  if (HACPAPI2 != null) // HACP Method
		{
			 endTime	= new Date();
			 tmpTime = new Date();
			 zeroTime = new Date (70,1,0,0,0,0)
			 test = zeroTime.getTime();
			 tmpTime.setTime (zeroTime.getTime()+(endTime.getTime() - startTime.getTime()) );

			 AICCTime =	padNumber(tmpTime.getHours(),4) + ":" +
							padNumber(tmpTime.getMinutes(),2) + ":" +
							padNumber(tmpTime.getSeconds(),2);

      output="";
      // [core] section
       output = output + escape ("[core]");
       output = output + "%0D%0A" ; // cr/lf
       // course status
		if (bComplete)
         output = output + escape ("lesson_status=complete");
        else
         output = output + escape ("lesson_status=incomplete,s");
        output = output + "%0D%0A" ; // cr/lf

       // course location
        output = output + escape ("Lesson_Location="+bookmark);
        output = output + "%0D%0A" ; // cr/lf

       // course score
		if (bComplete)
         output = output + escape ("score=100");
        else
         output = output + escape ("score=0");
        output = output + "%0D%0A" ; // cr/lf

       // course time
        output = output + escape ("time="+AICCTime);
        output = output + "%0D%0A" ; // cr/lf

      // [core_lesson] section
       output = output + escape ("[core_lesson]");
       output = output + "%0D%0A" ; // cr/lf

       output = output + escape ("coursedata="+"check="+check+"&"+"mastery="+mastery+"&"+"assess="+strAssess);
       output = output + "%0D%0A" ; // cr/lf

     if (bDebug) alert ("PutParam "+ output) ;

     aiccresult = HACPAPI2.CallFunction("PutParam", output) ;
     
     if (bDebugHACP) alert ("LMS HACP put data return = "+aiccresult)
	 
	  } // if (HACPAPI2)
		
  }

function	KQLMSGetData ()
	 {
	 
  if (AICCCMI) // CMI Method
		{
		 // Get LMS data
			// Bookmark
			bookmark     = "999"
			tmpbookmark = AICCCMI.LMSGetValue ("cmi.core.lesson_location");
			if (tmpbookmark != null) bookmark = tmpbookmark;

			if (bDebug) alert ("LMS bookmark data = "+tmpbookmark)
			// Initialize Data items to empty
			 check     = "000000000000000000000000000000000000000000000000000000000000"
			 mastery   = "000000000000000000000000000000000000000000000000000000000000"
			 strAssess = strAssessInit ;

			// Course data
			tstbuffer = AICCCMI.LMSGetValue ("cmi.suspend_data");
			if (tstbuffer != null) ParseLMSData (tstbuffer);

			if (bDebug) alert ("LMS suspend data = "+tstbuffer)
       } // if (AICCCMI)
       
  if (HACPAPI2 != null) // HACP Method
		{
      //alert ("KQLMSGetData - HACPAPI2");
		 // Get LMS data
			// Bookmark
			 bookmark     = "999" ;
			// Initialize Data items to empty
			 check     = "000000000000000000000000000000000000000000000000000000000000"
			 mastery   = "000000000000000000000000000000000000000000000000000000000000"
			 strAssess = strAssessInit ;

			// Course data
         //aiccresult = HACPAPI2.getData ("") ;
         
	      aiccresult = HACPAPI2.CallFunction("GetParam", "") ;
			if (bDebugHACP) alert ("LMS HACP return data = "+aiccresult) ;

	      if (aiccresult != null) ParseLMSData (aiccresult);
         //alert ("LMS HACP suspend data = "+aiccresult) ;
			//if (bDebugHACP) alert ("LMS HACP suspend data = "+aiccresult)
      } // if (HACPAPI2)
	  }

 //////////////////////////////////////////////////////////////////////////
 //Scoring
 //
 
 var myTasks;			// Arrays holding the individual tasks
 var strMastery = "0000000000000000000000000000000000000000000000000000000"

 function splitThem(strData, strMasteryin)
  {

  var myDummy, nEntries, params	= "";

   strMastery = strMasteryin ;
   if (bDebugScore) alert (strData);
	myTasks = strData.split("X");
	nEntries = myTasks.length-1;
   if (bDebugScore) alert("number of objective entries " + nEntries) ;
	PostResults();
  }
function PostResults()
{
var outMastery = "";
 nPassed = 0;
 nScored = 0;

	for (i=0;i<(strMastery.length/2);i++)
      {
		outMastery += PassedLesson(i);

		objectiveresult = PassedObjective(i);
      if (objectiveresult == "99")
        {
        if (bDebugScore) alert ("Passed objective " + i);
        nPassed++;
        nScored++;
        }
      if (objectiveresult == "00")
        {
        if (bDebugScore) alert ("Failed objective " + i);
        nScored++;
        }
      }

     if (bDebugScore)
       {
       if (nPassed > 0 && nScored > 0)
          alert ("Score based on objectives " + nPassed/nScored*100 + "%");
       else
           alert ("Score based on objectives 0%");
       }

   if (Launchmode != 0)
	    mastery = outMastery;

	registerEx(courseID+studentid+"A", mastery);

	if (bDebug) alert ("Saving Mastery data "+ mastery+" Key ="+courseID+studentid+"A");

}
function PassedObjective(Lesson)
{
	var i, strLess, LessFound = 50;
	
	for (i=0; i<(myTasks.length-1);i++)
	{
		strLess = myTasks[i].substring(1,3);
		if(strLess == Lesson)
		{
			if(!PassedTask(i))
				return "00";
			else
				LessFound="99";
		}
	}

	return LessFound;
}
function PassedLesson(Lesson)
{
	var i, strLess, LessFound = strMastery.substr(Lesson * 2,2);

	for (i=0; i<(myTasks.length-1);i++)
	{
		strLess = myTasks[i].substring(1,3);
		if(strLess == Lesson)
		{
			if(!PassedTask(i))
				return "00";
			else
				LessFound="99";
		}
	}
	return LessFound;
}
function PassedTask(Task)
{

// MADNOTE: Need to return a value - not bool for failed v. not taken.

	var i, nTaskWeight, nTodos, nWeight=0, nTotalWeight=0;

	nTaskWeight = parseInt(myTasks[Task].substring(3,5));
	nTodos = (myTasks[Task].length-5)/2;
	for(i=0; i<(nTodos*2); i=i+2)
	{
		nTotalWeight = nTotalWeight + parseInt(myTasks[Task].charAt(i+5));
		if(myTasks[Task].charAt(i+6) == "2" || myTasks[Task].charAt(i+6) == "0")
			nWeight = nWeight + parseInt(myTasks[Task].charAt(i+5));
	}
	intPerRight = Math.round(((nTotalWeight - nWeight) / nTotalWeight)*100);
	return (nTaskWeight > nWeight)?true:false;
}

function KQLMSWriteCompletion (bComplete, bresults)
  {
  if (AICCCMI)
		{
		// Set the status to completed
		if (bComplete)
		  {
        if (bDebug2) alert ("complete");
        if (!bresults)
          {
		    AICCCMI.LMSSetValue ("cmi.core.lesson_status", "completed");
		    // Added for Certificate of completion
          // If we are not scoring the AU then completion = passed
 		    AICCCMI.LMSSetValue ("cmi.core.score.min", "0");
 		    AICCCMI.LMSSetValue ("cmi.core.score.max", "100");
 		    AICCCMI.LMSSetValue ("cmi.core.score.raw", "100");
          }
        else
          {
          if (nPassed > 0 && nScored > 0) lessscore = nPassed/nScored*100 ;
          else                            lessscore = 0;

		    AICCCMI.LMSSetValue ("cmi.core.score.raw", lessscore);
          lmsmin = AICCCMI.LMSGetValue("cmi.core.score.min");
          if (lessscore >= lmsmin)
		       AICCCMI.LMSSetValue ("cmi.core.lesson_status", "passed");
          else
		       AICCCMI.LMSSetValue ("cmi.core.lesson_status", "failed");
          }

		  }
		else
		  {
        if (bDebug2) alert ("incomplete");
        lesson_status = AICCCMI.LMSGetValue("cmi.core.lesson_status") ;
		  if (lesson_status != "completed" && lesson_status != "passed")
					AICCCMI.LMSSetValue("cmi.core.lesson_status", "incomplete");
		  }

		AICCCMI.LMSSetValue ("cmi.core.lesson_location", bookmark);
		AICCCMI.LMSSetValue ("cmi.suspend_data", "check="+check+"&"+"mastery="+mastery+"&"+"assess="+strAssess);

		if (bDebug2) alert ("Commit Course data");

		// Commit the data to the LMS
		var emptyString = new String("");
		AICCCMI.LMSCommit (emptyString);
		}
		
  if (HACPAPI2)
	  {
       KQLMSWrite () ;
      }
		
		
  }

function TryAICCCMIInitialize()
{
	var str = "";
	
	try{
		if (bDebug)
			alert("Inside try");
		
		AICCCMIInitializeEx();
		if (bDebug)
			alert("Inside try - after call");
			
		// Check it to for security 
		str = AICCCMI.toString();
		
	}catch(e){
		if (bDebug)
			alert("Error from caller function");
		AICCCMI = null;	
	}
}



