/////////////////////////////////////////////////////////////
//                                                         //
//  This code was written and is owned by Steven Hoffman.  //
//   You are not permitted to copy or redistribute this    //
//          code without my written permission.            //
//                                                         //
//                 http://www.fustrate.com                 //
//                                                         //
/////////////////////////////////////////////////////////////

if(typeof(document.getElementById) == "undefined")
	document.getElementById = function (id)
	{
		return document.all[id];
	}

function update_score(add, remove)
{
	var newscore = parseInt(document.getElementById("total-points-1").innerHTML) + add - remove;
	document.getElementById("total-points-1").innerHTML = newscore;
	document.getElementById("total-points-2").innerHTML = newscore;
	document.getElementById("score").value = newscore
}

// Simple Challenge - One or more parts, each separate
function simple_complete(element, id)
{
	var others = document.getElementById(id + "-options").getElementsByTagName("span");
	var points = parseInt(element.title);
	var count = others.length;

	// Set the others to "off", and find out how many points we're taking away
	for (var i = 0; i < count; i++)
		if (others[i].className == "on")
		{
			remove = parseInt(others[i].title);
			others[i].className = "off";
		}

	element.className = "on";
	document.getElementById(id + "-points").innerHTML = parseInt(document.getElementById(id + "-points").innerHTML) + points - remove;
	document.getElementById(id + "-options-result").value = points;
	update_score(points, remove);

	return true;
}

// Complex Challenge - Multiple items, only a certain amount total
function complex_complete(element, parent, parts, allowed, id, notouch)
{
	var others = document.getElementById(parent).getElementsByTagName("span");
	var points = parseInt(element.title);
	var count = others.length;

	// Set the others to "off", and find out how many points we're taking away
	for (var i = 0; i < count; i++)
	{
		if (others[i].className == "on")
		{
			remove = parseInt(others[i].title);
			others[i].className = "off";
		}

		if (element == others[i])
			var which = i;
	}

	element.className = "on";
	document.getElementById(id + "-points").innerHTML = parseInt(document.getElementById(id + "-points").innerHTML) + points - remove;
	document.getElementById(parent).getElementsByTagName("input")[0].value = parseInt(points);
	update_score(points, remove);

	// Here's where it gets sticky. If there are more items activated than allowed, we have to remove
	// some from another part to make space for the (hopefully) correct value being sent right now.

	if (parts > 1)
		for (var i = 0; i < parts; i++)
			if (i != notouch)
			{
				sum = 0;

				// First, add them all up... no need to do anything if we're under max
				for (var x = 0; x < parts; x++)
				{
					var others2 = document.getElementById(id + "-options-" + x).getElementsByTagName("span");

					for (var j = 0; j < others2.length; j++)
						if (others2[j].className == "on")
							sum += j;
				}

				if (sum > allowed)
				{
					var over = sum - allowed;
					var others3 = document.getElementById(id + "-options-" + i).getElementsByTagName("span");
					var count = others3.length;

					// Don't even check the first one, since it creates an infinite loop
					for (var a = 1; a < count; a++)
						if (others3[a].className == "on")
						{
							lefthere = a - over;

							if (lefthere >= 0)
								complex_complete(others3[lefthere], id + "-options-" + i, parts, allowed, id, notouch);
							else
								complex_complete(others3[0], id + "-options-" + i, parts, allowed, id, notouch);
						}
				}
			}

	return true;
}

// Penalty or Bonus - Only for missions that affect the score.
function pb_complete(element, id)
{
	var others = document.getElementById(id + "-options").getElementsByTagName("span");
	var points = parseInt(element.title);
	var count = others.length;

	// Set the others to "off", and find out how many points we're taking away
	for (var i = 0; i < count; i++)
		if (others[i].className == "on")
		{
			remove = parseInt(others[i].title);
			others[i].className = "off";
		}

	element.className = "on";
	document.getElementById(id + "-options-result").value = points;

	document.getElementById(id + "-points").innerHTML = parseInt(document.getElementById(id + "-points").innerHTML) + points - remove;
	update_score(points, remove);

	return true;
}

// Preset a simple item
function simple_preset(points, id)
{
	var others = document.getElementById(id + "-options").getElementsByTagName("span");
	var count = others.length;

	// Come out, come out, wherever you are...
	for (var i = 0; i < count; i++)
		if (points == parseInt(others[i].title))
			simple_complete(others[i], id);
}

// Preset a complex item
function complex_preset(points, parent, parts, allowed, id, notouch)
{
	var others = document.getElementById(parent).getElementsByTagName("span");
	var count = others.length;

	// Find it and do it, or I'll cry.
	for (var i = 0; i < count; i++)
		if (points == parseInt(others[i].title))
			complex_complete(others[i], parent, parts, allowed, id, notouch);
}

// Preset a penalty or bonus
function pb_preset(points, id)
{
	var others = document.getElementById(id + "-options").getElementsByTagName("span");
	var count = others.length;

	// Loop through them until we /* Yo //, I'm happy for you, I'll let you finish, but # is one of the best comments of all time! OF ALL TIME! */
	for (var i = 0; i < count; i++)
		if (points == parseInt(others[i].title))
			pb_complete(others[i], id);
}