// -------------------------------------------------------------------------
// Form Select Search Object
// -------------------------------------------------------------------------
function jsFormSelectSearch(selectId,searchFieldId)
{
	this.init(selectId,searchFieldId);
}

jsFormSelectSearch.prototype.init = function(selectId,searchFieldId,preLoad)
{
	this.selectId			= selectId;
	this.searchFieldId		= searchFieldId;
	
	this.serializedOptions  = new Array();
	this.displayedOptions	= document.getElementById(this.selectId).options;
	
	this.serializedOptions = this.serializeOptions(this.displayedOptions);
}

jsFormSelectSearch.prototype.serializeOptions = function(opts)
{
	var sopts = new Array;
	
	for(var i=0; i<opts.length; i++)
	{
		sopts[i] = new Array(opts[i].value,opts[i].text);
	}
	
	return sopts;
}

jsFormSelectSearch.prototype.searchData = function()
{
	var j=0;
	re = RegExp(this.searchFieldValue,"i");

	for(var i=0; i<this.serializedOptions.length; i++)
	{
		if(re.test(this.serializedOptions[i][1]))
		{
			this.displayedOptions[j] = new Option(
				this.serializedOptions[i][1],
				this.serializedOptions[i][0]
			);
			
			j++;
		}
	}
}

jsFormSelectSearch.prototype.clearSelect = function()
{
	document.getElementById(this.selectId).length=0;
}

jsFormSelectSearch.prototype.resetSearch = function()
{
	document.getElementById(this.searchFieldId).value = '';
	this.searchFieldValue = '';
	
	this.clearSelect();
	this.searchData();
}

jsFormSelectSearch.prototype.activeFormInput = function(NScode)
{
	var code = (typeof event != 'undefined') ? window.event.keyCode : NScode;
	
	if(code < 8)						return;
	else if(code > 8 && code < 32)		return;
	else if(code > 32 && code < 46)		return;
	else if(code > 46 && code < 48)		return;
	else if(code > 90 && code < 92)		return;
	else if(code > 111 && code < 124)	return;
	else if(code > 144 && code < 146)	return;
	
	if(code==8)
	{
		sval = document.getElementById(this.searchFieldId).value;
		slen = document.getElementById(this.searchFieldId).value.length;
		
		this.searchFieldValue = sval.substr(0,slen-1);
	}
	else
		this.searchFieldValue = document.getElementById(this.searchFieldId).value + String.fromCharCode(code);
	
	this.clearSelect();
	this.searchData();
}
