var selectionIndex = 0;
var matches = new Array();
var selectedCellID;
var suggestionTimer;

function suggest(e, txtSearch)
{
	var code;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
	
	if ( suggestionTimer != null )
		window.clearTimeout ( suggestionTimer );
		
	if ( code >= 48 && code <= 90 )
		suggestionTimer = window.setTimeout("suggestGenerator(" + code + ", '" + txtSearch.id + "')", 200);
	else
		suggestGenerator(code, txtSearch.id);
}

function hideSuggestions()
{
	var div = document.getElementById("divSuggestions");

	selectionIndex = 0;
	matches = new Array();
	div.style.display = "none";
	toggleDropdowns(div);
}

function suggestGenerator(code, txtSearchID)
{	
	var txtSearch =  document.getElementById(txtSearchID)
	var div = document.getElementById("divSuggestions");
	var data = txtSearch.value;
	
	if (data == "")
		hideSuggestions();
	else
	{
		div.style.display = "";
		var match = false;		
		var i, s;
		
		if(code != 38 && code != 40) //not up or down
		{
			matches = new Array();
			selectionIndex = 0;

			for (i = 0; i < suggestions.length; i++)
			{
				s = suggestions[i];
				
				if(s.toUpperCase().indexOf(data.toUpperCase()) == 0)
				{
					if(!match && code >= 48 && code <= 90)
					{					
						//txtSearch.value = s;
						
						if( txtSearch.setSelectionRange ) 
							txtSearch.setSelectionRange(data.length, s.length);
						else
						{
							var tr = txtSearch.createTextRange();
							
							tr.moveStart('character', data.length);

							tr.select();
						}
					}
					
					if(!match)
						match = true;
						
					matches.push(s);
				}
			}
			
		}
		
		if(matches.length > 0)
		{
			if(code == 38 || code == 40) //38-up 40-down
			{
				if(code == 38) //up
				{
					selectionIndex --;
					if(selectionIndex < 0)
						selectionIndex = matches.length - 1;
				}
				else //down
				{
					selectionIndex ++;
					if(selectionIndex >= matches.length)
						selectionIndex = 0;
				}
			}
			
			var cellID;
			var innerHTML = "<table cellSpacing=\"0\" cellPadding=\"3\" border=\"0\" width=\"100%\">";
			for (i = 0; i < matches.length && i <= 20; i++)
			{
				var sColor;
				if ( i == selectionIndex )
				{
					sColor = "color: white;"
				}
				else
				{
					sColor = "";
				}
				
				s = matches[i];
				
				cellID = "td" + i;
				innerHTML += "<tr><td id=\"" + cellID + "\" onmouseover=\"highlightmatch(this);\" onmouseout=\"unhighlightmatch(this);\" onmousedown=\"selectmatch(this);\" style=\"FONT-FAMILY:Trebuchet MS,Tahoma,Arial,Verdana,sans-serif,Lucida Sans Unicode; font-size: 9pt;" + sColor + "\"";

				if(i == selectionIndex)
				{
					innerHTML += " bgColor=#E54489 ";
					selectedCellID = cellID;
					if(code == 38 || code == 40)
						txtSearch.value = s;
				}
				
				innerHTML += ">" + s + "</td></tr>";
			}
			innerHTML += "</table>";
			div.innerHTML = innerHTML;
		}
		else
		{
			hideSuggestions();
			txtSearch.value = txtSearch.value.toLowerCase();
		}
		
		toggleDropdowns(div);
	}
}

function highlightmatch(td)
{
	var selectedTD = document.getElementById(selectedCellID);
	selectedTD.bgColor = "#ffffff";
	td.bgColor = "#E54489";
	td.style.color = "#FFFFFF";
}

function unhighlightmatch(td)
{
	td.bgColor = "#ffffff";
	td.style.color = "#75706A";
}

function selectmatch(td)
{
	window.location = "http://search.pioneerlinens.com/search.aspx?keywords=" + td.innerHTML;
}

function toggleDropdowns(div)
{	
	if ( navigator.appName == "Microsoft Internet Explorer" )
	{
		var dds = document.body.getElementsByTagName("SELECT");
		var divX1 = findX(div);
		var divX2 = divX1 + div.clientWidth;
		var divY1 = findY(div);
		var divY2 = divY1 + div.clientHeight;
		
		/*
			alert(divX1);
			alert(divX2);
			alert(divY1);
			alert(divY2);
		*/
			
		var dd;
		var ddX1;
		var ddX2;
		var ddY1;
		var ddY2;
		
		for (var i = 0; i < dds.length; i++)
		{
			dd = dds[i];

			var ddX1 = findX(dd);
			var ddX2 = ddX1 + dd.clientWidth;
			var ddY1 = findY(dd);
			var ddY2 = ddY1 + dd.clientHeight;
	/*
			alert(ddX1);
			alert(ddX2);
			alert(ddY1);
			alert(ddY2);
	*/
			dd.style.visibility = "visible";

			if ( div.style.display == "" && (((ddX1 > divX1 && ddX1 < divX2) || (ddX2 > divX1 && ddX2 < divX2) || (ddX1 <= divX1 && ddX2 >= divX2)) && ((ddY1 > divY1 && ddY1 < divY2) || (ddY2 > divY1 && ddY2 < divY2) || (ddY1 <= divY1 && ddY2 >= divY2))))
				dd.style.visibility = "hidden";
		}
	}
}

function findX(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 findY(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;
}
