var separateurMilliers = ' ';
var actionButtons, actionButtonsDefault, actionButtonsEdit, price = 0, panier, templates, NbParticipant = 0;

$(function() {

    panier = (".panier");
    if ($("#contentPopIn").length) {
        templates = ("#contentPopIn > .templates");
    }
    else {
        templates = (".templates");
    }

    if (typeof getCurrentProduct !== 'function') {
        getCurrentProduct = function() { return product; };
    }

    if (!actionButtons) {
        actionButtons = $("<div id='actionButtons'></div>");
    }

    if (!actionButtonsEdit) {
        actionButtonsEdit = $("<div id='actionButtonsEdit'></div>");
        actionButtonsEdit.insertSession = function() { };
        actionButtonsEdit.cancelInsertSession = function() { };
        actionButtonsEdit.insertParticipant = function() { };
        actionButtonsEdit.cancelInsertParticipant = function() { };
    }

    if (!actionButtonsDefault) {
        actionButtonsDefault = $("<div id='actionButtonsDefault'></div>");
    }

    $("div.action-button-container cufon[alt=' ']").remove();

});

function addParticipantWidth() {
    $(".ajouter-participant a:visible, a.ajouter-date:visible").width($(".ajouter-participant a:visible, a.ajouter-date:visible").sort(function(a, b) { return $(b).width() - $(a).width(); }).width());
}


function hideButtons() {
    $(".participant .supprimer, .participant .modifier, .session .supprimer, .session .modifier-date:not(.btn-ok)").hide();
}
function showButtons() {
    $(".participant .supprimer, .participant .modifier, .session .supprimer, .session .modifier-date:not(.btn-ok)").show();
}

