var urlSite = 'http://www.lavauzelle.com/keops';

function isInteger(s) {
	return (s.toString().search(/^-?[0-9]+$/) == 0);
}

function updateLg(CodeArt, Activite, Qte, topP, leftP, idDivResult, idChampQte, CodeLigCde, updateQte, CodeCde, functionSuccess){
	
	if (typeof(CodeCde) == 'undefined'){CodeCde = 0;}
	if (typeof(CodeLigCde) == 'undefined'){CodeLigCde = 0;}
	if (typeof(updateQte) == 'undefined'){updateQte = 0;}
	
	if (typeof(idDivResult) != 'undefined' && idDivResult != ''){
		//un seul ajout et affichage du résultat
		$(idDivResult).setStyle({
			top: parseInt(topP) + 'px',
			left: parseInt(leftP) + 'px'
		});
		
		if(CodeArt == ''){
			$(idDivResult).innerHTML = 'Veuillez saisir une référence d\'article';
			$(idDivResult).style.display = '';
			new PeriodicalExecuter(function(pe) {
						$(idDivResult).hide();
						pe.stop();
					}, 3);
			return;
		}
		
		//quantite non saisie ou n'est pas un nombre entier
		if (Qte == '' || !isInteger(Qte) || Qte == 0){
			$(idDivResult).innerHTML = 'Veuillez saisir un nombre entier pour la quantité';
			$(idDivResult).style.display = '';
			$(idChampQte).focus();
			new PeriodicalExecuter(function(pe) {
						$(idDivResult).hide();
						pe.stop();
					}, 3);
			return;
		}
		
		//message pour patienter
		$(idDivResult).innerHTML = '<img src="' + urlSite + '/medias/indicator.gif" style="padding-right:10px;" />Veuillez patienter ... Enregistrement en cours ...';
		$(idDivResult).show();
		
		//modification dans la BDD
		new Ajax.Updater(idDivResult,
//											'/scripts/modifLigneCommande.php', {
											urlSite + '/scripts/modifLigneCommande.php', {
													parameters:'CodeArt=' + CodeArt + '&Activite=' + Activite + '&Qte=' + Qte + '&updateQte=' + updateQte + '&CodeCde=' + CodeCde,
															onSuccess:function(){
																//réaffichage du panier de commande dans la bannière gauche
																new Ajax.Updater('descPanier',urlSite + '/includes/panier.php');
																//fait disparaitre l'info-bulle du résultat de l'ajout
																new PeriodicalExecuter(function(pe) {
																			$(idDivResult).hide();
																			pe.stop();
																		}, 5);
																if (typeof(functionSuccess) != 'undefined'){
																	eval(functionSuccess);
																}
	
															},
															onComplete:function(){
																//si on est dans le detail d'une commande => réaffichage des lignes
//																if(window.location.pathname == '/detailCommande.php'){
																if(window.location.pathname == urlSite + '/detailCommande.php'){
																	majLignesCde();
																}
															}
													}
											);
	
	} else {
		//ajouts en série, pas d'affichag direct du résultat
	var res;
		//ajout de l'article
//		new Ajax.Request('/scripts/modifLigneCommande.php', {
		new Ajax.Request(urlSite + '/scripts/modifLigneCommande.php', {
											asynchronous:false,
											parameters:'CodeArt=' + CodeArt + '&Activite=' + Activite + '&Qte=' + Qte + '&CodeLigCde=' + CodeLigCde + '&updateQte=' + updateQte, 
											onSuccess:function(){
												if (typeof(functionSuccess) != 'undefined'){
													eval(functionSuccess);
												}

											},
											onComplete:function(){
//														new Ajax.Updater('descPanier','/includes/panier.php');
														new Ajax.Updater('descPanier',urlSite + '/includes/panier.php');
														res = true;
													},
											onFailure:function(){
														res = false;
													}
											});
		return res;

		
	}
	
}




