/**
 * Lib JS 2010
 * S'appuie sur la lib jquery
 * 20100413 
 */	

/********************************************
	VAR GLOBAL
 *********************************************/

/* Switch d'url */
var docurl = document.URL; 
if(docurl.search('phoebus')>0){
	var houra_url = "http://phoebus.houra.fr:88/~rguignard/houra_615/";
	var image_url = "http://phoebus.houra.fr:8888/images/";
}else if(docurl.search('beta')>0){
	var houra_url = "http://beta.houra.fr/";
	var image_url = "http://media.houra.fr/images/";	
}else if(docurl.search('pharaon')>0){
	var houra_url = "http://pharaon.houra.fr/";
	var image_url = "http://media.houra.fr/images/";	
}else if(docurl.search('192.168.6.118')>0){
	var houra_url = "http://192.168.6.118/";
	var image_url = "http://192.168.6.22/images/";	
}else{
	var houra_url = "http://www.houra.fr/";
	var image_url = "http://media.houra.fr/images/";	
}

var is_modif;
var time_modif = 'une heure';

/********************************************
 *	RÉCUPÉRATION DES INFOS DU NAVIGATEUR POUR IE 
 *********************************************/
var N=navigator.appName; var V=navigator.appVersion;
var version="?"; var nom=N; var os="?"; var langue="?";
if (N=="Microsoft Internet Explorer") {
	langue=navigator.systemLanguage
	version=V.substring(V.indexOf("MSIE",0)+5,V.indexOf(";",V.indexOf("MSIE",0)));
	if (V.indexOf("Win",0)>0) {
		if ( V.indexOf(";",V.indexOf("Win",0)) > 0 ) {
			os=V.substring(V.indexOf("Win",0),V.indexOf(";",V.indexOf("Win",0)));
		} else {
			os=V.substring(V.indexOf("Win",0),V.indexOf(")",V.indexOf("Win",0)));
		}
	}
	if (V.indexOf("Mac",0)>0) {
		os="Macintosh";
		version=V.substring(V.indexOf("MSIE",0)+5,V.indexOf("?",V.indexOf("MSIE",0)));
	}
}	

/********************************************
 * Fonctions Generales 
 **********************************************/

function AccentToNoAccent(str) {
	var norm = new Array('À','Á','Â','Ã','Ä','Å','Æ','Ç','È','É','Ê','Ë','Ì','Í','Î','Ï', 'Ð','Ñ','Ò','Ó','Ô','Õ','Ö','Ø','Ù','Ú','Û','Ü','Ý','Þ','ß', 'à','á','â','ã','ä','å','æ','ç','è','é','ê','ë','ì','í','î','ï','ð','ñ', 'ò','ó','ô','õ','ö','ø','ù','ú','û','ü','ý','ý','þ','ÿ');
	var spec = new Array('A','A','A','A','A','A','A','C','E','E','E','E','I','I','I','I', 'D','N','O','O','O','0','O','O','U','U','U','U','Y','b','s', 'a','a','a','a','a','a','a','c','e','e','e','e','i','i','i','i','d','n', 'o','o','o','o','o','o','u','u','u','u','y','y','b','y');
	for (var i = 0; i < spec.length; i++)
		str = replaceAll(str, norm[i], spec[i]);
	return str;
} 	

function SearchRegExp1(terme, contenu) {
	var exp=new RegExp(terme,"g");
	if ( exp.test(contenu) ) {
		return true;
	}else {
		return false; 
	}
}

function AddFavoris() {
	if ( navigator.appName != 'Microsoft Internet Explorer' ){ 
		window.sidebar.addPanel("houra.fr - le cybermarche","http://www.houra.fr",""); 
	}else { 
		window.external.AddFavorite("http://www.houra.fr","houra.fr - le cybermarche"); 
	} 
}

function hourapopup(url){
	window.open(url,'hourawin','scrollbars=yes,resizable=yes,width=500,height=550');
}

function Hide (id){
	$('#'+id).css('display', 'none');
}

function Show (id){
	$('#'+id).css('display', 'block');
}

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

//enleve les vides dans un tableau js
function arrayTrim(arr2){
	var arr1=new Array();
	for(j=0;j<arr2.length;j++)
		if(arr2[j]!="")
			arr1[arr1.length]=arr2[j];
	arr2.length=arr1.length;
	for(j=0;j<arr1.length;j++)
		arr2[j]=arr1[j];
	return arr2;
}

function trunc (id, longMax){
	var CLong = document.getElementById(id).offsetWidth;
	if (CLong > longMax){
		while (CLong > longMax){
			var content = document.getElementById(id).innerHTML;
			var pos = content.length-2;
			document.getElementById(id).innerHTML = content.substring(0, pos);
			CLong = document.getElementById(id).offsetWidth ;
		}
		document.getElementById(id).innerHTML = document.getElementById(id).innerHTML+' ...';
	}	
	return true;
}

/********************************************
 *	FONCTION POUR LES MENUS DEROULANT SUR IE6
 *	(pas de hover possible sur les li)
 *********************************************/


