var Coordy;
var opacity;
var timerfade;

function popupdiv(elem,id){

	Coordy = elem.offsetParent.offsetParent.offsetParent.offsetParent.offsetParent.offsetTop;

		
		var argajax = "action=ajax&idcom=" + id;

				$.ajax({   
				type: "GET",   
				url: "Liste-marchands.php",  
				data: argajax,   
				success: function(msg){ show_popup(msg);   } });
	

	return false;
}

function show_popup(reponse)
{

	$("#souspop").html(reponse);
	
	
	stylepop = document.getElementById("souspop").style;
	stylepop.top = (Coordy+225) + "px";
	stylepop.left ="100px";
	
	
   stylepop.visibility='visible';
   stylepop.display='block';
   opacity = 20;

   setopacity(stylepop,opacity);
      
  setTimeout("fadein()",50);
   
}

function closediv()
{
   setTimeout("fadeout()",50);
	return false;
}

function noident()
{
	if (confirm('Merci de vous inscrire et de vous enregistrer afin de bénéficier de cette fonctionnalité.\nVoulez vous vous inscrire maintenant ?'))
	{
	
		location.href="Inscription.php";
	}
	return false;
}


function Sendtofriend(elem,idcom)
{

	Coordy = elem.offsetParent.offsetParent.offsetParent.offsetParent.offsetParent.offsetTop;
		
	var argajax = "action=ajaxenvoyerami&id=" + idcom;

				$.ajax({   
				type: "GET",   
				url: "Liste-marchands.php",  
				data: argajax,   
				success: function(msg){ show_Sendtofriend(msg);   } });
	

	return false;

}

function show_Sendtofriend(reponse)
{

	$('#souspop').html(reponse);
	
	
	stylepop = document.getElementById("souspop").style;
	stylepop.top = (Coordy+225) + "px";
	stylepop.left ="300px";
	
	opacity = 0;
   setopacity(stylepop,opacity);
   stylepop.visibility='visible';
   stylepop.display='block';
      
   setTimeout("fadein()",50);

}

function recupinfoenvoiami()
{
	var form = document.envoiami;
	
	if (form.nomenvoyeur.value == "" || form.nomenvoi.value == "" || form.emailenvoi.value == "" || form.idcom.value == "" ) alert("Gracias por rellenar todas las casillas");
	else
	{
		var verifmail     = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]{2,}[.][a-zA-Z]{2,4}$/;
		if (!verifmail.exec(form.emailenvoi.value)) alert("La dirección electrónica de su amigo no es válida");
		else
		{
			//no error we continue
			
			var argajax = "action=ajaxrecupenvoyerami&idcom=" + form.idcom.value + "&nomenvoi=" + form.nomenvoi.value  + "&nomenvoyeur=" + form.nomenvoyeur.value + "&emailenvoi=" + form.emailenvoi.value ;

				$.ajax({   
				type: "GET",   
				url: "Liste-marchands.php",  
				data: argajax,   
				success: function(msg){ $("#formenvoi").html(msg);   } });

		}
	}
	
	return false;

}

function fadein()
	{
		 stylepop = document.getElementById("souspop").style;
		 
		opacity = opacity + 10;
		setopacity(stylepop,opacity);
		 
		 if (opacity<100) setTimeout("fadein()",50);
	}
	
	function fadeout()
	{
		 stylepop = document.getElementById("souspop").style;
		 
		opacity = opacity - 10;
		setopacity(stylepop,opacity);
		 
		 if (opacity>0) setTimeout("fadeout()",50);
		 else
		 {
			 stylepop.visibility='hidden';
			 stylepop.display='none';
		 }
	}
	
	
	function setopacity(stylepop,opacity)
	{
		stylepop.opacity = (opacity/100); 
		stylepop.MozOpacity = (opacity / 100); 
		stylepop.KhtmlOpacity = (opacity / 100); 
		stylepop.filter = "alpha(opacity=" + opacity + ")"; 
	}
	
	
function submit_form()
{
	var formrecherche = document.formrecherche;
	var val = formrecherche.rech.value;

	if (rech==false || val =="") alert("Gracias por coger una búsqueda");
	else	formrecherche.submit();
	
	return false;
}

var rech=autorech=arrow=divshow=false;
var count = timeauto = inserthtml = 0;
var pos=0;

