/* --------------------------------------------------------------------------------- */
/* Cette fonction permet de cacher un texte (exemple "Nom de l'utilisateur" onclick) */
/* --------------------------------------------------------------------------------- */

function cache_texte(texte, input)
{
	if(window.document.getElementById(input).value == texte)
	{
		window.document.getElementById(input).value = "";
	}
}

/* --------------------------------------------------------------------------------- */
/* Cette fonction permet d'afficher un texte (exemple "Nom de l'utilisateur" onblur) */
/* --------------------------------------------------------------------------------- */

function affiche_texte(texte, input)
{
	if(window.document.getElementById(input).value == "")
	{
		window.document.getElementById(input).value = texte;
	}
}

/* ----------------------------------------------------------------------------- */
/* Cette fonction permet de vérifier que la date saisie est bien dans la période */
/* ----------------------------------------------------------------------------- */

function verif_date_anterieure(jour, mois, annee, id, msg)
{
	var la_date = new Date(annee, mois-1, jour);
	var date_auj = new Date();

	if( la_date > date_auj )
	{
		window.document.getElementById(id).value = "";
		alert(msg);
	}
}

/* ----------------------------------------------------------------------------- */
/* Cette fonction permet de vérifier que la date saisie est bien dans la période */
/* ----------------------------------------------------------------------------- */
function verif_periode_date(annee_debut_periode, annee_fin_periode, periode_jour_debut, periode_mois_debut, periode_jour_fin, periode_mois_fin, jour, mois, annee, id, msg)
{
	var la_date = new Date(annee, mois-1, jour);
	var date_limite_debut = new Date(annee_debut_periode, periode_mois_debut-1, periode_jour_debut);
	var date_limite_fin = new Date(annee_fin_periode, periode_mois_fin-1, periode_jour_fin);
	var date_auj = new Date();

	//if( !((la_date >= date_limite_debut) && (la_date <= date_limite_fin)) )
	if( !((la_date >= date_limite_debut) && (la_date <= date_limite_fin) && (la_date <= date_auj)) )
	{
		window.document.getElementById(id).value = "";
		if (la_date <= date_auj){
			alert(msg);
		}
		else{
			alert("La date saisie ne peut être supérieure à la date du jour");
		}
	}
}

/* ------------------------------------------------------------------------------- */
/* Cette fonction permet de changer le type d'un champ input (indispensable pr IE) */
/* ------------------------------------------------------------------------------- */

function Change_type_input_focus(obj, new_type)
{
	var new_obj = document.createElement('input');
	new_obj.setAttribute('type',new_type);
	new_obj.setAttribute('name',obj.getAttribute('name'));
	new_obj.setAttribute('id',obj.getAttribute('id'));
	new_obj.setAttribute('size',obj.getAttribute('size'));
	new_obj.setAttribute('onblur',obj.getAttribute('onblur'));
	obj.parentNode.replaceChild(new_obj,obj);
	new_obj.focus();
	new_obj.focus();
}

/* ----------------------------------------------------------------------------- */
/* Cette fonction permet de supprimer tous les caractères autres que des entiers */
/* Paramètres d'entrée : valeur du champ, id du champ                            */
/* ----------------------------------------------------------------------------- */

function format_int(champ, id)
{
	var champ_modifie = "";

	for(var i = 0; i < champ.length; i++)
	{
		champ_modifie = champ_modifie + champ.charAt(i);
		champ_modifie = champ_modifie.replace(/[-a-zA-Z._/éèàùâêîôû%,:;!?*@°&|{}=<> ]/, "");
	} 

	window.document.getElementById(id).value = champ_modifie;
}

/* --------------------------------------------------- */
/* Idem que celle dessus mais interdit la saisie des 0 */
/* --------------------------------------------------- */

function format_int_zero_interdit(champ, id)
{
	var champ_modifie = "";

	for(var i = 0; i < champ.length; i++)
	{
		champ_modifie = champ_modifie + champ.charAt(i);
		champ_modifie = champ_modifie.replace(/[-a-zA-Z._/éèàùâêîôû%,:;!?*@°&|{}=<> ]/, "");
	} 

	if(champ_modifie=="0")
	{
		window.document.getElementById(id).value = "";
	}
	else
	{
		window.document.getElementById(id).value = champ_modifie;
	}
}

function format_int_zero_autorise(champ, id)
{
	var champ_modifie = "";

	for(var i = 0; i < champ.length; i++)
	{
		champ_modifie = champ_modifie + champ.charAt(i);
		champ_modifie = champ_modifie.replace(/[-a-zA-Z._/éèàùâêîôû%,:;!?*@°&|{}=<> ]/, "");
	} 

	window.document.getElementById(id).value = champ_modifie;
}

/* ----------------------------------------------------------------------------- */
/* Cette fonction permet de supprimer tous les caractères autres que des entiers */
/* Elle permet de transformer les grammes en kilos                               */
/* Paramètres d'entrée : valeur du champ et id du champ en gr, id du champ en kg */
/* ----------------------------------------------------------------------------- */

function GrammesEnKilos(valeur, id, nouveauchamp)
{
	format_int_zero_interdit(valeur, id); // on supprimer tout autre caractère que des entiers pour le champ grammes

	if(window.document.getElementById(id).value!="")
	{
		window.document.getElementById(nouveauchamp).value = window.document.getElementById(id).value/1000;
	}
}

/* ----------------------------------------------------------------------------- */
/* Cette fonction fait l'inverse                                                 */
/* Paramètres d'entrée : valeur du champ et id du champ en kg, id du champ en gr */
/* ----------------------------------------------------------------------------- */

function KilosEnGrammes(valeur, id, nouveauchamp)
{
	if(window.document.getElementById(id).value!="")
	{
		window.document.getElementById(nouveauchamp).value = window.document.getElementById(id).value*1000;
	}
}

/* ----------------------------------------------------------------------------- */
/* En cas d'ajout de prélèvement avec un Nbr d'1 : Age, Sexe et Poids DISABLED   */
/* Paramètres d'entrée : id Nbr, id Age, id Sexe, id Grammes et id Kilos		 */
/* ----------------------------------------------------------------------------- */

function AgeSexePoidsDisabled(id, id1, id2, id3, id4)
{
	if(window.document.getElementById(id).value > 1)
	{
		window.document.getElementById(id1).disabled='disabled';
		window.document.getElementById(id2).disabled='disabled';
		window.document.getElementById(id3).disabled='disabled';
		window.document.getElementById(id4).disabled='disabled';

	}
	else
	{
		window.document.getElementById(id1).disabled='';
		window.document.getElementById(id2).disabled='';
		window.document.getElementById(id3).disabled='';
		window.document.getElementById(id4).disabled='';
	}
}


/* ***************************************************************************************************************************** */
/* ******************************************************* FORMULAIRES ********************************************************* */
/* ***************************************************************************************************************************** */



/* -------------------------------------------------------------- */
/* Cette fonction permet de supprimer une sortie */
/* -------------------------------------------------------------- */

function supprimer_sortie(sortie)
{
	if (confirm("Voulez-vous vraiment supprimer cette sortie ?"))
	{
		window.document.getElementById('sortie-delete').value=sortie;
		window.document.sorties.submit();
	}
}


/* ----------------------------------------------------------------------------------------------------  */
/* Cette fonction permet d'afficher un message de confirmation avant la suppression d'un enregistrement  */
/* Paramètres d'entrée : id de l'enregistrement à supprimer, script appelé, paramètres à ajouter à l'url */
/* ----------------------------------------------------------------------------------------------------  */

function delete_enr(id, script, params)
{
	if (confirm("Voulez-vous vraiment supprimer cet enregistrement ?"))
	{
		location.href = script + "?del_id=" + id + params;
	}
}