function updateFavoris(codeArt, activite, ajout, top, left, idDivAfficher, idDivCacher, idDivAfficherFP, idDivCacherFP, idDivResult){
	
	// position de "l'info bulle"
	$(idDivResult).setStyle({
		top: parseInt(top) + 'px',
		left: parseInt(left) + 'px'
	});
	
	//mise à jour 
	new Ajax.Updater(idDivResult, 
												urlSite + '/scripts/ajoutFavoris.php', {
//												'/scripts/ajoutFavoris.php', {
													parameters:'codeArt=' + codeArt + '&activite=' + activite + '&ajout=' + ajout, evalScripts:true, asynchronous:true,
														onSuccess:function(){
															//affichage de la boite de message (se rend invisible au bout de 3 secondes)
															$(idDivResult).show();
															new PeriodicalExecuter(function(pe) {
															    	$(idDivResult).hide();
																		pe.stop();
																	}, 3);
															//modification du style et du libellé du lien Ajouter<=> Retirer
															if (typeof(idDivAfficher) != 'undefined'){
																if (idDivAfficher != ''){$(idDivAfficher).show();}
															}
															if (typeof(idDivCacher) != 'undefined'){
																if (idDivCacher != ''){$(idDivCacher).hide();}
															}
															//sur fiche produit si nécessaire
															if (typeof(idDivAfficherFP) != 'undefined'){
																if (idDivAfficherFP != ''){$(idDivAfficherFP).show();}
															}
															if (typeof(idDivCacherFP) != 'undefined'){
																if (idDivCacherFP != ''){$(idDivCacherFP).hide();}
															}
															
														}
													}
												);
}






function updateLgEdition(CodeArt, Activite, Qte, topP, leftP, idDivResult, idChampQte, Prix, ModifQte, functionSuccess, deleteLG){
	
	if (typeof(ModifQte) == 'undefined'){ModifQte = '';}
	if (typeof(deleteLG) == 'undefined' || deleteLG != '1'){deleteLG = '0';}
	
	if (typeof(idDivResult) != 'undefined' && idDivResult != ''){
		$(idDivResult).setStyle({
			top: parseInt(topP) + 'px',
			left: parseInt(leftP) + 'px'
		});
		
		//quantite non saisie ou n'est pas un nombre entier
		if (Qte == '' || !isInteger(Qte) || Qte == 0){
			$(idDivResult).innerHTML = 'Veuillez saisir un nombre entier pour la quantité';
			$(idDivResult).style.display = '';
			$(idChampQte).focus();
			new PeriodicalExecuter(function(pe) {
						$(idDivResult).hide();
						pe.stop();
					}, 3);
			return;
		}
		
		//message pour patienter
		$(idDivResult).innerHTML = '<img src="' + urlSite + '/medias/indicator.gif" style="padding-right:10px;" />Veuillez patienter ... Mise &agrave; jour de votre panier en cours ...';
		$(idDivResult).show();
		
		//modification dans la BDD
		new Ajax.Updater(idDivResult,
											urlSite + '/scripts/modifLigneCommandeEdition.php', {
													parameters:'CodeArt=' + CodeArt + '&Activite=' + Activite + '&Qte=' + Qte + '&Prix=' + Prix + '&ModifQte=' + ModifQte,
													onComplete:function(){
														//réaffichage du panier de commande dans la bannière gauche
														new Ajax.Updater('descPanier',urlSite + '/includes/panierEd.php');
														//fait disparaitre l'info-bulle du résultat de l'ajout
														new PeriodicalExecuter(function(pe) {
																	$(idDivResult).hide();
																	pe.stop();
																}, 5);
														if (typeof(functionSuccess) != 'undefined'){
															eval(functionSuccess);
														}
	
													}
													
												}
											);
	
	} else {
		
		new Ajax.Request(urlSite + '/scripts/modifLigneCommandeEdition.php', {
													parameters:'CodeArt=' + CodeArt + '&Activite=' + Activite + '&Qte=' + Qte + '&Prix=' + Prix + '&ModifQte=' + ModifQte + '&deleteLG=' + deleteLG,
													onComplete:function(){
														//réaffichage du panier de commande dans la bannière gauche
														new Ajax.Updater('descPanier',urlSite + '/includes/panierEd.php');
														if (typeof(functionSuccess) != 'undefined'){
															eval(functionSuccess);
														}
	
													}
													
												}
											);
		
		
	}

}