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_validateForm() { // avec 4 arguments
	var ok,i,d,p,q,nm,test,num,min,max,car,errors='',args=MM_validateForm.arguments;
	var regEmail	= /^[a-zA-Z0-9._-]+@[A-Za-z0-9-]{2,}([.][A-Za-z0-9-]{2,}){1,3}$/;
	var regMailing	= /^([a-zA-Z0-9._-]+@[A-Za-z0-9-]{2,}([.][A-Za-z0-9-]{2,}){1,3};*){1,}$/;
	var regEntier	= /^\d+$/;
	var regDecimal	= /^\d+[.]?\d*$/;
	var regDate		= /^\d{2}\/\d{2}\/\d{4}$/;
	var regFileJPG	= /(^[/(/)a-z0-9_-]+[.])((jpg)|(jpeg))$/i;
	var regFileImg	= /(^[/(/)a-z0-9_-]+[.])((gif)|(jpg)|(jpeg))$/i;
	var regFileAnim	= /(^[/(/)a-z0-9_-]+[.])((gif)|(jpg)|(jpeg)|(swf))$/i;
	var regFileFich	= /(^[/(/)a-z0-9_-]+[.])((gif)|(jpg)|(doc)|(pdf)|(zip)|(bmp)|(wmv)|(swf))$/i;
	var regFileMov	= /(^[/(/)a-z0-9_-]+[.])((gif)|(jpg)|(jpeg)|(mov)|(mpg)|(mpe)|(mpeg)|(avi)|(wmv)|(asf)|(asx)|(fli)|(rpm)|(ra)|(rm))$/i;
	var regSuppr	= /^(oui)$/i;
	
	for (i=0; i<(args.length-3); i+=4) { 
  		test=args[i+2];
		field=args[i+3];
		val=MM_findObj(args[i]);
		
		if (val) {		
			nm=val.name;
			val=val.value			
			if (val!="" || val=="null") {	 
				if (test.indexOf('isSuppr')!=-1)
					if (!regSuppr.exec(val)) errors+='- Le champ '+field+' doit contenir "Oui" si vous voulez supprimer l\'enregistrement, sinon cliquez sur le bouton annuler\n';

				if (test.indexOf('isEmail')!=-1)
					if (!regEmail.exec(val)) errors+='- Le champ '+field+' doit contenir une address e-mail valide Ex : nom.prenom@domaine.fr\n';

				if (test.indexOf('isMailing')!=-1)
					if (!regMailing.exec(val)) errors+='- Le champ '+field+' doit contenir une address e-mail valide Ex : nom.prenom@domaine.fr\n\nPour envoyer à plusieurs personnes, séparer les adresses par un ;\n';

				if (test.indexOf('isLimitecar')!=-1) {
					p=test.indexOf(':');
					car=test.substring(p+1);
		        	if (car > 0 && val.length>car) errors+='- Le champ '+field+' comporte '+val.length+' et ne doit pas dépasser '+car+' caractères\n'; 
		    	}
				if (test.indexOf('isDate')!=-1 || test.indexOf('isADate')!=-1) {
					ok = false;
					if( test.indexOf('isADate')!=-1 && val=="Toujours visible")
						ok = true;

					if (!regDate.exec(val) && !ok)
		        		 errors+='- Le champ '+field+' doit être une date au format jj/mm/yyyy\n';
					else if (!ok) { // la date existe t-elle ?
					
						// tab_mois[0] est janvier, [11] est decembre
						var tab_mois = new Array(31,29,31,30,31,30,31,31,30,31,30,31);	
						var tab_moisList = new Array("Janvier", "Février", "Mars", "Avril", "Mai", "Juin", "Juillet", "Août", "Septembre", "Octobre", "Novembre", "Décembre");

						jour = parseInt(eval(val.substring(0,2)));					
						mois = parseInt(eval(val.substring(3,5)));												
						an = parseInt(eval(val.substring(6,10)));
							
						// année bissextile ?
						if ((an % 100 != 0 && an % 4 == 0) || (an % 400 == 0)) { tab_mois[1] = 29; }
						else { tab_mois[1] = 28; }						
						
						if(mois>12 || mois<1)
							errors+='- Le champ '+field+' contient un mois invalide.\n';
							
						if(jour>31 || jour<1)
							errors+='- Le champ '+field+' contient un jour invalide.\n';
						
						mois = mois - 1;
						
						// on teste si la date choisie est anterieure au lundi 20 decembre 1582
						if ((an * 10000 + mois * 100 + jour) < 15821220)
							errors+='- Le champ '+field+' contient une date antérieure au 20 décembre 1582, hors du calendrier Grégorien. Veuillez sélectionner une date plus récente.\n';

						else if (tab_mois[mois] < jour) { // on verifie si la date est coherente
							errors+='- Il n\'y a pas ' + jour + ' jours en ' + tab_moisList[mois] + ' ' + an + ' mais ' + tab_mois[mois] + '. Veuillez choisir une autre date.\n';
						}
					}								
		    	}
				if (test.indexOf('isSelect')!=-1)
					if	(val == "null" && test.charAt(0) == 'R') errors+='- La première option de la liste '+field+' n\'est pas une sélection valide. Choisissez une des autres options.\n';

				if (test.indexOf('isEntier') != -1)
			        if (!regEntier.exec(val)) errors+='- Le champ '+field+' ne doit contenir que des Entiers.\n';

				if (test.indexOf('isEntierRange') != -1) {
					d=test.indexOf(':');
					p=test.indexOf('to');											
		        	min=parseInt(test.substring(d+1,p)); max=parseInt(test.substring(p+2));
			        if (val<min || max<val) errors+='- Le champ '+field+' doit contenir un nombre Entier compris entre '+min+' et '+max+'.\n';
			    }
				if (test.indexOf('isNombreMax') != -1) {			
					d=test.indexOf('>');
					p=test.indexOf(':');						
	        		max=parseFloat(test.substring(d+1,p));
			        if (val>max) errors+='- Le rapport > ou = des champs '+field+' est incorrect.\n';
				}
				if (test.indexOf('isDecimal') != -1)
					if (!regDecimal.exec(val)) errors+='- Le champ '+field+' ne doit contenir que Chiffres et vigule.\n';		

				if (test.indexOf('isDecimalRange') != -1) {
					d=test.indexOf(':');
					p=test.indexOf('to');						
		        	min=test.substring(d+1,p); max=test.substring(p+2);						
			        if (val<min || max<val) errors+='- Le champ '+field+' doit contenir un nombre compris entre '+min+' '+val+'  et '+max+'.\n';
			    }
				if (test.indexOf('isMin') != -1) {			
					d=test.indexOf('<');
					p=test.indexOf(':');						
	        		min=test.substring(d+1,p);																		
			        if (val.length<min) errors+='- Le champ '+field+' doit comporter au mois '+min+' caractères.\n';
				}
				// fractionne aux caractères \ ou / ou  pour ne récupérer que le nom de fichier
				if (test.indexOf('isFileMov') != -1) {					
				   	var tab = val.split(/\\|\//)
					if (!regFileMov.exec(tab[tab.length-1])) errors+='- Le champ '+field+' contient un NOM de fichier incorrect : \"'+tab[tab.length-1]+'\" n\'est pas un fichier Vidéo valide\n';		
			    }
				if (test.indexOf('isFileImg') != -1) {
				   	var tab = val.split(/\\|\//)
					if (!regFileImg.exec(tab[tab.length-1])) errors+='- Le champ '+field+' contient un NOM de fichier incorrect : \"'+tab[tab.length-1]+'\" n\'est pas un fichier Image valide\n';		
			    }
				if (test.indexOf('isFileJPG') != -1) {
				   	var tab = val.split(/\\|\//)
					if (!regFileJPG.exec(tab[tab.length-1])) errors+='- Le champ '+field+' ne doit contenir que des images .JPG/.JPEG\n';		
			    }
			    if (test.indexOf('isFileFich') != -1) {
				   	var tab = val.split(/\\|\//)
					if (!regFileFich.exec(tab[tab.length-1])) errors+='- Le champ '+field+' contient un NOM de fichier incorrect : \"'+tab[tab.length-1]+'\" n\'est pas un fichier valide\n';														        
			    }
				if (test.indexOf('isFileAnim') != -1) {
				   	var tab = val.split(/\\|\//)
					if (!regFileAnim.exec(tab[tab.length-1])) errors+='- Le champ '+field+' contient un NOM de fichier incorrect : \"'+tab[tab.length-1]+'\" n\'est pas un fichier valide\n';		
			    }		
				
			} else if (test.charAt(0) == 'R') errors += '- Le champ '+field+' est obligatoire.\n';
		}
	}
	if (errors)
		alert('Erreur(s) de saisie :\n\n'+errors);

	document.MM_returnValue = (errors == '');
}

function Compte_car(Target, maxi, nomchamp){
	var CharsLeft = Target.value.length;
	if (CharsLeft > maxi ){
		Target.value = Target.value.substring(0,maxi);
		CharsLeft = maxi;								
	}
	nomchamp.value = CharsLeft+' / '+maxi+' c. Maxi';	
}
