if (typeof(lls) == "undefined") var lls = new Object();
if (typeof(lls.dhtml) == "undefined") lls.dhtml = new Object();
if (typeof(evt) == "undefined") evt = new universalEvent();


lls.dhtml.txtInput = function (oid,obj) {
	this.id=oid;
	this.ref=obj;
	this.ident=parseInt(this.id.substr(this.id.lastIndexOf("-")+1));
	//alert (this.ident);
	if (!(this.ansRef=document.getElementById("int-txtInput-ans-" + this.ident))) {
		alert("LLS dhtml: Error finding answer <div> with id 'int-txtInput-ans-" + this.ident + "'");
	}
	else {
		this.ansRef.style.display="none";
		this.ansText = trimCrap(this.ansRef.firstChild.nodeValue);
	}
	
	if (txtBox = document.getElementById("int-txtInput-"+this.ident))
	{	
		if (def = document.getElementById("int-txtInput-default-"+this.ident))
		{
			def.style.display = "none";
			txtBox.value = def.innerHTML;
		}
		
	}
	
	this.feedbackTick = document.createElement("img");
	this.feedbackTick = insertAfter(this.feedbackTick,this.ref);
	//this.feedbackTick.height=25;
	//this.feedbackTick.width=25;
	this.feedbackTick.src="assets/images/spacer.gif";
	
	//alert ("butt-"+this.ident);
	if (buttPlace = document.getElementById("butt-"+this.ident))
	{
		//alert (this.ident);
		var tempRef = buttPlace;
		//alert (tempRef.id);
		//alert ('in');
	}
	else
	{
		if (!(restore=document.getElementById("restoreValues"))) //silly code to fix unanonymous err in mozilla
		{
			var tempRef = insertAfter(document.createElement("br"),this.feedbackTick);
		}
		else
		{
			var tempRef = document.getElementById("li"+this.ident);
		}
		//tempRef 	= insertAfter(document.createElement("br"),tempRef);
	}
	
	//alert (tempRef.id);
	if (!(tempRef.id))
	{
		if (txtBox.parentNode)
		{
			//alert ("the parent is "+txtBox.parentNode.id);
			var tempRef = txtBox.parentNode;
			tempRef 	= insertAfter(document.createElement("br"),tempRef);
			
		}
	}
	this.resetBut = isIE?document.createElement("<input onclick=\"findIIById('"+this.id+"').evaluate()\"'>"):document.createElement("input");
	this.resetBut.setAttribute("type","button");
	this.resetBut.setAttribute("value","Check answer");
	tempRef = insertAfter(this.resetBut,tempRef);
	if (isGecko) this.resetBut.setAttribute("onclick","findIIById('"+this.id+"').evaluate()");
	this.ansBut = isIE?document.createElement("<input onclick=\"findIIById('"+this.id+"').answer()\"'>"):document.createElement("input");
	this.ansBut.setAttribute("type","button");
	this.ansBut.setAttribute("value","Show answer");
	tempRef2	=	insertAfter(this.ansBut,tempRef);
	if (isGecko) this.ansBut.setAttribute("onclick","findIIById('"+this.id+"').answer()");
	
	// insert clear answers button
	this.clrBut = isIE?document.createElement("<input onclick=\"findIIById('"+this.id+"').clearanswer()\"'>"):document.createElement("input");
	this.clrBut.setAttribute("type","button");
	this.clrBut.setAttribute("value","Clear answer");
	tempRef3	=	insertAfter(this.clrBut,tempRef2);
	if (isGecko) this.clrBut.setAttribute("onclick","findIIById('"+this.id+"').clearanswer()");
	
	tempRef4 = insertAfter(document.createElement("br"),tempRef3);
	tempRef5 = insertAfter(document.createElement("br"),tempRef4);
}

lls.dhtml.txtInput.prototype.evaluate = function () {
	if (trimCrap(this.ref.value)=='') //empty
	{
		alert ("Please enter the answer");
		return false;
	}
	
	this.feedbackTick.src = (trimCrap(this.ref.value) == this.ansText) ?correct.src:incorrect.src ;
}

lls.dhtml.txtInput.prototype.answer = function () {
	this.ref.value = this.ansRef.firstChild.nodeValue;
	this.feedbackTick.src = correct.src;
}

lls.dhtml.txtInput.prototype.clearanswer = function ()
{
	if (restore = document.getElementById("restoreValues"))
	{
		this.ident=parseInt(this.id.substr(this.id.lastIndexOf("-")+1));
		//alert (this.ident);
		if (txtBox = document.getElementById("int-txtInput-"+this.ident))
		{
			
			if (def = document.getElementById("int-txtInput-default-"+this.ident))
			{
				txtBox.value = def.innerHTML; 
			}
		}
	}
	else
	{
		this.ref.value = "";
	}
	this.feedbackTick.src = "assets/images/spacer.gif";
}

function trimCrap(s) {
	 return s.replace(/(?:^[ \t]+|[ \t]+$|[\.]+$|[,]+$|^[,+\t+])+/g,"");
}

function findIIById(oid) {
	var i;
	for(i=0;i<intInputs.length;i++) {
		if (intInputs[i].id == oid) return intInputs[i];
	}
	return null;
}

var intInputs = new Array();
function init() {
	var i=0;
	while (d=document.getElementById("int-txtInput-"+(++i))) {
		//alert ('i val is '+i);
		intInputs.push(new textInput("int-txtInput-"+i,d));
	}
	
	if (restore=document.getElementById("restoreValues"))
	{
		restore.style.display = "none";
	}
}

var textInput = lls.dhtml.txtInput;
init();