var code_sep = ',';
var infobCur = null;

function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();

}
MM_reloadPage(true);

function linkChoice(thisBox, i,nb,numTab) 
{
	var nameForm
    var k = (thisBox.name).substr(5,1);
	if (numTab == 0) {
		nameForm = "selection";
	} else {
		nameForm = "selection" + numTab;
	}
	
	if (i == 0) 
	{
		for (j = 1; j < nb; j++) 
		{
			var nomelt = "type_" + k + "_" +j;
			// alert("nomelt "+ nomelt);
			document.getElementById(nameForm).elements["type_" + k + "_" +j].checked =
				document.getElementById(nameForm).elements["type_" + k +"_0"].checked;
		}
  	}
  	else
  	{
		tmpBool = false;
		for (j = 1; j < nb; j++) 
		{
			tmpBool |= document.getElementById(nameForm).elements["type_" + k +"_" + j ].checked;
		}
		
		document.getElementById(nameForm).elements["type_" + k +"_0"].checked = tmpBool;
  	}
}


function linkChoice1(thisBox, i,nb) 
{
	
    var k = (thisBox.name).substr(5,1);
	var formulaire = document.selection1;
	
	if (i == 0) 
	{
		for (j = 1; j < nb; j++) 
		{
			var nomelt = "type_" + k + "_" +j;
			// alert("nomelt "+ nomelt);
			document.getElementById("selection1").elements["type_" + k + "_" +j].checked =
				document.getElementById("selection1").elements["type_" + k +"_0"].checked;
		}
  	}
  	else
  	{

		tmpBool = false;
		for (j = 1; j < nb; j++) 
		{
			tmpBool |= document.getElementById("selection1").elements["type_" + k +"_" + j ].checked;
		}
		
		document.getElementById("selection1").elements["type_" + k +"_0"].checked = tmpBool;
  	}
  
}


function setTypeBien() 
{
	var sTypeBien = "";
	for (k = 0; k < 5; k++) 
	{
		var choix = document.getElementsByName("type_" + k);
		
		for (i = 0; i < choix.length; i++) 
		{
	 		if (choix[i].checked && choix[i].value.indexOf('_') != -1) 
	 		{
        			sTypeBien += choix[i].value + ";";
	  		}
		}
	}
	if (sTypeBien == "") sTypeBien = 'NULL';	
	var typeBien =document.selection.elements['typeBien'];
	//var typeBien = document.getElementsByName("typeBien");
	typeBien.value = sTypeBien;
	alert ("valeur typeBien" + typeBien.value);	
	return true;
}


function parseTypeBien()
{
	//alert ("parseTypeBien");
	if (document.selection)
	{
		//alert ("document.selection");
		var sTypeBien = "" + document.selection.elements['typeBien'].value;
		alert ("sTypeBien" + sTypeBien);
		for (k = 0; k < 5; k++) 
		{
			var choix = document.getElementsByName("type_" + k);
			for (i = 1; i < choix.length; i++) 
			{
		 		if (sTypeBien.indexOf(choix[i].value) != -1) 
		 		{
	        			choix[i].checked = true;
		  		}
				linkChoice(choix[i], i);
			}
		}
	}
}


function updatecritere(type_trans) {
	var html;
	if (type_trans == 10 ) {		
		 html =	'<strong>Ancienneté : </strong>';
		 html +=	'<input name="radioAncien" type="radio" id="radioAncien" value="indifferent" checked="checked" />';       
		 html +=	' Indifférent ';
		 html +=	'<input type="radio" name="radioAncien" id="radioAncien" value="ancien" />';
		 html +=	' Ancien ';
		 html +=	' <input type="radio" name="radioAncien" id="radioAncien" value="recent" />';
		 html +=	' Récent ';
		 html +=	'<input type="radio" name="radioAncien" id="radioAncien" value="neuf" />';
		 html +=	' Neuf '; 
	} else {
		 html =	'<strong>   Vide/Meublé : </strong>';
		 html +='<input name="radioVide" type="radio" id="radioVide" value="vide" checked="checked" />';       
		 html +=	' Vide ';
		 html +=	'<input type="radio" name="radioVide" id="radioVide" value="meuble" />';
		 html +=	' meublé ';
	}
	var trans = document.getElementById("al_type_vac");
	if (type_trans != 30 ) {
		trans.style.display='none';
	} else trans.style.display='block';
	var recAncien = document.getElementById("recAncien");
	recAncien.innerHTML = html;
}




