function pull_select(el_id, array, config)
{
  this.is_ie = ( /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent) );
  this.is_ie5 = ( this.is_ie && /msie 5\.0/i.test(navigator.userAgent) );
  this.is_ie6 = ( this.is_ie && /msie 6\.0/i.test(navigator.userAgent) );
  this.is_opera = ( /opera/i.test(navigator.userAgent) );

  this.CNs={sel: config.sel, def: config.def};

  if (typeof(config.h)=='undefined') this.h=100; else this.h=config.h;
  if (typeof(el_id)=='object') this.input=el_id; else this.input = document.getElementById(el_id);
  if (typeof(config.minChars)=='undefined') this.minChars=3; else this.minChars=config.minChars;
  if (typeof(config.ajax_url)=='undefined') this.ajax_url=false; else this.ajax_url=config.ajax_url;
  if (typeof(config.ajax_run)=='undefined') this.ajax_run=function () {}; else this.ajax_run=config.ajax_run;
  if (typeof(config.ajax_get_params)=='undefined') this.ajax_run=false; else this.ajax_get_params=config.ajax_get_params;
  if (typeof(config.add_func)=='undefined') this.add_func=function () {}; else this.add_func=config.add_func;

  if (typeof(config.draw_func)=='undefined') this.draw_func=function (str) {return str}; else this.draw_func=config.draw_func;
  if (typeof(config.on_select_func)=='undefined') this.on_select_func=null; else this.on_select_func=config.on_select_func;
  if (typeof(config.def_width)!='undefined') this.def_width=config.def_width;

  if (this.ajax_url)
  {
    this.ajax_obj=new getHTML();
    this.ajax_obj.run=this.ajax_run;
  }
  this.inp_val='';
  this.new_h=this.h;
  this.array = array;
  this.last_sel = -1;
  this.last_val='';
  var win = window;
  if(typeof(win.pageXOffset) != 'undefined')
    var xobj = win.document.getElementsByTagName('body').item(0);
  else
    var xobj = (win.document.compatMode && win.document.compatMode == "CSS1Compat")?win.document.documentElement.getElementsByTagName('body').item(0):win.document.body || null;

  if (this.is_ie6)
  {
    this.IfrRef = document.createElement('iframe');
    this.IfrRef.src='test.html';
    this.IfrRef.scrolling = 'no';
    this.IfrRef.frameBorder = "0";
    this.IfrRef.style.cssText='position: absolute; display:none;background:#ffffff';
    xobj.appendChild(this.IfrRef);
  }

  if (typeof(this.def_width)=='undefined')
    this.def_width=(parseInt(this.input.offsetWidth)>40?parseInt(this.input.offsetWidth):100);

  this.div01 = document.createElement('div');
  this.div01.style.cssText='position: absolute; z-index: 1000; width: '+this.def_width+'px';