/* ---------------------------------------------------------------------------------------- */
/* Cette fonction affiche la boite d'alerte si les champs du formulaire ne sont pas remplis */
/* Paramètre d'entrée : liste des message d'erreur                                          */
/* ---------------------------------------------------------------------------------------- */

function message_form(message)
{
	if ( (message=="") ||  (message=="no_add") || (message=="Prélèvement ou observation en cours de saisie") )
	{
		return true;
	}
	else
	{	
		alert("Merci de saisir les champs suivants : \r\n"+message);
		return false;
	}
}

/* -------------------------------------------------------------------------- */
/* Idem que la fonction au-dessus mais sans alert js affichée en cas d'erreur */
/* -------------------------------------------------------------------------- */

function valid_form(message)
{
	if (message=="")
	{
		return true;
	}
	else
	{	
		return false;
	}
}

/* -------------------------------------------------------------------- */
/* Cette fonction permet de tester si un champ est bien rempli          */
/* Paramètres d'entrée : id du champ et libellé affiché en cas d'erreur */
/* Class css si champ ok et Class css si erreur							*/
/* -------------------------------------------------------------------- */

function test_champ_vide(id, libelle, css_ok, css_erreur)
{
	if(window.document.getElementById(id).value=="")
	{
		message += " - " + libelle + " \r\n";
		if(css_erreur!="")
		{
			window.document.getElementById(id).className = css_erreur;
		}
	}
	else
	{
		if(css_ok!="")
		{
			window.document.getElementById(id).className = css_ok;
		}
	}
}

/* -------------------------------------------------------------------- */
/* Cette fonction permet de tester si une textarea est bien rempli      */
/* Paramètres d'entrée : id du champ et libellé affiché en cas d'erreur */
/* Class css si champ ok et Class css si erreur							*/
/* -------------------------------------------------------------------- */

function test_textarea_vide(id, libelle, css_ok, css_erreur)
{
	var textarea = window.document.getElementById(id).value;
	textarea = textarea.replace(/(^\s*)|(\s*$)/g,'');
	if (textarea == "")
	{
		message += " - " + libelle + " \r\n";
		if(css_erreur!="")
		{
			window.document.getElementById(id).className = css_erreur;
		}
	}
	else
	{
		if(css_ok!="")
		{
			window.document.getElementById(id).className = css_ok;
		}
	}
}

/* ---------------------------------------------------------------------------------------------- */
/* Cette fonction permet de tester si un champ atteint le nb minimal de caractères requis         */
/* Paramètres d'entrée : id du champ, nb minimal de caractères et libellé affiché en cas d'erreur */
/* Class css si champ ok et Class css si erreur													  */
/* ---------------------------------------------------------------------------------------------- */

function test_nb_carac_min(id, nbcarac, libelle, css_ok, css_erreur)
{
	if(window.document.getElementById(id).value.length < nbcarac)
	{
		message += " - " + libelle + " ("+nbcarac+" caractères minimum requis) \r\n";
		if(css_erreur!="")
		{
			window.document.getElementById(id).className = css_erreur;
		}
	}
	else
	{
		if(css_ok!="")
		{
			window.document.getElementById(id).className = css_ok;
		}
	}
}

/* ---------------------------------------------------------------------------------------------- */
/* Cette fonction permet de tester si un champ atteint le nb de caractères requis                 */
/* Paramètres d'entrée : id du champ, nb minimal de caractères et libellé affiché en cas d'erreur */
/* Class css si champ ok et Class css si erreur													  */
/* ---------------------------------------------------------------------------------------------- */

function test_nb_carac(id, nbcarac, libelle, css_ok, css_erreur)
{
	if(window.document.getElementById(id).value.length != nbcarac)
	{
		message += " - " + libelle + " ("+nbcarac+" caractères requis) \r\n";
		if(css_erreur!="")
		{
			window.document.getElementById(id).className = css_erreur;
		}
	}
	else
	{
		if(css_ok!="")
		{
			window.document.getElementById(id).className = css_ok;
		}
	}
}

/* ------------------------------------------------------------------------------------- */
/* Cette fonction permet de tester si un champ est bien rempli                           */
/* Paramètres d'entrée : id du champ 1, id du champ 2 et libellé affiché en cas d'erreur */
/* Class css si champ ok et Class css si erreur											 */
/* ------------------------------------------------------------------------------------- */

function test_champs_egaux(idchamp1, idchamp2, libelle, css_ok, css_erreur)
{
	if(window.document.getElementById(idchamp1).value!=window.document.getElementById(idchamp2).value)
	{
		message += " - " + libelle + " \r\n";
		if(css_erreur!="")
		{
			window.document.getElementById(idchamp1).className = css_erreur;
			window.document.getElementById(idchamp2).className = css_erreur;
		}
	}
	else
	{
		if(css_ok!="")
		{	
			window.document.getElementById(idchamp1).className = css_ok;
			window.document.getElementById(idchamp2).className = css_ok;
		}
	}
}

/* -------------------------------------------------------------------- */
/* Cette fonction permet de tester le format d'un champ mail            */
/* Paramètres d'entrée : id du champ et libellé affiché en cas d'erreur */
/* Class css si champ ok et Class css si erreur							*/
/* -------------------------------------------------------------------- */

function test_format_mail(id, libelle, css_ok, css_erreur)
{
	if (window.document.getElementById(id).value.search(/^[-a-zA-Z0-9._]*[-a-zA-Z0-9_]@[-a-zA-Z0-9_]+(\.[-a-zA-Z0-9_]+)*\.[a-zA-Z]{2,4}$/) < 0)
	{
		message += " - " + libelle + " \r\n";
		if(css_erreur!="")
		{
			window.document.getElementById(id).className = css_erreur;
		}
	}
	else
	{
		if(css_ok!="")
		{
			window.document.getElementById(id).className = css_ok;
		}
	}
}


/* -------------------------------------------------------------------------- */
/* Cette fonction vérifie la bonne saisie du formulaire de création de compte */
/* -------------------------------------------------------------------------- */

function verif_crea_compte()
{
	message = "";

	/* Login */
	test_nb_carac_min("login", 6, "Nom utilisateur", "input-blanc", "input-rouge");

	/* Mot de passe */
	test_nb_carac_min("pass", 8, "Mot de passe", "input-blanc", "input-rouge");

	/* Confirmation de Mot de passe */
	test_nb_carac_min("confirm-pass", 8, "Confirmation de mot de passe", "input-blanc", "input-rouge");

	/* Egalité des deux champs de mot de passe */
	test_champs_egaux("pass", "confirm-pass", "Mot de passe et confirmation de mot de passe non-égaux", "input-blanc", "input-rouge");

	/* Nom */
	test_champ_vide("nom", "Nom", "input-blanc input-name", "input-rouge input-name");

	/* Prénom */
	test_champ_vide("prenom", "Prénom", "input-blanc", "input-rouge");

	/* Jour de naissance */
	test_champ_vide("jour", "Jour de naissance", "select-orange", "input-rouge");

	/* Mois de naissance */
	test_champ_vide("mois", "Mois de naissance", "select-orange", "input-rouge");

	/* Année de naissance */
	test_champ_vide("annee", "Année de naissance", "select-orange", "input-rouge");

	/* Email */
	test_format_mail("email", "Email", "input-blanc", "input-rouge");

	/* CP */
	test_champ_vide("cp", "CP", "input-blanc", "input-rouge");

	/* Chasseur */
	if(window.document.getElementById("type-user-1").checked)
	{
		/* Permis */
		test_champ_vide("permis", "N° de permis de chasser", "input-orange-bkg", "input-rouge");

		/* Id */
		//test_champ_vide("id", "N° identifiant chasseur");
		test_nb_carac("id", 14, "N° identifiant chasseur", "input-orange-bkg", "input-rouge");
		
	}

	/* Piégeur */
	if(window.document.getElementById("type-user-2").checked)
	{
		/* Agrement */
		//test_champ_vide("agrement", "N° agrément piégeur", "input-orange-bkg", "input-rouge");
	}

	/* Chasseur ou Piégeur */
	if( (!window.document.getElementById("type-user-1").checked) && (!window.document.getElementById("type-user-2").checked) )
	{	
		message += " - Chasseur ou piégeur \r\n";
	}

	/* Conditions générales */
	if(!window.document.getElementById("cgu").checked)
	{
		message += " - Conditions générales d'utilisation du site \r\n";
	}
	
	return message_form(message);
}