function initTemplate() {

    $('input[name=prenom]').val(strPrenom + '*');
    $('input[name=nom]').val(strNom + '*');
    $('input[name=mail]').val(strMail + '*');
    $('input[name=fonction]').val(strFunction + '*');
    $('input[name=prenom]').attr('defaultValueString', strPrenom);
    $('input[name=nom]').attr('defaultValueString', strNom);
    $('input[name=mail]').attr('defaultValueString', strMail);
    $('input[name=fonction]').attr('defaultValueString', strFunction);

    $('input[name=prenom] ~ span').css({ 'visibility': 'hidden', 'display': 'inline' });
    $('input[name=nom] ~ span').css({ 'visibility': 'hidden', 'display': 'inline' });
    $('input[name=mail] ~ span').css({ 'visibility': 'hidden', 'display': 'inline' });
    $('input[name=fonction] ~ span').css({ 'visibility': 'hidden', 'display': 'inline' });


    $('.modifier', templates).html(strEdit);
    $('.annuler', templates).html(strCancel);

    if (typeof product == "object" && product.type === "ELearningPackage") {
        $('.ajouter-participant a', templates).html(strAddParticipantElearning);
    }
    else {
        $('.ajouter-participant a', templates).html(strAddParticipant);
    }
    $('.ajouter-date p strong', templates).first().html(strAddDate);
    $('a.ajouter-date', templates).html(strAddDate);
    $('.ajouter-date div.options p strong', templates).first().html(strOptions);

    var options = '';
    for (var i = 0; i < civiliteData.length; i++) {
        options += '<option value="' + civiliteData[i] + '">' + civiliteData[i] + '</option>';
    }
    $('select[name=civil]', templates).html(options);

    options = '';
    for (var j = 0; j < participantsData.length; j++) {
        if (j == 0) {
            options += '<option value=";;;;">' + strFirstItemParticipants + '</option>';
        }
        var d = participantsData[j].civiliteId + ';' + participantsData[j].firstName + ';' + participantsData[j].lastName + ';' + participantsData[j].mail + ';' + (participantsData[j]["function"] !== "" ? participantsData[j]["function"] : "");
        var d2 = '';
        if (j > 0) {
            d2 = participantsData[j].civiliteId + ' ' + participantsData[j].firstName + ' ' + participantsData[j].lastName + (participantsData[j]["function"] !== "" ? ', ' + participantsData[j]["function"] : "") + ' - ' + participantsData[j].mail;
        }
        options += '<option value="' + d + '">' + d2 + '</option>';
    }

    initBlur($('input:text'));
    initFocus($('input:text'));
    if (options != '') {
        $('.memoire', templates).html(options);
        $('.memoire', templates).prev().html(strInfoParticipant);
        initMemoireChange($('.memoire', templates));
    } else {
        $('.memoire', templates).hide();
    }
    $('select.memoire', templates).prev().html(strInfoParticipant);

    if (typeof (DoCufon) != 'undefined') { DoCufon(); }

}
function initMemoireChange(a) {
    $(a).unbind('change');
    $(a).change(function() {
        if ($(this).val() == ';;;;') {
            return;
        }
        var divParticipantEdit = $(this).parent().next();
        var values = $(this).val().split(';');
        $('select[name=civil]', divParticipantEdit).val(values[0]);
        $('input[name=prenom]', divParticipantEdit).val(values[1]);
        $('input[name=nom]', divParticipantEdit).val(values[2]);
        $('input[name=mail]', divParticipantEdit).val(values[3]);
        $('input[name=fonction]', divParticipantEdit).val(values[4]);
        $('input', divParticipantEdit).blur();
    });
}
function initBlur(a) {
    $(a).unbind('blur');
    $(a).blur(function() {
        var a = $(this).attr('defaultValueString');
        var b = '*';
        a += b;
        if ($(this).val() == '') {
            $(this).val(a);
        }
        if ($(this).val() == a) {
            $(this).css('color', 'DarkGray');
            $(this).css('font-style', 'italic');
        } else {
            $(this).css('color', '');
            $(this).css('font-style', '');
        }
    }).blur();
}
function initFocus(a) {
    $(a).unbind('focus');
    $(a).focus(function() {
        var a = $(this).attr('defaultValueString');
        var b = '*';
        a += b;
        if ($(this).val() == a) {
            $(this).val('');
            $(this).css('color', '');
            $(this).css('font-style', '');
        }
    });
}
function initAddNewpromotion(ulDetailsSession) {
    var html = '';

    var product = getCurrentProduct($(ulDetailsSession).parents(".panier"));

    for (var a = 0; a < product.sessions.length; a++) {
        var session = product.sessions[a];
        html += '<li sessionCode="' + session.code + '">';
        html += '<span>{0}</span>'.format(session.title);
        html += '<select name="ville" class="ville">';
        var lstVilles = [];
        for (var b = 0; b < session.sessionDetails.length; b++) {
            if ($.inArray(session.sessionDetails[b].placeId, lstVilles) == -1) {
                html += '<option value="{0}">{0}</option>'.format(session.sessionDetails[b].placeId);
                lstVilles.push(session.sessionDetails[b].placeId);
            }
        }
        html += '</select>';
        html += '<select name="date" class="date">';
        html += '</select>';
        html += '</li>';
    }
    $(ulDetailsSession).append($(html));

    $('select[name=ville]', ulDetailsSession).change(function() {
        onParcoursProSelectVilleChange(this, ulDetailsSession);
    });
    $('select[name=ville]', ulDetailsSession).change();



    actionButtons.html(actionButtonsEdit);
    if (price === 0) {
        $("div.cancel", actionButtons).hide();
    }
    else {
        $("div.cancel", actionButtons).show();
    }

    $("a.addParticipant", actionButtons).click(actionButtonsEdit.insertSession);
    $("a.cancel", actionButtons).click(actionButtonsEdit.cancelInsertSession);

    $("a.addParticipant", actionButtons).text(strAddThisParticipant);

    if (typeof (DoCufon) != 'undefined') { DoCufon(); }

    try { $.fn.colorbox.resize(); } catch (err) { }

}
function onParcoursProSelectVilleChange(selectVille, ulDetailsSession) {
    var sessionCode = $(selectVille).parents('li').attr('sessionCode');
    if (sessionCode == null)
        return;
    var selectDate = $('li[sessionCode=' + sessionCode + '] select[name=date]', ulDetailsSession);
    var session = getSessionByCode(ulDetailsSession, sessionCode);
    $(selectDate).html('');
    for (var b = 0; b < session.sessionDetails.length; b++) {
        if (session.sessionDetails[b].placeId == $(selectVille).val()) {
            var html = '<option value="' + session.sessionDetails[b].id + '">' + sessionDateTextFormat.format(session.sessionDetails[b].beginDate, session.sessionDetails[b].endDate) + '</option>';
            $(selectDate).append($(html));
        }
    }
}
function editPromotion(ctrl) {
    var divSession = $(ctrl).parents('div.session');
    $(divSession).children('div.modifier-parcours').show();
    $(divSession).children('ul.details-session').hide();
    $('div.titre', divSession).addClass('edition');
    $('div.titre a', divSession).hide();
    $('div.titre a:last', divSession).show();

    hideButtons();

    $(divSession).parents('.pro').find('.bottom').hide();
    try { $.fn.colorbox.resize(); } catch (err) { }

}
function bindPromotionToHeader(a, promotion) {
    var divSession = $(a).parents('.pro').find('div.session[sessionId=' + promotion.id + ']');
    $('.titre p strong', divSession).html('Promotion ' + (parseInt(promotion.id) + 1));
    $('div.modifier-parcours ul li select[name=ville]', divSession).each(function(index) {
        $(this).val(promotion.sessionDetails[index].detail.placeId);
        $(this).change();
    });
    $('div.modifier-parcours ul li select[name=date]', divSession).each(function(index) {
        $(this).val(promotion.sessionDetails[index].detail.id);
    });
    var html = '';
    $(promotion.sessionDetails).each(function(index) {
        html += '<li><strong>' + this.detail.placeId;
        html += ' - ' + sessionDateTextFormat.format(this.detail.beginDate, this.detail.endDate);
        html += ' - </strong>' + this.title + ' - ';
        if (this.detail.durationInDays <= 1) {
            html += strJourFormat.format(this.detail.durationInDays) + '</li>';
        } else {
            html += strJoursFormat.format(this.detail.durationInDays) + '</li>';
        }
    });
    $(divSession).children('ul.details-session').html($(html));
    $(divSession).children('div.modifier-parcours').hide();
    $(divSession).children('ul.details-session').show();
    $('div.titre a', divSession).show();
    $('div.titre a:last', divSession).hide();
}
function getSessionByCode(a, code) {

    var product = getCurrentProduct($(a).parents(".panier"));

    for (var b = 0; b < product.sessions.length; b++) {
        if (product.sessions[b].code == code) {
            return product.sessions[b];
        }
    }
    return null;
}
function refreshDdlSessionsForStage(ddlVilles) {

    var product = getCurrentProduct($(ddlVilles).parents(".panier"));

    var selectedSession = [];
    var idSession = -1;
    if (getMySession(ddlVilles) != null) {
        idSession = getMySession(ddlVilles).id;
    }
    var optionsSessions = '';

    for (var i = 0; i < getCurrentSelectedSessions(ddlVilles).length; i++) {
        if (getCurrentSelectedSessions(ddlVilles)[i].id != idSession) {
            selectedSession.push(getCurrentSelectedSessions(ddlVilles)[i].id + '');
        }
    }

    for (var i = 0; i < product.sessions.length; i++) {
        if (product.sessions[i].placeId == $(ddlVilles).val()) {
            if ($.inArray(product.sessions[i].id + '', selectedSession) == -1) {
                optionsSessions += '<option value="' + product.sessions[i].id + '">' + sessionDateTextFormat.format(product.sessions[i].beginDate, product.sessions[i].endDate) + '</option>';
            }
        }
    }
    $(ddlVilles).next().html(optionsSessions);
    $(ddlVilles).next().val(idSession);
}
function refreshDdlSessionsForCycle(ddlVilles) {
    var product = getCurrentProduct($(ddlVilles).parents(".panier"));
    var selectedSession = [];
    var idSession = -1;
    if (getMySession(ddlVilles) != null) {
        idSession = getMySession(ddlVilles).id;
    }
    var optionsSessions = '';

    for (var i = 0; i < getCurrentSelectedSessions(ddlVilles).length; i++) {
        if (getCurrentSelectedSessions(ddlVilles)[i].id != idSession) {
            selectedSession.push(getCurrentSelectedSessions(ddlVilles)[i].id + '');
        }
    }

    for (var i = 0; i < product.sessions.length; i++) {
        if (product.sessions[i].placeId == $(ddlVilles).val()) {
            if ($.inArray(product.sessions[i].id + '', selectedSession) == -1) {
                optionsSessions += '<option class="group" value="' + product.sessions[i].id + '">' + sessionDateTextFormat.format(product.sessions[i].beginDate, product.sessions[i].endDate) + '</option>';
                for (var a = 0; a < product.sessions[i].detailSessions.length; a++) {
                    /*optionsSessions += '<optgroup label="-' + sessionDateTextFormat.format(product.sessions[i].detailSessions[a].beginDate, product.sessions[i].detailSessions[a].endDate) + '"></optgroup>';*/
                    optionsSessions += '<option disabled="disabled" class="group-option">' + sessionDateTextFormat.format(product.sessions[i].detailSessions[a].beginDate, product.sessions[i].detailSessions[a].endDate) + '</option>';
                }
            }
        }
    }
    $(ddlVilles).next().html(optionsSessions);
    $(ddlVilles).next().val(idSession);
}
function editParticipant(divParticipant) {
    $('.ajouter', panier).hide();
    $('.modifier', panier).hide();
    $('.supprimer', panier).hide();
    $('#bottom', panier).hide();
    $('.bottom', panier).hide();
    $(divParticipant).hide();
    $(divParticipant).next().show();
    $(divParticipant).next().find("div > span:first").html(strInfoParticipant);
    $(divParticipant).next().find('.annuler').show();
    $('a.ajouter', panier).show();

    hideButtons();

    try { $.fn.colorbox.resize(); } catch (err) { }

}
function deleteParticipant(divParticipant) {
    var product = getCurrentProduct($(divParticipant).parents(".panier"));
    if (!confirm(strConfirmDeleteParticipant)) {
        return;
    }

    var session = getMySession(divParticipant);
    var participantId = $(divParticipant).attr('participantId');
    var user = null;

    if (product.type.toUpperCase() == 'PARCOURSPRO') {
        for (var b = 0; b < getMyPromotion(divParticipant).users.length; b++) {
            if (getMyPromotion(divParticipant).users[b].id == participantId) {
                user = getMyPromotion(divParticipant).users[b];
                break;
            }
        }
    } else if (product.type.toUpperCase() == 'ELEARNINGPACKAGE') {
        for (var b = 0; b < product.users.length; b++) {
            if (product.users[b].id == participantId) {
                user = product.users[b];
                break;
            }
        }
    } else {
        for (var b = 0; b < session.users.length; b++) {
            if (session.users[b].id == participantId) {
                user = session.users[b];
                break;
            }
        }
    }


    var onSuccess = function() {
        if (product.type.toUpperCase() == 'PARCOURSPRO') {
            for (var b = 0; b < getMyPromotion(divParticipant).users.length; b++) {
                if (getMyPromotion(divParticipant).users[b].id + '' == participantId) {
                    getMyPromotion(divParticipant).users.splice(b, 1);

                    $(divParticipant).nextAll('.participant').each(function(index) {
                        getMyPromotion(divParticipant).users[b].id = b;
                        $(this).attr('participantId', b);
                        b++;
                    });
                    refreshParcoursProNbPersonAndPrice(divParticipant);
                    $(divParticipant).next().remove();

                    if (getMyPromotion(divParticipant).users.length == 0) {
                        deletePromotion($(divParticipant));
                    }
                    $(divParticipant).remove();
                    break;
                }
            }
        } else if (product.type.toUpperCase() == 'ELEARNINGPACKAGE') {
            for (var b = 0; b < product.users.length; b++) {
                if (product.users[b].id + '' == participantId) {
                    product.users.splice(b, 1);

                    $(divParticipant).nextAll('.participant').each(function(index) {
                        product.users[b].id = b;
                        $(this).attr('participantId', b);
                        b++;
                    });
                    refreshNbPersonAndPrice(divParticipant);
                    if (product.users.length == 0) {
                        if (typeof deleteProductWithoutConfirm == "function") {
                            deleteProductWithoutConfirm($(divParticipant).parents(".panier"));
                        }
                        else {
                            $(divParticipant).next().show();
                            $(divParticipant).show();
                            editParticipant(divParticipant);
                            $(divParticipant).next().find('.annuler').hide();
                            $(divParticipant).next().find('input').val('');
                            $(divParticipant).next().find('[name=civil]')[0].selectedIndex = 0;
                            $(divParticipant).next().attr('participantId', '');
                            $(divParticipant).attr('participantId', '');

                            $(divParticipant).next().find(".memoire")[0].selectedIndex = 0;

                            actionButtons.html(actionButtonsEdit);
                            $("div.cancel", actionButtons).hide();

                            $("a.addParticipant", actionButtons).click(actionButtonsEdit.insertParticipant);
                            $("a.cancel", actionButtons).click(actionButtonsEdit.cancelInsertParticipant);

                            $("a.cancel", actionButtonsEdit).text(strCancel);
                            $("a.addParticipant", actionButtonsEdit).text(strAddThisParticipant);

                            if (typeof (DoCufon) != 'undefined') { DoCufon(); }
                        }
                    } else {
                        $(divParticipant).next().remove();
                        $(divParticipant).remove();
                        break;
                    }
                }
            }
        } else {
            for (var b = 0; b < session.users.length; b++) {
                if (session.users[b].id + '' == participantId) {
                    session.users.splice(b, 1);

                    $(divParticipant).nextAll('.participant').each(function(index) {
                        getMySession(divParticipant).users[b].id = b;
                        $(this).attr('participantId', b);
                        b++;
                    });
                    if (session.users.length == 0) {
                        deleteSession($(divParticipant).prevAll('.titre'));
                        break;
                    }
                    refreshNbPersonAndPrice(divParticipant);
                    $(divParticipant).next().remove();
                    $(divParticipant).remove();
                    break;
                }
            }
        }
        if (typeof (refreshTotalPrice) != 'undefined') {
            refreshTotalPrice();
        }
        try { $.fn.colorbox.resize(); } catch (err) { }

    };
    if (updateBasketOnEachAction) {
        if (product.type.toUpperCase() == 'PARCOURSPRO') {
            wsDeleteParcoursProParticipant(product.id, getMyPromotion(divParticipant).id, user.civiliteId, user.firstName, user.lastName, user.mail, user["function"], onSuccess, function() {
                alert('An error occured.');
            });
        } else if (product.type.toUpperCase() == 'ELEARNINGPACKAGE') {
            wsDeleteELearningParticipant(product.id, user.civiliteId, user.firstName, user.lastName, user.mail, user["function"], onSuccess, function() {
                alert('An error occured.');
            });
        } else {
            wsDeleteParticipant(product.id, session.id, user.civiliteId, user.firstName, user.lastName, user.mail, user["function"], onSuccess, function() {
                alert('An error occured.');
            });
        }
    } else {
        onSuccess();
    }
    try { $.fn.colorbox.resize(); } catch (err) { }


}
function showAddNewPromotion(ctrl) {
    var divPanierPro = $(ctrl).parents('.pro');
    $('div.ajouter-date select[name=ville]', divPanierPro).each(function() {
        $(this).find('option:first').attr('selected', 'selected');
        $(this).change();
    });
    $('div.ajouter-date', divPanierPro).show();
    $('div.modifier-participant select', divPanierPro).each(function() {
        $(this).find('option:first').attr('selected', 'selected');
    });
    $(divPanierPro).children('div.modifier-participant').find('input:checkbox').attr('checked', false);
    $(divPanierPro).children('div.modifier-participant').find('input[type=text]').each(function() { $(this).val($(this).attr('defaultValueString') + '*').blur(); });
    $(divPanierPro).children('div.modifier-participant').show();
    $('.modifier').hide();
    $('.supprimer').hide();
    $('.pro div.bottom').hide();
    $('.modifier-date').hide();

    $('div.modifier-participant .annuler', divPanierPro).html(strCancel);
    if (refreshParcoursProNbPersonAndPrice(divPanierPro) == 0) {
        $('div.modifier-participant .annuler', divPanierPro).hide();
    } else {
        $('div.modifier-participant .annuler', divPanierPro).show();
    }

    $('.pro .bottom').hide();
    $('.modifier').show();
    $('.supprimer').show();
    $('.ajouter').hide();

    actionButtons.html(actionButtonsEdit);
    if (price !== 0) {
        $('div.cancel', actionButtons).show();
    }
    else {
        $('div.cancel', actionButtons).hide();
    }

    $('a.addParticipant', actionButtons).click(actionButtonsEdit.insertSession);
    $('a.cancel', actionButtons).click(actionButtonsEdit.cancelInsertSession);

    $("a.addParticipant", actionButtons).text(strAddThisParticipant);

    hideButtons();

    if (typeof basket == "object") {

        $("a.addParticipant", actionButtonsEdit).unbind('click');
        $("a.addParticipant", actionButtonsEdit).unbind('click');

        $("a.addParticipant", actionButtonsEdit).click(actionButtonsEdit.insertPromotionParticipant);
        $("a.cancel", actionButtonsEdit).click(actionButtonsEdit.cancelInsertPromotionParticipant);

        $("a.addParticipant", actionButtonsEdit).text(strAddThisParticipant);

        actionButtonsEdit.insertAfter($(".bottom", divPanierPro));

        $("div.options:visible > p:has(strong:contains('Options')):has(+ ul:hidden)").hide();

        $("div.options:visible > p:empty:visible + ul:visible").hide();
    }

    if (typeof (DoCufon) != 'undefined') { DoCufon(); }

    try { $.fn.colorbox.resize(); } catch (err) { }

}
function cancelAddNewPromotion(a) {
    $(a).parents('div.modifier-participant').prev().hide();
    $(a).parents('div.modifier-participant').hide();

    $('.modifier').show();
    $('.supprimer').show();
    $('.pro div.bottom').show();
    $('.modifier-date').show();
    $('.ajouter-participant').show();

    actionButtons.html(actionButtonsDefault)

    showButtons();

    /*if(typeof basket == "object"){
    hideAddNewSession();
    }*/

    if (typeof (DoCufon) != 'undefined') { DoCufon(); }

    try { $.fn.colorbox.resize(); } catch (err) { }

}
function updateParticipant(divParticipant) {
    var product = getCurrentProduct($(divParticipant).parents(".panier"));
    if (!participantIsValid($(divParticipant).next())) {
        return;
    }
    var session = getMySession(divParticipant);

    var participantId = $(divParticipant).attr('participantId');
    var oldUser = null;
    var newUser = {
        'civiliteId': $(divParticipant).next().find('select#civil').val(),
        'firstName': $(divParticipant).next().find('input[name=prenom]').val(),
        'lastName': $(divParticipant).next().find('input[name=nom]').val(),
        'mail': $(divParticipant).next().find('input[name=mail]').val(),
        'function': $(divParticipant).next().find('input[name=fonction]').val(),
        'options': [],
        'precisions': []
    };
    $(divParticipant).next().find('div.options > ul:first li input').each(function(index) {
        for (var c = 0; c < product.options.length; c++) {
            if (product.options[c].id == $(this).attr('value')) {
                if ($(this).attr('checked')) {
                    newUser.options.push(product.options[c].id);
                }
            }
        }
    });
    $(divParticipant).next().find('div.options > ul ~ ul li input').each(function(index) {
        for (var c = 0; c < product.precisions.length; c++) {
            if (product.precisions[c].id == $(this).attr('value')) {
                if ($(this).attr('checked')) {
                    newUser.precisions.push(product.precisions[c].id);
                }
            }
        }
    });

    if (participantId != null && parseInt(participantId) > -1) {

        if (product.type.toUpperCase() == 'PARCOURSPRO') {
            for (var b = 0; b < getMyPromotion(divParticipant).users.length; b++) {
                if (getMyPromotion(divParticipant).users[b].id == participantId) {
                    oldUser = getMyPromotion(divParticipant).users[b];
                    break;
                }
            }
        } else if (product.type.toUpperCase() == 'ELEARNINGPACKAGE') {
            for (var b = 0; b < product.users.length; b++) {
                if (product.users[b].id == participantId) {
                    oldUser = product.users[b];
                    break;
                }
            }
        } else {
            for (var b = 0; b < session.users.length; b++) {
                if (session.users[b].id == participantId) {
                    oldUser = session.users[b];
                    break;
                }
            }
        }
        var onSuccess = function() {
            oldUser.civiliteId = newUser.civiliteId;
            oldUser.firstName = newUser.firstName;
            oldUser.lastName = newUser.lastName;
            oldUser.mail = newUser.mail;
            oldUser["function"] = newUser["function"];
            oldUser.options = newUser.options;
            oldUser.precisions = newUser.precisions;
            if (product.type.toUpperCase() == 'PARCOURSPRO') {
                bindDataToParticipant(divParticipant, oldUser, true);
            } else {
                bindDataToParticipant(divParticipant, oldUser);
            }
            cancelEditParticipant(divParticipant);
        };
        if (updateBasketOnEachAction) {
            if (product.type.toUpperCase() == 'PARCOURSPRO') {
                wsUpdateParcoursProParticipant(product.id, getMyPromotion(divParticipant).id, oldUser.civiliteId, oldUser.firstName, oldUser.lastName, oldUser.mail, oldUser["function"], newUser.civiliteId, newUser.firstName, newUser.lastName, newUser.mail, newUser["function"], newUser.options, newUser.precisions, onSuccess,
                    function() {
                        alert('An error occured.');
                    });
            } else if (product.type.toUpperCase() == 'ELEARNINGPACKAGE') {
                wsUpdateELearningParticipant(product.id, oldUser.civiliteId, oldUser.firstName, oldUser.lastName, oldUser.mail, oldUser["function"], newUser.civiliteId, newUser.firstName, newUser.lastName, newUser.mail, newUser["function"], newUser.options, newUser.precisions, onSuccess,
                    function() {
                        alert('An error occured.');
                    });
            } else {
                wsUpdateParticipant(product.id, session.id, oldUser.civiliteId, oldUser.firstName, oldUser.lastName, oldUser.mail, oldUser["function"], newUser.civiliteId, newUser.firstName, newUser.lastName, newUser.mail, newUser["function"], newUser.options, newUser.precisions, onSuccess,
                    function() {
                        alert('An error occured.');
                    });
            }
        } else {
            onSuccess();
        }

    } else {
        var onSuccess2 = function() {
            var isPro = false;
            if (product.type.toUpperCase() == 'PARCOURSPRO') {
                newUser.id = getMyPromotion(divParticipant).users.length;
                $(divParticipant).attr('participantId', getMyPromotion(divParticipant).users.length);
                getMyPromotion(divParticipant).users.push(newUser);
                isPro = true;
            } else if (product.type.toUpperCase() == 'ELEARNINGPACKAGE') {
                newUser.id = product.users.length;
                $(divParticipant).attr('participantId', product.users.length);
                product.users.push(newUser);
            } else {
                newUser.id = session.users.length;
                $(divParticipant).attr('participantId', session.users.length);
                session.users.push(newUser);
            }
            bindDataToParticipant(divParticipant, newUser, isPro);
            cancelEditParticipant(divParticipant);
        };
        if (updateBasketOnEachAction) {
            if (product.type.toUpperCase() == 'PARCOURSPRO') {
                wsAddParcoursProParticipant(product.id, getMyPromotion(divParticipant).id, newUser.civiliteId, newUser.firstName, newUser.lastName, newUser.mail, newUser["function"], newUser.options, newUser.precisions, onSuccess2, function() {
                    alert('An error occured.');
                });
            } else if (product.type.toUpperCase() == 'ELEARNINGPACKAGE') {
                wsAddELearningParticipant(product.id, newUser.civiliteId, newUser.firstName, newUser.lastName, newUser.mail, newUser["function"], newUser.options, newUser.precisions, onSuccess2, function() {
                    alert('An error occured.');
                });
            } else {
                wsAddParticipant(product.id, session.id, newUser.civiliteId, newUser.firstName, newUser.lastName, newUser.mail, newUser["function"], newUser.options, newUser.precisions, onSuccess2, function() {
                    alert('An error occured.');
                });
            }
        } else {
            onSuccess2();
        }
    }

    showButtons();
    addParticipantWidth();

    if (typeof hideAddNewSession == "function") {
        hideAddNewSession();
    }
}

