/**
 *  ==================================================
 *  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 newWin(myFile, myID, winSpecs) {
	app_id = myID.substring(3, myID.length);
	if (onlyOne && GetCookie('done_a_' + app_id + rand_no) == "true") {
		alert("This activity has already been completed; only one access is allowed.");
	}
	else {
		activityWin = window.open(myFile, myID, winSpecs);
	}
}

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("opener.a_value" + applet_id);

	// bail if the activity is not assigned any points
	// or it has not reached 100%
	if (applet_value == 0 || opener.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 (!opener.q_done_a[applet_id]) {
		opener.attempted_q++;
		opener.attempted_points = opener.attempted_points + applet_value;
		opener.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 == opener.old_score_a[applet_id] && cookit.arguments[3] == 1) {
		opener.old_score_a[applet_id] = opener.old_score_a[applet_id] + a_rounded_score;
	}

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

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

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

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

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

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

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