function number_format_price(price)
{
        var pricelength = price.length;
        //alert('SUPER kekette');
        if(price.charAt(pricelength - 3) != '.')
                if(price.charAt(pricelength-2) == '.')
                {
                        price = price + "0";
                }
                else
                {
                        price = price + ".00";
                }
        pricelength = price.length;
        if(pricelength>6)
                price = price.substring(0, (pricelength - 6)) + ',' + price.substring((pricelength - 6), pricelength);
        //alert('kekette');
        return price;
}

function updateSubtotal()
{
        var subtotal = 0;
        var spantags = document.body.getElementsByTagName('span');
        var spantagslength = spantags.length;
        for(var x=0; x<spantagslength; x++)
                if(spantags[x].className == 'cartprice')
                {
                        //subtotal += parseInt(spantags[x].innerHTML.replace(',', '') * 100);
                        subtotal += parseFloat(spantags[x].innerHTML.replace(',', '.'));
                }
        //document.getElementById('cartsubtotal').innerHTML = number_format_price((subtotal/100).toString()) + ' &euro;';
        //alert('on modif');
        document.getElementById('cartsubtotal').innerHTML = subtotal.toString() + ' &euro;';
        //alert('ok');
        return subtotal;
}

function updateQuantity(ref_produit, no_commande_produit)
{
        if(typeof document.getElementById != 'undefined')
        {
                var thiselement = document.getElementById('cartquantity_' + ref_produit);
                var userinput = thiselement.value;
                thiselement.value = thiselement.value.replace(/\D/g, '');
                var quantity = parseInt(thiselement.value);
                if(isNaN(quantity)||quantity<1)
                {
                        if(userinput == '' || userinput.match(/\s+/))
                                userinput = '[empty]';
                        alert(userinput + ' n\'est pas un nombre valide!');
                        quantity = 1;
                        document.getElementById('cartquantity_' + ref_produit).value = 1;
                }

                document.location = 'adjustcart.php?update=1&no_commande_produit=' + no_commande_produit + '&ref_produit=' + ref_produit + '&quantite=' + quantity + '&quitemode=1&frompanier=1';
                //alert('retour url');
                var price = parseFloat(document.getElementById('cartpriceperunit_' + ref_produit).innerHTML.replace(',', '.'));
                //alert('ici');
                var newprice = number_format_price((Math.round(price * quantity * 100)/100).toString());
                newprice = newprice.replace('.', ',');

                document.getElementById('cartprice_' + ref_produit).innerHTML = newprice;
                //alert('price = ' + price + ' et newprice = ' + newprice);
                updateSubtotal();
        }
        else
        {
                document.forms['cart'].submit();
        }
}

function removeProduct(ref_produit, no_commande_produit, rowindex)
{
        var adjusturl = 'adjustcart.php?delete=1&frompanier=1&ref_produit=' + ref_produit + '&no_commande_produit=' + no_commande_produit;
        if(typeof document.getElementById != 'undefined')
        {
                //document.getElementById('cartrow_' + ref_produit).innerHTML = '';
                document.getElementById('tabloachats').deleteRow(rowindex);
                //document.getElementById('cartrow_' + ref_produit).style.display = 'none';
                if(updateSubtotal())
                        adjusturl += '&quitemode=1';

        }
        document.location = adjusturl;// + '&void=' + random();
}

function popup(url, w, h, bool)
{
        var ajout = '';
        if(bool)
                ajout = ', SCROLLBARS=YES, RESIZABLE=YES';
        win = window.open(url, '', 'WIDTH='+w+',HEIGHT=' + h + ajout);
        win.moveTo(10, 10);
}