﻿
function ajaxGestionBasket(nomPage, strParamPage, traitement) {
    ajax(nomPage, strParamPage, false, function (res) {
        eval("var json = " + res);
        if (json.isOk == "ok") {
            traitement(json);
        } else if (json.isOk == "update") {
            alert(json.msg);
            updateBasket2(json.ida, json.nb_jours_choisis);
        } else if (json.isOk == "nok") {
            alert(json.msg);
        } else {
            alert(json.msg);
            document.location.reload(true);
        }
    }, true);
}

function removeFromBasket(id) {
    ajaxGestionBasket("/removeFromBasket.aspx", "ida=" + id,
                function (json) {
                    removeById("div" + id); //supprime l'élément à l'affichage

                    var nbItems = ed('affichageNbItems').innerHTML;
                    nbItems--;

                    //affiche le nb d'éléments
                    ed('affichageNbItems').innerHTML = nbItems;
                    ed('affichageNbItemsHaut').innerHTML = nbItems;
                    //affiche le prix total
                    ed('totalHT').innerHTML = json.totalHT;

                    if (nbItems <= 0) {
                        DesactiverPanier();
                    }
                }
            );
}

function updateBasket(ida) {
    var nbJoursSelected = document.getElementById('selectJoursPanier' + ida).value;

    updateBasket2(ida, nbJoursSelected);
}

function updateBasket2(ida, nbj) {
    ajaxGestionBasket("/updateBasket.aspx", ("ida=" + ida + "&nbj=" + nbj),
                function (json) {

                    var cbox = ed('opt_ad' + ida + '_' + nbj + 'j');

                    ed('prixHTAd' + ida).innerHTML = json.prixAdHT; //change le prix affiché

                    //affiche le prix total
                    ed('totalHT').innerHTML = json.totalHT;

                    var iSelected = 0;
                    var cbox = ed('selectJoursPanier' + ida);
                    for (var i = 0; i < cbox.length; i++) {
                        if (nbj == cbox[i].value) {
                            iSelected = i;
                            break;
                        }
                    }
                    cbox.selectedIndex = iSelected;
                }
            );
}

