﻿//Fonctions pour le carousel
c_current_index = 0;
function c_next() {
    next_index = c_current_index + 1 < document.getElementsByName('screens').length ? c_current_index + 1 : 0;
    c_move(next_index);
}
function c_prev() {
    next_index = c_current_index - 1 >= 0 ? c_current_index - 1 : document.getElementsByName('screens').length - 1;
    c_move(next_index);
}
function c_move(next_index) {
    move = next_index * -960;

    next_image = document.getElementsByName('screens')[next_index];
    current_image = document.getElementsByName('screens')[c_current_index];
    if (!next_image.complete) {
        //pour tenter un nouveau reload si l'image n'a pas réussi à s'afficher (bug IE 6)
        next_image.src = next_image.src;
    }

    if (next_image != current_image) {
        $(document.getElementById('ctl00_XmlPlaceHolder_Slider')).fadeOut(400);
        $(next_image).animate({ height: getNaturalHeight(next_image) }, { duration: 500 });
        $(current_image).animate({ height: 50 }, { duration: 2000 });

        $("#ctl00_XmlPlaceHolder_Carousel").animate({
            marginLeft: move + "px"
        }, 500, function() {
            $(document.getElementById('ctl00_XmlPlaceHolder_Slider')).fadeIn(400);
            for (i = 0; i < document.getElementsByName('c_slider_buttons').length; i++)
                document.getElementsByName('c_slider_buttons')[i].parentNode.className = "off";
            document.getElementsByName('c_slider_buttons')[c_current_index].parentNode.className = "on";
        });
    }
    c_current_index = next_index;
}

function getNaturalHeight(img) {
    if (typeof img.naturalHeight == "undefined") {
        var temp_image = new Image();
        temp_image.src = img.src;
        return temp_image.height;
    } else {
        return img.naturalHeight;
    }
}
//pour le carousel homepage
function c_move2(next_index) {
    move = next_index * -960;
    $("#ctl00_cLayer").animate({ marginLeft: move + "px" }, 500, null);
    for (i = 0; i < document.getElementById('ctl00_PageMenu').childNodes[0].childNodes.length; i++) {
        document.getElementById('ctl00_PageMenu').childNodes[0].childNodes[i].className = "";
    }
    document.getElementById('ctl00_PageMenu').childNodes[0].childNodes[next_index].className = "cItemOn";
    c_current_index = next_index;
}
function cScroll() {
    c_current_index = c_current_index < document.getElementById('ctl00_PageMenu').childNodes[0].childNodes.length - 1 ? c_current_index + 1 : 0;
    c_move2(c_current_index);
}

//fonctions pour les modal popup
function ShowPopup(popupId) {
    var htmlElement = document.getElementsByTagName('html')[0];
    htmlElement.style.overflow = 'hidden';

    popup = document.getElementById(popupId);
    popupBackground = popup.children[0];
    popupWrapper = popup.children[1];
    popupContent = popup.children[1].children[1];

    $(popup).animate({
        height: document.documentElement.clientHeight,
        width: document.documentElement.clientWidth,
        left: document.body.parentNode.scrollLeft,
        top: document.body.parentNode.scrollTop
    }, 0);

    $(popupBackground).animate({ opacity: 0.6 }, 0);

    popupContent.style.overflow = "";
    popupContent.style.height = "";
    if (popupWrapper.clientHeight > (document.documentElement.clientHeight - 100)) {
        popupContent.style.overflow = "scroll";
        $(popupContent).animate({
            height: document.documentElement.clientHeight - 100
        }, 0);
    }

    $(popupWrapper).animate({
        left: (document.documentElement.clientWidth - popupWrapper.clientWidth) / 2,
        top: (document.documentElement.clientHeight - popupWrapper.clientHeight) / 2
    }, 0);
}

var isHiding = false;
function HidePopup(popupId) {
    isHiding = true;
    var htmlElement = document.getElementsByTagName('html')[0];
    htmlElement.style.overflow = '';

    popup = document.getElementById(popupId);
    popupBackground = popup.children[0];
    $(popupBackground).fadeOut(200);
    $(popup).fadeOut(500, function() {
        $(popupBackground).fadeIn(0);
        $(popupBackground).fadeIn(0);
    });
    isHiding = false;
}

function IsPopupOpen(popupId) {
    return (document.getElementById(popupId).style.display == "block" && !isHiding) ? true : false;
}

