insertStyleSheet("/lls/llonline/assets/styles/grammar-words.css");
var curEvt = new universalEvent();
var p,q,e,answersShown=false,answers=new Array();

function evaluateQ(e) {
	curEvt.refresh(e);
	if (curEvt.key == 13 && curEvt.evtSrc.type == "text") return false;
	var n,a,v,i,rd;
	n = parseInt(curEvt.evtSrcId.substr(curEvt.evtSrcId.length-1));
	v=document.getElementById("response-" + n).value;
	/**
	if (!(v))
		v=document.getElementById("word-" + n).value;
	**/	
	rd=document.getElementById("comment-div-" + n);
	a=answers[n-1];
	var re = new RegExp(v,"i");
	if (a.match(re) == a) {
		p = document.createElement("p");
		removeChildElements(rd);
		p.appendChild(document.createTextNode("Correct!"));
		rd.appendChild(p);
	}
	else {
		if (c=checkWords(v,a)) c="Words you have entered which appear in the answer: " + c;
		else c="";
		if (d=checkPartial(v,a)) (d="This much of your answer is correct: " + d);
		else d="";
		removeChildElements(rd);
		p = document.createElement("p");
		p.appendChild(document.createTextNode("Incorrect!"));
		rd.appendChild(p);
		if (c != "") {
			p=document.createElement("p");
			p.appendChild(document.createTextNode(c));
			rd.appendChild(p);
		}
		if (d != "") {
			p=document.createElement("p");
			p.appendChild(document.createTextNode(d));
			rd.appendChild(p);
		}
	}
	rd.className="response-div";
	rd.style.display="block";

//clear the appendWord[show ans] div
if (appendWord =  document.getElementById("appendWord-"+n))
	appendWord.style.display="none";
}

function showAnswer(e) {
	curEvt.refresh(e);
	n = parseInt(curEvt.evtSrcId.substr(curEvt.evtSrcId.length-1));
	rd=document.getElementById("comment-div-" + n);
	a=answers[n-1];
	
	if (appendWord = document.getElementById("appendWord-"+n))
	{
		//alert ("in");
		/**
		p = document.createElement("div");
		p.setAttribute("id","fullComment-"+n);
		//alert (p.innerHTML);
		
		if (isIE)
			p.innerHTML = "Correct answer: " + a+appendWord.innerHTML;
		else
			p.innerText = "Correct answer: " + a+appendWord.innerText;
		//alert (appendWord.innerHTML)
		p.className = "response";
		rd.appendChild(p);
		**/
		//fullComment = document.getElementById("fullcomment-"+n);
		//alert (appendWord.innerHTML);
		//rd.innerHTML = "Correct answer: " + a+appendWord.innerHTML;
		//rd.innerHTML = "Test";
		/**
		p = document.createElement("p");
		bol = document.createElement("strong");
		bol.appendChild(document.createTextNode("Correct answer: " + a));
		p.appendChild(bol);
		p.appendChild(document.createElement('br'));
		p.appendChild(document.createTextNode(appendWord.innerHTML));
		**/
		//var duplicateAppend = appendWord.cloneNode(true);
		//alert (duplicateAppend.innerHTML);
		//rd.appendChild(duplicateAppend);
		
		appendWord.className = "response-div";
		appendWord.style.display="block";
		//display the correct answer in the textbox
		v=document.getElementById("response-" + n);
		if (v)
			v.value = answers[n-1];
			
		//clear the comment div[check-answer]	
		if (rd=document.getElementById("comment-div-" + n))
			rd.style.display="none";
	}
	else if (rd)
	{
		p = document.createElement("p");
		p.appendChild(document.createTextNode("Correct answer: " + a));
		if (rd)
		{
			removeChildElements(rd);
			rd.appendChild(p);
			rd.className = "response-div";
			rd.style.display="block";
		}
	}
	
	//rd.className="response-div";
	
}
function clearAnswer(e) {
	curEvt.refresh(e);
	n = parseInt(curEvt.evtSrcId.substr(curEvt.evtSrcId.length-1));
	rd=document.getElementById("comment-div-" + n);
	a=answers[n-1];
	if (rd)
	{
		removeChildElements(rd);
		// To Do : have to check at this point 
	//	p = document.createElement("p");
	//	p.appendChild(document.createTextNode("Correct answer: " + a));
	//	rd.appendChild(p);
		rd.className="";
		// clears the answer textblock
		rd.style.display="none";
	}
	//	clears the typed answer in the field textbox
	rt=document.getElementById("response-" + n);	
	if (rt)
		rt.value='';
		
	if (appendWord = document.getElementById("appendWord-"+n))
	{
		appendWord.className="";
		// clears the answer textblock
		appendWord.style.display="none";
	}
}
function showAnswerContext() {
	var txt,a,n,m,b;
	txt = document.getElementById("sample-text");
	if (txt.scrollIntoView) txt.scrollIntoView(true);
	if (answersShown) return false;
	var n=0;var p=1;
	
	while (a=document.getElementById("word-" + (++n))) {
		
		if (d=document.getElementById("alt-word-" + (n))) //[refer page: grammar/passive/4.xml]
		{
			//alert ("in");
			d.className="answer-shown";
			d.style.display="inline";
			a.style.display="none";
		}
		else //[refer page: grammar/passive/2.xml]
		{
			a.className="answer-shown";
		}
		m=1;
		while (b=document.getElementById("word-" + (n) + "." + (m++))) {
			b.className="answer-shown";
		}
	}
	answersShown = true;
	if (p=document.getElementById("comment")) p.style.display="block";
}
function clearAnswerContext() {
	var txt,a,n,m,b;
	txt = document.getElementById("sample-text");
	if (txt.scrollIntoView) txt.scrollIntoView(true);
	n=1;
	while (a=document.getElementById("word-" + (n++))) {
		if (d=document.getElementById("alt-word-" + (n-1))) {
			d.className="";
			d.style.display="none";
			a.style.display="inline";
		}
		else 
		{
			a.className="";
		}


		m=1;
		while (b=document.getElementById("word-" + (n-1) + "." + (m++))) {
			b.className="";
		}
	}
	answersShown = false;
	if (p=document.getElementById("comment")) p.style.display="block";
}
function checkWords(ent,answ) {
	answ=answ.split(" ");
	var re,reTx="",i=0,ret="";
	for (i=0;i<answ.length;i++) {
		re=new RegExp(answ[i],"i");
		if (re.test(ent)) {
			if (ret != "") ret += ", ";
			ret += answ[i];
		}
	}
	if (ret == "") return false;
	else return ret;
}

