var URL 		= 'http://www.fi-immobilier.com'							;

function loading(act)
{
	if (act == 1)
	{
		document.getElementById('loading').style.display='block'	;
	}
	else
	{
		document.getElementById('loading').style.display='none'	;
	}
}

function popup(message)
{
	if (message == 'Enregistrement termin&eacute;')
	{
		message	=	message+'<br /><br /><a href="'+URL+'/admin/accueil.php" class="blanc">Retour</a>'	;
	}
	document.getElementById('message').innerHTML=message		;
	document.getElementById('popup').style.display='block'		;
	document.getElementById('inpopup').style.display='block'	;
}

function closepopup()
{
	document.getElementById('popup').style.display='none'	;
	document.getElementById('inpopup').style.display='none'	;
}


function newsletter()
{
	var xhr = ajax()											;
	var email	=	document.getElementById('newslemail').value	;

	loading(1)													;

	document.getElementById('newslok').disabled=true			;
	document.getElementById('newslemail').disabled=true			;
	
	xhr.onreadystatechange = function()
	{
		if (xhr.readyState == 4 && xhr.status == 200)
		{
			var	result	=	xhr.responseText.split('|')			;

			if (result[0] == '1')
			{
				popup(''+result[1]+'')							;
				loading(0)										;
			}
			else if (result[0] == '0')
			{
				loading(0)										;
				popup(''+result[1]+'')							;
			}
		
			document.getElementById('newslemail').disabled=false			;
			document.getElementById('newslok').disabled=false				;
		}
	}
	
	mypost	=	function()
	{
		xhr.open('POST', URL+'/_PHP/post-newsletter.php', true)	;
		xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xhr.send('email='+email);
	}
	
	setTimeout('mypost()', 1000)	;
}

function ajoutSelection(id)
{
	loading(1)													;

	var xhr 		= 	ajax()									;
	
	xhr.onreadystatechange = function()
	{
		if (xhr.readyState == 4 && xhr.status == 200)
		{
			var	result	=	xhr.responseText.split('|')	;
			if (result[0] == '1')
			{
				$('#bien'+id).effect("transfer", { to: "#nbsel" }, 1000)		;
				timeanime	=	function()
				{
					document.getElementById('supsel'+id).style.display='block'		;
					loading(0)														;
					document.getElementById('ajsel'+id).style.display='none'		;
					document.getElementById('nbsel').innerHTML=result[1]			;
				}
				
				setTimeout('timeanime()', 1000)	;
			}
		}
	}
	
	xhr.open('POST', URL+'/_PHP/post-ajoutSelection.php', true)					;
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded')	;
	xhr.send('id='+id)															;
	
}

function ajoutSelectionFiche(id)
{
	loading(1)												;

	var xhr 		= 	ajax()								;
	
	xhr.onreadystatechange = function()
	{
		if (xhr.readyState == 4 && xhr.status == 200)
		{
			var	result	=	xhr.responseText.split('|')	;
			if (result[0] == '1')
			{
				$('#fiche').effect("transfer", { to: "#nbsel" }, 1000)		;
				timeanime	=	function()
				{
					document.getElementById('supsel'+id).style.display='block'		;
					loading(0)														;
					document.getElementById('ajsel'+id).style.display='none'		;
					document.getElementById('nbsel').innerHTML=result[1]			;
				}
				
				setTimeout('timeanime()', 1000)	;
			}
		}
	}
	
	xhr.open('POST', URL+'/_PHP/post-ajoutSelection.php', true)					;
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded')	;
	xhr.send('id='+id)															;
	
}

function suppSelection(id)
{
	loading(1)													;
	var xhr 		= 	ajax()									;
	
	xhr.onreadystatechange = function()
	{
		if (xhr.readyState == 4 && xhr.status == 200)
		{
			var	result	=	xhr.responseText.split('|')	;
			if (result[0] == '1')
			{
				$('#nbsel').effect("transfer", { to: "#bien"+id }, 1000)		;
				timeanime	=	function()
				{
					document.getElementById('supsel'+id).style.display='none'		;
					loading(0)														;
					document.getElementById('ajsel'+id).style.display='block'		;
					document.getElementById('nbsel').innerHTML=result[1]			;
				}
				
				setTimeout('timeanime()', 1000)	;
			}
		}
	}
	
	xhr.open('POST', URL+'/_PHP/post-suppSelection.php', true)					;
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded')	;
	xhr.send('id='+id)															;
}

