function show_error(field, msg) {
    modal_window({
        message: '<div class="alert alert-danger">' + msg + '</div>',
        title: 'Error in ' + field,
        close: function () {
            $j('#' + field).focus();
            $j('#' + field).parents('.form-group').addClass('has-error');
        }
    });

    return false;
}

$j(function () {
    $j('#update, #insert').click(function () {
        /* Make sure commission is valid */
        var commission = $j('#commission').val();

        if (isNaN(commission) || commission < 0 || commission > 100) {
            return show_error('commission', 'La commission doit etre entre 0 et 100.');
        }
    });

    /* Set montant a payer to read-only */
    $j('#total').prop('readonly', true);
    $j('#code_transaction').prop('readonly', true);
    $j('#num_facture').prop('readonly', true);

    /* calculer le total */
    $j('#prix_achat, #commission').change(function () {
        var montantAchat = parseFloat($j('#prix_achat').val()) || 0;
        var commission = parseFloat($j('#commission').val()) || 0;

        var montantPayer = montantAchat * commission / 100 + montantAchat;
        $j('#total').val(montantPayer);
    });
    /* calculate total price on opening the detail view form*/
    $j('#prix_achat').change();

})
