/*POP UP*/
function popup(pop_url,pop_width,pop_height) {
	pop_left = (window.screen.availWidth / 2) - (pop_width / 2);
	pop_top = (window.screen.availHeight / 2) - (pop_height / 2);
	window.open(pop_url,"","left=" + pop_left + ",top=" + pop_top + ",width=" + pop_width + ",height=" + pop_height + ",scrollbars=yes,noresize");
}

/*FERME/OUVRE UN BLOC*/
function display_block(nom){
	if(document.getElementById(nom).style.display == 'none') document.getElementById(nom).style.display = 'block';
	else document.getElementById(nom).style.display = 'none';
}

/*AFFICHE LES FLASH*/
function RunFoo(filename, width, height) {
	document.write("<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0\" width=\""+width+"\" height=\""+height+"\">");
	document.write("<param name=\"movie\" value=\""+filename+"\" />");
	document.write("<param name=\"quality\" value=\"high\" />");
	document.write("<param name=\"menu\" value=\"false\" />");
	document.write("<param name=\"wmode\" value=\"transparent\" />");
	document.write("<embed src=\""+filename+"\" quality=\"high\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" type=\"application/x-shockwave-flash\" menu=\"false\" width=\""+width+"\" height=\""+height+"\" wmode=\"transparent\"></embed>");
	document.write("</object>");
}

//FONCTION AJAX
function getXhr(){
	var xhr = null; 
	if(window.XMLHttpRequest) xhr = new XMLHttpRequest(); 
	else if(window.ActiveXObject){
		try{
			xhr = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e){
			xhr = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	else{
		alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest..."); 
		xhr = false; 
	} 
	return xhr
}

//AFFICHE AVEC AJAX
function ouvre_page(div,page,variable,type){
	var xhr = getXhr();
	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4 && xhr.status == 200){
			document.getElementById(div).innerHTML = xhr.responseText;
		}
	}
	xhr.open(type,page,true);
	if(type=="POST"){
		xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xhr.send(variable);
	}
	else xhr.send(null);
}



/* ************************************* SITE ************************************* */
//AFFICHE OU MASUQE LE FORMULAIRE DE RECHERCHE DE BIEN
function aff_criteres(){
	if(document.getElementById('recherche_criteres').style.display == 'none'){
		document.getElementById('recherche_criteres').style.display = 'block';
		document.getElementById('txt_aff_criteres').innerHTML = 'Masquer';
		document.getElementById('img_aff_criteres').src = 'images/criteres_off.jpg';
	}
	else{
		document.getElementById('recherche_criteres').style.display = 'none';
		document.getElementById('txt_aff_criteres').innerHTML = 'Afficher';
		document.getElementById('img_aff_criteres').src = 'images/criteres_on.jpg';
	}
}

// function aff_photo_detail(id, num){
function aff_photo_detail(num){	
	var liste = document.getElementById('gde_photo_detail').getElementsByTagName('a');
	for(var i=0;i<liste.length;i++){
		if(liste[i].id=="photo-bien-"+num) liste[i].style.display = 'block';
		else liste[i].style.display = 'none';
	}
}

//envoi la fiche d'un bien à un ami
function envoyer_ami(){	
	var variables = 'id='+document.forms.form_ami.id.value+'&nom='+document.forms.form_ami.nom.value+'&nom_ami='+document.forms.form_ami.nom_ami.value+'&email_ami='+document.forms.form_ami.email_ami.value;
	variables = variables+'&message='+document.forms.form_ami.message.value+'&envoi_ami=ok';
	
	var xhr = getXhr();
	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4 && xhr.status == 200){
			var reponse = xhr.responseText;
			var tab = reponse.split('|');
			
			if(tab[1]=="ok") document.forms.form_ami.reset();
			
			document.getElementById('erreur_envoi_ami').innerHTML = tab[0];
		}
	}
	xhr.open('POST', 'envoi_ami.php', true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send(variables);
}

// function aff_photo_detail(num ou video){
function aff_photo_detail_2(num){	
	if(num=='video') document.getElementById('bloc_video').style.display = 'block';
	else document.getElementById('bloc_video').style.display = 'none';
	
	var liste = document.getElementById('gde_photo_detail').getElementsByTagName('a');
	for(var i=0;i<liste.length;i++){
		if(liste[i].id=="photo-bien-"+num) liste[i].style.display = 'block';
		else liste[i].style.display = 'none';
	}
}



/* ************************************* PRIVATE ************************************* */
//STYLE DE TEXTE
function style_texte(debut, fin, preview, champ) {
	var input = document.getElementById(champ);
	input.focus();
	if(typeof document.selection != 'undefined') {
		var range = document.selection.createRange();
		var insText = range.text;
		range.text = debut + insText + fin;
		range = document.selection.createRange();
		if (insText.length == 0) range.move('character', -fin.length);
		else range.moveStart('character', debut.length + insText.length + fin.length);
		range.select();
	}
	else if(typeof input.selectionStart != 'undefined') {
		var start = input.selectionStart;
		var end = input.selectionEnd;
		var insText = input.value.substring(start, end);
		input.value = input.value.substr(0, start) + debut + insText + fin + input.value.substr(end);
		var pos;
		if (insText.length == 0) pos = start + debut.length;
		else pos = start + debut.length + insText.length + fin.length;
		input.selectionStart = pos;
		input.selectionEnd = pos;
	}
	else {
		var pos;
		var re = new RegExp('^[0-9]{0,3}$');
		while(!re.test(pos)) {
		  pos = prompt("Insertion à la position (0.." + input.value.length + "):", "0");
		}
		if(pos > input.value.length) pos = input.value.length;
		var insText = prompt("Veuillez entrer le texte à formater:");
		input.value = input.value.substr(0, pos) + debut + insText + fin + input.value.substr(pos);
	}
	
	if(preview!='') preview_texte(preview, champ);
}