function insertPromotionParticipant(ctrl) {
    var product = getCurrentProduct($(ctrl).parents(".panier"));
    if (!participantIsValid($(ctrl).parents('.modifier-participant'))) {
        return;
    }
    var proDiv = $(ctrl).parents('div.pro');


    var e = $(proDiv).children('div.ajouter-date:last');
    var f = $(proDiv).children('div.modifier-participant:last');


    var ids = '';
    $('select[name=date]', e).each(function() {
        ids += $(this).val() + ";";
    });
    var onSuccess = function() {
        var tmp = $('#templateSession', templates).children().clone(true);
        $(tmp).insertBefore($('div.ajouter-date', proDiv));
        var s = $(proDiv).children('.session:last');
        $(s).attr('sessionId', product.promotions.length);

        initAddNewpromotion($('div.modifier-parcours ul', s));
        product.promotions.push(promotion);
        bindPromotionToHeader(ctrl, promotion);
        $(e).hide();
        $(f).hide();

        tmp = $('#templateParticipant', templates).children().clone(true);
        $(tmp).insertBefore($('div.ajouter-participant', s));

        bindDataToParticipant($('div.participant:last', s), promotion.users[0], true);
        cancelEditParticipant($('div.participant:last', s));
        refreshParcoursProNbPersonAndPrice(ctrl);
        if (typeof (refreshTotalPrice) != 'undefined') {
            refreshTotalPrice();
        }
        $('.bottom', proDiv).show();
    };

    var civil = $('select[name=civil]', f);
    var ctrlPrenom = $('input[name=prenom]', f);
    var ctrlNom = $('input[name=nom]', f);
    var ctrlMail = $('input[name=mail]', f);
    var ctrlFonction = $('input[name=fonction]', f);

    var promotion = {
        'id': $('.session', proDiv).length - 1,
        'ids': ids,
        'sessionDetails': [],
        'users':
        [
        {
            'id': 0,
            'civiliteId': $(civil).val(),
            'firstName': $(ctrlPrenom).val(),
            'lastName': $(ctrlNom).val(),
            'mail': $(ctrlMail).val(),
            'function': $(ctrlFonction).val(),
            'options': [],
            'precisions': []
        }
        ]

    };
    $('ul.details-session li', e).each(function(index) {
        var session = getSessionByCode(ctrl, $(this).attr('sessionCode'));

        var sessionDetail = null;
        for (var a = 0; a < session.sessionDetails.length; a++) {
            if (session.sessionDetails[a].id == $(this).find('select[name=date]').val()) {
                sessionDetail = session.sessionDetails[a];
                break;
            }
        }

        var dataDetail = {
            title: session.title,
            code: session.code,
            detail: sessionDetail
        };
        promotion.sessionDetails.push(dataDetail);

        $('.ajouter', panier).show();
        $('.modifier', panier).show();
        $('.supprimer', panier).show();
        $('#bottom', panier).show();
        $('a.ajouter', panier).show();
    });
    $('div.options > ul li input[type=checkbox]:checked', f).each(function(index) {
        promotion.users[0].options.push($(this).val());
    });
    $('div.options > ul ~ul li input[type=checkbox]:checked', f).each(function(index) {
        promotion.users[0].precisions.push($(this).val());
    });
    if (updateBasketOnEachAction) {
        wsAddParcoursProSession(product.id, ids, promotion.users[0].civiliteId, promotion.users[0].firstName, promotion.users[0].lastName, promotion.users[0].mail, promotion.users[0]["function"], promotion.users[0].options, promotion.users[0].precisions, onSuccess, function() {
            alert('An error occured.');
        });
    } else {
        onSuccess();
    }

    try { $.fn.colorbox.resize(); } catch (err) { }

}

