// changement de la combo des classements
function testClass(course,classement) {	
	if ( classement > 0 ) {
		// récupère les données envoyées
		var resultats = query("ajax_classements.php","course=" + course + "&class=" + classement);
		elements = resultats.split("@@");
		var tabResultat = new Array();
		for (var i = 0; i < elements.length; i++) {
			temp = elements[i].split("=");
			tabResultat[i] = new Array(temp[0], unescape(temp[1]));
			};
		comboCourse = document.getElementById("manche");
		comboCla = document.getElementById("class");
		comboCourse.options.length = 0;
		// aucun classement dispo
		if ( tabResultat.length == 1 ) {
			comboCourse.options[comboCourse.options.length] = new Option("---Pas de courses disponibles---",0);
			comboCourse.options[0].style.color = "#FF0000";
			}
		// 1 seul classement : choix automatique
		if ( tabResultat.length == 2 ) {
			comboCourse.options[comboCourse.options.length] = new Option(tabResultat[0][0],tabResultat[0][1]);
			}
		// plusieurs classements : liste
		if ( tabResultat.length > 2 ) {
			// remise à zéro combo
			if ( comboCla.value > 0 )
				comboCourse.options[comboCourse.options.length] = new Option("---Choisir une course---",0);
				else
				comboCourse.options[comboCourse.options.length] = new Option("---Courses disponibles---",0);
			for (var i = 0; i < tabResultat.length-1; i++) {					
				comboCourse.options[comboCourse.options.length] = new Option(tabResultat[i][0],tabResultat[i][1]);
				}
			};
		};
	};


function trim(s) {
	return s.replace(/^\s+/g, '').replace(/\s+$/g, '');
	}

function check_empty(text) {
	return (text.length > 0);
	};

function confirmation(laPhrase) {
	var is_ok = confirm('Etes-vous sûr de vouloir ' + laPhrase + ' ?');
	return is_ok;
	};

// recopie une valeur et va sur le champ si focus=1
function recopie(source,cible,is_focus) {
	var champ_source = document.getElementById(source);
	var champ_cible = document.getElementById(cible);
	if ( !champ_source ||!champ_cible )
		alert("Champs non trouvés");
		else
		{
		champ_cible.value = champ_source.value;
		if ( is_focus == 1 )
			champ_cible.focus();
		};
	};

// renvoie TRUE si au moins une option est cochée
function test_radio(radioname) {
	var is_checked = false;
	radioname = radioname.substring(6,radioname.length);
	for (var i = 0 ; i < document.getElementsByName(radioname).length ; i++) {
	
		if ( document.getElementsByName(radioname).item(i).checked ){
			is_checked = true;
			break;
			};
		};
	return is_checked;
	};

// test un champ, affiche_msg = 0 pour pas de message
function test_champ(nom_champ,libelle,affiche_msg) {
	var nb_erreurs = 0;
	var msg_erreur = "";
	// recherche champ
	var champ = document.getElementById(nom_champ);
	if ( champ == null ) {
		alert("Erreur formulaire : champ " + nom_champ + " non trouvé");			
		return 1;
		};
	// test valeur du champ
	if ( champ.value == "" || champ.value == 0) {		
		if ( affiche_msg )
			alert("Vous devez saisir " + libelle + ".");
			else
			msg_erreur = "Vous devez saisir " + libelle + ". ";
		champ.focus();
		nb_erreurs++;
		}
		else
		{
		// champ spécifique adresse e-mail
		if ( nom_champ.indexOf("adressemail") > 0 && !testAdresse(champ.value) ) {
			if ( affiche_msg )
				alert("L\'adresse e-mail est incorrecte.");
				else
				msg_erreur = "L\'adresse e-mail est incorrecte.";
			champ.focus();
			nb_erreurs++;
			};
		// champ spécifique téléphone - PAS POSSIBLE SI ETRANGER
		/*if ( nom_champ.indexOf("tel",0) != -1 && !testTelephone(champ.value) ) {
			if ( affiche_msg )
				alert("Le numéro de téléphone est incorrect (format : 02 44 55 66 77 ou +33 2 44 55 66 77)");
				else
				msg_erreur = "Le numéro de téléphone est incorrect (format : 02 44 55 66 77 ou +33 2 44 55 66 77)";
			champ.focus();
			nb_erreurs++;
			};
		*/
		};
	if ( affiche_msg == 0 )
		return msg_erreur;
		else
		return nb_erreurs;
	
	};

