﻿

function calulate_price() {
    var total = 0.0
    var _nbarts = 0
    var t = $('tablePanier').getElementsByTagName('TR')

    for (var i = 0; i < t.length; i++) {
        if (t[i].getAttribute('row')) {
            total = total + getLineValue(t[i].getAttribute('row'))
        }
    }

    var prixgravtxt = 0.0
    if ($('pxgrav') != null) { prixgravtxt = parseFloat($('pxgrav').innerHTML) }
    var p=parseFloat(prixgravtxt + total)
    var tva = Math.round((p * 0.196), 2)
    
    $('totalht').update(formatPrice(p))
    $('tva').update(formatPrice(tva))
    $('total').update(formatPrice(p + tva))

    // nb articles 
    var _inputs = $('tablePanier').select('input');
    _inputs.each(function (c) {
        _nbarts = _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()

    params = 'action=5&guiProd=' + gui + '&quantite='+ q +''
    var a = new Ajax.Request('panier.ajax', {
        method: 'POST',
        postBody: params,
        onSuccess: function(transport) {
            var r = transport.responseText
            if (r.indexOf(",") == -1) { r = r + ",00"} 
            $('prix_' + id).update(r)
            var pr = parseFloat((r).replace(",", "."))
            var pq = parseFloat(q)
            var res = parseFloat(pr * pq)
            var prixtotal = formatPrice(res)
     
            $('prixTotal_' + id).update(prixtotal)
        }
    })
}

function updateRowQuantite(id,gui) {
    var q=$('q'+id).getValue()
    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
            var a = new Ajax.Request('panier.ajax', {
                method: 'POST',
                postBody: params,
                onComplete: function(transport) {
                    var r = transport.responseJSON
                    window._nbItemPanier = r.nbItem
                    updateTimbre()
                    calulate_price()
                }
            })
        } 
        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() 
                        }
                    }
                }
            }
        })
    }
}

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
       }
   })  
}



