

var currentAnswerPickers = new Array();

function addAnswerPicker(eleobj)
{
	currentAnswerPickers[currentAnswerPickers.length] = eleobj;
}

function hideAllAnswerPickers(evt)
{
	var elem = getEventElement((evt) ? evt : ((window.event) ? event : null));
	if(typeof(elem.id) == "undefined" || elem.id.indexOf("CATEGORY") < 0)
	{
		clearAllAnswerPickers();
	}
}

function clearAllAnswerPickers()
{
	for(var i=0; i<currentAnswerPickers.length; i++)
	{
		currentAnswerPickers[i].style.display = "none";
	}
	currentAnswerPickers = new Array();
}

function setupForm(formobj)
{
	var inputobj = null;
	for(var i=0; i<formobj.elements.length; i++)
	{
		inputobj = formobj.elements[i];
		if(typeof(inputobj.type) != "undefined" && inputobj.type == "text")
		{
			inputobj.onkeyup = showAnswerPicker;			
		}
	}
}

function showAnswerPicker(evt)
{
	var elem = getEventElement((evt) ? evt : ((window.event) ? event : null));
	if (elem)
	{
		processPastAnswers(elem);
	}  
	return(true);
}

var answerinputprefix = "search";

function processPastAnswers(inputobj)
{
	var inputname = inputobj.name;
	var inputvalue = inputobj.value;
//alert(inputvalue);

	var questionid = "SEARCH";
	var pastanswersid = "CATEGORY-" + questionid;
	var pastanswersdiv = document.getElementById(pastanswersid);
	
	var pastanswer = pastanswersdiv.firstChild;
	
	var displaycount = 0;
	
	while(pastanswer && displaycount<10)
	{
		if(checkPastAnswer(pastanswer, inputvalue))
		{
			displaycount++;
		}
		pastanswer = pastanswer.nextSibling;
	}
	if(inputvalue.length!=0 && displaycount!=0)
	{

		var left = getPositionLeft(inputobj);
		var top = getPositionTop(inputobj);
		var iLeftPos = left;
		var iTopPos = top + 20;	
		pastanswersdiv.style.left = (iLeftPos) + "px";
		pastanswersdiv.style.top = (iTopPos) + "px";	
		copyElementSize(inputobj, pastanswersdiv);
		pastanswersdiv.style.height = "";
		addAnswerPicker(pastanswersdiv);
		pastanswersdiv.style.display = "block";
		
	}
	else
	{
		pastanswersdiv.style.display = "none";
	}
}

/* This snippet and many others are available free online at
The JavaScript Source!! http://javascript.internet.com
Created by: Ultimater :: http://ultimiacian.tripod.com/ */

// To find the left position, add this snippet to your code:
function getPositionLeft(el) {
  var pL = 0;
  while(el) {
    pL+=el.offsetLeft;
    el=el.offsetParent;
  }
  return pL;
}

// To find the left position, add this snippet to your code:
function getPositionTop(el) {
  var pT = 0;
  while(el){
    pT+=el.offsetTop;
    el=el.offsetParent;
  }
  return pT;
}

function checkPastAnswer(eleobj, answer)
{
	if(typeof(eleobj.tagName) != "undefined")
	{
		var pastvalue = eleobj.innerHTML;
		if(answer.length!=0 && pastvalue.toLowerCase().indexOf(answer.toLowerCase()) >= 0)
		{
			eleobj.style.display = "block";
			return(true);
		}
		else
		{
			eleobj.style.display = "none";
		}
	}
	return(false);
}

function selectPastAnswer(pastanswerobj)
{
	var inputobj = document.getElementsByName(answerinputprefix);
	inputobj[0].value = pastanswerobj.innerHTML;
	clearAllAnswerPickers();
	document.getElementById("-searchaction").value ="categorysearch";
	document.getElementById("-categoryId").value = pastanswerobj.innerHTML;
	document.getElementById("searchform").submit();
}

var ltgray = "#EEEEEE";
var white = "#FFFFFF";

function rowBG(objid, colour)
{
	if(objid.style)
	{
		objid.style.backgroundColor = colour;
	}
}

addDocumentEventListener('mouseup', hideAllAnswerPickers);