sfHover = function() {
	/**
	 * @Todo : Rajoute une vérif sur le classe pour ne pas parcourir tous les li 
	 */	

	var sfEls = $("li.menu_deroulant");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className += " sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");			
		}

	}
}

/* Load la fonction dans les cas IE < 7 */
if (nom=="Microsoft Internet Explorer" && version < 7) {
	if (window.attachEvent) {
		window.attachEvent("onload", sfHover);				
	}	
}


/********************************************************
 * Fonction de resize des div pour le catalogue 
 ******************************************************/

function Resize(){
	if(/iPhone|iPod|iPad|Android|opera mini|blackberry|palm os|palm|hiptop|avantgo|plucker|xiino|blazer|elaine|iris|3g_t|windows ce|opera mobi|windows ce; smartphone;|windows ce;iemobile/i.test(navigator.userAgent)){
		return;
	}
	$('html, body').css('overflow', 'hidden');
	
	/* si TdZoneHomeRight  alors on est sur la Home PAge */
	if($('#TdZoneHomeRight > div').length){
		ResizeHomePage();
//alert("HP");		
	/* Zone de geuche + panier --> 3 scrollbar */	
	}else if($('div.BtkLeft').length &&  $('#td_panier').length){
		Resize3Scroll();
//alert("3Scroll");		
	}else if($('#td_panier').length){
		Resize2Scroll();
//alert("2Scroll");		
	}else{
		Resize1Scroll();
//alert("1Scroll");
	}
	
	/* Gere les ancre nommé */
	if(window.location.hash){
		window.location = window.location;
	}
	
	return ;
	
}

function Resize1Scroll(){
	var hauteur = $(window).height();
	hauteur -= $('#ZoneOngletTop').height();
	$('#ZoneContent').css('overflow', 'auto');
	$('#ZoneContent').height(hauteur);
}

/**
 * Cas avec 2 scroll ()
 */
function Resize2Scroll (){
	var haut = $(window).height();
	var haut_content = haut - $('#ZoneOngletTop').height() ;

	$('#ZoneContentLeft').css('overflow', 'auto');
	$('#ZoneContentLeft').height(haut_content);
	
	haut_content = haut_content - 10;
	
	if($('#bloc_cagnotte_right').length){
		haut_content = haut_content - 85;
	}
	
	/* Scroll du panier */
	$('#td_panier').height(haut_content);
	$('#div_ligne_articles').height(haut_content - 158);
	
	return;
	
}

/**
 * Cas avec 3 scroll (left, middle, right)
 */
function Resize3Scroll(){
	/* Fixe la hauteur des bloc pour pouvoir afficher les scroll */
	var haut = $(window).height();
	
	/* Vire la hauteur du top */
	var haut_content = haut - $('#ZoneOngletTop').height() ;
	/* -17, la hauteur du fil d'ariane */
	if($('.FilArianneBtk').length)
		haut_catalogue = haut_content - 17 ;
	else
		haut_catalogue = haut_content ;
	
	
	$('table.TabBtk').height(haut_content);
	$('div.ZoneCatalogue').height(haut_catalogue);
	$('div.ZoneRecherche').height(haut_catalogue);
	$('#TdZoneRecherche').height(haut_catalogue);
	$('div.BtkLeft').height(haut_catalogue);
	
	haut_content = haut_content - 10;
	
	if($('#bloc_cagnotte_right').length){
		haut_content = haut_content - 95;
	}
	
	$('#td_panier').height(haut_content);
	/* 158, hauteur du bloc total achats + commander du bas de panier */
	$('#div_ligne_articles').height(haut_content - 158);
	
	if($('div.ZoneCatalogue').length){
		/* padding entre les blocs article pour un effet centré */
		var largeur_bloc = 207;
		var Larg = $("div.ZoneCatalogue").width() - 17;
		var nb = parseInt(Larg/largeur_bloc);
		var pad = parseInt( (Larg - parseInt(nb*largeur_bloc)) / nb) ;
		pad = parseInt(pad/2)-1;
	
		if(nb>2){
			$("div.bloc_article_float").css('padding-left', pad+'px');
			$("div.bloc_article_float").css('padding-right', pad+'px');
		}
	}
}

function ResizeHomePage(){
	/* Pour palier a un bug IE 6 */
	if (nom=="Microsoft Internet Explorer" && version < 7) {
		$('html, body').css('overflow', 'auto');
		return;
	}
	
	/* Fixe la hauteur des bloc pour pouvoir afficher les scroll */
	var haut = $(window).height();
	/* Vire la hauteur du top */
	var haut_right = haut - $('#TdZoneHomeRight > div').height() - $('#ZoneOngletTop').height();
	var haut_content = haut - $('#ZoneOngletTop').height() ;
	haut_right  = haut_right - 10 ;

	$('div.ZoneHomePage').height(haut_content);
	$('div.HomeLeft').height(haut_content);
	$('#td_panier').height(haut_right);
	// 158, la hauteur du bloc du bas du panier (total achat, bt commander, incentive, etc..)
	$('#div_ligne_articles').height(haut_right - 158);
}

