// @(#)SCO.js	1.18-02/03/29 


// SCO initialization and navigation functions. These functions are
// dependents on the SCO API Wrapper functions in SCOAPIWrapper.js

//
// Initialized for this file
moduleID= new Array();
//moduleID = ["Module%201","Module%202","Module%203","Module%204"]
moduleID[0] = "Introducing%20BizTalk%20Server:%20A%20BPI%20Solution";
moduleID[1] = "Identifying%20BizTalk%20Server%20Business%20Opportunities";
moduleID[2] = "Creating%20a%20Specific%20BizTalk%20Server%20Solution";
moduleID[3] = "Deploying%20the/a%20BizTalk%20Server%20Solution";
//Initialized for this file Ends

// global vars 
//
var resultBook = false;

//var firstPage="1_1_1.htm";
//var lastPage="1_4_9.htm";

// Initialized by sco_load()
var curPage;
var lessonStatus;
var retScore;
var retxdata;
var lastLocation;


var flagset = 0; // for assessment scoring

var exitRqst;      // set true for explicit exit rqsts
// sco frame page load action
function sco_load() 
{

    var result = doLMSInitialize();
	//alert("result: "+result);
    if (result == "false")
         return;
    // Initialize global vars
    curPage = "";
    exitRqst = false;

   //retreived bookmark
	 lastLocation = doLMSGetValue("cmi.core.lesson_location");
//complete/incomplete

	 retScore= doLMSGetValue("cmi.core.score.raw");
	 retxdata= doLMSGetValue("cmi.suspend_data");
    // Set first time lesson status.
	lessonStatus = doLMSGetValue("cmi.core.lesson_status");
	

	//alert("Enter parameters:lastLocation " + lastLocation + " lessonStatus " + lessonStatus + " retScore " + retScore +" retxdata " + retxdata );
	//alert("SCOretScore:--"+retScore);

	var lessonMode="";
	//alert("SCOlastLocation:--"+lastLocation);
	//alert("SCOretxdata: "+retxdata);
	if(lessonStatus!="completed")
	{
		 if (lessonStatus == "not attempted")
		 {
			 if (lessonMode == "browse") 
			 {
				 lessonStatus = "browsed";
			 }
			 else
			 {
				 lessonStatus = "incomplete";
			 }
			 doLMSSetValue("cmi.core.lesson_status", lessonStatus);	
			 doLMSCommit();
		 }
	}

	 
	
return lastLocation;

}
//===================================================================================

// content page load action dcs changed pageLoad to pageload
function sco_pageload(location) {
    if (!LMSIsInitialized()) {
        sco_restart();
        return;
    }
    curPage = location;
	//alert("curPage " + curPage);

	doLMSSetValue("cmi.core.lesson_location", location);
	doLMSSetValue("cmi.core.lesson_status", "incomplete");
    
	/*if (curPage == lastPage && lessonStatus != "browsed") {
        // spec says "" means no preferred starting point for next SCO entry.
		visited=true;
        doLMSSetValue("cmi.core.lesson_location", "");
        doLMSSetValue("cmi.core.lesson_status", "completed");
    } else {
		if(lessonStatus!="completed"){
			doLMSSetValue("cmi.core.lesson_location", location);
			doLMSSetValue("cmi.core.lesson_status", "incomplete");
		}
    }

	*/
    doLMSCommit("");
}

function sco_AssmentScore(totalScore) 
{    
	assessmentScore = totalScore;//totalScore is sent from the Assessment Summary screen and is a percentage as an int
	finalScore=doLMSGetValue("cmi.core.score.raw");//Whatever is stored in the LMS currently
	/*
		The following logic is that if the finalScore, which is received from the LMS is less than 85 then the user
		has not acheived a passing score on the Assessment or they have not taken it before. Therefore they
		make it in the first if.

		Secondly, the assessmentScore is checked to see if it is greater than or equal to 85, which is the passing
		score. If it is greater to or equal than then we should save it in the suspend data field otherwise do not. 
		Regardless, since we made it in the first if we want to store the assessmentScore in the score.raw field and
		then commit the updates to the LMS.
	*/
	if(finalScore < 85)
	{
		if(assessmentScore >= 85)
			doLMSSetValue("cmi.suspend_data",assessmentScore);
		doLMSSetValue("cmi.core.score.raw",assessmentScore);
		doLMSCommit("");
	}
}

// sco frame page onResize event handler.
//
// On a SCORM course window resize, The browser will call this event 
// handler after calling the onUnload handler (sco_unload()) since both
// handlers are "registered" event handlers in the SCO frame page.
// Since the SCO has been unloaded, we need to restart it.
function sco_resize() {
    // Since all the methods check for an initialized API 
    // adaptor, we don't need to restart on resize.  This 
    // causes a failure of the conformance test in IE.
    if (!LMSIsInitialized()) {
        sco_restart();
    }
}

// Restart the SCO.
function sco_restart() {
// is it overkill to reload the page?? my current thinking is yes.
// left this here in case we want to reevaluate this question.
//    window.location.reload();
    return doLMSInitialize();
}

// sco frame page onUnload event handler.
function sco_unload() {
    // If the exit was explicitly requested, we've already done these
    // LMS operations.
	parent.window.setData();
    if (!exitRqst) { 
        if (!LMSIsInitialized()) {
            sco_restart();
        }
        doLMSSetValue("cmi.core.exit", "suspend");
        doLMSFinish();
		parent.window.close();
    }
}

// last page of SCO next button action.
function sco_exitRqstHandler() {
//commented for testing
  if (!LMSIsInitialized()) {
        sco_restart();
    }
    exitRqst = true;
    doLMSSetValue("cmi.core.exit", ""); 
    doLMSFinish();
	setTimeout('window.close()', 1000);
}

//writing the values before exiting
function sco_exit(bookmark,mystatus,myscore,xdata,mvisitedStatus)
{
	
//added to make it work
	if (!LMSIsInitialized()) 
	{
		sco_restart();
    }
    exitRqst = true; 
/////		
  doLMSSetValue("cmi.core.lesson_location", bookmark);
  doLMSSetValue("cmi.core.lesson_status", mystatus); 
  doLMSSetValue("cmi.core.score.raw",myscore);
  doLMSSetValue("cmi.suspend_data",xdata);
	//alert("bookmark:--"+bookmark);
	//alert("mystatus:--"+mystatus);
	//alert("myscore:--"+myscore);
	//alert("xdata:--"+xdata);
	/*
	for (i=0;i<4 ;i++ )
	{
		doLMSSetValue("cmi.objectives."+i+".id", moduleID[i]);
		doLMSSetValue("cmi.objectives."+i+".score.raw", "");
		doLMSSetValue("cmi.objectives."+i+".status", mvisitedStatus[i]);
	}
	*/
	

	doLMSCommit("");
   // doLMSCommit("");
  	sco_exitCourseRqstHandler();
}

// exit course button action.
function sco_exitCourseRqstHandler() 
{
	if (!LMSIsInitialized()) 
	{
		sco_restart();
    }
    exitRqst = true; 
    doLMSSetValue("cmi.core.exit", ""); 
	doLMSFinish();
	setTimeout('top.close()', 1000);
	//setTimeout('top.close()',1000);
}

