// Ajout un produit depuis la commande par référence
function ajouterProduitCommandeParReference(){
	var asProduitId = document.getElementById('produit_id').value;
	var asQteProduit = document.getElementById('qte_produit').innerHTML;
	// Controle de la validité du produit
	var xhr_object = createXMLHttpRequest();
	var url = reverse_path+"service_project/ajax/index.php?component=panier&action=commande_reference_produit_valid&produit_id="+asProduitId+"&produit_qte="+asQteProduit+"&cmd_ref=1";
	xhr_object.open("GET",url,true);
	xhr_object.onreadystatechange=onreadystatechange = function() {
		if ( xhr_object.readyState == 4 && xhr_object.status == 200 ) {
			var laInfos = xhr_object.responseText.split(new RegExp("separator","g"));
			if (laInfos[0] != 'ok') {
				setAndExecute('message_erreur',xhr_object.responseText);
				document.getElementById("message_erreur").style.display = "block";
			}
			else {
				ajouterPanier(asProduitId,asQteProduit,1);
				$("#"+asProduitId).children(".caddie").hide();
				$("#"+asProduitId).children(".quantite").show();
				// Si on a ajouté moins de produits que la quantité désirée (stock insuffisant)
				if ( parseInt(asQteProduit) > parseInt(laInfos[2]) ) {
					asQteProduit = laInfos[2];
				}
				$("#qte"+asProduitId).text(parseInt(asQteProduit)+parseInt(laInfos[1]));
				document.getElementById("message_erreur").style.display = "none";
				document.getElementById("produit_id").value = '';
				$("#quantite_cmd_ref").children(".quantite_cmd_ref").text(1);
			}
			document.getElementById("produit_id").focus();
		}
	}
	xhr_object.send(null);
}

// Appel AJAX : Ajoute un produit dans le panier
function ajouterPanier(asProduitId,asQteProduit,cmdRef) {
	var xhr_object = createXMLHttpRequest();
	var url = reverse_path+"service_project/ajax/index.php?component=panier&action=ajouter_panier&produit_id="+asProduitId+"&produit_qte="+asQteProduit;
	if (  document.getElementById('div_commande')!=null ) {
		url += '&recap_commande=1';
	}	
	if ( cmdRef == 1 ) {
		url += '&cmd_ref=1';
	}
	xhr_object.open("GET",url,true);
	xhr_object.onreadystatechange=onreadystatechange = function(){
		if ( xhr_object.readyState == 4 && xhr_object.status == 200 ) {
			var laInfos = xhr_object.responseText.split(new RegExp("separator","g"));
			if ( laInfos[0] == 'ko' ) {
				if ( cmdRef != 1 ) {
					// cas du caddie
					$("#"+asProduitId).children(".caddie").show();
					$("#"+asProduitId).children(".quantite").hide();
					// cas du bouton "ajouter panier"
					$("#"+asProduitId).children(".ajoutPanier").show();
					$("#"+asProduitId).children(".quantiteProduit").hide();
				}
				setAndExecute('panier',laInfos[1]);
			}
			else {
				setAndExecute('panier',laInfos[1]);
				togglePanier();
			}
		}
	}
	xhr_object.send(null);
}

// Modifie la quantite 
function incrementerDecrementerQte(asProduitId, asIncrement, asProvenance) {
	lsChampQte = document.getElementById('qte'+asProduitId).innerHTML;
	liQte = parseInt(lsChampQte);
	// Si qte incorrecte remise a 0
	if ( liQte < 0) {
		return false;
	}
	var xhr_object = createXMLHttpRequest();
	var url = '';
	// pour les offres, on ne gère pas l'incrémentation/décrémentation, mais la suppression d'une ligne de l'offre ('numero_offre-numero_ligne')
	if ( asProduitId.indexOf('-') != -1 ) {
		url = reverse_path+"service_project/ajax/index.php?component=panier&action=supprimer_produit&produit_id="+asProduitId;
	}
	else {
		url = reverse_path+"service_project/ajax/index.php?component=panier&action=modifier_qte_produit&produit_id="+asProduitId+"&produit_qte="+asIncrement+"&provenance="+asProvenance;
	}
	xhr_object.open("GET",url,true);
	xhr_object.onreadystatechange=onreadystatechange = function(){
		if ( xhr_object.readyState == 4 && xhr_object.status == 200 ) {
			var laInfos = xhr_object.responseText.split(new RegExp("separator","g"));
			if ( laInfos[0] == 'ko' ) {
				$("#"+asProduitId).children(".caddie").hide();
				$("#"+asProduitId).children(".quantite").show();
				$("#qte"+asProduitId).text(parseInt($("#qte"+asProduitId).html())-1);
				setAndExecute('panier',laInfos[1]);
			}
			else {
				setAndExecute('panier',laInfos[1]);
				if ( laInfos[2]!=undefined && document.getElementById('div_commande')!=null ) {
					setAndExecute('div_commande',laInfos[2]);
				}
			}
			

		}
	}
	xhr_object.send(null);
}

