//--------------------------------------------------------
// AUTOCOMPLETION of field specifying player name

function addplayerautocomplete(inputid, containerid, hiddenplayerid, customevent, requireselection)
{
	//handle selection of a player from the autocomplete list
	var playernameSelectHandler = function(sType, aArgs) { 
		var oMyAcInstance = aArgs[0]; // your AutoComplete instance 
		var elListItem = aArgs[1]; //the <li> element selected in the suggestion 
								   //container 
		var aData = aArgs[2]; //array of the data for the item as returned by the DataSource 
		document.getElementById(hiddenplayerid).value = aData[3];	// the playerid
		if (oMyAcInstance.customevent)
			oMyAcInstance.customevent.fire();
	}; 
		 
	// An XHR DataSource 

		var lookupSource = new YAHOO.widget.DS_XHR("/lookupplayers.php", ["\n", "\t"]); 
		lookupSource.responseType = YAHOO.widget.DS_XHR.TYPE_FLAT; 		
	
	// Create the Auto-Complete object
	var playerAutoComplete = new YAHOO.widget.AutoComplete(inputid,containerid, lookupSource); 
	playerAutoComplete.maxResultsDisplayed = 20; 
	playerAutoComplete.forceSelection = !requireselection; 
	playerAutoComplete.autoHighlight = false; 
	playerAutoComplete.queryDelay = 0; 
	playerAutoComplete.itemSelectEvent.subscribe(playernameSelectHandler);

	playerAutoComplete.formatResult = function(aResultItem, sQuery) { 
		document.getElementById(hiddenplayerid).value = 0;	// make sure previous result is cleared.
		var sResult = aResultItem[1]; 
		if(sResult) { 
			return sResult + " (" + aResultItem[2] + ")"; 
		} 
		else { 
			return ""; 
		} 
	}; 
	playerAutoComplete.customevent = customevent;
}
// End of AUTOCOMPLETION section