function initSelectCivil(selectCivil) {
    var options = '';
    for (var i = 0; i < civiliteData.length; i++) {
        options += '<option value="' + civiliteData[i] + '">' + civiliteData[i] + '</option>';
    }
    $(selectCivil).html(options);
}
function initSelectMemoire(selectMemoire, selectCivilToBind, selectPrenomToBind, selectNomToBind, selectMailToBind, selectFonctionToBind) {
    var options = '';
    for (var j = 0; j < participantsData.length; j++) {
        var d = participantsData[j].civiliteId + ';' + participantsData[j].firstName + ';' + participantsData[j].lastName + ';' + participantsData[j].mail + ';' + (participantsData[j]["function"] !== "" ? participantsData[j]["function"] : "");
        var d2 = strFirstItemParticipants;
        if (j > 0) {
            d2 = participantsData[j].civiliteId + ' ' + participantsData[j].firstName + ' ' + participantsData[j].lastName + (participantsData[j]["function"] !== "" ? ', ' + participantsData[j]["function"] : "") + ' - ' + participantsData[j].mail;
        }
        options += '<option value="' + d + '">' + d2 + '</option>';
    }
    if (options != '') {
        $(selectMemoire).html(options);
        $(selectMemoire).change(function() {
            if ($(this).val() == ';;;') {
                return;
            }
            var divParticipantEdit = $(this).parent().next();
            var values = $(this).val().split(';');
            $(selectCivilToBind).val(values[0]);

            $(selectPrenomToBind).val(values[1]);
            $(selectPrenomToBind).blur();
            $(selectNomToBind).val(values[2]);
            $(selectNomToBind).blur();
            $(selectMailToBind).val(values[3]);
            $(selectMailToBind).blur();
            $(selectFonctionToBind).val(values[4]);
            $(selectFonctionToBind).blur();

        });
    } else {
        $(selectMemoire).hide();
    }
}