function isValid(aChamps,aLibelles) {
	var nb_erreurs = 0;
	var type_champ_autre = false;
	// vérification cohérence champs et libellés associés
	if ( aChamps.length != aLibelles.length ) {
		alert("Problème configuration tableaux (" + aLibelles.length + " champs et " + aChamps.length + " libellés)" );
		return false;
		};
	
	// contrôle de tous les champs
	for ( var i = 0; i < aChamps.length; i++ ) {
		type_champ_autre = false;
		
		// champ optionnel, à tester uniquement si valorisé
		if ( aChamps[i].substring(0,7) == "option_" ) {
			type_champ_autre = true;
			// test si valorisé
			if ( document.getElementById(aChamps[i].substring(7,aChamps[i].length)).value != "" )
				nb_erreurs += test_champ(aChamps[i].substring(7,aChamps[i].length),aLibelles[i],1);
			};

		// champ au choix (noms séparés par pipe)
		if ( aChamps[i].indexOf("|",0) != -1 ) {
			type_champ_autre = true;
			var liste_champs = aChamps[i].split("|");			
			var erreur_choix = "";
			var nb_erreur_choix = 0;
			for ( j = 0 ; j < liste_champs.length ; j++ ) {
				var erreur_choix = test_champ(liste_champs[j],aLibelles[i],0);
				if ( erreur_choix.length > 0 ) {
					nb_erreur_choix++;  }
				};
			
			// erreurs maxi possibles : nombre total de champs-1
			if ( nb_erreur_choix > liste_champs.length-1 ) {
				alert(erreur_choix);
				nb_erreurs++;
				//aChamps[i] = liste_champs[0];
				};
			};

		// test champ radio
		if ( aChamps[i].substring(0,6) == "radio_" ) {
			type_champ_autre = true;
			if ( test_radio(aChamps[i]) == false ) {
				alert("Vous devez saisir " + aLibelles[i] + ".");
				nb_erreurs++;
				break;
				};
			};

		// champ texte
		if ( type_champ_autre == false )			
			nb_erreurs += test_champ(aChamps[i],aLibelles[i],1);
		
		if ( aChamps[i].name != "undefined" )
			// si erreur, contour en rouge et stop les contrôles sinon contour couleur d'origine
			if ( nb_erreurs > 0 ) {
				if ( document.getElementById(aChamps[i]) != null ) {
					document.getElementById(aChamps[i]).style.borderColor = "#FF0000"; document.getElementById(aChamps[i]).focus(); }
				break;
				} else {
				if ( document.getElementById(aChamps[i]) != null )
					document.getElementById(aChamps[i]).style.borderColor = "#AAAAAA";
				};
		};
	if ( nb_erreurs <= 0 )
		return true;
		else
		return false;
	};

// vérification numéro de téléphone
function testTelephone(numero) {
	// autorise 01 à 09 (sauf 07) ou +33 1-9 et séparateurs ./- et espace
	var masque = /^(0|\+33\s?)[1|2|3|4|5|6|8|9]([\s\.\/\-]?\d{2}){4}$/;
	return masque.test(numero);
	};

function affiche(objet) {
	document.getElementById(objet).style.visibility = "visible";
	document.getElementById(objet).style.position = "relative";
	};

function masque(objet) {
	document.getElementById(objet).style.visibility = "hidden";
	document.getElementById(objet).style.position = "absolute";
	};

function testAdresse(adrMail) {
var adrMailLength=adrMail.length; // longueur de la chaîne adrMail
var caractOk="_.-0123456789abcdefghijklmnopqrstuvwxyz"; // caratères corect d'une adresse mail
var posArrobas=adrMail.indexOf('@',1); // position du premier Arrobas
var posPoint=adrMail.lastIndexOf('.',adrMailLength); // position du dernier point
var posCaractFaux=adrMail.indexOf('..',posArrobas+1); // position d'un caractère faux ('..' ou ' ')
var caract=0; // caractère extrait de la chaîne adrMail (utilisée par les boucles while)
var caractFaux=adrMail.charAt(0)+adrMail.charAt(posArrobas-1)+adrMail.charAt(posArrobas+1)+adrMail.charAt(posPoint-1);
var posCaract=0; // position du caractère à extraire de la chaîne adrMail
var posCaractOk=0; // position du caractère extrait de la chaîne adrMail
var posCaractNon=-1;
var retour = false;

if (posCaractFaux!=-1 || adrMail.indexOf(' ',0)!=-1 || adrMail.indexOf('--',0)!=-1 || adrMail.indexOf('__',0)!=-1) {
   posCaractFaux=0;
}

for (posCaract;posCaract<4;posCaract++) {
    caract=caractFaux.charAt(posCaract);
    posCaractNon=caractOk.lastIndexOf(caract,2);
    if (posCaractNon!=-1) {
       posCaractFaux=0;
       break;
    }
}
if (adrMailLength>0 && posArrobas!=-1 && posPoint!=-1 && posArrobas<posPoint && posCaractFaux==-1 && adrMailLength-posPoint-1>=2 && adrMailLength-posPoint-1<=4) {
   adrMail=adrMail.toLowerCase();
   posCaract=0;
   while (posCaract<posArrobas && posCaractOk!=-1) {
         caract=adrMail.charAt(posCaract);
         posCaractOk=caractOk.indexOf(caract,0);
         posCaract++;
   }
   posCaract++; // on passe l'arobase
   while (posCaract<posPoint && posCaractOk!=-1) {
         caract=adrMail.charAt(posCaract);
         posCaractOk=caractOk.indexOf(caract,1);
         posCaract++;
   }
   posCaract++; // on passe le point
   while (posCaract<adrMailLength && posCaractOk!=-1) {
         caract=adrMail.charAt(posCaract);
         posCaractOk=caractOk.indexOf(caract,13);
         posCaract++;
   } // fin de la boucle while
  
   if (posCaractOk!=-1) {
      retour=true;
   }
}
return retour;
}