function suppSelection2(id)
{
	loading(1)													;
	var xhr 		= 	ajax()									;
	
	xhr.onreadystatechange = function()
	{
		if (xhr.readyState == 4 && xhr.status == 200)
		{
			var	result	=	xhr.responseText.split('|')	;
			if (result[0] == '1')
			{
				document.getElementById('bien'+id).style.display='none'				;
				document.getElementById('supsel'+id).style.display='none'			;
				loading(0)															;
				document.getElementById('ajsel'+id).style.display='block'			;
				document.getElementById('nbsel').innerHTML=result[1]				;
			}
		}
	}
	
	xhr.open('POST', URL+'/_PHP/post-suppSelection.php', true)					;
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded')	;
	xhr.send('id='+id)															;
}

function suppSelectionFiche(id)
{
	loading(1)													;
	var xhr 		= 	ajax()									;
	
	xhr.onreadystatechange = function()
	{
		if (xhr.readyState == 4 && xhr.status == 200)
		{
			var	result	=	xhr.responseText.split('|')	;
			if (result[0] == '1')
			{
				$('#nbsel').effect("transfer", { to: "#fiche" }, 1000)		;
				timeanime	=	function()
				{
					document.getElementById('supsel'+id).style.display='none'		;
					loading(0)														;
					document.getElementById('ajsel'+id).style.display='block'		;
					document.getElementById('nbsel').innerHTML=result[1]			;
				}
				
				setTimeout('timeanime()', 1000)	;
			}
		}
	}
	
	xhr.open('POST', URL+'/_PHP/post-suppSelection.php', true)					;
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded')	;
	xhr.send('id='+id)															;
}

function vendrelouer()
{
	loading(1)													;
	document.getElementById('submitbien').disabled=true			;
	
	var xhr = ajax()											;
	
	xhr.onreadystatechange = function()
	{
		if (xhr.readyState == 4 && xhr.status == 200)
		{
			result	=	xhr.responseText.split('|')	;
			if (result[0] == '1')
			{
				popup(''+result[1]+'')												;
				loading(0)															;
				document.getElementById('contentsfull').innerHTML='<p>Merci !</p>'	;
			}
			else if (result[0] == '2')
			{
				popup(''+result[1]+'')										;
				document.getElementById('submitbien').disabled=false		;
				loading(0)													;
			}
			else
			{
				msgerror	=	''					;
				nberrors	=	result.length		;
				
				for (xi=1; xi<nberrors; xi++)
				{
					msgerror	=	msgerror+'<p>'+result[xi]+'</p>'		;
				}
				
				popup(''+msgerror+'')										;
				document.getElementById('submitbien').disabled=false		;
				loading(0)													;
			}
		}
	}
	
	xhr.open('POST', URL+'/_PHP/post-vendrelouer.php', true)					;
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded')	;
	xhr.send(getFields('formvl'))												;
}

function quartiers(ville, champ, langue)
{
	loading(1)																	;
	var xhr 		= 	ajax()													;
	
	xhr.onreadystatechange = function()
	{
		if (xhr.readyState == 4 && xhr.status == 200)
		{
			result	=	xhr.responseText.split('|')	;
			nbquart	=	result.length				;
			if ((result[0]) == '1')
			{
				while (document.getElementById('bienquartier'+champ).options.length > 0)
				{
					document.getElementById('bienquartier'+champ).options[0] = null;
				}

				for (xi = 1; xi<nbquart; xi++)
				{
					thisquart	=	result[xi].split('###')												
					olength		=	document.getElementById('bienquartier'+champ).options.length	;
					
					document.getElementById('bienquartier'+champ).options[olength] = new Option(''+thisquart[1]+'', ''+thisquart[0]+'')	;
				}
			}
			else
			{
				while (document.getElementById('bienquartier'+champ).options.length > 0)
				{
					document.getElementById('bienquartier'+champ).options[0] = null	;
				}
				olength		=	document.getElementById('bienquartier'+champ).options.length							;

				if (langue == 'en')
				{
					document.getElementById('bienquartier'+champ).options[olength] = new Option('All districts', '0')	;
				}
				else if (langue == 'he')
				{
					document.getElementById('bienquartier'+champ).options[olength] = new Option('', '0')	;
				}
				else
				{
					document.getElementById('bienquartier'+champ).options[olength] = new Option('Tous les quartiers', '0')	;
				}
			}
			loading(0)		;			
		}
	}

		var ranget	=	parseInt(Math.random()*1000000)									;
		var urlget	=	URL+'/_PHP/get-quartiers.php?ville='+ville+'&random='+ranget	;
		xhr.open( 'GET', urlget,  true)													; 
		xhr.send(null)																	; 
}

