// Copyright(c) Outstart inc.
// Author: Binh Tong
// SoftSim version 6.0
// File description: this javascript file define variables that store Sim's data.
function splitStr(strSteps, strSeparate) {
	var arrStepDetails = new Array();
	var index1, index2, i;
	var n = strSeparate.length;
	index1 = 0; 
	index2 = 0;
	var count = 0;
	strSteps = "" + strSteps ;
	index2 = strSteps.indexOf(strSeparate, index1);
	while(index2 != -1)	{
		arrStepDetails[count] = strSteps.substring(index1,index2);
		count = count+1;
		index1 = index2+n;
		index2 = strSteps.indexOf(strSeparate, index1);
		if (index2 == -1) {
			arrStepDetails[count] = strSteps.substring(index1,strSteps.length);
		}
	}
	return arrStepDetails;
}

SSObjective = function() {
	this.ObjType = 1 ;
	this.ObjID = "";
	this.ObjTitle = "";
	this.ObjPassingScore=0;
	this.ObjStatus=0; // passed or failed
	this.ObjScore=0;
	this.separate="#!";
}
SSObjective.prototype.parseStr = function (strSteps) {
	var arrStepDetails;
	this.StepStr = strSteps;
	arrStepDetails = splitStr(strSteps, this.separate);
	if (typeof arrStepDetails[0] != "undefined") {
		this.ObjID = arrStepDetails[0];
	}
	if (typeof arrStepDetails[1] != "undefined") {
		this.ObjType = arrStepDetails[1];
	}
	if (typeof arrStepDetails[2] != "undefined") {
		this.ObjTitle = arrStepDetails[2];
	}
	if (typeof arrStepDetails[3] != "undefined") {
		this.ObjPassingScore = arrStepDetails[3];
	}
	if (typeof arrStepDetails[4] != "undefined") {
		this.ObjScore = arrStepDetails[4];
	}
	if (typeof arrStepDetails[5] != "undefined") {
		this.ObjStatus = arrStepDetails[5];
	}
}
StepData = function () {
	this.ID = "";
	this.Description = "";
	this.result = "0";  // 0: false; 1: true
	this.numOfresponse = 0;
	this.numOfattempt = 0;
	this.correct_responses = new Array();
	this.weighting = 0;
	this.learner_response = "";
	this.type = "performance";
	this.latency = "";
	this.time = "00:00:00";
	this.StepValue = 0;

	this.StepStr = "";
	this.separate = "@#";
	this.CorrectResponseSeparate = "#!";
} 
StepData.prototype.CopyFrom = function (StepItem) {
var i = 0;
	this.ID = StepItem.ID;
	this.Description = StepItem.Description;
	this.result = StepItem.result;  // 0: false; 1: true
	this.numOfresponse = StepItem.numOfresponse;
	this.numOfattempt = StepItem.numOfattempt;
	this.correct_responses = new Array();
	for(i=0;i<StepItem.correct_responses.length; i++) {
		this.correct_responses[i] = StepItem.correct_responses[i];
	}
	this.weighting = StepItem.weighting;
	this.learner_response = StepItem.learner_response;
	this.type = StepItem.type;
	this.latency = StepItem.latency;
	this.time = StepItem.time;
	this.StepStr = StepItem.StepStr;
}
StepData.prototype.getResult = function () {
var str = ""
	if (this.result == "0") {
		str = "wrong";
	}
	if (this.result == "1") {
		str = "correct";
	}
	return str;
}
StepData.prototype.splitStr = function (strSteps,strSeparate) {
	var arrStepDetails = new Array();
	var index1, index2, i;
	var n = strSeparate.length;
	index1 = 0; 
	index2 = 0;
	var count = 0;
	strSteps = "" + strSteps ;
	index2 = strSteps.indexOf(strSeparate, index1);
	if ((index2 == -1) && (strSteps != "")) {
		arrStepDetails[count] = strSteps;
		count = count + 1;
	}
	while(index2 != -1)	{
		arrStepDetails[count] = strSteps.substring(index1,index2);
		count = count+1;
		index1 = index2+n;
		index2 = strSteps.indexOf(strSeparate, index1);
		if (index2 == -1) {
			arrStepDetails[count] = strSteps.substring(index1,strSteps.length);
		}
	}
	return arrStepDetails;
}
StepData.prototype.parseCorrectResponse = function (str) {
	var arrStepDetails;
	var i=0;
	arrStepDetails = this.splitStr(str, this.CorrectResponseSeparate);
	for (i=0; i<arrStepDetails.length ; i++) {
		this.correct_responses[i] = arrStepDetails[i];
	}
}
StepData.prototype.parseStr = function (strSteps) {
	var arrStepDetails;
	this.StepStr = strSteps;
	arrStepDetails = this.splitStr(strSteps, this.separate);
	if (typeof arrStepDetails[0] != "undefined") {
		this.ID = arrStepDetails[0];
	}
	if (typeof arrStepDetails[1] != "undefined") {
		this.Description = arrStepDetails[1];
	}
	if (typeof arrStepDetails[2] != "undefined") {
		this.result = arrStepDetails[2];
	}
	if (typeof arrStepDetails[3] != "undefined") {
		this.numOfresponse = arrStepDetails[3];
	}
	if (typeof arrStepDetails[4] != "undefined") {
		this.numOfattempt = arrStepDetails[4];
	}
	if (typeof arrStepDetails[5] != "undefined") {
		// this.correct_response = arrStepDetails[5];
		this.parseCorrectResponse(arrStepDetails[5]);
	}
	if (typeof arrStepDetails[6] != "undefined") {
		this.weight = arrStepDetails[6];
	}
	if (typeof arrStepDetails[7] != "undefined") {
		this.learner_response = arrStepDetails[7];
	}
	if (typeof arrStepDetails[8] != "undefined") {
		this.type = arrStepDetails[8];
	}
	if (typeof arrStepDetails[9] != "undefined") {
		this.latency = arrStepDetails[9];
	}
	if (typeof arrStepDetails[10] != "undefined") {
		this.time = arrStepDetails[10];
	}
	if (typeof arrStepDetails[11] != "undefined") {
		this.StepValue = arrStepDetails[11];
	}
}

