function add_to_cart_quantity(id_product)
{
	// gère le fait qu'il puisse exister plusieurs ID addToCartQuantityProduct
	var v = 1, i = 0, f = false, l = document.getElementsByTagName('input');
	for (; i < l.length; i++) {
		if (l[i].id == 'addToCartQuantityProduct' + id_product && l[i].value != '') {
			v = parseInt(l[i].value);
			f = true;
			break;
		}
	}

	if (f == true) {
		return v;
	}

	v = 1, i = 0, f = false, l = document.getElementsByTagName('select');
	for (; i < l.length; i++) {
		if (l[i].id == 'addToCartQuantityProduct' + id_product && l[i].value != '') {
			v = parseInt(l[i].value);
			f = true;
			break;
		}
	}

	return v;
}

function add_to_cart(id_product, params, quantity, firstAlternative, secondAlternative)
{
	var xmlhttpobj = getXmlHttpObject();
	if (typeof xmlhttpobj != 'undefined') {
		// on check si c'est un shortproduct pour éviter d'envoyer les variantes du produit principal dans le cas de produits associés
		var inputsProduct = document.getElementsByName('id_product');
		var elementProduct;
		for (i = 0; i < inputsProduct.length; i++) {
			if (inputsProduct[i].value == id_product) {
				elementProduct = inputsProduct[i];
				break;
			}
		}

		var isSummaryProduct = null;
		if (typeof elementProduct != 'undefined')
		{
			closestItem = elementProduct;
			var antiloop = 1000;
			while (closestItem && --antiloop) {
				if (closestItem.classList && closestItem.classList.contains('short_product_container')) {
					isSummaryProduct = closestItem;
					break;
				}
				closestItem = closestItem.parentNode;
			}
			antiloop    = null;
			closestItem = null;
		}

		var post_vars = '&id_product='+id_product;
		if (typeof quantity !== 'undefined' && quantity !== false) {
			post_vars += '&quantity='+encodeURIComponent(quantity);
		} else if (document.getElementById('addToCartQuantityProduct'+id_product)) {
			quantity = add_to_cart_quantity(id_product);
			post_vars += '&quantity='+encodeURIComponent(add_to_cart_quantity(id_product));
		} else if (typeof firstAlternative !== 'undefined'
			&& document.getElementById('addToCartQuantityProduct' + id_product + firstAlternative + secondAlternative)
		) {
			var quantity = parseInt(document.getElementById('addToCartQuantityProduct' + id_product + firstAlternative + secondAlternative).value);
			if (isNaN(quantity) || quantity === 0) {
				quantity = 1;
			}
			post_vars += '&quantity=' + encodeURIComponent(quantity);
		}

		// On évite la récupération des variantes d'un produit principal si le produit en question est un produit associé
		if (isSummaryProduct == null) {
			if (typeof firstAlternative !== 'undefined') {
				post_vars += '&var1=' + encodeURIComponent(firstAlternative);
			} else if (document.getElementById('variante1')) {
				post_vars += '&var1=' + encodeURIComponent(document.getElementById('variante1').value);
			}
			if (typeof secondAlternative !== 'undefined') {
				post_vars += '&var2=' + encodeURIComponent(secondAlternative);
			} else if (document.getElementById('variante2')) {
				post_vars += '&var2=' + encodeURIComponent(document.getElementById('variante2').value);
			}
		}
		if (document.getElementById('options')) {
			post_vars += '&opt='+encodeURIComponent(document.getElementById('options').value);
		}

		if (document.getElementById('product'+id_product)) {
			var parent                = document.getElementById('product' + id_product);
			var textareaList          = parent.getElementsByTagName('textarea');
			var customised_texts_list = new Array();
			if (textareaList) {
				var txtareaLenght = textareaList.length;
				for (var i = 0; i < txtareaLenght; i++) {
					if (textareaList[i].name == 'customised_text') {
						customised_texts_list.push(textareaList[i]);
					}
				}
			}
		} else {
			var customised_texts_list = document.getElementsByName('customised_text');
		}

		if (customised_texts_list.length > 0) {
			var customised_text = '';
			for (var i = 0; i < customised_texts_list.length; i++) {
				customised_text += customised_texts_list[i].id + ':' + customised_texts_list[i].value + '|||';
			}
			post_vars += '&customised_text=' + encodeURIComponent(customised_text);
		}
		uri = location.href.split('/');
		if (uri[uri.lenght-1] != '') {
			uri.pop();
			url = uri.join('/') + '/';
		} else {
			url = uri.join('/');
		}
		url += 'addtocartoverlib?rnd=' + Math.ceil(Math.random() * 1000000);
		displayOverlib('Veuillez patienter','Ajouter au panier','Fermer','od_ad_to_cart',params);
		var od_content = document.getElementById('overlibcontent');

		var onErrorUrl = url + post_vars + '&redirect=true';

		var xmlhttp = getXmlHttpObject();
		if (!xmlhttp || typeof xmlhttp !== 'object') {
			window.location = onErrorUrl;
		}
		xmlhttp.open("POST", url, true, null, null);
		xmlhttp.onreadystatechange = function(){
			if (xmlhttp.readyState == 4) {
				if (xmlhttp.status == 200) {
					var contentText = xmlhttp.responseText;
					document.getElementById('overlibcontent').innerHTML = contentText;
					updateBox();
					hideAttachedFile();
					sendDatalayerAddToCartCallback(contentText);
					if (typeof onEmajineAddToCartCallback == 'function') {
						onEmajineAddToCartCallback(contentText, id_product, quantity);
					}
				} else {
					window.location = onErrorUrl;
				}
			}
		}

		xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		xmlhttp.send(post_vars);
	} else {
		document.getElementById('add_to_cart_form_' + id_product).submit();
	}
}