/*
  this.div01.style.position = 'absolute';
  this.div01.style.zIndex = '1';
  this.div01.style.width = this.def_width+'px';
  alert(this.def_width);
*/
  this.div02 = document.createElement('div');
  this.div02.style.cssText='position: absolute; display: none; z-index: 11000; border: 1px solid black; overflow: hidden; overflow-y: scroll';
  this.div02.style.height= parseInt(this.new_h)+'px';
  this.div02.style.width = (this.def_width-2)+'px';//ująć bordery parentow

  var tbl = document.createElement('table');
  tbl.style.width = (this.def_width-4)+'px';//ująć bordery parentow
  tbl.style.borderCollapse = 'collapse';

  this.table = document.createElement('tbody');

  tbl.appendChild(this.table);
  this.div02.appendChild(tbl);
  this.div01.appendChild(this.div02);
  xobj.appendChild(this.div01);

  this.input.setAttribute('AUTOCOMPLETE',"off");
  var self=this;

  this.set_def_width=function(w)
  {
    if (typeof(w)=='undefined')
      self.def_width=(parseInt(self.input.offsetWidth)>40?parseInt(self.input.offsetWidth):100);
    else
      self.def_width=(parseInt(w)>40?parseInt(w):100);
    self.div01.style.width=self.def_width+'px';
    self.div02.style.width=(self.def_width-2)+'px';
    self.table.parentNode.style.width = (self.def_width-4)+'px';
  }

  this.ajax_req= function(id)
  {
    var params=new Array();
    for (var i=0, n=self.ajax_get_params.length; i<n;i++)
    {
      if (document.getElementById(self.ajax_get_params[i].e_id)) 
        params[params.length]=self.ajax_get_params[i].name+'='+encodeURI(document.getElementById(self.ajax_get_params[i].e_id).value);
    }
    params[params.length]='fraza='+encodeURI(self.input.value.replace('+', '%2b'));
    if (typeof(id)!='undefined')
      params[params.length]='id='+encodeURI(id);
    self.ajax_obj.getDataNoXML(self.ajax_url+'&'+params.join('&'), function (data) {self.ajax_run(data, self); self.change();});
  }

  this.div02.onmousedown= function()
  {
    self.div02.setAttribute('focused', 1);
//    self.input.setAttribute('focused', 0);
    return true;
  }

  this.input.onchange = function()
  {
//     self.add_func(self, this);
  }


  var x_ob=this.input.onblur;
  if (typeof(x_ob)!=='function') x_ob=function (){};
  this.input.onblur = function () 
  {
	x_ob();
    var foc_dd=self.div02.getAttribute('focused');
    if (typeof(foc_dd)=='undefined'||foc_dd==null) foc_dd=0;

    var foc_ii=this.getAttribute('focused');
    if (typeof(foc_ii)=='undefined'||foc_ii==null) foc_ii=0;

    if (foc_dd==1)
      setTimeout(function() {self.input.focus()}, 50);
    else
    {
      self.change(27);
      this.setAttribute('focused', 0);
    }

    self.div02.setAttribute('focused', 0);
  }

  var x_of=this.input.onfocus;
  if (typeof(x_of)!=='function') x_of=function (){};
  this.input.onfocus = function () 
  {
    x_of();
    var foc_dd=self.div02.getAttribute('focused');
    if (typeof(foc_dd)=='undefined'||foc_dd==null) foc_dd=0;

    var foc_ii=this.getAttribute('focused');
    if (typeof(foc_ii)=='undefined'||foc_ii==null) foc_ii=0;
//    $('my_div').innerHTML+=foc_dd+' '+foc_ii+' - ';
    if (foc_ii==0 && foc_dd==0) self.change();

    this.setAttribute('focused', 1);
    self.div02.setAttribute('focused', 0);
  }

  this.k_press=function (kk)
  {
    if (self.minChars<=self.input.value.length&&self.ajax_url&&self.inp_val!=self.input.value)
      self.ajax_req();
    else if (self.minChars > self.input.value.length)
    {
//      self.add_func(self);
      self.change(27);
    }
    else
      self.change(kk);
  }