/* --------------------------------------------------------------- */
/* Cette fonction vérifie la bonne saisie du formulaire de contact */
/* --------------------------------------------------------------- */

function verif_contact()
{
	message = "";

	/* Nom */
	test_champ_vide("nom", "Nom", "input-blanc input-name", "input-rouge input-name");

	/* Prénom */
	test_champ_vide("prenom", "Prénom", "input-blanc", "input-rouge");

	/* Email */
	test_format_mail("email", "Email", "input-blanc", "input-rouge");

	/* Message */
	test_textarea_vide("message", "Message", "input-blanc", "input-rouge");

	return message_form(message);
}





/* ************************************************************************************************************************ */
/* ******************************************************* LISTES ********************************************************* */
/* ************************************************************************************************************************ */

/* ----------------------------------------------------------------------- */
/* Cette fonction permet de passer une ligne de territoire en mode edition */
/* ----------------------------------------------------------------------- */

function modifier_territoire_prelevement(territoire)
{
	// on cache les colonnes d'affichage
	window.document.getElementById("dpt-tdview-"+territoire).style.display="none";
	window.document.getElementById("comm-tdview-"+territoire).style.display="none";
	window.document.getElementById("terr-tdview-"+territoire).style.display="none";

	// on affiche les colonnes d'édition
	window.document.getElementById("dpt-tdalter-"+territoire).style.display="";
	window.document.getElementById("comm-tdalter-"+territoire).style.display="";
	window.document.getElementById("terr-tdalter-"+territoire).style.display="";
}


/* ------------------------------------------------ */
/* Cette fonction permet de supprimer un territoire */
/* ------------------------------------------------ */

function supprimer_territoire(territoire)
{
	if (confirm("Voulez-vous vraiment supprimer cet enregistrement ?"))
	{
		window.document.getElementById('terr-delete').value=territoire;
		window.document.territoires.submit();
	}
}


/* ----------------------------------------------------------- */
/* Cette fonction permet d'ajouter un prélèvement à une sortie */
/* ----------------------------------------------------------- */

function ajouter_prelevement()
{
	message = "";

	/* Espece */
	test_champ_vide("espece-new", "Espèce","","");

	/* Origine */
	test_champ_vide("origine-new", "Origine","","");

	/* Nombre */
	test_champ_vide("nbr-new", "Nombre","","");

	/* Age */
	/* test_champ_vide("age-new", "Age","",""); */

	/* Sexe */
	/* test_champ_vide("sexe-new", "Sexe","",""); */

	/* Grammes */
	/* test_champ_vide("grammes-new", "Poids en grammes","",""); *:

	/* Kilos */
	/* test_champ_vide("kilos-new", "Poids en kilos","",""); */
	
	// Si tous les champs sont bien renseignés
	if(message_form(message))
	{
		window.document.getElementById("ajouter_prelevement").value="ok";
		window.document.sorties.submit();
	}
}

/* -------------------------------------------------------- */
/* Cette fonction permet d'ajouter un piegeage à une sortie */
/* -------------------------------------------------------- */

function ajouter_piegeage()
{
	message = "";

	/* Espece */
	test_champ_vide("espece-new", "Espèce","","");

	/* Type de piège */
	test_champ_vide("tpiege-new", "Type de piège","","");

	/* Nombre */
	test_champ_vide("nbr-new", "Nombre","","");

	/* Age */
	/* test_champ_vide("age-new", "Age","",""); */

	/* Sexe */
	/* test_champ_vide("sexe-new", "Sexe","",""); */

	/* Grammes */
	/* test_champ_vide("grammes-new", "Poids en grammes","",""); */

	/* Kilos */
	/* test_champ_vide("kilos-new", "Poids en kilos","",""); */
	
	// Si tous les champs sont bien renseignés
	if(message_form(message))
	{
		window.document.getElementById("ajouter_piegeage").value="ok";
		window.document.sorties.submit();
	}
}

/* --------------------------------------------------------------- */
/* Idem que la fonction au-dessus ms sans alert js en cas d'erreur */
/* Cette fonction est appelée lorsque l'on sort des champs         */
/* --------------------------------------------------------------- */

function ajouter_prelevement_blur()
{
	message = "";

	/* Espece */
	test_champ_vide("espece-new", "Espèce","","");

	/* Origine */
	test_champ_vide("origine-new", "Origine","","");

	/* Nombre */
	test_champ_vide("nbr-new", "Nombre","","");

	/* Activation / Désactivation lien "Ajouter" */
	if( (window.document.getElementById("espece-new").value != "") && (window.document.getElementById("origine-new").value != "") && (window.document.getElementById("nbr-new").value != "") )
	{
		window.document.getElementById("ajouter-pre-lien-actif").style.display = "";
		window.document.getElementById("ajouter-pre-lien-inactif").style.display = "none";
	}
	else
	{
		window.document.getElementById("ajouter-pre-lien-actif").style.display = "none";
		window.document.getElementById("ajouter-pre-lien-inactif").style.display = "";
	}

	if(window.document.getElementById("nbr-new").value == 1)
	{
		/* Age */
		/* test_champ_vide("age-new", "Age","",""); */

		/* Sexe */
		/* test_champ_vide("sexe-new", "Sexe","",""); */

		/* Grammes */
		/* test_champ_vide("grammes-new", "Poids en grammes","",""); */

		/* Kilos */
		/* test_champ_vide("kilos-new", "Poids en kilos","",""); */
	}
	
	// Si tous les champs sont bien renseignés
	if((valid_form(message)) && (window.document.getElementById("nbr-new").value != 1))
	{
		window.document.getElementById("ajouter_prelevement").value="ok";
		window.document.sorties.submit();
	}
}


/* --------------------------------------------------------------------------------------- */
/* Cette fonction permet d'ajouter un prélèvement à une sortie sur click du lien "Ajouter" */
/* --------------------------------------------------------------------------------------- */

function ajouter_prelevement_click()
{
	message = "";

	/* Espece */
	test_champ_vide("espece-new", "Espèce","","");

	/* Origine */
	test_champ_vide("origine-new", "Origine","","");

	/* Nombre */
	test_champ_vide("nbr-new", "Nombre","","");
	
	// Si tous les champs sont bien renseignés
	if(message_form(message))
	{
		window.document.getElementById("ajouter_prelevement").value="ok";
		window.document.sorties.submit();
	}
}

/* --------------------------------------------------------------- */
/* Idem que la fonction au-dessus ms sans alert js en cas d'erreur */
/* Cette fonction est appelée lorsque l'on sort des champs         */
/* --------------------------------------------------------------- */

