// JavaScript Document
function openNewWindow(URLtoOpen, windowName, windowFeatures) {
newWindow=window.open(URLtoOpen, windowName, windowFeatures); }

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_validateEmail(email,user) {
  var toto,hack, hack2;
  toto=email.value;
  p=toto.indexOf('@');
  q=toto.indexOf('.');
  hack=toto.indexOf('location.com');
  hack2=user.value.indexOf('location.com');
  if (email.value != ""){
  if (p==-1 || q==-1)  return '- '+email.name+' doit contenir une adresse e-mail valide.\n';
  if ((hack>0) || (hack2>0))  return ' - accès refusé!\n';
  }
}

function MM_validateTel(tel) {
  var i,j,ch,allValid=true,checkOK = '0123456789',allNum = '';
  for (i = 0;  i < tel.value.length;  i++){
        if ((i==2) || (i==5)){
                if (tel.value.charAt(i) != '-'){
                        allValid = false;
                }
        }  else {
                ch = tel.value.charAt(i);
                for (j = 0;  j < checkOK.length;  j++)
                        if (ch == checkOK.charAt(j)) break;
                        if (j == checkOK.length){
                                allValid = false;
                                break;
                        }
                        if (ch != ",")allNum += ch;
        }       
  }
  if ((!allValid) || ((tel.value.length != 8) && (tel.value.length != 0))){ return '- le champ téléphone doit être de la forme nn-nn-nn.\n';}
}

function MM_validateFormContactAnnonce() { //v4.0
  //utilise dans annonces-in.php
  var nom, email,tel,errors='',args=MM_validateFormContactAnnonce.arguments;
  nom=MM_findObj(args[0]);
  email=MM_findObj(args[1]);
  tel=MM_findObj(args[2]);
  if (nom.value == ""){
  errors+='- le champ Nom doit être renseigné.\n';
  }
  if ((tel.value == "") && (email.value == "")){ errors+= "- vous devez choisir un moyen de contact: email ou téléphone.\n";}
  // test de l'email
  if (email.value != ""){
    test=MM_validateEmail(email, email);
        if (typeof(test)!="undefined"){errors+=test}
  }
  // test du tel
  if (tel.value != ""){
    testtel=MM_validateTel(tel);
        if (typeof(testtel)!="undefined"){errors+=testtel}
  }
  if (errors){ alert('Le formulaire n\'est pas valide:\n'+errors);}
  document.MM_returnValue = (errors == '');
}

function MM_validateFormprofile() { //v4.0
  var valpass,valpass1,valpass2,valemail,errors='',args=MM_validateFormprofile.arguments;
  valpass=MM_findObj(args[0]);
  valpass1=MM_findObj(args[1]);
  valpass2=MM_findObj(args[2]);
  valemail=MM_findObj(args[3]);
  if ((valpass.value == "") && ((valpass1.value != "") || (valpass2.value != ""))){
  errors+='- le champ "Mot de passe actuel" doit être renseigné.\n';
  }
  if (valpass1.value != valpass2.value){
        errors+='- erreur dans la vérification du nouveau mot de passe.\n';
  }
  // test de l'email
  test=MM_validateEmail(valemail, valemail);
  if (typeof(test)!="undefined"){errors+=test}
  if (errors) alert('Le formulaire n\'est pas valide:\n'+errors);
  document.MM_returnValue = (errors == '');
}

