// Variable d'utilisation a ne pas modifier
var champVerifier = 0;
var tab = new Array();
var indexTab = 0;
var index;


//fonction du chargement de page pour desactiver l'envoie
function desactiverSubmit(idDuBoutonSubmit){
	document.getElementById(idDuBoutonSubmit).disabled = true;
}

//fonction pour réactiver l'envoie du formulaire si tout est verifier
function verifFormulaire(nombreDeChampVerifier, idDuBoutonSubmit){
	if(nombreDeChampVerifier==champVerifier){
		document.getElementById(idDuBoutonSubmit).disabled = false;
	}
}

//Fonction de verification d'adresse mel
function verifMail(idDuChampAVerifier, idDuChampAffichage){
	var reg = new RegExp(/^[^@]+@(([\w\-]+\.){1,4}[a-zA-Z]{2,4}|(([01]?\d?\d|2[0-4]\d|25[0-5])\.){3}([01]?\d?\d|2[0-4]\d|25[0-5]))$/);
	var mail = document.getElementById(idDuChampAVerifier).value;
	if(reg.test(mail))
	{
		if(navigator.appName == "Microsoft Internet Explorer"){
			document.getElementById(idDuChampAffichage).innerHTML = "E-mail Valide";
			document.getElementById(idDuChampAffichage).style.setAttribute("cssText", "color:green;");
		}
		else{
			document.getElementById(idDuChampAffichage).textContent = "E-mail Valide";
			document.getElementById(idDuChampAffichage).setAttribute("style", "color:green;");
		}
		champValide(idDuChampAVerifier);
		return true;
	}
	else
	{
		if(navigator.appName == "Microsoft Internet Explorer"){
			document.getElementById(idDuChampAffichage).innerHTML = "E-mail Invalide";
			document.getElementById(idDuChampAffichage).style.setAttribute("cssText", "color:red;");
		}
		else{
			document.getElementById(idDuChampAffichage).textContent = "E-mail Invalide";
			document.getElementById(idDuChampAffichage).setAttribute("style", "color:red;");
		}
		champFaux(idDuChampAVerifier);
		return false;
	}
}

//fonction de verification de champ vide
function verifRempli(idDuChampAVerifier, idDuChampAffichage){
	var champ = document.getElementById(idDuChampAVerifier).value
	if(champ==""){
		if(navigator.appName == "Microsoft Internet Explorer"){
			document.getElementById(idDuChampAffichage).innerHTML = "Champ obligatoire non renseigné";
			document.getElementById(idDuChampAffichage).style.setAttribute("cssText", "color:red;");
		}
		else{
			document.getElementById(idDuChampAffichage).textContent = "Champ obligatoire non renseigné";
			document.getElementById(idDuChampAffichage).setAttribute("style", "color:red;");
		}
		champFaux(idDuChampAVerifier);
		return false;
	}
	else{
		if(navigator.appName == "Microsoft Internet Explorer"){
			document.getElementById(idDuChampAffichage).innerHTML = "Champ obligatoire renseigné";
			document.getElementById(idDuChampAffichage).style.setAttribute("cssText", "color:green;");
		}
		else{
			document.getElementById(idDuChampAffichage).textContent = "Champ obligatoire renseigné";
			document.getElementById(idDuChampAffichage).setAttribute("style", "color:green;");
		}
		champValide(idDuChampAVerifier);
		return true;
	}
}

//function pour verifier que le champ ne comporte que des nombres, et aussi le bon nombre de nombre
function verifChampNumerique(idDuChampAVerifier, idDuChampAffichage, nombreDeChiffreMinimum, nombreDeChiffreMaximum){
	var champ = document.getElementById(idDuChampAVerifier).value;
	
	if(isNaN(champ)){
		if(navigator.appName == "Microsoft Internet Explorer"){
			document.getElementById(idDuChampAffichage).innerHTML = "Ce champ ne doit comporter que des nombres";
			document.getElementById(idDuChampAffichage).style.setAttribute("cssText", "color:red;");
		}
		else{
			document.getElementById(idDuChampAffichage).textContent = "Ce champ ne doit comporter que des nombres";
			document.getElementById(idDuChampAffichage).setAttribute("style", "color:red;");
		}
		champFaux(idDuChampAVerifier);
		return false;
	}
	else{
		if(champ.length < nombreDeChiffreMinimum || champ.length > nombreDeChiffreMaximum){
			if(navigator.appName == "Microsoft Internet Explorer"){
				document.getElementById(idDuChampAffichage).innerHTML = "Ce champ n'est pas de la taille demandé";
				document.getElementById(idDuChampAffichage).style.setAttribute("cssText", "color:red;");
			}
			else{
				document.getElementById(idDuChampAffichage).textContent = "Ce champ n'est pas de la taille demandé";
				document.getElementById(idDuChampAffichage).setAttribute("style", "color:red;");
			}
			champFaux(idDuChampAVerifier);
			return false;
		}
		else{
			if(navigator.appName == "Microsoft Internet Explorer"){
				document.getElementById(idDuChampAffichage).innerHTML = "Valide";
				document.getElementById(idDuChampAffichage).style.setAttribute("cssText", "color:green;");
			}
			else{
				document.getElementById(idDuChampAffichage).textContent = "Valide";
				document.getElementById(idDuChampAffichage).setAttribute("style", "color:green;");
			}
			champValide(idDuChampAVerifier);
			return true;
		}
	}
}