function ajouter_piegeage_blur()
{
	message = "";

	/* Espece */
	test_champ_vide("espece-new", "Espèce","","");

	/* Type de piège */
	test_champ_vide("tpiege-new", "Type de piège","","");

	/* Nombre */
	test_champ_vide("nbr-new", "Nombre","","");

	/* Activation / Désactivation lien "Ajouter" */
	if( (window.document.getElementById("espece-new").value != "") && (window.document.getElementById("tpiege-new").value != "") && (window.document.getElementById("nbr-new").value != "") )
	{
		window.document.getElementById("ajouter-pre-lien-actif").style.display = "";
		window.document.getElementById("ajouter-pre-lien-inactif").style.display = "none";
	}
	else
	{
		window.document.getElementById("ajouter-pre-lien-actif").style.display = "none";
		window.document.getElementById("ajouter-pre-lien-inactif").style.display = "";
	}

	if(window.document.getElementById("nbr-new").value == 1)
	{
		/* Age */
		/* test_champ_vide("age-new", "Age","",""); */

		/* Sexe */
		/* test_champ_vide("sexe-new", "Sexe","",""); */

		/* Grammes */
		/* test_champ_vide("grammes-new", "Poids en grammes","",""); */

		/* Kilos */
		/* test_champ_vide("kilos-new", "Poids en kilos","",""); */
	}
	
	// Si tous les champs sont bien renseignés
	if((valid_form(message)) && (window.document.getElementById("nbr-new").value != 1))
	{
		window.document.getElementById("ajouter_piegeage").value="ok";
		window.document.sorties.submit();
	}
}

/* ------------------------------------------------------------------------------------ */
/* Cette fonction permet d'ajouter un piegeage à une sortie sur click du lien "Ajouter" */
/* ------------------------------------------------------------------------------------ */

function ajouter_piegeage_click()
{
	message = "";

	/* Espece */
	test_champ_vide("espece-new", "Espèce","","");

	/* Origine */
	test_champ_vide("tpiege-new", "Type de piège","","");

	/* Nombre */
	test_champ_vide("nbr-new", "Nombre","","");
	
	// Si tous les champs sont bien renseignés
	if(message_form(message))
	{
		window.document.getElementById("ajouter_piegeage").value="ok";
		window.document.sorties.submit();
	}
}

/* -------------------------------------------------------------- */
/* Cette fonction permet de supprimer un prelevement d'une sortie */
/* -------------------------------------------------------------- */

function supprimer_prelevement(prelevement)
{
	if (confirm("Voulez-vous vraiment supprimer ce prelevement ?"))
	{
		window.document.getElementById('prel-delete').value=prelevement;
		window.document.sorties.action = "#prel";
		window.document.sorties.submit();
	}
}

/* ----------------------------------------------------------- */
/* Cette fonction permet de supprimer un piegeage d'une sortie */
/* ----------------------------------------------------------- */

function supprimer_piegeage(piegeage)
{
	if (confirm("Voulez-vous vraiment supprimer ce piegeage ?"))
	{
		window.document.getElementById('pieg-delete').value=piegeage;
		window.document.sorties.action = "#aCapture";
		window.document.sorties.submit();
	}
}

/* ------------------------------------------------------------ */
/* Cette fonction permet d'ajouter une observation à une sortie */
/* ------------------------------------------------------------ */

function ajouter_observation()
{
	message = "";

	/* Espece */
	test_champ_vide("obs-espece-new", "Espèce","","");

	/* Nombre blessés et/ou pas trouvé */
	test_champ_vide("obs-nbr-new", "Nombre blessés et/ou pas trouvé","","");

	/* Vus différents */
	test_champ_vide("obs-vue-new", "Vus différents","","");
	
	// Si tous les champs sont bien renseignés
	if(message_form(message))
	{
		window.document.getElementById("ajouter_observation").value="ok";
		window.document.sorties.submit();
	}
}

/* --------------------------------------------------------------- */
/* Idem que la fonction au-dessus ms sans alert js en cas d'erreur */
/* Cette fonction est appelée lorsque l'on sort des champs         */
/* --------------------------------------------------------------- */

function ajouter_observation_blur()
{
	//alert("ajouter_observation_blur");
	message = "";

	/* Espece */
	test_champ_vide("obs-espece-new", "Espèce","","");

	/* Nombre blessés et/ou pas trouvé */
	test_champ_vide("obs-nbr-new", "Nombre blessés et/ou pas trouvé","","");

	/* Vus différents */
	test_champ_vide("obs-vue-new", "Vus différents","","");

	// Si tous les champs sont bien renseignés
	if(valid_form(message))
	{
		/*
		 * Ce code "document.activeElement" ne marche que sous IE tant
		 * mieux car le try est là pour solutionner un pb uniquement sous IE
		 * lors que l'on ai sur un input de saisi d'observation, on exécute pas 
		 * le formulaire si le focus est placé sur le bouton "Valider"
		 */
		try
		{
			var btn = window.document.getElementById("btn-validerr");
			//alert( 'btn ' + btn );
			//alert( 'btn id ' + btn.id );
			//window.document.getElementById("btn-validerr").focus();
			//alert( 'qui a le faux cusse ' + document.activeElement );
			//alert( 'faux qsse id ' + document.activeElement.id );
			//alert( 'fo qse class ' + document.activeElement.className );
			
			if ( btn.id == document.activeElement.id )
			{
				//alert( 'pas de submit' );
				return;
			}
		}catch(ex){}
		
		window.document.getElementById("ajouter_observation").value="ok";
		window.document.sorties.submit();
	}
}


/* --------------------------------------------------------------- */
/* Cette fonction permet de supprimer une observation d'une sortie */
/* --------------------------------------------------------------- */

function supprimer_observation(observation)
{
	if (confirm("Voulez-vous vraiment supprimer cette observation ?"))
	{
		window.document.getElementById('obs-delete').value = observation;
		window.document.sorties.action = "#pied";
		window.document.sorties.submit();
	}
}

/* ----------------------------------------------------------------- */
/* Cette fonction permet de supprimer une observation d'observatoire */
/* ----------------------------------------------------------------- */

function supprimer_observatoire(observation)
{
	if (confirm("Voulez-vous vraiment supprimer cette observation ?"))
	{
		window.document.getElementById('obs-delete').value = observation;
		window.document.observations.action = "#pied";
		window.document.observations.submit();
	}
}


/* ------------------------------------------------------------------------------ */
/* Cette fonction permet d'ajouter un prelèvement et une observation à une sortie */
/* Elle est appelée lors du clic sur le bouton VALIDER (validation finale)        */
/* ------------------------------------------------------------------------------ */

