// Ajout aux favoris
	function favIsbn(isbn,favAddOk) {
		var xhttp = new XMLHttpRequest();
		xhttp.onreadystatechange = function() {
			if (this.readyState == 4 && this.status == 200) {
				document.getElementById("favAddOk" + isbn).innerHTML = this.responseText;
			}
		};
	xhttp.open("POST", "/favoris-add", true);
	xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xhttp.send("isbn=" + isbn);
	//	Refresh header pour compter le nombre de favoris ajouté
	$("#refresh").load(location.href + " #refresh");
	$("#refresh2").load(location.href + " #refresh2");
}

// Onclick Link Opacity 
	$(".urlOpacity, .pagination a, .delfiltre a").click(function(){
		jQuery('html,body').animate({scrollTop:0},500);
		$(".divOpacity").css("opacity", ".2");
	});

// Onclick burger menu opacity page 
	$(".menu-btn").click(function(){
		$(".contentpad, footer, #menu-tab").css("opacity", ".1");
	});
// Onclick burger menu close delete opacity page 
	$(".close-btn").click(function(){
		$(".contentpad, footer, #menu-tab").css("opacity", "1");
	});

// Menu Tab : On Resize window close opacity + show #filter
$(document).ready(function(){
  $(window).resize(function(){
		$(".contentpad, footer, #menu-tab").css("opacity", "1");
  });
});

// Accordion Filtre
	var acc = document.getElementsByClassName("filtreTitre");
	var i;
	for (i = 0; i < acc.length; i++) {
		acc[i].onclick = function(){
			this.classList.toggle("active");
			this.nextElementSibling.classList.toggle("show");
	}
	}

// Open filtre en 1024px
function openFilter() {
	document.getElementById("filter").style.display = "block";
	document.documentElement.style.overflow = "hidden";  // firefox, chrome
	document.body.scroll = "no"; // ie only  
}

// Close filtre en 1024px
function closeFilter() {
	document.getElementById("filter").style.display = "none";
	document.documentElement.style.overflow = "auto";  // firefox, chrome
	document.body.scroll = "yes"; // ie only  
}

// Si la fenetre est >1024 alors on affiche le #filtre
$(window).resize(function(){
	var filtre = document.getElementById("filter");
	if ($(window).width() >= 1024 && filtre!= null) {
		filtre.style.display = "block";
		document.documentElement.style.overflow = "auto";  // firefox, chrome
		document.body.scroll = "yes"; // ie only
	} else {
		filtre.getElementById("filter").style.display = "none";
		document.documentElement.style.overflow = "auto";  // firefox, chrome
		document.body.scroll = "yes"; // ie only
	}
});


// Saisir chiffre et "." uniquement
	function champNumericOnly(champ)  {
		var chiffres = new RegExp("[0-9.,]");
		var verif;
			for(x = 0; x < champ.value.length; x++)	{
				verif = chiffres.test(champ.value.charAt(x));
					if(verif == false){champ.value = champ.value.substr(0,x) + champ.value.substr(x+1,champ.value.length-x+1); x--;}
			} 
}

// Footer Accordion
	var acc = document.getElementsByClassName("accordion");
	var i;
	for (i = 0; i < acc.length; i++) {
		acc[i].onclick = function() {
	for (let i = 0; i < acc.length; i++) {
		acc[i].classList.remove('active');
		acc[i].style.maxHeight = null;
		acc[i].nextElementSibling.style.maxHeight = null;
	}
	this.style.maxHeight = this.scrollHeight+'px';
	this.classList.add('active');
	var panel = this.nextElementSibling;
	panel.style.maxHeight = panel.scrollHeight + 'px'
	}
	}
// Tab Content Vertical
function openTab(evt, cityName) {
  var i, tabcontent, tablinks;
  tabcontent = document.getElementsByClassName("tabcontent");
  for (i = 0; i < tabcontent.length; i++) {
    tabcontent[i].style.display = "none";
  }
  tablinks = document.getElementsByClassName("tablinks");
  for (i = 0; i < tablinks.length; i++) {
    tablinks[i].className = tablinks[i].className.replace(" active", "");
  }
  document.getElementById(cityName).style.display = "block";
  evt.currentTarget.className += " active";
}

// ShowPassword
function showPassword() {
  var x = document.getElementById("motpasse");
  if (x.type === "password") {
    x.type = "text";
  } else {
    x.type = "password";
  }
}
// ShowPassword 1
function showPassword1() {
  var x = document.getElementById("motpasse1");
  if (x.type === "password") {
    x.type = "text";
  } else {
    x.type = "password";
  }
}

//Confirmation RGPD
function postRgpd() {
 var request = new XMLHttpRequest();
    request.open("POST", "/rgpd_conf.php");
    request.onreadystatechange = function() {
        if(this.readyState === 4 && this.status === 200) {
			document.getElementById("rgpd").style.display = "none";
        }
    };
    var myRgpd = document.getElementById("myRgpd");
    var formData = new FormData(myRgpd);
    request.send(formData);
}

/* FORM RECHERCHE + AUTOCOMPLETE */
// Saisir 3 caractères minimum pour le moteur de recherche
	function searchLength() {
		var value = document.getElementById('q').value;
			if (value.length < 3) {
				alert('3 caractères minimum !');
			return false; // keep form from submitting
			}
	return true;
	}
// Saisir 3 caractères minimum pour le moteur de recherche Tablette
	function searchLength2() {
		var value = document.getElementById('q2').value;
			if (value.length < 3) {
				alert('3 caractères minimum !');
			return false; // keep form from submitting
			}
	return true;
	}


