	var resultsdelay = "10";
	var r;
	var t = null;
	var highlight;
	var last_lookup;
	var selected = false;
	var gprefix = null;

	function init()
	{
		//document.getElementById('TemplateMainSection_MainPackageProcessing_ScanscreenLoadTextbox').setAttribute("autocomplete", "off");
	}


	function resultsdelay()
	{
		window.setTimeout("hideresults()",resultsdelay);
	}


	function hideresults()
	{
		document.getElementById('results').style.display = "none";

		var highlight = document.getElementById('highlighted');
		if (highlight) { highlight.removeAttribute("id"); }
	}



	function showresults()
	{
		if (r.getReadyState() < 4) { t = window.setTimeout('showresults()',resultsdelay); }
		else 
		{
			var res = document.getElementById("results");
			var sh = document.getElementById("rows");
	
			// set the x,y coordinates of the results
			res.style.top = findPosY(document.getElementById(gprefix + 'ScanscreenLoadTextbox')) + 13;
			res.style.left = findPosX(document.getElementById(gprefix + 'ScanscreenLoadTextbox')) - 6;

			res.style.display="block";
			sh.innerHTML = r.getResponse();
		}
	}


	function HighlightRow(element)
	{
		element.setAttribute("id", "highlighted"); 
		element.className="highlighted"; 
	}

	function UnHighlightRow(element)
	{
		element.removeAttribute('id'); 
		element.className = "row";
	}

	function eventhandler(event)
	{
		highlight = document.getElementById("highlighted");
		
		var element;
		if (event.target) element = event.target;
		else element = event.srcElement;
		
		gprefix = element.id.replace('ScanscreenLoadTextbox', '');
		
		switch(event.keyCode)
		{
			case 40: //ARROWDOWN
				if (!highlight) { 
					// Set highlight
					highlight = document.getElementById('rows').firstChild; 

				} else { 
					// set highlight to next sibling
					UnHighlightRow(highlight);
					highlight = highlight.nextSibling;
				}

				// now that highlight is set, set it's classname
				if (highlight) { HighlightRow(highlight); }
			break;

			case 38: //ARROWUP
				if (!highlight) { 
					highlight = document.getElementById("rows").lastChild; 
				} else { 
					UnHighlightRow(highlight);
					highlight = highlight.previousSibling;
				}

				// now that highlight is set, set it's classname
				if (highlight) { HighlightRow(highlight); }
			break;

			case 27: //ESC
				if (highlight) { highlight.removeAttribute("id"); }
				hideresults();
			break;

			case 9:  //TAB
				if (!highlight) {
					highlight = document.getElementById('rows').firstChild;
					highlight.className = "highlighted";
				} 

				document.getElementById(gprefix + 'ScanscreenLoadTextbox').value = highlight.getAttribute("entityid");
				document.getElementById(gprefix + 'LookupType').value = highlight.getAttribute("type");
				selected = true;
			break;

			case 13: //RETURN
				if (highlight) {
					document.getElementById(gprefix + 'ScanscreenLoadTextbox').value = highlight.getAttribute("entityid");
					document.getElementById(gprefix + 'LookupType').value = highlight.getAttribute("type");
				}

				//document.getElementById('TemplateMainSection_MainPackageProcessing_ScanscreenLoadButton').click();
			break;
		}

		//if (event && event.preventDefault) { event.preventDefault(); }
	}

	function rowhover(row)
	{
		highlight = document.getElementById('highlighted');
		if (highlight) { UnHighlightRow(highlight); }

		HighlightRow(row);
	}



	function rowclick(row)
	{
		try{
			document.getElementById('TemplateMainSection_MainPackageProcessing_ScanscreenLoadTextbox').value = row.getAttribute("entityid");
			document.getElementById('TemplateMainSection_MainPackageProcessing_LookupType').value = row.getAttribute("type");
			document.getElementById('TemplateMainSection_MainPackageProcessing_ScanscreenLoadButton').click();
		}
		catch(err){	
		}
		try{
			document.getElementById('TemplateMainSection_MainPackageProcessingC1_ScanscreenLoadTextbox').value = row.getAttribute("entityid");
			document.getElementById('TemplateMainSection_MainPackageProcessingC1_LookupType').value = row.getAttribute("type");
			document.getElementById('TemplateMainSection_MainPackageProcessingC1_ScanscreenLoadButton').click();
		}
		catch(err){
		}

	}

	function search(lookupstring, prefix)
	{
		// make request
		if (lookupstring != last_lookup)
		{
            var res = document.getElementById("results");
			var sh = document.getElementById("rows");
	
			// set the x,y coordinates of the results
			res.style.top = findPosY(document.getElementById(prefix + 'ScanscreenLoadTextbox')) + 13;
			res.style.left = findPosX(document.getElementById(prefix + 'ScanscreenLoadTextbox')) - 6;
			res.style.display="block";
			if(prefix == 'TemplateMainSection_MainPackageProcessingC1_')
				sh.innerHTML = ABOL.IABOL.V10.UI.Modules.MainPackageProcessingC1.GetTableRow(lookupstring).value;
			else
				sh.innerHTML = ABOL.IABOL.V10.UI.Modules.MainPackageProcessing.GetTableRow(lookupstring).value;
		}
	}

	function findPosX(obj)
	{
		var curleft = 0;
		if (obj.offsetParent)
		{
			while (obj.offsetParent)
			{
				curleft += obj.offsetLeft
				obj = obj.offsetParent;
			}
		}
		else if (obj.x) 
		{ 
			curleft += obj.x; 
		}
		return curleft;
	}

	function findPosY(obj)
	{
		var curtop = 0;
		if (obj.offsetParent)
		{
			while (obj.offsetParent)
			{
				curtop += obj.offsetTop
				obj = obj.offsetParent;
			}
		}
		else if (obj.y)
		{
			curtop += obj.y;
		}
		return curtop;
	}

	// This is here to act as a standards-compliant x-platform 'element.innerText'
	function getInnerText(text) { return text.replace(/<[^>]+>/g,""); }

	init();
