//Get new captcha field
function reload_captcha()
{
    var get_captcha = $('#get-captcha').attr('rel');
    $.post(get_captcha, 
           {'csrfmiddlewaretoken' : csrf_token},
           function (data)
           {
               $('#id_captcha_0').val(data.timestamp);
               $('#id_captcha_1').val(data.hash);
               $('div.captcha img').attr('src', data.img);
           });
}

// Before request handler. Disable button and add animation.
function callback_prepare()
{
    // Clean all errors
    $('#callback .errorlist').remove();
    // Disable send button
    $('#callback-submit').attr('disabled', 'disabled');
    // Show animation
    $('#ajax-process')
        .css({top: ($(window).height() - $('#ajax-process').height()) / 2 + $(window).scrollTop() + 'px',
              left: ($(window).width() - $('#ajax-process').width()) / 2 + $(window).scrollLeft() + 'px'})
        .show();
}

// Hide animation and unlock button
function callback_complete()
{
    // Unlock button
    $('#callback-submit').removeAttr('disabled');
    // Hide animation
    $('#ajax-process').hide();
    reload_captcha();
}

// Process data, 
function callback_success(data)
{
    if (data.success)
    {
        // Remove errors
        $('#callback div.error-clear').remove();
        $('#callback ul.errorlist').remove();
        $('#callback label.error-check').remove();
        $('#callback')[0].reset(); // Reset form
        // Show info tooltip
        $('#ajax-info').html('<p>' + gettext('Request successfully sent') + '</p>')
            .css({
                top: ($(window).height() - $('#ajax-info').height()) / 2 + $(window).scrollTop() + 'px',
                left: ($(window).width() - $('#ajax-info').width()) / 2 + $(window).scrollLeft() + 'px'
            }).show();
        return;
    }
    if (data.errors)
    {
        // Remove old errors
        $('#callback div.error-clear').remove();
        $('#callback ul.errorlist').remove();
        $('#callback label.error-check').remove();
        // Add remainder
        $('<label class="error-check">' + gettext('Please check that you have filled<br /> out all the fields correctly') 
          + '</label><div class="error-clear" style="clear: left;"></div>').insertBefore('#callback div.code');
        // Show errorlist
        for (name in data.errors)
        {
            var errorlist = $('<ul>').addClass('errorlist');
            for (var i = 0, j = data.errors[name].length; i < j; i++)
                $('<li>').text(data.errors[name][i]).appendTo(errorlist);
            $('#callback div.' + name).after(errorlist);
            errorlist.after('<div class="error-clear" style="clear: left;"></div>');
        }
    }
}

// Show error if request failed
function callback_error()
{
    callback_complete();
    $('#ajax-info').html('<p>' + gettext('An error has occured') + '</p>')
        .css({
            top: ($(window).height() - $('#ajax-info').height()) / 2 + $(window).scrollTop() + 'px',
            left: ($(window).width() - $('#ajax-info').width()) / 2 + $(window).scrollLeft() + 'px'
        }).show();
}

$(function ()
  {
      $('#callback-submit').click(function ()
                                  {
                                      var url = $('#callback').attr('action');
                                      $.ajax({
                                          url: url,
                                          beforeSend: callback_prepare,
                                          data: $('#callback').serialize(),
                                          success: callback_success,
                                          error: callback_error,
                                          complete: callback_complete,
                                          type: 'POST'
                                      });
                                      return false;
                                  });
  });
