//***************************//
// *** FICHIER AJAX FRONT ***//
//***************************//

function no_cache()
{
	date_object = new Date();
	var param = date_object.getTime();

	return param;
}

function AjaxObject()
{
	if(window.XMLHttpRequest)
	{
		xhr_object = new XMLHttpRequest();
		return xhr_object;
	}
	else if(window.ActiveXObject)
	{
		xhr_object = new ActiveXObject('Microsoft.XMLHTTP');
		return xhr_object;
	}
	else
	{ 
		alert('Votre navigateur ne supporte pas les objets XMLHTTPRequest...');
		return;
	}
}

/* Modification du type de livraison pour les spas */
function changeLivSpa(id_type)
{
	xhr_object = AjaxObject();
	var param = no_cache();
	
	xhr_object.onreadystatechange = function()
	{
		if(xhr_object.readyState == 4 && xhr_object.status == 200)
		{			
			var reponse = xhr_object.responseText;
			document.location.href = reponse;
		}
	}
	xhr_object.open('GET',add_url + '/ajax/changeLivSpa/' + id_type + '/' + param ,true);
	xhr_object.send(null);
}

/* Modification du type de livraison et changement date de livraison */
function changeDateLiv(id_type)
{
	xhr_object = AjaxObject();
	var param = no_cache();
	
	xhr_object.onreadystatechange = function()
	{
		if (xhr_object.readyState != 4) 
		{
			document.getElementById('changeDateLiv').innerHTML = '<img src="' + add_surl + '/images/default/ajax-loader.gif">';
		}
	
		if(xhr_object.readyState == 4 && xhr_object.status == 200)
		{			
			var reponse = xhr_object.responseText;			
			document.getElementById('changeDateLiv').innerHTML = reponse;
		}
	}
	xhr_object.open('GET',add_url + '/ajax/changeDateLiv/' + id_type + '/' + param ,true);
	xhr_object.send(null);
}

/* Attribution de l'adresse de livraison à la facturation */
function sameFactAdresse(id_adresse,type)
{
	xhr_object = AjaxObject();
	var param = no_cache();
	
	xhr_object.onreadystatechange = function()
	{
		if (xhr_object.readyState != 4) 
		{
			document.getElementById('adresseFactu').innerHTML = '<img src="' + add_surl + '/images/default/ajax-loader.gif">';
		}
	
		if(xhr_object.readyState == 4 && xhr_object.status == 200)
		{
			var reponse = xhr_object.responseText;			
			document.getElementById('adresseFactu').innerHTML = reponse;
		}
	}
	xhr_object.open('GET',add_url + '/ajax/sameFactAdresse/' + id_adresse + '/' + type + '/' + param ,true);
	xhr_object.send(null);
}

/* Fonction AJAX suppression d'un code promo */
function deletePromo(id_code)
{
	xhr_object = AjaxObject();
	var param = no_cache();
	
	xhr_object.onreadystatechange = function()
	{
		if (xhr_object.readyState != 4) 
		{
			document.getElementById('panierDetails').innerHTML = '<img src="' + add_surl + '/images/default/ajax-loader.gif">';
		}
	
		if(xhr_object.readyState == 4 && xhr_object.status == 200)
		{
			var reponse = xhr_object.responseText;			
			document.getElementById('panierDetails').innerHTML = reponse;	
			panierHeader();
		}
	}
	xhr_object.open('GET',add_url + '/ajax/deletePromo/' + id_code + '/' + param ,true);
	xhr_object.send(null);
}

/* Fonction AJAX suppression d'un spa du panier */
function deleteSpa(id_produit,id_details)
{
	xhr_object = AjaxObject();
	var param = no_cache();
	
	xhr_object.onreadystatechange = function()
	{
		if (xhr_object.readyState != 4) 
		{
			document.getElementById('panierDetails').innerHTML = '<img src="' + add_surl + '/images/default/ajax-loader.gif">';
		}
	
		if(xhr_object.readyState == 4 && xhr_object.status == 200)
		{
			var reponse = xhr_object.responseText;			
			document.getElementById('panierDetails').innerHTML = reponse;	
			panierHeader();
		}
	}
	xhr_object.open('GET',add_url + '/ajax/deleteSpa/' + id_produit + '/' + id_details + '/' + param ,true);
	xhr_object.send(null);
}

