function toggle_selection(input) {
    checked = input.checked;
    $(input.form).find('input[type=checkbox]').attr('checked', checked);
}

function async_post_form(button, element) {
    // Inicializuj, najdi hodnoty vo formulari.
    if (button instanceof HTMLFormElement) {
        form = button;
        button = null;
    } else {
        button.disabled = true;
        form = button.form;
    }

    xajax.config.requestURI = form.action;
    form_name = form.getAttribute("name");
    post_values = xajax.getFormValues(form_name);

    // Zrus tlacitka.
    $(form).find('input[type=submit]').each(function() {
            post_values[this.name] = undefined;
    });

    // Pridaj stlacene.
    if (button) {
        post_values[button.name] = button.value;
    }

    // Vykonaj dotaz.
    ret_val = xajax_asynchronous_process(element, post_values);

    return ret_val;
}

function sync_post_form(button, element) {
    xajax.config.defaultMode = 'synchronous';
    ret_val = async_post_form(button, element);
    xajax.config.defaultMode = 'asynchronous';
    return ret_val;
}

function ask_question(question, ajax_element, url_relative) {
    if (!confirm(question)) {
        return false;
    }

    if (ajax_element != null) {
        return async_request(url_relative, ajax_element);
    } else {
        return true;
    }
}

function ask_question_submit(question, button, ajax_element) {
     if (!confirm(question)) {
         return false;
     }

     if (ajax_element != null) {
         return async_post_form(button, ajax_element);
     } else {
         return true;
     }
}

function dialog_close(element) {
    window.atteq.dialog.closing = 1;
    $("#" + element).dialog('close');
    window.atteq.dialog.closing = 0;
}

function dialog_show(url, element) {
    // Synchronny dotaz.
    xajax.config.defaultMode = 'synchronous';
    if (async_request(url, element)) {
        return true;
    }
    xajax.config.defaultMode = 'asynchronous';

    $('#' + element).dialog('open');
    return false;
}

function dialog_init(caption, element, close_callback) {
    $(document).ready(function () {
        $('#' + element).dialog({
            autoOpen: false,
            title: caption,
            modal: true,
            minWidth: 800,
            width: 800,
            close: function (event) {
                if (event.cancelable === undefined) {
                    event.cancelable = false;
                }
                close_callback(event.cancelable, element);

                // Editor zatvorime.
                close_dialog_editor();
            }
        });
    });
}

function dialog_close_click_cancel_button(cancel_button, element) {
    if (!window.atteq.dialog.closing && cancel_button != null) {
        $('#' + element + " input[name=" + cancel_button + "]").click();
    }
}

function dialog_button_clicked(parent, dialog, button) {
    // Update pre obsah pripadneho editora.
    update_dialog_editor();
    // Zatvor editor, nech mame priestor pre novy.
    close_dialog_editor();

    // Synchronny dotaz.
    if (sync_post_form(button, parent + "|" + dialog)) {
        return true;
    }

    // Test navratovej hodnoty.
    if (!window.atteq.xajax.return_value) {
        dialog_close(dialog);
    } else {
        refresh_dialog_editor();
    }

    // V kazdom pripade, vratime false, aby sa nevykonal standardny dotaz.
    return false;
}

function create_editor(element, config) {
    return CKEDITOR.replace(element, config);
}

function create_dialog_editor(element, config, standalone_editor) {
    close_dialog_editor();
    editor = create_editor(element, config);

    if (!standalone_editor) {
        window.atteq.editor.instance = editor;
        window.atteq.editor.element = element;
        window.atteq.editor.config = config;
    }
}

function update_dialog_editor() {
    if (window.atteq.editor.instance) {
        window.atteq.editor.instance.updateElement();
    }
}

function refresh_dialog_editor() {
    create_dialog_editor(window.atteq.editor.element, window.atteq.editor.config);
}

function close_dialog_editor() {
    if (window.atteq.editor.instance) {
        try {
            window.atteq.editor.instance.destroy();
        } catch (e) {
        } finally {
            window.atteq.editor.instance = null;
        }
    }
}

