﻿var SFTimeOut = false;
var KeyID = 0;

document.onkeydown = KeyCheck;       

function KeyCheck(e) {
   KeyID = (window.event) ? event.keyCode : e.keyCode;
   
   if (KeyID == 13) {
      if ($('jsDropDownBox').visible()) {
        CloseEngine();
      } else {
        __doPostBack('ctl00$cntKalkulatory$WebWyszukiwarkaAdv$wysz_submit','');
      }
   }
}

function AutoCompleteEngine(input, opt, par, type) {
  if(KeyID >46 || KeyID==8 ) {
      if(SFTimeOut) clearTimeout(SFTimeOut);
    
      if (!(input.value.length < 3)) {
        SFTimeOut = setTimeout("CallEngine('"+input.id+"','"+input.value+"','"+opt+"','"+par+"','"+type+"')",300);
      } else {
         CloseEngine();
      }
  }
}

function CallEngine(name, value, opt, par, type) {
    $('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 - 9 + 'px';
    $('jsDropDownBox').style.left = posX - $('wrapper').offsetLeft + 1 + 'px';
    
    $('jsDropDownBox_BtnClose').style.top = posY - 4 + 'px';
    $('jsDropDownBox_BtnClose').style.left = posX - $('wrapper').offsetLeft + $(name).offsetWidth - 59 + 1 + 'px';
    $('jsDropDownBox_BtnClose').style.height = $(name).offsetHeight - 3 + 'px';
    
    var result = "";
    var TablicaDanych = new Array();
    
    var CiagDanych = "";
    
    var par_value = "";
       
    if ($$('.'+par) != null) {
        switch (type) {
            case 'list':
                    if (!$$('.'+par)[0].value == "---")
                        par_value = $$('.'+par)[0].options[$$('.'+par)[0].selectedIndex].text;
                        
                break;
            case 'input':
                par_value = $$('.'+par)[0].value;
                
                break;
            default:
        }
    }
    
    var url = 'virgolisty.ashx?typ=' + opt + '&prefix=' + value + '&parent=' + par_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++)
                            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';
            } else {
                $('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();
}

function wysz_zaawansowane_click() {
    $('ZaawansowaneOpcjeWyszukiwania').toggle();
}

function GMF_Toggle(name) {
    $('gmf_' + name + 'Box').toggle();
    
    if (($('gmf_' + name + 'Box').getStyle('display') == "block") || (($('gmf_' + name + 'Box').getStyle('display') == ""))) {
        $('GMF_RowDzielnice').style.height = 48 + 'px';
        $('GMF_RowDzielnice').firstDescendant().style.height = 46 + 'px';
        
        $('GMF_RowLokalizacja').firstDescendant().value = name;
    } else {
            $('GMF_RowDzielnice').style.height = 19 + 'px';
            $('GMF_RowDzielnice').firstDescendant().style.height = 18 + 'px';
            
            $('GMF_RowLokalizacja').firstDescendant().value = "";
    }
}

var GMF_Dzielnice = "";

function GMF_Process(name) {
    name = GAL_Decoder(name);
    
    if (GMF_Dzielnice.indexOf(name) < 0) {
        GMF_Dzielnice += name + ",";
    } else {
        GMF_Dzielnice = GMF_Dzielnice.replace(name + ",", "");
    }
    
    $('GMF_RowDzielnice').firstDescendant().value = GMF_Dzielnice;
    
    $('GMF_RowLokalizacja').firstDescendant().value = "Warszawa";
}

function GAL_Decoder(tekst) {

    tekst = tekst.replace("_", " ");
    
    tekst = tekst.gsub("#1#", "ę");
    tekst = tekst.gsub("#2#", "ą");
    tekst = tekst.gsub("#3#", "ó");
    tekst = tekst.gsub("#4#", "ś");
    tekst = tekst.gsub("#5#", "ć");
    tekst = tekst.gsub("#6#", "ź");
    tekst = tekst.gsub("#7#", "ż");
    tekst = tekst.gsub("#8#", "ń");
    tekst = tekst.gsub("#9#", "ł");
    
    tekst = tekst.gsub("#01#", "Ę");
    tekst = tekst.gsub("#02#", "Ą");
    tekst = tekst.gsub("#03#", "Ó");
    tekst = tekst.gsub("#04#", "Ś");
    tekst = tekst.gsub("#05#", "Ć");
    tekst = tekst.gsub("#06#", "Ź");
    tekst = tekst.gsub("#07#", "Ż");
    tekst = tekst.gsub("#08#", "Ń");
    tekst = tekst.gsub("#09#", "Ł");

    return tekst;
}