//////////////// dependencies  ///////////////////////////////
//	must have "CMITimespan.js" included
//	must have "SCOObjectives.js" included
//
///////////////////////////////////////////////////////////////////////////////
// 2008/4/8, Jalen: functions "getAllPages()", "addVisitedPages()" removed; "PageStruct" reference removed.
// 2007/3/12, Jalen: "Comments From Learner" supported.
// 2007/1/3, Jalen: "sessionStart" should be an inheritable property.
// 2006/10/12, Jalen: function "isSCOCredit()" modified.
// 2006/4/4, Jalen: function "calcSCOStatus()" modified to take one float paremeter rather than two integers.
// 2005/12/6, Jalen: "cmi.objectives" data is supported.
// 2005/11/23, Jalen: Debug mode added.
// 2005/11/9, Jalen: Script changed for Flash-based course, no control over PageStruct any more.
// Last revised on 2005/6/21, Jalen


function jdCourse() {
	this.init = init;
	this.getCurrentPage = getCurrentPage;
	this.getLMSCurrentPage = getLMSCurrentPage;
	this.setCurrentPage = setCurrentPage;
	this.setLMSCurrentPage = setLMSCurrentPage;
	this.getLastVisitedPage = getLastVisitedPage;
	this.getVisitedPages = getVisitedPages;
	this.getLMSVisitedPages = getLMSVisitedPages;
	this.setVisitedPages = setVisitedPages;
	this.setLMSVisitedPages = setLMSVisitedPages;
	this.calcSCOStatus = calcSCOStatus;
	this.updateSCOStatus = updateSCOStatus;
	this.SCOSetLessonStatus = SCOSetLessonStatus;
	this.getMasteryScore = getMasteryScore;
	this.isSCOCredit = isSCOCredit;
	this.getQuizScore = getQuizScore;
	this.getLMSQuizScore = getLMSQuizScore;
	this.setQuizScore = setQuizScore;
	this.setLMSQuizScore = setLMSQuizScore;
	this.getUserID = getUserID;
	this.getUserFullName = getUserFullName;
	this.getObjectives = getObjectives;
	this.setObjectives = setObjectives;
	this.getCommentsFromLearner = getCommentsFromLearner;
	this.getLMSCommentsFromLearner = getLMSCommentsFromLearner;
	this.setCommentsFromLearner = setCommentsFromLearner;
	this.setLMSCommentsFromLearner = setLMSCommentsFromLearner;
	this.isQuizAttempted = isQuizAttempted;
	this.setQuizAttemptedStatus = setQuizAttemptedStatus;
	this.SCOSetSessionTime = SCOSetSessionTime;
	this.SCOInitialize = SCOInitialize;
	this.SCOFinish = SCOFinish;
	this.isDebugMode = isDebugMode;

	// Internal variables
	var sAllPages;		// String of all Page IDs in the format of "M01|M01_01|M01_02|M02|M03"
	var iQuizScore;
	var sUserFirstName = "John";
	var sUserLastName = "Smith";
	var oObjectives;
	var sCommentsFromLearner;
	var bQuizAttempted = false;

	this.init();

	function init() {
		// CMI data
		//   lesson mode
		this.BROWSE = "browse";
		this.NORMAL = "normal";
		this.REVIEW = "review";
		//   lesson status
		this.NOT_ATTEMPTED = "not attempted";
		this.BROWSED = "browsed";
		this.INCOMPLETE = "incomplete";
		this.COMPLETED = "completed"; 
		this.PASSED = "passed";
		this.FAILED = "failed";
		//	Flag to make sure that LMSInitialize and LMSFinish are only issued once
		//	and that LMS Functions are called in the right order
		this.SCOInitialized = false;  
	
		// More properties
		this.sessionStart = new Date();		// default dummy value
		this.sSeparator = String.fromCharCode(124);	// "|"
		this.defaultMasteryScore;	// when LMS does not support "cmi.student_data.mastery_score", this property will be used to determine lesson_status.
		
		// Protected variables
		this.sCurrentPage;    // Current page Id  (e.g. "M01_02")
		this.sVisitedPages;   // Visited pages list  (e.g. "m01_02|m01_04|m01_03")
	}
	
	function getCurrentPage() {
		if (this.sCurrentPage == null) {
			this.sCurrentPage = this.getLMSCurrentPage();
		}
		//alert("getCurrentPage : " + this.sCurrentPage);
		return this.sCurrentPage;
	}
	
	function getLMSCurrentPage() {
		return this.isDebugMode() ? "14" : "";
	}
	
	function setCurrentPage(sPageID) {
		//alert("setCurrentPage called : " + sPageID);
		this.sCurrentPage = sPageID.toUpperCase();
		this.setLMSCurrentPage(this.sCurrentPage);
	}
	
	function setLMSCurrentPage(sPageID) {
	}
	
	function getLastVisitedPage() {
		return this.getCurrentPage();
	}
	
	function getVisitedPages() {
		if (this.sVisitedPages == null) {
			this.sVisitedPages = this.getLMSVisitedPages();
		}
		return this.sVisitedPages;
	}
	
	function getLMSVisitedPages() {
		if (this.isDebugMode()) {
			var sTemp = "16,15,14,12,11,10,9,8,7,6,5,4,3,2";
			return sTemp.split(",").join("|");
		} else {
			return ""
		}
	}
	
	function setVisitedPages(sPages) {
		//alert("setVisitedPages called : " + sPages);
		this.sVisitedPages = sPages;
		this.setLMSVisitedPages(this.sVisitedPages);
	}
	
	function setLMSVisitedPages(sPages) {
	}
	
	function calcSCOStatus(fCompletionRate) {
		//alert("jdCourse.calcSCOStatus() fCompletionRate: " + fCompletionRate);
		var sStatus = this.NOT_ATTEMPTED;
		if (fCompletionRate >= 1) {
			if (this.isSCOCredit()) {
				if (this.getQuizScore() != null && this.getQuizScore().toString().length > 0) {
					if (this.getQuizScore() >= this.getMasteryScore()) {
						sStatus = this.PASSED;
					} else {
						sStatus = this.FAILED;
					}
				} else {
					sStatus = this.INCOMPLETE;
				}
			} else {
				//alert("Course Completed. (Total " + this.getVisitedPages().split(this.sSeparator).length + " pages)");
				sStatus = this.COMPLETED;
			}
		} else if (fCompletionRate > 0) {
			sStatus = this.INCOMPLETE;
		}
		return sStatus;
	}

	function updateSCOStatus(fPercentage) {
		//alert("jdCourse.updateSCOStatus() fPercentage: " + fPercentage);
		var sStatus = this.calcSCOStatus(fPercentage);
		if (this.isDebugMode())	alert("fPercentage: " + fPercentage + "; Course status: " + sStatus + "; Page: " + this.getCurrentPage() + "; Score: " + this.getQuizScore());
		//if (this.isDebugMode())	alert("fPercentage: " + fPercentage + "; Course status: " + sStatus + "; Page: " + this.getCurrentPage() + "; Score: " + this.getQuizScore() + "; Visited Pages: " + this.getVisitedPages());
		this.SCOSetLessonStatus(sStatus);
	}

	function SCOSetLessonStatus(newStatus) {
	}
	
	function getMasteryScore() {
		return this.defaultMasteryScore;
	}
	
	function isSCOCredit() {
		return (this.getMasteryScore() != null);
	}
	
	function getQuizScore() {
		if (iQuizScore == null) {
			iQuizScore = this.getLMSQuizScore();
		}
		return iQuizScore;
	}
	
	function getLMSQuizScore() {
		return "";
	}

	function setQuizScore(iScore) {
		//alert("jdCourse.setQuizScore(): " + iScore);
		iQuizScore = iScore;
		this.setLMSQuizScore(iScore);
	}
	
	function setLMSQuizScore(iScore) {
	}
	
	function getUserID() {
		return "10001";
	}

	function getUserFullName() {
		return sUserFirstName + " " + sUserLastName;
	}

	function getObjectives() {
		if (oObjectives == null) {
			oObjectives = new SCOObjectives("PreQuiz~^26^^~completed|SimQuiz~^86^^~completed|PostQuiz~^^^~incomplete");
		}
		return oObjectives;
	}
	
	function setObjectives(obj) {
		oObjectives = obj;
	}

	function getCommentsFromLearner() {
		if (sCommentsFromLearner == null) {
			sCommentsFromLearner = this.getLMSCommentsFromLearner();
		}
		return sCommentsFromLearner;
	}
	
	function getLMSCommentsFromLearner() {
		return "";
	}

	function setCommentsFromLearner(sComments) {
		sCommentsFromLearner = sComments;
		this.setLMSCommentsFromLearner(sComments);
	}
	
	function setLMSCommentsFromLearner(sComments) {
	}

	function isQuizAttempted() {
		return bQuizAttempted;
	}
	
	function setQuizAttemptedStatus(bStatus) {
		bQuizAttempted = bStatus;
	}
	
	function SCOSetSessionTime() {
		var sessionEnd = new Date();
		var sessionTime = sessionEnd.getTime() - this.sessionStart.getTime();
		//alert((new CMITimespan()).Parse(sessionTime));
	}
	
	
	function SCOInitialize(dummy) {
		var success = false;
		if (!this.SCOInitialized) {
			this.SCOInitialized = true;
			success = true;
			this.sessionStart = new Date();
			this.getCurrentPage();
			this.getVisitedPages();
			this.getQuizScore();
		}
		//alert("Bookmark: " + this.getCurrentPage() + "; Visited pages: " + this.getVisitedPages());
		return (success + "");	  // Force type to string
	}
	
	function SCOFinish(dummy) {
		var success = false;
		if (this.SCOInitialized) {
			this.SCOSetSessionTime();
			this.SCOInitialized = false;
			success = true;
			window.close();
		}
		return (success + "");	  // Force type to string
	}
	
	function isDebugMode() {
		return false;
	}

}