function ac_updatecritere(type_trans,elem) {
	var trans = document.getElementById(elem);
	if (type_trans != 30 ) {
		trans.style.display='none';
	} else trans.style.display='block';
}


function coloc(type_trans,elem) {
	var trans = document.getElementById(elem);
	var libel = document.getElementById("libelcoloc");
	if (type_trans != 2010 ) {
		trans.style.visibility='hidden';
		libel.style.visibility='hidden';
	} else {
		trans.style.visibility='visible';
		libel.style.visibility='visible';
	}
	
}


function displayPics()
{
		var photos = document.getElementById('galerie_mini') ;
		// On récupère l'élément ayant pour id galerie_mini
		var liens = photos.getElementsByTagName('a') ;
		// On récupère dans une variable tous les liens contenu dans galerie_mini
		var big_photo = document.getElementById('big_pict') ;
		// Ici c'est l'élément ayant pour id big_pict qui est récupéré, c'est notre photo en taille normal
		// Une boucle parcourant l'ensemble des liens contenu dans galerie_mini
		for (var i = 0 ; i < liens.length ; ++i) {
			// Au clique sur ces liens 
			liens[i].onmouseover = function() {
				big_photo.src = this.href; // On change l'attribut src de l'image en le remplaçant par la valeur du lien
				big_photo.alt = this.title; // On change son titre
				return false; // Et pour finir on inhibe l'action réelle du lien
			};
		}
}


function visibilityControl(action, elem, elem2, elem3) {
	switch(action) {
		
		case "auto" :
			if (document.getElementById( elem ).style.visibility =='visible') {
				document.getElementById( elem ).style.visibility='hidden';
				if (elem2 != "")
					document.getElementById( elem2 ).style.visibility='visible';
				if (elem3 != "")
					document.getElementById( elem3 ).style.visibility='visible';
			}
			else {
				document.getElementById( elem ).style.visibility='visible';
				if (elem2 != "")
					document.getElementById( elem2 ).style.visibility='hidden';
				if (elem3 != "")
					document.getElementById( elem3 ).style.visibility='hidden';
			}
			break;
		case "show" :
			document.getElementById( elem ).style.visibility='visible';
			if (elem2 != "")
				document.getElementById( elem2 ).style.visibility='hidden';
			if (elem3 != "")
				document.getElementById( elem3 ).style.visibility='hidden';
			break;

		case "hide" :
			document.getElementById( elem ).style.visibility='hidden';
			if (elem2 != "")
				document.getElementById( elem2 ).style.visibility='visible';
			if (elem3 != "")
				document.getElementById( elem3 ).style.visibility='visible';
			break;
	}
}


function addVal(elem, valeur, method) {
	
	switch (method) {
		
		case "equ" :
			document.getElementById( elem ).value = value;
			// alert ("addval " + elem + value + method);
			break;
		
		case "auto" :
			var str = document.getElementById( elem ).value;
			var pattern = eval('/'+valeur+'/i');
			//var pattern = eval('/'+valeur+'/');
			// alert ("str :'" + str + "' pattern :" + pattern + "valeur :" +valeur+":" );
			// alert (pattern.test(str));
			
			if ( pattern.test(str) ) {
				var new_str = str.replace(pattern, '');
				document.getElementById( elem ).value = new_str;
			} else {
				if (elem == 'type_biens_enter') {
					if (valeur == 'Indifferent,') {
						document.getElementById('Indifferent').checked = true;
						document.getElementById( elem ).value += valeur;
					} else {
						var new_str = str.replace('Indifferent,', '');
						document.getElementById('Indifferent').checked = false;
						document.getElementById( elem ).value = new_str + valeur;
					}
				} else {
					document.getElementById( elem ).value += valeur;
				}
			}
			break;
		
	}
}


