
var HelpList_All = new Array();
var HelpList_in_Action = false;
var HelpList_Type = 1; // 2-es: illeszkedést a szó elejétől nézi, 1-es: illeszkedést szöveg egészére
var HelpList_Type2 = 3; // leékérdezés típusa, mod_kereses/kereses.php
var HelpList_Akt = 0;
var HelpList_Active = false;
var HelpList_found = new Array();
// var HelpList_inputid = 'spl_search';
var HelpList_inputid = 'search1';
var HelpList_ibefore = 'inputsearch';
var HelpList_helper = 'helper';

var mezoid = '';

function Load_HelpList(val, e, k, table, field, tblname)
{
  // HelpList_Type = $('searchType').value;
  
  ajaxRequest = new Ajax.Request(
    serverurl + '/functions/ajaxfunc.php?table='+table+'&field='+field,
    {
      method: 'get',
      parameters: 'funcid=search&type=' + HelpList_Type2,
      onComplete: function(request) 
      { 
        HelpList_All.clear();
        HelpList_All = request.responseText.split(";");
        // alert('val :: ' + val + ' - ' + e + ' - ' + k + ' len: ' + HelpList_All.length);
        Help_List(val, e, k, table, field, tblname);
        // $('testID').innerHTML = request.responseText;
      }
    }
  )    
}