/* On va assigner le fonction resize a l'evenement on resize */
$(document).ready(function() {
	$(window).resize(function() { Resize();});
});	


/********************************
 * Classe Elipsis pour firefox non gérer dur les version < 8
 *******************************/

if(N=='OLDNetscape'){
	$(document).ready(function() {
		$("div.trunc span").each(function(i) {
			var p = $(this).parent();
			var pp = p.parent();
			if (this.offsetWidth > parseInt(pp.width())) {

				var p = $(this).parent();
				p.css('overflow', 'hidden');	
				p.css('float', 'left');	
				p.width(parseInt(pp.width()-10));
				p.after('&hellip;');
			}
		});
	});
}



/********************************************
	VERIF DES FORM
 *********************************************/
function ValidCp (Obj){
	if ( Obj.value == "" || isNaN(Obj.value) )
		return "Veuillez saisir un code postal";
	else if(Obj.value.length != 5)	
		return "Le code postal doit etre sur 5 chiffres ";
	else
		return "ok";
}

function CheckIsNotNull (Obj){
	if(Obj.value==''){
		Obj.focus();
		return false;
	}else	
		return true;
}

function ValidMail (Obj){
	var mail = Obj.value;
	if ((mail.indexOf("@")>=0)&&(mail.indexOf(".")>=0)) 
		return "ok";
		else {
			Obj.focus();
			return "Mail invalide !";      
		}
}

function VerifyEmail(Obj) {	
	var modele = "^[A-Za-z0-9\-_\.]+@[A-Za-z0-9\-_\.]+\.[A-Za-z]{2,5}$";
	var re = new RegExp(modele);
	if (Obj.val().match(re))
		return true;
	Obj.focus();
	return false;
}

function VerifyCheck(obj){
	if (obj.is(':checked')==true){
		return true;	
	}else{
		return false;
	}
}	



/********************************************
	FONCTION DES ACHATS 
 *********************************************/
function ModifQte(id_form, id_qte, val, qte_dispo, qte_max, qte_min) {
	var pluriel = "";

	/*Recup la valeur courante */
	var qte = $("#"+id_qte).val();

	/* Ajoute ou soustrait */
	qte = parseInt(qte) + parseInt(val);

	/* @todo : ONVA GERER LA QTE ACHETABLE en dynamique : qte_max */

	if(qte>1)
		pluriel='s';
	if(qte < qte_min){
		qte = $('#'+id_qte).val();
	}else if(qte > qte_max){	
		if($('#LibQteMax_'+id_qte))
			$('#LibQteMax_'+id_qte).html('<img src="'+image_url+'bt_croix_rouge.png" alt="Quantit&eacute; maximum atteinte"  title="Quantit&eacute; maximum atteinte"> Quantit&eacute; maximum atteinte');
		qte = $('#'+id_qte).val();		
	}else if(qte > qte_dispo){
		if($('#LibQteMax_'+id_qte))
			$('#LibQteMax_'+id_qte).html('<img src="'+image_url+'bt_croix_rouge.png" alt="Quantit&eacute; maximum atteinte"  title="Quantit&eacute; maximum atteinte"> Quantit&eacute; maximum atteinte');
		qte = $('#'+id_qte).val();
	}else{	
		if($('#LibQteMax_'+id_qte))
			$('#LibQteMax_'+id_qte).html('');
		/* Met a jour le champ*/
		$('#'+id_qte).val(qte);		
	}	
}	

function AcheterArticle (FormId){
	/* Recup l'id_article */
	var id_art = $("#"+FormId+" > input[name='id_article']").val();
	var origine  = $("#"+FormId+" > input[name='origine']").val();
	var id_origine = $("#"+FormId+" > input[name='id_origine']").val();
	var qte = $("#"+FormId+" input[name='quantite']").val();
	$.ajax({
		type: 'POST',
		dataType: 'json',
		url: houra_url+'pan/pan_ajax.php',
		data: $('#'+FormId).serialize(),
		success: function(msg){
			if(msg.code_retour == 0){
				//cas recap panier
				if(document.URL.search('com/recapitulatif_panier.php') >= 0){
					is_modif = 'O'; //sert à lancer reloadBeforeClose de FancyBox
					ShowQtePanier(id_art, msg.qte ,qte);
					//cas Mise en avant : on reload la page si achat depuis une mise en avant
					if (FormId.search('MEA') >= 0){
						document.location = document.location;
						$('body, html').css('cursor', 'wait');
					}
				}else {
					ShowQtePanier(id_art, msg.qte ,qte);
					//Cas remplacement d'article dans les listes, on recharge la page
					if($('input[name="remplacer_article"]').length)
						is_modif = 'O';
					else
						$.post(houra_url+'pan/pan_ajax.php',{action_panier: "reload_panier", tag:msg.tag, id_article:id_art}, function(html){							
							$('#td_panier').html(html);
							Resize();
						});
				}
			}
			else if (msg.code_retour == -20018 || msg.code_retour == -20041){
				ShowQtePanier(id_art, msg.qte ,qte);
				$.post(houra_url+'pan/pan_ajax.php',{action_panier: "reload_panier"}, function(html){
					$('#td_panier').html(html);
					Resize();
				});
				alert('Quantité maximum atteinte');
			}
			
			/* gestion du compteur euro fid */
			//if(msg.euro_fid_panier && msg.points_fid_panier)
				MajCagnotePanier(msg.euro_fid_panier, msg.points_fid_panier);
			
			return true;
			
				 
		}
	});				
}


