var asyncSubmitIndicatorClass = "async-submit-modal";

var interval = null;

function displayAsyncActionStatus(target, context)
{
    
    if(context.failed)
    {
        $(target).append("<div id=\"async-err-div\"></div>");
        $("#async-err-div").showError(context.errorMessage);
        clearInterval(interval);
    }
    else
    {
        $(target).empty();
        if(context.statusTextLines.length > 10)
        {
            $(target).css("overflow","scroll");
            $(target).css("height","300px");
        }
        for (var i = 0; i < context.statusTextLines.length; i++)
        {
            $(target).append("<p>" + context.statusTextLines[i] + "</p>");
        }
        if(context.finished)
        {
            $(target).append("<p><strong>FINISHED</strong></p>");
            $(target).append("<p>You may now close the window.</p>");
            clearInterval(interval);
            $(target).dialog( { close : function(event, ui)
            {
                $(this).empty();
                document.location.reload();
            }});
        }
    }
}

function updateAsyncActionStatusFromServer(target, actionId)
{
    $.ajax({
        url: 'async_action_exec_status_ajax.do',
        data : 'actionId=' + actionId,
        type : 'POST',
        cache: false,
        success: function(context)
        {
            displayAsyncActionStatus($(target), context)
        },
        error: function(XMLHttpRequest, textStatus, errorThrown)
        {
            $(target).showError(errorThrown);
        }
    });
}

/**
 * Ajax function submits for to inline page element (div)
 * @param form
 * @param target
 */
function ajaxSumbitFormForAsyncActionInline(form, target)
{
    $(target).showProgress();
    $.ajax({
        url: form.action,
        data : $(form).serialize() + "&ignore_enc_filter=true",
        type : 'POST',
        cache: false,
        success: function(data)
        {
            $(target).empty();
            if(data.length)
            {
                $(target).append(data);
            }
            else
            {
                displayAsyncActionStatus(target, data);
                clearInterval(interval);
                interval = setInterval("updateAsyncActionStatusFromServer('" + target + "', '" + data.actionId + "');", 1000);
            }
        },
        error: function(XMLHttpRequest, textStatus, errorThrown)
        {
            $(target).empty();
            $(target).showError(errorThrown);
        }
    });
}