/// <summary>
///     Affiche la ressource technique dont l'id est placé en paramètre.
///     Rend invisible toutes les ressources techniques (via class area_off) puis affiche uniquement celle qui nous intéresse
/// </summary>
/// <param name="id_area">id de la balise DIV qui contient la ressource technique demandée</param>
function ShowTechnicalResources(id_area) {

    area = document.getElementById('ctl00_ContentPlaceHolder1_' + id_area);
    parentArea = area.parentNode;

    for (i = 0; i < parentArea.children.length; i++)
        parentArea.children[i].className = " area_off";

    area.className = "";
    tr_menu = document.getElementById('ctl00_ContentPlaceHolder1_TechnicalResourcesMenu').children[0];
    for (i = 0; i < tr_menu.children.length; i++) {
        tr_menu.children[i].className = tr_menu.children[i].children[0].href.substring(35).search(id_area) != -1 ? "on" : "";
    }
}

/// <summary>
///     Affiche la FAQ dont l'index est en paramètre
///     Rend invisible toutes les autres FAQs
///     Si la FAQ était déjà visible, on la remet invisible
/// </summary>
function ShowFaq(index_faq) {
    faqs = document.getElementById('ctl00_ContentPlaceHolder1_Faq');
    for (i = 0; i < faqs.children.length; i++)
        if (faqs.children[i].tagName == "DIV") {
            faq = faqs.children[i];
            //if (index_faq == i - 1 && faq.className != "faq_on"){
            if (index_faq == faq.children[0].name && faq.className != "faq_on") { 
                faq.className = "faq_on";
                $(faq.children[2]).slideDown(function() {
                    window.location.hash = index_faq;
                });
            }
            else {
                if (faq.className == "faq_on") {
                    faq.className = "faq_off";
                    $(faq.children[2]).slideUp(function() {
                        window.location.hash = index_faq;
                    });

                }
                else faq.className = "faq_off";
            }
        }
    
}
// Méthode pour sélectionner les partenaires selon la DropDownList (partners.htm)
function ChangePartnersList() {
    for (i = 0; i < document.getElementById('ctl00_ContentPlaceHolder1_PartnersBoxes').childNodes.length; i++) {
        if (document.getElementById('ctl00_ContentPlaceHolder1_PartnersBoxes').childNodes[i].className == 'partners')
            document.getElementById('ctl00_ContentPlaceHolder1_PartnersBoxes').childNodes[i].style.display = 'none';
    }
    dropList = document.getElementById('ctl00_ContentPlaceHolder1_DropDownListCountry');
    var value = dropList.options[dropList.selectedIndex].value;
    document.getElementById('ctl00_ContentPlaceHolder1_Partners' + value).style.display = 'block';
}

function ChangeProfileFormCountry(value) {          
    if(document.getElementById('ctl00_XmlPlaceHolder_PanelCurrency') != null){
        document.getElementById('ctl00_XmlPlaceHolder_PanelCurrency').style.display = value == 216 ? 'block' : 'none';
    }
    var stateCell = document.getElementById('ctl00_XmlPlaceHolder_ProfileForm1_StateCell');
    stateCell.style.display = value == 216 ? 'block' : 'none';

    var vatCell = document.getElementById('ctl00_XmlPlaceHolder_ProfileForm1_VATCell');
    var euroCountriesId = new Array(13, 20, 30, 52, 53, 54, 63, 68, 69, 74, 78, 91, 97, 99, 111, 117, 118, 126, 145, 164, 165, 169, 184, 185, 189, 197, 215);
    vatCell.style.display = 'none';
    ValidatorEnable(document.getElementById('ctl00_XmlPlaceHolder_ProfileForm1_REVATNumber'), false);
    for (i = 0; i < euroCountriesId.length; i++)
        if (euroCountriesId[i] == value && document.getElementById('ctl00_XmlPlaceHolder_ProfileForm1_Company').value != '') {
            vatCell.style.display = 'block';
            ValidatorEnable(document.getElementById('ctl00_XmlPlaceHolder_ProfileForm1_REVATNumber'), true);
            return;
        }
    }

//affiche input si clic sur checkbox other (formulaire become partenaire)
function OtherInput(checkbox, inputId) {
    document.getElementById('ctl00_ContentPlaceHolder1_' + inputId).style.display = checkbox.checked ? 'block' : 'none';
}
