insertStyleSheet("/lls/llonline/assets/styles/showlinks.css");
var curEvt = new universalEvent();

var classInsts = new Array();
var classCounter = 0;

var radioquiz2 = function (butsDiv) {
	this.ident = butsDiv.substr(butsDiv.indexOf("-")+1,1);
	this.ans = document.getElementById("rq2-"+this.ident+"-ans").firstChild.nodeValue.split(",");
	classInsts['rq2-'+(++classCounter)] = this;
	this.bdiv=document.getElementById(butsDiv);
	this.t=document.createElement("input");
	this.t.setAttribute("type","button");
	this.t.setAttribute("value","Check answers");
	this.t.id='ca-rq2-'+classCounter;
	this.t.onclick=function () { findClass(arguments[0]).checkAns(); };
	this.bdiv.appendChild(this.t);
	this.t=document.createElement("input");
	this.t.setAttribute("type","button");
	this.t.setAttribute("value","Show answers");
	this.t.id='sa-rq2-'+classCounter;
	this.t.onclick=function () { findClass(arguments[0]).showAns(); };
	this.bdiv.appendChild(this.t);
	// Insert Reset Button 
	this.t=document.createElement("input");
	this.t.setAttribute("type","button");
	this.t.setAttribute("value","Clear answers");
	this.t.id='clra-rq2-'+classCounter;
	this.t.onclick=function () { findClass(arguments[0]).clearAns(); };
	this.bdiv.appendChild(this.t);
	
	this.qs=new Array();
	var i=1,j,d,ta,r;
	while (++i <= arguments.length) {
		j=0;
		ta=new Array();
		while (d=document.getElementById(arguments[i-1]+"-"+(++j))) {
			r=(isIE) ? document.createElement("<input name='"+arguments[i-1]+"'>"):document.createElement("input");
			r.type="radio";
			r.name=arguments[i-1];
			r.id = d.id+"r";
			ta.push(d.insertBefore(r,d.firstChild));
			
			/** **/
			//alert (i-1 + "and " + j);
			var k = i-1;
			//alert("comment-" + k + "-" + j);
			if (d=document.getElementById("answers-" + k +"-" + j)) 
			{
					d.style.display="none";
					if (!d.className) d.className="essay-comment";
					d.insertBefore(newCloseGraphic(),d.firstChild);
			}
		}
		this.qs.push(ta);
	}
}

radioquiz2.prototype.checkAns = function () {
	var i,j,optionSelected=0;
	this.clearTicks();
	for (i=0;i<this.qs.length;i++) {
		for (j=0;j<this.qs[i].length;j++) {
			if (this.qs[i][j].checked) 
			{
				optionSelected++; // denotes that the option is selected
				this.qs[i][j].parentNode.appendChild(newFeedback((this.ans[i] == (j+1))?true:false));
				// new piece
				//alert (i+"and"+j);
				var a=i+1;b=j+1;
				//alert("comment-"+a+"-"+b);
				if (cmt=document.getElementById("answers-"+a+"-"+b))
				{
					cmt.style.position ="relative";
					//alert(this.qs[i][j].parentNode);
					sp=document.getElementById("rq2-1-"+a+"-"+b);
					p=findTopInlineNode(sp);
					//alert (sp.parentNode.id);
					insertAfter(cmt,sp.parentNode);
					cmt.style.display="block";
				}
			}
			
		}
	}
	var e;
	if (e=document.getElementById("answer-"+this.ident))
	{
		if (optionSelected>0)
			e.style.display="block";
		else
			alert ("Please select an option");
	}
}

radioquiz2.prototype.showAns = function () {
	for (i=0;i<this.qs.length;i++) {
		this.qs[i][(this.ans[i]-1)].click();
	}
	this.checkAns();		
}

radioquiz2.prototype.clearAns = function () {
	for (i=0;i<this.qs.length;i++) {
		for (j=0;j<this.qs[i].length;j++) {
			this.qs[i][j].checked=false;
		}
	}
	this.clearTicks();
	var e;
	if (e=document.getElementById("answer-"+this.ident))
		e.style.display="none";
}

radioquiz2.prototype.clearTicks = function () {
	var i,j,imgs;
	for (i=0;i<this.qs.length;i++) {
		for (j=0;j<this.qs[i].length;j++) {
			while ((imgs=this.qs[i][j].parentNode.getElementsByTagName("img")).length > 0) imgs[0].parentNode.removeChild(imgs[0]);
			
			var a=i+1;b=j+1;
			//alert("comment-"+a+"-"+b);
			if (cmt=document.getElementById("answers-"+a+"-"+b))
			{
				//cmt.style.position ="absolute";
				cmt.style.display="none";
			}
		}
	}
}

var newFeedback = function(which) {
	var img = document.createElement("img");
	img.height=25;
	img.width=25;
	img.src = (which) ? correct.src:incorrect.src;
	return img;
}

var evtrq2 = new universalEvent;
var findClass = function(e) {
	evtrq2.refresh(e);
	return classInsts['rq2-' + evtrq2.evtSrcId.substr(evtrq2.evtSrcId.length-1)];
}

// new code begins here

function newCloseGraphic() {
	var im = document.createElement("img");
	im.setAttribute("src","assets/images/close.gif");
	im.setAttribute("width","14");
	im.setAttribute("height","14");
	im.setAttribute("alt","Close box");
	im.onclick=closeDiv;
	im.className = "close-but";
	return im;
}
function closeDiv(e) {
	curEvt.refresh(e);
	var p;
	(p=curEvt.evtSrc.parentNode).style.display="";
	//highlightRef(parseInt(p.id.substr(p.id.lastIndexOf("-")+1)),false);
}


/*
i=0;j=0;
while ((d=document.getElementById("answers-" + i +"-" + ++j)) != null) 
{
		//alert(d.innerHTML);
		d.style.display="none";
		d.insertBefore(newCloseGraphic(),d.firstChild);
}
*/