//Modifie la quantite 
function modifierProduitQte(asProduitId, aiQte) {
	var xhr_object = createXMLHttpRequest();
	var url = reverse_path+"service_project/ajax/index.php?component=panier&action=modifier_qte_produit&produit_id="+asProduitId+"&produit_qte="+aiQte;
	xhr_object.open("GET",url,true);
	xhr_object.onreadystatechange=onreadystatechange = function(){
		if ( xhr_object.readyState == 4 && xhr_object.status == 200 ) {
			var laInfos = xhr_object.responseText.split(new RegExp("separator","g"));
			setAndExecute('panier',laInfos[1]);
			if ( laInfos[2]!=undefined && document.getElementById('commande')!=null ) {
				setAndExecute('commande',laInfos[2]);
			}
		}
	}
	xhr_object.send(null);
}

// Modifie la quantite en incrementant ou non
function incrementerDecrementerQteCommandeRef(asChamp,aiIncrement) {
	aoChampQte = document.getElementById(asChamp);
	aiQte = aoChampQte.value;
	if(isNaN(aiQte)) {
		nouvelleQte = 0;
	}
	else {
		nouvelleQte = parseInt(aiQte) + aiIncrement;
	}

	if(nouvelleQte >= 0) {
		aoChampQte.value = nouvelleQte;
	}
}

// Appel AJAX : Supprime la / les listes de courses en cours pour l'utilisateur courant
function supprimerListeCourse() {
	var xhr_object = createXMLHttpRequest();
	var url = reverse_path+"service_project/ajax/index.php?component=panier&action=supprimer_liste_course";
	xhr_object.open("GET",url,false);
	xhr_object.send(null);
}

function togglePanier(){
	var checkPanier = $("#detailPanier").css("display");
	if( checkPanier == "none" ){
		$("#detailPanier").show();
	}
	else{
		$("#detailPanier").hide();
	}
}

// Appel AJAX : Ajoute un donnant droit à une offre
function ajouterDonnantDroitOffre(asNumeroOffre,aiNumeroLigne,asProdRef) {
	var xhr_object = createXMLHttpRequest();
	var url = reverse_path+"service_project/ajax/index.php?component=panier&action=ajouter_donnant_droit_offre&offre_num="+asNumeroOffre+"&num_ligne="+aiNumeroLigne+"&produit_id="+asProdRef;
	xhr_object.open("GET",url,true);
	xhr_object.onreadystatechange=onreadystatechange = function(){
		if ( xhr_object.readyState == 4 && xhr_object.status == 200 ) {
			var laInfos = xhr_object.responseText.split(new RegExp("separator","g"));
			setAndExecute('panier',laInfos[1]);
			togglePanier();
		}
	}
	xhr_object.send(null);
}

// Appel AJAX : Ajoute un avantage à une offre
function ajouterAvantageOffre(asNumeroOffre,aiNumeroLigne,asProdRef) {
	var xhr_object = createXMLHttpRequest();
	var url = reverse_path+"service_project/ajax/index.php?component=panier&action=ajouter_avantage_offre&offre_num="+asNumeroOffre+"&num_ligne="+aiNumeroLigne+"&produit_id="+asProdRef;
	xhr_object.open("GET",url,true);
	xhr_object.onreadystatechange=onreadystatechange = function(){
		if ( xhr_object.readyState == 4 && xhr_object.status == 200 ) {
			var laInfos = xhr_object.responseText.split(new RegExp("separator","g"));
			setAndExecute('panier',laInfos[1]);
			togglePanier();
		}
	}
	xhr_object.send(null);
}

// Appel AJAX : Ajoute un avantage à une offre
function ajouterAvantageManquantOffre(asNumeroOffre,asProduitId,asNumeroAvantage) {
	var xhr_object = createXMLHttpRequest();
	var url = reverse_path+"service_project/ajax/index.php?component=panier&action=ajouter_avantage_manquant_offre&offre_num="+asNumeroOffre+"&num_avantage="+asNumeroAvantage+"&produit_id="+asProduitId;
	xhr_object.open("GET",url,true);
	xhr_object.onreadystatechange=onreadystatechange = function(){
		if ( xhr_object.readyState == 4 && xhr_object.status == 200 ) {
			var laInfos = xhr_object.responseText.split(new RegExp("separator","g"));
			setAndExecute('panier',laInfos[1]);
			togglePanier();
		}
	}
	xhr_object.send(null);
}

