//http://www.barelyfitz.com/projects/filterlist/index.php/all

function filterlist(selectobj)
{
	this.selectobj = selectobj;
	this.flags = 'i';
	this.match_text = true;
	this.match_value = false;
	this.show_debug = false;
	this.init = function() {

	if (!this.selectobj) return this.debug('selectobj not defined');
	if (!this.selectobj.options) return this.debug('selectobj.options not defined');

	this.optionscopy = new Array();
	if (this.selectobj && this.selectobj.options)
	{
		for (var i=0; i < this.selectobj.options.length; i++)
		{
			this.optionscopy[i] = new Option();
			this.optionscopy[i].text = selectobj.options[i].text;
			this.optionscopy[i].des = selectobj.options[i].des;
			if (selectobj.options[i].value)
			{this.optionscopy[i].value = selectobj.options[i].value;}
			else
			{this.optionscopy[i].value = selectobj.options[i].text;}
		}
	}
seleccionado = selectobj.options[selectobj.options.selectedIndex].value // adecuación
}

this.reset = function(){this.set('')}

this.set = function(pattern)
{
	var loop=0, index=0, regexp, e;
	if (!this.selectobj) return this.debug('selectobj not defined');
	if (!this.selectobj.options) return this.debug('selectobj.options not defined');
	this.selectobj.options.length = 0;
	try {regexp = new RegExp(pattern, this.flags)} catch(e) {
	if (typeof this.hook == 'function'){this.hook()}
      return;
    }

alfinal = 0 // adecuación
    for (loop=0; loop < this.optionscopy.length; loop++) {

      var option = this.optionscopy[loop];

/*      if ((this.match_text && regexp.test(option.text)) ||
          (this.match_value && regexp.test(option.value)) ||
          (this.match_text && regexp.test(option.des)))
*/
      if(this.match_text && regexp.test(option.des))
{


        this.selectobj.options[index++] =
          new Option(option.text, option.value, false);

if(option.value==seleccionado){alfinal = index-1} // adecuación

      }
    }

this.selectobj.selectedIndex = alfinal

    if (typeof this.hook == 'function') {
      this.hook();
    }

  }

  this.set_ignore_case = function(value) {

    if (value) {
      this.flags = 'i';
    } else {
      this.flags = '';
    }
  }

  this.debug = function(msg) {
    if (this.show_debug) {
      alert('FilterList: ' + msg);
    }
  }
  this.init();
}
