
function changeImage(compt, index, src, maxWidth, maxHeight){
   	var img=document.getElementById('img'+compt);
   	img.src=src;
    imgWidth=document.getElementById("width_"+index+"_"+compt).value;
    imgHeight=document.getElementById("height_"+index+"_"+compt).value;
    if(maxWidth/imgWidth * imgHeight <= maxHeight){
    	img.width=maxWidth;
    	img.height=maxWidth/imgWidth * imgHeight;
    	return;
    }
    if(maxHeight/imgHeight * imgWidth <= maxWidth){
    	img.width=maxHeight/imgHeight * imgWidth;
    	img.height=maxHeight;
    	return;    	
    }
    img.width=imgWidth;
    img.height=imgHeight;
}

function supprimerAnnonceForm(i, id){

  var td = document.getElementById('td'+i);
  td.style.display="";

  var contenu ="<p class=\"pIdentification\">Identification</p>";
      contenu +="<p style=\"text-align:justify;\">Veuillez saisir votre e-mail et votre mot de passe afin de supprimer votre annonce :" 
      contenu +="<table border='0' width='100%'><tr>";
      contenu +="<td>Votre e-mail : </td><td><input type='text' id='email"+i+"' value='' maxlength='40' size='45'></td></tr>";
      contenu +="<tr><td>Mot de Passe : </td><td><input type='text' value='' id='mdp"+i+"' maxlength='10' size='20'></td></tr>";
      contenu +="<tr><td>&nbsp;</td><td></td></tr>";
      contenu +="<tr><td>&nbsp;</td><td></td></tr>";
      contenu +="<tr><td><input type='button' value='Supprimer' id='b"+i+"' ";
      contenu +="onclick=\"supprimerAnnonce('" + id + "',document.getElementById('email"+i+"').value, document.getElementById('mdp"+i+ "').value, '"+i+"' )\" />";

      contenu +="</td><td align='right'><span onmouseover=\"this.style.cursor='pointer'\"  onclick=\"document.getElementById('td" + i + "').style.display='none'\"><u>Fermer</u></span></td></tr></table>";
      contenu +="<p id='p" + i + "'></p>";

	   td.innerHTML = contenu;

}




function modifierAnnonceAutentificationForm(i, id){
  var td = document.getElementById('td'+i);
  td.style.display="";

  var contenu ="<p class=\"pIdentification\">Identification</p>";
      contenu +="<p style=\"text-align:justify;\">Veuillez saisir votre e-mail et votre mot de passe afin de modifier votre annonce :" 
      contenu +="<table border='0' width='100%'><tr>";
      contenu +="<td>Votre e-mail : </td><td><input type='text' id='email"+i+"' value='' maxlength='40' size='45' ></td></tr>";
      contenu +="<tr><td>Mot de Passe : </td><td><input type='text' value='' id='mdp"+i+"' maxlength='10' size='20'></td></tr>";
      contenu +="<tr><td>&nbsp; </td><td> </td></tr>";
      contenu +="<tr><td>&nbsp; </td><td> </td></tr>";
      contenu +="<tr><td><input type='button' value='Validez' id='b"+i+"' ";
      contenu +="onclick=\"modifierAnnonce('" + id + "',document.getElementById('email"+i+"').value, document.getElementById('mdp"+i+ "').value, '"+i+"' )\" />";

      contenu +="</td><td align='right'><span onmouseover=\"this.style.cursor='pointer'\"  onclick=\"document.getElementById('td" + i + "').style.display='none'\"><u>Fermer</u></span></td></tr></table>";
      contenu +="<p id='p" + i + "'></p>";

	   td.innerHTML = contenu;

//onblur='if(!verifEmail(this.value)) alert(L\'adresse e-mail n\'est pas valide!);'
}




function envoyerMessageAuProprietaireForm(i, ownerEmail, titre){
 
  var td = document.getElementById('td'+i);
  td.style.display="";
  var contenu  ="<table border='0' width='100%'><tr>";
      contenu +="<td>Votre e-mail : </td><td><input type='text' id='email"+i+"' value='' maxlength='40' size='45'></td></tr>";
      contenu +="<tr><td align='center'>Votre message : </td><td><textarea cols='35' rows='10' id='msgToSend" +i+"'>Tapez votre message</textarea></td>";
      contenu +="</tr><tr><td><input type='button' value='Envoyer' id='b"+i+"' ";
      contenu +="onclick=\"registerMessageToOwner('" +ownerEmail+ "', '" +addslashes(titre)+ "', " + "document.getElementById('email" + i + "').value, ";
      contenu +=" document.getElementById('msgToSend" + i + "').value, " + "'" + i + "' ) \" />";
      
      contenu +="</td><td align='right'><span onmouseover=\"this.style.cursor='pointer'\"  onclick=\"document.getElementById('td" + i + "').style.display='none'\"><u>Fermer</u></span></td></tr></table>";
      contenu +="<p id='p" + i + "'></p>";


   td.innerHTML = contenu;


}