/* Fonction AJAX suppression d'un produit du panier */
function deleteProduit(id_produit,id_details)
{
	xhr_object = AjaxObject();
	var param = no_cache();
	
	xhr_object.onreadystatechange = function()
	{
		if (xhr_object.readyState != 4) 
		{
			document.getElementById('panierDetails').innerHTML = '<img src="' + add_surl + '/images/default/ajax-loader.gif">';
		}
	
		if(xhr_object.readyState == 4 && xhr_object.status == 200)
		{
			var reponse = xhr_object.responseText;			
			document.getElementById('panierDetails').innerHTML = reponse;	
			panierHeader();
			panierCrossSelling();
		}
	}
	xhr_object.open('GET',add_url + '/ajax/deleteProduit/' + id_produit + '/' + id_details + '/' + param ,true);
	xhr_object.send(null);
}

/* Fonction AJAX MAJ du cross selling dans le panier */
function panierCrossSelling()
{
	xhr_object = AjaxObject();
	var param = no_cache();
	
	xhr_object.onreadystatechange = function()
	{
		if (xhr_object.readyState != 4) 
		{
			document.getElementById('panierCrossSelling').innerHTML = '<img src="' + add_surl + '/images/default/ajax-loader.gif">';
		}
	
		if(xhr_object.readyState == 4 && xhr_object.status == 200)
		{
			var reponse = xhr_object.responseText;			
			document.getElementById('panierCrossSelling').innerHTML = reponse;	
			$(".thickbox").colorbox();
		}
	}
	xhr_object.open('GET',add_url + '/ajax/panierCrossSelling/' + param ,true);
	xhr_object.send(null);
}

/* Fonction AJAX suppression d'un produit d'une wishlist */
function deleteProduitWishList(id_wishlist)
{
	xhr_object = AjaxObject();
	var param = no_cache();
	
	xhr_object.onreadystatechange = function()
	{
		if (xhr_object.readyState != 4) 
		{
			document.getElementById('wishlistDetails').innerHTML = '<img src="' + add_surl + '/images/default/ajax-loader.gif">';
		}
	
		if(xhr_object.readyState == 4 && xhr_object.status == 200)
		{
			var reponse = xhr_object.responseText;	
			document.getElementById('wishlistDetails').innerHTML = reponse;
			$(".thickbox").colorbox();
		}
	}
	xhr_object.open('GET',add_url + '/ajax/deleteProduitWishList/' + id_wishlist + '/' + param ,true);
	xhr_object.send(null);
}

/* Fonction AJAX changement de la quantite d'un spa */
function changeQuantiteSpa(id_produit,id_detail,type)
{
	xhr_object = AjaxObject();
	var param = no_cache();
	
	xhr_object.onreadystatechange = function()
	{
		if (xhr_object.readyState != 4) 
		{
			document.getElementById('panierDetails').innerHTML = '<img src="' + add_surl + '/images/default/ajax-loader.gif">';
		}
	
		if(xhr_object.readyState == 4 && xhr_object.status == 200)
		{
			var reponse = xhr_object.responseText;			
			document.getElementById('panierDetails').innerHTML = reponse;
			panierHeader();
		}
	}
	xhr_object.open('GET',add_url + '/ajax/changeQuantiteSpa/' + id_produit + '/' + id_detail + '/' + type + '/' + param ,true);
	xhr_object.send(null);
}

/* Fonction AJAX changement de la quantite d'un produit */
function changeQuantiteProduit(id_produit,id_detail,type)
{
	xhr_object = AjaxObject();
	var param = no_cache();
	
	xhr_object.onreadystatechange = function()
	{
		if (xhr_object.readyState != 4) 
		{
			document.getElementById('panierDetails').innerHTML = '<img src="' + add_surl + '/images/default/ajax-loader.gif">';
		}
	
		if(xhr_object.readyState == 4 && xhr_object.status == 200)
		{
			var reponse = xhr_object.responseText;			
			document.getElementById('panierDetails').innerHTML = reponse;
			panierHeader();
		}
	}
	xhr_object.open('GET',add_url + '/ajax/changeQuantiteProduit/' + id_produit + '/' + id_detail + '/' + type + '/' + param ,true);
	xhr_object.send(null);
}