function hideAttachedFile()
{
	if (document.getElementById('displayAttachedFile'))  {
		elem = document.getElementById('displayAttachedFile') ;
		elem.parentNode.removeChild(elem);
	}
}

function add_to_cart_pack(id_product,params,pack){
	var xmlhttpobj=getXmlHttpObject();
	if(typeof xmlhttpobj != 'undefined'){
		var post_vars='&id_product='+id_product;
		if(document.getElementById('addToCartQuantityProduct'+id_product))
			post_vars+='&quantity='+encodeURIComponent(document.getElementById('addToCartQuantityProduct'+id_product).value);
		if(!pack) {
			if(document.getElementById('variante1'))
				post_vars+='&var1='+encodeURIComponent(document.getElementById('variante1').value);
			if(document.getElementById('variante2'))
				post_vars+='&var2='+encodeURIComponent(document.getElementById('variante2').value);
			if(document.getElementById('options'))
				post_vars+='&opt='+encodeURIComponent(document.getElementById('options').value);
		}
		if(pack)
			post_vars+='&pack=true';

		var customised_texts_list=document.getElementsByName('customised_text');

		if(customised_texts_list.length>0){
			var customised_text='';
			for(var i=0;i<customised_texts_list.length;i++)
				customised_text+=customised_texts_list[i].id+':'+customised_texts_list[i].value+'|||';
			post_vars+='&customised_text='+encodeURIComponent(customised_text);
		}
		uri=location.href.split('/');
		if(uri[uri.lenght-1]!=''){
			uri.pop();
			url=uri.join('/')+'/';
		}else{
			url=uri.join('/');
		}
		url+='addtocartoverlib';
		displayOverlib('Veuillez patienter','Ajouter au panier','Fermer','od_ad_to_cart',params);
		var od_content=document.getElementById('overlibcontent');
		var xmlhttp=getXmlHttpObject();
		xmlhttp.open("POST", url, true, null, null);
		xmlhttp.onreadystatechange=function(){
			if(xmlhttp.readyState==4){
				if (xmlhttp.status==200){
					var contentText=xmlhttp.responseText;
					document.getElementById('overlibcontent').innerHTML=contentText;
					updateBox();
				}
			}
		}
		xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xmlhttp.send(post_vars);
	}else{
		document.getElementById('add_to_cart_form_'+id_product).submit();
	}
}

function updateBox(){
	if(document.getElementById('boxcart')){
		var xmlhttpobj=getXmlHttpObject();
		xmlhttpobj.open('GET', 'update_box_cart?rnd='+Math.ceil(Math.random()*1000000), true);
		xmlhttpobj.onreadystatechange=function(){;
			if(xmlhttpobj.readyState==4){
				if (xmlhttpobj.status==200){
					document.getElementById('boxcart').innerHTML=xmlhttpobj.responseText;
				}
			}
		}
		xmlhttpobj.send(null);
	}
}

function sendDatalayerAddToCartCallback(contentText){

	// Création d'un élément temporaire pour extraire les scripts
	var tempElement = document.createElement('div');
	tempElement.innerHTML = contentText;

	// Sélection de toutes les balises <script> extraites
	var scriptTags = tempElement.getElementsByTagName('script');

	// Parcours des balises <script> et exécution de leur contenu
	for (var i = 0; i < scriptTags.length; i++) {
		var scriptTag = scriptTags[i];
		var contentScript = scriptTag.innerHTML;
		eval(contentScript);
	}

}