function validationAnnonce (){

 if(! verifEmail(document.getElementById('email').value)){
    messageUnvalidEmail();
    return false;
 }
 
 if( !verifInt(document.getElementById('telephone').value) ){
    alert("Le numÃ©ro du tÃ©lÃ©phone n'est pas valide!");
   return false;
 }
 
 if(document.getElementById("mdp").value == ""){
   alert("Veuillez saisir un mot de passe!");
   return false; 
 }

 if(document.getElementById("cmdp").value == ""){
   alert("Veuillez confirmer votre mot de passe!");
   return false; 
 }
 
 if(document.getElementById("mdp").value != document.getElementById("cmdp").value ){
   alert("Veuillez saisir un mot de passe et le confirmer!");
   return false; 
 }


 if( rlTrim(document.getElementById('codePostal').value)=="" || !verifInt(document.getElementById('codePostal').value) ){
    alert("Le code postal doit contenir 5 chiffres!");
   return false;
 }
 
 if( rlTrim(document.getElementById('titreAnnonce').value) == "" ){
    alert("Veuillez saisir le titre de votre annonce!");
   return false;
 }
 
  if(document.getElementById('typeBien').value == 0){
    alert("Veuillez sÃ©lectionner le type de votre bien!");
    return false;
  } 

  if( !verifInt(document.getElementById('pieces').value) ){
    alert("Le numbre des piÃ¨ces n'est pas valide!");
   return false;
 }

 if( !verifFloat(document.getElementById('surface').value) ){
    alert("La surface n'est pas valide!");
   return false;
 }
 
 if( !verifInt(document.getElementById('prix').value) ){
    alert("Le prix n'est pas valide!");
   return false;
 }
 
 return true;
 
 
}


function launchReaserchAnnonce(e){
	var code=0;
	if(e.which) //Mozilla
		code=e.which;
	else if(e.keyCode) //IE
		code=e.keyCode;
	
	if(code != 13)
	  return 
	
	document.getElementById('dateAlerte').value=''; 
	chercher(0);
}

