function tally()
{
	var inputs = document.getElementsByTagName("input");
	var E, I, S, N, T, F, J, P;
	E = I = S = N = T = F = J = P = 0;
	var personality = '';
	var score1 = document.getElementById("score1");
	var score2 = document.getElementById("score2");
	var score3 = document.getElementById("score3");
	var score4 = document.getElementById("score4");

	// Tally the scores
	var inputLength = inputs.length;
	for (i = 0; i < inputLength; i++)
	{
		if (inputs[i].checked)
		{
			switch (inputs[i].value)
			{
				case "E":
					E++;
					break;
				case "I":
					I++;
					break;
				case "S":
					S++;
					break;
				case "N":
					N++;
					break;
				case "T":
					T++;
					break;
				case "F":
					F++;
					break;
				case "J":
					J++;
					break;
				case "P":
					P++;
					break;
			}
		}
	}

	// Set the four-letter type
	personality += (E >= I) ? "E" : "I";
	personality += (S >= N) ? "S" : "N";
	personality += (T >= F) ? "T" : "F";
	personality += (J >= P) ? "J" : "P";

	// Display scores
	document.getElementById("E").innerHTML = "(" + E + ")";
	document.getElementById("I").innerHTML = "(" + I + ")";
	document.getElementById("S").innerHTML = "(" + S + ")";
	document.getElementById("N").innerHTML = "(" + N + ")";
	document.getElementById("T").innerHTML = "(" + T + ")";
	document.getElementById("F").innerHTML = "(" + F + ")";
	document.getElementById("J").innerHTML = "(" + J + ")";
	document.getElementById("P").innerHTML = "(" + P + ")";
	document.getElementById("tally").style.display = "block";
	document.getElementById("scrolldown").style.display = "block";
	document.getElementById("personality").innerHTML = personality;
	getDescription(personality);

	// Construct graphs
	score1.style.width = (Math.abs(E - I) * 10) + "px";
	if (Math.abs(E - I) == 0)
		score1.style.width = "1px";
	if (E - I <= 0)
		score1.style.marginLeft = "100px";
	else
		score1.style.marginLeft = (100 - ((E - I) * 10)) + "px";

	score2.style.width = (Math.abs(S - N) * 5) + "px";
	if (Math.abs(S - N) == 0)
		score2.style.width = "1px";
	if (S - N <= 0)
		score2.style.marginLeft = "100px";
	else
		score2.style.marginLeft = (100 - ((S - N) * 5)) + "px";

	score3.style.width = (Math.abs(T - F) * 5) + "px";
	if (Math.abs(T - F) == 0)
		score3.style.width = "1px";
	if (T - F <= 0)
		score3.style.marginLeft = "100px";
	else
		score3.style.marginLeft = (100 - ((T - F) * 5)) + "px";

	score4.style.width = (Math.abs(J - P) * 5) + "px";
	if (Math.abs(J - P)==0)
		score4.style.width = "1px";
	if (J - P <= 0)
		score4.style.marginLeft = "100px";
	else
		score4.style.marginLeft = (100 - ((J - P) * 5)) + "px";
}

function initXML(file)
{
	try // Internet Explorer
	{
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
	}
	catch(e)
	{
		try // Other browsers
		{
			xmlDoc = document.implementation.createDocument("","",null);
		}
		catch(e)
		{
			alert(e.message);
		}
	}
	try
	{
		xmlDoc.async = false;
		xmlDoc.load(file);
		return(xmlDoc);
	}
	catch(e)
	{
		alert(e.message)
	}
	return null;
}

function getDescription(type)
{
	var xmlDoc = initXML("/personalitytypes.xml");
	var title = document.getElementById("title");
	var description = document.getElementById("description");
	var items = xmlDoc.getElementsByTagName("item");

	var itemLength = items.length
	for (i = 0; i < itemLength; i++)
	{
		if (items[i].getAttribute("label") == type)
		{
			title.innerHTML = xmlDoc.getElementsByTagName("title")[i].firstChild.nodeValue;
			description.innerHTML = xmlDoc.getElementsByTagName("description")[i].firstChild.nodeValue;
			break;
		}
	}
}
