
function pola_formularza(ob, dane) {
    
    this.obiekt = ob;
    this.dane   = dane;
    var self    = this;
    
    
    this.obiekt.addClass('pola_formularza');
    
    
    this.obiekt.html('Budynek:<select name="budynek"></select>Klatka:<select name="klatka"></select>Piętro<select name="pietro"></select>Mieszkanie<select name="numer"></select><input type="hidden" name="iden"/>');
    
    this.budynek = this.obiekt.children('select[name="budynek"]');
    this.klatka  = this.obiekt.children('select[name="klatka"]');
    this.pietro  = this.obiekt.children('select[name="pietro"]');
    this.numer   = this.obiekt.children('select[name="numer"]');
    this.iden    = this.obiekt.children('input[name="iden"]');
    
    
    this.budynek.change(function(){
        self.set_klatka();
        });
    
    
    this.klatka.change(function(){
        self.set_pietro();
        });
    
    
    this.pietro.change(function(){
        self.set_numer();
        });
    
    
    this.numer.change(function(){
        self.set_hidden();
        });
    
    
    this.set_budynek();
    }


//.................................................................................................


pola_formularza.prototype.set_budynek = function() {
    
    var opcje = {};
    
    var string = '<option value="-">-</option>';
    
    
    jQuery(this.dane).each(function(key, val){
        
        if (!opcje[val.budynek]) {
            
            opcje[val.budynek] = true;
            
            string += '<option value="' + val.budynek + '">' + val.budynek + '</option>';
            }
        });
    
    this.budynek.html(string);
    
    this.cleat_klatka();
    this.clear_pietro();
    this.clear_numer();
    }


//.................................................................................................


pola_formularza.prototype.set_klatka = function() {
    
    
    var budynek_opcja = this.budynek.val();



    var opcje = {};
    
    var string = '<option value="-">-</option>';
    
    
    jQuery(this.dane).each(function(key, val){
        
        if (val.budynek !== budynek_opcja) {
            return;
            }
        
        
        if (!opcje[val.klatka]) {
            
            opcje[val.klatka] = true;
            
            string += '<option value="' + val.klatka + '">' + val.klatka + '</option>';
            }
        });


    this.klatka.html(string);
    
    
    this.clear_pietro();
    this.clear_numer();
    };


//.................................................................................................


pola_formularza.prototype.set_pietro = function() {
    
    var budynek_opcja = this.budynek.val();
    var klatka_opcja  = this.klatka.val();
    

    var opcje = {};
    
    var string = '<option value="-">-</option>';
    
    
    jQuery(this.dane).each(function(key, val){
        
        if (val.budynek !== budynek_opcja || val.klatka !== klatka_opcja) {
            return;
            }
        
        
        if (!opcje[val.pietro]) {
            
            opcje[val.pietro] = true;
            
            string += '<option value="' + val.pietro + '">' + val.pietro + '</option>';
            }
        });


    this.pietro.html(string);
    
    this.clear_numer();
    }


//.................................................................................................


pola_formularza.prototype.set_numer = function() {
    

    var budynek_opcja = this.budynek.val();
    var klatka_opcja  = this.klatka.val();
    var pietro_opcja  = this.pietro.val();


    var opcje = {};
    
    var string = '<option value="-">-</option>';
    
    
    jQuery(this.dane).each(function(key, val){
        
        if (val.budynek !== budynek_opcja || val.klatka !== klatka_opcja || val.pietro !== pietro_opcja) {
            return;
            }
        
        if (!opcje[val.numer]) {
            
            opcje[val.numer] = true;
            
            string += '<option value="' + val.numer + '">' + val.numer + '</option>';
            }
        });

    
    this.numer.html(string);
    };

//.................................................................................................

pola_formularza.prototype.set_hidden = function() {
    
    var budynek_opcja = this.budynek.val();
    var klatka_opcja  = this.klatka.val();
    var pietro_opcja  = this.pietro.val();
    var numer_opcja   = this.numer.val();
    
    var self = this;
    
    jQuery(this.dane).each(function(key, val){
        
        if (val.budynek !== budynek_opcja || val.klatka !== klatka_opcja || val.pietro !== pietro_opcja) {
            return;
            }
        
        self.iden.val(val.iden);
        });
    }

//.................................................................................................


pola_formularza.prototype.cleat_klatka = function() {
    
    this.klatka.html('');
    };


//.................................................................................................


pola_formularza.prototype.clear_pietro = function() {
    
    this.pietro.html('');
    };


//.................................................................................................


pola_formularza.prototype.clear_numer = function() {

    this.numer.html('');
    };



/*
<SELECT NAME="partnumber">
<OPTION VALUE="7382"          >steam turbine
<OPTION VALUE="2928"          >resistor array
<OPTION VALUE="3993" SELECTED >widget analyzer
<OPTION VALUE="9398"          >fiber identifier
</SELECT>
*/