function bindDataToParticipant(divParticipant, userData, isPro) {
    var product = getCurrentProduct($(divParticipant).parents(".panier"));
    $(divParticipant).attr('participantId', userData.id);
    $('span:first', divParticipant).html(userData.civiliteId + ' ' + userData.firstName + ' ' + userData.lastName + ', ' + userData["function"] + ' - ' + userData.mail);
    var rOptions = [];
    var pOptions = 0;

    var text = "";

    for (var numOption = 0; numOption < userData.options.length; numOption++) {
        if (userData.options[numOption] === "")
            continue;

        var option = $.grep(product.options, function(elem, index) { return elem.id === userData.options[numOption]; })[0];
        if (!option) {
            userData.options.splice(numOption, 1);
            numOption--;
            continue;
        }
        
        text += (text !== "" ? " <br/> " : "") + option.title + " : " + option.price + " " + product.price.symbol + (option.qty != 1 ? " x " + option.qty : "");
    }

    for (var numPrecision = 0; numPrecision < userData.precisions.length; numPrecision++) {
        if (userData.precisions[numPrecision] === "")
            continue;

        var option = $.grep(product.precisions, function(elem, index) { return elem.id === userData.precisions[numPrecision]; })[0];
        if (!option) {
            userData.precisions.splice(numPrecision, 1);
            numPrecision--;
            continue;
        }

        text += (text !== "" ? " <br/> " : "") + option.title;
    }

    $('#options', divParticipant).html(text);


    /*
    $(divParticipant).next().find('.options > ul li input').each(function(index) {
        
    });

    /*
    
    $(divParticipant).next().find('.options > ul:first li input').each(function(index) {
    for (var a = 0; a < product.options.length; a++) {
    if (product.options[a].id == $(this).attr('value')) {
    var r = '';
    if ($.inArray(product.options[a].id, userData.options) > -1) {
    $(this).attr('checked', true);
    r += product.options[a].title;
    if (product.options[a].qty > 1) {
    r += ' x ' + product.options[a].qty;
    }
    pOptions += product.options[a].price;
    } else {
    $(this).attr('checked', false);
    //r += strNot.format(product.options[a].title);
    }
    if (r != '') {
    rOptions.push(r);
    }
    break;
    }
    }
    });
    
    
    $(divParticipant).next().find('.options > ul ~ ul li input').each(function(index) {
    for (var a = 0; a < product.precisions.length; a++) {
    if (product.precisions[a].id == $(this).attr('value')) {
    var r = '';
    if ($.inArray(product.precisions[a].id, userData.precisions) > -1) {
    $(this).attr('checked', true);
    r += product.precisions[a].title;
    } else {
    $(this).attr('checked', false);
    }
    if (r != '') {
    rOptions.push(r);
    }
    break;
    }
    }
    });

    if (rOptions.length == 0) {
    $('#options', divParticipant).html('');
    } else {
    $('#options', divParticipant).html('+ ' + pOptions.toBasketPrice() + ' (' + rOptions.join(' / ') + ')');
    }*/

    $(divParticipant).next().find('select[name=civil]').val(userData.civiliteId);
    $(divParticipant).next().find('input[name=prenom]').val(userData.firstName);
    $(divParticipant).next().find('input[name=nom]').val(userData.lastName);
    $(divParticipant).next().find('input[name=mail]').val(userData.mail);
    $(divParticipant).next().find('input[name=fonction]').val(userData["function"]);
    $(divParticipant).next().find('input').blur();
    if (isPro == null || isPro == false) {
        refreshNbPersonAndPrice(divParticipant);
    } else {
        refreshParcoursProNbPersonAndPrice(divParticipant);
    }
    if (typeof (refreshTotalPrice) != 'undefined') {
        refreshTotalPrice();
    }
}
function showOptions(divModifierparticipant) {
    $('div.options', divModifierparticipant).show();
    $('div.options p:first', divModifierparticipant).show();
    $('div.options ul:first', divModifierparticipant).show();
}
function showPrecisions(divModifierparticipant) {
    $('div.options', divModifierparticipant).show();
    $('div.options p:last', divModifierparticipant).show();
    $('div.options ul:last', divModifierparticipant).show();
}
function hideOptions(divModifierparticipant) {
    if ($('div.options p:last', divModifierparticipant).css('display') == 'none') {
        $('div.options', divModifierparticipant).show();
    }
    $('div.options p:first', divModifierparticipant).hide();
    $('div.options ul:first', divModifierparticipant).hide();
}
function hidePrecisions(divModifierparticipant) {
    if ($('div.options p:first', divModifierparticipant).css('display') == 'none') {
        $('div.options', divModifierparticipant).hide();
    }
    $('div.options p:last', divModifierparticipant).hide();
    $('div.options ul:last', divModifierparticipant).hide();
}
function initOptions(ulOptions) {
    var product = getCurrentProduct($(ulOptions).parents(".panier"));
    var html = '';
    $(product.options).each(function(index) {
        if (this.qty > 1) {
            html += '<li><input id="option' + this.id + '" type="checkbox" value="' + this.id + '"><label for="option' + this.id + '">' + this.title + ' : ' + this.price + ' ' + product.price.symbol + ' x  ' + this.qty + '</label></li>';
        }
        else {
            html += '<li><input id="option' + this.id + '" type="checkbox" value="' + this.id + '"><label for="option' + this.id + '">' + this.title + ' : ' + this.price + ' ' + product.price.symbol + '</label></li>';
        }
    });
    $(ulOptions).html(html);
    $(ulOptions).prev().children().html(strOptions);
    if (html != '') {
        showOptions($(ulOptions).parents('div'));
    } else {
        hideOptions($(ulOptions).parents('div'));
    }
}
function initPrecisions(ulPrecisions) {
    var product = getCurrentProduct($(ulPrecisions).parents(".panier"));
    var html = '';
    $(product.precisions).each(function(index) {
        html += '<li><input id="precision' + this.id + '" type="checkbox" value="' + this.id + '"><label for="precision' + this.id + '">' + this.title + '</label></li>';
    });
    $(ulPrecisions).html(html);
    $(ulPrecisions).prev().children().html(strPrecisions);
    if (html != '') {
        showPrecisions($(ulPrecisions).parents('div'));
    } else {
        hidePrecisions($(ulPrecisions).parents('div'));
    }
}