function ShowQtePanier (id_article, qte, qte_voulue){
	/* Les boutiques */
	if($('#QtePanier_'+id_article).length){
		if(qte_voulue && qte_voulue > qte){
			var img = image_url+'caddie_coche_orange.gif';
			var css = 'quantite_achat';
		}else{
			var img = image_url+'caddie.gif';
			var css = 'quantite_achat';
		}
		if($('#QtePanier_'+id_article).length){
			if (qte==0)
				$('#QtePanier_'+id_article).html('');	
			else	
				$('#QtePanier_'+id_article).html('<table><tr><td><img src="'+img+'"  alt="'+qte+' déja dans mon panier" title="'+qte+' déjà dans mon panier"></td>	<td class="'+css+'">'+qte+'</td>	</tr>	</table>');	
			/*$('#QtePanier_'+id_article).html('<img src="'+img+'" alt="'+qte+' dans mon panier" title="'+qte+' dans mon panier"> <span class="'+css+'">'+qte+'</span> ');*/	
		}
		if($('#ZoneInfos_'+id_article).length){
			if (qte==0)
				$('#ZoneInfos_'+id_article).css('background', '').removeClass('en_panier');	
			else
				$('#ZoneInfos_'+id_article).css('background',  '#d3e6af').addClass('en_panier');
		}
	}

	/* Le layer de substitution */
	if($('#SG_ligne_lib_'+id_article).length){
		if(qte_voulue && qte_voulue > qte){
			var img = image_url+'caddie_coche_orange.gif';
			var css = 'quantite_achat';
		}else{
			var img = image_url+'caddie.gif';
			var css = 'quantite_achat';
		}
		if($('#SG_qte_pan_'+id_article).length){
			if (qte==0)
				$('#SG_qte_pan_'+id_article).html('');	
			else	
				$('#SG_qte_pan_'+id_article).html('<img src="'+img+'" alt="'+qte+' déja dans mon panier" title="'+qte+' déjà dans mon panier"><b style="color:#2ABF41">'+qte+'<b>');	
			/*$('#QtePanier_'+id_article).html('<img src="'+img+'" alt="'+qte+' dans mon panier" title="'+qte+' dans mon panier"> <span class="'+css+'">'+qte+'</span> ');*/	
		}

		if($('#SG_ligne_lib_'+id_article).length){
			if (qte==0)
				$('#SG_ligne_lib_'+id_article).css('background', '');	
			else	
				$('#SG_ligne_lib_'+id_article).css('background',  '#F5FED3');
		}		
	}

	/* Une fiche article */
	if($('#FAQtePanier_'+id_article).length){
		if(qte_voulue && qte_voulue > qte){
			var img = image_url+'bt_coche_orange.png';
			var css = 'QtPanierIncorrect';
		}else{
			var img = image_url+'bt_coche_verte.png';
			var css = 'QtPanier';
		}

		if (qte==0){
			$('#FAQtePanier_'+id_article).html('');	
			$('#QtePanier_'+id_article).html('<img width="36" height="1" src="'+image_url+'_.gif">');	
		}
		else	
			$('#FAQtePanier_'+id_article).html('<span class="'+css+'"><img src="'+img+'" alt="'+qte+'  dans mon panier" title="'+qte+' dans mon panier"> <strong>'+qte+'</strong> dans mon panier</span>');
	}
	/* pour les article recherche */
	if($('#SearchZoneInfos_'+id_article).length){
		if (qte==0){
			$('#SearchZoneInfos_'+id_article).css('background', '');	
			$('#ZoneLabel_'+id_article).css('background', '');	
			$('#ZoneBRI_'+id_article).css('background', '');	
			$('#ZonePrix_'+id_article).css('background', '');	
			$('#QtePanier_'+id_article).html('<img src="'+image_url+'_.gif" width="36" height="1" >');
		}else{	
			$('#SearchZoneInfos_'+id_article).css('background',  '#d3e6af');
			$('#ZoneLabel_'+id_article).css('background',  '#d3e6af');
			$('#ZoneBRI_'+id_article).css('background',  '#d3e6af');
			$('#ZonePrix_'+id_article).css('background',  '#d3e6af');
		}	
	}
	
	/* pour les article Asso */
	if($('#ArtAssoZoneInfos_'+id_article).length){
		if (qte==0){
			$('#ArtAssoZoneInfos_'+id_article).css('background', '');	
			$('#ArtAssoZoneLabel_'+id_article).css('background', '');	
			$('#ArtAssoZoneBRI_'+id_article).css('background', '');	
			$('#ArtAssoZonePrix_'+id_article).css('background', '');	
			$('#ArtAssoQtePanier_'+id_article).html('<img src="'+image_url+'_.gif" width="36" height="1" >');
		}else{	
			$('#ArtAssoZoneInfos_'+id_article).css('background',  '#d3e6af');
			$('#ArtAssoZoneLabel_'+id_article).css('background',  '#d3e6af');
			$('#ArtAssoZoneBRI_'+id_article).css('background',  '#d3e6af');
			$('#ArtAssoZonePrix_'+id_article).css('background',  '#d3e6af');
			var img = image_url+'caddie.gif';			
			$('#ArtAssoQtePanier_'+id_article).html('<table><tr><td><img src="'+img+'"  alt="'+qte+' déja dans mon panier" title="'+qte+' déjà dans mon panier"></td>	<td class="'+css+'">'+qte+'</td>	</tr>	</table>');
		}	
	}
	
	/* Ancienne fonction de color des anicenne BTK, memo, recherche .... */
	if($('#ligne_lib_'+id_article).length){
		if (qte==0)
			uncolorArt (id_article);	
		else	
			colorArt (id_article, qte);
	}
}

