// JavaScript Document

var AutoComplete;
if (!AutoComplete) AutoComplete = {};

AutoComplete = function(textboxID, url, callback, callbackArgs)
{
	this.textbox = document.getElementById(textboxID);
	
	/**
	 * The url can either be a string to which the value of the textbox will be appended before sending the request
	 * or it can be a delegate function which takes the value of the textbox as its argument and returns the url
	 */
	this.url = url;
	
	if (callback) this.callback = callback;
	if (callbackArgs) this.callbackArgs = callbackArgs;

	var self = this;
	if (this.textbox) {
		this.textbox.setAttribute('autocomplete', 'off');
		this.textbox.onblur = function(e) { self.AJAXClick(e); }
		this.textbox.onkeydown = function(e) { self.checkKey(e); }
		this.textbox.onkeyup = function(e) { self.getList(e); }
	}
	
	this.scrolling = false;
	this.selectedValue = "";
	this.selectedIndex = -1;
}

AutoComplete.prototype.getList = function()
{
	if (!this.scrolling)
	{
		this.selectedIndex = -1;
		this.selectedValue = "";
		
		if (typeof this.url == 'function')
			var url = this.url(this.textbox.value);
		else
			var url = this.url + this.textbox.value;
		
		var self = this;
		new Ajax.Request(url,
					 {
						 onSuccess: function(response) { self.showList(response); }
					 });
	}
}

AutoComplete.prototype.showList = function(xmlhttp)
{
	this.removeTable();

	if (!xmlhttp || !xmlhttp.responseText || xmlhttp.responseText == "")
		return;

	this.table = document.createElement('table');
	this.table.cellPadding = "0";
	this.table.cellSpacing = "0";
	this.table.className = "TableSelect";
	this.table.style.left = findPos(this.textbox).left + "px";
	this.table.style.top = findPos(this.textbox).top + this.textbox.offsetHeight + "px";
	this.table.style.width = this.textbox.offsetWidth + "px";
	document.body.appendChild(this.table);
	
	var response = eval("(" + xmlhttp.responseText + ")");
	var self = this;
	
	for (var i = 0; i < response.length; i++)
	{
		var row = this.table.insertRow(-1);
		var cell = row.insertCell(0);
		cell.className = "ajax_unselected";
		cell.setAttribute('value', response[i].value);
		cell.innerHTML = response[i].text;
		cell.onmouseover = function() { self.selectOption(this); }
	}
}

AutoComplete.prototype.checkKey = function(event)
{
	var key;
	if (window.event)
		key = window.event.keyCode;
	else if (event)
		key = event.which;
	
	if (key == 38)
		this.scrollAC(-1);
	else if (key == 40)
		this.scrollAC(1);
	else if (key == 13)
	{
		this.AJAXClick();
		return false;
	}
	else
		this.scrolling = false;
}

AutoComplete.prototype.scrollAC = function(dir)
{
	this.scrolling = true;
	var index;
	var size = this.table.rows.length;

	var oldIndex = this.selectedIndex;
	index = this.selectedIndex + dir;
	if (index < 0)
		index = size - 1;
	else if (index >= size)
		index = 0;
	
	this.selectOption(this.table.rows[index].cells[0]);
	
}

AutoComplete.prototype.selectOption = function(tdElem)
{
	this.selectedIndex = tdElem.parentNode.rowIndex;
	this.selectedValue = tdElem.innerHTML;
	tdElem.className = "ajax_selected";
	for (var i = 0; i < this.table.rows.length; i++)
	{
		if (i != this.selectedIndex)
			this.removeSelect(this.table.rows[i].cells[0]);
	}
}

AutoComplete.prototype.removeSelect = function(tdElem)
{
	tdElem.className = "ajax_unselected";
}

AutoComplete.prototype.AJAXClick = function()
{
	if (this.table)
	{
		if (this.selectedIndex != -1)
		{
			this.textbox.value = this.selectedValue;
			if (this.callback)
			{
				var arg = this.table.rows[this.selectedIndex].cells[0].getAttribute('value');
				
				if (!this.callbackArgs)
					this.callback(arg);
				else
					this.callback(arg, this.callbackArgs);
			}
		}
		this.removeTable();
	}		
}

AutoComplete.prototype.removeTable = function()
{
	if (this.table)
	{
		if (this.table.parentNode)
			this.table.parentNode.removeChild(this.table);
			
		this.table = null;
	}
	
	this.selectedValue = '';
	this.selectedIndex = -1;
}

function getProductName(productID, input)
{
	input.value = productID;
}