﻿

function calulate_price(tot) {
    var total = 0,
        _nbarts = 0,
        total = parseFloat(tot.replace(",",".")),
        tva = Math.round((total * 0.196), 2);

    $('totalht').update(formatPrice(total));
    $('tva').update(formatPrice(tva));
    $('total').update(formatPrice(total + tva));

    // nb articles 
    var _inputs = $('tablePanier').select('input');
    _inputs.each(function (c) {
        _nbarts += parseInt(c.value);
    });
    $('nbarts').update(_nbarts);
}

function getLineValue(id){
    var u =  ($('prixTotal_'+id).innerHTML).replace(",", ".");
    var d = (parseFloat(u));
    return d;
}

function updatePrixQnt(id, gui, q) {
    // calcuel le prix total pour un article en fonction de la quantité
    var q = $('q' + id).getValue(),
        supp = 0;

    params = 'action=5&guiProd=' + gui + '&quantite='+ q +'';
    var a = new Ajax.Request('panier.ajax', {
        method: 'POST',
        postBody: params,
        asynchronous : false,
        onComplete: function (transport) {
            var obj = transport.responseJSON,
                r = parseFloat(obj.Prix.replace(",", ".")),
                supp = parseFloat(obj.Supp.replace(",",".")),
                quantite = parseFloat(q);

            $('prix_' + id).update(r);
            var res = (r * quantite);
            //res += (supp == 0) ? 0 : (supp * quantite);
            var prixtotal = formatPrice(res);
            $('prixTotal_' + id).update(prixtotal);
        }
    });
}

function updateRowQuantite(id,gui) {
    var q = $('q' + id).getValue();
       // gravure = updatePrixQnt(id, gui, q);

    if (q != '') {
        if (IsANumber(q) == true) {
            $('error_q' + id).hide();
            
            //$('q' + id).removeClassName("error")
            params = 'action=3&gui=' + gui + '&quantite=' + q;

            window.location = 'panier.aspx?' + params;
            
            var a = new Ajax.Request('panier.ajax', {
                method: 'POST',
                postBody: params,
                asynchronous: false,
                onComplete: function (transport) {
                    var r = transport.responseJSON,
                        total = r.total;

                    window._nbItemPanier = r.nbItem;
                    updateTimbre();
                    calulate_price(total);
                }
            });
            
        } 
        else {
        $('error_q' + id).show();
          //  $('q' + id).addClassName("champerreur")
        }
    } else {
        $('error_q' + id).show();
        //$('q' + id).addClassName("champerreur")
    }
}

function delItem(gui) {
    if (confirm('Voulez vous vraiment supprimer cet article ?')) {
        params = 'action=2&gui=' + gui;
        var a = new Ajax.Request('panier.ajax', {
            method: 'POST',
            postBody: params,
            onComplete: function (transport) {
                var r = transport.responseJSON;
                if (r.result == true) {
                    var t = $('tablePanier').getElementsByTagName('TR');
                    for (var i = 0; i < t.length; i++) {
                        if (t[i].getAttribute('datakey') == r.gui) {
                            (t[i]).remove();
                            window._nbItemPanier = r.nbItem;
                            updateTimbre();
                        }
                    }
                    for (var i = 0; i < t.length; i++) {
                        if (t[i].getAttribute('datakey') == r.gui) {
                            (t[i]).remove();
                        }
                    }
                    location.reload();   
                }
            }
        });
    }
    
}

function formatPrice(price) {
    var myprice =  price;
    myprice = myprice.toString().replace(".", ",");
    if (myprice.indexOf(",") == -1) {
        myprice = myprice + ",00";
    } 
    else if (myprice.length - myprice.indexOf(",") == 2) {
        myprice = myprice + "0";
    } 
    else {
        myprice = myprice.substring(0, myprice.indexOf(",") + 3);
    }
    return  myprice;
}

function updateComment(txt) {
    params = 'action=7&cmdcomment=' + txt + '';
    var a = new Ajax.Request('panier.ajax', {
        method: 'POST',
        postBody: params,
        onSuccess: function(transport) {
        var r = transport.responseText;
        }
   });
}