function consulterAnnonce(){

 //Critères de recherche
              
	  var text ="<input type=\"hidden\" name=\"dateAlerte\" value=\"\" id=\"dateAlerte\">"; 
           
            text +="<table id =\"tableConsult\" border=\"0\">";


            text +="<tr><td colspan='9' class='firstLineChercherAnnonce'></td></tr>";
            text +="<tr>";
             text +="<td class='intituleChercherAnnonce'>";
	      text +="Type d'annonce"; 
	     text +="</td>";	
             text +="<td class='intituleChercherAnnonce'>:</td>";
             text +="<td>";
	      text +="<input type='radio' name='typeAnnonce' value='offre' id='typeAnnonce1' checked>Offre";
              text +="<input type='radio' name='typeAnnonce' value='demande' id='typeAnnonce2'>Demande";
             text +="</td>"; 
	   
             text +="<td class='intituleChercherAnnonce'>Cat&eacute;gorie</td>";
             text +="<td class='intituleChercherAnnonce'>:</td>";
             text +="<td>";
	      text +="<input type='radio' name='categorie' value='V' id='categorie1' checked>Vente";
              text +="<input type='radio' name='categorie' value='L' id='categorie2'>Location";
             text +="</td>"; 

//Particulier ou pro
            text +="<td class='intituleChercherAnnonce'>Afficher les annonces des&nbsp;</td>";
            text +="<td class='intituleChercherAnnonce'>:</td>";  
            text +="<td class='rightChercherAnnonce'><input type='radio' name='parOuPro' value='par' id='par'>Particuliers";
	    text +="<input type='radio' name='parOuPro' value='pro' id='pro'>Professionnels";
	    text +="<input type='radio' name='parOuPro' value='tous' id='tous' checked>Tous</td></tr>";

 
//Type du bien 
        text +="<tr><td class='intituleChercherAnnonce'>Type du bien</td>";
        text +="<td class='intituleChercherAnnonce'>:</td>";
        text +="<td><select name='typeBien' id='typeBien'>";
    text +="<option value='0' id='typeBien0'>Choisissez</option>";
    text +="<option value='1' id='typeBien1'>Studio</option>";
    text +="<option value='2' id='typeBien2'>Appartement</option>";
    text +="<option value='3' id='typeBien3'>Maison</option>";
    text +="<option value='4' id='typeBien4'>Terrain</option>";
    text +="<option value='5' id='typeBien5'>Parking</option>";
    text +="<option value='6' id='typeBien6'>Garage</option>";
    text +="<option value='7' id='typeBien7'>Autre</option>";
    text +="</select></td>";

 //Code Postal

    text +="<td class='intituleChercherAnnonce'>Code Postal</td>";
    text +="<td class='intituleChercherAnnonce'>:</td>";
    text +="<td><input type='text'  class='text' name='codePostal' value='' maxlength='5' size='10' id='codePostal'";
    text +=" onKeyPress='launchReaserchAnnonce(event);' ";
    text +=" onBlur='if(!verifInt(this.value)) {alert(\"Le code postal ne doit contenir que 5 chiffres\"); this.value=\"\";}'></td>"; 

 //Ville

    text +="<td class='intituleChercherAnnonce'>Ville</td>";
    text +="<td class='intituleChercherAnnonce'>: </td>";
    text +="<td class='rightChercherAnnonce'><input type='text'  class='text' name='ville' value='' maxlength='50' size='30' id='ville' /></td></tr>"; 

//Nombre de piÃ¨ces
	    text +="<tr>";
             text +="<td class='intituleChercherAnnonce'>Pi&egrave;ces min</td>";
             text +="<td class='intituleChercherAnnonce'>:</td>";
             text +="<td>" + pieceMinList()  +"</td>";

             text +="<td class='intituleChercherAnnonce'>Surface min</td><td class='intituleChercherAnnonce'>:</td><td>" + surfaceMinList()+"</td>";

             text +="<td class='intituleChercherAnnonce'>prix min</td><td class='intituleChercherAnnonce'>:</td><td class='rightChercherAnnonce'>";
             text +="<input type='text' class='text' name='prixMin' value='' maxlength='7' size='10' id='prixMin'  onBlur='if(!verifInt(this.value)) {alert(\"Le prix ne doit contenir que des chiffres\"); this.value=\"\";}' >" +"</td>"; 
            text +="</tr>";
	    text +="<tr>";
             text +="<td class='intituleChercherAnnonce'>Pi&egrave;ces max</td><td class='intituleChercherAnnonce'>:</td><td>"  + pieceMaxList()  +"</td>";
 
             text +="<td class='intituleChercherAnnonce'>Surface max</td><td class='intituleChercherAnnonce'>:</td><td>" + surfaceMaxList() +"</td>";

             text +="<td class='intituleChercherAnnonce'>prix max</td><td class='intituleChercherAnnonce'>:</td><td class='rightChercherAnnonce'	>";
             text +="<input type='text'  class='text' name='prixMax' value='' maxlength='7' size='10' id='prixMax'  onBlur='if(!verifInt(this.value)) {alert(\"Le prix ne doit contenir que des chiffres\"); this.value=\"\";}'  >" +"</td>"; 
            text +="</tr>";



          


//tri
            text +="<tr><td class='intituleChercherAnnonce'>Trier les r&eacute;sultats par</td>";
            text +="<td class='intituleChercherAnnonce'>:</td>";
            text +="<td class='intituleChercherAnnonce'><select name='tri' id='tri'>";
            text +="<option value='creationD DESC'>Date de cr&eacute;ation</option><option value='prix, creationD DESC'>Prix</option></select></td>";
            text +="<td class='intituleChercherAnnonce'>&nbsp;</td><td class='intituleChercherAnnonce'>&nbsp;</td><td class='intituleChercherAnnonce'>&nbsp;</td>";
            text +="<td class='intituleChercherAnnonce'></td><td class='intituleChercherAnnonce'>";
            text +="</td><td align='right' class='rightChercherAnnonce'><input type='button' value='CHERCHER' onclick=\"";
            text +="document.getElementById('dateAlerte').value=''; chercher(0);\" class='bouton'></td></tr>";
            text +="<tr><td colspan='9' class='lastLineChercherAnnonce'></td><tr>";

	    text +="<tr style='display: none;'><td class='createAlert' colspan='8' >Ajoutez votre e-mail si vous souhaitez cr&eacute;er une alerte";
            text += "&nbsp;:&nbsp;";
            text +="<input type='text'  value='' maxlength='40' size='45' id='emailAlerte'>";

            text +="&nbsp;<input type='button' value='Cr&eacute;er alerte' onclick='createAlert();' class='bouton'></td></tr>";

            //text +="<tr><td colspan='9' class='lastLineChercherAnnonce'></td><tr>";
          text +="</table>";
            

        clearBlock();
	document.getElementById("formulRecherche").innerHTML = text;
   }



