function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function ImgRestore(doc) { //v3.0
			var titreList = doc.getElementsByTagName("img");
			titreList[0].width="135";
			titreList[0].height="80";
			titreList[0].style.margin="0px 0 0 0px";
}

function swapImage(doc) { //v3.0
			var titreList = doc.getElementsByTagName("img");
			titreList[0].width="145";
			titreList[0].height="86";
			titreList[0].style.margin="0px 0 0 -5px";
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function req_formcom(f)
	{
/*@cc_on @*/
	var xhr_object = null;
	
	if(window.XMLHttpRequest) // Firefox
		xhr_object = new XMLHttpRequest();
	else if(window.ActiveXObject) // Internet Explorer
   		xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
	else
		{ // XMLHttpRequest non supporté par le navigateur
   		alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
   		return;
		}
	xhr_object.open("POST", "insert_com.php", true);
	
	xhr_object.onreadystatechange = function()
		{
		if(xhr_object.readyState == 4)
			{
	       eval(xhr_object.responseText);
			}
		}

	xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	var data = "pseudo="+f.elements["q3r1"].value+"&email="+f.elements["q4r1"].value+"&com="+f.elements["q5r1"].value+"&type="+f.elements["type"].value+"&idtop="+f.elements["idtop"].value+"&note1="+$('current-rating-1').style.width+"&note2="+$('current-rating-2').style.width+"&note3="+$('current-rating-3').style.width+"&note4="+$('current-rating-4').style.width;

   	xhr_object.send(data);
    return false;

	}


function req_formcht(f)
	{
/*@cc_on @*/
	var xhr_object = null;

	{
	if(window.XMLHttpRequest) // Firefox
		xhr_object = new XMLHttpRequest();
	else if(window.ActiveXObject) // Internet Explorer
   		xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
	else
		{ // XMLHttpRequest non supporté par le navigateur
   		alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
   		return;
		}
	xhr_object.open("POST", "insert_cgt.php", true);
	
	xhr_object.onreadystatechange = function()
		{
		if(xhr_object.readyState == 4)
			{
	       eval(xhr_object.responseText);
			}
		}

	xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	var data = "pseudo="+f.elements["qcgtr1"].value+"&email="+f.elements["qcgtr2"].value+"&com="+f.elements["qcgtr3"].value+"&idtop="+f.elements["idtop"].value;

   	xhr_object.send(data);
    return false;
	}
	}

function req_formresa(f)
	{
/*@cc_on @*/
	var xhr_object = null;

	{
	if(window.XMLHttpRequest) // Firefox
		xhr_object = new XMLHttpRequest();
	else if(window.ActiveXObject) // Internet Explorer
   		xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
	else
		{ // XMLHttpRequest non supporté par le navigateur
   		alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
   		return;
		}
	xhr_object.open("POST", "insert_resa.php", true);
	
	xhr_object.onreadystatechange = function()
		{
		if(xhr_object.readyState == 4)
			{
	       eval(xhr_object.responseText);
			}
		}

	xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	var data = "pseudo="+f.elements["qresar1"].value+"&email="+f.elements["qresar2"].value+"&com="+f.elements["qresar3"].value+"&idtop="+f.elements["idtop"].value+"&resa="+f.elements["resa"].value;

   	xhr_object.send(data);
    return false;
	}
	}


function gestionFiltrage(type,fonct,elment,statut,cat,value)
//fonct =1 triage  value=0 trier par nom
//                 value=1 trier par note

//fonct =2 Nb résultat  value=Nb résultat

//fonct =3 New page value=NB page
//fonct =4 affichage carte / liste
//                       1      0

//fonct =0 filtrage
//statut =0 retire filtrage
//statut =1 ajoute filtrage

//cat =0 filtrage ville
//cat =1 filtrage categorie

//Value = valeur filtrage
//

{
switch(fonct) 	
	{
	case 0:
	var div_filtrage="liste_filtre_"+cat;


	if (statut==0) //retire filtrage
		{
		}
	else
		{
		var node0 = $(div_filtrage).select('ul');
		var node1 = node0[0].select('li');
	
		var presence_filtre=0;
		
		for (i=0;i<$(node1).length;i++)
			{
			if ($(node1)[i].id==value)
			presence_filtre=1;
			}
		}

	break;
	
	case 4:
		if (value==1)
			{
			$("carte").style.display="block";
		     map = new GMap(document.getElementById("carte_resto"));
			 
		      map.addControl(new GSmallMapControl());
		      map.addControl(new GMapTypeControl());
        map.setCenter(new GLatLng(47.38, 0.68), 9);

		     // getMarkers();
		      
		      GEvent.addListener(map, "click", function(overlay, point) {
			      if (overlay){	// marker clicked
				      overlay.openInfoWindowHtml(overlay.infowindow);	// open InfoWindow
			      } else if (point) {	// background clicked
		      }
	      });
			}
     	else
			{
			$("carte").style.display="none";
			}
	break;
			
	}
	
	
if ((presence_filtre==0)||(statut==0)||(fonct!=0))
	{
	new Ajax.Request(
	'recup_bdd.php',
		{
		method: 'post',
		postBody: 'type='+type+'&fonct='+fonct+'&statut='+statut+'&cat='+cat+'&value='+value,
		onComplete: function (reponse) 
			{
			if (!reponse.responseXML)
				{
				$('liste').update(reponse.responseText);
				}
			else
				{
				$('liste').update("");
				if (markers.length){
					for (var i = 0; i < markers.length; i++) { // cycle thru locations
					map.removeOverlay(markers[i]);
					}
				}
						var xmlDoc = reponse.responseXML;
					locations = xmlDoc.documentElement.getElementsByTagName("location");
					
					if (locations.length){
						$('nb_result2').update(locations.length);
						for (var i = 0; i < locations.length; i++) { // cycle thru locations
							markers[i] = new GMarker(new GLatLng(locations[i].getAttribute("lat"),locations[i].getAttribute("lng")));
							// Add attributes to the marker so we can poll them later.
							// When clicked, an overlay will have these properties.
							markers[i].infowindow = "<div class='ct_wrap'><h3>"+unescape(locations[i].getAttribute("name"))+"</h3>";
							markers[i].infowindow += "<p class='adr'><span>"+(locations[i].getAttribute("tel"))+"</span><br />"+unescape(locations[i].getAttribute("adr"))+"<br/>"+unescape(locations[i].getAttribute("ville"))+"</p>";


							largeur=calcul_note(locations[i].getAttribute("note"));

							markers[i].infowindow += "<div class='note'><ul class='star-rating small-star'><li class='current-rating ' style='width:"+largeur+"%;'></li></ul>";
					
							markers[i].infowindow += "<p>"+locations[i].getAttribute("nb_avis")+" avis</p></div>";
							
							markers[i].infowindow += "<p class='spec'>"+unescape(locations[i].getAttribute("spec"))+"</p></div>";

							// Useful things to store on a marker (Not needed for this example, could be removed)
							// Tells you what index in the markers[] array an overlay is
							markers[i].markerindex = i;
							// Store the location_id of the location the marker represents.
							// Very useful to know the true id of a marker, you could then make
							// AJAX calls to the database to update the information if you had it's location_id
							markers[i].db_id = locations[i].getAttribute("location_id");
							
							map.addOverlay(markers[i]);
							
						}}
					
				}
			
			switch(fonct) 	
				{
				case 0:
				if (statut ==0)	
					{
					var aremove=$(div_filtrage).select('[id="'+$(value)+'"]');
					var elt=aremove[0];
					elt.remove();
					}
				else
					{
						
					var newLi = new Element('li');
		            newLi.id=value;
					newLi.update(elment);
					newImg = new Element('img');
					newImg.src = 'images/validate_ko.png';
					var newA = new Element('a');
		            newA.href='javascript:gestionFiltrage("'+type+'",0,"",0,'+cat+','+value+')';
					newA.insert ({'bottom'  :  newImg } );
					newLi.insert ({'bottom'  :  newA } );
					$(node0)[0].insert ({'bottom'  :  newLi } );
					}
				break;
				case 1:
					var elt=elment.up(1);
					var lesLi=elt.select('li');
					for (var i=0;i<lesLi.length;i++)
						{

						if 	(lesLi[i].select('a').length!=0)
							var contenu=lesLi[i].select('a')[0].innerHTML;
						else
							var contenu=lesLi[i].innerHTML;
						if (i==value)	
							lesLi[i].update(contenu);
						else
							lesLi[i].update("<a href='#' onclick='gestionFiltrage(\""+type+"\",1,this,-1,-1,"+i+")'>"+contenu+"</a>");
						}
				break;
				case 4:
					var elt=elment.up(1);
					var lesLi=elt.select('li');
					
					for (var i=0;i<lesLi.length;i++)
						{
						if 	(lesLi[i].select('a').length!=0)
							var contenu=lesLi[i].select('a')[0].innerHTML;
						else
							var contenu=lesLi[i].innerHTML;
						if (i==value)	
							lesLi[i].update(contenu);
						else
							lesLi[i].update("<a href='#' onclick='gestionFiltrage(\""+type+"\",4,this,-1,-1,"+i+")'>"+contenu+"</a>");
						}
				break;
				}
			}
		}
	);
	}
}

function gestionCom(type,id,value)
{
new Ajax.Request(
	'recup_com.php',
		{
		method: 'post',
		postBody: 'type='+type+'&id='+id+'&value='+value,
		onComplete: function (reponse) 
			{
			$('com').update(reponse.responseText);
			}
		}
	);
}



function calcul_note(note)
	{
		not=Math.round(note, 2);
		flo=not-Math.floor(not);
		if (flo<5)
			return Math.floor(not)*20;
		else
			return (Math.floor(not)+0.5)*20;
		}


function deploy()
	{
		$('panel').morph('height:160px', {duration:1.5});
		//$('open').update('fermer');
		$('open').href='javascript:undeploy()';

	}
	
function undeploy()
	{
		$('panel').morph('height:0px', {duration:1.5});
//		$('open').update('ouvrir');
		$('open').href='javascript:deploy()';
	}
	
function gestionconnexion()
{
var nom = $('qlogin').value;
var pwd = $('qmdp').value;
new Ajax.Updater(	'toppanel',	'connexion_bdd.php',
				 {		method: 'post',		postBody: 'nom='+nom+'&pwd='+pwd	,evalScripts:true	}
				 );
}

function deconnexion()

{
new Ajax.Updater(	'toppanel',	'deconnexion_bdd.php',
				 {	method: 'post'	,evalScripts:true	}
				 );
}


function sup_file(id,type,id_etab)

{
	var xhr_object = null;

	{
	if(window.XMLHttpRequest) // Firefox
		xhr_object = new XMLHttpRequest();
	else if(window.ActiveXObject) // Internet Explorer
   		xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
	else
		{ // XMLHttpRequest non supporté par le navigateur
   		alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
   		return;
		}
	xhr_object.open("POST", "sup_item.php", true);
	
	xhr_object.onreadystatechange = function()
		{
		if(xhr_object.readyState == 4)
			{
	       eval(xhr_object.responseText);
			}
		}

	xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	var data = "id="+id+"&type="+type+"&id_etab="+id_etab;

   	xhr_object.send(data);
    return false;
	}

}


