var g_strImageDir = "images";
var g_strCounterDir = "images/counter";
var g_strQuestionDir = "images/questions";


function GetElement(id)
{
	return	document.all? document.all.item(id):
			document.getElementById? document.getElementById(id):
			document.layers? document.layers[id]: null;
}

function GetMovie(name)
{
	if(navigator.appName.indexOf("Microsoft") != -1)
		return window[name];
	else
	{
		var obj = GetElement(name);
		if(obj)
		{
			if(obj.tagName.match(/^embed$/i))
				return obj;

			for(var i=0; i<obj.childNodes.length; i++)
			{
				if(obj.childNodes[i].tagName && obj.childNodes[i].tagName.match(/^embed$/i))
					return obj.childNodes[i];
			}
		}

		return document[name];
	}
}

function ShowResult(args)
{
	var result = eval("(" + args + ")");

	var obj;

	if(obj = GetElement("result_recommendation" + result.result_class))
		obj.style.display = "block";

	if(obj = GetElement("exam_results"))
		obj.style.display = "block";

	var table, tbody, row, column, content;

	if(obj = GetElement("result_right_count"))
	{
		obj.innerHTML = "";
		content = document.createElement("img");
		content.src = g_strCounterDir + "/" + result.right_count + ".gif";
		obj.appendChild(content);
	}

	if(obj = GetElement("result_wrong_count"))
	{
		obj.innerHTML = "";
		content = document.createElement("img");
		content.src = g_strCounterDir + "/" + (10 - result.right_count) + ".gif";
		obj.appendChild(content);
	}

	// Construct result detail list
	if(obj = GetElement("result_detail"))
	{
		obj.innerHTML = "";

		for(var i=0; i<result.answers.length; i++)
		{
			table = document.createElement("table");
			table.className = "detail";
			table.cellSpacing = 0;
			obj.appendChild(table);
			tbody = document.createElement("tbody");
			table.appendChild(tbody);

			row = document.createElement("tr");
			tbody.appendChild(row);

			column = document.createElement("th");
			row.appendChild(column);
			content = document.createElement("img");
			content.src = g_strImageDir + "/q" + (i + 1) + ".gif";
			column.appendChild(content);

			column = document.createElement("td");
			column.className = "question";
			column.colSpan = 4;
			row.appendChild(column);
			content = document.createElement("img");
			content.src = g_strQuestionDir + "/" + (result.answers[i].question + 1) + ".gif";
			column.appendChild(content);

			row = document.createElement("tr");
			tbody.appendChild(row);

			column = document.createElement("th");
			row.appendChild(column);
			content = document.createElement("img");
			content.src = g_strImageDir + "/label_answer.gif";
			column.appendChild(content);

			column = document.createElement("td");
			column.className = "right_or_wrong";
			row.appendChild(column);
			content = document.createElement("img");
			content.src = g_strImageDir + "/" + (result.answers[i].right? "right": "wrong") + ".gif";
			column.appendChild(content);

			column = document.createElement("th");
			row.appendChild(column);
			content = document.createElement("img");
			content.src = g_strImageDir + "/label_right.gif";
			column.appendChild(content);

			column = document.createElement("td");
			column.className = "right_answer";
			row.appendChild(column);
			content = document.createElement("img");
			content.src = g_strImageDir + "/answer" + result.answers[i].answer + ".gif";
			column.appendChild(content);

			column = document.createElement("td");
			column.className = "hint";
			row.appendChild(column);
			content = document.createElement("a");
			content.href = result.answers[i].hint;
			content.target = "_blank";
			content.innerHTML = '<img src="images/hint.gif" alt="" />';
			column.appendChild(content);
		}
	}

	if(obj = GetElement("results_and_hints"))
		obj.style.display = "block";
}
function Restart()
{
	var obj;

	for(var i=0; i<6; i++)
	{
		if(obj = GetElement("result_recommendation" + i))
			obj.style.display = "none";
	}

	if(obj = GetElement("exam_results"))
		obj.style.display = "none";

	if(obj = GetElement("results_and_hints"))
		obj.style.display = "none";

	location.hash = "pankuzu";

	if(obj = GetMovie("cholesterol"))
	{
		obj.StopPlay();
		obj.Rewind();
		obj.Play();
	}
}

function cholesterol_DoFSCommand(command, args)
{
	if(command == "ShowResult")
	{
		ShowResult(args);
	}
}


// Hook for IE
if (navigator.appName && navigator.appName.indexOf("Microsoft") != -1 && navigator.userAgent.indexOf("Windows") != -1 && navigator.userAgent.indexOf("Windows 3.1") == -1) {
	document.write('<script language=\"VBScript\"\>\n');
	document.write('On Error Resume Next\n');
	document.write('Sub cholesterol_FSCommand(ByVal command, ByVal args)\n');
	document.write('	Call cholesterol_DoFSCommand(command, args)\n');
	document.write('End Sub\n');
	document.write('</script\>\n');
}

