//////////////// dependencies  ///////////////////////////////
//	must have "CMITimespan.js" included
//	must have "jdCourse.js" included
//
///////////////////////////////////////////////////////////////////////////////
// 2007/1/9, Jalen: Function "SCOClose()" added.
// 2007/1/3, Jalen: Outdated codes fixed.
// 2006/5/9, Jalen: Property "Type" added to indicate the course type.
// Revised on 2005/8/10, Jalen: Code re-organized.
// Last revised on 2005/5/30, Jalen

jdCourse_HACP.prototype = new jdCourse();
function jdCourse_HACP() {
	this.init = init;
	this.setLMSCurrentPage = setLMSCurrentPage;
	this.setLMSVisitedPages = setLMSVisitedPages;
	this.SCOSetLessonStatus = SCOSetLessonStatus;
	this.getQuizUserAns = getQuizUserAns;
	this.setQuizUserAns = setQuizUserAns;
	this.setQuizUserAns4OneQst = setQuizUserAns4OneQst;
	this.setLMSQuizScore = setLMSQuizScore;
	this.setUserFullName = setUserFullName;
	this.SCOSetSessionTime = SCOSetSessionTime;
	this.SCOFinish = SCOFinish;
	this.SCOClose = SCOClose;
	this.isAICCFound = isAICCFound;

	this.init();

	function init() {
		jdCourse_HACP.prototype.init.call(this);	// Generates error on IE5, so it's commented just for IE5.
		// Added Properties
		this.Type = "AICC";
		this.comeFromQuiz = false;
		// Internal variables
		this.aQuizUserAns = new Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
	}
	
	function setLMSCurrentPage(sPageID) {
		window.frames["dummyFrame"].SCOSetValue("Lesson_Location", sPageID);
	}
	
	function setLMSVisitedPages(sPages) {
		window.frames["dummyFrame"].SCOSetValue("Visited_Pages", sPages);
	}
	
	function SCOSetLessonStatus(sStatus) {
		window.frames["dummyFrame"].SCOSetValue("Lesson_Status", sStatus);
	}
	
	function getQuizUserAns() {
		return this.aQuizUserAns;
	}
	function setQuizUserAns(vArr) {
		if (typeof(vArr) == "string") {
			vArr = vArr.split(",");
		}
		this.aQuizUserAns = vArr;
	}
	
	function setQuizUserAns4OneQst(str) {
		va = str.split(",");
		if (va.length == 2) {
			vIndex = va[0] - 1;
			vValue = va[1];
			this.getQuizUserAns();
			this.aQuizUserAns[vIndex] = vValue;
		}
	}
	
	function setLMSQuizScore(iScore) {
		window.frames["dummyFrame"].SCOSetValue("Score", iScore);
	}

	function setUserFullName(sName) {
		window.frames["dummyFrame"].SCOSetValue("Student_Name", sName);
	}

	function SCOSetSessionTime() {
		var sessionEnd = new Date();
		var sessionTime = sessionEnd.getTime() - this.sessionStart.getTime();
		window.frames["dummyFrame"].SCOSetValue("Time", (new CMITimespan()).Parse(sessionTime));
	}
	
	
	function SCOFinish(dummy) {
		var success = false;
		if (this.SCOInitialized) {
			this.SCOSetSessionTime();
			window.frames["dummyFrame"].ExitAU();
			this.SCOInitialized = false;
			success = true;
		}
		return (success + "");	  // Force type to string
	}
	
	function SCOClose() {
		window.close();
	}
	
	//returns true if both aicc_url and aicc_sid can be found from the URL.
	function isAICCFound() {
		var s = window.location.search.substr(1);
		return ((_KeyValue("aicc_url", s, false) != "") && (_KeyValue("aicc_sid", s, false) != ""));
	}

	function _KeyValue(sKey, sKeyValueString, bCaseSensitive){
		//return the value of the key; if the key does not exist in the key-value pair string, "" is returned;
		//expects sKeyValueString to be a clean string of key=value's separated by "&"
		if ((sKey == "")||(sKey==null)||(sKey==undefined)) return "";
		if ((sKeyValueString == "") || (sKeyValueString == null) || (sKeyValueString == undefined)) return "";
		var aKeyValues = sKeyValueString.split("&");
		for (var i=0;i<aKeyValues.length;i++){
			if (bCaseSensitive) {
				if (aKeyValues[i].split("=")[0] == sKey) return aKeyValues[i].split("=")[1];
			} else {
				if (aKeyValues[i].split("=")[0].toLowerCase() == sKey.toLowerCase()) return aKeyValues[i].split("=")[1];
			}
		}
		return "";
	}

}