function editSession(divTitre) {
    var product = getCurrentProduct($(divTitre).parents(".panier"));
    if (product.type.toUpperCase() == "CYCLELONG") {
        $('.details-session', panier).hide();
    }
    if (product.type.toUpperCase() != "PARCOURSPRO") {
        refreshDdlVilles($('#ville', divTitre));
        $('#ville', divTitre).val(getMySession(divTitre).placeId);
        $('#ville', divTitre).change();
        $('#date', divTitre).val(getMySession(divTitre).id);
    }

    $(divTitre).addClass('edition');

    $('.modifier-session', divTitre).show();
    $('.afficher-session', divTitre).hide();
    $('.validation', divTitre).show();
    $('.annuler', divTitre).show();

    $('.ajouter', panier).hide();
    $('.modifier', panier).hide();
    $('.supprimer', panier).hide();
    $('#bottom', panier).hide();
    $('a.ajouter', panier).show();
    try { $.fn.colorbox.resize(); } catch (err) { }

}
function cancelEditSession(divTitre) {
    var product = getCurrentProduct($(divTitre).parents(".panier"));
    if (product.type.toUpperCase() == "CYCLELONG") {
        $('.details-session', panier).show();
    }

    $(divTitre).removeClass('edition');

    $('.modifier-session', divTitre).hide();
    $('.afficher-session', divTitre).show();
    $('.validation', divTitre).hide();
    $('.annuler', divTitre).hide();

    $('.ajouter', panier).show();
    $('.modifier', panier).show();
    $('.supprimer', panier).show();
    $('#bottom', panier).show();

    showButtons();

    try { $.fn.colorbox.resize(); } catch (err) { }

}
function updateSession(divTitre) {
    var product = getCurrentProduct($(divTitre).parents(".panier"));
    var oldSessionId = $(divTitre).parent().attr('sessionId');
    var newSessionId = $(divTitre).parent().find('p.modifier-session select#date').val();
    var newSession = null;

    var onSuccess = function() {

        for (var a = 0; a < product.sessions.length; a++) {
            if (product.sessions[a].id == newSessionId) {
                newSession = product.sessions[a];
                break;
            }
        }
        if (getMySession(divTitre).id == oldSessionId) {
            getMySession(divTitre).id = newSessionId;
        }
        var prix = $(divTitre).parent().find('span:first');

        $(prix).next().next().html(newSession.place);
        $(prix).next().next().next().next().html(sessionDateTextFormat.format(newSession.beginDate, newSession.endDate));
        $(divTitre).parent().attr('sessionId', newSessionId);

        if (product.type.toUpperCase() == "CYCLELONG") {
            var ulInnerHTML = '';
            for (var s = 0; s < newSession.detailSessions.length; s++) {
                ulInnerHTML += '<li>{0}</li>'.format(sessionDateTextFormat.format(newSession.detailSessions[s].beginDate, newSession.detailSessions[s].endDate));
            }
            $('.details-session', panier).html(ulInnerHTML);
        }
        $(divTitre).show();
        cancelEditSession(divTitre);
    };
    if (updateBasketOnEachAction) {
        wsUpdateSession(product.id, oldSessionId, newSessionId, onSuccess, function() {
            alert('An error occured.');
        });
    } else {
        onSuccess();
    }
}
function updatePromotion(ctrl) {
    var product = getCurrentProduct($(ctrl).parents(".panier"));
    var divSession = $(ctrl).parents('div.session');
    var promotion = product.promotions[$(divSession).attr('sessionId')];

    var ids = '';
    $('select[name=date]', divSession).each(function() {
        ids += $(this).val() + ";";
    });
    var onSuccess = function() {
        promotion.ids = ids;
        promotion.sessionDetails = [];
        var e = $('div.modifier-parcours:first', divSession);
        $('ul.details-session li', e).each(function(index) {
            var session = getSessionByCode(ctrl, $(this).attr('sessionCode'));

            var sessionDetail = null;
            for (var a = 0; a < session.sessionDetails.length; a++) {
                if (session.sessionDetails[a].id == $(this).find('select[name=date]').val()) {
                    sessionDetail = session.sessionDetails[a];
                    break;
                }
            }

            var dataDetail = {
                title: session.title,
                code: session.code,
                detail: sessionDetail
            };
            promotion.sessionDetails.push(dataDetail);
        });
        $('div.titre', divSession).removeClass('edition');
        bindPromotionToHeader(ctrl, promotion);
        $(divSession).parents('.pro').find('.bottom').show();
    }
    if (updateBasketOnEachAction) {
        wsUpdateParcoursProSession(product.id, promotion.id, ids, onSuccess, function() {
            alert('An error occured.');
        });
    } else {
        onSuccess();
    }

    showButtons();

    try { $.fn.colorbox.resize(); } catch (err) { }
}

