﻿var SFTimeOut = false;
var KeyID = 0;

document.onkeydown = KeyCheck;       

function KeyCheck(e) {
   KeyID = (window.event) ? event.keyCode : e.keyCode;
}

function AutoCompleteEngine(input, opt) {
  if(KeyID >46 || KeyID==8 ) {
      if(SFTimeOut) clearTimeout(SFTimeOut);
       
      SFTimeOut = setTimeout("CallEngine('"+input.id+"','"+input.value+"','" + opt + "')",300);
  }
}

function CallEngine(name, value, opt) {
    $('jsDropDownBox').hide();
    $('jsDropDownBox_BtnClose').hide();

    var posX = $(name).offsetLeft;
    var posY = $(name).offsetTop;
    var parent=$(name).offsetParent;
                                                     
    while(parent != null)
    {
        posX += parent.offsetLeft;
        posY += parent.offsetTop;
        parent = parent.offsetParent;
    }
                
    // Etap 1
    $('jsDropDownBox').style.top  = posY + $(name).offsetHeight/2 + 8 + 'px';
    $('jsDropDownBox').style.left = posX - $('wrapper').offsetLeft + 'px';
    
    $('jsDropDownBox_BtnClose').style.top = posY - $(name).offsetHeight/2 + 10 + 'px';
    $('jsDropDownBox_BtnClose').style.left = posX - $('wrapper').offsetLeft + $(name).offsetWidth - 25 + 1 + 'px';
    $('jsDropDownBox_BtnClose').style.height = $(name).offsetHeight - 3 + 'px';
    
    var result = "";
    var TablicaDanych = new Array();
    
    var CiagDanych = "";
    
    var url = 'virgolisty.ashx?typ=' + opt + '&prefix=' + value;
    
    new Ajax.Request(url, {
      method: 'get',
      onSuccess: function(transport) {
            eval(transport.responseText);
    
            switch (opt) {
                case "miasta": 
                    CiagDanych = Ciagmiasta;
                    break;
                case "dzielnice": 
                    CiagDanych = Ciagdzielnice;
                    break;
                case "ulice":
                    CiagDanych = Ciagulice;
                    break; 
            }

            if ((CiagDanych != null) && (CiagDanych.length > 0)) {
                TablicaDanych = CiagDanych.split(",");
                
                for (var i=0; i<TablicaDanych.length; i++)
                    if (TablicaDanych[i].toLocaleLowerCase().startsWith(value.toLocaleLowerCase()))
                        result += "<tr><td onclick=\"$(\'" + name + "\').value=\'" + TablicaDanych[i] + "\';CloseEngine();\">" + TablicaDanych[i] + "</td></tr>";
                
                $('jsDropDownBox').innerHTML = "<table id=\"jsDropDownBox_Inner\">" + result + "</table>";
                $('jsDropDownBox').show();
                $('jsDropDownBox_BtnClose').show();
            }
             
            // Etap 2                                                          
            if($('jsDropDownBox_Inner').offsetHeight > 200) {
                  $('jsDropDownBox').style.overflow = 'auto';
                  $('jsDropDownBox').style.height = 200+'px';
            } else
                  $('jsDropDownBox').style.height = $('jsDropDownBox_Inner').offsetHeight + 'px';
            
            // Etap 3                                                                                                                                                                                                  
            if( $('jsDropDownBox').offsetWidth < $(name).offsetWidth ) {    
                 $('jsDropDownBox').style.width = $(name).offsetWidth - 2 + 'px';
                 $('jsDropDownBox_Inner').style.width = $(name).offsetWidth - 2 + 'px';
            }    
                                                                        
            // Etap 4
            if( $('jsDropDownBox_Inner').offsetWidth < $('jsDropDownBox').offsetWidth )
                $('jsDropDownBox_Inner').style.width = $('jsDropDownBox').clientWidth + 'px';
            
            SFTimeOut = false;
        }
    });
}

function CloseEngine() {
    $('jsDropDownBox').hide();
    $('jsDropDownBox_BtnClose').hide();
}
