// 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) {
  var toto;
  toto=email.value;
  p=toto.indexOf('@');
  q=toto.indexOf('.');
  if (email.value != ""){
  if (p==-1 || q==-1)  return '- '+email.name+' doit contenir une adresse e-mail valide.\n';
  }
}

function TestPhoto(photo,i) {
	if ((photo.indexOf('.jpg')==-1) && (photo.indexOf('.jpeg')==-1) && (photo.indexOf('.gif')==-1) && (photo.indexOf('.png')==-1) && (photo.indexOf('.JPG')==-1) && (photo.indexOf('.JPEG')==-1) && (photo.indexOf('.GIF')==-1) && (photo.indexOf('.PNG')==-1)) return '- Votre '+i+' photo doit être de type png, gif, jpg ou jpeg.\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_validateFormAnnonce() { //v4.0
  //utilise dans annonces-in.php
  var categorie, i,j,titre, prix ,test, test2, test3, test4, test5,ch,tel,photo1,photo2,photo3,photo4,photo5,video,allValid=true,allNum = '',checkOK = '0123456789',errors='',args=MM_validateFormAnnonce.arguments;
  categorie=MM_findObj(args[0]);
  titre=MM_findObj(args[1]);
  prix=MM_findObj(args[2]);
  tel=MM_findObj(args[3]);  
  photo1=MM_findObj(args[4]);
  photo2=MM_findObj(args[5]);
  photo3=MM_findObj(args[6]);
  photo4=MM_findObj(args[7]);
  photo5=MM_findObj(args[8]);
  video=MM_findObj(args[9]);
  if ((titre.value =="") || (categorie.options[categorie.selectedIndex].value ==0) || (prix.value =="")){ errors += '- Les champs suivis d\'un astérisque doivent être renseignés.\n';}
  if (photo1.value != ""){
    test1=TestPhoto(photo1.value, "première");
 		if (typeof(test1)!="undefined"){errors+=test1}
 }
 if (photo2.value != ""){
    test2=TestPhoto(photo2.value, "deuxième");
 		if (typeof(test2)!="undefined"){errors+=test2}
 }
  if (photo3.value != ""){
    test3=TestPhoto(photo3.value, "troisième");
 		if (typeof(test3)!="undefined"){errors+=test3}
 }
  if (photo4.value != ""){
    test4=TestPhoto(photo4.value, "quatrième");
 		if (typeof(test4)!="undefined"){errors+=test4}
 }
  if (photo5.value != ""){
    test5=TestPhoto(photo5.value, "cinquième");
 		if (typeof(test5)!="undefined"){errors+=test5}
 }   
  for (i = 0;  i < prix.value.length;  i++){
        ch = prix.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 prix ne peut contenir que des chiffres.\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) && (video.value=="")){alert('Avez-vous pensé à la vidéo?')}
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 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);}
}