function deleteSessionWithConfirm(divTitre) {
    if (!confirm(strConfirmDeleteSession)) {
        return;
    }
    deleteSession(divTitre);
    try { $.fn.colorbox.resize(); } catch (err) { }

}
function deletePromotionWithConfirm(divTitre) {
    if (!confirm(strConfirmDeletePromotion)) {
        return;
    }
    deletePromotion(divTitre);
    try { $.fn.colorbox.resize(); } catch (err) { }

}
function refreshDdlVilles(selectVille) {
    var product = getCurrentProduct($(selectVille).parents(".panier"));
    var idSession = parseInt($(selectVille).parents('.session').attr('sessionId'));
    var idPlace = '';
    if (idSession != null) {
        for (var i = 0; i < getCurrentSelectedSessions(selectVille).length; i++) {
            if (getCurrentSelectedSessions(selectVille)[i].id == idSession) {
                idPlace = getCurrentSelectedSessions(selectVille)[i].placeId;
                break;
            }
        }
    }
    var allPlaces = [];
    for (var i = 0; i < product.sessions.length; i++) {
        allPlaces.push(product.sessions[i].placeId);
    }

    var result = [];
    var options = '';
    for (var i = 0; i < allPlaces.length; i++) {
        if ($.inArray(allPlaces[i], result) == -1) {
            if (idPlace == allPlaces[i]) {
                options += '<option value="' + allPlaces[i] + '">' + allPlaces[i] + '</option>';
                result.push(allPlaces[i]);
            } else {
                var lengthOfSelectedPlaces = 0;
                var lengthOfPlaces = 0;
                for (var j = 0; j < product.sessions.length; j++) {
                    if (product.sessions[j].placeId == allPlaces[i]) {
                        lengthOfPlaces++;
                    }
                }
                for (var j = 0; j < getCurrentSelectedSessions(selectVille).length; j++) {
                    if (getCurrentSelectedSessions(selectVille)[j].placeId == allPlaces[i]) {
                        lengthOfSelectedPlaces++;
                    }
                }
                if (lengthOfPlaces > lengthOfSelectedPlaces) {
                    options += '<option value="' + allPlaces[i] + '">' + allPlaces[i] + '</option>';
                    result.push(allPlaces[i]);
                }
            }
        }
    }
    $(selectVille).html(options);
    if (result.length > 0) {
        $(selectVille).val(result[0]);
        $(selectVille).change();
    }
}