function ajoutVille()
{
	loading(1)																;
	inner 		=	document.getElementById('champville').innerHTML			;
	newvalue	=	parseInt(document.getElementById('nbvilles').value) +1	;
	
	document.getElementById('nbvilles').value = newvalue					;

	var xhr 		= 	ajax()		;
	xhr.onreadystatechange = function()
	{
		if (xhr.readyState == 4 && xhr.status == 200)
		{
			document.getElementById('champville').innerHTML=inner+xhr.responseText	;
			loading(0)																;				
		}
	}

	var ranget	=	parseInt(Math.random()*1000000)									;
	var urlget	=	URL+'/_PHP/get-ajoutville.php?nbv='+newvalue+'&random='+ranget	;
	xhr.open( 'GET', urlget,  true)													; 
	xhr.send(null)																	; 
}

function newalert()
{
	loading(1)	;
	var xhr = ajax()											;

	document.getElementById('submitalert').disabled=true		;
	
	xhr.onreadystatechange = function()
	{
		if (xhr.readyState == 4 && xhr.status == 200)
		{

			result	=	xhr.responseText.split('|')	;
			if (result[0] == '1')
			{
				popup(''+result[1]+'')												;
				loading(0)															;
				document.getElementById('contentsfull').innerHTML='<p>Merci !</p>'	;
			}
			else if (result[0] == '2')
			{
				popup(''+result[1]+'')										;
				document.getElementById('submitalert').disabled=false		;
				loading(0)													;
			}
			else
			{
				msgerror	=	''					;
				nberrors	=	result.length		;
				
				for (xi=1; xi<nberrors; xi++)
				{
					msgerror	=	msgerror+'<p>'+result[xi]+'</p>'		;
				}
				
				popup(''+msgerror+'')										;
				document.getElementById('submitalert').disabled=false		;
				loading(0)													;
			}
		}
	}
	
	mypost	=	function()
	{
		xhr.open('POST', URL+'/_PHP/post-newalert.php', true);
		xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xhr.send(getFields('formalert'));
	}
	
	setTimeout('mypost()', 1000)	;
}

function contact()
{
	loading(1)																		;
	document.getElementById('coordsubmit').disabled=true							;
	
	var xhr 		= 	ajax()													;
	xhr.onreadystatechange = function()
	{
		if (xhr.readyState == 4 && xhr.status == 200)
		{
			result	=	xhr.responseText.split('|')	;
			if (result[0] == '1')
			{
				popup(''+result[1]+'')												;
				loading(0)															;
				document.getElementById('contentsfull').innerHTML='<p>Merci !</p>'	;
			}
			else if (result[0] == '2')
			{
				popup(''+result[1]+'')										;
				document.getElementById('coordsubmit').disabled=false		;
				loading(0)													;
			}
			else
			{
				msgerror	=	''					;
				nberrors	=	result.length		;
				
				for (xi=1; xi<nberrors; xi++)
				{
					msgerror	=	msgerror+'<p>'+result[xi]+'</p>'		;
				}
				
				popup(''+msgerror+'')										;
				document.getElementById('coordsubmit').disabled=false		;
				loading(0)													;
			}
		}
	}
	
	mypost	=	function()
	{
		xhr.open('POST', URL+'/_PHP/post-contact.php', true)						;
		xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded')	;
		xhr.send(getFields('formcontact'))												;
	}
	
	setTimeout('mypost()', 1000)	;
}