function Help_List(othis, e, k, table, field, tblname)
{
  //alert(table);
  mezoid = tblname;
  if(!k)k=1;
  if(HelpList_Active==true){
    HelpList_Active = false;
  }
  else{
  // alert(HelpList_All.length);
  // if((HelpList_All.length < 1 && !HelpList_in_Action) || HelpList_Type != $('searchType').value){
  if((HelpList_All.length < 1 && !HelpList_in_Action))
  {
    // alert(HelpList_Active);
    HelpList_in_Action = true;
    Load_HelpList(othis, e, k, table, field,tblname);
    var a = new Element('div', { id:HelpList_helper, 'class': 'helper', 'style':'text-align:left;top:22px;' }).update("");
    new Insertion.After(HelpList_ibefore, a);
  }
  else if(HelpList_All.length > 0){
    HelpList_Active = true;    
    var founded = false;
    var teszt = '';
    
    if(othis.value.length > 0){
      counter=1;
      // alert(HelpList_Type);
      if(HelpList_Type == 1)
      {
        pattern1 = new RegExp('(#&&#)', "gi");
        pattern2 = new RegExp('(#&#)', "gi");
        pattern = new RegExp('(' + othis.value + ')', "gi");        
        var matched = ''
        var aktmatched = '';
        for(m=0;m<HelpList_All.length;m++)
        {        
          // if( m < 5 )
          // {
            // alert(HelpList_All[m]);
          // }
          if(aktmatched = HelpList_All[m].match( pattern ))
          {
            founded = true;
            HelpList_found[counter-1] = HelpList_All[m];
            last = '';
            matched = HelpList_All[m];
            for(b=0;b<aktmatched.length;b++)
            {
              if(!last.include(aktmatched[b]))
              {
                last = last + ';' + aktmatched[b];
                matched = matched.replace(aktmatched[b], '#&&#' + aktmatched[b] + '#&#');
              }
            }
            matched = matched.replace(pattern1, '<font class="HelpList_found">');
            matched = matched.replace(pattern2, '</font>');
            if(counter < 11)
            {
              teszt = teszt + '<div id="_' + counter + '" class="hl_off" onmouseover="Help_List_Which(this);"><a class="help_list" href="javascript:void(0)" onclick="Set_Value_HelpList(\'' + HelpList_inputid + '\', \'' + (counter-1) + '\', HelpList_helper);" style="text-decoration:none;">' + matched + '</a></div>';
            }
            counter++;        
          }      
        }
        // alert(founded);
        
      }
      else if(HelpList_Type==2){
        // HelpList_All.grep('(' + othis.value + ')').each(function(s) {
        HelpList_All.grep('(' + othis.value + ')').each(function(s) {
          founded = true;
          var Alls = s.split(" ");
          var FullAlls = new Array();
          var RemoveCounter = new Array();
          pattern = new RegExp('(^' + othis.value + ')', "gi");
          
          var matched = ''
          var aktmatched = '';
          for(m=0;m<Alls.length;m++)
          {    
            
            if(aktmatched = Alls[m].match( pattern )){
              conti = true;
              if(Alls[m].include('\n')){
                first = Alls[m].split('\r\n');
                Alls[m] = first[0];
                conti = false;
              }
              matched = Alls[m].replace(aktmatched[0], '<font class="HelpList_found">' + aktmatched[0] + '</font>');
              
              if(conti && Alls[m+1] && !Alls[m+1].include('\r\n'))
                matched = matched + ' ' + Alls[m+1] + (Alls[m+2] ? '...' : '');
              else conti = false;
              
              HelpList_found[counter-1] = (Alls[m]+((Alls[m+1] && conti) ? ' '+Alls[m+1] : ''));
              if(counter < 11)
                teszt = teszt + '<div id="_' + counter + '" class="hl_off" onmouseover="Help_List_Which(this);"><a class="help_list" href="javascript:void(0)" onclick="Set_Value_HelpList(\'' + HelpList_inputid + '\', \'' + (counter-1) + '\', HelpList_helper);" style="text-decoration:none;">' + matched + '</a></div>';
              counter++;
            }            
          }
                
        });
      }
    }
    
    if(founded)
    {  
      if($(HelpList_helper))
      {
      
        // alert('Ok: A [helper] azonosító létezik!');
        // alert(teszt);
        $(HelpList_helper).innerHTML = teszt + '<div class="HelpList_close"><a href="javascript:void(0)" onclick="$(\'helper\').style.display = \'none\';">' + HelpList_close + '</a></div>';
        // $('helper').innerHTML = '<div class="HelpList_close"><a href="javascript:void(0)" onclick="$(\'helper\').style.display = \'none\';">' + HelpList_close + '</a></div>';
        $(HelpList_helper).style.display = '';
      }
      else
      {
        alert('Error: A [helper] azonosító nem létezik!');
      }
      if(k==1 || k==2){
        var ktst = 2;
        if(/Opera/gi.test(navigator.userAgent)){
          ktst = 1;
        }

        mKcode = (e.keyCode ? e.keyCode : e.which);
        
        counter=11;
        if(mKcode == 38 && HelpList_Akt>0){
          // if($('_' + HelpList_Akt))$('_' + HelpList_Akt).className = 'hl_off';
        
          //if(k==1)   --> kiveve 20090807
            HelpList_Akt = parseInt(HelpList_Akt-1);
          if($('_' + HelpList_Akt)) $('_' + HelpList_Akt).className = 'hl_on';
          // alert(HelpList_Akt + ' - ' + mKcode);
        }
        else if(mKcode == 40 && HelpList_Akt<counter-1 && HelpList_Akt>=0){
          // if($('_' + HelpList_Akt))$('_' + HelpList_Akt).className = 'hl_off';
          if(k==ktst)HelpList_Akt = parseInt(HelpList_Akt+1);
          if($('_' + HelpList_Akt))$('_' + HelpList_Akt).className = 'hl_on';
        }
        else if(mKcode == 13 && HelpList_Akt<=counter && HelpList_Akt>0){
          Set_Value_HelpList(HelpList_inputid, (HelpList_Akt-1), HelpList_helper);  
          return false;
        }
        else{HelpList_Akt=0;}
        // alert(HelpList_Akt + ' - ' + k);
      }  
      // if($('testID'))$('testID').innerHTML = teszt + ("teszt, teszt, teszt").replace(/es/g, "F");

    }
    else{
      if($(HelpList_helper))$(HelpList_helper).style.display = 'none';
    }
    HelpList_Active = false;
    // HelpList_All.grep(/othis.value/);
  }
  }
}
function Help_List_Which(othis){
  if(HelpList_Akt > 0)$('_' + HelpList_Akt).className = 'hl_off';
  HelpList_Akt = parseInt(othis.id.substring(1));
}
function Set_Value_HelpList(id, m, hide){
  HelpList_Akt = 0;
  HelpList_Active = false;
  if($(id))$(id).value = (HelpList_found[m]?HelpList_found[m]:'');
  
  if($(mezoid))$(mezoid).value = (HelpList_found[m]?HelpList_found[m]:'');
  if($(hide))$(hide).style.display = 'none';
  //submitThis('searchForm');
}
function submitThis(form){
  $(form).submit();
}

