function DynamicInputSelect( controlId, outputId, newWidth )
{
	this.id = controlId;

	this.allowMultipleEntries = false;

	this.styleContainer 		= 'dynamicInputSelect';
	this.styleContainerItemOn 	= 'dynamicInputSelectItemOn';
	this.styleContainerItemOff 	= 'dynamicInputSelectItemOff';

	this.control 		= document.createElement('INPUT');
	this.control.id 	= this.id;
	this.control.name 	= this.id;
	this.control.type 	= 'text';
	this.control.objRef = this;

	var width = "100";
	if( newWidth != undefined )
	{
		width = newWidth;
	}
	this.control.style.width = width + 'px';

	this.control.onkeyup = function()
	{
		this.objRef.ShowSuggestions();
	}

	this.control.onblur = function()
	{
		this.objRef.CloseSuggestions();
	}

	this.control.onclick = function()
	{
		if( this.objRef.allowMultipleEntries == false )
		{
			this.select();
		}
	}

	// Setup the container
	this.container 				= document.createElement('DIV');
	this.container.id 			= this.id + "_container";
	this.container.className	= this.styleContainer;
	this.container.style.width 	= this.control.style.width;

	// Setup Ajax Object
	this.ajax = new AjaxClient();
    this.ajax.requestFile      = "/shared/includes/ajax/Objects/DynamicInputSelect.php";


	if( out = document.getElementById(outputId) )
	{
		out.appendChild( this.control );
		out.appendChild( document.createElement('BR') );
		out.appendChild( this.container );
	}
}

DynamicInputSelect.prototype.ShowSuggestions = function()
{
	obj = this;
	this.ajax.callbackFunction = obj.Callback;

	var queryValue = this.control.value.substr( (this.control.value.lastIndexOf(",")+1) );
	queryValue = queryValue.replace(/(\s+$)|(^\s+)/g, '');

	if( queryValue != '' )
	{
		this.ajax.SendRequest( queryValue, this.groupRestrict );
	}
	else
	{
		this.container.innerHTML = '';
		this.container.style.display = 'none';
	}
}

DynamicInputSelect.prototype.Callback = function( objXml, strXml )
{
	obj.container.innerHTML 		= '';

	var users = objXml.getElementsByTagName("user");

    if( users.length > 0 )
    {
		obj.container.style.display = 'block';

		for (var i = 0; i < users.length; i++)
        {
        	var lookupValue = obj.control.value;

            var id = ( users[i].getElementsByTagName("id")[0].firstChild ) ? users[i].getElementsByTagName("id")[0].firstChild.nodeValue : '';
            var username = ( users[i].getElementsByTagName("username")[0].firstChild ) ? users[i].getElementsByTagName("username")[0].firstChild.nodeValue : '';
            var firstName = ( users[i].getElementsByTagName("first_name")[0].firstChild ) ? users[i].getElementsByTagName("first_name")[0].firstChild.nodeValue : '';
            var lastName = ( users[i].getElementsByTagName("last_name")[0].firstChild ) ? users[i].getElementsByTagName("last_name")[0].firstChild.nodeValue : '';

            var dFirstName 	= firstName;
            var dLastName 	= lastName;

            var startReplace 	= 0;

            if( (lVal = lookupValue.lastIndexOf(",")) != -1 )
            {
            	oldValue = lookupValue;
            	lookupValue = lookupValue.substring( (lVal+2), oldValue.length );

            	startReplace = (lVal+2);
            }

           	if( firstName.substring(0, lookupValue.length).toLowerCase() == lookupValue.toLowerCase() )
           	{
	    		fName0 		= "<b>" + firstName.substring(0, lookupValue.length) + "</b>";
	    		fName1 		= firstName.substring(lookupValue.length);
	    		dFirstName 	= fName0 + fName1;
           	}
           	if( lastName.substring(0, lookupValue.length).toLowerCase() == lookupValue.toLowerCase() )
           	{
	    		lName0 		= "<b>" + lastName.substring(0, lookupValue.length) + "</b>";
	    		lName1 		= lastName.substring(lookupValue.length);
	    		dLastName 	= lName0 + lName1;
           	}

           	var displayName 	= "\"" + dFirstName + " " + dLastName + "\" &lt;"+username+">";
			divItem 			= document.createElement('DIV');
			divItem.id 			= id;
			divItem.innerHTML 	= displayName;

			if( obj.allowMultipleEntries )
			{
				divItem.clickValue 	= "\"" + firstName + " " + lastName + "\" <"+username+">, ";
			}
			else
			{
				divItem.clickValue 	= "\"" + firstName + " " + lastName + "\" <"+username+">";
			}
			divItem.parentObj	= obj;
			divItem.className	= obj.styleContainerItemOff;

			divItem.currentStartReplace = startReplace;

			divItem.onclick = function()
			{
				var newValue = "";
				if( this.parentObj.allowMultipleEntries )
				{
					if( this.parentObj.control.value.length > 0 )
					{
						newValue = this.parentObj.control.value.substr( 0, this.currentStartReplace );
					}
				}
				newValue += this.clickValue;

				this.parentObj.control.value = newValue;

				this.parentObj.CloseSuggestions();
			}
			divItem.onmouseover = function()
			{
				this.parentObj.control.onblur = '';
				this.className = this.parentObj.styleContainerItemOn;
			}
			divItem.onmouseout = function()
			{
				this.parentObj.control.onblur = function()
				{
					this.objRef.CloseSuggestions();
				}
				this.className = this.parentObj.styleContainerItemOff;
			}
			obj.container.appendChild( divItem );
        }
    }
    else
    {
    	obj.container.style.display = 'none';
    }

}

DynamicInputSelect.prototype.CloseSuggestions = function()
{
	this.container.style.display = 'none';
}