function MM_validateFormAnnonce() { //v4.0
  //utilise dans annonces-in.php
  var fonction,vallocation,titre,valtype, lieu,loyer,ch,tel,photo1,photo2,photo3,photo4,photo5,allValid=true,allNum = '',checkOK = '0123456789',errors='',args=MM_validateFormAnnonce.arguments;
  loyer=MM_findObj(args[0]);
  tel=MM_findObj(args[1]);
  fonction=MM_findObj(args[2]);
  vallocation=MM_findObj(args[3]);
  titre=MM_findObj(args[4]);
  valtype=MM_findObj(args[5]);
  lieu=MM_findObj(args[6]);
  email=MM_findObj(args[7]);
  photo1=MM_findObj(args[8]);
  photo2=MM_findObj(args[9]);
  photo3=MM_findObj(args[10]);
  photo4=MM_findObj(args[11]);
  photo5=MM_findObj(args[12]);
  if ((titre.value =="") || (fonction.options[fonction.selectedIndex].value =="") || (vallocation.options[vallocation.selectedIndex].value =="") || (valtype.options[valtype.selectedIndex].value =="") || (lieu.value =="") || (loyer.value =="")){ errors += '- les champs suivis d\'un astérisque doivent être renseignés.\n';}
  if ((tel.value == "") && (email.value == "")){ errors+= "- vous devez choisir un moyen de contact: email ou téléphone.\n";}
  // test de l'email
  if (email.value != ""){
    test=MM_validateEmail(email, email);
        if (typeof(test)!="undefined"){errors+=test}
  }
  for (i = 0;  i < loyer.value.length;  i++){
        ch = loyer.value.charAt(i);
        for (j = 0;  j < checkOK.length;  j++)
                if (ch == checkOK.charAt(j)) break;
                if (j == checkOK.length){
                        allValid = false;
                        break;
                }
                if (ch != ",")allNum += ch;
  }     
  if (!allValid) errors += '- le champ loyer ne peut contenir que des chiffres.\n';
  if (loyer.value==0) errors += '- le champ loyer ne peut être nul et, pour les locataires, représente un budget approximatif.\n'; 
  allValid=true;
  // test du tel
  if (tel.value != ""){
    testtel=MM_validateTel(tel);
        if (typeof(testtel)!="undefined"){errors+=testtel}
  }
  if (errors){ alert('Le formulaire n\'est pas valide:\n'+errors);}
if ((!errors) && ((photo1.value != "") || (photo2.value != "") || (photo3.value != "") || (photo4.value != "") || (photo5.value != ""))){toggleLayer('commentForm');}
  document.MM_returnValue = (errors == '');
}

function toggleLayer(whichLayer)
{
if (document.getElementById)
{
// this is the way the standards work
var style2 = document.getElementById(whichLayer).style;
style2.display = style2.display? "":"block";
}
else if (document.all)
{
// this is the way old msie versions work
var style2 = document.all[whichLayer].style;
style2.display = style2.display? "":"block";
}
else if (document.layers)
{
// this is the way nn4 works
var style2 = document.layers[whichLayer].style;
style2.display = style2.display? "":"block";
}
}

function MM_validateForminscription() { //v4.0
  var toto,i,j,p,q,nm,test,num,min,max,errors='',args=MM_validateForminscription.arguments;
  valuser2=MM_findObj(args[0]);
  valpwd1=MM_findObj(args[1]);
  valpwd2=MM_findObj(args[2]);
  valemail1=MM_findObj(args[3]);
  if ((valuser2.value == "") || (valpwd1.value == "")|| (valemail1.value == "")){
  errors+='- tous les champs doivent être renseignés.\n';
  }
  if (valpwd1.value != valpwd2.value) {errors+='- erreur dans la vérification du mot de passe.\n';}
  // test de l'email
  test=MM_validateEmail(valemail1, valuser2);
  if (typeof(test)!="undefined"){errors+=test}
  var checkStr = valuser2.value;
  if (checkStr.indexOf('\'') != -1){
                errors+='- Les caractères suivants ne sont pas admis pour l\'identifiant: \'\n';
  }
  if (errors) alert('Le formulaire n\'est pas valide:\n'+errors);
  document.MM_returnValueinscription = (errors == '');
}

function MM_validateFormContact() { //v4.0
  //utilise dans contact.php
  var nom, email,tel,errors='',args=MM_validateFormContact.arguments;
  nom=MM_findObj(args[0]);
  email=MM_findObj(args[1]);
  tel="6";
  if (nom.value == ""){
  errors+='- le champ Nom doit être renseigné.\n';
  }
  if (email.value == ""){
  errors+='- le champ Email doit être renseigné.\n';
  }
  if (email.value != ""){
    test=MM_validateEmail(email, email);
        if (typeof(test)!="undefined"){errors+=test}
  }
  if (errors){ alert('Le formulaire n\'est pas valide:\n'+errors);}
  document.MM_returnValue = (errors == '');
}

function max_caracteres(){
  max_caract = 500;
  if (document.form1.commentaires.value.length > max_caract){document.form1.commentaires.value = document.form1.commentaires.value.substring(0, max_caract);}
}