
//---------------------------------------------------------
//
// Property Searching
//
//---------------------------------------------------------

function suggest(text, e)
{ 
	textlength = text.length;
	var key = "";
	
	if(e==null)
	{
		e = window.event;
	}
	
	if(e.keyCode)
	{
		key = e.keyCode;	// Other
	}
	else
	{
		key = String.fromCharCode(e.which);	// IE
	}
	
	if((key > 48 && key < 90) || key == 8)
	{
		xmlHttp=GetXmlHttpObject()
		
		
		if (xmlHttp==null)
		{
			alert ("Browser does not support HTTP Request");
			return
		}
		
		if(text == "" || text.length < 2)
		{
			document.getElementById("search_suggest").style.display = "none";
			document.getElementById("search_suggest").innerHTML = "";
			idcount = 0;
		}
		else
		{
			var url="/includes/suggest/suggest.php";
			url = url + "?text=" + text;
			url = url + "&rand=" + Math.floor(Math.random()*10000000);
			
			xmlHttp.onreadystatechange=stateSuggest;
			xmlHttp.open("GET",url,true);
			xmlHttp.send(null);
		}
	}
}

function stateSuggest() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		var responsearray = xmlHttp.responseText.split("|");
		if(responsearray[0] == "")
		{
			document.getElementById("search_suggest").style.display = "none";
		}
		else
		{
			document.getElementById("search_suggest").innerHTML = responsearray[0];
			totalitems = parseInt(responsearray[1]);
			document.getElementById("search_suggest").style.display = "block";
		}
	} 
}

function SetValue(text)
{
	text = text.replace("<strong>", "");
	text = text.replace("</strong>", "");
	document.getElementById("suggestbox").value = text;
}

function GoTo(value)
{
	window.location = goToURL + value;
}

function listener(e, text)
{
	var key = "";
	highlightSuggestDiv(idcount, "#FFFFFF");
	if(e==null)
	{
		e = window.event;
	}
	
	if(e.keyCode)
	{
		key = e.keyCode;	// Other
	}
	else
	{
		key = String.fromCharCode(e.which);	// IE
	}
	
	try
	{
		if(key == "40")
		{
			idcount = (idcount < totalitems) ? (idcount+1) : 1;
			highlightSuggestDiv(idcount, "#CCCCCC");
			SetValue(document.getElementById("suggest_item_" + idcount).getAttribute('propertyname'));
		}
		else if(key == "38")
		{
			idcount = (idcount <= 1) ? totalitems : idcount-1;
			highlightSuggestDiv(idcount, "#CCCCCC");
			SetValue(document.getElementById("suggest_item_" + idcount).getAttribute('propertyname'));
		}
		else if(key == "13")
		{
			SetValue(document.getElementById("suggest_item_" + idcount).getAttribute('propertyname'));
		}
	}
	catch(e)
	{
	
	}
	
}

function highlightSuggestDiv(id, colour)
{
	try
	{
		var itemname = "suggest_item_"+id;
		document.getElementById("suggest_item_" + id).style.backgroundColor = colour;
	}
	catch(e)
	{
		//alert(e);
	}
}