function MajCagnotePanier (EuroFid, PointFid) {

	if(typeof(CounterPointFid)=='undefined'){ 
		return false;
	}
	
	if(typeof(nb_euro_fid)=='undefined'){ 
		nb_euro_fid=0;
	}
	if(typeof(nb_point_fid)=='undefined'){ 
		nb_point_fid=0;
	}
	
	
	
	if(EuroFid!=nb_euro_fid  && CounterEuroFid){
		if(EuroFid > nb_euro_fid)
			CounterEuroFid.incrementTo(EuroFid, 2);
		else if(nb_euro_fid > EuroFid){
			/* Pour une soustraction on réinitialize le compterur car il galere avec le*/
			$("#counter_euro_fid").html("");
			CounterEuroFid = new flipCounter('counter_euro_fid', {value:EuroFid, inc:100, pace:200, auto:false});
		}	
			//CounterEuroFid.setValue(EuroFid)
			//CounterEuroFid.subtract((nb_euro_fid-EuroFid));	
		nb_euro_fid = EuroFid;
		
	}
	if(PointFid!=nb_point_fid  && CounterPointFid){
		if(PointFid > nb_point_fid)
			CounterPointFid.incrementTo(PointFid, 2);
		else if(nb_point_fid > PointFid){
			/* Pour une soustraction on réinitialize le compterur car il galere avec le*/
			$("#counter_point_fid").html("");
			CounterPointFid = new flipCounter('counter_point_fid', {value:PointFid, inc:100, pace:200, auto:false});
		}

		nb_point_fid = PointFid;
	}
	
}

function ReloadBeforeClose (){
	if(is_modif=='O'){
		$('body, html').css('cursor', 'wait');
		document.location = document.location;
	}
	return true;
}

function AddArtToListe (id_article, id_liste, qte, id_origine){
	$.ajax({
		type: "POST",
		url: houra_url+'memolistes/liste_ajax.php',
		data: {form_action : 'add_art_to_list', 
			id_article : id_article, 
			id_liste : id_liste, 
			qte : qte, 
			id_origine : id_origine},
			success: function(msg){
				if($('#FARetourListe_'+id_article+'_'+id_liste)) 
					$('#FARetourListe_'+id_article+'_'+id_liste).html(msg); 
				if($('#RetourListe_'+id_article+'_'+id_liste)) 
					$('#RetourListe_'+id_article+'_'+id_liste).html(msg);
			}
	});
}

function AddArtToNewList (id_article, qte, id_origine, origine){
	/* Recup le nom de liste */
	if(origine=='FA'){
		var nom  = $('#FANewList_'+id_article).val();
		var ret = 'FARetourNewListe_'+id_article;
	}else{	
		var nom  = $('#NewList_'+id_article).val();
		var ret = 'RetourNewListe_'+id_article;
	}	
	$.ajax({
		type: "POST",
		url: houra_url+'memolistes/liste_ajax.php',
		data: {form_action : 'add_art_to_new_list', 
			id_article : id_article, 
			qte : qte, 
			nom_liste : nom, 
			origine : origine, 
			id_origine : id_origine},
			success: function(msg){
				$('#'+ret).html(msg); 
			}
	});
}

/**
 * 
 * @param id_article
 * @param id_origine
 * @param origine
 */
