//+++++++++++++++++++++++++++//
// DÉCLARATION DES VARIABLES //
//+++++++++++++++++++++++++++//
var tab = new Array();
var objet;
var where_is_my_mouse = "out";
var IE=0;
var NS=0;
var c=navigator.appName;
var version=navigator.appVersion;
var x=c.indexOf('Internet');
var y=c.indexOf('Netscape');
if(x != -1) IE=1;
if(y != -1) NS=1;
var _form;
var _objectInputTextVille;
var _ville;
var localisationOk = false;
var resetAll;
var check_all;
var multiple_villes = 0;
var flag_handler_ville = true;
var _indice = 0;
var _Div;
var _nbspan = 0;
var _direction;
var _focusedSpan = null;
var t;
var _isHighLight = false;
var _localisationIsFocused=false;
var _geoLocalisationIsFocused=false;
var _colocationLocalisationIsFocused=false;
var _tmp;
var _userChaine = new String();
var isWorking = false;
var objectRequest;
var keycode;
var _iewait = 'go';
var _shift;
var _event;
var _userChaine;
var expVilleAjax = /^[\w \' -]+\(\d{5}\)$/;
var expDepAjax = /^[\w \' -]+\(\d{2}\)$/;
var cp = /^\d{1,5}$/;
var ret1;
var ret2;
var ret3;
var _posLeft;
var _posTop;
var user_is_writting = 0; 
var trigger;
var idPays = 'FR';
var timer;
var old_menu;
var _MtransactionIsOver=false;
var _MtypeBiensIsOver=false;
var _MsurfaceIsOver=false;
var _MpiecesIsOver=false;
var _MLocalisationIsOver=false;
var _McriteresIsOver=false;
var trackerGA;
var _choices = new Array('Mtransaction','MtypeBiens','Msurface','Mpieces','MLocalisation','Mcriteres');

//+++++++++++++++++++++++++++++++++++++++++++++++++++++//
// INTERCEPTION DE L'ÉVENEMENT KEYUP (GÉOLOCALISATION) //
//+++++++++++++++++++++++++++++++++++++++++++++++++++++//
document.onkeyup=keyUpHandler;

//*************************************************************************//
// FONCTION GÉRANT L'AJAX LORSQU'UNE TOUCHE EST RELÂCHÉE (GÉOLOCALISATION) //
//*************************************************************************//
function keyUpHandler(e){	
	// On ignore l'évènement et on efface l'éventuelle liste de villes si nous sommes en dehors d'un moteur de recherche	
	if (!objet || objet == '') {removeDivVille();return;}
	
	// On ignore également la touche si le champ "localisation_search" a l'indication
	if (objet != 'geolocalisation' && objet != 'localisation_colocation' && document.getElementById(moteur_recherche).localisation_search.value == 'Ajoutez une localisation ici' && document.getElementById(moteur_recherche).localisation.value == 'Saisir une localisation ci-dessus') {return;}
	
	// Attribution des touches spéciales
	var ENT = 13;
	var LEFT = 37;
	var RIGHT = 39;
	var UP = 38;
	var DOWN = 40;
	var BACKSPACE = 8;
	var F5 = 116;
	var SHIFT = 16;
	var SUPPR = 46;
	var TAB = 9;
	var QUOTE = 222;
	var VIRG = 188;	
			
	// Récupération de la touche frappée	
	if (window.event){
		_event = window.event;
		keycode = window.event.keyCode;
	}else if (e){
		_event = e;
		keycode = e.which;
	}else{
		return;
	}
	// Si ce n'est pas une des touches spéciales, on applique notre trigger
	if(objet && keycode != ENT && keycode != LEFT && keycode != RIGHT && keycode != UP && keycode != DOWN && keycode != F5 && keycode != SHIFT && keycode != SUPPR && keycode != TAB && keycode != QUOTE && keycode != VIRG){
		_userChaine = document.getElementById(objet).value;			
		if (user_is_writting == 0){
			user_is_writting = 1;
			trigger = setTimeout("user_is_writting = 0; handlerAjax(_userChaine,checkVilles)", 300);
			return;
		}else{		
			clearTimeout(trigger);
			trigger = setTimeout("user_is_writting = 0; handlerAjax(_userChaine,checkVilles)", 300);
			return;
		}
	}
	
	//+++++++++++++++++++++++++++++++++++++//
	// NAVIGATION DANS LA LISTE DES VILLES //
	//+++++++++++++++++++++++++++++++++++++//	
	if(objet && (keycode == UP || keycode == DOWN) && (objet == "localisation_search" || objet == "geolocalisation" || objet == "localisation_colocation")){
		// Vers le haut
		if(keycode == UP && _indice >1){
			_direction = 'up';
			_indice--;
			highlight(_indice);
		}
		// Vers le bas
		if (keycode == DOWN && _indice < tab.length){	
			_direction = 'down';
			_indice++;
			highlight(_indice);
		}			
		return;
	}
		
	//+++++++++++++++++++++++++++++++++++++++++++++++++//
	// CAS D'UNE VALIDATION (ENTER OU VIRGULE) REFUSÉE //
	//+++++++++++++++++++++++++++++++++++++++++++++++++//
	if(keycode == ENT && !_isHighLight && objet){
		if (objet == "localisation_search") _form = eval('document.' + recherche_form);
		if (objet == "localisation_colocation") _form = eval('document.' + recherche_form_Colocation);
		if (objet == "geolocalisation") _form = eval('document.' + recherche_form_Googlemap);
		if (document.getElementById('localisation').value == 'Saisir une localisation ci-dessus'){
			alert('Vous devez choisir au moins une localisation !');
			return;
		}else{
			if(_form.name != 'form_particuliers' && _form.name != 'form_proprietaires' && _form.name != 'form_alertemail'){
				_form.submit();
				return;
			}
		}
	}

	//++++++++++++++++++++++++++++++++++++++++++++++++++//
	// CAS D'UNE VALIDATION (ENTER OU VIRGULE) ACCEPTÉE //
	//++++++++++++++++++++++++++++++++++++++++++++++++++//
	if(keycode == ENT && _isHighLight){
		mDown();
		return;
	}
	
}

//****************************************************//
// FONCTION PEMETTANT LE FILTRAGE D'UNE TOUCHE ENTRÉE //
//****************************************************//
function GetInput(e){
	var keyLabels = new Array('ENTER','LEFT','RIGHT','UP','DOWN','BACKSPACE','F5','SHIFT','SUP','TAB','QUOTE','VIRGULE');
	var keyCodes = new Array(13,37,39,38,40,8,116,16,46,9,222,188);
	var authorized_chars = /[A-Za-z1234567890]/;
	
	if (window.event){
		keycode = window.event.keyCode;
	}else if (e){
		keycode = e.which;
	}else{
		return;
	}
	
	for(var i=0;i<keyLabels.length;i++){
		if(keycode == keyCodes[i]){
			return keyLabels[i];
		}
	}
		
	var char = new String(getChar());
	return char;
}


//**************************************************************************//
// FONCTION GÉRANT LA SUPPRESSION D'UNE PARTIE OU DE TOUTE UNE LOCALISATION //
//**************************************************************************//
function deleteChars(char){
	// Si aucun objet n'est défini, on outre passe la fonction
	if (!document.getElementById(objet)) return;
	
	//suppression possible d'une partie de la ville;
	var range;
	var start;
	var stop;
	
	if(document.getElementById(objet).setSelectionRange){
		start = document.getElementById(objet).selectionStart;
		stop = document.getElementById(objet).selectionEnd;
		range = stop-start;
	}else if(document.getElementById(objet).createTextRange){
    		document.getElementById(objet).createTextRange;
		_t=document.selection.createRange().duplicate();
		if(_t.text.length >0){
    	_t.moveEnd("textedit",1);
    	start=document.getElementById(objet).value.length-_t.text.length;
			range=_t.text.length;
		}
	}
	
	if(char == 'SUP' && !start){
		return;
	}
	
	if(range > 0 && _userChaine.length >0){
		var delta = _userChaine.length - range;		
		tmp = new String();
		for(var i=0;i<delta;i++){
			tmp += _userChaine.charAt(i);			
		}
		_userChaine = tmp;
	}else{
		//appuie simple sur le bouton de suppression
		var l = _userChaine.length;
		var tmp = new String();
		for(var i=0;i<(l-1);i++){
			tmp += _userChaine.charAt(i);
		}
		_userChaine = tmp;
	}
	return _userChaine;
}


//**********************************//
// FONCTION LANCANT LA REQUÊTE AJAX //
//**********************************//
function GoAjax(_userChaine){
	if(_userChaine.length >0){		
		setTimeout("handlerAjax(_userChaine,checkVilles)",100);
	}
}


//******************************************//
// FONCTION PEMETTANT DE CRÉER L'OBJET HTTP //
//******************************************//
function getHTTPObject(){
	var xmlhttp;
	try {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	catch(e){
		xmlhttp = new XMLHttpRequest()
	}
	return xmlhttp;
}
objectRequest = getHTTPObject();


//*******************************************************//
// FONCTION AJAX RÉCUPÉRANT LE LISTING DES LOCALISATIONS //
//*******************************************************//
function handlerAjax(ville,handler){
		// Si aucun objet n'est défini, on outre passe la fonction
	if (!document.getElementById(objet)) return;
	if (_localisationIsFocused || _geoLocalisationIsFocused || _colocationLocalisationIsFocused ) {
		removeDivVille();
		if (objet == 'localisation_search'){
			_objectInputTextVille = 'localisation';	
			var url = '/perl/recherche/ajaxVillesCp.pl?ville=' + ville;
		}
		if (objet == 'localisation_colocation'){
			_objectInputTextVille = 'localisation_colocation';
			var url = '/perl/recherche/ajaxVilles.pl?ville=' + ville;
		}
		if (objet == 'geolocalisation'){
			_objectInputTextVille = 'geolocalisation';		
			var url = '/perl/recherche/ajaxVilles.pl?ville=' + ville;
		}
		if(!isWorking && objectRequest){			
			objectRequest.onreadystatechange = function () {}
			objectRequest.abort();
			objectRequest.open("GET", url, true);
			objectRequest.onreadystatechange = handler;
			objectRequest.send(null);
		}
	}
}

//*************************************************************//
// FONCTION RÉCUPÉRANT LES VILLES ET VÉRIFIANT LEUR PERTINANCE //
//*************************************************************//
function checkVilles(){
	if (_localisationIsFocused || _geoLocalisationIsFocused || _colocationLocalisationIsFocused) {
		if (objectRequest.readyState == 4){
			if (objectRequest.responseText.indexOf('invalid') == -1){
				var result;			
				result = objectRequest.responseText;
				handleResult(result);
				isWorking = false;
			}
		}		
	}
}

//**************************************************************//
// FONCTION PERMETTANT L'AFFICHAGE DES DIFFÉRENTES POSSIBILITÉS //
//**************************************************************//
function handleResult(result){
	// Si aucun objet n'est défini, on outre passe la fonction
	try{
	if (!document.getElementById(objet)) return;
	var pv = /;/;
	var reg=/[0-9]+/g;
	var reg2=/[0-9]{1}/;
	
	if(result == 'false' ){	//Si la cgi ne retourne rien la ville n'est pas valid donc affichage de la pop up		
		
		alert(document.getElementById(objet).value+' ne correspond pas à une ville existante');
		localisationOk=false;
		_isHighLight=false;		
		document.getElementById(objet).value ='';
// 		document.getElementById('div_localisation').style.visibility='hidden';
// 		document.getElementById(objet).value = document.getElementById(objet).value.substring(0,((document.getElementById(objet).value.length)-1));		
		setTimeout("handlerAjax(document.getElementById(objet).value, checkVilles)", 100);
		return;
	}
	
	else if (result == '' || !result) {
		document.getElementById('div_localisation').style.display='hidden';
		localisationOk=false;
		return;			
	}else {
		_indice=1;
		if (objet == 'localisation_search') var _champVille = document.getElementById('localisation_search');
		if (objet == 'localisation_colocation') var _champVille = document.getElementById('localisation_colocation');
		if (objet == 'geolocalisation') var _champVille = document.getElementById('geolocalisation');		
		var result = new String(result);
		multiple_villes =1;		
		_posLeft = fieldOffset(_champVille,'offsetLeft') + "px";
		_posTop = fieldOffset(_champVille,'offsetTop') + 19 + "px";
//		if ((document.all) && (objet == 'geolocalisation')){
//			_posLeft = fieldOffset(_champVille,'offsetLeft') + 30 + "px";
//			_posTop = fieldOffset(_champVille,'offsetTop') + 332 + "px";
//		}
//		if ((document.all) && (objet == 'localisation_search')){
//			_posLeft = fieldOffset(_champVille,'offsetLeft') + 4 + "px";
//			_posTop = fieldOffset(_champVille,'offsetTop') + 18 + "px";
//		}
		if(document.getElementById('_Div_' + _objectInputTextVille)){
			document.removeElement(document.getElementById('_Div_' + _objectInputTextVille));
		}
		_Div = document.createElement('DIV');
		var _SpanTitre = document.createElement('SPAN');
		_SpanTitre.style.fontSize="10px";
		_SpanTitre.style.fontWeight="Bold";
		_SpanTitre.style.color="#C30009";
		_SpanTitre.style.fontFamily="verdana,arial,sans-serif";
		_SpanTitre.style.marginLeft="3";
		_SpanTitre.innerHTML = 'Merci de préciser...';		
		_Div.appendChild(_SpanTitre);
		_Div.id='_Div_' + _objectInputTextVille;		
		_Div.style.position="absolute";
		_Div.style.left = _posLeft;
		_Div.style.top = _posTop;
		_Div.style.borderRight="#a5a8b2 1px solid";
		_Div.style.borderLeft="#a5a8b2 1px solid";
		_Div.style.borderTop="#a5a8b2 1px solid";
		_Div.style.borderBottom="#a5a8b2 1px solid";
		_Div.style.zIndex="99";
		_Div.style.paddingRight="6px";
		_Div.style.paddingLeft="3px";
		_Div.style.paddingTop="3px";
		_Div.style.paddingBottom="3px";
		_Div.style.backgroundColor="white";
		_Div.style.width="auto";
		_Div.style.overflow='auto';
		//Constrution des span villes;
		tab = result.split(";");
		if(tab.length > 20){
			_Div.style.height="275px";
		}
		for(var i=0;i< tab.length;i++){
			//tab[i]=decode(tab[i], '#');
			_nbspan++;
			var d = document.createElement('DIV');
			d.id = 'div_' + i;
			var s = document.createElement('SPAN');
			s.id = 'span_' + i;
			s.style.fontSize="10px";
			s.style.color="black";
			s.style.fontFamily="verdana,arial,sans-serif";
			s.style.marginLeft="3";
			s.innerHTML = tab[i];
			d.onmouseover=mOver;
			d.onmouseout=mOut;
			d.onmousedown = mDown;
			d.appendChild(s);
			_Div.appendChild(d);
			//***********suppression du code postal, ou de la possibilité de passer par un département pour la champ geolocalisation*********
			ret1 = tab[i].match(expVilleAjax);
			ret2 = tab[i].match(cp);
			ret3 = tab[i].match(expDepAjax);
						
			if ((objet == 'localisation_search' || objet == 'localisation_colocation') && (tab[i] == 'PARIS(75)')){
				s.style.display='none';
			}
			//************************************************************************************************
		}
		if (objet == 'localisation_search' && document.getElementById('localLegende')) document.getElementById('localLegende').style.visibility='hidden';
		document.body.appendChild(_Div);
		highlight(_indice);
		if (objet == 'localisation_search') _focusedSpan = document.getElementById('_Div_localisation').childNodes[1].childNodes[0];
		if (objet == 'localisation_colocation') _focusedSpan = document.getElementById('_Div_localisation_colocation').childNodes[1].childNodes[0];
		if (objet == 'geolocalisation') _focusedSpan = document.getElementById('_Div_geolocalisation').childNodes[1].childNodes[0];		
		_Div.style.visibility='visible';
		_isHighLight = true;
		document.getElementById('div_localisation').style.visibility='visible';
		flag_handler_ville = false;
		localisationOk = false;
	}
	}catch (err) {}
}

//**********************************************************//
// FONCTION GÉRANT LE SURLIGNAGE D'UNE VILLE PARMI LA LISTE //
//**********************************************************//
function highlight(indice){
	// Si aucun objet n'est défini, on outre passe la fonction
	if (!document.getElementById(objet)) return;
	
	if (objet == "localisation_search") var div = "_Div_localisation";
	if (objet == "localisation_colocation") var div = "_Div_localisation_colocation";
	if (objet == "geolocalisation") var div = "_Div_geolocalisation";
	
	var s=document.getElementById(div).childNodes[indice];
	_focusedSpan = s.childNodes[0];
	setStyle(s,'mouseover');
	
	if(_direction == 'down' && document.getElementById(div).childNodes[indice-1]){
		var d=document.getElementById(div).childNodes[indice-1];
		setStyle(d,'mouseout');		
	}else if(_direction == 'up' && document.getElementById(div).childNodes[indice+1]){
		var d=document.getElementById(div).childNodes[indice+1];
		setStyle(d,'mouseout');		
	}
}


//**********************************************************//
// FONCTION GÉRANT LA POSITION DE LA LISTE DE LOCALISATIONS //
//**********************************************************//
function fieldOffset(field,attr){
	var val=0;
	while(field){
		val+=field[attr]; 
		field=field.offsetParent;
	}
	return val;
}


//*********************************************************************//
// FONCTION GÉRANT LE PASSAGE DE SOURIS DANS LA LISTE DE LOCALISATIONS //
//*********************************************************************//
var mOver=function(){
	setStyle(this,'mouseover');
	_focusedSpan=this.childNodes[0];
}


//********************************************************************//
// FONCTION GÉRANT LA SORTIE DE SOURIS DANS LA LISTE DE LOCALISATIONS //
//********************************************************************//
var mOut=function(){
	setStyle(this,'mouseout');
}


//*******************************************************************//
// FONCTION GÉRANT LE CLICK DE SOURIS DANS LA LISTE DE LOCALISATIONS //
//*******************************************************************//
var mDown=function(){
	// Si aucun objet n'est défini, on outre passe la fonction
	if (!document.getElementById(objet) || !document.getElementById("_Div_" + _objectInputTextVille)) return;
	switch (objet){
		//++++++++++++++//
		// LOCALISATION //
		//++++++++++++++//
		case "localisation_search":
			var t;
			var ret;
			if(_focusedSpan){
				t=_focusedSpan;
			}else{
				t=this.childNodes[0];
			}
			if (document.getElementById(moteur_recherche).localisation.value == "Saisir une localisation ci-dessus") document.getElementById(moteur_recherche).localisation.value = "";
			// On reporte la localisation choisie dans le champ "Situé à" si et seulement si elle n'est pas déjà sélectionnée
			if (!IsAlreadySelected(t.innerHTML)) document.getElementById(moteur_recherche).localisation.value = t.innerHTML + ', ' + document.getElementById(moteur_recherche).localisation.value;	
			
			// Vidage de l'élément de recherche
			//document.getElementById(moteur_recherche).localisation_search.value = "Ajoutez une localisation ici";
			document.getElementById(moteur_recherche).localisation_search.value = '';	
			document.getElementById(moteur_recherche).blur();
					
			document.getElementById('_Div_' + _objectInputTextVille).style.visibility = 'hidden';
			document.body.removeChild(document.getElementById('_Div_' + _objectInputTextVille));
			localisationOk = true;
			if (document.getElementById('localLegende')) document.getElementById('localLegende').style.visibility='visible';
			_userChaine = '';
			_isHighLight = false;
			_focusedSpan = null;
			// On onlève le focus du champ
			document.getElementById(moteur_recherche).localisation_search.value='';
			document.getElementById(moteur_recherche).localisation_search.blur();

			if(document.getElementById('blocNomAgence')){
				if(document.getElementById('blocNomAgence').style.display=='none'){
					document.getElementById('blocNomAgence').style.display='block';
				}
			}
			break;

		//++++++++++++++//
		// COLOCATION //
		//++++++++++++++//
		case "localisation_colocation":
			var t;
			var ret;
			if(_focusedSpan){
				t=_focusedSpan;
			}else{
				t=this.childNodes[0];
			}
			document.getElementById('localisation_colocation').value = document.recherche_Colocation.colocation_localisation_hidden.value = t.innerHTML;
			//alert("GEOLOC :"+document.getElementById('geolocalisation').value);
			//alert("GEOLOC_HIDDEN :"+document.recherche_Googlemap.geolocalisation_hidden.value);
			document.getElementById('_Div_' + _objectInputTextVille).style.visibility = 'hidden';
			document.body.removeChild(document.getElementById('_Div_' + _objectInputTextVille));
			localisationOk = true;			
			_userChaine = '';
			_isHighLight = false;
			_focusedSpan = null;
			document.getElementById('recherche_Colocation').submit();
			break;

		//+++++++++++++++++//
		// GÉOLOCALISATION //
		//+++++++++++++++++//		
		case "geolocalisation":	
			var t;
			var ret;
			if(_focusedSpan){
				t=_focusedSpan;
			}else{
				t=this.childNodes[0];
			}
			document.getElementById('geolocalisation').value = document.recherche_Googlemap.geolocalisation_hidden.value = t.innerHTML;			
			//alert("GEOLOC :"+document.getElementById('geolocalisation').value);
			//alert("GEOLOC_HIDDEN :"+document.recherche_Googlemap.geolocalisation_hidden.value);
			document.getElementById('_Div_' + _objectInputTextVille).style.visibility = 'hidden';
			document.body.removeChild(document.getElementById('_Div_' + _objectInputTextVille));
			localisationOk = true;			
			_userChaine = '';
			_isHighLight = false;
			_focusedSpan = null;
			document.getElementById('recherche_Googlemap').submit();
			break;		
	}
}


//*************************************************//
// FONCTION PERMETTANT DE CALLER LE TEXTE A DROITE //
//*************************************************//
function moveRight(){
	if(window.event){
		window.event.keyCode = 39;
	}
}


//********************************************************************//
// FONCTION PERMETTANT DE GÉRER LE STYLE DE LA LISTE DE LOCALISATIONS //
//********************************************************************//
function setStyle(c,name){
	if(name=='mouseover'){
		c.style.backgroundColor="#CCCCCC";
	}else{
		c.style.backgroundColor="#FFFFFF";
	}					
}


//********************************************************************//
// FONCTION PERMETTANT DE FAIRE DISPARAÎTRE LA LISTE DE LOCALISATIONS //
//********************************************************************//
function removeDivVille(){
	if(document.getElementById('_Div_' + _objectInputTextVille)){
		document.body.removeChild(document.getElementById('_Div_' + _objectInputTextVille));
	}
}


//****************************************//
// FONCTION GÉRANT LES CHAÎNES DES VILLES //
//****************************************//
function getExpVille(){
	// Si aucun objet n'est défini, on outre passe la fonction
	if (!document.getElementById(objet)) return;
	
	if(document.getElementById(objet).setSelectionRange){		
		var d=document.getElementById(objet).selectionStart;		
	}else if(document.getElementById(objet).createTextRange){		
		var selection=document.selection.createRange().duplicate();		
		var d=document.getElementById(objet).value.length-selection.text.length;
	}
	
	var _chaine = new String(document.getElementById(objet).value);
	var char;
	var _sousChaine = '';
	var i=2;
	
	while(i <= d){
		char = new String(_chaine.charAt(d-i));
		if(char != '' && char != ','){
			tab.push(char);
		}else{
			break;
		}
		i++;
	}
	tab = tab.reverse();
	var chaine = '';
	
	for(var i=0;i< tab.length;i++){
		chaine += tab[i];
	}
	
	// Un peu de nettoyage
	var exp = /^ /;
	var chaine = new String(chaine);
	var chaine = chaine.replace(exp,'');
	return chaine;
}

//*************************************************************//
// FONCTION RÉCUPÉRANT LES VILLES ET VÉRIFIANT LEUR PERTINANCE //
//*************************************************************//
function checkLocalisationInput(){	
	// Si aucun objet n'est défini, on outre passe la fonction
	if (!document.getElementById(objet)) return;
	isWorking=true;
		
	var _localisation = document.getElementById(objet);
	var chaine1 = new String(_localisation.value);
	var expReg1;
	if (objet == "localisation2") expReg1 = new RegExp(",* *$","");
	if (objet == "geolocalisation" || objet == "localisation_search" || objet == "localisation_colocation") expReg1 = /,* *$/;
	
	 var expVilleAjax = /^[\w ' -]+\(\d{5}\)$/;
	 var expDepAjax = /^[\w ' -]+\(\d{2}\)$/;
	 var cp = /^\d{1,5}$/;
	
	var chaine2 = chaine1.replace(expReg1,'');
	tab = chaine2.split(',');
	var flag;
	for(var i=0;i<tab.length;i++){
		ret1 = tab[i].match(expVilleAjax);
		ret2 = tab[i].match(cp);
		ret3 = tab[i].match(expDepAjax);
		if(ret1 || ret2 || ret3){
			//isWorking=true;
			flag = true;
		}else{
			flag = false;			
			handlerAjax(tab[i],checkVilles);
			return flag;			
		}
	}	
	return flag;
}

//*************************************//
// FONCTION GÉRANT LA PREMIERE VIRGULE //
//*************************************//
function removeFirstVirg(){	
	// Si aucun objet n'est défini, on outre passe la fonction
	if (!document.getElementById(objet)) return;
	
	var virgDebut = /^,/;
	var localisation_search = new String(document.getElementById(objet).value);
	var chaine = localisation_search.replace(virgDebut,'');
	document.getElementById(objet).value = chaine;
}


//*************************************//
// FONCTION GÉRANT LA DERNIERE VIRGULE //
//*************************************//
function removeLastVirg(){	
	// Si aucun objet n'est défini, on outre passe la fonction
	if (!document.getElementById(objet)) return;
	
	var virgFin = /,*$/;
	var localisation_search = new String(document.getElementById(objet).value);
	var chaine = localisation_search.replace(virgFin,',');
	document.getElementById(objet).value = chaine;
}


//*************************************//
// FONCTION GÉRANT LES AUTRES VIRGULES //
//*************************************//
function removeVirgs(){
	// Si aucun objet n'est défini, on outre passe la fonction
	if (!document.getElementById(objet)) return;
	
	var virgs = /,{2,}/g;
	var localisation_search = new String(document.getElementById(objet).value);
	var chaine = localisation_search.replace(virgs,',');
	document.getElementById(objet).value = chaine;
	document.getElementById(objet).focus();
	setTimeout("removeFirstVirg()",200);
	setTimeout("removeLastVirg()",200);
}


//*****************************//
// FONCTION GÉRANT LES ESPACES //
//*****************************//
function removeEspace(){
	// Si aucun objet n'est défini, on outre passe la fonction
	if (!document.getElementById(objet)) return;
	
	var espaceDebut = /^ /;
	var virgEspace = /, /;
	var localisation_search = new String(document.getElementById(objet).value);
	var chaine = localisation_search.replace(espaceDebut,'');
	var chaine = localisation_search.replace(virgEspace,',');
	document.getElementById(objet).value = chaine;
}

//***********************************************//
// FONCTION PERMETTANT DE CONVERTIR LES CHIFFRES //
//***********************************************//
function getChar(){
	var quote = '\'';
	var union = '-';
	var space = ' ';
	var zero = 0;
	var un = 1;
	var deux = 2;
	var trois = 3;
	var quatre = 4;
	var cinq = 5;
	var six = 6;
	var sept = 7;
	var huit = 8;
	var neuf = 9;
	var char = String.fromCharCode(keycode);
	if(keycode == 222 || keycode == 52){
		char = quote;
	}else if(keycode == 54 || keycode == 109){
		char = union;
	}else if(keycode == 32){
		char = space;
	}else if(keycode == 96){
		char = zero;
	}else if(keycode == 97){
		char = un;
	}else if(keycode == 98){
		char = deux;
	}else if(keycode == 99){
		char = trois;
	}else if(keycode == 100){
		char = quatre;
	}else if(keycode == 101){
		char = cinq;
	}else if(keycode == 102){
		char = six;
	}else if(keycode == 103){
		char = sept;
	}else if(keycode == 104){
		char = huit;
	}else if(keycode == 105){
		char =  neuf;
	}
	return char;
}


//*****************************************************************************************//
// FONCTION PERMETTANT DE METTRE A JOUR LES FORMULAIRES EN FONCTION DU COCHAGE / DÉCOCHAGE //
//*****************************************************************************************//
function updateForm(objectFormCheck,objectForm,objectFormvalue){
	removeDivVille();
	if(objectFormCheck.checked == true){
		if(document.getElementById(objectForm).innerHTML == ''){
			document.getElementById(objectForm).innerHTML = objectFormvalue;
		}else{
			if(objectFormCheck.type == 'radio'){
				document.getElementById(objectForm).innerHTML = objectFormvalue;
			}else if(objectFormCheck.type == 'checkbox'){
				document.getElementById(objectForm).innerHTML += ', ' + objectFormvalue;
			}
		}
		if(objectFormCheck.name == 'terrain'){
			if(document.getElementById('div_superficie')){
				document.getElementById('div_superficie').style.display='block';
			}
		}
	}else{
		var chaine1 = new String(document.getElementById(objectForm).innerHTML);
		var expReg1 = new RegExp(",* *" + objectFormvalue,"");
		var expReg2 = new RegExp("^,* *","");
		var chaine2 = new String(chaine1.replace(expReg1,''));
		var ret = chaine2.replace(expReg2,'');
		document.getElementById(objectForm).innerHTML = ret;
		if(objectFormCheck.name == 'terrain'){
			if(document.getElementById('div_superficie')){
				document.getElementById('div_superficie').style.display='none';
				document.getElementById('surfaceterr_min').value='';
				document.getElementById('surfaceterr_max').value='';
			}
		}
	}
}


//********************************************************************************//
// FONCTION PERMETTANT D'AFFICHER LA LISTE DES VILLES POUR LE MOTEUR DE RECHERCHE //
//********************************************************************************//
function ShowCityList(){
	// On supprime l'éventuel élément étranger affiché précédemment
	if (old_menu && old_menu != 'MLocalisation') document.getElementById(old_menu).style.display='none';

	// On mémorise l'élément actuel
	old_menu = 'MLocalisation';
	
	// On supprime le timer qui avait été lancé (dans le cas d'un retour au même menu)
	clearTimeout(timer);
	
	// Tant que la souris n'est pas ressortie, on ne ré-exécute pas la fonction
	if (where_is_my_mouse == "out"){
		// On indique que la souris est à l'intérieur
		where_is_my_mouse = "in";
		
		// Déclaration des variables
		var liste_ville;
		var tab_liste_ville;
		var UL_content;
	
		// On récupère la valeur de la liste de villes
		liste_ville = document.getElementById(moteur_recherche).localisation.value;
	
		// Si la liste est vide, on réinitialise le UL
		if (liste_ville == "Saisir une localisation ci-dessus"){
			//UL_content = '<li><label for="vente">Aucune localisation présente</label></li>';
			return;
		}else{
			// Sinon, on sépare le contenu dans un tableau afin de construire le Div
			tab_liste_ville = liste_ville.split(',');
					
			// On prépare le contenu de l'UL
			UL_content = "";
			for (i = 0; i < tab_liste_ville.length; i++){
				if (tab_liste_ville[i] != " " && tab_liste_ville[i] != "") UL_content = UL_content + '<li><input onclick="BuildCityList(' + i + ')" type="checkbox" id="ville_' + i + '" name="ville_' + i + '" value="1" checked /><label for="' + trim(tab_liste_ville[i]) + '">' + trim(tab_liste_ville[i]) + '</label></li>';
			}
		}
	
		// On remplace le UL			
		document.getElementById('MLocalisation_UL').innerHTML = UL_content;
					
	
		// On affiche la Div				
		document.getElementById('MLocalisation').style.display='block';
		document.getElementById('MLocalisation').style.position='absolute';
	}
}


//****************************************************************************//
// FONCTION PERMETTANT DE RECONSTRUIRE LA LISTE DES VILLES APRES UN DÉCOCHAGE //
//****************************************************************************//
function BuildCityList(indice){
	// Tout d'abord, on camoufle la liste
	document.getElementById('MLocalisation').style.display='none';
		
	// Déclaration des variables
	var liste_ville;
	var tab_liste_ville;
	var UL_content;

	// On récupère la valeur de la liste de villes
	liste_ville = document.getElementById(moteur_recherche).localisation.value;
	
	// On recrée le tableau des villes initial
	tab_liste_ville = liste_ville.split(',');
	if(tab_liste_ville.length >0){
		// Si la dernière ville se termine par une virgule, le dernier élément ne vaut rien, on l'enlève
		if (!tab_liste_ville[tab_liste_ville.length - 1]) tab_liste_ville = deleteRow(tab_liste_ville, tab_liste_ville.length - 1);
		
		// On supprime l'indice envoyé
		tab_liste_ville = deleteRow(tab_liste_ville, indice);
		
		// On recrée l'UL à partir du tableau ainsi obtenu si ce dernier est non vide
		if(tab_liste_ville[0]){
			UL_content = "";
			for (i = 0; i < tab_liste_ville.length; i++){			
				UL_content = UL_content + '<li><input onclick="BuildCityList(' + i + ')" type="checkbox" id="ville_' + i + '" name="ville_' + i + '" value="1" checked /><label for="' + trim(tab_liste_ville[i]) + '">' + trim(tab_liste_ville[i]) + '</label></li>';
			}
		}
		
		// On affiche les nouvelles villes au champ (si liste non vide)	
		liste_ville = tab_liste_ville.join(', ');	
		if (liste_ville != "") document.getElementById(moteur_recherche).localisation.value = liste_ville;
		else {
			try{document.getElementById('div_localisation').style.visibility='hidden';}catch(err){}
			document.getElementById(moteur_recherche).localisation.value = "Saisir une localisation ci-dessus";
		}
			
		// On réaffiche la liste
		ShowCityList();
	}
}


//*******************************************************************************************//
// FONCTION PERMETTANT DE SUPPRIMER UNE LIGNE D'UN TABLEAU AVEC RECONSTRUCTION DE CE TABLEAU //
//*******************************************************************************************//
function deleteRow(tableau, indice){	
	// On crée le nouveau tableau (une case de moins que l'initial)
	var new_tableau = new Array(tableau.length - 1);
	
	// Déclaration et initialisation des variables pointeurs
	var i, j;
	i = j = 0;
		
	// On supprime l'élément du tableau
	delete tableau[indice];
	
	// Si l'élément vaut quelquechose, on le recopie, sinon, on passe
	for (i = 0; i < tableau.length; i++){			
		if (tableau[i]){
			new_tableau[j] = trim(tableau[i]);
			j++;
		}		
	}
	
	// On renvoit le tableau ainsi défini
	return new_tableau;
}


//*****************************************************************************//
// FONCTION PERMETTANT DE VÉRIFIER SI UNE LOCALISATION EST DÉJA CHOISIE OU PAS //
//*****************************************************************************//
function IsAlreadySelected(localisation){
	// On récupère les localisations déjà insérées
	var localisations = document.getElementById(moteur_recherche).localisation.value;
	
	// On recherche une éventuelle occurence de ladite localisation
	if (localisations.indexOf(localisation) > -1) return true;
	else return false;	
}


//****************************************************************//
// FONCTIONS PERMETTANT D'AJOUTER UN TIMER AUX LISTES DÉROULANTES //
//****************************************************************//

function ShowChoices(div){
	// On affiche le menu désiré si ce n'est pas encore le cas
	if(document.getElementById('surfaceh_min') && document.getElementById('surfaceh_max')){
		if(document.getElementById('surfaceh_min').focused==true || document.getElementById('surfaceh_max').focused==true){
			return;
		}
	}
	if(document.getElementById(div).style.display=='block'){
		document.getElementById(div).style.display='none';
	}else{
		document.getElementById(div).style.position='absolute';
		document.getElementById(div).style.display='block';
	}
}
function HideAllChoices(){
	if(document.getElementById('surfaceh_min') && document.getElementById('surfaceh_max')){
		if(document.getElementById('surfaceh_min').focused==true || document.getElementById('surfaceh_max').focused==true){
			return;
		}
	}
	for(var i=0;i < _choices.length;i++){
		var _divIsOver=eval('_' + _choices[i] + 'IsOver');
		if(_divIsOver==false){
			if(document.getElementById(_choices[i])){
				document.getElementById(_choices[i]).style.display='none';
			}
		}
	}
}


// Expressions régulières servant aux 3 fonction suivantes
var regExpBeginning = /^\s+/;
var regExpEnd       = /\s+$/;
 
 
//***********************************************************//
// FONCTION SIMULANT LA FONCTION TRIM DE DIFFÉRENTS LANGAGES //
//***********************************************************//
function trim(aString) {
    return aString.replace(regExpBeginning, "").replace(regExpEnd, "");
}
 

//************************************************************//
// FONCTION SIMULANT LA FONCTION LTRIM DE DIFFÉRENTS LANGAGES //
//************************************************************//
function ltrim(aString) {
    return aString.replace(regExpBeginning, "");
}
 
//************************************************************//
// FONCTION SIMULANT LA FONCTION RTRIM DE DIFFÉRENTS LANGAGES //
//************************************************************//
function rtrim(aString) {
    return aString.replace(regExpEnd, "");
} 


//**********************************************************************************************//
// FONCTION PERMETTANT D'ENCODER LES CARACTERES POUR OUTRE-PASSER LES PROBLEMES D'ENCODAGE AJAX //
//**********************************************************************************************//
// Param 1 : le string à coder
// Param 2 : le délimiteur
function encode (string, delimiteur){
	phrase=String(string);
	resultat="";
	
	for (i=0; i<phrase.length; i++) {
		resultat=resultat+delimiteur+phrase.charCodeAt(i);
	}
	return resultat;
}


//***************************************************************************************//
// FONCTION PERMETTANT DE DÉCODER LES CARACTERES RECUS DE L'AJAX (ET ENCODÉS PAR LA CGI) //
//***************************************************************************************//
// Param 1 : le string à décoder
// Param 2 : le délimiteur
function decode (string, delimiteur){		
	var tab=string.split(delimiteur);	
	var resultat="";
	for (i=0; i<tab.length; i++) {		
		resultat = resultat + String.fromCharCode(parseInt(tab[i]));
	}	
	return resultat;
}


//****************************************************************//
// FONCTION PERMETTANT D'AUTORISER QUE LES CHIFFRES DANS UN CHAMP //
//****************************************************************//
function NumMask(e){	
	var IE5 = false;

	if (!e) var e = window.event;
	if (e.keyCode){
		IE5= true; code = e.keyCode;}
	else if (e.which) code = e.which ;

	//test du code
	if (code < 48 || code > 57) {
		if(IE5){
			e.returnValue = false;
		}
		else
			e.preventDefault();
	} 
}
//****************************************************************//
// FONCTION PERMETTANT D'AUTORISER QUE LES CHIFFRES DANS UN CHAMP //
// Le 2e parametre autorise la sasie d'un caractere identifié par son code ascii//
//****************************************************************//
function NumMask2(e, ok){	
	var IE5 = false;

	if (!e) var e = window.event;
	if (e.keyCode){
		IE5= true; code = e.keyCode;}
	else if (e.which) code = e.which ;
	
	var ok=ok;

	//test du code
	if ( (code < 48 ) || code > 57 ) {
		if (code!=ok) {
			if(IE5){
				e.returnValue = false;
			}
			else
				e.preventDefault();
			} 
		}
}
//****************************************************************//
// FONCTION PERMETTANT DE MODIFIER LE TEXTE DU CHAMP surface EN FONCTION DE LA SAISIE //
//****************************************************************//
function updateSurface(){
	var _formDst = eval('document.' + recherche_form);
	var surfaceh_min = _formDst.surfaceh_min.value;
	var surfaceh_max = _formDst.surfaceh_max.value;
	if(document.getElementById('surface')){
		if(surfaceh_min > 0 && surfaceh_max == ''){
			document.getElementById('surface').innerHTML = 'Plus de ' + surfaceh_min + 'm²';
			_formDst.surfacehbetween[0].checked = true;
		}else if(surfaceh_min > 0 && surfaceh_max > 0){
			document.getElementById('surface').innerHTML = 'De ' + surfaceh_min + ' à ' + surfaceh_max + 'm²';
			_formDst.surfacehbetween[0].checked = true;
		}else if(surfaceh_min == '' && surfaceh_max >0){
			document.getElementById('surface').innerHTML = 'Moins de ' + surfaceh_max + 'm²';
			_formDst.surfacehbetween[0].checked = true;
		}else if(surfaceh_min == '' && surfaceh_max == ''){
			document.getElementById('surface').innerHTML = '';
		}
	}
}

function coNouveautesGeoloc(){
	var regExpCo = /m_co/;
	var regExpNew = /m_new/;
	var page = document.location.href;
	if(page.match(regExpCo)){
		document.recherche_Googlemap.co.value=1;
	}else if(page.match(regExpNew)){
		document.recherche_Googlemap.nouveautes.value=1;
	}
}