function verif_formulaire()
{
 if(document.getElementById("loc").value == "")  {
   alert("Veuillez entrer une localisation!");
   document.critereSelection.focus();
   return false;
  }
}


function infobMove(e) {
	if (!e) e=event;
	if (e.pageX) {
		l = e.pageX+document.documentElement.scrollLeft+30;
		t = e.pageY+document.documentElement.scrollTop+20;
		//alert ("page");
	}
	else
	{
		//document.documentElement 
		l=e.clientX+document.documentElement.scrollLeft-70;
		t=e.clientY+document.documentElement.scrollTop-170;
		//alert("client");
	}
	
	//alert (t + " " + l);	
	if(infobCur!=null) {
//		infobCur.innerHTML=l+" top"+t;
		//infobCur.style.top=t-170;
		//infobCur.style.left=l-70;
		infobCur.style.top=t;
		infobCur.style.left=l;
		infobCur.style.display='inline';
		infobCur.style.visibility='visible';


	}
}


function infobDisp(oo) {
	var o = 	document.getElementById(oo);
	if(o) {
		infobCur=o;
		//o.style.display='inline';
		//o.style.visibility='visible';
		document.onmousemove=infobMove;
	}
}


function infobHide(oo) {
	var o = document.getElementById(oo);
	if(o) {
		document.onmousemove='';
		infobCur=null;
		o.style.display='none';
		o.style.visibility='hidden';
	}
}


function focuscp (o) {
	if (!o) return;
	if (o.value==defaultfocuscp) {
		o.value="";
		o.style.color="black";
	}
}


function blurcp (o) {
	if (!o) return;
	if (o.value=="" || o.value==defaultfocuscp) {
		o.style.color="gray";
		if (o.value=="") o.value=defaultfocuscp;
	}
	else
		o.style.color="black";
}




function focusinput (o) {
	if (o.value==defaultfocusinput) {
		o.value="";
		o.style.color="black";
	}
}


function blurinput (o) {
	if (o.value=="" || o.value==defaultfocusinput) {
		o.style.color="gray";
		if (o.value=="") o.value=defaultfocusinput;
	}
	else
		o.style.color="black";
}


function putFrame(theDiv){
	var f    = document.createElement("iframe");
	var d    = document.getElementById(theDiv);


	with(f.style){
		position	= "absolute";
	 	width    	= d.offsetWidth+"px";
	  	height		= d.offsetHeight+"px";;
	 	top			= d.offsetTop+"px";
	  	left		= d.offsetLeft+"px";
	   	zIndex		= "1";
	}


	document.body.appendChild(f);
}


function imprimer(ann_id,infos) {
	// Ouvrir une popup avec les informations puis mettre bouton lien pour imprimer
	// ouvre une fenetre sans barre d'etat, ni d'ascenceur
	w=open("",'popup','width=400,height=200,toolbar=no,scrollbars=no,resizable=yes');	
	w.document.write("<TITLE>Imprimer votre annonce "+ann_id+"</TITLE>");
	w.document.write("<BODY> Bonjour <BR><BR>");
	w.document.write("Ce popup n'est pas un fichier HTML, il est écrit directement par la fenêtre appelante");
	w.document.write(infos);
	var longueur=infos.length;
	w.document.write("longueur du tableau "+longueur);
	w.document.write('<a href="javascript:window.print()">Imprimer</a>');
	w.document.write("</BODY>");
	w.document.close();
	
}
function noPriseEnCompte(urlB) {
	// alert ('noPriseEnCompte');
	var options = "Width=300,Height=200,top=300,left=300'" ;
    var nomProc = urlB+"nonPriseEnCompte.php";
    // alert (nomProc);
    window.open(nomProc,"langues",options) ;
}
function edition(ann_id,urlB)
    {
    var options = "Width=700,Height=730" ;
    var nomProc = urlB+"imprimer.php?code_ann="+ann_id;
    // alert (nomProc);
    window.open(nomProc,"impression",options) ;
    }
function condition(urlB)
    {
    var options = "Width=675,Height=530" ;
    var nomProc = urlB+"CG.html";
    // alert (nomProc);
    window.open(nomProc,"conditions",options) ;
    }


