newElem = Array();
function keypress(e, listElemID)
{
	var key = e.keyCode;
//window.alert("got cursor up or down" + key);
    if (key == 38 || key == 40) // up arrow and down arrow
    {
//var ddl = document.getElementById(listElemID);
//document.getElementById("sh").innerHTML = ddl.scrollHeight;
        highlightEntry(key, listElemID);    
        return false;
    }
	else if (key == 13)
	{
		return false;
	}
	return true;
}

function disableEnter(e)
{
window.alert("in key down");
	return (e.keyCode != 13);
}

function handleKey(obj, evt, listElemID, listArr, hiddenInputId, stateChangeFunc, reqdestination)
{
	var ddl = document.getElementById(listElemID);
	var key = evt.keyCode;
	var elemID = obj.getAttribute("id");

	if (key == 27) // escape key
	{
		hideDropDown(listElemID);
		return;
	}
	else if (key == 13) // enter key
	{
		setHighlightedTown(listElemID, elemID, hiddenInputId, stateChangeFunc, reqdestination);
		return true;
	}
	else if ((key < 65 || key > 90) && key != 189 && key != 8)
	{
		return;
	}
	//var input = document.getElementById("townlist").value;
	var input = obj.value;
	var len = input.length;
	if (len == 0)
	{
		hideDropDown(listElemID);
		return;
	}
	var c = 0;
	var toWrite = Array();
	var substring;
	//for (i=0; i<towns.length && c < 15; i++)
	for (i=0; i<listArr.length; i++)
	{
		substring = listArr[i].substr(0, len);
		if (input.toLowerCase() == substring.toLowerCase())
		{
			toWrite[c] = listArr[i];
			c++;
		}
	}
	toWrite.sort();
	ddl.innerHTML = "";
	if (toWrite.length > 0)
	{
		showDropDown(listElemID);
	}
	else
	{
		hideDropDown(listElemID);
	}

	for (i=0; i<toWrite.length; i++)
	{
		newElem = document.createElement("p");
		newElem.setAttribute("id", "id" + i);
		newElem.onmouseover = function() {handleOver(this, listElemID);};
		newElem.onmouseout = function() {handleOut(this);};
		newElem.onmousedown = function() {selectElement(this, elemID, listElemID, hiddenInputId, stateChangeFunc, reqdestination);};
		newElem.innerHTML = toWrite[i];
		ddl.appendChild(newElem);
	}
	ddl.onblur = function () {hideDropDown(listElemID);};
	//ddl.onblur = hideDropDown(listElemID);
	return true;
}

function hideDropDown(listElemID)
{
	var ddl = document.getElementById(listElemID);
	ddl.style.display = "none";
        document.getElementById("district").style.visibility = "visible";
}

function showDropDown(listElemID)
{
        document.getElementById("district").style.visibility = "hidden";
	var ddl = document.getElementById(listElemID);
	ddl.style.display = "block";
}

function getStyle(el,styleProp)
{
	var x = document.getElementById(el);
	if (x.currentStyle)
		var y = x.currentStyle[styleProp];
	else if (window.getComputedStyle)
		var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
	return y;
}

function highlightEntry(keyCode, listElemID)
{
	var elem;
	var ddl = document.getElementById(listElemID);
	var bgc;
	var attr;
	var highlightedNode;
	highlightedNode = getHighlightedNode(ddl);
	if (highlightedNode)
	{
		if (keyCode == 38)
		{
			elem = highlightedNode.previousSibling;
		}
		else if (keyCode == 40)
		{
			elem = highlightedNode.nextSibling;
		}
		handleOver(elem, listElemID);
		//handleOut(highlightedNode);
	}
	else
	{
		elem = ddl.firstChild;
		handleOver(elem, listElemID);
	}

	return true;
}

function getHighlightedNode(parentNode)
{
	var elem = 0;
	var attr;
	var bgc;
	for (i=0; i<parentNode.childNodes.length; i++)
    {
		if (parentNode.childNodes[i].nodeType == 1)
		{
			bgc = parentNode.childNodes[i].style.backgroundColor;
			if (bgc == "#0282cc")
			{
				elem = parentNode.childNodes[i];
				break;
			}
		}
	}
	return elem;
}

function setHighlightedTown(listElemID, elemID, hiddenInputId, stateChangeFunc, reqdestination)
{
	var ddl = document.getElementById(listElemID);
	var elem = getHighlightedNode(ddl);
	if (!elem)
	{
		return;
	}
	else
	{
		selectElement(elem, elemID, listElemID, hiddenInputId, stateChangeFunc, reqdestination);
	}
}

function selectElement(obj, elemID, listElemID, hiddenInputId, stateChangeFunc, reqdestination)
{
	var inp = document.getElementById(elemID);
	inp.value = obj.innerHTML.replace("&amp;", "&");
	hideDropDown(listElemID);
//get(null, 'beerfinder-venuesearch');
get(elemID, hiddenInputId, stateChangeFunc, reqdestination);
	submitForm();
}

function handleOver(obj, listElemID)
{
	resetDropdownStyles(listElemID);
	obj.style.backgroundColor =  "#0282cc";
	obj.style.color = "white";
}

function handleOut(obj)
{
	obj.style.backgroundColor = "white";
	obj.style.color = "black";
}

function resetDropdownStyles(listElemID)
{
	var ddl = document.getElementById(listElemID);
	for (i=0; i<ddl.childNodes.length; i++)
    {
        if (ddl.childNodes[i].nodeType == 1)
        {
			bgc = ddl.childNodes[i].style.backgroundColor;
			if (bgc == "#0282cc")
			{
				handleOut(ddl.childNodes[i]);
				//break;
			}
		}
	}
	return true;
}

function disableOnBlur(inputElemID)
{
	var elem = document.getElementById(inputElemID);
	elem.onblur = "";
}

function enableOnBlur(inputElemID, listElemID)
{
	var elem = document.getElementById(inputElemID);
	elem.onblur = function () {hideDropDown(listElemID);};
}

function submitForm()
{
// get(this.parentNode,'beerfinder-venuesearch');
return false;
}

