﻿    

function MiseEnPanier(id, title) {
    var li = document.createElement("LI");
    li.id = id;
    var ahref = document.createElement("A");
    ahref.setAttribute("href", "javascript:DelCatalogue('" + id + "');");
    var img = document.createElement("IMG");
    img.setAttribute("src", "/img/croix.gif");
    ahref.appendChild(img);
    li.appendChild(document.createTextNode(title));
    li.appendChild(ahref);
    $("#panier").append(li);
}
function refreshCptPanier(fade) {
    cpt = $('#panier').children().length;

    if (cpt == 0) {
        if (fade)
            $("#cptPanier").fadeOut(function() { $("#cptPanier").text("aucune formation sélectionnée").fadeIn() });
        else
            $("#cptPanier").text("aucune formation sélectionnée");
        $("#comparer").hide();
        $("#compvide").show();
    }
    else
        if (cpt == 1) {
        if (fade)
            $("#cptPanier").fadeOut(function() { $("#cptPanier").text("une formation sélectionnée").fadeIn() });
        else
            $("#cptPanier").text("une formation sélectionnée");
        $("#comparer").show();
        $("#compvide").hide();
    }
    else {
        if (fade)
            $("#cptPanier").fadeOut(function() { $("#cptPanier").text(cpt + " formations sélectionnées").fadeIn() });
        else
            $("#cptPanier").text(cpt + " formations sélectionnées");
        $("#comparer").show();
        $("#compvide").hide();
    }
}


function AddCatalogue(id, title) {
    cpt = $('#panier').children().length;
    if (GetCookie(id) == null) {
        if (cpt >= 7) {
            $('#panierfull').show();
            return false;
        } else {
            SetCookie(id, title);
            MiseEnPanier(id, title);
            refreshCptPanier(1);
            if (GetCookie("PFERME")) flipPanier();
            return true;
        }
    }
}

function AddCatalogue2(id, title) {
    AddCatalogue(id, title);
}
function DelCatalogue(id) {
    if (GetCookie(id)) {
        $('#panierfull').hide();
        $("#" + id).remove();
        DeleteCookie(id);
        refreshCptPanier(1);
    }
}

function DelCatalogueC(id) {
    if (GetCookie(id)) {
        DeleteCookie(id);
        location.reload();
    }
}

function flipPanier() {
    if (GetCookie("PFERME")) {
        DeleteCookie("PFERME");
        $("#liste_form").show();
        $("#cptPanier").addClass("open");
    }
    else {
        SetCookie("PFERME", "1");
        $("#liste_form").hide();
        $("#cptPanier").removeClass("open");
    }
}
function initPanier() {
    var a_all_cookies = document.cookie.split(';');
    for (i = 0; i < a_all_cookies.length; i++) {
        a_temp_cookie = a_all_cookies[i].split('=');
        cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
        if ((cookie_name.length > 3) && (cookie_name.substr(0, 3) == "PAN")) {
            MiseEnPanier(cookie_name, GetCookie(cookie_name));
        }
    }
}
function manPanier() {
    // Mise à Niveau du Panier
    // (préparé sur le serveur / les cookies font foi)

    //suppression d'éventuels éléments retirés
    lpan = $('#panier').children().length - 1;


    for (var i = lpan; i >= 0; i--) {
        var id = $('#panier').children().get(i).id;
        
        if (!GetCookie(id)) {
            //suppression du dom
            $("#" + id).remove();
            if ($('#I' + id).length) $('#I' + id).attr("src", "/formation/img/etoile_vide.gif");
        }
    }
    //ajout d'éventuels éléments ajoutés
    var a_all_cookies = document.cookie.split(';');
    for (i = 0; i < a_all_cookies.length; i++) {
        a_temp_cookie = a_all_cookies[i].split('=');
        cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
        if ((cookie_name.length > 3) && (cookie_name.substr(0, 3) == "PAN")) {
            if (!$("#" + cookie_name).length) {
                MiseEnPanier(cookie_name, GetCookie(cookie_name));
                if ($('#I' + cookie_name).length) $('#I' + cookie_name).attr("src", "/formation/img/etoile_pleine.gif");
            }
        }
    }
    refreshCptPanier(0);
    
}

function SetCookie(name, value) {
    document.cookie = name + "=" + encodeURIComponent((value)) + ";path=/";
}
function GetCookie(check_name) {
    // first we'll split this cookie up into name/value pairs
    // note: document.cookie only returns name=value, not the other components
    var a_all_cookies = document.cookie.split(';');
    var a_temp_cookie = '';
    var cookie_name = '';
    var cookie_value = '';
    var b_cookie_found = false; // set boolean t/f default f

    for (var i = 0; i < a_all_cookies.length; i++) {
        // now we'll split apart each name=value pair
        a_temp_cookie = a_all_cookies[i].split('=');
        
        // and trim left/right whitespace while we're at it
        cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

        // if the extracted name matches passed check_name
        if (cookie_name == check_name) {
            b_cookie_found = true;
            // we need to handle case where cookie has no value but exists (no = sign, that is):
            if (a_temp_cookie.length > 1) {
                cookie_value = a_temp_cookie[1].replace(/^\s+|\s+$/g, '');
            }
            // note that in cases where cookie is initialized but no value, null is returned
            return decodeURIComponent(cookie_value);
            break;
        }
        a_temp_cookie = null;
        cookie_name = '';
    }
    if (!b_cookie_found) {
        return null;
    }
}
// this deletes the cookie when called
function DeleteCookie(name) {
    //if (GetCookie(name)) document.cookie = name + "=test";
    if (GetCookie(name))    document.cookie = name + "=;path=/;expires=Thu, 01-Jan-1970 00:00:01 GMT";

}