function ajouter_prelevement_observation()
{
	//alert("ajouter_prelevement_observation a");
	try
	{
		window.document.getElementById("btn-validerr").focus();
	}catch(ex){}

	/* Partie TERRITOIRE */
	message = "";
	message_territoire = "";

	/* Territoire */
	test_champ_vide("territoire", "Territoire","","");

	/* Date */
	test_champ_vide("calendar_1", "Date de sortie","","");

	/* Mode de chasse */
	test_champ_vide("mode_chasse", "Mode de chasse","","");

	/* Bredouille */
	if( (!window.document.getElementById("bredouille_1").checked) && (!window.document.getElementById("bredouille_2").checked) )
	{
		message += " - Bredouille \r\n";
	}

	/* Arc */
	if( (!window.document.getElementById("arc_1").checked) && (!window.document.getElementById("arc_2").checked) )
	{
		message += " - Chasseur à l'arc \r\n";
	}

	message_territoire = message;
	
	
	
	/* Partie PRELEVEMENT */
	message = "";
	message_prelevement = "";

	/* Si un des champs est saisi, on oblige la saisie globale */
	if( (window.document.getElementById("espece-new").value!="") || (window.document.getElementById("origine-new").value!="") || (window.document.getElementById("nbr-new").value!="") || (window.document.getElementById("age-new").value!="") || (window.document.getElementById("sexe-new").value!="") || (window.document.getElementById("grammes-new").value!="") || (window.document.getElementById("kilos-new").value!="") )
	{
		/* Espece */
		test_champ_vide("espece-new", "Espèce","","");

		/* Origine */
		test_champ_vide("origine-new", "Origine","","");

		/* Nombre */
		test_champ_vide("nbr-new", "Nombre","","");

		/* Age */
		/* test_champ_vide("age-new", "Age","",""); */

		/* Sexe */
		/* test_champ_vide("sexe-new", "Sexe","",""); */

		/* Grammes */
		/* test_champ_vide("grammes-new", "Poids en grammes","",""); */

		/* Kilos */
		/* test_champ_vide("kilos-new", "Poids en kilos","",""); */

		message_prelevement = message;

	}
	else
	{
		message_prelevement = "no_add";
	}	


	/* Partie OBSERVATION */
	message = "";
	message_observation = "";

	/* Si un des champs est saisi, on oblige la saisie globale */
	if( (window.document.getElementById("obs-espece-new").value!="") || (window.document.getElementById("obs-nbr-new").value!="") || (window.document.getElementById("obs-vue-new").value!="") )
	{
		/* Espece */
		test_champ_vide("obs-espece-new", "Espèce","","");

		/* Nombre blessés et/ou pas trouvé */
		test_champ_vide("obs-nbr-new", "Nombre blessés et/ou pas trouvé","","");

		/* Vus différents */
		test_champ_vide("obs-vue-new", "Vus différents","","");

		message_observation = message;
	}
	else
	{
		message_observation = "no_add";
	}
	
	
	/* VALIDATION GLOBALE */
//alert("message_prelevement="+message_prelevement);
//alert("message_observation="+message_observation);

	if( (message_prelevement!="no_add") || (message_observation!="no_add") )
	{
//alert("je passe 1");
		if( (message_form(message_prelevement)) && (message_form(message_observation)) && (message_form(message_territoire)) )
		{
//alert("je passe 2");
			window.document.getElementById("ajouter_prelevement").value="ok";
			
			window.document.getElementById("ajouter_observation").value="ok";
			//alert("prelevement " + window.document.getElementById("ajouter_prelevement").value);
			//alert("observation " + window.document.getElementById("ajouter_observation").value);
			
			
			window.document.getElementById("validation_finale").value="ok";
			window.document.sorties.submit();
		}
	}
	else
	{
//alert("je passe 1B");
		if(message_form(message_territoire))
		{
//alert("je passe 2B");
			window.document.getElementById("validation_finale").value="ok";
			window.document.sorties.submit();
		}
	}
}

/* -------------------------------------------------------- */
/* Cette fonction permet d'ajouter une capture à une sortie */
/* Cette fonction est appelée lorsque l'on sort des champs  */
/* -------------------------------------------------------- */

function ajouter_capture_blur()
{
	message = "";

	/* Espece */
	test_champ_vide("cap-espece-new", "Espèce","","");

	/* Nombre de relachés */
	test_champ_vide("cap-nbr-new", "Nombre de relachés","","");
	
	// Si tous les champs sont bien renseignés
	if(valid_form(message))
	{
		window.document.getElementById("ajouter_capture").value="ok";
		window.document.sorties.submit();
	}
}


/* ----------------------------------------------------------- */
/* Cette fonction permet de supprimer une capture d'une sortie */
/* ----------------------------------------------------------- */

function supprimer_capture(observation)
{
	if (confirm("Voulez-vous vraiment supprimer cette capture ?"))
	{
		window.document.getElementById('cap-delete').value=observation;
		window.document.sorties.submit();
	}
}

/* --------------------------------------------------------------------------- */
/* Cette fonction permet d'ajouter un piegeage et une capture à une sortie     */
/* Elle est appelée lors du clic sur le bouton VALIDER (validation finale)     */
/* --------------------------------------------------------------------------- */

function ajouter_piegeage_capture_blur()
{
	/* Partie TERRITOIRE */
	message = "";
	message_territoire = "";

	/* Territoire */
	test_champ_vide("territoire", "Territoire","","");

	/* Date */
	test_champ_vide("calendar_1", "Date de sortie","","");

	/* Mode de chasse */
	test_champ_vide("lieu_piege", "Lieu de le capture","","");

	message_territoire = message;
	
	
	
	/* Partie PIEGEAGE */
	message = "";
	message_piegeage = "";

	/* Si un des champs est saisi, on oblige la saisie globale */
	//if( (window.document.getElementById("espece-new").value!="") || (window.document.getElementById("tpiege-new").value!="") || (window.document.getElementById("nbr-new").value!="") || (window.document.getElementById("age-new").value!="") || (window.document.getElementById("sexe-new").value!="") || (window.document.getElementById("grammes-new").value!="") || (window.document.getElementById("kilos-new").value!="") )
	if( (window.document.getElementById("espece-new").value!="") || (window.document.getElementById("nbr-new").value!="") || (window.document.getElementById("age-new").value!="") || (window.document.getElementById("sexe-new").value!="") || (window.document.getElementById("grammes-new").value!="") || (window.document.getElementById("kilos-new").value!="") )
	{
		/* Espece */
		test_champ_vide("espece-new", "Espèce","","");

		/* Type de piège */
		test_champ_vide("tpiege-new", "Type de piège","","");

		/* Nombre */
		test_champ_vide("nbr-new", "Nombre","","");

		/* Age */
		/* test_champ_vide("age-new", "Age","",""); */

		/* Sexe */
		/* test_champ_vide("sexe-new", "Sexe","",""); */

		/* Grammes */
		/* test_champ_vide("grammes-new", "Poids en grammes","",""); */

		/* Kilos */
		/* test_champ_vide("kilos-new", "Poids en kilos","",""); */

		message_piegeage = message;
	}
	else
	{
		message_piegeage = "no_add";
	}	


	/* Partie CAPTURE */
	message = "";
	message_capture = "";

	/* Si un des champs est saisi, on oblige la saisie globale */
	if( (window.document.getElementById("cap-espece-new").value!="") || (window.document.getElementById("cap-nbr-new").value!="") )
	{
		/* Espece */
		test_champ_vide("cap-espece-new", "Espèce","","");

		/* Nombre de relachés */
		test_champ_vide("cap-nbr-new", "Nombre de relachés","","");

		message_capture = message;
	}
	else
	{
		message_capture = "no_add";
	}
	
	
	/* VALIDATION GLOBALE */
	if( (message_piegeage!="no_add") || (message_capture!="no_add") )
	{
		if( (message_form(message_piegeage)) && (message_form(message_capture)) && (message_form(message_territoire)) )
		{
			window.document.getElementById("ajouter_piegeage").value="ok";
			window.document.getElementById("ajouter_capture").value="ok";
			window.document.getElementById("validation_finale").value="ok";
			window.document.sorties.submit();
		}
	}
	else
	{
		if(message_form(message_territoire))
		{
			window.document.getElementById("validation_finale").value="ok";
			window.document.sorties.submit();
		}
	}
}

/* ------------------------------------------------------------------------------ */
/* Cette fonction permet d'ajouter un prelèvement et une observation à une sortie */
/* Elle est utilisée sur le script de visualisation d'une sortie				  */
/* Elle est appelée lors du clic sur le bouton VALIDER (validation finale)        */
/* ------------------------------------------------------------------------------ */

