    function load_elfinder($id) {
        var root = this;
        $div = $('<div id="_elf_browser">elfinder</div>');
        $(this).after($div);
        $div.elfinder({
           url : '/_fm/elf/connectors/php/connector.php',
           lang : 'ru',
           docked: false,
           dialog : { width : 600, modal : true },
           editorCallback : function(url) {
              $(root).val(url);
              $div.remove();
           }
        });
        return false;
    }

    function onChangeAdr( el, dname ) {
        elnext = $(el).next();
        var isend = true;
        var arr = new Array();
        isend = (typeof $(elnext).val() === 'undefined');
        while (!isend) {
            arr.push(elnext);
            elnext = ($(elnext).next());
            isend = (typeof $(elnext).val() === 'undefined');
        }
        for (i = 0; i < arr.length; i++) {
            $(arr[i]).remove();
        }
        $(el).parent().append('<p id="wait"><i>Загружается, ждите...</i></p>');
        $.getJSON('/adr.php?id='+$(el).val()+'&dname='+dname, {}, function(json){
            $('#wait').remove();
            $(el).parent().append(json.aret);
        });
    }

    function changeIdent (el)
    {
        var tab = $(el).attr('data-table');
        var parent = $(el).attr('data-parent');
        var id = $(el).attr('data-id');

        if($(el).val()=='') {
            $(el).removeClass('_allow').addClass('_error');
            return false;
        }
        if($(el).val().search(/[^\w\d_\-]/)!=-1) {
            $(el).removeClass('_allow').addClass('_error');
            return false;
        }

        $.ajax({
            data: {
                controller: 'dbf_l',
                action: 'check_ident',
                id: id,
                tab: tab,
                fld: $(el).attr('name'),
                parent: parent,
                text: $(el).val()
            },
            success: function(data) {
                if (handleState(data)) {
                    if (data.allow=='ok') {
                        $(el).removeClass('_error').addClass('_allow');
                    } else {
                        $(el).removeClass('_allow').addClass('_error');
                    }
                }
            }
        });
    }

    function checkEmail(el) {
        if (!$(el).hasClass('_noempty') && $(el).val() == '') {
            $(el).removeClass('_error').removeClass('_allow');
            return true;
        }
        $.ajax({
            data: {
                controller: 'dbf_l',
                action: 'check_email',
                text: $(el).val()
            },
            success: function(data) {
                if (handleState(data)) {
                    if (data.allow=='ok') {
                        $(el).removeClass('_error').addClass('_allow');
                    } else {
                        $(el).removeClass('_allow').addClass('_error');
                    }
                }
            }
        });
    }

    function selectNode($input) {
        selectPageDialog.show(function (data) {
            $input.next('input').val(data.name + ' - ' + data.full_path);
            $input.val(data.id);
        });
    }

    $(function() {

        $('input.fb, textarea.fb').each(function () {
            $(this).attr('data-caption', $(this).val());
            $(this).focus(function() {
                if ($(this).val() == $(this).attr('data-caption')) {
                    $(this).val('');
                }
            });
            $(this).blur(function() {
                if ($(this).val() == '') {
                    $(this).val($(this).attr('data-caption'));
                }
            });
        });
        
        $('input._field_date').datepicker({
            autosize: true,
            gotoCurrent: true,
            showButtonPanel: true,
            changeMonth: true,
            changeYear: true
        });

        $('input._field_link').each(function () {
            var root = this;
            $(this).dblclick(function () {
                load_elfinder.call(this);
            });
            var $a = $('<a href="#">Загрузить</a>').click(function () {
                load_elfinder.call(root);
                return false;
            });
            $(this).after($a);
        });

        $('input._field_node_show').dblclick(function () {
            selectNode($(this).prev('input._field_node'));
        });

        $('a._field_node_select').click(function () {
            selectNode($(this).parent().find('input._field_node'));
            return false;
        });

        $('input._field_int').keypress(function (e) {
            if( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)) {
                return false;
            }
        });

        $('input._field_email').filter(function() { return $(this).hasClass('_noempty'); }).each(function () {
            checkEmail(this);
        });
        $('input._field_email').keyup(function () {
            checkEmail(this);
        }).change(function () {
            checkEmail(this);
        });

        $('input._field_ident').each(function () {
            changeIdent(this);
        }).keyup(function () {
            changeIdent(this);
        }).change(function () {
            changeIdent(this);
        });

        $('input._button_close').click(function () {
            window.close();
        });

        $('fieldset._fieldset > div:odd').addClass('_odd');

        $('form._entity').submit(function () {
            $(this).find('input.fb, textarea.fb').each(function () {
                if ($(this).val() == $(this).attr('data-caption')) {
                    $(this).val('');
                }
            });
            var ret = true;
            $(this).find('._error').each(function () {
                if (ret) {
                    alert('Неправильно заполнено поле "' + $(this).attr('title') + '"!');
                    $(this).focus();
                    ret = false;
                }
            });
            if (ret) {
                var root = this;
                $(this).find('._noempty').each(function () {
                    if ($(this).hasClass('fb')) {
                        if (ret && is_empty_str($(this).val())) {
                            alert('Поле "' + $(this).attr('data-caption') + '" не может быть пустым!');
                            $(root).find('input.fb, textarea.fb').each(function () {
                                if ($(this).val() == '') {
                                    $(this).val($(this).attr('data-caption'));
                                }
                            });
                            
                            $(this).focus();
                            ret = false;
                        }
                    }
                });
            }
            if (ret) {
                $(this).find('._noempty').each(function () {
                    if (ret && is_empty_str($(this).val())) {
                        alert('Поле "' + $(this).attr('title') + '" не может быть пустым!');
                        $(this).focus();
                        ret = false;
                    }
                });
            }
            return ret;
//            return false;
        });
    });