function clearBlock(){
 
 document.getElementById('formulRecherche').innerHTML="";
 document.getElementById("annoncesTrouvees").innerHTML="";
 
 var o = document.getElementById("message"); 
 if(o)
  o.innerHTML = "";
}

function showAnnonce(id, titre, img1, img2, img3, prix, pieces, surface, annonce, typeBien, type, email, telephone, codepostal, parOuPro, mdp, ville){

 document.form.action="?a=3&id="+id;
 
 if(type == "V"){// offre vente
    document.getElementById('typeAnnonce1').checked = true;
    document.getElementById('categorie1').checked = true;
 }else if (type== "L"){// offre location
    document.getElementByid('typeAnnonce1').checked = true;
    document.getElementById('categorie2').checked = true;
 }else if(type == "RV"){// demande vente
    document.getElementById('typeAnnonce2').checked = true;
    document.getElementById('categorie1').checked = true;
 }else{//demande location
    document.getElementById('typeAnnonce2').checked = true;
    document.getElementById('categorie2').checked = true;
 }
 if(parOuPro == 'par')
    document.getElementById('parOuPro1').checked=true;
 else
    document.getElementById('parOuPro2').checked=true;

 document.getElementById('email').value=email;
 document.getElementById('telephone').value=telephone;
 document.getElementById('mdp').value=mdp;
 document.getElementById('cmdp').value=mdp;
 document.getElementById('codePostal').value=codepostal;
 showVille(codepostal, ville);
 document.getElementById('titreAnnonce').value=titre;

 

 document.getElementById('typeBien').selectedIndex=typeBien;

 document.getElementById('pieces').value=pieces;
 document.getElementById('surface').value=surface;
 var  reg=new  RegExp("(#XX#)", "g");
 annonce=annonce.replace(reg, "\n");
 reg=new  RegExp("(#YY#)", "g");
 annonce=annonce.replace(reg, "\r");

 document.getElementById('annonce').value=annonce;
 document.getElementById('prix').value=prix;
 
 
 if(img1 != ""){

        var o = document.getElementById('tdimg1');
        o.innerHTML = "<img src='images/"+img1+"' width='80'/>"+"<input type='hidden' name='bimg1' value='"+img1+"' />";
      
 }
 
 if(img2 != ""){

        var o = document.getElementById('tdimg2');
        o.innerHTML = "<img src='images/"+img2+"' width='80'/>"+"<input type='hidden' name='bimg2' value='"+img2+"' />";
 }

 if(img3 != ""){

        var o = document.getElementById('tdimg3');
        o.innerHTML = "<img src='images/"+img3+"' width='80'/>"+"<input type='hidden' name='bimg3' value='"+img3+"' />";

 }

}


function initialiserFormuleRecherche(prixMin,prixMax,piecesMin,piecesMax,surfaceMin,surfaceMax,annonceCreationD,idTypeBien,typeAnnonce,codepostal,parOuPro){
  if(prixMin > 0)
    document.getElementById('prixMin').value=prixMin;
  else
    document.getElementById('prixMin').value="";

  document.getElementById('prixMax').value=(prixMax > 0)?prixMax:"";
  document.getElementById('codePostal').value=(codepostal != "")?codepostal:"";
  if(typeAnnonce.length == 2)
    document.getElementById('typeAnnonce2').checked=true;
  if(typeAnnonce == "RL" || typeAnnonce == "L")
        document.getElementById('categorie2').checked=true;

  if(parOuPro == "pro")
        document.getElementById('pro').checked=true;
  else if(parOuPro =="tous")
        document.getElementById('tous').checked=true;

  
  document.getElementById('dateAlerte').value=annonceCreationD;
 
  document.getElementById('piecesMin'+piecesMin).selected=true;
  document.getElementById('piecesMax'+piecesMax).selected=true;
  document.getElementById('surfaceMin'+surfaceMin).selected=true;
  document.getElementById('surfaceMax'+surfaceMax).selected=true;
  document.getElementById('typeBien'+idTypeBien).selected=true;


}

function alternar_banner(contador){ 
    window.document["banner"].src = array_img[contador].src
    var linkPub = window.document.getElementById("linkPub");
    linkPub.href=array_url[contador];
    //window.document.links[0].href = array_url[contador] 
    contador ++ 
    contador = contador % array_img.length 
    setTimeout("alternar_banner("+contador+")",10000) 
}