function open_fact(cmd_id)
    {
    options = "Width=800,Height=730" ;
    var nomProc = "facture.php?code_cmd="+cmd_id;
    window.open( nomProc, "facture", options ) ;
    }


function imprimer_fact()
    {
    options = "Width=800,Height=730" ;
    var nomProc = "imprimerFactures.php";
    window.open( nomProc, "factures", options ) ;
    }
    
function affichePopup(sPage, nLargeur, nHauteur) {
		window.wdwChoix = window.open(sPage,'choix','WIDTH=' + nLargeur + ',HEIGHT=' + nHauteur + ',SCROLLBARS=YES,RESIZABLE=YES');
	}
    
function ChangeImage( idImage , UrlImage ) {
	document.getElementById( idImage ).src = UrlImage;
	document.getElementById( idImage ).alt = UrlImage;
}


function modifierCritere()
{ 
	document.getElementById("etape").value = "modifier2"; 
	document.envoiMail.submit();
} 


function modifierCritere2() { 
	
	document.getElementById("etape2").value = "modifier";
	document.getElementById("etat").value = "modif"; 
	document.validerMail.submit();
} 


function soumissionLocation(ident) {
	document.getElementById(ident).value = "20";
	sablier();
	if (ident == "trans1") {
		document.standard.submit();
	} else {
		document.confort.submit();
	}
}


function soumissionAlerte() {
	document.getElementById("etape").value = "listeAlerte";
	// alert (document.getElementById("etape").value);
	document.alerte.submit();
	
}


function soumissionPswd() {
	// alert ("soumissionPswd");
	document.getElementById("etape5").value = "modPswd";
	// alert (document.getElementById("etape5").value);
	document.espace.submit();
}


function soumissionDepot(ident) {
	// alert ('soumissionDepot' + ident);
	if (ident == "location") {
		document.getElementById("trans1").value = "20";
	}
	if (ident == "vacances") {
		document.getElementById("trans1").value = "30";
		document.getElementById("module1").value = "deposerAnnonce_vac";
	}
	document.depot.submit();
}


function extraction (Chaine) {
	var Resultat = Chaine.indexOf(' ');
	alert (Resultat);
}


function Left(str, n){
		alert ("Left" + str + " " + n );
		if ( n < 0) {
	       return "";
		}
	    else {
			if (n > str.length) {
				 return str;
			}
			else {
				var res = str.substring(0,n);
				alert ("res " + res);
				return res;
			}
			   
	    }
	}
	