StepData.prototype.toString = function () {
	var str;
	var tmpstr = "";
	var i = 0;
	str = this.ID;
	str += this.separate;
	str += this.Description;
	str += this.separate;
	str += this.result;  // 0: false; 1: true
	str += this.separate;
	str += this.numOfresponse;
	str += this.separate;
	str += this.numOfattempt;
	str += this.separate;
	for(i=0; i<this.correct_responses.length; i++){
		if (tmpstr != "") {
			tmpstr += this.CorrectResponseSeparate;
		}
		tmpstr += this.correct_responses[i];
	}
	str += tmpstr;
	str += this.separate;
	str += this.weighting;
	str += this.separate;
	str += this.learner_response;
	str += this.separate;
	str += this.type;
	str += this.separate;
	str += this.latency;
	str += this.separate;
	str += this.time;
	str += this.separate;
	str += this.StepValue;
	return str;
}
//**********************************************
StepTrace = function () {
	this.StepList = new Array();
	this.Objectives = new Array();
	this.StepStr = "";
	this.max = 100;
	this.min = 0;
	this.raw = 0;
	this.lessonname = "";
	this.factor = 0;
	this.passingscore = 0;
	this.exitstatus = 0;
	this.timespent = 0;
	this.sessiontime = "00:00:00";
	this.status = "unknown";
	this.playmode = "4";
	this.NumOfcorrectStep = 0;
	this.NumOfincorrectStep = 0;
	this.currStep = "";
	this.yourPoint = 0;
	this.totalPoint = 0;
	this.complete = "incomplete";
	this.completedSteps = 0;
	this.StepTotal = 0;

	this.separate = "#";
	this.stepSeparate = "@@";
	this.Separate1 = "##";
 }

StepTrace.prototype.getPlayMode = function () {
var str = "";
	switch(this.playmode) {
	case 0:
		str = '';
		break;
	case 1:
		str = 'Show Me';
		break;
	case 2:
		str = 'Guide Me';
		break;
	case 3:
		str = 'Quiz';
		break;
	case 4:
		str = 'Assessment';
		break;
	}
	return str;
}

StepTrace.prototype.getMax = function () {
	return this.max;
}

StepTrace.prototype.getMin = function () {
	return this.min;
}

StepTrace.prototype.getRaw = function () {
	return this.raw;
}

StepTrace.prototype.getPassingScore = function () {
	return this.passingscore;
}

StepTrace.prototype.getSessionTime = function () {
	return this.sessiontime;
}

StepTrace.prototype.getExitStatus = function () {
	var str = "suspend";
	switch (parseInt(this.exitstatus)) {
		case 0:
			str = "suspend" ;
			break;
		case 3:
			str = "time-out" ;
			break;
		case 4:
			str = "logout" ;
			break;
	}
	return str;
}

StepTrace.prototype.getTimeSpent = function () {
	return this.timespent;
}

StepTrace.prototype.getStatus = function () {
	return this.status;
}
StepTrace.prototype.progress_measure = function () {
	if (this.StepTotal == 0) {
		return 0;
	}
	var result = (this.completedSteps/this.StepTotal) * 100 + 0.5;
	result = Math.floor((result>100?100:result))/100 ;
	return (result);
}
StepTrace.prototype.getComplete = function () {
	return this.complete;
}

