var st_process = new Object();

function process(form_id, url, before_id, result_id, ident, before_img, before_img_size) {
    if (st_process[ident] == false) { return; }
    if (!before_img) { before_img = '/images/load.gif'; }
    if (before_img_size) { before_img_size = 'width="' + before_img_size + '"'; }
    jQuery.ajax({
        url: url,
        type: "POST",
        dataType: "json",
        data: jQuery("#" + form_id).serialize(),
        beforeSend: function() {
            st_process[ident] = false;
            if (before_id) {
                if (!$("#" + before_id).is(":visible")) {
                    $("#" + before_id).show();
                }
                $("#" + before_id).html('<img src="' + before_img + '" ' + before_img_size + '/>');
            }
        },
        success: function(response) {
            $.each(response, function(i, val) {
                $('#' + i).html(val);
            });
        },
        error: function(response) {
            $("#" + result_id).html('<div class="error">Произошла ошибка. Попробуйте снова!</div>');
        },
        complete: function() {
            st_process[ident] = true;
        }
    });
}

function number_format(number, decimals, dec_point, thousands_sep) {
    var i, j, kw, kd, km;

    if (isNaN(decimals = Math.abs(decimals))) {
        decimals = 2;
    }
    if (dec_point == undefined) {
        dec_point = ",";
    }
    if (thousands_sep == undefined) {
        thousands_sep = ".";
    }

    i = parseInt(number = (+number || 0).toFixed(decimals)) + "";

    if ((j = i.length) > 3) {
        j = j % 3;
    } else {
        j = 0;
    }

    km = (j ? i.substr(0, j) + thousands_sep : "");
    kw = i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousands_sep);
    kd = (decimals ? dec_point + Math.abs(number - i).toFixed(decimals).replace(/-/, 0).slice(2) : "");


    return km + kw + kd;
}

function calcul(input, result_id, cifr, obmenPerc) {
    var value = input.value;
    var rep = /[^(1234567890)]/g;
    if (rep.test(value)) {
        value = value.replace(rep, '');
        input.value = number_format(value, cifr, ',', ' ');
    }
    if (obmenPerc > 0) { value = Number(value) + Number(value * Number(obmenPerc / 100));
        value = Math.floor(value); }
    value = number_format(value, cifr, ',', ' ');
    $("#" + result_id).html(value);
}

function change_capcha() {
    $("#signup img.capcha").attr("src", "/capcha.php");
}