function addToBasket(id) {
    var max = 100000;
    ajaxGestionBasket("/addToBasket.aspx", "idp=" + id + "&nocache=" + (Math.floor(Math.random() * max + 1)),
                function (json) {

                    //var tab = ed('tableau_recapitulatif');     //ajoute l'élément à l'affichage


                    var tr = document.createElement("tr");
                    tr.id = "div" + json.ida;

                    var td = document.createElement("td");

                    var p = document.createElement("p");

                    var btn_suppr = document.createElement("a");
                    btn_suppr.id = "case_elem_panier" + json.ida;
                    btn_suppr.title = "Supprimer cet élément du panier";
                    btn_suppr.className = "supprimer_du_panier";
                    btn_suppr.href = "javascript:removeFromBasket(" + json.ida + ")";

                    var text_croix = document.createTextNode("x");
                    btn_suppr.appendChild(text_croix);

                    var text_format = document.createTextNode(json.format);

                    var a_nom_zone = document.createElement("a");
                    a_nom_zone.href = "/zone.aspx?idz=" + json.idz;

                    a_nom_zone.appendChild(document.createTextNode(json.nomZone));

                    var select_jours = document.createElement("select");
                    select_jours.title = "Sélectionner un autre produit de la zone";
                    select_jours.onchange = function () { updateBasket(json.ida); };
                    select_jours.id = "selectJoursPanier" + json.ida;
                    select_jours.className = "select_jours";
                    select_jours.selected = json.nb_jours_choisis;

                    for (var i = 0; i < json.tabJours.length; i++) {
                        if (json.tabJours[i]) {
                            var item_jour = document.createElement("option");
                            item_jour.id = "opt_ad" + json.ida + "_" + json.tabJours[i] + "j";
                            item_jour.value = json.tabJours[i];

                            if (json.nb_jours_choisis == json.tabJours[i]) item_jour.selected = "selected";
                            item_jour.appendChild(document.createTextNode(json.tabJours[i]));
                            select_jours.appendChild(item_jour);
                        }

                    }

                    var b_select_jours = document.createElement("b");
                    b_select_jours.appendChild(select_jours);

                    //Modif KBR - Article sponsorisé
                    if (select_jours.selected == 0) {
                        select_jours.setAttribute("style", "visibility:hidden;width:0px;");
                    }
                    if (select_jours.selected != 0) {
                        b_select_jours.appendChild(document.createTextNode(" j"));
                    }
                    //old : b_select_jours.appendChild(document.createTextNode(" j"));
                    //Fin Modif KBR - Article sponsorisé


                    var b_prix_HT = document.createElement("b");
                    b_prix_HT.id = "prixHTAd" + json.ida;
                    b_prix_HT.appendChild(document.createTextNode(json.prixAdHT));

                    var text_HT = document.createElement("span");
                    text_HT.className = "texte_hors_taxes";
                    text_HT.appendChild(document.createTextNode(" HT"));

                    //construction du html :
                    p.appendChild(btn_suppr);
                    p.appendChild(text_format);
                    p.appendChild(document.createElement("br"));
                    p.appendChild(a_nom_zone);
                    p.appendChild(document.createElement("br"));
                    p.appendChild(b_select_jours);
                    //Modif KBR - Article sponsorisé
                    if (select_jours.selected != 0) {
                        p.appendChild(document.createTextNode(" - "));
                    } else {
                        if (json.mode == "nofollow") {
                            b_select_jours.appendChild(document.createTextNode(" (Sans suivi) "));
                        }
                        else {
                            b_select_jours.appendChild(document.createTextNode(" (Lien normal) "));
                        }                        
                    }
                    //old : p.appendChild(document.createTextNode(" - "));
                    //Fin Modif KBR - Article sponsorisé
                    p.appendChild(b_prix_HT);
                    p.appendChild(text_HT);

                    td.appendChild(p);

                    tr.appendChild(td);


                    var tableau_recap = ed('tableau_recapitulatif').getElementsByTagName('table')[0];
                    if (tableau_recap.getElementsByTagName('tbody')[0] == null) {
                        var tbody = document.createElement('tbody');

                        tableau_recap.appendChild(tbody);
                    }

                    ed('tableau_recapitulatif').getElementsByTagName('table')[0].getElementsByTagName('tbody')[0].appendChild(tr);


                    var nbItems = ed('affichageNbItems').innerHTML;

                    nbItems++;

                    if (nbItems > 0) {
                        Afficher_Panier();
                    }



                    //affiche le nb d'éléments
                    ed('affichageNbItems').innerHTML = nbItems;
                    ed('affichageNbItemsHaut').innerHTML = nbItems;
                    //affiche le prix total
                    ed('totalHT').innerHTML = json.totalHT;


                }
            );
}



function createCookie(name, value) {//expiration 1 jour

    var date = new Date();
    date.setTime(date.getTime() + (24 * 60 * 60 * 1000));
    var expires = "; expires=" + date.toGMTString();

    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}


function Afficher_Panier() {

    document.getElementById('widget_panier').style.display = 'block';
    document.getElementById('affich_widget').style.display = 'inline';
    document.getElementById('affich_widget').style.cursor = 'default';
    createCookie('cPanier', 'True');

}

function Masquer_Panier() {
    document.getElementById('widget_panier').style.display = 'none';
    document.getElementById('affich_widget').style.display = 'inline';
    document.getElementById('affich_widget').style.cursor = 'pointer';
    createCookie('cPanier', 'False');

}

function DesactiverPanier() {
    document.getElementById('widget_panier').style.display = 'none';
    document.getElementById('affich_widget').style.display = 'none';
}


function affichagePanierAuChargement(nbItemsInBasket) {

    if (nbItemsInBasket > 0) {

        if (readCookie('cPanier') == 'False') {
            Masquer_Panier();
        } else {
            Afficher_Panier();
        }
    } else {
        DesactiverPanier();
    }

}