function advanced()
{
	loading(1)	;
	
	var modenow	=	document.getElementById('moderecherche').value		;
	var langnow	=	document.getElementById('languerecherche').value	;
	var xhr		= 	ajax()												;
	
	xhr.onreadystatechange = function()
	{
		if (xhr.readyState == 4 && xhr.status == 200)
		{
			document.getElementById('inputzone').innerHTML=xhr.responseText		;
			loading(0)															;

			if (modenow == 1) { document.getElementById('moderecherche').value=0	; }
			if (modenow == 0) { document.getElementById('moderecherche').value=1	; }
			
			titrerecherche(langnow)	;
		}
	}

	var ranget	=	parseInt(Math.random()*1000000)											;
	var urlget	=	URL+'/_PHP/get-advancedsearch.php?modenow='+modenow+'&langnow='+langnow+'&random='+ranget	;
	xhr.open( 'GET', urlget,  true)															; 
	xhr.send(null)																			; 
}

function titrerecherche(langue)
{
	var xhr		= 	ajax()												;
	
	xhr.onreadystatechange = function()
	{
		if (xhr.readyState == 4 && xhr.status == 200)
		{
			docXML		=	xhr.responseXML	;
			innerText	=	docXML.documentElement.childNodes[2].childNodes[8].text		;
			document.getElementById('titrerecherche'+langue).innerHTML	=	innerText	;
		}
	}

	xhr.open( 'GET', URL+'/_XML/'+langue+'.xml',  true)		; 
	xhr.send(null)											; 
}

function typeRecherche(valeur)
{
	loading(1)															;
	var langnow	=	document.getElementById('languerecherche').value	;
	var xhr		= 	ajax()												;
	
	xhr.onreadystatechange = function()
	{
		if (xhr.readyState == 4 && xhr.status == 200)
		{
			document.getElementById('divprix').innerHTML=xhr.responseText		;
			loading(0)														;
		}
	}

	var ranget	=	parseInt(Math.random()*1000000)															;
	var urlget	=	URL+'/_PHP/get-prixsearch.php?modenow='+valeur+'&langnow='+langnow+'&random='+ranget	;
	xhr.open( 'GET', urlget,  true)																			; 
	xhr.send(null)																							;
}

function ficheContact(id)
{
	loading(1)													;
	document.getElementById('fichenom').disabled=true			;
	document.getElementById('ficheprenom').disabled=true		;
	document.getElementById('fichemail').disabled=true			;
	document.getElementById('fichetel').disabled=true			;
	document.getElementById('fichecom').disabled=true			;
	document.getElementById('submitcontactfiche').disabled=true	;

	var xhr 		= 	ajax()													;
	xhr.onreadystatechange = function()
	{
		if (xhr.readyState == 4 && xhr.status == 200)
		{
			result	=	xhr.responseText.split('|')	;
			if (result[0] == '1')
			{
				popup(''+result[1]+'')												;
				loading(0)															;
			}
			else if (result[0] == '2')
			{
				popup(''+result[1]+'')										;
				loading(0)													;
			}
			else
			{
				msgerror	=	''					;
				nberrors	=	result.length		;

				for (xi=1; xi<nberrors; xi++)
				{
					msgerror	=	msgerror+'<p>'+result[xi]+'</p>'		;
				}

				popup(''+msgerror+'')										;
				loading(0)													;
			}

			document.getElementById('fichenom').disabled=true			;
			document.getElementById('ficheprenom').disabled=true		;
			document.getElementById('fichemail').disabled=true			;
			document.getElementById('fichetel').disabled=true			;
			document.getElementById('fichecom').disabled=true			;
			document.getElementById('submitcontactfiche').disabled=true	;
		}
	}

	mypost	=	function()
	{
		xhr.open('POST', URL+'/_PHP/post-fichecontact.php', true)						;
		xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded')		;
		xhr.send(getFields('ficheform'))												;
	}

	setTimeout('mypost()', 1000)	;
}

function cxdispo(value)
{
	if (value == 0)
	{
		document.getElementById('date1').style.display='block'	;
	}
	else
	{
		document.getElementById('date1').style.display='none'	;
	}
}