//PREVISUALISATION DES TEXTES
function preview_texte(preview, champ){
	ouvre_page(preview, 'index.php', 'preview=ok&texte='+document.getElementById(champ).value, 'POST');
}

//AFFICHE LE DETAIL D'UN MAIL
function aff_detail_mails(id){
	if(document.getElementById('ligne_mails_'+id).className=='ligne_mails'){
		var mot_ereg = new RegExp("ligne_mails_","g");	
		var liste_mails = document.getElementById('liste_mails').getElementsByTagName('div');
		for(var i=0;i<liste_mails.length;i++){
			if(liste_mails[i].id.match(mot_ereg)){
				if(liste_mails[i].id=="ligne_mails_"+id){
					liste_mails[i].className = 'ligne_mails_gras';
					document.getElementById('detail_mails').style.display = 'block';
					ouvre_page('detail_mails', 'mails.php?aff_detail='+id, '', 'GET');
				}
				else liste_mails[i].className = 'ligne_mails';
			}
		}
	}
}

//VERIFIE LE FORMULAIRE DE L'AGENCE
function verif_form_agence(){
	if(document.forms.form_agence.titre.value==''){
		alert('Veuillez saisir un titre');
		return;
	}
	if(document.forms.form_agence.texte_1.value==''){
		alert('Veuillez saisir un texte pour le paragraphe location');
		return;
	}
	if(document.forms.form_agence.texte_2.value==''){
		alert('Veuillez saisir un texte pour le paragraphe investissement');
		return;
	}
	if(document.forms.form_agence.texte_3.value==''){
		alert('Veuillez saisir un texte pour le paragraphe transaction');
		return;
	}

	document.forms.form_agence.submit();
}

//VERIFIE LE FORMULAIRE DE GESTION
function verif_form_gestion(){
	if(document.forms.form_gestion.titre.value==''){
		alert('Veuillez saisir un titre');
		return;
	}
	if(document.forms.form_gestion.texte.value==''){
		alert('Veuillez saisir un texte');
		return;
	}

	document.forms.form_gestion.submit();
}

//VERIFIE LE FORMULAIRE DES ACTUALITES
function verif_form_actu(){
	if(document.forms.form_actu.titre.value==''){
		alert('Veuillez saisir un titre');
		return;
	}
	if(document.forms.form_actu.sous_titre.value==''){
		alert('Veuillez saisir un sous-titre');
		return;
	}
	if(document.forms.form_actu.texte.value==''){
		alert('Veuillez saisir un texte');
		return;
	}

	document.forms.form_actu.submit();
}

//VERIFIE LE FORMULAIRE DE CONTACT
function verif_form_contact(){
	if(document.forms.form_contact.titre.value==''){
		alert('Veuillez saisir un titre');
		return;
	}
	if(document.forms.form_contact.texte.value==''){
		alert('Veuillez saisir un texte');
		return;
	}

	document.forms.form_contact.submit();
}

//VERIFIE LE FORMULAIRE DES MENTIONS LEGALES
function verif_form_legal(){
	if(document.forms.form_legal.titre.value==''){
		alert('Veuillez saisir un titre');
		return;
	}
	if(document.forms.form_legal.texte.value==''){
		alert('Veuillez saisir un texte');
		return;
	}

	document.forms.form_legal.submit();
}

//VEIRFIE LE FORMULAIRE DES BIENS
function verif_form_bien(){
	var formulaire = document.forms.form_bien;

	var variables = 'id_type='+formulaire.id_type.value+'&new_type='+formulaire.new_type.value+'&jour_dispo='+formulaire.jour_dispo.value+'&mois_dispo='+formulaire.mois_dispo.value+'&annee_dispo='+formulaire.annee_dispo.value;
	variables = variables+'&ref='+formulaire.ref.value+'&titre='+formulaire.titre.value+'&nb_pieces='+formulaire.nb_pieces.value+'&prix='+formulaire.prix.value+'&surface='+formulaire.surface.value;
	variables = variables+'&adresse='+formulaire.adresse.value+'&cp='+formulaire.cp.value+'&id_ville='+formulaire.id_ville.value+'&new_ville='+formulaire.new_ville.value;
	variables = variables+'&description_courte='+formulaire.description_courte.value+'&description='+formulaire.description.value+'&verif_champs=ok';
	
	var xhr = getXhr();
	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4 && xhr.status == 200){
			var reponse = xhr.responseText;
			if(reponse==1) document.forms.form_bien.submit();
			else alert(reponse);
		}
	}
	xhr.open('POST', 'bien_detail.php', true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send(variables);
}
