/*global $, waitingDialog, gettext */
var async_task_id = null;
var async_task_timeout = null;
var async_task_check_url = null;

function async_task_check() {
    "use strict";
    $.ajax(
        {
            'type': 'GET',
            'url': async_task_check_url,
            'success': async_task_check_callback,
            'error': async_task_check_error,
            'context': this,
            'dataType': 'json'
        }
    );
}

function async_task_check_callback(data, jqXHR, status) {
    "use strict";
    if (data.ready && data.redirect) {
        location.href = data.redirect;
        return;
    }
    async_task_timeout = window.setTimeout(async_task_check, 250);
    $("#loadingmodal p").text(gettext('Your request has been queued on the server and will now be ' +
                                      'processed.'));
}

function async_task_check_error(jqXHR, textStatus, errorThrown) {
    "use strict";
    var c = $(jqXHR.responseText).filter('.container');
    if (c.length > 0) {
        waitingDialog.hide();
        ajaxErrDialog.show(c.first().html());
    } else {
        if (jqXHR.status >= 400 && jqXHR.status < 500) {
            waitingDialog.hide();
            alert(gettext('An error of type {code} occured.').replace(/\{code\}/, jqXHR.status));
        } else {
            // 500 can be an application error or overload in some cases :(
            $("#loadingmodal p").text(gettext('We currenctly cannot reach the server, but we keep trying.' +
                                              ' Last error code: {code}').replace(/\{code\}/, jqXHR.status));
            async_task_timeout = window.setTimeout(async_task_check, 5000);
        }
    }
}

function async_task_callback(data, jqXHR, status) {
    "use strict";
    $("body").data('ajaxing', false);
    if (data.redirect) {
        location.href = data.redirect;
        return;
    }
    async_task_id = data.async_id;
    async_task_check_url = data.check_url;
    async_task_timeout = window.setTimeout(async_task_check, 100);

    history.pushState({}, "Waiting", async_task_check_url.replace(/ajax=1/, ''));
}

function async_task_error(jqXHR, textStatus, errorThrown) {
    "use strict";
    $("body").data('ajaxing', false);
    if (jqXHR.responseText.indexOf('container') > 0) {
        var c = $(jqXHR.responseText).filter('.container');
        waitingDialog.hide();
        ajaxErrDialog.show(c.first().html());
    } else {
        if (jqXHR.status >= 400 && jqXHR.status < 500) {
            waitingDialog.hide();
            alert(gettext('An error of type {code} occured.').replace(/\{code\}/, jqXHR.status));
        } else {
            waitingDialog.hide();
            alert(gettext('We currenctly cannot reach the server. Please try again. ' +
                          'Error code: {code}').replace(/\{code\}/, jqXHR.status));
        }
    }
}

$(function () {
    "use strict";
    $("body").on('submit', 'form[data-asynctask]', function (e) {
        e.preventDefault();
        if ($("body").data('ajaxing')) {
            return;
        }
        async_task_id = null;
        $("body").data('ajaxing', true);
        waitingDialog.show(gettext('We are processing your request …'));

        $.ajax(
            {
                'type': 'POST',
                'url': $(this).attr('action'),
                'data': $(this).serialize() + '&ajax=1',
                'success': async_task_callback,
                'error': async_task_error,
                'context': this,
                'dataType': 'json'
            }
        );
    });
});

@@ -829,16 +829,158 @@
 ocessed.
+ If this takes longer than two minutes, please contact us or go ' +%0A                                      'back in your browser and try again.
 '));%0A%7D%0A%0A
@@ -2164,24 +2164,378 @@
 eck, 100);%0A%0A
+    $(%22#loadingmodal p%22).text(gettext('Your request has been queued on the server and will now be ' +%0A                                      'processed. If this takes longer than two minutes, please contact us or go ' +%0A                                      'back in your browser and try again.'));%0A    if (location.href.indexOf(%22async_id%22) === -1) %7B%0A    
     history.
@@ -2600,24 +2600,30 @@
 x=1/, ''));%0A
+    %7D%0A
 %7D%0A%0Afunction 
@@ -2732,24 +2732,168 @@
 se);%0A    if 
+(textStatus === %22timeout%22) %7B%0A        alert(gettext(%22The request took to long. Please try again.%22));%0A        waitingDialog.hide();%0A    %7D else if 
 (jqXHR.respo
@@ -3850,16 +3850,327 @@
 st %E2%80%A6'));
+%0A        $(%22#loadingmodal p%22).text(gettext('We are currently sending your request to the server. If this takes longer ' +%0A                                          'than one minute, please check your internet connection and then reload ' +%0A                                          'this page and try again.'));
 %0A%0A      
@@ -4479,32 +4479,67 @@
 ataType': 'json'
+,%0A                'timeout': 60000,
 %0A            %7D%0A 

Change messages in asynctask.js