var idZoneC=-1;
var nbZone=17;
var maxHeight=250;
var opened=-99;

var n= navigator.userAgent.toLowerCase();
var db= (document.comptaMode && document.comptaMode.tolowerCase() != "backcompat")?document.documentElement:(document.body||null);
var op= (window.opera && document.getElementById);
var op6= (op && !(db && db.innerHTML));
if (op && !op6)
	document.onmousedown= new Function('e','if (((e= e || window.event).target || e.srcElement).tagName=="IMAGE") return false;');
var ie= this.n.indexOf("msie")!=-1 && document.all && db && !op;
var iemac= ie && n.indexOf("mac")!=-1;
var ie4= ie && !document.getElementById;
var n4= (document.layers && typeof document.classes != "undefined");
var sfr= n.indexOf("safari")>-1;
var n6= !op && !sfr && !ie && document.defaultView && typeof document.defaultView.getComputedStyle != "undefined";
var ce= document.captureEvents && document.releaseEvents;
var px= n4 || op6;
var ie6= false;

var offsetClose= 50;
var offsetLargeur=200;
var offsetHauteur=200;

var PHPSID;



function ValInt(pVal)
{
	return isNaN(r=parseInt(pVal))?0:r;
}

function getLeft(o)
{
	if (o==null) return 0;
	if (n4) return ValInt(o.pageX);
	var y=0;
	while (o)
	{
		y+= ValInt(o.offsetLeft);
		o= o.offsetParent;
	}
	if (iemac) y+= ValInt(db.currentStyle.marginLeft)+ValInt(db.currentStyle.paddingLeft);
	return y;
}

function getTop(o)
{
	if (o==null) return 0;
	if (n4) return ValInt(o.pageY);
	var y=0;
	while (o)
	{
		y+= ValInt(o.offsetTop);
		o= o.offsetParent;
	}
	if (iemac) y+= ValInt(db.currentStyle.marginTop)+ValInt(db.currentStyle.paddingTop);
	return y;
}

function getWidth(o)
{
	if (o==null) return 0;
	return ValInt(o.width || (n6?window.getComputedStyle(o, "").getPropertyValue("width") : 0));
}

function getHeight(o)
{
	if (o==null) return 0;
	return ValInt(o.height || (n6?window.getComputedStyle(o, "").getPropertyValue("height") : 0));
}

function doClic(e)
{
	if (opened==-99) return;
	//hideAll();
	//return;
	 if (opened<0)
	 	o= document.getElementById('zoneResultats');
	 else
	 	o= document.getElementById('zoneRes_'+idZoneC);
	
	x = (navigator.appName.substring(0,3) == "Net") ? e.pageX : event.x+document.documentElement.scrollLeft;
	y = (navigator.appName.substring(0,3) == "Net") ? e.pageY : event.y+document.documentElement.scrollTop;
	
	//alert(x+" - "+ y + " /  " + getLeft(o)+" - "+getTop(o)+ " Dim= "+getWidth(o)+' - '+getHeight(o));
	//if (x<getLeft(o)-offsetClose|| x>getLeft(o)+offsetLargeur+offsetClose || y<getTop(o)-offsetClose || y>getTop(o)+offsetHauteur+offsetClose) hideAll();
}



function hideAll()
{
	i=1;
	while (document.getElementById('zoneRes_'+i)!=undefined)
	{
		document.getElementById('zoneRes_'+i).style.visibility = 'hidden';
		i++;
	}
	opened=-99;
}

function afficherResultats(obj) {
	//alert("Result");
     // Construction des noeuds
     var o;
	 var tabResult = obj.responseXML.getElementsByTagName('resultat');
	 //alert('result '+tabResult);
	 //alert(idZoneC);
	 if (idZoneC<0)
	 	o= document.getElementById('zoneResultats');
	 else
	 	o= document.getElementById('zoneRes_'+idZoneC);
	 opened=idZoneC;
     o.innerHTML = '';
     if (tabResult.length > 0) {
          // On définit la hauteur de la liste en fonction du nombre de rsultats et de la hauteur de ligne
          var hauteur = Math.min(tabResult.length * 17,maxHeight) + 5; 
          with(o.style) {
               visibility = 'visible';
               height = hauteur + 'px';
          };
          for (var i = 0; i < tabResult.length; i++) {
               resultat = tabResult.item(i); 
               var egt = document.createElement('li');
               var lnk = document.createElement('a');
               var texte = document.createTextNode(resultat.getAttribute('titre'));
               lnk.appendChild(texte);
               lnk.setAttribute('href', resultat.getAttribute('url'));
			   if (resultat.getAttribute('info')!='')
			   {
               	lnk.setAttribute('title', resultat.getAttribute('info'));
			   	lnk.setAttribute('alt', resultat.getAttribute('info'));
			   }
               egt.appendChild(lnk);
               o.appendChild(egt);
			   
          }
     }
     else {
          o.style.visibility = 'hidden';
     }
}

function loadData() {
	 
	 hideAll();
	 idZoneC=-1;
     // Création de l'objet
     var XHR = new XHRConnection();
     XHR.appendData("reg_com_nom", document.getElementById('reg_com_nom').value);
     // On soumet la requête
     // Signification des paramètres:               
     //      + On indique à l'objet qu'il faut appeler le fichier search.php
     //      + On utilise la méthode POST, adaptée l'envoi d'information
     //      + On indique quelle fonction appeler lorsque l'opération a été effectuée
	
     XHR.sendAndLoad("municipal_search.php?PHPSID="+PHPSID, "POST", afficherResultats);
}

function loadIndex(valIndex,idZone) {
	hideAll();
	idZoneC= idZone;
     // Création de l'objet
     var XHR = new XHRConnection();
     XHR.appendData("reg_com_lettre", valIndex);
	 //alert(valIndex);
     // On soumet la requête
     // Signification des paramètres:               
     //      + On indique à l'objet qu'il faut appeler le fichier search.php
     //      + On utilise la méthode POST, adaptée l'envoi d'information
     //      + On indique quelle fonction appeler lorsque l'opération a été effectuée
     XHR.sendAndLoad("municipal_search.php?PHPSID="+PHPSID, "POST", afficherResultats);
}


function setPhpSid(sessionID)
{
		PHPSID=sessionID;
}


if (navigator.appName.substring(0,3) == "Net") document.captureEvents(Event.MOUSEMOVE);
document.onmouseup = doClic;