function checkPartial(ent,answ) {
	ent=ent.toLowerCase();
	ans=answ.toLowerCase();
	var lng,ansD,entD;
	if (ent.length > ans.length) {
		lng=ent.length;
		entD=0;
		ansD=lng-ans.length;
	}
	else {
		lng=ans.length;
		ansD=0;
		entD=lng-ent.length;
	}
	
	i=0,j=lng-1;
	while (i < lng && (ans.charAt(i) == ent.charAt(i))) i++;
	while ((j-ansD+entD) > 0 && (ans.charAt(j-ansD)==ent.charAt(j-entD))) j--;
	if (j < lng && ent.charCodeAt(j-entD) == 20) j++;
	var seg1,seg2;
	seg1=(i > 0) ? ent.substr(0,(i-1)):"";
	seg2=(j < lng) ? ent.substr(j-entD+1):"";
	if (i == 0 && j == (lng-1)) return false;
	else return seg1 + "..." + seg2;
}

var i=0;
while(d=document.getElementById("question-" + (++i))) {
	p=document.createElement("p");
	p.setAttribute("class","response");
	e=document.createElement("label");
	e.appendChild(document.createTextNode("Enter your answer: "));
	p.appendChild(e);
	e=document.createElement("input");
	e.setAttribute("type","text");
	e.setAttribute("size","20");
	e.setAttribute("id","response-"+i);
	//e.setAttribute("class","response");
	e.setAttribute("class","int-type-txt");
	e.className="int-type-txt";
	p.appendChild(e);
	p.appendChild(document.createElement("br"));
	p.appendChild(document.createElement("br"));
	e=document.createElement("input");
	e.setAttribute("type","button");
	e.setAttribute("class","response");
	e.setAttribute("value","Check answer");
	e.setAttribute("id","response-button-"+i);
	e.onclick=evaluateQ;
	p.appendChild(e);
	e=document.createElement("input");
	e.setAttribute("type","button");
	e.setAttribute("class","response");
	e.setAttribute("value","Show answer");
	e.setAttribute("id","answer-button-"+i);
	e.onclick=showAnswer;
	p.appendChild(e);
	e=document.createElement("input");
	e.setAttribute("type","button");
	e.setAttribute("class","response");
	e.setAttribute("value","Clear answer");
	e.setAttribute("id","clearanswer-button-"+i);
	e.onclick=clearAnswer;
	p.appendChild(e);
	
	p.appendChild(document.createElement("br"));
	d.appendChild(p);
	if (e = document.getElementById("appendWord-"+i))
	{
		//e = d.appendChild(e);
		//d.insertBefore(p,e);
		e.style.display = "none";
	}
	p.appendChild(document.createElement("br"));
	//else
	//{
	e=document.createElement("div");
	e.setAttribute("id","comment-div-"+i);
	e.setAttribute("style","display:none");
	e.setAttribute("class","response-div");
	e=d.appendChild(e);
	d.insertBefore(p,e);
	//}
}
var sample	=	document.getElementById("sample-text");
var bre		=	document.createElement("br");
if (sample)
{
	sample.appendChild(bre);
	sample.appendChild(bre);
}
e=document.createElement("input");
e.setAttribute("type","button");
e.setAttribute("class","response");
e.setAttribute("value","Show answers in context");
e.setAttribute("id","context-button");
e.onclick=showAnswerContext;
qns = document.getElementById("questions");
if (sample)
{
	sample.appendChild(e);
}
else if (qns)
	qns.appendChild(e);

// add the Reset / Clear Answer Buttons 
e=document.createElement("input");
e.setAttribute("type","button");
e.setAttribute("class","response");
e.setAttribute("value","Clear answers in context");
e.setAttribute("id","context-button");
e.onclick=clearAnswerContext;
qns = document.getElementById("questions");
if (sample)
	sample.appendChild(e);
else if (qns)
	qns.appendChild(e);


i=0;
while (e=document.getElementById("word-" + (++i))) {
	//e.style.display = "none";
	if (d=document.getElementById("alt-word-"+i)) answers[i-1] = d.firstChild.nodeValue;
	else answers[i-1] = e.firstChild.nodeValue;
	n=0;
	while (f=document.getElementById("word-" + i + "." + (++n) )) {
		answers[i-1] += " " + f.firstChild.nodeValue;
	}
}

function catchSubmit(e) {
	if (isIE) {
		event.returnValue=false;
	}
	else {
		e.preventDefault();
	}
}

window.onsubmit=catchSubmit;