/*
	Flexible Modular Core Ultimate by Alesandro Ubriaco
	TextSelect Script
*/

function TextSelect(obj, name, id)
{
	var _self = this;
	this.Container = obj;
	this.Container.className = 'textselect';
	this.Options = new Array();
	this.Input = document.createElement('INPUT');
	this.Input.type = 'text';
	this.Input.name = name;
	if(id)
		this.Input.id = id;
	else
		this.Input.id = 'form-field-' + name;
	this.Input.setAttribute('autocomplete','off');
	this.Select = document.createElement('DIV');
	this.Select.style.display = 'none';
	this.Select.className = 'select';
	this.Container.appendChild(this.Input);
	this.Container.appendChild(this.Select);
	
	
	this.handler_optionSelected = function(id,value) { };
	
	
	this.Input.onkeyup = function()
	{
		var __self = _self;
		_self.Select.innerHTML = '';
		var i = 0;
		for(var n = 0; n < _self.Options.length; n++)
		{
			var item = _self.Options[n];
			if(_self.Input.value.length < 1 || item[1].toLowerCase().search(_self.Input.value.toLowerCase()) == 0)
			{
				var a = document.createElement('A');
				a.href = '#';
				a.innerHTML = item[1];
				a._id = item[0];
				a._value = item[1];
				a.onclick = function()
				{
					_self.Input.value = this._value;
					_self.Select.style.display = 'none';
					_self.handler_optionSelected(this._id, this._value);
					return false;
				};
				_self.Select.appendChild(a);
				i++;
			}
		}
		if(i > 0)
			_self.Select.style.display = 'block';
		else
			_self.Select.style.display = 'none';
	};
	
	this.Input.onclick = this.Input.onkeyup;
	
	this.Input.onblur = function()
	{
		setTimeout(function() { _self.Select.style.display = 'none'; },200);
	};
	
	this.addOption = function(id, value)
	{
		this.Options[this.Options.length] = new Array(id,value);
	};
	
	this.setList = function(s)
	{
		this.Options = new Array();
		var p = s.split('|');
		for(var n = 0; n < p.length; n++)
			this.addOption(0, p[n]);
	};
}