//  if (!(this.is_opera))
  {
    this.input.onkeyup = function (e) 
    { 
      if (typeof(e)!='undefined') event = e; 

      if (event.keyCode != 38 && event.keyCode != 40)
      {
        if ((event.keyCode>=65 && event.keyCode<=90)||
            (event.keyCode>=97 && event.keyCode<=122)|| 
            (event.keyCode==32)|| 
            (event.keyCode==8)|| 
            (event.keyCode==46)|| 
            (event.keyCode>=48 && event.keyCode<=57)
           )
          self.add_func(self);
        self.k_press(event.keyCode);
        self.inp_val=self.input.value;
      }
    }
  }

  if (this.is_ie)
  {
    this.input.onkeydown = function (e) 
    { 
      if (typeof(e)!='undefined') event = e; 
      if ((event.keyCode == 13 || event.keyCode == 27)&&self.div02.style.display!='none') return false;
      else if (event.keyCode == 9&&self.div02.style.display!='none') return false;
      else
      {
        self.k_press(event.keyCode);
        self.inp_val=self.input.value;
      }
    }
  }
  else if (this.is_opera)
  {
    this.input.onkeypress = function (e)
    {
      if (typeof(e)!='undefined') event = e; 
      if ((event.keyCode == 13 || event.keyCode == 27)&&self.div02.style.display!='none') return false;
      if (event.keyCode == 9&&self.div02.style.display!='none') return false;
      if (event.keyCode == 38 || event.keyCode == 40) self.change(event.keyCode);
//      if (event.keyCode == 9&&self.div02.style.display!='none') return false;
    }
  
  }
  else
  {
    this.input.onkeypress = function (e)
    {
      if (typeof(e)!='undefined') event = e; 

      if (event.keyCode == 38 || event.keyCode == 40)
        self.change(event.keyCode);
    }

    this.input.onkeydown = function (e) 
    { 
      if (typeof(e)!='undefined') event = e; 
      if ((event.keyCode == 13 || event.keyCode == 27)&&self.div02.style.display!='none') return false;
      if (event.keyCode == 9&&self.div02.style.display!='none') return false;
    }
  }

  this.change = function(e)
  {
//    alert(config.minChars+' '+self.input.value.length);
    if ((e == 13 || e == 27 || e == 9) || (e == 40 || e == 38))
    {
      var tds = self.table.getElementsByTagName('td');
      var x_id;
      if ((e == 13 || e == 9) && (self.div02.style.display=='' || self.div02.style.display=='block')&& self.last_sel!=-1)
      {
        self.input.value = tds[self.last_sel].getAttribute('val');
        x_id = tds[self.last_sel].getAttribute('id');
//        self.input.value = tds[self.last_sel].firstChild.data;
        self.div02.style.display = 'none';
        if (self.is_ie6) self.IfrRef.style.display = 'none';

        self.last_sel=-1;
      }
  
      if (e == 27 && (self.div02.style.display=='' || self.div02.style.display=='block'))
      {
        self.div02.style.display = 'none';
        if (self.is_ie6) self.IfrRef.style.display = 'none';

        self.last_sel=-1;
      }
  
      if (self.last_sel<0)
        var last_tmp=tds.length-1;
      else if (self.last_sel>tds.length-1)
        var last_tmp=0;
      else
        var last_tmp=self.last_sel;

      if (tds.length>0)
      {
        if (e == 40)
        {
          if (self.last_sel < tds.length-1)
          {
            self.last_sel++;
            var ile_m = Math.floor(self.new_h/parseInt(tds[0].offsetHeight));
            var scroll_ile=parseInt(self.div02.scrollTop)/parseInt(tds[0].offsetHeight);
     
            if(self.last_sel+1>scroll_ile+ile_m)
              self.div02.scrollTop = parseInt((self.last_sel+1-ile_m)*tds[0].offsetHeight);
          }
          else 
          {
            self.last_sel=0;
            self.div02.scrollTop = 0;
     
          }
        }
        else if (e == 38)
        {
          if (self.last_sel > 0)
          {
            self.last_sel--;
            var ile_m = Math.floor(self.new_h/parseInt(tds[0].offsetHeight));
            var scroll_ile=parseInt(self.div02.scrollTop)/parseInt(tds[0].offsetHeight);
     
            if(self.last_sel-scroll_ile<0)
              self.div02.scrollTop = parseInt((self.last_sel)*tds[0].offsetHeight);
          }
          else 
          {
            self.last_sel=tds.length-1;
            self.div02.scrollTop = tds.length*tds[0].offsetHeight;
          }
        }

        tds[last_tmp].className=self.CNs.def;

        if (self.last_sel>-1)
          tds[self.last_sel].className=self.CNs.sel;
/*      
        for (var i=0; i < tds.length; i++)
          tds[i].className = ((self.last_sel==i)?self.CNs.sel:self.CNs.def);
*/
      }

      if ((e == 13||e == 9)&&self.minChars<=self.input.value.length&&self.ajax_url)//&&self.inp_val!=self.input.value
      {
        if (typeof(self.on_select_func)=='function')
          self.on_select_func(x_id);
        //else
        //  self.ajax_req(x_id);
      }
    }
    else
    {
      self.div02.scrollTop=0;
      array_tmp = self.check_in_array(self.input.value);

      var nw=parseInt(self.div02.style.borderTopWidth);
      var nh=parseInt(self.div02.style.borderBottomWidth);

      nh=(isNaN(nh)?0:nh);
      nw=(isNaN(nw)?0:nw);

      var left=0;
      var top=0;
      var ee=self.input;
      while (ee.offsetParent)
      {
        left += ee.offsetLeft ;
        top  += ee.offsetTop  ;

        if (!(self.is_opera))
        {
          var add_left=parseInt(ee.clientLeft);
          var add_top=parseInt(ee.clientTop);
    	  if (!(isNaN(add_left))&&add_left>1) left += add_left;
	  if (!(isNaN(add_top))&&add_top>1)  top += add_top;
        }

        ee   = ee.offsetParent;
      }
      self.div01.style.left=left+'px';
      self.div01.style.top=top+self.input.offsetHeight+'px';

      
      if (array_tmp.length > 0 && self.minChars<=self.input.value.length)
      {
        var trs = self.table.getElementsByTagName('tr');
  
        for(var i=trs.length-1;i>=0;i--)
         self.table.removeChild(trs[i]);
  
        for (var i=0, n=array_tmp.length; i < n; i++)
        {
          var tr = document.createElement('tr');
          var td = document.createElement('td');
          td.className = self.CNs.def;
          td.innerHTML = array_tmp[i].n;
          td.setAttribute('val', array_tmp[i].v);
          if(typeof(array_tmp[i].id)!='undefined'&&array_tmp[i].id!=null)
            td.setAttribute('id', array_tmp[i].id);

          td.onclick = function () 
          {  
//             self.input.value = this.firstChild.data;
             self.input.value = this.getAttribute('val');
             var x_id = this.getAttribute('id');
             self.div01.style.display = 'none';

             if (self.is_ie6) self.IfrRef.style.display = 'none';
             if (self.minChars<=self.input.value.length&&self.ajax_url)//&&self.inp_val!=self.input.value
             {
               if (typeof(self.on_select_func)=='function')
                 self.on_select_func(x_id);
               //else
               //  self.ajax_req(x_id);
             }
          }

          td.onmouseover = function () 
          { 
            var tds = this.parentNode.parentNode.getElementsByTagName('td');
            for (var i=0; i < tds.length; i++)
            {
              tds[i].className = ((tds[i]==this)?self.CNs.sel:self.CNs.def);
              if (tds[i]==this) {self.last_sel = i; }
            }
          }

          td.onmouseout = function () {  }
          tr.appendChild(td);
          self.table.appendChild(tr);
        }
        self.last_sel=-1;
        self.div01.style.display = '';
        self.div02.style.display = '';
        
        if (self.is_ie6)
        {
          self.div02.style.display = "block";
  
          if (self.is_ie6)
          {
            self.IfrRef.style.width = self.div02.offsetWidth+'px';
            self.IfrRef.style.height = self.div02.offsetHeight+'px';
          }
          var left = 0;
          var top  = 0;
  
          var ee=self.div01;
          while (ee.offsetParent)
          {
            left += ee.offsetLeft ;
            top  += ee.offsetTop  ;
            ee   = ee.offsetParent;
          }
  
          if (self.is_ie6)
          {
            self.IfrRef.style.top = top+'px';
            self.IfrRef.style.left = left+'px';
            self.IfrRef.style.frameBorder='0px';
  
            self.IfrRef.style.zIndex = self.div01.style.zIndex - 1;
            self.IfrRef.style.display = "block";
          }
        }
      }
      else
      {
        self.div02.style.display = 'none';

        if (self.is_ie6)
          self.IfrRef.style.display = 'none';

        self.last_sel=-1;
      }

      if (self.table.getElementsByTagName('td').length>0)
        self.new_h=((array_tmp.length)*self.table.getElementsByTagName('td').item(0).offsetHeight)+nw+nh;

      if (self.h<self.new_h) self.new_h=self.h;

      self.div02.style.height= parseInt(self.new_h)+'px';
      if (self.is_ie6)
        self.IfrRef.style.height = parseInt(self.new_h)+'px';

      self.div02.scrollTop=0;
      self.last_val=self.input.value;

    }
  }


  this.check_in_array= function(str)
  {
    if (typeof(str)!='String') str=str.toString();
    var arr_tmp=new Array();
    var eq_cnt=0;
//    alert(self.array);
    if (str != '')
      for (var i=0,j=0; i < self.array.length; i++)
      {
        var eq=(self.trans_pl(self.array[i].val).toLowerCase()==self.trans_pl(str).toLowerCase());
        if (self.trans_pl(self.array[i].val.toString()).search(new RegExp(''+self.trans_pl(str)+'','i')) !== -1)
        {
          f_val = self.draw_func(self.array[i]);
          var pos = self.trans_pl(f_val).search(new RegExp('('+self.trans_pl(str)+')','i'));
          f_val = f_val.substring(0, pos)+"<b>"+f_val.substring(pos, str.length+pos)+"</b>"+f_val.substring(str.length+pos);
          var x_id;
          if (typeof(self.array[i].id)!='undefined') x_id=self.array[i].id;

          arr_tmp[j++] = {v:self.array[i].val.replace(' [+]', ''), n:f_val, id:x_id};
          if (eq) eq_cnt++;
//          arr_tmp[j++] = {v:self.array[i], n:self.trans_pl(self.array[i]).replace(new RegExp('^('+self.trans_pl(str)+')','i'), "<b>$1</b>")};
        }
      }
//    if (eq_cnt>1 || eq_cnt==0)
      return arr_tmp;
//    else
//      return new Array();
  }   

  this.trans_pl = function(str)
  {

    var diacritics =[
      /[\u00c0-\u00c6\u0100\u0102\u0104]/g, /[\u00e0-\u00e6\u0101\u0103\u0105]/g, // A, a
      /[\u00c8-\u00cb\u0112\u0114\u0116\u0118\u011A]/g, /[\u00e8-\u00eb\u0113\u0115\u0117\u0119\u011B]/g, // E, e
      /[\u00cc-\u00cf]/g, /[\u00ec-\u00ef]/g, // I, i
      /[\u00d2-\u00d6]/g, /[\u00f2-\u00f8]/g, // O, o
      /[\u00d9-\u00dc]/g, /[\u00f9-\u00fc]/g,  // U, u
      /[\u00d1\u0143\u0145\u0147\u014A]/g, /[\u00f1\u0144\u0146\u0148\u0149\u014B]/g, // N, n
      /[\u00c7\u0106\u0108\u010A\u010C]/g, /[\u00e7\u0107\u0109\u010B\u010D]/g, // C, c
      /[\u015A\u015C\u015E\u0160]/g, /[\u015B\u015D\u015F\u0161]/g, // S, s
      /[\u0141]/g, /[\u0142]/g, // L, l
      /[\u0179\u017B\u017D]/g, /[\u017A\u017C\u017E]/g // Z, z
    ];
//javascript: alert(pull_to.trans_pl('ŁódĽ'))
    var chars = ['A','a','E','e','I','i','O','o','U','u','N','n','C','c','S','s','L','l','Z','z'];

    for (var i = 0; i < diacritics.length; i++)
    {
      str = str.replace(diacritics[i],chars[i]);
    }
//    alert(str);
    return str;
  }
}



function replaceDiacritics(s)
{
  var s;

  var diacritics =[
    /[\300-\306]/g, /[\340-\346]/g, // A, a
    /[\310-\313]/g, /[\350-\353]/g, // E, e
    /[\314-\317]/g, /[\354-\357]/g, // I, i
    /[\322-\330]/g, /[\362-\370]/g, // O, o
    /[\331-\334]/g, /[\371-\374]/g,  // U, u
    /[\321]/g, /[\361]/g, // N, n
    /[\307]/g, /[\347]/g, // C, c
  ];

  var chars = ['A','a','E','e','I','i','O','o','U','u','N','n','C','c'];

  for (var i = 0; i < diacritics.length; i++)
  {
    s = s.replace(diacritics[i],chars[i]);
  }

  document.write(s);
}

