function createRequestObject() {
    var ro;
    if(window.XMLHttpRequest){
        ro = new XMLHttpRequest(); 
    }else if(window.ActiveXObject){
        try {
            ro = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            ro = new ActiveXObject("Microsoft.XMLHTTP");
        }
    } else { 
        alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest..."); 
        ro = false; 
    }
    return ro;
}

function close_error(){
    document.getElementById("error_window").style.display="none";
}

function close_info(){
    document.getElementById("info_window").style.display="none";
}

function panier_action(act,id_art_ref){

    if(act=='update'){
        document.getElementById("choix_action").value=act;
        document.getElementById("panier_produits").submit();
    }
    if(act=='delete'){
        if(confirm("Etes-vous sûr de vouloir supprimer ce produit ?")){
            document.getElementById("choix_action").value=act;
            document.getElementById("id_art_ref").value=id_art_ref;
            document.getElementById("panier_produits").submit();
        }
    }
    if(act=='delete_panier'){
        if(confirm("Etes-vous sûr de vouloir supprimer tous les produits de votre panier ?")){
            document.getElementById("choix_action").value=act;
            document.getElementById("panier_produits").submit();
        }
    }
    if(act=='delete_gesteeco'){
        document.getElementById("choix_action").value=act;
        document.getElementById("panier_produits").submit();
    }
    if(act=='delete_echantillon'){
        document.getElementById("choix_action").value=act;
        document.getElementById("panier_produits").submit();
    }
}

function panier_action_en(act,id_art_ref){
	
    if(act=='update'){
        document.getElementById("choix_action").value=act;
        document.getElementById("panier_produits").submit();
    }
	
    if(act=='delete'){
        if(confirm("Remove this product from your cart ?")){
            document.getElementById("choix_action").value=act;
            document.getElementById("id_art_ref").value=id_art_ref;
            document.getElementById("panier_produits").submit();
        }
    }
    if(act=='delete_panier'){
        if(confirm("Remove all product from your cart?")){
            document.getElementById("choix_action").value=act;
            document.getElementById("panier_produits").submit();
        }
    }
}

function setReferer(){
    var ref = document.referrer;
    var ref_http = createRequestObject();
    ref_http.open('get','/core/ajax/ajax_referer.php?ref='+ref);
    ref_http.send(null);
}

function setCarnetDefaut(param){
    var ref_http = createRequestObject();
    ref_http.open('get','/core/ajax/ajax_carnetdefaut.php?param='+param);
    ref_http.send(null);
}

function clickMea(idmea,iduniv){
    var ref_http = createRequestObject();
    ref_http.open('get','/core/ajax/ajax_stat_mea.php?id_mea='+idmea+"&id_univers="+iduniv);
    ref_http.send(null);
}

function ajoutQt(id,id_ref) {
    val = document.getElementById("qte_"+id+"_"+id_ref).value;
    val++;
    document.getElementById("qte_"+id+"_"+id_ref).value = val;
}

function suppQt(id,id_ref) {
    val = document.getElementById("qte_"+id+"_"+id_ref).value;
    if (val>1) val--;
    document.getElementById("qte_"+id+"_"+id_ref).value = val;
}

function pass_perdu(){
    openPopUp("passperdu_window");
    return;
}

function comparatif_tailles(){
    myWindow = window.open("index.php?module=contenu&contenu=guide_taille", "Preview", "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,copyhistory=0,width=590,height=500,left=100,top=50");
}

function enreg_panier(){
    myWindow = window.open("/core/enreg_panier.php", "Preview", "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,copyhistory=0,width=340,height=170,left=100,top=100");
}
	
function alerte_stock(id_art_ref){
    myWindow = window.open("/core/alerte_client.php?id_art_ref="+id_art_ref, "Alerte", "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,copyhistory=0,width=480,height=160,left=100,top=100");
}

function validCgv(){
    if(document.getElementById("valid_cgv").checked==false){
        alert("Vous devez accepter les Conditions Générales de Vente pour pouvoir commander.");
        return false;
    }else{
        return true;
    }
}

function validCgvEn(){
    if(document.form_conditions.valid_cgv.checked==false){
        alert("You must accept the Terms and Conditions in order to order.");
        return false;
    }else{
        return true;
    }
}

function format_number(number,dec){
    var snum = new String(number);
    var sec = snum.split('.');
    var number_int = parseFloat(sec[0]);
    var number_float = parseFloat("0."+sec[1]);

    var res ="";
    if(number_float > 0){
        res = Math.round(number_float*Math.pow(10,dec));
        var res2 = Math.round(number_float*Math.pow(10,dec+1))-(res*10);
        if(res2 > 4){
            res++;
        }
    }else{
        for(i=1 ; i<=dec;i++){
            res +="0";
        }
    }
    return number_int+"."+res;
}


function infos_fidelite(){
    overlib("Pour bénéficier du <b>prix fidélité</b>, rendez-vous dans l'espace \"Programme Fidélité\" de votre compte client pour convertir vos points fidélité en avantage !", FGCOLOR,'#FBF7E4',BGCOLOR,'#FF8000',ABOVE,LEFT,HEIGHT,50,WIDTH,300,TEXTSIZE,11);
}

function infos_codepromo(txt){
    overlib(txt, FGCOLOR,'#FBF7E4',BGCOLOR,'#1CB7C9',ABOVE,RIGHT,HEIGHT,50,WIDTH,300,TEXTSIZE,10);
}

