/*
 * Display the error dialog box
 *
 * @param msg   Message to display
 */
function show_error_box(msg)
{
    tchui_error('msgbox_container', 'error_box', msg);
}

/*
 * Display the message dialog box
 *
 * @param msg   Message to display
 */
function show_message_box(msg)
{
    tchui_msg_dialog('msgbox_container', 'msg_box', 'Message', msg, 'OK');
}

/*
 * Display the confirmation dialog box
 *
 * @param title     Title of the dialog
 * @param msg       Message to display
 * @param callback  Name of the callback to call if YES is clicked
 */
function show_confirm_box(title, msg, callback)
{
    tchui_yes_no('msgbox_container', 'confirm_box', title, msg, callback);
}


function open_inline_editor (targetId, editorUrl)
{
    $.get(
        editorUrl,
        function(data, status)
        {
            if (status == 'success')
            {
                var targetSelector = '#' + targetId;

                $(targetSelector).load(editorUrl);
                __focus(targetSelector);
            }
            else
            {
                __show_ajax_error(status);
            }
        }
    );
    return false;
}

function restore_inline_view (targetId, targetUrl)
{
    $.get(
        targetUrl,
        function(data, status)
        {
            if (status == 'success')
            {
                var targetSelector = '#' + targetId;

                $(targetSelector).load(targetUrl);
                __remove_all_validator_errors ();
            }
            else
            {
                __show_ajax_error(status);
            }
        }
    );
    return false;
}

/**
 * Submit an inline editor
 */
function do_save_inline_editor(formId, submitUrl, targetId, targetUrl)
{
    var api = $("#" + formId).data("validator");
    var valid = api ? api.checkValidity() : true;

    if (valid)
    {
        $.ajax({
            url: submitUrl,
            cache: false,
            contentType: 'application/x-www-form-urlencoded',
            data: $("#" + formId).serialize(),
            processData: false,
            success: function (data, status)
            {
                if (status == 'success')
                {
                    $('#' + targetId).load(targetUrl)
                }
                else
                {
                    show_error_box (status);
                }
            }
        });
    }
}



/*=========== PRIVATE */
function __show_ajax_error(status)
{
    show_error_box(status);
}

function __focus (selector)
{
    $(selector + " input:visible:first").focus();
}

function __remove_all_validator_errors ()
{
    $('.error').each (function(index, item)
    {
        item.parentNode.removeChild(item);
    });
}