function visu_sortie_ajouter_prelevement_observation()
{
	/* Partie RECHERCHE */
	message = "";
	message_recherche = "";
	
	/* territoire */
	test_champ_vide("territoire_select", "Territoire","","");

	/* date de sortie */
	test_champ_vide("calendar_1", "Date de la sortie","","");

	/* territoire */
	test_champ_vide("mode_chasse_select", "Mode de chasse","","");

	message_recherche = message;
	
	/* Partie PRELEVEMENT */
	message_prelevement = "";

	/* Si un des champs est saisi, on oblige la saisie globale */
	if( (window.document.getElementById("espece-new").value!="") || (window.document.getElementById("origine-new").value!="") || (window.document.getElementById("nbr-new").value!="") || (window.document.getElementById("age-new").value!="") || (window.document.getElementById("sexe-new").value!="") || (window.document.getElementById("grammes-new").value!="") || (window.document.getElementById("kilos-new").value!="") )
	{
		/* Espece */
		test_champ_vide("espece-new", "Espèce","","");

		/* Origine */
		test_champ_vide("origine-new", "Origine","","");

		/* Nombre */
		test_champ_vide("nbr-new", "Nombre","","");

		/* Age */
		/* test_champ_vide("age-new", "Age","",""); */

		/* Sexe */
		/* test_champ_vide("sexe-new", "Sexe","",""); */

		/* Grammes */
		/* test_champ_vide("grammes-new", "Poids en grammes","",""); */

		/* Kilos */
		/* test_champ_vide("kilos-new", "Poids en kilos","",""); */

		message_prelevement = message;
	}
	else
	{
		message_prelevement = "Prélèvement ou observation en cours de saisie";
	}	


	/* Partie OBSERVATION */
	message = "";
	message_observation = "";

	/* Si un des champs est saisi, on oblige la saisie globale */
	if( (window.document.getElementById("obs-espece-new").value!="") || (window.document.getElementById("obs-nbr-new").value!="") || (window.document.getElementById("obs-vue-new").value!="") )
	{
		/* Espece */
		test_champ_vide("obs-espece-new", "Espèce","","");

		/* Nombre blessés et/ou pas trouvé */
		test_champ_vide("obs-nbr-new", "Nombre blessés et/ou pas trouvé","","");

		/* Vus différents */
		test_champ_vide("obs-vue-new", "Vus différents","","");

		message_observation = message;
	}
	else
	{
		message_observation = "Prélèvement ou observation en cours de saisie";
	}
	
	
	/* VALIDATION GLOBALE */
	if( (message_prelevement!="Prélèvement ou observation en cours de saisie") || (message_observation!="Prélèvement ou observation en cours de saisie") )
	{
		if( (message_form(message_prelevement)) && (message_form(message_observation)) )
		{
			window.document.getElementById("ajouter_prelevement").value="ok";
			window.document.getElementById("ajouter_observation").value="ok";
			window.document.getElementById("validation_finale").value="ok";
			window.document.sorties.submit();
		}
	}
	else
	{
		if( message_form(message_recherche) )
		{
			window.document.getElementById("validation_finale").value="ok";
			window.document.sorties.submit();
		}
	}
}

/* ----------------------------------------------------------------------- */
/* Cette fonction permet d'ajouter un piegeage et une capture à une sortie */
/* Elle est utilisée sur le script de visualisation d'une sortie		   */
/* Elle est appelée lors du clic sur le bouton VALIDER (validation finale) */
/* ----------------------------------------------------------------------- */

function visu_sortie_ajouter_piegeage_capture()
{
	
	/* Partie PIEGEAGE */
	message = "";
	message_piegeage = "";

	/* Si un des champs est saisi, on oblige la saisie globale */
	if( (window.document.getElementById("espece-new").value!="") || (window.document.getElementById("tpiege-new").value!="") || (window.document.getElementById("nbr-new").value!="") || (window.document.getElementById("age-new").value!="") || (window.document.getElementById("sexe-new").value!="") || (window.document.getElementById("grammes-new").value!="") || (window.document.getElementById("kilos-new").value!="") )
	{
		/* Espece */
		test_champ_vide("espece-new", "Espèce","","");

		/* Type de piège */
		test_champ_vide("tpiege-new", "Type de piège","","");

		/* Nombre */
		test_champ_vide("nbr-new", "Nombre","","");

		/* Age */
		/* test_champ_vide("age-new", "Age","",""); */

		/* Sexe */
		/* test_champ_vide("sexe-new", "Sexe","",""); */

		/* Grammes */
		/* test_champ_vide("grammes-new", "Poids en grammes","",""); */

		/* Kilos */
		/* test_champ_vide("kilos-new", "Poids en kilos","",""); */

		message_piegeage = message;
	}
	else
	{
		message_piegeage = "no_add";
	}	


	/* Partie CAPTURE */
	message = "";
	message_capture = "";

	/* Si un des champs est saisi, on oblige la saisie globale */
	if( (window.document.getElementById("cap-espece-new").value!="") || (window.document.getElementById("cap-nbr-new").value!="") )
	{
		/* Espece */
		test_champ_vide("cap-espece-new", "Espèce","","");

		/* Nombre de relachés */
		test_champ_vide("cap-nbr-new", "Nombre de relachés","","");

		message_capture = message;
	}
	else
	{
		message_capture = "no_add";
	}
	
	
	/* VALIDATION GLOBALE */
	if( (message_piegeage!="no_add") || (message_capture!="no_add") )
	{
		if( (message_form(message_piegeage)) && (message_form(message_capture)) )
		{
			window.document.getElementById("ajouter_piegeage").value="ok";
			window.document.getElementById("ajouter_capture").value="ok";
			window.document.getElementById("validation_finale").value="ok";
			window.document.sorties.submit();
		}
	}
	else
	{
		window.document.getElementById("validation_finale").value="ok";
		window.document.sorties.submit();
	}
}


/* --------------------------------------------------------- */
/* Cette fonction permet d'interdir la saisie de prelèvement */
/* --------------------------------------------------------- */

function interdire_saisie_prelevement()
{
	if(window.document.getElementById("divPrel"))
		window.document.getElementById("divPrel").style.display = "none";
	window.document.getElementById("divBredouilleOui").style.display = "block";
	window.document.getElementById("divBredouilleNon").style.display = "none";
	/*
	window.document.getElementById("espece-new").disabled='disabled';
	window.document.getElementById("origine-new").disabled='disabled';
	window.document.getElementById("nbr-new").disabled='disabled';
	window.document.getElementById("age-new").disabled='disabled';
	window.document.getElementById("sexe-new").disabled='disabled';
	window.document.getElementById("grammes-new").disabled='disabled';
	window.document.getElementById("kilos-new").disabled='disabled';
	*/
}

/* ---------------------------------------------------------- */
/* Cette fonction permet d'autoriser la saisie de prelèvement */
/* ---------------------------------------------------------- */

function autoriser_saisie_prelevement()
{
	if(window.document.getElementById("divPrel"))
		window.document.getElementById("divPrel").style.display = "block";
	window.document.getElementById("divBredouilleOui").style.display = "none";
	window.document.getElementById("divBredouilleNon").style.display = "block";
	/*
	window.document.getElementById("espece-new").disabled='';
	window.document.getElementById("origine-new").disabled='';
	window.document.getElementById("nbr-new").disabled='';
	window.document.getElementById("age-new").disabled='';
	window.document.getElementById("sexe-new").disabled='';
	window.document.getElementById("grammes-new").disabled='';
	window.document.getElementById("kilos-new").disabled='';
	*/
}


/* ---------------------------------------------------------------- */
/* Cette fonction permet d'ajouter une observation à l'observatoire */
/* Cette fonction est appelée sur validation du formulaire		    */
/* ---------------------------------------------------------------- */

function observatoire_ajouter_observation()
{
	message = "";

	/* Département */
	test_champ_vide("departement-new", "Département","","");

	/* Commune */
	test_champ_vide("commune-new", "Commune","","");

	/* Espece */
	test_champ_vide("espece-new", "Espèce","","");

	/* Type d'observation */
	test_champ_vide("type-observation-new", "Type d'observation","","");

	/* Date */
	test_champ_vide("calendar_1", "Date de la sortie","","");

	/* Nombre */
	test_champ_vide("nbr-new", "Nombre","","");

	/* Commentaires */
	/* test_textarea_vide("commentaires-new", "Commentaires","",""); */
	
	// Si tous les champs sont bien renseignés
	if(message_form(message))
	{
		window.document.getElementById("ajouter_observation").value="ok";
		window.document.observations.submit();
	}
}

