$('.bids-form input[name="Bid[phone]"]').inputmask("phone", {
    mask: "+7(999)999-99-99"
});

$('form.bids-form').on('beforeSubmit', function(e) {
    var form = $(this), xhr = new XMLHttpRequest, filebool = false, file, data = new FormData();

    form.find('input, textarea').each(function(){
        data.append($(this).attr('name'), $(this).val());
    });
    
    if(form.find('input[type=file]').length) {
        file = form.find('input[type=file]')[0].files[0];
        filebool = !filebool;
        data.append("Bid[file]", file);
    }

    xhr.open("POST", '/bids/bid/add', true);
    xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");

    xhr.send(data);

    xhr.onreadystatechange = function()
    {
        if (xhr.readyState == 4){
            try
            {
                var response = JSON.parse(xhr.responseText);
            }
            catch(e)
            {
                var response = xhr.responseText;
            }

            if(response.success) 
            {
                form.find('input:not(.not_clear), textarea').val('');
                $('.send_secce').show();
            }
        }
    }
}).on('submit', function(e){
    return false;
});