var evt3 = new universalEvent();

var checkSpan = function (e) {
	evt3.refresh(e);
	var el = evt3.evtSrc;
	if (el.id.indexOf("txtClick3") == -1 && el.id.indexOf("sp-") == -1) return false;
	if (el.className == "highlightactive") return false;
	el.className="highlightactive";
	if (el.id.indexOf("txtClick3") > -1) el.appendChild(newFeedback(true));
	else el.appendChild(newFeedback(false));
}

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 showAns = function() {
	var p=document.getElementById("txtClick3");
	var sp = p.getElementsByTagName("span");
	for (var i=0;i<sp.length;i++) {
		if (sp[i].className == "highlightactive") {
			sp[i].className="";
			sp[i].removeChild(sp[i].getElementsByTagName("img")[0]);
		}
		if (sp[i].id.indexOf("txtClick3") > -1) {
			sp[i].appendChild(newFeedback(true));
			sp[i].className="highlightactive";
		}
	}
}

var resetPara = function () {
	var p=document.getElementById("txtClick3");
	var sp = p.getElementsByTagName("span");
	for (var i=0;i<sp.length;i++) {
		if (sp[i].className == "highlightactive") {
			sp[i].className="";
			sp[i].removeChild(sp[i].getElementsByTagName("img")[0]);
		}
	}
}

var init = function () {
	var p=document.getElementById("txtClick3");
	var words,i,j,e,sp;
	
	p=p.childNodes;
	
	for (i=0;i<p.length;i++) {
		if (p[i].nodeName.toLowerCase() == "span" && p[i].id.indexOf("txtClick3") == -1) {
			words=p[i].firstChild.nodeValue.split(" ");
			p[i].removeChild(p[i].firstChild);
			for(j=0;j<words.length;j++) {
				e=document.createElement("span");
				e.appendChild(document.createTextNode(words[j]));
				e.id="sp-"+i+j;
				p[i].appendChild(e);
				if (j < (words.length-1)) p[i].appendChild(document.createTextNode(" "));
			}
		}
	}
	p=p.parentNode;
	sp= document.getElementsByTagName("span");
	for (i=0;i<sp.length;i++) {
		sp[i].style.cursor="pointer";
		sp[i].onclick=checkSpan;
	}
	
	//insert buttons
	var bdiv=document.getElementById("txtClick3-buts"),t;
	t=document.createElement("input");
	t.setAttribute("type","button");
	t.setAttribute("value","Clear answers");
	t.onclick=resetPara;
	bdiv.appendChild(t);
	t=document.createElement("input");
	t.setAttribute("type","button");
	t.setAttribute("value","Show answers");
	t.onclick=showAns;
	bdiv.appendChild(t);
}

runOnLoad("init();");