function TronqueItem( strItem ) {


		// recherche du premier espace
		// ---------------------------
		var iFirstSpace = String( strItem ).indexOf( ' ' );
		alert (iFirstSpace);
		if ( iFirstSpace >= 0 ) {
			// ne renvoie que la gauche sans l'espace
			// (index base 0, Left base 1)
			// --------------------------------------
			return Left( strItem, iFirstSpace );
		} else {
			return strItem;
		} // endif


	} // TronqueItem
	
	function cacher()
	{
		id = document.getElementById('annee').options.selectedIndex;
		// alert ("cacher " + id);
		idSelected = document.getElementById('annee').value;
		// alert ("cacher " + idSelected);
		if (id == 0 ) {
			document.getElementById('year').style.display = 'block';
			document.getElementById('yearNext').style.display = 'none';
			document.getElementById('yearT').style.display = 'block';
			document.getElementById('yearNextT').style.display = 'none';
		} else {
			document.getElementById('year').style.display = 'none';
			document.getElementById('yearNext').style.display = 'block';

			document.getElementById('yearT').style.display = 'none';
			document.getElementById('yearNextT').style.display = 'block';
		}
	}
	
	function  toutCocher(elmId,nbsem) {
		// alert ("Toutcocher " + elmId + " " + nbsem);
		if (elmId == "cb_1" ) {
			i = 1;
		} else {
			i = 2;
		}
		if (document.getElementById(elmId).checked == true) {
			for (cpt_sem = 1 ; cpt_sem < nbsem ; cpt_sem++) {
				indice = "a" + i + "_dispo_" + cpt_sem;
				document.getElementById(indice).checked=true; 
			}
		}

		else {
			for (cpt_sem = 1 ; cpt_sem < nbsem ; cpt_sem++) {
				indice = "a" + i + "_dispo_" + cpt_sem;
				document.getElementById(indice).checked=false; 
			}
		}
	}
	
	function verifTarif (id) {
		//extraire n°
		var numId = id.substring(6);
		var idNc = "nc_" + numId;
		if (document.getElementById(id).value == "") {
			document.getElementById(idNc).checked = true;
		} else {
			document.getElementById(idNc).checked = false;
		}
		
	}
	
	function verifNC (id) {
		var numId = id.substring(3);
		var idTarif = "tarif_" + numId;
		// alert ("idTarif" + idTarif);
		if (document.getElementById(id).checked == true) {
			// alert ("case cocher");
			document.getElementById(idTarif).value = "";
		} 
	}
	
	function changeBGcolor(idType){
		document.getElementById(idType).style.background = 
		document.getElementById(idType).options[document.getElementById(idType).selectedIndex].style.background;
	 
	}
	
	function confirmerSuppression(msg,urldel) {
		if (confirm(msg) ) {
			// aller vers url qui fait la suppression sinon rien
			location.href=urldel;
		}
	}
	
	function confirmerSuppressionForm(msg,nom) {
		if (confirm(msg) ) {
			// aller vers url qui fait la suppression sinon rien
			document.getElementById(nom).submit();
		}
	}
	
	function sablier () {
		document.getElementsByTagName('body')[0].style.cursor = 'wait'; //sablier
	}
	
	function flecheClassique () {
     document.getElementsByTagName('body')[0].style.cursor = 'default' //fleche classique
	}
	
	function modifierHautBlanc (haut) {
		document.getElementById('blanc').style.height = haut+'px';
	}
	
	function modifierlargBlanc (larg) {
		var largCentre = larg -5;
		document.getElementById('blanc').style.width = larg+'px';
		document.getElementById('centre').style.width = largCentre +'px';
	}
	
	function init_code_action(ident) {
		// mettre à jour code_action
		var code = document.getElementById(ident).value;
		if (code != "") {
			document.getElementById('code_action203').value = code;

			document.getElementById('code_action206').value = code;
			document.getElementById('code_action2012').value = code;
			document.getElementById('code_action503').value = code;
			document.getElementById('code_action506').value = code;
			document.getElementById('code_action5012').value = code;
			document.getElementById('code_actionIL3').value = code;
			document.getElementById('code_actionIL6').value = code;
			document.getElementById('code_actionIL12').value = code;
			
		}
	}
	
	// Ajoute la fonction trim comme méthode de l'objet String.
		String.prototype.trim = function()
		{ return this.replace(/(^\s*)|(\s*$)/g, ""); }

	function envoiAff(formulaire) {
		 //alert ("envoiAff");
		 var code = document.getElementsByName("code_trans")[0];
		 var trans = code.options[code.selectedIndex].value 
		 //alert ("trans " + trans)
		 var type_trans = "Immobilier";
		 if (trans == 10) type_trans = "achat";
		 if (trans == 20) type_trans = "location";
		 if (trans == 30) type_trans = "location-vacances";
		 if (trans == 1010) type_trans = "vente-neuf";
		 //alert ("type_trans"+ type_trans);
		 var val = document.getElementById("loc").value;
		 val = RemoveAccents(val);
		 val = ReplaceCaracBlancTiret(val);
		 //val = ReplaceCarac(val);
		 var tab_val = val.split(",");
		 var valN = "";
		 for (i=0;i<tab_val.length;i++) {
		 	//alert ("i " + i +  tab_val[i] );
		 	if (i > 0) valN += ",";
		 	valN += tab_val[i].trim();
		 }
		 if (valN == "") valN = val;
		 //alert ("loc" + valN)
		
		 var code = document.getElementById("biens");
		 var valB = code.options[code.selectedIndex].value;
		 //alert ("type de bien " + valB);
		 var action = "";
		 if (val != "") {
			action += valN + "/";
			if (valB != "") {
				action += valB + "/";
			 }
			 action += type_trans+".html";
		 } else {
		 	if (valB != "") {
				action += type_trans.toLowerCase() + '-' + valB +"-tb.html";
			} else {
				if (type_trans == "achat") type_trans = 'vente';
				action += "annonces-" + type_trans.toLowerCase() +".html";
			}
		 }
		 
		
		 
		 //formulaire.action="index.php?localisation="+val+"&module=afficherAnnonces";
		 var debut;
		 if (valN == "" ) {
		 	debut = "";
		 } else {
		 	debut = "annonces.immobilieres/"
		 	if (valB == "") debut = "annonces-immobilieres/";
		 }
		 formulaire.action= debut+action;
		 formulaire.submit();
		 return(true);
	}
	
	function RemoveAccents(Texte){
		var Accents = 'àâäéèêëïïöôûüù';
		var NoAccen = 'aaaeeeeiioouuu';
		 
		Accents = Accents.split('');
		NoAccen = NoAccen.split('');
		var i=0;
		while(Accents[i]){
			var Reg=new RegExp(Accents[i],'gi');
		    Texte=Texte.replace(Reg,NoAccen[i]);
		    i++
		}
		return Texte;
	}                  	
	function ReplaceCarac(Texte){
		var Accents = '-';
		var NoAccen = ' ';
		 
		Accents = Accents.split('');
		NoAccen = NoAccen.split('');
		var i=0;
		while(Accents[i]){
			var Reg=new RegExp(Accents[i],'gi');
		    Texte=Texte.replace(Reg,NoAccen[i]);
		    i++
		}
		return Texte;
	} 
	function ReplaceCaracBlancTiret(Texte){
		var Accents = ' ';
		var NoAccen = '-';
		 
		Accents = Accents.split('');
		NoAccen = NoAccen.split('');
		var i=0;
		while(Accents[i]){
			var Reg=new RegExp(Accents[i],'gi');
		    Texte=Texte.replace(Reg,NoAccen[i]);
		    i++
		}
		return Texte;
	}                        
	function envoi(formulaire) {
		 var val = document.getElementById("loc").value;
		 val = RemoveAccents(val);
		 //val = ReplaceCarac(val);
		 var tab_val = val.split(",");
		 var valN = "";
		 for (i=0;i<tab_val.length;i++) {
		 	//alert ("i " + i +  tab_val[i] );
		 	if (i > 0) valN += ",";
		 	valN += tab_val[i].trim();
		 }
		 if (valN == "") valN = val;
		 //enlever les blancs
		 //val = val.trim();
		 var trans10 = document.getElementById("trans10").checked;
		 var trans20 = document.getElementById("trans20").checked;
		 var trans30 = document.getElementById("trans30").checked;
		 var trans1010 = document.getElementById("trans1010").checked;
		 var type_trans = "Vente,Achat,Location,Location_Vacances";
		 if (trans10) type_trans = "Vente,Achat";
		 if (trans20) type_trans = "Location";
		 if (trans30) type_trans = "Vacances_Location";
		 if (trans1010) type_trans = "Vente_Neuf";
		 var valBiens = document.getElementById("type_biens_enter").value;
		 var valB = "";
		 if (valBiens != "Indifferent,") { 
			 var tab_bien = valBiens.split(",");
			 for (i=0;i<tab_bien.length;i++) {
			 	//alert ("i " + i +  tab_val[i] );
			 	if (i > 0) valB += ",";
			 	valB += tab_bien[i].trim();
			 }
		 }
		 if (valB == "Indifferent,") valB="";
		 var action = "";
		 if (val != "") {
			action += valN + "/";
		 }
		 if (valB != "") {
			action += valB + "/";
		 }
		
		 action += type_trans+".html";
		 //formulaire.action="index.php?localisation="+val+"&module=afficherAnnonces";
		 var debut;
		 if (valN == "" && valB == "") {
		 	debut = "annonces/";
		 } else {
		 	debut = "annonces.immobilieres/"
		 	if (valB == "") debut = "annonces-immobilieres/";
		 	if (valN == "") debut = "annonces_immobilieres/";
		 }
		 formulaire.action= debut+action;
		 
		 formulaire.submit();
		 return(true);
	}
	
	