// Formulaire de recherche Autocomplete
function autocomplet() {
	var keyword = $('#q').val();
	$.ajax({
		url: '/suggestions',
		type: 'POST',
		data: {keyword:keyword},
		success:function(data){
			$('#autocomplete').show();
			$('#autocomplete').html(data);
		}
	});
}
// Formulaire de recherche Autocomplete Tablette
function autocomplet2() {
	var keyword = $('#q2').val();
	$.ajax({
		url: '/suggestions',
		type: 'POST',
		data: {keyword:keyword},
		success:function(data){
			$('#autocomplete2').show();
			$('#autocomplete2').html(data);
		}
	});
}

/*
function set_item(item) { // Met la valeur dans le champ au clic
// change input value
	$('#q').val(item);
// hide proposition list
	$('#autocomplete').hide();
}
*/

// Ferme l'autocomplete au clic dans la page
$(document).click(function() {
    var obj = $("#form");
    if (!obj.is(event.target) && !obj.has(event.target).length) {
    //   Outside
		$('#autocomplete').hide();
    }
    else {
    //   Inside
		$('#autocomplete').show();
    }
});
// Ferme l'autocomplete au clic dans la page Tablette
$(document).click(function() {
    var obj = $("#form2");
    if (!obj.is(event.target) && !obj.has(event.target).length) {
    //   Outside
		$('#autocomplete2').hide();
    }
    else {
    //   Inside
		$('#autocomplete2').show();
    }
});

//Change le texte du Button au clic
function changeTextButton() {
  document.getElementById("sub").innerHTML = "Chargement...";
}
function changeTextButtonReverse() {
  document.getElementById("sub").innerHTML = "Envoyer";
}

// Au clic dans l apage en dehors de l'élément provoque une action
window.onclick = function(event) {
var btn = document.getElementById("sub"); 
 if (event.target == btn) {
    btn.innerHTML = "Envoyer";
  }
}



// Affichage message en haut du site
$(document).ready(function(){
$('.message').fadeIn().delay(4000).fadeOut(200);
});


// Ajout au panier
function addcart(appel){ // A mettre sur chaque page d'ajout au panier

$(document).ready(function() {
	$(".addPanier").click(function(){
		var element = $(this);
		var Id = element.attr("id");
// On vide le message d'erreur au clic "ajouter"
		$("#loadplace"+Id).empty();
// Champ envoyé
		var data = $(this).attr("data-add");
// Chaine input datas
	switch(appel){
		case "CATALOGUE":
			quantite = 1;                         // quantité de 1 de base pour l'add quick -> catalogue
		break;
		case "FICHEARTICLE":
		//	quantite = $("#quantite").val();      // quantité issue de l'input fiche art.
			quantite = $('#quantite option:selected').val();
		break;
	} // end switch


	var dataString = 'data='+encodeURIComponent(data)+"&quantite="+encodeURIComponent(quantite);
	// On doit saisir un prix
		if(data=='') {
			alert("Erreur !\nCet article ne peut pas être ajouté au panier.\nMerci d'essayer via la fiche article.");
		} else {
	$("#flash"+Id).show();
	$("#flash"+Id).fadeIn(400).html('<button class="btn btnorange"><img src="/images/1px20px.png" width="20" height="20"> . . . </button>');
	$.ajax({
	type: "POST",
	url: "/panier/ajout",
	data: dataString,
	cache: false,
	success: function(html){
	$("#loadplace"+Id).append(html);
	$("#flash"+Id).hide();
	// On rafraichit le Header pour actualiser le montant du panier
	$("header.header").load(location.href + " header.header");
	}
	});
	} return false;
	});
});
}
// Texte rotator header tablette
(function($){
    $.fn.extend({ 
        rotaterator: function(options) {
            var defaults = {
                fadeSpeed: 500,
                pauseSpeed: 100,
				child:null
            };
            var options = $.extend(defaults, options);
            return this.each(function() {
                  var o =options;
                  var obj = $(this);                
                  var items = $(obj.children(), obj);
				  items.each(function() {$(this).hide();})
				  if(!o.child){var next = $(obj).children(':first');
				  }else{var next = o.child;
				  }
				  $(next).fadeIn(o.fadeSpeed, function() {
						$(next).delay(o.pauseSpeed).fadeOut(o.fadeSpeed, function() {
							var next = $(this).next();
							if (next.length == 0){
									next = $(obj).children(':first');
							}
							$(obj).rotaterator({child : next, fadeSpeed : o.fadeSpeed, pauseSpeed : o.pauseSpeed});
						})
					});
            });
        }
    });
})(jQuery);
 $(document).ready(function() {
        $('#rotate').rotaterator({fadeSpeed:500, pauseSpeed:3000});
 });
 
 
// Change texte button + submit form
// 1 - <input type="text" onfocus="changeTxtBtnReverse('btn','Envoyer')";>
// 2 - <button value="send" id="btn" onclick="changeTxtBtn(this.id,'send','Patientez...');">Envoyer</button>

function changeTxtBtn(idbtn,idform,txtbtn) {
	var btn = document.getElementById(idbtn);
		btn.disabled=true;								// Empêche de recliquer
		btn.readonly=true;								// Prend en compte la valeur du button
		btn.innerHTML = txtbtn;							// Change le texte
		document.forms[idform].submit(); 				// Envoi le formulaire
}

function changeTxtBtnReverse(idbtn,txtbtn) {
	var btn = document.getElementById(idbtn);
		btn.disabled=false;								// Empêche de recliquer
		btn.readonly=false;								// Prend en compte la valeur du button
		btn.innerHTML = txtbtn;							// Change le texte
}