
function AddProduct(pProductID, pQtite, pPrice, pLibelle) {
    if (isPositif(pQtite)) {
        pQtite = parseInt(pQtite);

        AddItemToServerCart("/Pages/VirtualStore.aspx?operation=ADD&code_produit=" + pProductID + "&quantite=" + pQtite);
        AddItemToPortlet(pProductID, pQtite, pPrice, pLibelle);
        $('#panier_infos').jScrollPane({ scrollbarWidth: 9 });
    }
    else {
        alert("La valeur saisie est incorrecte.");
    }
}

function RemoveProduct(pProductID, pQtite, pPrice, pLibelle) {
    if (isPositif(pQtite)) {
        pQtite = "-" + parseInt(pQtite);

        AddItemToServerCart("/Pages/VirtualStore.aspx?operation=ADD&code_produit=" + pProductID + "&quantite=" + pQtite);
        AddItemToPortlet(pProductID, pQtite, pPrice, pLibelle);
        $('#panier_infos').jScrollPane({ scrollbarWidth: 9 });
    }
    else {
        alert("La valeur saisie est incorrecte.");
    }
}

function AddItemToServerCart(pURL) {
    $.get(pURL, { noCache: (new Date()).getTime() });
}

function AddItemToPortlet(pProductId, pQuantity, pPrixUnitaire, pLibelle) {
    var divPanier = document.getElementById("panier_infos");
    if (!divPanier) {
        var divPan = document.getElementById("ctl00_PlaceHolderMain_ucEchPortlets_pnlCart");
        var div = getChild(divPan, "DIV", 1);
        var br = getChild(div, "BR", 1);

        var newDiv = document.createElement("div");
        newDiv.id = "panier_infos";
        newDiv.className = "panier_scrollpane";
        newDiv.style.overflow = "auto";
        newDiv.innerHTML = "<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\"><tbody></tbody></table>";
        div.insertBefore(newDiv, br);

        divPanier = document.getElementById("panier_infos");
    }

    var table = getChild(divPanier, "TABLE", 1);
    for (var i = 0; i < table.rows.length; i++) {
        var tr = table.rows[i];
        if (tr.nodeName == "TR" && tr.className != "sep") {
            var tdId = getChild(tr, "TD", 5);

            var inputId = getChild(tdId, "INPUT", 1);
            if (pProductId == inputId.value) {
                var tdQtity = getChild(tr, "TD", 2);
                var inputQtity = getChild(tdQtity, "SPAN", 1);
                var oldQtity = new Number(inputQtity.innerHTML);
                var newQtity = oldQtity + new Number(pQuantity);
                if (newQtity >= 0) {
                    inputQtity.innerHTML = newQtity;
                    var strPrice = getChild(tdId, "STRONG", 1).innerHTML;
                    var numPrice = new Number(strPrice.substring(0, strPrice.length - 2));

                    var newPrice;
                    if (oldQtity == "0") {
                        newPrice = Math.round((new Number(pPrixUnitaire) * new Number(pQuantity)) * 100) / 100;

                    } else {
                        newPrice = Math.round((numPrice + (new Number(pPrixUnitaire) * new Number(pQuantity))) * 100) / 100;
                    }
                    getChild(tdId, "STRONG", 1).innerHTML = newPrice + " &euro;";

                    RefreshTotaux(pQuantity, newPrice - numPrice);
                }
                return true;
            }
        }
    }

    var tbody = getChild(table, "TBODY", 1);

    var libelle2 = pLibelle.replace("'", "&rsquo;");

    var montant = (Math.round((new Number(pPrixUnitaire) * new Number(pQuantity)) * 100) / 100);

    // Ligne produit
    var tr = document.createElement("tr");
    var td1 = document.createElement("td");
    td1.innerHTML = "<img onclick=\"RemoveProduct('" + pProductId + "','1','" + pPrixUnitaire + "','" + libelle2 + "');\" style=\"cursor:pointer;\" class=\"btnC\" src=\"/Style Library/ECH/Images/portlet/panier-moins.gif\" title=\"Enlever un article\" id=\"imgMoins\">";
    tr.appendChild(td1);
    var td2 = document.createElement("td");
    td2.className = "td_saisie";
    td2.innerHTML = "<span style=\"display: inline-block; height: 13px;\" class=\"portlet_panier_quantity nb-xx btnC\" id=\"ctl00_PlaceHolderMain_ucEchPortlets_ucEchPortletsPanier_rptListeArticles_ctl01_txtQuantity\">" + pQuantity + "</span>";
    tr.appendChild(td2);
    var td3 = document.createElement("td");
    td3.innerHTML = "<img onclick=\"AddProduct('" + pProductId + "','1','" + pPrixUnitaire + "','" + libelle2 + "');\" style=\"cursor:pointer;\" class=\"btnC\" src=\"/Style Library/ECH/Images/portlet/panier-plus.gif\" title=\"Ajouter un article\" id=\"imgMoins\">";
    tr.appendChild(td3);
    var td4 = document.createElement("td");
    td4.innerHTML = "<span title=\"" + pLibelle + "\">" + pLibelle.substring(0, 13) + "...</span>";
    tr.appendChild(td4);
    var td5 = document.createElement("td");
    td5.innerHTML = "<strong class=\"btnB\">" + montant + " &euro;</strong><input type=\"hidden\" value=\"" + pProductId + "\"/>";
    tr.appendChild(td5);
    tbody.insertBefore(tr, tbody.firstChild);

    // Ligne vierge       
    var td = document.createElement("td");
    td.innerHTML = " ";
    var tr = document.createElement("tr");
    tr.className = "sep";
    tr.appendChild(td);
    tbody.insertBefore(tr, tbody.firstChild);

    RefreshTotaux(pQuantity, montant);
}

function RefreshTotaux(pQuantity, pPriceDelta) {
    var divTotaux = document.getElementById("ctl00_PlaceHolderMain_ucEchPortlets_pnlCart");
    var div = getChild(divTotaux, "DIV", 1);
    var p = getChild(div, "P", 1);

    // Votre panier contient :
    var spane = getChild(p, "SPAN", 1);
    spane.innerHTML = "Votre panier contient";

    // NbArticles
    var span = getChild(p, "SPAN", 2);
    var strong = getChild(span, "STRONG", 1);
    var spanNbArticles = getChild(strong, "SPAN", 1);
    var len = spanNbArticles.innerHTML.indexOf(" ", 0);
    var res = spanNbArticles.innerHTML.substring(spanNbArticles.innerHTML, len);
    var intRes = new Number(res) + new Number(pQuantity);
    if (intRes == "1") {
        spanNbArticles.innerHTML = intRes + " article";
    } else {
        spanNbArticles.innerHTML = intRes + " articles";
    }

    // Montant Total
    var span = getChild(p, "SPAN", 3);
    var strong = getChild(span, "STRONG", 1);
    var spanMontant = getChild(strong, "SPAN", 1);
    var strMontant = spanMontant.innerHTML.substring(0, spanMontant.innerHTML.length - 2).replace(",", ".");
    var newMontant = Math.round((new Number(strMontant) + new Number(pPriceDelta)) * 100) / 100;
    spanMontant.innerHTML = newMontant + " &euro;";
}

function getChild(pSource, pBalise, pRang) {
    var resultat = null;
    if (pSource != null) {
        var i = 0;
        var rang = 0;
        while (rang < pRang && i < pSource.childNodes.length) {
            resultat = pSource.childNodes[i];
            if (resultat.nodeName == pBalise) {
                rang++;
            }
            i++;
        }
    }
    return resultat;
}