/* ------------------------------------------------------------------------------------------------------------ */
/* SAISIE ET SUIVI DES CARNETS FNC (Fédération)																	*/
/* Cette fonction permet de valider l'entete de choix d'ID de chasseur/Agrément piegeur et de type de carnet	*/
/* Cette fonction est appelée sur clic du bouton "Valider"									  		            */
/* ------------------------------------------------------------------------------------------------------------ */
function validFormEnteteCarnet(type) {
	// Saisie CPU
	if(type == "PRE") {
		if( (window.document.getElementById("id_chasseur").value == "") && (!window.document.getElementById("chasseur_anonyme").checked) ) {
			alert("Vous devez saisir un N° identifiant chasseur ou cocher la case anonyme");
		}
		else {
			window.document.suivi_carnets_1.submit();
		}
	}
	// Saisie PIEGEAGE
	else if(type == "PIE") {
		if( (window.document.getElementById("num_piegeur").value == "") && (!window.document.getElementById("piegeur_anonyme").checked) ) {
			alert("Vous devez saisir un N° agrément piègeur ou cocher la case anonyme");
		}
		else {
			window.document.suivi_carnets_1.submit();
		}
	}
}

function ajTerritoire() {
	if(window.document.suivi_carnets_2.dptnew.value != "" && window.document.suivi_carnets_2.commnew.value != "0") {
		window.document.suivi_carnets_2.ajTer.value = 1;
		window.document.suivi_carnets_2.submit();
	}
	else {
		alert("Veuillez renseigner tous les champs");
	}
}

function supTerritoire(numTer) {
	if(confirm("Etes-vous sûr(e) de vouloir supprimer ce territoire ?")) {
		window.document.suivi_carnets_2.supTer.value = numTer;
		window.document.suivi_carnets_2.submit();
	}
}

function validFormTerritoireCarnet(type) {
	window.document.suivi_carnets_2.submit();
}

/* --------------------------------------------- */
/* SAISIE ET SUIVI DES CARNETS FNC (Fédération)  */
/* Cette fonction permet de changer de mois		 */
/* Cette fonction est appelée sur clic d'un mois */
/* --------------------------------------------- */

function suivi_carnet_change_mois(mois, annee)
{
	window.document.suivi_carnets_3.mois_suivant.value = mois;
	window.document.suivi_carnets_3.annee_suivante.value = annee;
	validSuiviCarnets3();
}


/* ------------------------------------------------------------------------ */
/* SAISIE ET SUIVI DES CARNETS FNC (Fédération)								*/
/* Cette fonction permet de tester la présence d'une valeur dans un tableau */
/* Si ce n'est pas le cas, le champ (paramètre 3) est mis à vide			*/
/* ------------------------------------------------------------------------ */

function presence_valeur_array(nom_array, valeur, champ)
{
	var valeur_presente = false;
	
	for( var n = 0 ; n < nom_array.length ; n++)
	{
		if(nom_array[n] == valeur)
		{
			valeur_presente = true;
		}
	}

	if(valeur_presente == false)
	{
		window.document.getElementById(champ).value="";
	}
}

/* ----------------------------------------------------- */
/* Idem que la précédente mais recherche dans 2 tableaux */
/* ----------------------------------------------------- */

function presence_valeur_2array(nom_array, nom_array2, valeur, champ)
{
	var valeur_presente = false;
	
	// Tableau 1
	for( var n = 0 ; n < nom_array.length ; n++)
	{
		if(nom_array[n] == valeur)
		{
			valeur_presente = true;
		}
	}

	// Tableau 2
	for( var n = 0 ; n < nom_array2.length ; n++)
	{
		if(nom_array2[n] == valeur)
		{
			valeur_presente = true;
		}
	}

	if(valeur_presente == false)
	{
		window.document.getElementById(champ).value="";
	}
}

/* ----------------------------------------------------- */
/* Idem que la précédente mais recherche dans 3 tableaux */
/* ----------------------------------------------------- */

function presence_valeur_3array(nom_array, nom_array2, nom_array3, valeur, champ)
{
	var valeur_presente = false;
	
	// Tableau 1
	for( var n = 0 ; n < nom_array.length ; n++)
	{
		if(nom_array[n] == valeur)
		{
			valeur_presente = true;
		}
	}

	// Tableau 2
	for( var n = 0 ; n < nom_array2.length ; n++)
	{
		if(nom_array2[n] == valeur)
		{
			valeur_presente = true;
		}
	}

	// Tableau 3
	for( var n = 0 ; n < nom_array3.length ; n++)
	{
		if(nom_array3[n] == valeur)
		{
			valeur_presente = true;
		}
	}

	if(valeur_presente == false)
	{
		window.document.getElementById(champ).value="";
	}
}

/* ------------------------------------------------------------------------- */
/* SAISIE ET SUIVI DES CARNETS FNC (Fédération)								 */
/* Cette fonction permet de stocker la valeur d'une checkbox dans un tableau */
/* Cette fonction est appelée sur cochage d'un mode de chasse				 */
/* ------------------------------------------------------------------------- */

function stockage_chk_array(nom_array, valeur_chk, etat_chk)
{
	if(etat_chk == true) // ajout élément dans le tableau
	{
		nom_array.push(valeur_chk);
	}
	else // suppression de l'élément dans le tableau
	{
		for( var n = 0 ; n < nom_array.length ; n++)
		{
			if(nom_array[n] == valeur_chk)
			{
				nom_array.splice(n, 1);
			}
		}

		// vidage des dates de piegeages ayant la valeur de cette checkbox

		/*if(valeur_chk.length == 1) // ajoute le "0" devant les nombres à 1 chiffre
		{
			valeur_chk = "0" + valeur_chk;
		}

		for( var d = 1 ; d <= 12 ; d++)
		{
			if(window.document.getElementById("num_date_"+d).value == valeur_chk)
			{
				window.document.getElementById("num_date_"+d).value = "";
			}
		}*/
	}
	nom_array.sort();
}

/* ---------------------------------------------- */
/* SAISIE ET SUIVI DES CARNETS FNC (Fédération)   */
/* Cette fonction permet de supprimer un piegeage */
/* ---------------------------------------------- */

function supprimer_piegeage_fnc(piegeage) {
	if (confirm("Voulez-vous vraiment supprimer ce piegeage ?")) {
		window.document.suivi_carnets_3.supPiegeage.value = piegeage;
		validSuiviCarnets3();
	}
}

function supprimer_capture_fnc(capture) {
	if (confirm("Voulez-vous vraiment supprimer cette capture accidentelle ?")) {
		window.document.suivi_carnets_3.supCapture.value = capture;
		validSuiviCarnets3();
	}
}

function supprimer_prelevement_fnc(prelevement) {
	if (confirm("Voulez-vous vraiment supprimer ce prélèvement ?")) {
		window.document.suivi_carnets_3.supPrel.value = prelevement;
		validSuiviCarnets3();
	}
}

/* --------------------------------------------------------- */
/* SAISIE ET SUIVI DES CARNETS FDC (Fédération)				 */
/* Cette fonction permet de visualiser le détail d'un carnet */
/* --------------------------------------------------------- */

