/* This is for a irregular quiz where every option has a different pool of choices */

insertStyleSheet("/lls/assets/styles/quiz.css");
var i=0,p,q,j;
var a="";
var correct=new Image(25,25);
correct.src="assets/images/correct.gif";
var incorrect=new Image(25,25);
incorrect.src="assets/images/incorrect.gif";

function evaluate() {
	for (i=0;i<answers.length;i++) {
		o=document.getElementById("select-" + (i+1));
		if (o.selectedIndex == 0) {
			alert("You must choose an option for each item.");
			return false;
		}
	}
	var o;
	for (i=0;i<answers.length;i++) {
		o=document.getElementById("select-" + (i+1));
		if (answers[i] == o.options[o.selectedIndex].value) document.getElementById("fb-img-"+(i+1)).src=correct.src;
		else document.getElementById("fb-img-"+(i+1)).src=incorrect.src;
	}
}

function showAnswers() {
	var i,j;
	for (i=0;i<answers.length;i++) {
		o=document.getElementById("select-" + (i+1));
		for (j=0;j<o.options.length;j++) if (o.options[j].value == answers[i]) o.selectedIndex=j;
		document.getElementById("fb-img-"+(i+1)).src=correct.src;
	}
	if (p=document.getElementById("comment")) p.style.display="block";
	//evaluate();
	
}
function clearAnswers() {
	var i,j;
	for (i=0;i<answers.length;i++) {
		o=document.getElementById("select-" + (i+1));
		for (j=0;j<o.options.length;j++) o.selectedIndex=0;
		document.getElementById("fb-img-"+(i+1)).src="assets/images/spacer.gif";
	}
	
}
var answers = document.getElementById("answers").firstChild.nodeValue.split(",");
var options;

i=0;
while (o=document.getElementById("option-" + (++i))) {
	p=document.createElement("select");
	p.setAttribute("id","select-" + i);
	q = document.createElement("option");
	q.appendChild(document.createTextNode("Select one"));
	p.appendChild(q);
	options = document.getElementById("opt-"+i+"-choices").firstChild.nodeValue.split(",");
	for (j=0;j<options.length;j++) {
		q = document.createElement("option");
		q.setAttribute("value",options[j]);
		q.appendChild(document.createTextNode(options[j]));
		p.appendChild(q);
	}
	o.appendChild(p);
	p=document.createElement("img");
	p.setAttribute("src","assets/images/spacer.gif");
	p.setAttribute("id","fb-img-"+i);
	p.className="fb-img";
	p=o.appendChild(p);
	p.height=25;
	p.width=25;
	o.style.whiteSpace="nowrap";
}
o=document.getElementById("interaction").nextSibling;
o=o.parentNode.insertBefore(document.createElement("div"),o);
o.className="buttons-div";
p=document.createElement("input");
p.setAttribute("type","button");
p.setAttribute("value","Check answers");
p.onclick=evaluate;
o.appendChild(p);
o.appendChild(document.createElement("br"));
p=document.createElement("input");
p.setAttribute("type","button");
p.setAttribute("value","Show answers");
p.onclick=showAnswers;
o.appendChild(p);
p=document.createElement("input");
p.setAttribute("type","button");
p.setAttribute("value","Clear answers");
p.onclick=clearAnswers;
o.appendChild(p);