function LoadBtkMemoListeLayer(id_article, id_origine, origine){

	if(origine=='FA'){
		var layer_list = '#FA_list_list_'+id_article;
		var act = 'load_fa_memoliste'; 
	}else{
		var layer_list = '#list_list_'+id_article;
		var act = 'load_btk_memoliste';
	}
	/*
	$('#FA_list_list_'+id_article).ajaxStart(function() {
		  $(this).text('Chargement en cours...');
		});
	 */
	$(layer_list).html('Chargement en cours...');

	$(layer_list).css('display', 'inline');

	$.ajax({
		type: "POST",
		url: houra_url+'memolistes/liste_ajax.php',
		data: {form_action : act, id_article : id_article, id_origine : id_origine, origine : origine},
		success: function(msg){
			$(layer_list).html(msg); 
			/* Si on est dans une boutique on lance un resize du catalogue puisque l'affichage du layer peut changer la taille du catalogue */
			/*
			    if (act == 'load_btk_memoliste'){
				   Resize();
			   }
			 */   
		}
	});

}

function PanierToMemo(id_panier){
	var obj=document.getElementById("img_creer_0");
	obj.style.display='none';
	var obj=document.getElementById("img_creer_1");
	obj.style.display='block';
	var obj=document.getElementById("creer_0");
	obj.style.display='none';
	var obj=document.getElementById("creer_1");
	obj.style.display='block';

	$.ajax({
		type: "POST",
		url: houra_url+'memolistes/liste_ajax.php',
		data: {form_action : 'panier_to_memo', id_panier : id_panier},
		success: function(msg){
			$('#retour_liste').html(msg); 
		}
	});
}

function AcheterPanierAbandonne(id_panier){
	Hide("img_fullList_0");
	Show("img_fullList_1");
	Hide("fullList_0");
	Show("fullList_1");
	$("#form_acheter_panier_abandonne").submit();
}

function AlertModifEnCours(){
	alert("Vous avez déjà une commande en cours de modification, si vous souhaitez modifier une autre commande vous devez d'abord valider ou annuler les modifications en cours.");
}

function ModifCommande(id_cmd, id_user){
	if(confirm('Vous disposez d\''+time_modif+' pour modifier et valider vos modifications.\n'
			+'Si vous n\'avez pas validées celles-ci passé ce délai, vous recevrez votre commande initiale.\n'
			+'Etes-vous sûr de vouloir modifier cette commande ? ')){
		$('body').css('cursor','wait');   
		WaitingBoutonModifCmd(id_cmd);
		$.ajax({
			type: "POST",
			url: houra_url+'pan/pan_ajax.php',
			data: {action_panier : 'modif_cmd', 
				id_cmd : id_cmd, 
				id_user : id_user},
				success: function(msg){
					$('body').css('cursor','default');     	 
					$('#RetModifCmd').html(msg); 
				}
		});
	}
}

function ModifCreneau(id_cmd, id_user){
	if(confirm('Vous disposez d\''+time_modif+' pour modifier et valider vos modifications.\n'
			+'Si vous n\'avez pas validées celles-ci passé ce délai, vous recevrez votre commande initiale.\n'
			+'Etes-vous sûr de vouloir modifier votre rendez-vous ? ')){
		$('body').css('cursor','wait');     
		WaitingBoutonModifCmd(id_cmd);
		$.ajax({
			type: "POST",
			url: houra_url+'pan/pan_ajax.php',
			data: {action_panier : 'modif_creneau', 
				id_cmd : id_cmd, 
				id_user : id_user},
				success: function(msg){
					$('body').css('cursor','default');     	 
					$('#RetModifCmd').html(msg); 
				}
		});
	}
}

function ModifCommandeListePanier(id_cmd, id_user){
	if(confirm('Vous avez un panier en cours, celui-ci va être sauvegardé dans une liste.'))
		$.ajax({ type: "POST", url: houra_url+'pan/pan_ajax.php', data: {action_panier : 'liste_from_panier'} });
	ModifCommande(id_cmd, id_user);
}

function ModifCreneauListePanier(id_cmd, id_user){
	if(confirm('Vous avez un panier en cours, celui-ci va être sauvegardé dans une liste.'))
		$.ajax({ type: "POST", url: houra_url+'pan/pan_ajax.php', data: {action_panier : 'liste_from_panier'} });
	ModifCreneau(id_cmd, id_user);
}

function AnnuleModif (id_cmd, id_user){
	if(confirm('Si vous annulez vos modifications, vous recevrez votre commande initiale.  Confirmez-vous l\'annulation de vos modifications ?')){
		$('body').css('cursor','wait');     
		$.ajax({
			type: "POST",
			url: houra_url+'pan/pan_ajax.php',
			data: {action_panier : 'annule_modif_cmd', 
				id_cmd : id_cmd, 
				id_user : id_user},
				success: function(msg){
					$('body').css('cursor','default');     
					$('#RetModifCmd').html(msg); 
				}
		});
	}
}

function WaitingBoutonModifCmd(id_cmd){
	var img_wait = '<img src="'+image_url+'wait.gif" alt="Patienter">'
	$('#ZoneBtModifCmd_'+id_cmd).html(img_wait);
	return true;
}

