
function modifProduit(even, message) {
    var quantite = even.value;
    var reference = even.name;

    var re = /^\d*$/; 
    quantite = quantite.toString();
    
    if (quantite.match(re)) {
        bloquer(message);
        $.ajax({
            type: 'post', 
            url: '/commande/recapitulatif/update/',
            data: '&idProduitVente=' + reference + '&quantite=' + quantite,
            async: true,
            dataType: 'json',
            success: renderResults
        });
    }
}

function supprProduit(reference) {
    $.ajax({
        type: 'post', 
        url: '/commande/recapitulatif/update/',
        data: '&idProduitVente=' + reference + '&quantite=0',
        async: true,
        dataType: 'json',
        success: renderResults
    });
}

function modifService(idServiceVente, active, titre, texte, bouton) {
    var myAjax = $.ajax({
        type: 'post',
        url: '/commande/recapitulatif/update/',
        data: '&idServiceVente=' + idServiceVente + '&active=' + active,
        async: true,
        dataType: 'json',
        success: function(response) {
            renderResults(response);
            
            if(active == 0) {
                options = new Array();
                options["picto"] = "avertissement";
                messageBox(titre, texte, bouton, options);
            }
        }
    });
}

function modifServiceByCheckBox(idServiceVente, titre, texte, bouton) {
//    var idServiceVente = even.name;
    var active = 0;
    if(document.getElementById('checkboxGarantie' + idServiceVente).checked) {
        active = 1;
    }
    modifService(idServiceVente, active, titre, texte, bouton);
}

function modifierReduction(even) {
    var id = even.name;
    var url = '/commande/recapitulatif/update/';
    var parametre = '&idReduction=' + id;
    var myAjax = $.ajax({
        type: 'post',
        url: url,
        data: parametre,
        async: true,
        dataType: 'json',
        success: renderResults
    });
}

function renderResults(response, options){
    if(response['formRecapitulatifCommande'] != undefined) {
        $('#idFormRecap').html(response['formRecapitulatifCommande']);
    }
    $('#menuPanier').html(response['contenuPanier']);
    $('#nomPanier').html(response['indexPanier']);
    
    if(response['message'] != null) {
        messageBox(response['message']['titre'], response['message']['message'], response['message']['bouton'], options);
    }
}

function ajouterCodePromo(nomCodePromo) {
    if(nomCodePromo != '') {
        var myAjax = $.ajax({
            type: 'post',
            url: '/commande/recapitulatif/update/',
            data: '&nomCodePromo=' + nomCodePromo,
            async: true,
            dataType: 'json',
            success: function(response) {
                renderResults(response);
            }
        });
    } else {
        debloquer();
    }
}

function supprimerCodePromo(idReduction) {
    var myAjax = $.ajax({
        type: 'post',
        url: '/commande/recapitulatif/update/',
        data: '&idReduction=' + idReduction + '&codePromo=' + 1,
        async: true,
        dataType: 'json',
        success: function(response) {
            renderResults(response);
        }
    });
}