function cancelEditParticipant(divParticipant) {
    var product = getCurrentProduct($(divParticipant).parents(".panier"));
    var participantId = parseInt($(divParticipant).attr('participantId'));
    if (participantId > -1) {
        if (product.type.toUpperCase() == 'PARCOURSPRO') {
            for (var b = 0; b < getMyPromotion(divParticipant).users.length; b++) {
                if (getMyPromotion(divParticipant).users[b].id == participantId) {
                    bindDataToParticipant($(divParticipant), getMyPromotion(divParticipant).users[b], true);
                    break;
                }
            }
        } else if (product.type.toUpperCase() != 'ELEARNINGPACKAGE') {
            for (var b = 0; b < getMySession(divParticipant).users.length; b++) {
                if (getMySession(divParticipant).users[b].id == participantId) {
                    bindDataToParticipant($(divParticipant), getMySession(divParticipant).users[b]);
                    break;
                }
            }
        }
        $(divParticipant).show();
        $(divParticipant).next().hide();
    } else {
        $(divParticipant).next().remove();
        $(divParticipant).remove();
    }
    $('.ajouter', panier).show();
    $('.modifier', panier).show();
    $('.supprimer', panier).show();
    $('#bottom', panier).show();
    $('.bottom', panier).show();

    actionButtons.html(actionButtonsDefault);

    showButtons();

    if (typeof (DoCufon) != 'undefined') { DoCufon(); }

    try { $.fn.colorbox.resize(); } catch (err) { }
}

String.prototype.format = function() {
    var pattern = /\{\d+\}/g;
    var args = arguments;
    return this.replace(pattern, function(capture) {
        return args[capture.match(/\d+/)];
    });
};
Number.prototype.toBasketPrice = function() {
    var sRetour = "";
    var sNombre = this;
    if (this == 0) {
        return strPriceFormat.format("0");
    }
    while (sNombre.length % 3 != 0) {
        sNombre = "0" + sNombre;
    }
    for (i = 0; i < sNombre.length; i += 3) {
        if (i == sNombre.length - 1) separateurMilliers = '';
        sRetour += sNombre.substr(i, 3) + separateurMilliers;
    }
    while (sRetour.substr(0, 1) == "0") {
        sRetour = sRetour.substr(1);
    }
    return strPriceFormat.format(sRetour.substr(0, sRetour.lastIndexOf(separateurMilliers)).trim());
}

function participantIsValid(participantEdit) {
    var isValid = true;
    var ctrlPrenom = $('input[name=prenom]', participantEdit);
    var ctrlNom = $('input[name=nom]', participantEdit);
    var ctrlMail = $('input[name=mail]', participantEdit);
    var ctrlFunction = $('input[name=fonction]', participantEdit);
    var errors = '';

    if ($(ctrlPrenom).val() == null || $(ctrlPrenom).val() == '' || $(ctrlPrenom).val() == strPrenom + '*') {
        $(ctrlPrenom).next().css('visibility', 'visible');
        isValid = false;
        errors += '<li>' + strFieldErrorMessage.format(strPrenom) + '</li>';
    } else {
        $(ctrlPrenom).next().css('visibility', 'hidden');
    }
    if ($(ctrlNom).val() == null || $(ctrlNom).val() == '' || $(ctrlNom).val() == strNom + '*') {
        $(ctrlNom).next().css('visibility', 'visible');
        isValid = false;
        errors += '<li>' + strFieldErrorMessage.format(strNom) + '</li>';
    } else {
        $(ctrlNom).next().css('visibility', 'hidden');
    }
    if ($(ctrlMail).val() == null || $(ctrlMail).val() == '' || !regMail.test($(ctrlMail).val()) || $(ctrlMail).val() == strMail + '*') {
        $(ctrlMail).next().css('visibility', 'visible');
        isValid = false;
        errors += '<li>' + strFieldErrorMessage.format(strMail) + '</li>';
    } else {
        $(ctrlMail).next().css('visibility', 'hidden');
    }
    if ($(ctrlFunction).val() == null || $(ctrlFunction).val() == '' || $(ctrlFunction).val() == strFunction + '*') {
        $(ctrlFunction).next().css('visibility', 'visible');
        isValid = false;
        errors += '<li>' + strFieldErrorMessage.format(strFunction) + '</li>';
    } else {
        $(ctrlFunction).next().css('visibility', 'hidden');
    }
    if (!isValid) {
        $('.erreur').html('<ul>' + errors + '</ul>');
        $('.erreur').show();
    } else {
        $('.erreur').hide();
        $('.erreur').html('');
    }
    if (!isValid) {
        try { $.fn.colorbox.resize(); } catch (err) { }

    }

    return isValid;
}