/* Fonction AJAX changement du type de FDP */
function changeTypeFDP(id_type)
{
	xhr_object = AjaxObject();
	var param = no_cache();
	
	xhr_object.onreadystatechange = function()
	{
		if (xhr_object.readyState != 4) 
		{
			document.getElementById('panierDetails').innerHTML = '<img src="' + add_surl + '/images/default/ajax-loader.gif">';
		}
		
		if(xhr_object.readyState == 4 && xhr_object.status == 200)
		{
			var reponse = xhr_object.responseText;			
			document.getElementById('panierDetails').innerHTML = reponse;
			panierHeader();
		}
	}
	
	xhr_object.open('GET',add_url + '/ajax/changeTypeFDP/' + id_type + '/' + param ,true);
	xhr_object.send(null);
}

/* Fonction AJAX modification details produit */
function blocQuantitePrix(id_produit,id_details,quantite)
{
	xhr_object = AjaxObject();
	var param = no_cache();
	
	xhr_object.onreadystatechange = function()
	{
		if(xhr_object.readyState == 4 && xhr_object.status == 200)
		{
			var reponse = xhr_object.responseText;			
			document.getElementById('blocQuantitePrix').innerHTML = reponse;
			$('.single-product .product-description .top .sum form').jqTransform();
			$(".thickbox").colorbox();
			addWishlist(id_produit,id_details);
		}
	}
	
	xhr_object.open('GET',add_url + '/ajax/blocQuantitePrix/' + id_produit + '/' + id_details + '/' + quantite + '/' + param ,true);
	xhr_object.send(null);
}

/* Fonction AJAX modification addWishlist */
function addWishlist(id_produit,id_details)
{
	xhr_object = AjaxObject();
	var param = no_cache();
	
	xhr_object.onreadystatechange = function()
	{
		if(xhr_object.readyState == 4 && xhr_object.status == 200)
		{
			var reponse = xhr_object.responseText;			
			document.getElementById('addWishlist').innerHTML = reponse;
			$(".thickbox").colorbox();
		}
	}
	
	xhr_object.open('GET',add_url + '/ajax/addWishlist/' + id_produit + '/' + id_details + '/' + param ,true);
	xhr_object.send(null);
}

/* Fonction AJAX modification details spa */
function blocDetailSpa(id_gamme)
{
	xhr_object = AjaxObject();
	var param = no_cache();
	
	var id_spa  = document.getElementById('id_spa').value;
	
	xhr_object.onreadystatechange = function()
	{
		if(xhr_object.readyState == 4 && xhr_object.status == 200)
		{
			var reponse = xhr_object.responseText;
			document.getElementById('blocDetailSpa').innerHTML = reponse;
			$(".thickbox-panier").colorbox();
			$(".thickbox").colorbox();
			$('.static-entry').jScrollPane({
				verticalDragMinHeight		: 29,
				verticalDragMaxHeight		: 29
			});
			
			$('.savoir .page-holder').jScrollPane({
				verticalDragMinHeight		: 29,
				verticalDragMaxHeight		: 29
			});
			/*$('.jspDrag').css('background-color', '#44251F');
			$('.jspTrack').css('background', "url('/images/default/drag-bg-brown.png') repeat-y 0 0");
			$('.jspCap').css('background', "url('/images/default/drag-bg-brown.png') repeat-y 0 0");*/
		}
	}
	
	xhr_object.open('GET',add_url + '/ajax/blocDetailSpa/' + id_gamme + '/' + id_spa + '/' + param ,true);
	xhr_object.send(null);
}

/* Fonction AJAX renouveler une commande */
function renouvelerCommande(id_transaction)
{
	xhr_object = AjaxObject();
	var param = no_cache();
	
	xhr_object.onreadystatechange = function()
	{
		if(xhr_object.readyState == 4 && xhr_object.status == 200)
		{
			panierHeader();
		}
	}
	
	xhr_object.open('GET',add_url + '/ajax/renouvelerCommande/' + id_transaction + '/' + param ,true);
	xhr_object.send(null);
}

/* Fonction AJAX ajouter produit au panier */
function addPanier(id_produit,id_details,quantite,id_spa)
{
	xhr_object = AjaxObject();
	var param = no_cache();
	
	xhr_object.onreadystatechange = function()
	{
		if(xhr_object.readyState == 4 && xhr_object.status == 200)
		{
			// Quand c'est fini on met à jour le Panier du Header
			panierHeader();
			$(".thickbox-panier").colorbox({overlayClose:false,escKey:false});
		}
	}
	
	xhr_object.open('GET',add_url + '/ajax/addPanier/' + id_produit + '/' + id_details + '/' + quantite + '/' + id_spa + '/' + param ,true);
	xhr_object.send(null);
}