function settimediv(type)
{
	if (type==1) 
	{
		clearTimeout(timeauto);
		timeauto = setTimeout("Timeautocomp()",2500);
	}
	else	clearTimeout(timeauto);
}

function Timeautocomp()
{
	if(divshow)
	{
		settimediv(0);
		HideShow(0);
	}
}

function HideShow(type)
{
	if (type==1) 
	{
		
		$("#rapidrech").html(inserthtml);
		styledivrech.display="block";
		styledivrech.visibility="visible";	
		divshow = true;

		settimediv(1);
	}
	else	
	{
		$("#rapidrech").html("");
		styledivrech.display="none";
		styledivrech.visibility="hidden";
		divshow = false;
	}
}

function Gotolink(pos)
{
	var suggList=document.getElementById("rapidrech").getElementsByTagName("a");
  	var nbsugg =suggList.length;

	for(c=0;c<nbsugg;c++)
	{
		if(c==pos-1) url = suggList[c].href;
	}

	document.location=url;
}

function cleanstylepos()
{
	pos=0;
	var suggList=document.getElementById("rapidrech").getElementsByTagName("a");
  	var nbsugg =suggList.length;

	for(c=0;c<nbsugg;c++)	suggList[c].className="";
				
	settimediv(1);
}

function buildlink(idcom,urldoc)
{
	return "<a href='" + urldoc + "?id=" + idcom + "' onmouseover='cleanstylepos()'>" + tabinfo[idcom]["nom"] + " &nbsp;" + "<span style='font-size:9px;color:#FF6E00'>(" + tabinfo[idcom]['remu'] + " " + tabinfo[idcom]['type'] + ")</span>" + "</a>";
}

$(document).ready(function(){
	
	
    $('#recherchecom').click(function(){

		if (!rech)
		{	
			this.value='';
			rech=true;
		}
	});
	
	$('#submitrech').click(function(){

		submit_form();
			
		return false;
	});

	
	$("#recherchecom").keydown( function(e) {
	
		if (e.which == 13) 
		{
			if (divshow) 
			{
				if (pos>0)	Gotolink(pos);
				else	submit_form();
			}
			else	submit_form();
		}

		if (e.which==40 || e.which==38)
		{
			arrow=true;
			settimediv(1);

			if (count>0)
			{
				if (!divshow) HideShow(1);
				
			 	var suggList=document.getElementById("rapidrech").getElementsByTagName("a");
  			 	var nbsugg =suggList.length;
				
				if (e.which==40) pos = (pos<nbsugg)? pos=pos +1:pos=1; 
				if (e.which==38) pos = (pos>1)? pos=pos -1:pos=nbsugg; 
				
				for(c=0;c<nbsugg;c++)
				{
					suggList[c].className="";
				}
				
				if (pos>0) suggList[pos-1].className="selected";

				//alert(nbsugg + " " + pos);
			}
		}	else	arrow=false;
	});
	
	$("#recherchecom").keypress( function(e) {

		if (scanTouche(e,"rech"))	
		{
			autorech=true;
			retour = true; 
		}
		else	
		{
			retour = false;
			autorech=false;
		}
	
		return	retour; 
	} );
	
	$("#recherchecom").keyup( function(e) { 
	
		if (!arrow)
		{
			styledivrech = document.getElementById("rapidrech").style;
			var formrecherche = document.formrecherche;
			var val = formrecherche.rech.value;
			count = 0;
			
			tabpos = findPos(this,"mailo_base-centre");
			
			toppos  = tabpos[1];
			leftpos = tabpos[0];
			
			styledivrech.top = toppos + 19 + "px";
			styledivrech.left = leftpos + "px";
	
			if (autorech && val !="")
			{
				var pattern = "^"+ val + "(.*)";
				regexp = new RegExp(pattern, "i");
				
				var tabtrouvenom = new Array();
				var tabtrouveid = new Array();
				
				inserthtml = "";
				
				for(idcom in tabinfo)
				{
					if (regexp.test(tabinfo[idcom]["nom"]) && count <5)	
					{
						//urldoc = "Annuaire_marchands.php";
						urldoc = "Liste-marchands.php";

						inserthtml += buildlink(idcom,urldoc);
						count++;
					}
				}
				
				if (count>0)	HideShow(1);
				
			}

			if (count==0)HideShow(0);

		}
		
	} );

	$("#formrecherche").keydown( function(e) {
	
		if(e.which==13) return false;
	} );

		
});//document ready

