var xmlHttp;
var loaded=0;

var waitingforreply = 0;
var sentmessage = "";
var oldMessage = "";
var lastselectedrow = 0;
var lastselectedcolor = 0;

var resultswidth = 267;
function setResultsWidth(width){resultswidth = width;}
try{
// Setup and send the resquest to the server
function sendRequest(event, formvalue){

	if(formvalue.value.length == 0)
	{
		document.getElementById("Possible_Properties").innerHTML = "";
		resizeDropDownMenu();
		//document.getElementById("searchinimg").style.visibility = "hidden";
		return;
	}

	if(waitingforreply == 1)
		return;

	if(oldMessage == formvalue.value)
		return;

	if(!sendSuggestRequest())
		return;

	var rqst;
	rqst = "../suggestPropertyName.jsp?";
	rqst = rqst + "propname=" + formvalue.value;

	//document.getElementById("Possible_Properties").innerHTML = "Searching...";

	document.getElementById("searchinimg").style.visibility = "visible";

	oldMessage = formvalue.value;

	xmlHttp.open("POST",rqst,true);
	xmlHttp.send(null);

	waitingforreply = 1;
	sentmessage = rqst
}

var inputsearchfocus = 0;

// Used to help open and close the div properly
var overlist = 0;
function setOverList(){overlist = 1}
function setOutList(){overlist = 0;}

// Closes the div that displays the results
function closelist(){

	if(overlist == 0){
		document.getElementById("Possible_Properties").style.visibility = "hidden";
	}
	else
		document.getElementById("Possible_Properties").focus();

	inputsearchfocus = 0;
}

// Closes the div when the list has focus
function closelistfromlist(){

	document.getElementById("Possible_Properties").style.visibility = "hidden";

	inputsearchfocus = 0;
}

// Open the div that displays the results
function openlist(){

	document.getElementById("Possible_Properties").style.visibility = "visible";

	resizeDropDownMenu();

	inputsearchfocus = 1;
}

// Send what the user has typed so far to see if we can kinda a match
function sendSuggestRequest()
{
	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				document.getElementById("Possible_Properties").innerHTML = "Your browser does not support AJAX!";
				return false;
			}
		}
	}
	try{
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState==4)
		{
			try{
			waitingforreply = 0;

			// Get the servers response
			document.getElementById("Possible_Properties").innerHTML = xmlHttp.responseText;

			document.getElementById("Possible_Properties").style.visibility = "hidden";

			// Set the div to visible if we got a none empty response
			if(document.getElementById("Possible_Properties").innerHTML != "")
				document.getElementById("Possible_Properties").style.visibility = "visible";

			resizeDropDownMenu();

			document.getElementById("searchinimg").style.visibility = "hidden";

			if(sentmessage != document.getElementById("search_by_Name").value)
				sendRequest();

			}catch(e){}
		}
	}
	}
	catch(e){}

	loaded=1;

	return true;
}

// Set the element in the table inside the div to the highlighted colour
function mouseOverTableElement(element){
	try{
		element.style.background = "#C0FEB1";//#FFFF99#3CB401
	}
	catch(e){}
}

// Set the element in the table inside the div back to it's origonal colour
function mouseOutTableElement(element, colour){
	try{
		element.style.background = colour;
	}
	catch(e){}
}

// Check for clicks in the document.  Used to make sure the div for displaying search results closes properly
document.body.onclick = function() {

	if(overlist == 0 && inputsearchfocus == 0){
		document.getElementById("Possible_Properties").style.visibility = "hidden";
	}
}

// Resize the menu if it's not full to max height.  Workaround for IE
function resizeDropDownMenu(){

	try{

		document.getElementById("responsetable").width = resultswidth+"px";

		document.getElementById("Possible_Properties").style.height = Math.min(150,(document.getElementById("responsetable").offsetHeight)) + "px";

		// Resize the width of the table if there's no scollbar
		if(document.getElementById("responsetable").offsetHeight < 150)
			document.getElementById("responsetable").width = (resultswidth+16)+"px";//"267px";(+17)
	}
	catch(e)
	{
		document.getElementById("Possible_Properties").style.visibility = "visible";//hidden
		document.getElementById("Possible_Properties").style.height = "16px";
	}
}

function tableclicked(tablerow){

	//document.getElementById("search_by_Name").value = tablerow;
	//document.getElementById("search_by_Name").focus();
}
}catch(e){}
