
function AutoSuggest(id, suggestId, displayNum, submitForm, file, delegator)
{
	this.source = new Array();
	this.file = file;
	this.id = id;
	this.inputNode = document.getElementById(id);
	this.submitForm = submitForm;
	this.suggestNode = document.getElementById(suggestId);
	this.displayNum = displayNum;
	this.delegator = (undefined != delegator) ? delegator : null;
	this.suggestionIndex = -1;

	this.inputNode.onkeydown = this.keydown;
	this.inputNode.onkeyup = this.keyup;
	this.inputNode.suggest = this; //pointer to AutoSuggest Object
	this.inputNode.suggestbox = this.suggestNode;
	this.inputNode.onblur = function() { this.suggest.hide(); };
	this.suggestNode.onmousedown = this.mousedown;
	this.suggestNode.inbox = this.inputNode;
	this.suggestNode.suggestbox = this;

//	this.inputNode.value = '';
	this.suggestNode.value = '';
	this.inprogress = false; // ready for autocomplete query the first time
}

AutoSuggest.prototype.getdatasource = function()
{
	var thisAS = this; // save away this AutoSuggest object as the meaning of "this" inside Ajax.Request object changes to point to the Ajax.Request object

	var userInput = this.inputNode.value;
	if (userInput.length == 0) { // don't make an ajax request when no userinput
		this.source = new Array();
		this.hide();
		return;
	}
	
	if (this.inprogress) return;
	if (this.inputNode == null) return;

	//var url = '/tools/autosuggestlist.html?f=suggest-where.tab&t=';
	var url = '/tools/autosuggestlist.html';
	this.inprogress = true;

	new Ajax.Request(url,
	  {
	    method:'get',
	    //asynchronous: 'false',
	    //parameters: {f: 'suggest-where.tab', t: userInput},
	    parameters: {f: this.file, t: userInput, c: thisAS.inputNode.name},
	    onSuccess: function(transport){
	      var results = transport.responseText || "no response text";
	      
	      // Should use JSON parsing methods from prototype instead of eval'ing the results 
		  thisAS.source = eval(results);
		  thisAS.inprogress = false;
		  thisAS.suggest();
	    },
	    onFailure: function(){ /*alert('Something went wrong...')*/ }
	  });

}

AutoSuggest.prototype.keyup = function(event)
{
	event = event || window.event;
	var keyValue = (event.keyCode) ? event.keyCode : (event.which) ? event.which : null;
	switch(keyValue)
	{
		case 38: //up arrow
				this.suggest.moveup();
				break;
		case 40: //down arrow
				this.suggest.movedown();
				break;
		default:
				if (keyValue != 13) {		
					// NEW APPROACH: run getdatasource which runs ajax call and let onSuccess callback run AutoSuggest.suggest() instead of here in keyup
					this.suggest.getdatasource();
					//this.suggest.suggest();
				}
				break;
	}
}

AutoSuggest.prototype.keydown = function(event)
{
	event = event || window.event;
	var keyValue = (event.keyCode) ? event.keyCode : (event.which) ? event.which : null;

	if (13 == keyValue) {
		this.suggest.hide();
	}
}

AutoSuggest.prototype.moveup = function()
{
	if (this.suggestionIndex > 0)
	{
		this.suggestNode.childNodes[this.suggestionIndex].style.backgroundColor = "#FFFFFF";
		this.suggestionIndex--;

		this.suggestNode.childNodes[this.suggestionIndex].style.backgroundColor = "#C4C4C4";
		this.inputNode.value = this.suggestNode.childNodes[this.suggestionIndex].firstChild.nodeValue;
	}
}

AutoSuggest.prototype.movedown = function()
{
	if (this.suggestionIndex < this.suggestNode.childNodes.length - 1)
	{
		if (this.suggestionIndex >= 0)
			this.suggestNode.childNodes[this.suggestionIndex].style.backgroundColor = "#FFFFFF";

		this.suggestionIndex++;
		this.suggestNode.childNodes[this.suggestionIndex].style.backgroundColor = "#C4C4C4";
		this.inputNode.value = this.suggestNode.childNodes[this.suggestionIndex].firstChild.nodeValue;
	}
}

AutoSuggest.prototype.mousedown = function(event)
{
	event = event || window.event;
	var text = (event.srcElement) ? event.srcElement.firstChild.nodeValue : event.target.firstChild.nodeValue;
	this.inbox.value = text;
	this.suggestbox.hide();
	this.inbox.focus();
}

// Modified because data no longer in "this.source" property of AutoSuggest.
AutoSuggest.prototype.suggest = function() {
	//if (this.source == null) this.source = dataSource; // Ajax call may not have been returned on initialization
	//if (this.source == null) return;

	var input = this.inputNode.value;
	if (!input || input == "") {
		this.hide();
	} else {
		if (this.source.length == 0) {
			this.hide();
		} else {
			var suggestions = "";
			for (i = 0; i < this.source.length; i++) {
				if (i >= this.displayNum) break;
				suggestions += "<div>" + this.source[i].value + "</div>";
			}
			this.show(suggestions);
		}
	}
}

AutoSuggest.prototype.hide = function()
{
	this.suggestNode.style.display='none';
	this.suggestionIndex = -1;
}

AutoSuggest.prototype.show = function(suggestions)
{
	this.suggestNode.style.display = 'block';
	this.suggestNode.innerHTML = suggestions;
}