function ConfDeconexionModif (){
//	if(confirm("Confirmez-vous votre deconnexion ? \n Si oui, les modifications en cours seront perdues et vous recevrez votre commande initial. "))
	if(confirm("Confirmez-vous votre déconnexion ? \n Si oui, les modifications en cours seront perdues et vous recevrez votre commande initiale."))
		window.top.location.href = houra_url+'index.php?deconnexion=true';
}

function Rechercher(){
	if($('#ZoneSaisie').val()=="")
		alert ('Il faut saisir un mot a rechercher');
	else{
		$('body').css('cursor','wait');
		$('#Recherche').submit();
	}
}

function AddWord (id, libelle){
	/* Pour le moment on ne servira pas de l'id */
	var mots = $('#ZoneSaisie').val();
	var tab_mot = mots.split("\n");
	tab_mot.push(libelle);
	if(tab_mot[0] == "")
		tab_mot.shift();
	$('#ZoneSaisie').val(tab_mot.join("\n"));
	$("#ZoneSaisie").attr({ scrollTop: $("#ZoneSaisie").attr("scrollHeight") });
}

/**
 * Fonction pour fancybox
 * "fancyboxise" les lien a.LayerFA et resize les FB dont hauteur < a la hautteur de la window - 150;
 */
function InitFancyBoxResized(selecteur){
	$(document).ready(function() {
		FancyBoxResized(selecteur);
	});
}
/**
 * Configuration fancybox standardisée
 * @param selecteur
 */
function FancyBoxResized(selecteur){
	$(selecteur).colorbox({
		maxWidth: '90%',
		maxHeight: '90%',
		title: ' '
	});
}

/**
 * fonction générique qui layerise un element (colorbox)
 * @param selecteur un selecteur css
 */
function Layerise(selecteur){
	$(selecteur).colorbox({
		maxWidth: '90%',
		maxHeight: '90%',
		title: ' '
	});
}

/**
 * Fonction générique qui layerise sur le document ready
 * @param selecteur un selecteur css
 */
function LayeriseWhenReady(selecteur){
	$(document).ready(function() {
		Layerise(selecteur);
	});
}

/* Lance  le fancy box du layer d'idendification */
InitFancyBoxResized('#LnkLayerId');

/**
 * Crée un Obj fancy sur les a href avec la classe class_css, passé en argument 
 * @param class_css
 */
function FancyBoxise (class_css){
	$("a."+class_css).colorbox({
		maxWidth: '90%',
		maxHeight: '90%',
		opacity: 0.3,
		title: ' '
	});
	/*
	var hauteur_max_fancy = 370;
	$(document).ready(function() {
		$("a."+class_css).fancybox({
			ajax : {type : "GET", cache	: false},
			padding : 10,
			margin: 5,
			titleShow: false,
			centerOnScroll: true,
			onComplete: function(){
				//alert($('#fancybox-inner').height());
				//var LayerContent = $('#fancybox-inner').children();

				//alert($('#fancybox-inner > div').height());

				if ($('#fancybox-inner > div').height()< hauteur_max_fancy ){
					$.fancybox.resize();
				}
			}
		});
	});
	*/
}

function LayerIdentify(dest){
	url = houra_url+'layer/layer_identification.php';
	url += dest == undefined ? "" : '?dest='+dest;
	$.colorbox({
		href: url
	});
}

function LayerIdentifyBloquant(lnk){
	$.colorbox({
		href: lnk,
		escKey: false,
		overlayClose: false,
		onLoad: function(){
			$('#cboxClose').remove();
		},
		onComplete: function(){
			$('#cboxLoadedContent').css('margin-top', '0px');
		}
	});
}

/**
 * Ouvre un layer qui pointe vers le lien passé en paramètre
 * @param lnk
 */
function LayerFromHref(lnk){
	$.colorbox({
		maxWidth: '90%',
		maxHeight: '90%',
		title: ' ',
        href: lnk
    }); 
}

function LayerFromHtml(html){
	$.colorbox({
		maxWidth: '90%',
		maxHeight: '90%',
		title: ' ',
        html: html
    }); 
}


/*Retourne le HTML d'une mise en avant article pour le marché */
function GetMEALeMarche(){
	$.ajax({
		type: "POST",
		url: houra_url+'lst_prod/mea_le_marche.php',
		success: function(msg){
			$('#ZoneMEAMarche').html(msg); 
		}
	});
}

/* Retourne le HTML d'une mise en avant article pour l'univers Noel */
function GetMEANoel(){
	$.post(houra_url+'lst_prod/mea_noel.php', function(msg){
		$('#ZoneMEANoel').html(msg);
	});
}

/*********************************
 * Fonctions relatives au panier *
 *********************************/
/**
 * Colore la ligne article au mouseover dans le panier
 */
function lineColor(id_article) {
	$('#'+id_article+'_a').css('background-color', '#FCCCB8');
	$('#'+id_article+'_b').css('background-color', '#FCCCB8');
	$('#'+id_article+'_c').css('background-color', '#FCCCB8');
}

/**
 * Décolore la ligne article au mouseout dans le panier
 */
