function updateuserlist(editbox, users)
{
	var userlist = editbox.nextSibling;
	var rows = "";
	var numrows = 0;

	for (user in users)
	{
		rows += "<tr><td>" + user + "</td><td><a href='#' onclick='seteditbox(\""+user+"\"); this.parentNode.parentNode.parentNode.parentNode.parentNode.style.visibility = \"hidden\";'>" + users[user] + "</a></td></tr>\n";
		numrows++;
	}

	if(rows != "")
	{
		if(userlist.style.visibility != "visible")
		{
			var editpos = findPos(editbox);
			userlist.style.left = editpos[0] + "px";
			userlist.style.top = editpos[1] + editbox.offsetHeight + "px";
			userlist.style.width = editbox.offsetWidth*2 + 110 + "px";
			userlist.style.visibility = "visible";
		}
		userlist.style.height = (numrows*2.1) + "em";
	}
	else
	{
		userlist.style.visibility = "hidden";
	}
		
	userlist.firstChild.innerHTML = rows;
}

function getmatchingusers(editbox, text)
{
	var xmlhttp=false;

	        try
	        {
	                xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	        }
	        catch (e)
	        {
	                try
	                {
	                        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	                }
	                catch (E)
	                {
	                        xmlhttp = false;
	                }
	        }
	
	if (!xmlhttp && typeof(XMLHttpRequest) != 'undefined')
	{
	
	    try
	    {
	      xmlhttp = new XMLHttpRequest();
	    }
	    catch (e)
	    {
	      xmlhttp = false;
	    }
	}
	if(xmlhttp)
	{
		xmlhttp.onreadystatechange = (function ()
						{
							if(xmlhttp.readyState == 4)
							{
								if(xmlhttp.status == 200)
								{
									updateuserlist(editbox, eval('(' + xmlhttp.responseText + ')'));
								}
								else
									alert("Error: " + xmlhttp.status);
							}
						});
		xmlhttp.open("GET", "getusers.php?name=" + escape(text), true);
		xmlhttp.send(null);
	}
	return false;
}


function autocompletecheck(e)
{
	var e = e || window.event;
	var inp = e.currentTarget || e.target;
	var text = inp.value;
	var keycode = e.which || e.keyCode;
	if ( keycode >= 32)
		text += String.fromCharCode(e.which || e.keyCode);
	else if (keycode == 8)
		text = text.substr(0, text.length-1);
	inp.style.backgroundColor = "#ffffff"; 
	inp.valid = false;
	getmatchingusers(inp, text);
	return true;
}

function findPos(obj)
{
	var curleft = curtop = 0;
	if (obj.offsetParent)
	{
		do
		{
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
		
		return [curleft,curtop];
	}
}
		
function seteditbox(username)
{
	var editbox = document.getElementById("username");
	editbox.value = username;
	editbox.style.backgroundColor = "#c2ff99";
	editbox.valid = true;
}

function checkvalidusername(form)
{
	if(form.username.valid != true) 
	{
		alert("Please enter the member's name and select it from the pop-up list");
		return false;
	}
	return true;
}
