// Fonctions internes_________________________________________________

function numerique(champ,taille)
/* champ = taille chiffres ? */
{
var valid="oui";

	if (champ.value.length != taille || isNaN(champ.value)) {
		valid="non";
		champ.focus();
		champ.select();
	}

return valid;
}

function email(champ)
/* email correct ? */
{
var arob,point,ptvir;

	arob=champ.value.indexOf("@");
	point=champ.value.lastIndexOf(".");
	ptvir=champ.value.indexOf(";");

	if (arob<1 || point<3 || point<arob || ptvir>=0) return -1;

return 0;
}

function unchoix(champ)
/* un radio choisi ? */
{
var i;
var selec=0;
var lg	
	
	lg = champ.length;
	for (i=0;i<lg;i++) if (champ[i].checked) selec++;

return selec;
}


// Fonctions de verif._____________________________________________________________

function verif_identite(form,i)
/* verif. de base */
{

	if (form.portable.value.indexOf("06")!= 0) {
		form.portable.focus();
		form.portable.select();
		alert ("Votre numéro de portable doit commencer par 06");
		return false;
	}

	if (numerique(form.portable,10)=="non") {
		alert ("Indiquez les 10 chiffres de votre numéro de portable sans espace (ex.: 0690102030)");
		return false;
	}

	if (numerique(form.password1,6)=="non") {
		alert ("Votre code d'accès doit comporter 6 chiffres");
		return false;
	}

	if (i==1) {
		if (form.password1.value != form.password2.value) {
			form.password2.focus();
			form.password2.select();
			window.alert("Le code d'accès et la confirmation ne sont pas identiques");
			return false;
		}
		if (email(form.email) != 0) {
			form.email.focus();
			form.email.select();
			alert("Votre courriel est incorrect");
			return false;
		}
		if ( (numerique(form.dept,1) =="non") && (numerique(form.dept,2) =="non") 
		 || ( (form.dept.value < 1)  || (form.dept.value > 99) ) ) {
			form.dept.focus();
			form.dept.select();
			alert("Vous devez entrer un nombre entre 1 et 99 pour le département");
			return false;
		}
		if (form.typabo.name=="typabo") {
			if (! form.typabo.checked) {
				alert ("Veuillez choisir un type d'abonnement");
				form.typabo.focus();
				form.typabo.select();
				return false;
			}
		}
		else if (form.typabo[0].name=="typabo") {
			if (unchoix(form.typabo) != 1) {
				alert ("Veuillez choisir un type d'abonnement");
				form.typabo[0].focus();
				form.typabo[0].select();
				return false;
			}
		}

	}
		
return true;
}


function champsok(form, nblig)
/* verif des champs du formulaire en creation */
{
	var i;
	var nbsel=0;
	var nbph=0;


   	for (i=0;i<form.elements.length;i++) {
		if (form.elements[i].name=="rl[]" && form.elements[i].checked) nbsel++;
	}
	
	if (form.D1.selectedIndex !=0) nbph++;
	if (form.D2.selectedIndex !=0) nbph++;

	if (nbph == 0) {
		alert("Choisissez au moins une tranche horaire");
		document.location="#saisie";
		return false;
	}
	if (nbsel == 0) {
		alert("Choisissez au moins une ligne");
		document.location="#saisie";
		return false;
	}
	if (nbsel > nblig) {
		alert("Vous avez choisi trop de lignes");
		document.location="#saisie";
		return false;
	}
	return true;
}
function champsok_tot(form, nblig)
/* verif des champs du formulaire en modif. */
{
	var i,j;
	var nbsels=0;
	var nbselw=0;
	var nbphs=0;
	var nbphw=0;

	if (email(form.email) != 0) {
		form.email.focus();
		form.email.select();
		alert("Votre courriel est incorrect");
		return false;
	}

	if ( (numerique(form.dept,1) =="non") && (numerique(form.dept,2) =="non") 
	 || ( (form.dept.value < 1)  || (form.dept.value > 99) ) ) {
		form.dept.focus();
		form.dept.select();
		alert("Vous devez entrer un nombre entre 1 et 99 pour le département");
		return false;
	}

   	for (i=0;i<form.elements.length;i++) {
		if (form.elements[i].name=="rls[]" && form.elements[i].checked) nbsels++;
		if (form.elements[i].name=="D1s" && form.elements[i].selectedIndex != 0) nbphs++;
		if (form.elements[i].name=="D2s" && form.elements[i].selectedIndex != 0) nbphs++;
	}
   	for (i=0;i<form.elements.length;i++) {
		if (form.elements[i].name=="rlw[]" && form.elements[i].checked) nbselw++;
		if (form.elements[i].name=="D1w" && form.elements[i].selectedIndex != 0) nbphw++;
		if (form.elements[i].name=="D2w" && form.elements[i].selectedIndex != 0) nbphw++;
	}


	if (nbsels > nblig) {
		alert("Vous avez choisi trop de lignes pour la semaine");
		document.location="#semaine";
		return false;
	}

	if (nbselw > nblig) {
		alert("Vous avez choisi trop de lignes pour le week-end");
		document.location="#samedi";
		return false;
	}

	if (nbsels == 0 && nbselw == 0 && nbphs == 0 && nbphw == 0) {
		alert("Vous n'avez fait aucun choix de lignes ni de tranches horaires !");
		document.location="#parametres";
		return false;
	}

	if ( (nbsels == 0 && nbphs != 0) || (nbsels != 0 && nbphs == 0) ) {
		alert("Votre choix en semaine est incohérent : si vous choisissez des horaires, choisissez des lignes (et inversement).");
		document.location="#semaine";
		return false;
	}
	if ( (nbselw == 0 && nbphw != 0) || (nbselw != 0 && nbphw == 0) ) {
		alert("Votre choix du week-end est incohérent : si vous choisissez des horaires, choisissez des lignes (et inversement).");
		document.location="#samedi";
		return false;
	}

return true;
}

function verif_prix(form)
{
	if (form.prixabo.name=="prixabo") {
		if (! form.prixabo.checked) {
			alert ("Veuillez choisir une durée d'abonnement");
			form.prixabo.focus();
			form.prixabo.select();
			return false;
		}
	}
	else if (form.prixabo[0].name=="prixabo") {
		if (unchoix(form.prixabo) != 1) {
			alert ("Veuillez choisir une durée d'abonnement");
			form.prixabo[0].focus();
			form.prixabo[0].select();
			return false;
		}
	}

return true;
}


function effac_form(form)
{
	form.reset();
}

