﻿qBlokadaFocusa_d = false;
qInputFocused_d = false;
qBlokadaOnChange_d = false;
qTimeout_d = 0;
timer_d = null;

function qDzielniceKeyUp(event) {
    var element = event.element();

    var key = getEventKeyCode(event);
    
    var execute = false;

    if (key == 40) {
        qBlokadaFocusa_d = true;
        $('DzielniceSelect').focus();
        $('DzielniceSelect').options[0].selected = true;
    } else if (key == 39) {
        if (getSelectionStart(element) == element.value.length) {
            if ($('DzielniceSelect').options.length > 0 && ($('DzielniceBox').getStyle('display') == 'block')) {
                var firstValue = $('DzielniceSelect').options[0].innerHTML;
                
                if (firstValue.lastIndexOf("/") == -1) {
                    element.value = firstValue;
                } else {
                    var rest = firstValue.substr(0, firstValue.lastIndexOf("/"));

                    element.value = firstValue.substr(firstValue.lastIndexOf("/")+1, firstValue.length-1);
                    
                    if (rest.indexOf("/") == -1) {
                        element.value = rest + ", " + element.value;
                    } else {
                        element.value = rest.substr(0,rest.indexOf("/")) + ", " + element.value;
                    }
                }
            }
            
            execute = true;
        }
    } else if ((key >= 65 && key <= 90)||(key >= 97 && key <= 122)||(key==8)||(key==32)) {
        if (qBlokadaFocusa_d) {
            qBlokadaFocusa_d = false;
            return;
        }
        
        execute = true;   
    }
    
    if (execute) {
        qTimeout_d = 500;
        if (timer_d) clearTimeout(timer_d);
        timer_d = setTimeout(ExecuteSearch_Dzielnice, qTimeout_d);
    }
}

function ExecuteSearch_Dzielnice() {
    var inp = $('txtDzielnice');
        
    var result = "";
    var TablicaDanych = new Array();
    
    var CiagDanych = "";
    
    var parent = "";
    var prefix = inp.value;
    
    var woj = $$('.lstWojewodztwa')[0].options[$$('.lstWojewodztwa')[0].selectedIndex].value;
    
    if (inp.value.indexOf(",") > -1) {
        parent = inp.value.substr(0, inp.value.indexOf(","));
        prefix = inp.value.substr(inp.value.indexOf(",")+1, inp.value.length-1);
    }
    
    var start_count = -1;
    
    if (GetLocationsIDS().length == 0) start_count = 2;
    
    if (prefix.length > start_count || parent.length > 4) {
        var url = 'virgolisty.ashx?typ=dzielnice_rejony&prefix='+trim(prefix, " ")+"&parent="+trim(parent, " ")+"&wojewodztwo="+woj+"&lok_ids="+GetLocationsIDS();

        new Ajax.Request(url, {
          method: 'get',
          onSuccess: function(transport) {
                eval(transport.responseText);
                
                CiagDanych = Ciagdzielnice_rejony;
                
                $('DzielniceSelect').options.length = 0;

                if ((CiagDanych != null) && (CiagDanych.length > 0)) {
                    TablicaDanych = CiagDanych.split(",");

                    for (var i=0; i<TablicaDanych.length; i++)
                        $('DzielniceSelect').options[i] = new Option(TablicaDanych[i].split(';')[1].split('/').join(' - '), TablicaDanych[i].split(';')[0]); 
                
                    if ($('DzielniceBox').getStyle('display') == 'none') {
                        $('DzielniceBox').show();
                    }
                    
                    RegisterEvent($('DzielniceSelect'), "keydown", respondToKeyDown_d);                      
                    RegisterEvent($('DzielniceSelect'), "change", respondToChange_d);
                    RegisterEvent($('DzielniceSelect'), "blur", sel_blur_d);
                    
                    qInputFocused = true;
                    $('txtDzielnice').focus();
                } else {
                    if ($('DzielniceBox').getStyle('display') == 'block') {
                        $('DzielniceBox').hide();
                    }
                }
           }
        });
    } else {
        $('DzielniceBox').hide();
    }
}

function inp_focus_d(event) {
    qInputFocused_d = true;
    
    if ($('txtDzielnice').value == static_text_d) $('txtDzielnice').value = '';
    
    if (GetLocationsIDS().length > 0) {
        qTimeout_d = 500;
        if (timer_d) clearTimeout(timer_d);
        timer_d = setTimeout(ExecuteSearch_Dzielnice, qTimeout_d);
    }
}

function inp_blur_d(event) {
    qInputFocused_d = false;
    
    if (!qBlokadaFocusa_d) {
        if ($('DzielniceSelect').selectedIndex == -1) $('DzielniceBox').hide();
    }
    
    qBlokadaFocusa_d = false;
    
    if ($('txtDzielnice').value == '') $('txtDzielnice').value = static_text_d;
}

function respondToKeyDown_d(event) {
  var element = getEventTarget(event);
  
  var key = getEventKeyCode(event);
  
  qBlokadaOnChange_d = true;

  if (key == 13) {
    var sel = $('DzielniceSelect');

    if (sel.selectedIndex != -1) {
        AddElement(sel.options[sel.selectedIndex]);
        
        element.options[element.selectedIndex] = null;

        $('DzielniceBox').hide();
        
        qBlokadaFocusa_d = true;
        
        $('txtDzielnice').select();
        
        qBlokadaOnChange_d = false;
    }
  }
  
  if (key == 38) {
        if ($('DzielniceSelect').selectedIndex == 0) {
            qInputFocused_d = true;
            $('txtDzielnice').focus();
            
            $$('#DzielniceSelect option').each(function(elem){
                  elem.selected = false;
            });
        }
    }
}

function respondToChange_d(event) {
    if(!qBlokadaOnChange_d) {
        var element = getEventTarget(event);

        AddElement(element.options[element.selectedIndex]);
        
        element.options[element.selectedIndex] = null;
           
        if (GetLocationsIDS().length == 0 || element.options.length == 0) {
            $('DzielniceBox').hide();
        }
        
        if (GetLocationsIDS().length == 0) {
            $('txtDzielnice').select();
        }
    }
}

function sel_blur_d(event) {
    $('DzielniceBox').hide();
    
    if (GetLocationsIDS().length == 0) {
        $('txtDzielnice').select();
    }
}