/*****************************************************************************
Copyright 2002(c), OutStart, 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 getSuspendData() {
   suspendData = doLMSGetValue("cmi.suspend_data");
   return suspendData;
}

function getStudentName() {
   studentName = doLMSGetValue("cmi.core.student_name");
   return studentName;
}

function getStudentId() {
   studentId = doLMSGetValue("cmi.core.student_id");
   return studentId;
}

function trackQuestionScore(iQuestionIdx, iScore, strStatus, strResponse) {
	var endTime = new Date();
	//var strTime = endTime.getHours() + ":" + endTime.getMinutes() + ":" + endTime.getSeconds();

	var h = endTime.getHours();
	while(String(h).length < 2)
	{
		h = '0' + h;
	}

	var m = endTime.getMinutes();
	while(String(m).length < 2)
	{
		m = '0' + m;
	}

	var mm = endTime.getSeconds();
	while(String(mm).length < 2)
	{
		mm = '0' + mm;
	}

	var strTime = h + ":" + m + ":" + mm;
	
    // 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);	
    
	// SCORM 2004
	// alert("set end_time:" + strTime);
	strTemp = "cmi.interactions." + iQuestionIdx + ".time";
	doLMSSetValue(strTemp, strTime);
	
    doLMSCommit();
}

var iNumQuestions = 0;
function registerQuestion(strQuestionName, strType, iWeight) {
    // 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");

	// SCORM 2004
	strTemp = "cmi.objectives." + iNumQuestions + ".score.min";
	doLMSSetValue(strTemp, 0);
	strTemp = "cmi.objectives." + iNumQuestions + ".score.max";
	doLMSSetValue(strTemp, 100);
	strTemp = "cmi.interactions." + iNumQuestions + ".weighting";
	doLMSSetValue(strTemp, iWeight);
		
	doLMSCommit();
	return(iNumQuestions++);
}

function trackTestScore(iScore) {
	doLMSSetValue("cmi.core.score.raw", iScore);
	doLMSSetValue("cmi.core.score.max", 100);
	doLMSSetValue("cmi.core.score.min", 0);
	doLMSCommit();
}

/* exit the sco */
function doExit(bCourseComplete, bPass, strLocation, strSessionTime, strSuspendData) {
  var lessonMode, credit;
  lessonMode =  doLMSGetValue("cmi.core.lesson_mode");
  credit = doLMSGetValue("cmi.core.credit");

  // not track score
  if (lessonMode == 'browse' || lessonMode == 'review')
  {
    doLMSSetValue("cmi.core.exit", '');
    if (lessonMode == 'browse')
      doLMSSetValue("cmi.core.lesson_status", "browsed");
    doLMSCommit();
    doLMSFinish();
    return;
  }
  // track values of course
  if (bCourseComplete == 0) {	// not visited lastpage
	doLMSSetValue("cmi.core.exit", "suspend");
	doLMSSetValue("cmi.core.lesson_status", "incomplete");
  } 
  else if (credit == 'no-credit'){
	doLMSSetValue("cmi.core.exit", "");	
	doLMSSetValue("cmi.core.lesson_status", "completed");
  }
  else if (bPass == true){
	doLMSSetValue("cmi.core.exit", "");	
	doLMSSetValue("cmi.core.lesson_status", "passed");
  }
  else {
	doLMSSetValue("cmi.core.exit", "");	
	doLMSSetValue("cmi.core.lesson_status", "failed");
  }

  // 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);

  // Get suspend data within the sco
  doLMSSetValue("cmi.suspend_data", strSuspendData);
   
  // commit and finish
  doLMSCommit();
  doLMSFinish();
}
/*
function GetTimeString(Time)
{ strStart = '';
  hours   = 0;
  minutes = 0;
  seconds = 0;
  Time = Time / 1000;	// Convert to seconds.

  if(Time >= 3600)
   { hours = Math.floor(Time / 3600);
     Time = Time - hours * 3600;
   };

  if(Time >= 60)
   { minutes = Math.floor(Time / 60);
     Time = Time - minutes * 60;
   };

  seconds = Math.floor(Time);

  // Get Hours.
  strTemp = hours;
  if(strTemp < 10) { strStart += '0'; };
  strStart += strTemp;
  strStart += ':';
 
  // Get Minutes.
  strTemp = minutes;
  if(strTemp < 10) { strStart += '0'; };
  strStart += strTemp;
  strStart += ':';

  // Get Seconds.
  strTemp = seconds;
  if(strTemp < 10) { strStart += '0'; };
  strStart += strTemp;
  return(strStart);
};
*/
