//********* EFFACER LES ESPACES ************************
function trim(string) {
    return string.replace(/(^\s*)|(\s*$)/g, '');
}

function createXMLHttp() {
    var aVersions = [ "MSXML2.XMLHttp.5.0",
        "MSXML2.XMLHttp.4.0","MSXML2.XMLHttp.3.0",
        "MSXML2.XMLHttp","Microsoft.XMLHttp"
    ];
    var xhr;
    try {
        xhr = new XMLHttpRequest();
        return xhr;
    } catch(e) {
        for (var i = 0; i < aVersions.length; i++) {
            try {
                var oXmlHttp = new ActiveXObject(aVersions[i]);
                return oXmlHttp;
            } catch (oError) {
                //Do nothing
            }
        }
        throw new Error("MSXML is not installed or does not support AJAX.");
    }
}

function ajaxCall(xhr, URL) {
    var delimiter = (URL.indexOf("?") != -1) ? "&" : "?";
    URL = URL + delimiter + new Date().getTime();
    xhr.open("GET", URL, true);
    xhr.setRequestHeader("If-Modified-Since", "Thu, 1 Jan 1970 00:00:00 GMT");
    xhr.setRequestHeader("Cache-Control", "no-cache");
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
    xhr.send(null);
}

function getPath() {
    var path = window.location.pathname;
    var pos = path.lastIndexOf('.do');
    path = path.substring(0, pos);
    pos = path.lastIndexOf('/');
    return path.substring(0, pos);
}

//********* LISTE DES LANGUES APPRISES ************************
function listeLangueApprise() {
    var xhr = createXMLHttp();

    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4) {
            if (xhr.status == 200) {
                var e = document.getElementById('idLangueApprise');
                moteur = e.innerHTML;
                e.innerHTML = xhr.responseText;
            }
        }
    };

    var langueMaternelle = document.getElementById('idLangueMaternelle').value;
    ajaxCall(xhr, getPath() + "/listeLangueApprise.do?choixLangueMaternelle=" + langueMaternelle);
}

function afficheImage(urlImage, lienPhoto) {
    var image = document.getElementById("photo");
    var lien = document.getElementById("lienPhoto");
    image.src = urlImage;
    lien.href = "/photo.do?urlPhoto="+lienPhoto;
}

//**** afficher ou cacher le formulaire d'adresse de livraison ****

function adresseLivraison(visible, titreAdrFactIfVisible, titreAdrFactIfHidden) {
    if (visible) {
        document.getElementById('btnEtapeSuivante1').style.display = 'none';
        document.getElementById('divFormAdresseLivraison').style.display = 'block';
        document.getElementById('titreAdresse1').innerHTML = titreAdrFactIfVisible;
    } else {
        document.getElementById('btnEtapeSuivante1').style.display = 'block';
        document.getElementById('divFormAdresseLivraison').style.display = 'none';
        document.getElementById('titreAdresse1').innerHTML = titreAdrFactIfHidden;
    }
}

function rectoVerso(image,idBlock){
    if(image.indexOf("-front-") != -1)
        image = image.replace('-front-','-back-');
    else if(image.indexOf("-back-") != -1)
        image = image.replace('-back-','-front-');

    var xhr  = createXMLHttp();
    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4) {
            if (xhr.status == 200) {
                var e = document.getElementById(idBlock);
                $("#"+idBlock).fadeIn("slow");
                e.innerHTML = xhr.responseText;
            }
        }
    };
    ajaxCall(xhr, getPath() + 'photo.do?urlPhoto='+image);
}

/* Fonction d'affichage des items nouveautes */
function afficheItemInfos(link){
    var xhr  = createXMLHttp();
    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4) {
            if (xhr.status == 200) {
                var e = document.getElementById('itemInfos');
                e.innerHTML = xhr.responseText;
                $('p#nouveauteDescription').ellipsis(true);
            }
        }
    };
    ajaxCall(xhr, getPath() + link);
}

/* Fonction qui affiche / cache un div à partir d'une checkbox */
function showHideElementFromCheckbox(checkboxId, id) {
    $(document).ready(function() {
        if(document.getElementById(checkboxId).checked) {
            $(document.getElementById(id)).show();
        }
        else {
            $(document.getElementById(id)).hide();
        }
    });
}

function retrieveModeReglementChoisi(){

    $(document).ready(
        function(){
            var value;
            $(".radioRegl").each(
                function () {
                    if($(this).is(':checked')){
                        value=$(this).val();
                        return;
                    }
                }
            );
            infosModeReglement(value);
        }
    );
}


function infosModeReglement(modeReglementValue){
    if(modeReglementValue == 'CB'){
        hideById('CH');
        hideById('TEL');
        showById('CB');
    }
    else if(modeReglementValue == 'CH'){
        hideById('CB');
        hideById('TEL');
        showById('CH');
    }
    else if(modeReglementValue == 'FX'){
        hideById('CB');
        hideById('CH');
        showById('TEL');
    }     
}

function hideById(id) {
    document.getElementById(id).style.visibility='hidden';
    document.getElementById(id).style.display='none';
}

function showById(id) {
    document.getElementById(id).style.visibility='visible';
    document.getElementById(id).style.display='inline';
}

function lienAuteur(idAuteur, idLangue) {
    var xhr  = createXMLHttp();
    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4) {
            if (xhr.status == 200) {
                var e = document.getElementById('descriptionAuteur');
                e.style.display = "block";
                e.style.visibility = "visible";
                e.innerHTML = xhr.responseText;
            }
        }
    };
    ajaxCall(xhr, getPath() + "/descriptionAuteur.do?idAuteur=" + idAuteur + "&idLangue=" + idLangue);
}

function lienPreface(idMethode, idLangue) {
    var xhr  = createXMLHttp();
    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4) {
            if (xhr.status == 200) {
                var e = document.getElementById('descriptionAuteur');
                e.style.display = "block";
                e.style.visibility = "visible";
                e.innerHTML = xhr.responseText;
            }
        }
    };
    ajaxCall(xhr, getPath() + "/descriptionPreface.do?idMethode=" + idMethode + "&idLangue=" + idLangue);
}

//********* LISTE DES Etats ************************
function listeEtat(idZone)  {
    var xhr = createXMLHttp();
    var idPays;

    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4) {
            if (xhr.status == 200) {
                var e = document.getElementById(idZone);
                e.innerHTML = xhr.responseText;
            }
        }
    };

    if(idZone.indexOf('Facturation',0) != -1)
        idPays = document.getElementById('paysFacturation').value;
    else if(idZone.indexOf('Livraison',0) != -1)
        idPays = document.getElementById('paysLivraison').value;
    
    ajaxCall(xhr, getPath() + "/listeEtat.do?idPays=" + idPays + "&idZone=" + idZone);
}