function lineUncolor(id_article) {
	$('#'+id_article+'_a').css('background-color', '#FFFFFF');
	$('#'+id_article+'_b').css('background-color', '#FFFFFF');
	$('#'+id_article+'_c').css('background-color', '#FFFFFF');
}

/**
 * Récupere le dernier panier et recharge la zone panier
 */
function recupererDernierPanier(id_panier){
	$.ajax({
		url: houra_url+'pan/pan_ajax.php',
		data: {action_panier: 'recuperer_panier', id_origine: id_panier, page_src: document.location.href},
		success: function(msg){
			$('#td_panier').html(msg);
			Resize();
		}
	});
	
}

/**
 * Délock le panier et recharge la zone panier
 */
function delockerPanier(){
	$.ajax({
		url: houra_url+'pan/pan_ajax.php',
		data: {action_panier: 'delocker'},
		success: function(msg){
			$('#td_panier').html(msg);
			Resize();
		}
	});
}

/**
 * Action du bouton +1 ou -1 d'ajout article
 */
function modifierQteArticle(id_ligne, qte){
	$.ajax({
		url: houra_url+'pan/pan_ajax.php',
		data: {action_panier: 'maj_article_panier', id_ligne: id_ligne, quantite: qte},
		beforeSend: function(){
			$('html, body').css('cursor', 'wait');
		},
		success: function(msg){
			$('#td_panier').html(msg);
			$('html, body').css('cursor', 'default');
		},
		error: function(){
			$('html, body').css('cursor', 'default');
		}
	});
}

/**
 * Supprime un article du panier
 * @param id_ligne
 */
function supprArtPanier(id_ligne, nom){
	if(confirmSuppr(nom)){
		$.ajax({
			url: houra_url+'pan/pan_ajax.php',
			data: {action_panier: 'suppr_ligne_panier', id_ligne: id_ligne},
			success: function(msg){
				$('#td_panier').html(msg);
			}
		});
	}
}

/**
 * Demande une confirmation de suppression d'article
 * @returns boolean
 */
function confirmSuppr(nom){
	return (confirm("Ce produit va être supprimé de votre panier :\n" + nom + "\nVoulez-vous continuer?"));
}

/**********************************************
 * Fonctions du calendrier des creneaux home  *
 **********************************************/

function PlusDate(){
	no_jour = parseInt(no_jour)+2;
	MoveDate();
}
function MoinsDate(){
	no_jour = parseInt(no_jour)-2;
	MoveDate();
}

function MoveDate(){
	for (var u=1; u<=nb_jour; u++){
		if(u >=no_jour && u<parseInt(no_jour)+2){
			$('#jour_'+u).css('display', '');
			$('#am_'+u).css('display', '');
			$('#pm_'+u).css('display', '');
		}else{
			$('#jour_'+u).css('display', 'none');
			$('#am_'+u).css('display', 'none');
			$('#pm_'+u).css('display', 'none');
		}
	}
	
	if (no_jour > 2){
		$('#img_arr_left').css('display', '');
	}else{
		$('#img_arr_left').css('display', 'none');
	}
		
	if (no_jour > parseInt(nb_jour)-2){
		$('#img_arr_right').css('display', 'none');
	}else{
		$('#img_arr_right').css('display', '');
	}
}


function pcto_dis() {if (document.createElement) { var cto_dis_im=document.createElement('IFRAME');if (cto_dis_im) { cto_dis_im.width = '1px';cto_dis_im.height = '1px';cto_dis_im.style.display='none'; cto_dis_im.src='http://dis.criteo.com/dis/dis.aspx?p=3412&c=2&cb='+Math.floor(Math.random()*99999999999); var cto_dis_doc=document.getElementById('pcto_dis_div');if (cto_dis_doc!==null && cto_dis_doc.appendChild){ cto_dis_doc.appendChild(cto_dis_im);}}}}

/**************************************************************************************************************************************/
/**************************************************************************************************************************************/
/**************************************************************************************************************************************/
/**************************************************************************************************************************************/

/**********************************************
 * Fonction de DEBUG 
 **********************************************/
function print_r(obj) {
	win_print_r = window.open('about:blank', 'win_print_r');
	win_print_r.document.write('<html><body>');
	r_print_r(obj, win_print_r);
	win_print_r.document.write('</body></html>');
}

function r_print_r(theObj, win_print_r) {
	if(theObj.constructor == Array ||
			theObj.constructor == Object){
		if (win_print_r == null)
			win_print_r = window.open('about:blank', 'win_print_r');
	}
	for(var p in theObj){
		if(theObj[p].constructor == Array||
				theObj[p].constructor == Object){
			win_print_r.document.write("<li>["+p+"] =>"+typeof(theObj)+"</li>");
			win_print_r.document.write("<ul>");
			r_print_r(theObj[p], win_print_r);
			win_print_r.document.write("</ul>");
		} else {
			win_print_r.document.write("<li>["+p+"] =>"+theObj[p]+"</li>");
		}
	}
	win_print_r.document.write("</ul>");
}

