/* This quiz file is for where you have a few set answers for each option,
as opposed to having 1 option specifically related to 1 answer. */

insertStyleSheet("/lls/assets/styles/quiz.css");
var i=0,p,q,j;
var a="";
var origOrder = new Array(),checkHomes = new Array();
var correct=new Image(25,25);
correct.src="assets/images/correct.gif";
var incorrect=new Image(25,25);
incorrect.src="assets/images/incorrect.gif";
var optionVals = document.getElementById("responses").firstChild.nodeValue.split(",");
var q2answers = document.getElementById("answers").firstChild.nodeValue.split(",");
function q2evaluate() {
	for (i=0;i<q2answers.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<q2answers.length;i++) {
		o=document.getElementById("select-" + (i+1));
		//alert("q2answers[i]-1 === " + (q2answers[i]-1) + " *** o.selectedIndex === " + o.selectedIndex);
		if (q2answers[i] == o.selectedIndex) document.getElementById("fb-img-"+(i+1)).src=correct.src;
		else document.getElementById("fb-img-"+(i+1)).src=incorrect.src;
	}
}

function q2showAnswers() {
	for (i=0;i<q2answers.length;i++) {
		o=document.getElementById("select-" + (i+1));
		
		o.selectedIndex=q2answers[i];
	}
	if (p=document.getElementById("comment")) p.style.display="block";
	q2evaluate();
}
function q2clearAnswers() {
	for (i=0;i<q2answers.length;i++) {
		o=document.getElementById("select-" + (i+1));
		o.selectedIndex=0;
		document.getElementById("fb-img-"+(i+1)).src="assets/images/spacer.gif";
	}
}


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(" "));
	p.appendChild(q);
	**/
	/** default value '' for 'Select one' **/
		q = document.createElement("option");
		q.setAttribute("value",'');
		q.appendChild(document.createTextNode("Select one"));
		p.appendChild(q);
	for (j=0;j<optionVals.length;j++) {
		q = document.createElement("option");
		q.setAttribute("value",j);
		q.appendChild(document.createTextNode(optionVals[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=q2evaluate;
o.appendChild(p);
//o.appendChild(document.createElement("br"));
p=document.createElement("input");
p.setAttribute("type","button");
p.setAttribute("value","Show answers");
p.onclick=q2showAnswers;
o.appendChild(p);
p=document.createElement("input");
p.setAttribute("type","button");
p.setAttribute("value","Clear answers");
p.onclick=q2clearAnswers;
o.appendChild(p);