function close_addcart(){
    close_info();
}
setTimeout("close_addcart()",4000);



var t;
var lock=false;
var overdiv=0;
function active_moncompte(){
    clearTimeout(t);
    overdiv = 1;
    document.getElementById('inmoncompte').style.display="block";
    document.getElementById('hidder_moncompte').style.display="block";
}

function desactive_moncompte(){
	overdiv = 0;
    if(lock == false){
        t = setTimeout("closefenetre()",2000);
    }
}

function lock_moncompte(){
    lock=true;
    clearTimeout(t);
}

function unlock_moncompte(){
    lock = false;
    if(overdiv == 0){
    t = setTimeout("closefenetre()",2000);
    }
}

function closefenetre(){
    if(lock == false){
        document.getElementById('inmoncompte').style.display="none";
        document.getElementById('hidder_moncompte').style.display="none";
    }
}

var login_http;
function login(login,passwd){
    login_http = createRequestObject();
    login_http.open('get','core/signin.php?login='+login+"&pass="+passwd);
    login_http.onreadystatechange = window_reload;
    login_http.send(null);
    
}

function window_reload(){
    if(login_http.readyState == 4){
        location.reload(true);
    }
}

function display_panier(){
    document.getElementById("titre_bloc_panier").className = "titre_bloc";
    document.getElementById("titre_bloc_courses").className = "titre_bloc_inactif";
    document.getElementById("bloc_panier").style.display='block';
    document.getElementById("bloc_courses").style.display='none';
}

function display_courses(){
    document.getElementById("titre_bloc_panier").className = "titre_bloc_inactif";
    document.getElementById("titre_bloc_courses").className = "titre_bloc";
    document.getElementById("bloc_panier").style.display='none';
    document.getElementById("bloc_courses").style.display='block';
}

function verif_compte(){
    if (document.pass_perdu_form.email_in.value.length<2) {
        alert("Veuillez indiquer votre adresse e-mail.");
        document.pass_perdu_form_form.email_in.focus();
        return false;
    }
    var email   = document.pass_perdu_form.email_in.value;
    var verif   = /^[A-Za-z0-9._-]+@[A-Za-z0-9.-]{2,}[.][A-Za-z]{2,4}$/
    if (verif.exec(email) == null){
        alert("Votre email est incorrect");
        document.pass_perdu_form.email_in.focus();
        return false;
    }
    document.pass_perdu_form.submit();
    return true;
}

function switch_chk(checked,id_panier){
    var inputs = document.getElementsByTagName("input");
    for(var i = 0 ; i < inputs.length ; i++){
        if(inputs[i].type == "checkbox"){
            var id = inputs[i].id;
            if(id.split("-")[0] == "panier"+id_panier){
                inputs[i].checked = checked;
            }
        }
    }
}

function check_chk(id){
    var inputs = document.getElementsByTagName("input");
    var id_panier = id.split("-")[0].replace("panier","");
    var checked = true;
    for(var i = 0 ; i < inputs.length ; i++){
        if(inputs[i].type == "checkbox"){
            var id = inputs[i].id;
            if(id.split("-")[0] == "panier"+id_panier){
                if(!inputs[i].checked){
                    checked = false;
                }
            }
        }
    }
    document.getElementById("pan_sav_"+id_panier).checked = checked;
}
/**
 * Open popup and gray overlay
 */

function openPopUp(id_popup){
    window.location.hash="main_site";

    var box = document.getElementById(id_popup);
    var w = parseInt(box.style.width);
    var h = parseInt(box.style.height);

    var myWidth = 0;
    var myHeight = 0;
    if( typeof( window.innerWidth ) == 'number' ) {
        //Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
        //IE 6
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    }
   
    var left = (myWidth/2)-w/2;
    var top = (myHeight/2)-h/2;

    box.style.top  = top+"px";
    box.style.left = left+"px";

    var overlay = document.getElementById("overlay");
    overlay.style.height= myHeight+"px";
    overlay.style.width = myWidth+"px";
    
    Effect.Appear('overlay',{
        duration:0.2,
        to:0.5
    });
    Effect.Appear(''+id_popup,{
        duration:0.2
    });

}

/**
 * Open popup and gray overlay
 */

function closePopUp(id_popup) {
    if((id_popup == 'passperdu_window' && document.getElementById("login_window").style.display == "none") || id_popup != 'passperdu_window'){
        Effect.Fade('overlay',{
            duration:0.1
        });
    }
    Effect.Fade(id_popup,{
        duration:0.1
    });
}

var timer;
function onkey_recherche(){
    document.getElementById("recherche_update").style.display='none';
    clearTimeout(timer);
    timer=setTimeout(do_recherche,700);
}
var rech_http;
function do_recherche(){
    var value = document.getElementById("recherche").value;
    if(value.length > 3){
        rech_http = createRequestObject();
        rech_http.open('get','core/recherche/autocomplete.php?recherche='+value);
        rech_http.onreadystatechange = do_recherche_valide;            
        rech_http.send(null);
    }
}

function do_recherche_valide(){
    if(rech_http.readyState == 4){
        var response = rech_http.responseText;
        if(response.length > 0){
            document.getElementById("recherche_update").innerHTML = response;
            document.getElementById("recherche_update").style.display='block';
            window.onclick = close_recherche;
        }
    }
}

function close_recherche(){
    document.getElementById("recherche_update").style.display='none';
    window.onclick = null;
}




