function manage_main_node(check) {

    var select = document.getElementById('mainNodeSelect');
    var id = check.id.split('_');
    var child = select.childNodes;

    id = id[1];

    if (check.checked)
    {
        var option = document.createElement('option');
        var label = document.getElementById('label_'+id);
        option.value=id;
        option.innerHTML=label.innerHTML;
        select.appendChild(option);
    } else {
        
        for(i=0; i<child.length; i++) {
            if (child[i].value == id) {
                select.removeChild(child[i]);
                break;
            }
        }
    }

    if (select.childNodes.length > 1) {
        select.style.display = 'block';
    } else {
        select.style.display = 'none';
    }

    for(i=0; i<child.length; i++) {
        if (child[i].value == main_parent_node_id) {
            child[i].selected = true;
            break;
        }
    }
}

// obiekt do zaznacznia
function check_sections(){
    // zaznaczone sekcje 
    this.sections = new Array();
    // dostepne sekcje
    this.avialibleSections = new Array();
    // limit sekcji
    this.sectionLimit = 1;
    this.errorMsg = '';

    // ustawienie kodu bledu
    this.setErrorMsg = function(str) {
        this.errorMsg = str;
    }

    // dodanie sekcji do listy
    this.addSection = function(sekcja) {
       if (this.sections.indexOf( sekcja ) < 0) {
            if (this.sections.length == 0) {
                this.sections = new Array(sekcja);
            } else {
                this.sections.push(sekcja);
            }
        }
    }

    // usuniecie sekcji z listy
    this.removeSection = function(section) {
        // usuniecie tylko w przypadku gdy nie ma juz zaznaczonych takich sekcji
        if (this.isSectionChecked(section) == false)
            {
            for (i = 0; i < this.sections.length; i++) {
                if (this.sections[i] == section) {
                    this.sections.splice(i,1);
                }
            }
        }
    }

    // ustawienie dostepnych sekcji
    this.setAvialibleSections = function(str) {
        this.avialibleSections = str.split(',');
    }

    // pierwsze ustawienie sekcji
    this.setSections = function(str) {
        this.sections = str.split(',');
    }
    // ustawienie limitu
    this.setSectionLimit = function(limit) {
        this.sectionLimit = limit;
    }

    // reakcja na onClick
    this.onClick = function(check) {
        // jezeli kliknieto w radiobutton - znaczy w mani node zmiana na checkboksa
        if (check.type == 'radio') {
            id = check.id.split('_');
            id = id[1];
            check = document.getElementById('node_'+id);
        }
        // pobranie id sekcji z nazwy klasy
        var section = check.className.split('_');
        section = section[1];
        // jezeli guzik jest zaznaczony
        if (check.checked) {
            this.addSection(section);
        } else if (check.checked === false) {
            this.removeSection(section);
        }
        // sprawdzenie i wylaczenie odpowiednich sekcji
        this.checkAndDisable();
    }
    // sprawdzenie czy dana sekcja jest jeszcze zaznaczona
    this.isSectionChecked = function(section) {
        var _inputs = document.getElementsByTagName('input');
        for (i = 0; i < _inputs.length; i++) {
            if (_inputs[i].className == 'section_'+section && _inputs[i].checked) {
                return true;
            }
        }
        return false;
    }

    // wlaczenie wszystkich sekcji
    this.enableSections = function() {
        var _inputs = document.getElementsByTagName('input');

        for (j = 0; j < this.avialibleSections.length; j++) {
            for (i = 0; i < _inputs.length; i++) {
                if (_inputs[i].className == 'section_'+this.avialibleSections[j]) {

                    id = _inputs[i].id.split('_');
                    id = id[1];

                    label = document.getElementById('label_'+id);
                    label.className = 'enabled';
                }
            }
        }
    }

    // sprawdzenie i wylaczenie
    this.checkAndDisable = function() {
        if (this.sections.length == this.sectionLimit) {
            // wlaczenie sekcji
            this.enableSections();
            // wylaczenie niepotrzebnych
            this.disableSections();
        } else if (this.sections.length > this.sectionLimit) {
                // alert z komunikatem
                alert(this.errorMsg+': '+this.sectionLimit);
        } else {
            this.enableSections();
        }
    }

    // wylacznie sekcji
    this.disableSections = function() {
        var _inputs = document.getElementsByTagName('input');
        var _avialibleSections = new Array();
        // kopia tablicy
        for (i = 0; i < this.avialibleSections.length; i++) {
            _avialibleSections.push(this.avialibleSections[i]);
        }

        for (i=0; i < this.sections.length; i++) {
            index = _avialibleSections.indexOf(this.sections[i]);
        if (index > -1) {
                _avialibleSections.splice(index,1);
            }
        }

        for (j = 0; j < _avialibleSections.length; j++) {
            for (i = 0; i < _inputs.length; i++) {
                if (_inputs[i].className == 'section_'+_avialibleSections[j]) {

                    id = _inputs[i].id.split('_');
                    id = id[1];

                    label = document.getElementById('label_'+id);
                    label.className = 'disabled';
                }
            }
        }
    }
    return this;
}

if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(elt /*, from*/)
  {
    var len = this.length;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++)
    {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
}