//Appel AJAX : Reprise du panier
function reprisePanier() {
	var xhr_object = createXMLHttpRequest();
	var url = reverse_path+"service_project/ajax/index.php?component=panier&action=reprise_panier";
	xhr_object.open("GET",url,true);
	xhr_object.onreadystatechange=onreadystatechange = function() {
		if ( xhr_object.readyState == 4 && xhr_object.status == 200 ) {
			var laInfos = xhr_object.responseText.split(new RegExp("separator","g"));
			setAndExecute('panier',laInfos[1]);
			togglePanier();
		}
	}
	xhr_object.send(null);
}

//Appel AJAX : Reprise de la liste des précédents achats
function repriseFavorisPrecedentsAchats(asProduitIds) {
	var lsProduitIds = '';
	laProduitIds = asProduitIds.split('-');
	for (var i = 0; i < laProduitIds.length; i++) {
		lsProduitIds += '&produit_ids[]='+laProduitIds[i];
	}	

	var xhr_object = createXMLHttpRequest();
	var url = reverse_path+"service_project/ajax/index.php?component=panier&action=reprise_favoris_precedents_achats"+lsProduitIds;
	xhr_object.open("GET",url,true);
	xhr_object.onreadystatechange=onreadystatechange = function() {
		if ( xhr_object.readyState == 4 && xhr_object.status == 200 ) {
			var laInfos = xhr_object.responseText.split(new RegExp("separator","g"));
			setAndExecute('panier',laInfos[1]);
			var laProduitsQtes = jQuery.parseJSON(laInfos[2]);
			for (var i = 0; i < laProduitIds.length; i++) {
				for (var j = 0; j < laProduitsQtes.length; j++) {
					if ( laProduitsQtes[j].produit_id == laProduitIds[i] ) {
						$("#"+laProduitsQtes[j].produit_id).children(".caddie").hide();
						$("#"+laProduitsQtes[j].produit_id).children(".quantite").show();
						$("#qte"+laProduitsQtes[j].produit_id).text(laProduitsQtes[j].produit_qte);
						break;
					}
					$("#"+laProduitIds[i]).children(".caddie").show();
					$("#"+laProduitIds[i]).children(".quantite").hide();
					$("#qte"+laProduitIds[i]).text('0');
				}
			}
			togglePanier();
		}
	}
	xhr_object.send(null);
}

//Appel AJAX : Reprise d'une commande
function repriseCommande(asCommandeId) {
	var xhr_object = createXMLHttpRequest(asCommandeId);
	var url = reverse_path+"service_project/ajax/index.php?component=panier&action=reprise_commande&commande_id="+asCommandeId;
	xhr_object.open("GET",url,true);
	xhr_object.onreadystatechange=onreadystatechange = function() {
		if ( xhr_object.readyState == 4 && xhr_object.status == 200 ) {
			setAndExecute('panier',xhr_object.responseText);
			togglePanier();
		}
	}
	xhr_object.send(null);
}

// Appel AJAX : Ajoute un produit dans le panier
function refreshPanier() {
	var xhr_object = createXMLHttpRequest();
	var url = reverse_path+"service_project/ajax/index.php?component=panier&action=get_mon_panier";
	xhr_object.open("GET",url,true);
	xhr_object.onreadystatechange=onreadystatechange = function(){
		if ( xhr_object.readyState == 4 && xhr_object.status == 200 ) {
			var laInfos = xhr_object.responseText.split(new RegExp("separator","g"));
			setAndExecute('panier',laInfos[1]);
			togglePanier();
		}
	}
	xhr_object.send(null);
}

//Appel AJAX : Ajoute un produit au panier en utilisant des combinaisons de paiement
function ajoutProduitWithPaiement(asProduitId,asCombinaison,aiQte,cmdRef,asProvenance) {
	var xhr_object = createXMLHttpRequest();
	var url = reverse_path+"service_project/ajax/index.php?component=panier&action=modifier_qte_produit&produit_id="+asProduitId+"&combinaison="+asCombinaison+"&produit_qte="+aiQte+"&provenance="+asProvenance;
	if (cmdRef == 1) {
		url += '&cmd_ref=1';
	}
	xhr_object.open("GET",url,true);
	xhr_object.onreadystatechange=onreadystatechange = function() {
		if ( xhr_object.readyState == 4 && xhr_object.status == 200 ) {
			var laInfos = xhr_object.responseText.split(new RegExp("separator","g"));

			setAndExecute('panier',laInfos[1]);

			if ( laInfos[2]!=undefined && document.getElementById('div_commande')!=null ) {
				setAndExecute('div_commande',laInfos[2]);
			}
		}
	}
	xhr_object.send(null);
}
