var listBackgroundColor="#ffffff";
var listBorderColor="#A5A6AD";
var listTopOffset=0;
var listLeftOffset=150;
var listItemFontSize=12;
var request;
var listContainer=null;
var list;
var delimiter=',';
var keywordSet = new Array();
var listItem = new Array();
var activeBackgroundColor="#cccccc";
var activeColor="#ffffff";
var inactiveBackgroundColor="#ffffff";
var inactiveColor="#000000";
var inputObject;
var ARROWUP=38;
var ARROWDOWN=40;
var ENTER=13;
var currentItem=-1;
var state="nsw";
var controlTop=257;
var controlLeft=443;

function createRequest()
{
if(window.XMLHttpRequest)
request = new XMLHttpRequest();
else
if(window.ActiveXObject)
request =new ActiveXObject("Microsoft.XMLHTTP");
}

function activeItem(evt)
{
	var obj;
if(typeof(window.event)!="undefined" && window.event!=null)
	obj=event.srcElement;
else
	obj=evt.target;

obj.style.backgroundColor=activeBackgroundColor;
obj.style.color=activeColor;
obj.className="active";
}
function inactiveItem(evt)
{
	var obj;
if(typeof(window.event)!="undefined" && window.event!=null)
	obj=event.srcElement;
else
	obj=evt.target;
	
obj.style.backgroundColor=inactiveBackgroundColor;
obj.style.color=inactiveColor;
obj.className="inactive";
}

function hideList(evt)
{

if(typeof(window.event)!="undefined" &&window.event!=null)
inputObject.value=window.event.srcElement.innerHTML;
else
inputObject.value=evt.target.innerHTML;

if(typeof(listContainer)!="undefined")
listContainer.style.display="none";

}
function filtItem()
{
var reg;
if(isNaN(Math.ceil(inputObject.value)) || inputObject.value.length==0)
	reg=new RegExp("^"+inputObject.value,"i");
else
	reg=new RegExp(inputObject.value,"i");

	currentItem=-1;
	for(i=0;i<listItem.length;i++)
	{	
		
		if(reg.test(listItem[i].innerHTML))
		{
			listItem[i].style.display="block";
			if(currentItem==-1)
			{
				currentItem=i;
				listItem[i].className="active";
				listItem[i].style.backgroundColor=activeBackgroundColor;
				listItem[i].style.color=activeColor;
			}
			else
				{
				listItem[i].className="inactive";
				listItem[i].style.backgroundColor=inactiveBackgroundColor;
				listItem[i].style.color=inactiveColor;
				}
		}
		else
		{
 				listItem[i].style.display="none";
				listItem[i].className="inactive";
				listItem[i].style.backgroundColor=inactiveBackgroundColor;
				listItem[i].style.color=inactiveColor;
		}
	}
}
function addList()
{

if(request.readyState==4)
{

if(request.status==200){
list=request.responseText.split(delimiter);

for(i=0;i<list.length-1;i++)
{
	var newItem=false;
	if(typeof(listItem[i])=="undefined")
	{
		listItem[i]=document.createElement("div");
		newItem=true;
	}
	
	listItem[i].style.width=listContainer.style.width;
	listItem[i].style.height="20px";
	listItem[i].innerHTML=list[i];
	listItem[i].style.fontSize=listItemFontSize;
	listItem[i].style.backgroundColor=inactiveBackgroundColor;
	listItem[i].style.inactiveColor=inactiveColor;
	listItem[i].style.display="block";
	
	if(newItem)
		listContainer.appendChild(listItem[i]);
		
	listContainer.style.display="block";

	if(document.attachEvent)
	{
	listItem[i].attachEvent("onmouseover",activeItem);
	listItem[i].attachEvent("onmouseout",inactiveItem);
	listItem[i].attachEvent("onclick",hideList);
	}
	else
  if(document.addEventListener)
	{
	listItem[i].addEventListener("mouseover",activeItem,false);
	listItem[i].addEventListener("mouseout",inactiveItem,false);
	listItem[i].addEventListener("click",hideList,false);
	}
}
if(list.length>1)
{
listItem[0].style.backgroundColor=activeBackgroundColor;
listItem[0].style.activeColor=activeColor;
}

filtItem();
showList();
}
else
alert("Error with Status code"+request.statusText);
}
}

function isListUpdated(str)
{ 
if(keywordSet[str]==1)
return true;
else
return false;
}

function showList(obj)
{
if(listContainer!=null &&listContainer.style.display=="none")
listContainer.style.display="block";
}
function getList()
{
	if(inputObject.value.length<3)
	{
		if(typeof(listContainer)!="undefined" && listContainer!=null )
			listContainer.style.display="none";
			return;
	}  
	if(listContainer==null)
	{
		listContainer = document.createElement("div");
		listContainer.name="dropList";
		listContainer.style.border="1px solid "+listBorderColor;
		listContainer.style.position="absolute";
		listContainer.style.display="none";
		listContainer.style.top=controlTop+"px";//parseInt(inputObject.style.top)+parseInt(inputObject.style.height)+listTopOffset+"px" ;
		listContainer.style.left=controlLeft+"px";//parseInt(inputObject.style.left)+listLeftOffset+"px" ;
		listContainer.style.height="100px";
		listContainer.style.width=inputObject.style.width;
		listContainer.style.backgroundColor=listBackgroundColor;
		listContainer.style.overflow="auto";
		listContainer.style.scrollbarBaseColor=listBorderColor;
		document.body.appendChild(listContainer);
	}
	
	var indexStr=inputObject.value.substr(0,3);
	var region=document.getElementById("region");
state=region.value;
var country=document.getElementById("countries").value;	
/*if(document.getElementById("countries").value.toLowerCase()!="aud")return;*/

	var requestString = serverAddr + "?st="+state+"&country="+country+"&";
	if(isNaN(Math.ceil(inputObject.value)) || inputObject.value.length==0)
	{
		requestString=requestString + "sub="+inputObject.value;
	}
	else
	{
		requestString=requestString + "post="+inputObject.value;
	}

	
	if(!isListUpdated(indexStr))
	{
		keywordSet[indexStr]=1;
		createRequest();
		request.onreadystatechange=addList;
		request.open("GET",requestString,true);
		request.send(null);

	}
	else
 	{
	filtItem();
	showList(inputObject);
	}
}

function nextItem()
{
/*	alert(listItem[currentItem].style.backgroundColor);*/
	return;
	if(listItem[currentItem].className=="active")
	{
	
	listItem[currentItem].style.backgroundColor=inactiveBackgroundColor;
	listItem[currentItem].style.color=inactiveColor;
	currentItem++;
	while(listItem[currentItem].style.display=="none" && currentItem<listItem.length)
	currentItem++;

	if(currentItem==listItem.length)currentItem=0;
	listItem[currentItem].style.backgroundColor=activeBackgroundColor;
	listItem[currentItem].style.color=activeColor;
	}	
}
function getSuburb(evt)
{
var eventObject;
	if(!window.event)
	{
		inputObject=evt.target;
		eventObject=evt;

	}
	else
	{
		inputObject=window.event.srcElement;
		eventObject=window.event;

	}
		
		if(eventObject.keyCode==ARROWUP && listItem.length>0);
		if(eventObject.keyCode==ARROWDOWN && listItem.length>0)nextItem();
		if(eventObject.keyCode==ENTER && listItem.length>0)
			inputObject.value=listItem[currentItem].innerText;

getList();	
}