//fonction qui verifie la similitude de deux champs MDP
function verifMotDePass(idDuChampMdp, idDuChampAffichage, idDuChampMdpBis){
	var mdp = document.getElementById(idDuChampMdp).value;
	var mdpBis = document.getElementById(idDuChampMdpBis).value;
	
	if(mdp == ""){
		if(navigator.appName == "Microsoft Internet Explorer"){
			document.getElementById(idDuChampAffichage).innerHTML = "Mot de passe vide";
			document.getElementById(idDuChampAffichage).style.setAttribute("cssText", "color:red;");
		}
		else{
			document.getElementById(idDuChampAffichage).textContent = "Mot de passe vide";
			document.getElementById(idDuChampAffichage).setAttribute("style", "color:red;");
		}
		champFaux(idDuChampMdp);
		return false;
	}
	else{
		if(mdp == mdpBis){
			if(navigator.appName == "Microsoft Internet Explorer"){
				document.getElementById(idDuChampAffichage).innerHTML = "Valide";
				document.getElementById(idDuChampAffichage).style.setAttribute("cssText", "color:green;");
			}
			else{
				document.getElementById(idDuChampAffichage).textContent = "Valide";
				document.getElementById(idDuChampAffichage).setAttribute("style", "color:green;");
			}
			champValide(idDuChampMdp);
			return true;
		}
		else{
			if(navigator.appName == "Microsoft Internet Explorer"){
				document.getElementById(idDuChampAffichage).innerHTML = "Mot de passe different";
				document.getElementById(idDuChampAffichage).style.setAttribute("cssText", "color:red;");
			}
			else{
				document.getElementById(idDuChampAffichage).textContent = "Mot de passe different";
				document.getElementById(idDuChampAffichage).setAttribute("style", "color:red;");
			}
			champFaux(idDuChampMdp);
			return false;
		}
	}
}

//fonction qui vérifie qu'un champ a été saisie dans une balise select
function verifSelect(idDuChampAVerifier, idDuChampAffichage){
	var champ = document.getElementById(idDuChampAVerifier).value
	if(champ==""){
		if(navigator.appName == "Microsoft Internet Explorer"){
			document.getElementById(idDuChampAffichage).innerHTML = "Champ obligatoire non renseigné";
			document.getElementById(idDuChampAffichage).style.setAttribute("cssText", "color:red;");
		}
		else{
			document.getElementById(idDuChampAffichage).textContent = "Champ obligatoire non renseigné";
			document.getElementById(idDuChampAffichage).setAttribute("style", "color:red;");
		}
		champFaux(idDuChampAVerifier);
		return false;
	}
	else{
		if(navigator.appName == "Microsoft Internet Explorer"){
			document.getElementById(idDuChampAffichage).innerHTML = "Champ obligatoire renseigné";
			document.getElementById(idDuChampAffichage).style.setAttribute("cssText", "color:green;");
		}
		else{
			document.getElementById(idDuChampAffichage).textContent = "Champ obligatoire renseigné";
			document.getElementById(idDuChampAffichage).setAttribute("style", "color:green;");
		}
		champValide(idDuChampAVerifier);
		return true;
	}
}

// fonction interne au fonctionne du fichier JS a ne pas mettre dans le HTML et a ne pas modifier

function estValide(idARechercher){
	var reponse = false;
	for(i=0;i<tab.length;i++){
		if(tab[i]==idARechercher){
			reponse = true;
			index = i;
		}
	}
	return reponse;
}

function estVide(){
	var reponse = null;
	var i = 0;
	while(i<tab.length && reponse==null){
		if(tab[i]==""){
			reponse = i;
		}
		i++;
	}
	return reponse;
}

function champFaux(idDuChamp){
	index = null;
	if(estValide(idDuChamp)){
		champVerifier--;
		tab[index] = "";
	}
}

function champValide(idDuChamp){
	index = null;
	if(estValide(idDuChamp)){
	}
	else{
		champVerifier++;
		index = estVide();
		if(index==null){
			tab[indexTab] = idDuChamp;
			indexTab++;
		}
		else{
			tab[index] = idDuChamp;
		}
	}
}