$( document ).ready(function() {
    var create_vm_form = $('#virtual_machine_create_form');
        create_vm_form.submit(placeOrderPayment);
        function placeOrderPayment(e) {
            e.preventDefault();
            $.ajax({
                url: create_vm_form.attr('action'),
                type: 'POST',
                data: create_vm_form.serialize(),
                init: function () {
                    ok_btn = $('#createvm-modal-done-btn');
                    close_btn = $('#createvm-modal-close-btn');
                    ok_btn.addClass('btn btn-success btn-ok btn-wide hide');
                    close_btn.addClass('btn btn-danger btn-ok btn-wide hide');
                },
                success: function (data) {   
                    fa_icon = $('.modal-icon').find('.fa-cog');
                    modal_btn = $('#createvm-modal-done-btn');
                    if (data.error) {
                        // Display error.message in your UI.
                        modal_btn.attr('href', error_url).removeClass('sr-only sr-only-focusable');
                        fa_icon.attr('class', 'fa fa-close');
                        modal_btn.attr('class', '').addClass('btn btn-danger btn-ok btn-wide');
                        $('#createvm-modal-title').text("Error Occurred");
                        $('#createvm-modal-body').html(data.error.message);
                    } else {
                        window.location.href = data.redirect;
                    }
                 }
            });
        }
});