function detail_carnet_fdc(num, type, id)
{
	if(type == "PRE")
	{
		if((num == "Anonyme") && (id != ""))
		{
			window.document.getElementById("chasseur_anonyme").checked = true;
			window.document.getElementById("chasseur_id").value = id;
		}
		else
		{
			window.document.getElementById("id_chasseur").value = num;
		}
		window.document.suivi_carnets.action = "cpu-fnc-saisie-01.php";
	}
	else if(type == "PIE")
	{
		if((num == "Anonyme") && (id != ""))
		{
			window.document.getElementById("piegeur_anonyme").checked = true;
			window.document.getElementById("piegeur_id").value = id;
		}
		else
		{
			window.document.getElementById("num_piegeur").value = num;
		}

		window.document.suivi_carnets.action = "cpu-fnc-saisie-02.php";
	}
	
	window.document.suivi_carnets.submit();
}

function goToPage(numPage) {
	window.document.suivi_carnets_3.pageSaisie.value = numPage;
	validSuiviCarnets3();
}

function supCarnet(numCarnet) {
	if (confirm("Voulez-vous vraiment supprimer ce carnet ?")) {
		window.document.suivi_carnets_1.etape.value = "0";
		window.document.suivi_carnets_1.supCarnet.value = numCarnet;
		window.document.suivi_carnets_1.submit();
	}
}

function sortieIsEditable(chk, isEditable) {
	if(!isEditable) {
		chk.checked = !chk.checked;
		alert("Cette sortie a fait l'objet d'une saisie par le chasseur internaute,\nvous ne pouvez pas la modifier.");
	}
	
	return isEditable;
}

function validFormSortie(myForm, idSortie, validationFinale) {
	var msg = "";
	
	if(myForm.territoire.value == "") {
		msg += " - Territoire, commune et type de territoire\n";
	}
	if(myForm.date_sortie.value == "") {
		msg += " - Date de sortie\n";
	}
	if(!myForm.bredouille[0].checked && !myForm.bredouille[1].checked) {
		msg += " - Bredouille\n";
	}
	if(myForm.mode_chasse.value == "") {
		msg += " - Mode de chasse\n";
	}
	
	if(msg != "") {
		alert("Veuillez renseigner le(s) champ(s) suivant(s) :\n"+msg);
	}
	else {
		if(idSortie == "")
			myForm.ajSortie.value = "1";
		
		if(validationFinale) {
			if(myForm.bredouille[1].checked && myForm.nbPrel.value == "0")
				alert("Veuillez renseigner au moins un prélèvement");
			else {
				myForm.validation_finale.value = "ok";
				myForm.submit();
			}
		}
		else myForm.submit();
	}
}

function validFormPiegeage(myForm, idSortie, validationFinale) {
	var msg = "";
	
	if(myForm.territoire.value == "") {
		msg += " - Territoire et commune\n";
	}
	if(myForm.date_sortie.value == "") {
		msg += " - Date de la capture\n";
	}
	if(myForm.lieu_piege.value == "") {
		msg += " - Lieu de la capture\n";
	}
	
	if(msg != "") {
		alert("Veuillez renseigner le(s) champ(s) suivant(s) :\n"+msg);
	}
	else {
		if(idSortie == "")
			myForm.ajSortie.value = "1";
		
		if(validationFinale) {
			if(myForm.nbCapture.value == "0")
				alert("Veuillez renseigner au moins une capture");
			else {
				myForm.validation_finale.value = "ok";
				myForm.submit();
			}
		}
		else myForm.submit();
	}
}

function validObsChasseur(myForm) {
	if (myForm.obsespecenew.value == "" || myForm.obsnbrnew.value == "" || myForm.obsvuenew.value == "") {
		alert("Pour valider une nouvelle observation, les 3 champs sont obligatoires");
	}
	else {
		window.document.getElementById("ajouter_observation").value = "ok";
		myForm.action = "#pied";
		myForm.submit();
	}
}

function validObsPieChasseur(myForm) {
	if (myForm.capespecenew.value == "" || myForm.capnbrnew.value == "") {
		alert("Pour valider une nouvelle observation, les 2 champs sont obligatoires");
	}
	else {
		window.document.getElementById("ajouter_capture").value = "ok";
		myForm.action = "#pied";
		myForm.submit();
	}
}

function validPrelChasseur(myForm) {
	var msg = "";
	
	if(myForm.especenew.value == "")
		msg += " - Espèce\n";
	if(myForm.nbrnew.value == "")
		msg += " - Nbr\n";
	
	if(msg != "") {
		alert("Veuillez rensigner le(s) champ(s) suivant(s) :\n"+msg);
	}
	else {
		window.document.getElementById("ajouter_prelevement").value = "ok";
		myForm.action = "#prel";
		myForm.submit();
	}
}

function validPiegeageChasseur(myForm) {
	var msg = "";
	
	if(myForm.especenew.value == "")
		msg += " - Espèce\n";
	if(myForm.tpiegenew.value == "")
		msg += " - Type de piège\n";
	if(myForm.nbrnew.value == "")
		msg += " - Nbr\n";
	
	if(msg != "") {
		alert("Veuillez rensigner le(s) champ(s) suivant(s) :\n"+msg);
	}
	else {
		window.document.getElementById("ajouter_piegeage").value = "ok";
		myForm.action = "#aCapture";
		myForm.submit();
	}
}

function showHideCom() {
	var myDiv = window.document.getElementById("divCom");
	
	if(myDiv.style.display == "none")
		 myDiv.style.display = "block";
	else myDiv.style.display = "none";
}

function validSuiviCarnets3() {
	window.document.suivi_carnets_3.submit();
}

function searchPreModifSortie(idSortie) {
	window.document.sorties.idModifSortie.value = idSortie;
	window.document.sorties.action = "cpu-prelevement-sorties-02.php";
	window.document.sorties.submit();
}

function searchPieModifSortie(idSortie) {
	window.document.sorties.idModifSortie.value = idSortie;
	window.document.sorties.action = "cpu-piegeage-sorties-02.php";
	window.document.sorties.submit();
}

function searchSupSortie(idSortie) {
	if(confirm("Voulez-vous réellement supprimer cette sortie ?")) {
		window.document.sorties.idSupSortie.value = idSortie;
		window.document.sorties.submit();
	}
}

function printPreSortie(myForm, mode) {
	var url = "cpu-prelevement-sorties-print-01.php?";
	
	url += "mode="+mode;
	url += "&saison="+myForm.saison.value;
	url += "&cter="+myForm.print_cter.value;
	url += "&cdatedeb="+myForm.print_cdatedeb.value;
	url += "&cdatefin="+myForm.print_cdatefin.value;
	url += "&cmodechasse="+myForm.print_cmodechasse.value;
	url += "&cespece="+myForm.print_cespece.value;
	url += "&cbredouille="+myForm.print_cbredouille.value;
	
	if(mode == "print") {
		Shadowbox.open({
		    content:    url,
		    player:     'iframe',
		    height:		'700',
		    width:		'700'
		});
	}
	else {
		window.document.sorties.target = "_blank";
		window.document.sorties.action = url;
		window.document.sorties.submit();
		
		window.document.sorties.target = "";
		window.document.sorties.action = "cpu-prelevement-sorties-01.php";
	}
}

function printPieSortie(myForm, mode) {
	var url = "cpu-piegeage-sorties-print-01.php?";
	url += "mode="+mode;
	url += "&saison="+myForm.saison.value;
	url += "&cter="+myForm.print_cter.value;
	url += "&cdatedeb="+myForm.print_cdatedeb.value;
	url += "&cdatefin="+myForm.print_cdatefin.value;
	url += "&clieucapture="+myForm.print_clieucapture.value;
	
	if(mode == "print") {
		Shadowbox.open({
		    content:    url,
		    player:     'iframe',
		    height:		'700',
		    width:		'700'
		});
	}
	else {
		window.document.sorties.target = "_blank";
		window.document.sorties.action = url;
		window.document.sorties.submit();
		
		window.document.sorties.target = "";
		window.document.sorties.action = "cpu-piegeage-sorties-01.php";
	}
}
