$.ajaxSetup({
  headers: {"X-Requested-With":"Ajax"},
  dataType: "html",
  cache: false
});
/**
 * success, failed - callbacks
 * loading -
 */
$.fn.ajaxSubmit = function(options) {
    options = $.extend({
        url       : this.attr('action'),
        method    : this.attr('method'),
        parameters: this.serialize(),
        hide      : false,
        dataType  : 'html'
    }, options || {});

    if (!options.url) {
        err = 'Url is empty.'
        console.error(err);
        throw(err);
    }

    if (options.confirm) {
        send = confirm(options.confirm);
    } else {
        send = true;
    }
    if(send) {
        Loader.show();

        if (options.hide) {
            this.hide();
        }


        $.ajax({
            url:  options.url,
            type: options.method,
            data: options.parameters,
            dataType: options.dataType,
            cache: false,
            success: function(html) {
                Loader.hide();

                if (options.update) {
                    options.update.html(html);
                }
                if (typeof options.success == 'function') {
                    options.success();
                }
            },
            complete: function (XMLHttpRequest, textStatus) {
                var r = XMLHttpRequest.getResponseHeader('Hard-Redirect');
                if (r) {
                    document.location = r;
                }
            },
            error: function (XMLHttpRequest, status, error) {
                Loader.hide();
                alert('Page error');
                if (console) {
                    console.log(XMLHttpRequest.responseText);
                }

            }
        });
    }
  
}