StepTrace.prototype.getCorrectSteps = function () {
	return this.NumOfcorrectStep;
}

StepTrace.prototype.getInCorrectSteps = function () {
	return this.NumOfincorrectStep;
}

StepTrace.prototype.setPlayMode = function (playmode) {
	this.playmode = playmode;
}

StepTrace.prototype.setMax = function (max) {
	this.max = max;
}

StepTrace.prototype.setMin = function (min) {
	this.min = min;
}

StepTrace.prototype.setRaw = function (raw) {
	this.raw = raw;
}

StepTrace.prototype.setPassingScore = function (passingscore) {
	this.passingscore = passingscore;
}

StepTrace.prototype.setExitStatus = function (exitstatus) {
	return this.exitstatus = exitstatus;
}

StepTrace.prototype.setTimeSpent = function (timespent) {
	this.timespent = timespent;
}

StepTrace.prototype.setStatus = function (status) {
	this.status = status;
}

StepTrace.prototype.setComplete = function (complete) {
	this.complete = complete;
}

StepTrace.prototype.setCorrectSteps = function (correctsteps) {
	this.NumOfcorrectStep = correctsteps;
}

StepTrace.prototype.setInCorrectSteps = function (incorrectsteps) {
	this.NumOfincorrectStep = incorrectsteps;
}

StepTrace.prototype.clearAll = function () {
	this.deleteAll();
	this.raw = 0;
	this.exitstatus = 0;
	this.timespent = 0;
	this.sessiontime = "00:00:00";
	this.status = "unknown";
	this.NumOfcorrectStep = 0;
	this.NumOfincorrectStep = 0;
	this.currStep = "";
	this.yourPoint = 0;
	this.totalPoint = 0;
	this.complete = "incomplete";
	this.completedSteps = 0;
	this.StepTotal = 0;
}

StepTrace.prototype.deleteAll = function () {
	while (this.StepList.length>0) 
		this.StepList.pop();
	this.StepStr = "";
}

StepTrace.prototype.InsertStepStr = function (StepStr) {
	var StepDataItem = new StepData();
	StepDataItem.parseStr(StepStr);
	this.InsertStep(StepDataItem);
}

StepTrace.prototype.InsertStep = function (StepDataItem) {
	var pStepData;
	var bFound = 0;
	var i=0;
	while ((bFound == 0) && (i<this.StepList.length)) {
		pStepData = this.StepList[i];
		if (pStepData.ID == StepDataItem.ID) {
			bFound = 1;
		} else {
			i++;
		}
	}
	this.StepList[i] = StepDataItem;
	if (bFound != 0) {
		delete pStepData;
	}
}

StepTrace.prototype.InsertObjective = function (ObjectiveStr) {
	var ObjectiveItem = new SSObjective();
	ObjectiveItem.parseStr(ObjectiveStr);
	this.Objectives[this.Objectives.length] = ObjectiveItem;
}

StepTrace.prototype.splitStr = function (strSteps, strSeparate) {
	var arrStepDetails = new Array();
	var index1, index2, i;
	var n = strSeparate.length;
	index1 = 0; 
	index2 = 0;
	var count = 0;
	strSteps = "" + strSteps ;
	index2 = strSteps.indexOf(strSeparate, index1);
	while(index2 != -1)	{
		arrStepDetails[count] = strSteps.substring(index1,index2);
		count = count+1;
		index1 = index2+n;
		index2 = strSteps.indexOf(strSeparate, index1);
		if (index2 == -1) {
			arrStepDetails[count] = strSteps.substring(index1,strSteps.length);
		}
	}
	return arrStepDetails;
}
StepTrace.prototype.parseStr = function (strSteps) {
	var arrStepDetails;
	arrStepDetails = this.splitStr(strSteps, this.Separate1);
	for (i=0; i<arrStepDetails.length; i++){
		var StepDataItem = new StepData();
		StepDataItem.parseStr("" + arrStepDetails[i]);
		this.InsertStep(StepDataItem);	
	}
	this.StepStr = strSteps ;
}

StepTrace.prototype.parseSimData = function (strSteps) {
	var arrStepDetails;
	arrStepDetails = this.splitStr(strSteps, this.separate);
	
	if (typeof arrStepDetails[0] != "undefined") {
		this.timespent = arrStepDetails[0];
	}
	if (typeof arrStepDetails[1] != "undefined") {
		this.NumOfcorrectStep = arrStepDetails[1];
	}
	if (typeof arrStepDetails[2] != "undefined") {
		this.NumOfincorrectStep = arrStepDetails[2];
	}
	if (typeof arrStepDetails[3] != "undefined") {
		this.currStep = arrStepDetails[3];
	}
	if (typeof arrStepDetails[4] != "undefined") {
		this.yourPoint = arrStepDetails[4];
	}
	if (typeof arrStepDetails[5] != "undefined") {
		this.totalPoint = arrStepDetails[5];
	}
	if (typeof arrStepDetails[6] != "undefined") {
		this.completedSteps = arrStepDetails[6];
	}
	if (typeof arrStepDetails[7] != "undefined") {
		this.StepTotal = arrStepDetails[7];
	}
}