/* Fonction AJAX maj panier header */
function panierHeader()
{
	xhr_object = AjaxObject();
	var param = no_cache();
	
	xhr_object.onreadystatechange = function()
	{
		if(xhr_object.readyState == 4 && xhr_object.status == 200)
		{
			var reponse = xhr_object.responseText;			
			document.getElementById('panierHeader').innerHTML = reponse;	
			$(".thickbox").colorbox();
			$('table.wishlist tr:even').css('backgroundColor', '#fff');
		}
	}
	
	xhr_object.open('GET',add_url + '/ajax/panierHeader/' + param ,true);
	xhr_object.send(null);
}

/* Fonction AJAX vote produit */
function voteProduit(id_produit,vote)
{
	xhr_object = AjaxObject();
	var param = no_cache();
	
	xhr_object.onreadystatechange = function()
	{
		if (xhr_object.readyState != 4) 
		{
			document.getElementById('resultVote').innerHTML = '<img src="' + add_surl + '/images/default/ajax-loader.gif">';
		}
		
		if(xhr_object.readyState == 4 && xhr_object.status == 200)
		{
			var reponse = xhr_object.responseText;
			document.getElementById('resultVote').innerHTML = reponse;
			avisPetale(id_produit);
		}
	}
	
	xhr_object.open('GET',add_url + '/ajax/voteProduit/' + id_produit + '/' + vote + '/' + param ,true);
	xhr_object.send(null);
}

/* Fonction AJAX affichage vote produit */
function avisPetale(id_produit)
{
	xhr_object = AjaxObject();
	var param = no_cache();
	
	xhr_object.onreadystatechange = function()
	{
		if (xhr_object.readyState != 4) 
		{
			document.getElementById('avisPetale').innerHTML = '<img src="' + add_surl + '/images/default/ajax-loader.gif">';
		}
		
		if(xhr_object.readyState == 4 && xhr_object.status == 200)
		{
			var reponse = xhr_object.responseText;
			document.getElementById('avisPetale').innerHTML = reponse;
			$(".thickbox").colorbox();
		}
	}
	
	xhr_object.open('GET',add_url + '/ajax/avisPetale/' + id_produit + '/' + param ,true);
	xhr_object.send(null);
}

/* Fonction AJAX ajout message cadeau */
function addMessageCadeau()
{
	xhr_object = AjaxObject();
	var param = no_cache();
	
	// Variables du formulaire
	var nom_beneficiaire = document.getElementById('nom_beneficiaire').value;
	var prenom_beneficiaire = document.getElementById('prenom_beneficiaire').value;
	var de_la_part_de = document.getElementById('de_la_part_de').value;
	var message = document.getElementById('message').value;
	var id_produit = document.getElementById('id_produit').value;
	var id_detail = document.getElementById('id_detail').value;
	
	nom_beneficiaire = nom_beneficiaire.replace(/\&/g,"et");
	prenom_beneficiaire = prenom_beneficiaire.replace(/\&/g,"et");
	de_la_part_de = de_la_part_de.replace(/\&/g,"et");
	message = message.replace(/\&/g,"et");
	
	// On traite les donnees en POST via l'ajax
	xhr_object.open('POST', add_url + '/ajax/addMessageCadeau',false);
	xhr_object.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr_object.send("nom_beneficiaire="+nom_beneficiaire+"&prenom_beneficiaire="+prenom_beneficiaire+"&de_la_part_de="+de_la_part_de+"&message="+message+"&id_produit="+id_produit+"&id_detail="+id_detail);
}

/****** Inscription NL ******/
function registerNL(email)
{

	if(email=='')
		return false;
	xhr_object = AjaxObject();
	var param = no_cache();
	
	
	xhr_object.onreadystatechange = function()
	{
		if (xhr_object.readyState != 4) 
		{
			
		}
		if(xhr_object.readyState == 4 && xhr_object.status == 200)
		{
			var reponse = xhr_object.responseText;
			if(reponse=='error')
			{
				$.fn.colorbox({href:'/ajax/nl/error/'+param,width:'500', height:'250'}); 
			}
			else if(reponse=='already')
			{
				$.fn.colorbox({href:'/ajax/nl/already/'+param,width:'500', height:'250'}); 
			}
			else if(reponse=='ok')
			{
				$.fn.colorbox({href:'/ajax/nl/ok/'+param,width:'500', height:'250'}); 
			}
		}
	}
	xhr_object.open('GET', '/ajax/newsletter/' + email + '/'  +param ,true);
	xhr_object.send(null);
	return false;
	
		
}
