/**
 *  ==================================================
 *  SoftChalk LessonBuilder
 *  Copyright 2003-2006 SoftChalk LLC
 *  All Rights Reserved.
 *
 *  http://www.softchalk.com
 *  ==================================================
 *  File date: September 12, 2006
 */

/*
 * other = 0 as a safety
 * 1 is not used
 *
 * FLASH_CARD_ACTIVITY = 2;
 * SEEK_A_WORD_ACTIVITY = 3;
 * DRAG_N_DROP_ACTIVITY = 4;
 * ORDERING_ACTIVITY = 5;
 * CROSSWORD_ACTIVITY = 6;
 * LABELING_ACTIVITY = 7;
 * SORTING_ACTIVITY = 8;
 * HOT_SPOT_ACTIVITY = 9;
 */


function readit() {
  cookString = "?";
  if(document.cookie != "") {
    theCook = document.cookie.split("; ");
    for (i = 0; i < theCook.length; i ++) {
      cookString += theCook[i] + "&";
    }
  }
}

function cookit() {
	//alert("0 is " + cookit.arguments[0] + "\n1 is " + cookit.arguments[1] + "\n2 is " + cookit.arguments[2] + "\n3 is " + cookit.arguments[3]);
	var arg1 = cookit.arguments[0];

	// bail if the activity has no ID
	if (arg1.length < 6) {
		return;
	}

	var v = arg1.substring(0,arg1.length - 5);
	var c = v.indexOf(":");

	if (c != -1) {
		var applet_id = v.substring(0, c);
		var act_type = v.substring(c + 1);
	}
	else {
		var applet_id = v;
		var act_type = 0;
	}

	var applet_value = eval("parent.a_value" + applet_id);

	// bail if the activity is not assigned any points
	// or it has not reached 100%
	if (applet_value == 0 || parent.q_skip_a[applet_id]) {
		return;
	}

	var percent_value = cookit.arguments[1];

	// check for first attempt at activity
	// and set attempted_points
	// q_done_a is only for showing if activity has been attempted
	if (!parent.q_done_a[applet_id]) {
		parent.attempted_q++;
		parent.attempted_points = parent.attempted_points + applet_value;
		parent.q_done_a[applet_id] = true;
	}

	var a_running_score = ((applet_value * percent_value) / 100);
	var a_rounded_score = Math.round(a_running_score * 10) / 10;

	if (a_rounded_score == parent.old_score_a[applet_id] && cookit.arguments[3] == 1) {
		parent.old_score_a[applet_id] = parent.old_score_a[applet_id] + a_rounded_score;
	}

	if (a_rounded_score < parent.old_score_a[applet_id]) {
		parent.old_score_a[applet_id] = parent.old_score_a[applet_id] - a_rounded_score;
		var added_score = a_rounded_score;
		parent.score_a[applet_id] = parent.score_a[applet_id] + added_score;
		parent.my_score = parent.my_score + added_score;
	}
	else {
		if (parent.score_a[applet_id] > 0) {
			var added_score = Math.round((a_rounded_score - parent.old_score_a[applet_id]) * 10) / 10;
			parent.score_a[applet_id] = parent.score_a[applet_id] + added_score;
			parent.my_score = parent.my_score + added_score;
		}
		else {
			parent.score_a[applet_id] = a_rounded_score;
			parent.my_score = parent.my_score + a_rounded_score;
		}
	}

	parent.old_score_a[applet_id] = a_rounded_score;
	parent.main.stayTopLeft()
	parent.main.document.getElementById("floatingscore").style.visibility = "visible";
	parent.main.document.getElementById("my_score_span").innerHTML = "Score:<br><b>" + (Math.round(parent.my_score * 10) / 10) + ' of ' + parent.total_points;

	//SCORM data
	if (parent.scorm) {
		var q_type = 7;
		var student_answer = "n/a";
		var correct = "no";

		if (percent_value == "100") {
			correct = "yes";
		}

		//alert("parent.my_status is " + parent.my_status + "\nparent.total_points is " + parent.total_points + "\nparent.my_score is " + parent.my_score + "\napplet_id is " + applet_id + "\nq_type is " + q_type + "\nstudent_answer is " + student_answer + "\ncorrect is " + correct);
		if (parent.attempted_q == parent.totalQ) {
			if (parent.scorm_completed_status) {
				parent.my_status = "completed";
			}
		}

		//alert("attempted_q is " + parent.attempted_q + "totalQ is " + parent.totalQ + "my_status is " + parent.my_status);
		parent.sendScorm(parent.my_status, parent.total_points, parent.my_score, applet_id, q_type, act_type, student_answer, correct, percent_value);
	}

	// check if all answers are correct
	if ((percent_value == "100") || (parent.score_a[applet_id] >= applet_value)) {
		parent.q_skip_a[applet_id] = true;
		alert("You have reached the maximum score for this activity.");
	}
}
