﻿//-----------------------------------------------------------------------------
// Copyright (C) 2010 Cingletree Learning, LLC.  All rights reserved.
//-----------------------------------------------------------------------------
var passingScore = "Good Job!";
var perfectScore = "Perfect Score!";

document.body.onload = OnLessonLoad;

function OnLessonLoad() {
	DisableEvents();
}

function DisableEvents() { // IE only

	document.getElementById("Lesson-content").onselectstart = new Function("return false;");
	document.getElementById("Lesson-content").oncontextmenu = new Function("return false;");
}

//-----------------------------------------------------------------------
function Toggle(aEvent) {

	if (window.event) { // IE
		selection = window.event.srcElement;
	}
	else { // Mozilla
		selection = aEvent.target;
	}

	if (selection.id.charAt(0) != "s" || selection.className.indexOf("orrect") > -1) {
		return;
	}

	if (selection.className.indexOf("off") > -1) {
		selection.className = selection.className.replace("off", "on");
		selection.className = selection.className.replace("hidden", "visible");
		selection.className = selection.className.replace("noCap", "cap");
		selection.className = selection.className.replace("lower", "upper");
	}
	else if (selection.className.indexOf("on") > -1) {
		selection.className = selection.className.replace("on", "off");
		selection.className = selection.className.replace("visible", "hidden");
		selection.className = selection.className.replace("cap", "noCap");
		selection.className = selection.className.replace("upper", "lower");
	}
}

//-----------------------------------------------------------------------
var correctAnswers = 0;
var responseBinaryString = "";
var lessonScored = false;
var isTest = false;

function ScoreClick(problem) {

	if (!ProblemAttempted(problem)) {
		alert("Please make a selection before checking answer");
		return;
	}

	Score(problem);
}

function ProblemAttempted(problem) {

	var i = 0;

	while (selection = document.getElementById("s" + problem + "-" + i)) {
		if (selection.className.indexOf("on") >= 0) {
			return true;
		}
		i++;
	}

	return false;
}

function Score(problem) {

	var button = document.getElementById("b" + problem);

	if (button.className != "testButton" && button.className != "answerButton") {
		return;
	}

	var answers = key[problem - 1];
	var selection = null;
	var selections = "";
	var i = 0;

	while (selection = document.getElementById("s" + problem + "-" + i)) {
		var selected = (selection.className.indexOf("on") > -1);
		selections += (selected ? "1" : "0")
		var answer = answers.charAt(i);
		selection.className = selection.className + (answer == "1" ? " correct" : " notCorrect");
		if (answer == "0" && (selection.className.indexOf("on") > -1 || selection.className.indexOf("hidden") > -1)) {
			selection.style.color = "#ffffff";
			if (selection.className.indexOf("hidden") < 0) {
				selection.style.borderColor = "#cc0000";
			}
		}
		i++;
	}

	var correct = (answers == selections);

	if (correct) {
		button.className = "correctButton";
		correctAnswers++;
		SetScore();
	}
	else {
		button.value = "X";
		button.className = "errorButton";
	}

	button.onclick = null;
	button.blur();
}

function GetSelections(problem) {

	var selection = null;
	var selections = "";
	var i = 0;

	while (selection = document.getElementById("s" + problem + "-" + i)) {
		var selected = (selection.className.indexOf("on") > -1);
		selections += (selected ? "1" : "0")
		i++;
	}

	return selections;
}

function CheckProblems() {

	for (problem = 1; problem <= key.length; problem++) {
		var button = document.getElementById("b" + problem);

		if ((button.className == "testButton" || button.className == "answerButton") && !ProblemAttempted(problem)) {
			return false;
		}
	}

	return true;
}

function ScoreProblems() {

	if (lessonScored) {
		return;
	}

	for (problem = 1; problem <= key.length; problem++) {
		Score(problem);
		responseBinaryString += GetSelections(problem);
	}

	var scorePercentage = Math.round(correctAnswers * 100 / key.length)
	if (scorePercentage == 100) {
		alert(perfectScore);
	}
	else if (scorePercentage > 79) {
		alert(passingScore);
	}

	lessonScored = true;
}

function ScoreLesson() {

	if (!CheckProblems()) {
		alert("Please answer all the questions");
		return false;
	}

	ScoreProblems();

	return true;
}

function SetScore() {
	document.getElementById("Score-label").firstChild.nodeValue = " " + Math.round(correctAnswers * 100 / key.length) + "% (" + correctAnswers + "/" + key.length + ")";
}

function ResetLesson() {
	location.reload(true);
}

function PrintLesson() {

	if (CheckProblems()) {
		document.body.style.display = "block";
		document.title = "Student:  " + document.getElementById("Student-name").value;
		window.print();
	}
	else {
		alert("Answer all the questions before printing");
	}
}

function SubmitButton_Click() {
	var isValid = ScoreLesson();

	if (isValid) {
		SaveInteractions();
		SubmitLesson();
	}
}

function SubmitLesson() {

	var scoreScaled = Math.round(correctAnswers * 100 / key.length) / 100;

	__SetValue("cmi.score.scaled", "" + scoreScaled);
	__SetValue("cmi.score.raw", "" + correctAnswers);

	if (scoreScaled >= 0.8) {
		__SetValue("cmi.success_status", "passed");
	}
	else {
		__SetValue("cmi.success_status", "failed");
	}

	__SetValue("cmi.completion_status", "completed");
	__SetValue("cmi.exit", "normal");

	var canNavigateToNext = __GetValue("adl.nav.request_valid.continue");
	if (canNavigateToNext == "true") {
		__SetValue("adl.nav.request", "continue");
	}

	__Terminate();
}

function SaveInteractions() {

	var suspendData = "";

	for (problem = 1; problem <= key.length; problem++) {
		var problemButton = document.getElementById("b" + problem);

		if (problemButton.className != "answerButton" && problemButton.className != "testButton") {
			suspendData += (suspendData.length == 0 ? "" + problem : "," + problem);
		}

		var selections = GetSelections(problem);
		var answers = key[problem - 1];
		var studentResponse = "";
		var correctResponse = "";

		for (index = 0; index < selections.length; index++) {
			if (selections.charAt(index) == "1") {
				studentResponse += (studentResponse.length == 0 ? "" + (index + 1) : "[,]" + (index + 1));
			}
			if (answers.charAt(index) == "1") {
				correctResponse += (correctResponse.length == 0 ? "" + (index + 1) : "[,]" + (index + 1));
			}
		}

		__SetValue("cmi.interactions." + (problem - 1) + ".id", "" + problem);
		__SetValue("cmi.interactions." + (problem - 1) + ".type", "choice");
//		__SetValue("cmi.interactions." + (problem - 1) + ".timestamp", loadTimestamp);
		__SetValue("cmi.interactions." + (problem - 1) + ".learner_response", studentResponse);
		__SetValue("cmi.interactions." + (problem - 1) + ".correct_responses.0.pattern", correctResponse);

		if (answers == selections) {
			__SetValue("cmi.interactions." + (problem - 1) + ".result", "correct");
		}
		else {
			__SetValue("cmi.interactions." + (problem - 1) + ".result", "incorrect");
		}
	}

	__SetValue("cmi.suspend_data", suspendData);
}


