/*****************************************************************************
Copyright 2002(c), Trainersoft, Inc. All Rights Reserved
Written by:		Bryan Bosely/Sean Coleman/Joseph Barnes
Date written: 	05/24/2002
Last Updated:   08/02/2002
Description:	This file contains javascripts called from a SCO 

Dependencies:	None	
*****************************************************************************/

/* Finds objects within the dom */
function findObj(n, d) { 
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

/* load SCO */
function loadSCO(){
  doLMSInitialize();
  
  // determine if the student has been in the sco before
  var entry = doLMSGetValue("cmi.core.entry");
  
  // is the student resuming a previous session
  if (entry == "resume") {
  	var location = doLMSGetValue("cmi.core.lesson_location"); // get the last page the student was on
	return location
  }
  else {
  	// the student is now attempting the lesson
	doLMSSetValue("cmi.core.lesson_status", "incomplete");
	return null
  }
}

function trackQuestionScore(iQuestionIdx, iScore, strStatus, strResponse) {	
    // Put data into the objectives
	strTemp = "cmi.objectives." + iQuestionIdx + ".score.raw";
	doLMSSetValue(strTemp, iScore);
	strTemp = "cmi.objectives." + iQuestionIdx + ".status";
	doLMSSetValue(strTemp, strStatus);

	// Put data into the interactions
	strTemp = "cmi.interactions." + iQuestionIdx + ".objectives.0.score.raw";
	doLMSSetValue(strTemp, iScore);
	strTemp = "cmi.interactions." + iQuestionIdx + ".objectives.0.status";
	doLMSSetValue(strTemp, strStatus);
	strTemp = "cmi.interactions." + iQuestionIdx + ".student_response";
	if (String(strResponse).length > 255) {
		doLMSSetValue(strTemp, String(strResponse).substring(0, 255));
	} else {
		doLMSSetValue(strTemp, strResponse);
	}
	
	strTemp = "cmi.interactions." + iQuestionIdx + ".result";
	strResult = "neutral";
    switch (strStatus) {
		case "passed": 
			strResult = "correct"; break;				            
		case "failed":
			strResult = "wrong"; break;
		default:
			strResult = "neutral"; break;
    }	
    doLMSSetValue(strTemp, strResult);	
    
    doLMSCommit();
}

var iNumQuestions = 0;
function registerQuestion(strQuestionName, strType) {
    // Put data into the objectives
	strTemp = "cmi.objectives." + iNumQuestions + ".id";
	doLMSSetValue(strTemp, strQuestionName)
	strTemp = "cmi.objectives." + iNumQuestions + ".status";
	doLMSSetValue(strTemp, "not attempted");
	
	// Put data into the interactions
	strTemp = "cmi.interactions." + iNumQuestions + ".id";
	doLMSSetValue(strTemp, strQuestionName);
	strTemp = "cmi.interactions." + iNumQuestions + ".type";
	doLMSSetValue(strTemp, strType);
	strTemp = "cmi.interactions." + iNumQuestions + ".objectives.0.id";
	doLMSSetValue(strTemp, strQuestionName);
	strTemp = "cmi.interactions." + iNumQuestions + ".objectives.0.status";
	doLMSSetValue(strTemp, "incomplete");
	
	doLMSCommit();
	return(iNumQuestions++);
}

function trackTestScore(iScore) {
	doLMSSetValue("cmi.core.score.raw", iScore);
	doLMSCommit();
}

/* exit the sco */
function doExit(bCourseComplete, strLocation, strSessionTime) {

  // set the course status
  if (bCourseComplete == 0) {
	doLMSSetValue("cmi.core.lesson_status", "incomplete");
	doLMSSetValue("cmi.core.exit", "suspend");
  } else {
	doLMSSetValue("cmi.core.lesson_status", "completed");
	doLMSSetValue("cmi.core.exit", "logout");	
  }
  // update the session time
  doLMSSetValue("cmi.core.session_time", strSessionTime);
  
  // Get current page and set the location within the sco
  doLMSSetValue("cmi.core.lesson_location", strLocation);
   
  // commit and finish
  doLMSCommit();
  doLMSFinish();
}