StepTrace.prototype.parseFinalData = function (strSteps) {
	var arrStepDetails;
	arrStepDetails = this.splitStr(strSteps, this.separate);
	if (typeof arrStepDetails[0] != "undefined") {
		this.raw = arrStepDetails[0];
	}
	if (typeof arrStepDetails[1] != "undefined") {
		this.exitstatus = arrStepDetails[1];
	}
	if (typeof arrStepDetails[2] != "undefined") {
		this.status = arrStepDetails[2];
	}
	if (typeof arrStepDetails[3] != "undefined") {
		this.complete = arrStepDetails[3];
	}
	if (typeof arrStepDetails[4] != "undefined") {
		this.sessiontime = arrStepDetails[4];
	}
	if (typeof arrStepDetails[5] != "undefined") {
		this.factor = arrStepDetails[5];
	}
}

StepTrace.prototype.setToLocation = function () {
	var str;
	str = this.playmode;
	str += this.separate;
	str += this.NumOfcorrectStep;
	str += this.separate;
	str += this.NumOfincorrectStep;
	return str;
}

StepTrace.prototype.getFromLocation = function (strLocation) {
	var arrStepDetails;
	arrStepDetails = this.splitStr(strLocation, this.separate);
	
	if (typeof arrStepDetails[0] != "undefined") {
		this.playmode = arrStepDetails[0];
	}
	if (typeof arrStepDetails[1] != "undefined") {
		this.NumOfcorrectStep = arrStepDetails[1];
	}
	if (typeof arrStepDetails[2] != "undefined") {
		this.NumOfincorrectStep = arrStepDetails[2];
	}
}

StepTrace.prototype.setToSuspendData = function () {
	var i=0;
	var pStepData;
	var str = "";
	var StepStr = "";
	str += this.timespent;
	str += this.separate;
	str += this.status;
	str += this.separate;
	str += this.playmode;
	str += this.separate;
	str += this.NumOfcorrectStep;
	str += this.separate;
	str += this.NumOfincorrectStep;
	str += this.separate;
	str += this.currStep;
	str += this.separate;
	str += this.yourPoint;
	str += this.separate;
	str += this.totalPoint;
	str += this.separate;
	str += this.completedSteps;
	str += this.separate;
	str += this.StepTotal;
	str += this.stepSeparate;
	StepStr = "";
	for(i=0; i<this.StepList.length; i++) {
		pStepData = this.StepList[i];
		if (StepStr != "") {
			StepStr += this.Separate1;
		}
		StepStr += pStepData.toString();
	}	
	str += StepStr;
	return str;
}

StepTrace.prototype.getFromSuspendData = function (strSuspendData) {
	var arrStepDetails;
	var StepStr = "";
	var str = "";
	arrStepDetails = this.splitStr(strSuspendData, this.stepSeparate);
	
	str += arrStepDetails[0];
	StepStr += arrStepDetails[1];
	
	this.parseStr(StepStr);
	
	arrStepDetails = this.splitStr(str, this.separate);

	if (typeof arrStepDetails[0] != "undefined") {
		this.timespent = arrStepDetails[0];
	}
	if (typeof arrStepDetails[1] != "undefined") {
		this.status = arrStepDetails[1];
	}
	if (typeof arrStepDetails[2] != "undefined") {
		this.playmode = arrStepDetails[2];
	}
	if (typeof arrStepDetails[3] != "undefined") {
		this.NumOfcorrectStep = arrStepDetails[3];
	}
	if (typeof arrStepDetails[4] != "undefined") {
		this.NumOfincorrectStep = arrStepDetails[4];
	}
	if (typeof arrStepDetails[5] != "undefined") {
		this.currStep = arrStepDetails[5];
	}
	if (typeof arrStepDetails[6] != "undefined") {
		this.yourPoint = arrStepDetails[6];
	}
	if (typeof arrStepDetails[7] != "undefined") {
		this.totalPoint = arrStepDetails[7];
	}
	if (typeof arrStepDetails[8] != "undefined") {
		this.completedSteps = arrStepDetails[8];
	}
	if (typeof arrStepDetails[9] != "undefined") {
		this.StepTotal = arrStepDetails[9];
	}
}