function visualiser(id,act)
{
	loading(1)	;

	if ((act == 1) && (document.getElementById('modediapo').value==1))
	{
		document.getElementById('modediapo').value=0	;
	}
	
	if ((act == 1) || ((act == 0) && (document.getElementById('modediapo').value==1)))
	{
		for (i=1; i<=6; i++)
		{
			if (i != id)
			{
				if (document.getElementById('imgphotoannonce'+i))
				{
					document.getElementById('imgphotoannonce'+i).style.display='none'	;
				}
			}
		}
		$('img:#imgphotoannonce'+id).fadeIn("slow");
	}
	loading(0)	;
}

function diaporama(nb)
{
	diapo = function()
	{
		if (document.getElementById('modediapo').value==1)
		{
			for (i=1; i<=nb; i++)
			{
				setTimeout('visualiser('+i+',0)', i*4000)	;
			}
		}
	}
	
	premierpassage = function(nb)
	{
		for (i=1; i<=nb; i++)
		{
			if (i==1)
			{
				if (document.getElementById('imgphotoannonce'+(i+1)))
				{
					setTimeout('visualiser('+(i+1)+',0)', 4000)		;
				}
			}
			if ((i>=2) && (i<nb))
			{
				if (document.getElementById('imgphotoannonce'+(i+1)))
				{
					setTimeout('visualiser('+(i+1)+',0)', (i*4000))	;
				}
			}
			if (i==nb)
			{
				if (document.getElementById('imgphotoannonce'+nb))
				{
					setTimeout('visualiser('+nb+',0)', ((nb*4000)-4000))	;
				}
			}
		}
	}
	
	premierpassage(nb)	;
	
	timerint = setInterval('diapo()', nb*4000)	;
}

function openAmi()
{
	$("#envoyerami").slideDown("slow");
}

function envoiAmi()
{
	loading(1)	;
	document.getElementById('submitami').disabled=true	;

	var xhr 		= 	ajax()													;
	xhr.onreadystatechange = function()
	{
		if (xhr.readyState == 4 && xhr.status == 200)
		{
			result	=	xhr.responseText.split('|')	;
			if (result[0] == '1')
			{
				popup(''+result[1]+'')										;
				loading(0)													;
				$("#envoyerami").slideUp("slow")							;
			}
			else
			{
				msgerror	=	''					;
				nberrors	=	result.length		;
				
				for (xi=1; xi<nberrors; xi++)
				{
					msgerror	=	msgerror+'<p>'+result[xi]+'</p>'		;
				}
				
				popup(''+msgerror+'')										;
				document.getElementById('submitami').disabled=false			;
				loading(0)													;
			}
		}
	}
	
	mypost	=	function()
	{
		xhr.open('POST', URL+'/_PHP/post-ami.php', true)							;
		xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded')	;
		xhr.send(getFields('formami'))												;
	}
	
	setTimeout('mypost()', 1000)	;
}

function protect()
{
	document.oncontextmenu = function()
	{
		popup('Copyright &copy; '+URL)	;
		return false	;
	}
}

function elargi(recherche)
{
	document.location.href=URL+recherche	;
}

function orderby(ord)
{
	urlnow = document.location.href				;
	document.location.href=urlnow+'&order='+ord	;
}

function parpage(quant)
{
	urlnow = document.location.href					;
	if (urlnow == URL+'/recherche/')
	{
		document.location.href=urlnow+'?parpage='+quant	;
	}
	else
	{
		urlnow	=	urlnow.replace('&parpage=10', '')	;
		urlnow	=	urlnow.replace('&parpage=20', '')	;
		urlnow	=	urlnow.replace('&parpage=30', '')	;
		document.location.href=urlnow+'&parpage='+quant	;
	}
}

function page(num)
{
	urlnow 		= 	document.location.href										;
	urlnow		=	urlnow.replace('page='+(num-1)+'&', '')						;
	urlnow		=	urlnow.replace('page='+(num+1)+'&', '')						;
	urlnow		=	urlnow.replace('page='+num+'&', '')							;
	
	urlnow		=	urlnow.replace('?page='+(num-1), '')						;
	urlnow		=	urlnow.replace('?page='+(num+1), '')						;
	urlnow		=	urlnow.replace('?page='+num, '')							;
	
	urlnow		=	urlnow.replace('?', '&')									;
	urlnow		=	urlnow.replace('/recherche/', '/recherche/?page='+num)		;
	document.location.href=urlnow												;
}
