/*-----------------------------------------------------------------------------------------------*/
//
// Copyright (c) 2008 Agence Clark (http://www.agence-clark.com)
//
/*-----------------------------------------------------------------------------------------------*/

/*-----------------------------------------------------------------------------------------------*/
// Init Page
/*-----------------------------------------------------------------------------------------------*/
Event.observe(window, 'load', InitPage, false);
function InitPage(){
	
	if($('liste_alertes')){
		// Ouverture des alertes
		$$('#liste_alertes .titre a.open').each(function(e,i){
			var fathers = e.ancestors();
			Event.observe(e,'click',function(){ OpenClose(e,fathers[1].next()) });
			
		});
		$$('#liste_alertes .titre li.edit a').each(function(e,i){
			var a = e.ancestors()[2].down(1);
			var div = e.ancestors()[2].next();
			//var fathers = e.ancestors();
			Event.observe(e,'click',function(){ OpenClose(a,div) });			
		});
		
		// Ouverture dans l'alerte
		var n = false;
		$$('#liste_alertes fieldset').each(function(e,i){
			var children = e.childElements();
			if(!n)
			{
				OpenClose(children[0].childElements()[0],children[1]);
				n = true;
			}
			Event.observe(children[0].childElements()[0],'click',function(){ OpenClose(children[0].childElements()[0],children[1]) });
		});
		
		// Ajouter destinataire
		$$('#liste_alertes a.bt_add_dest').each(function(e,i){
			Event.observe(e,'click',function(){ ajoutDestinataire(e); });
		});
		
		// Calendrier
		$$('#liste_alertes input.calendrier').each(function(e,i){
			
			Event.observe(e,'click',function(){ displayCalendar(this,'dd/mm/yyyy',this,false,false); });
		});		
	}
}

/*-----------------------------------------------------------------------------------------------*/
// Fonctions
/*-----------------------------------------------------------------------------------------------*/
//-- Ajouter Destinataire --//
var ajoutDestinataire = function(e){
	var div = e.ancestors()[1];	
	var num_id = div.id.substr(div.id.lastIndexOf('_')+1);
	var nb = $$('#liste_alertes #cont_ami_'+num_id+' input').size();
	if(nb<=4){
		var valeur_template = {id: 'altAmi1_'+num_id+'_'+nb+1, lib:cl_app_email_ami, num_id:num_id};
		var template = new Template('<div class="sep"><label for="#{id}">#{lib}</label><input class="imputalert2" type="text" id="#{id}" name="altAmi1_#{num_id}[]" /></div>');
		var nouveau = template.evaluate(valeur_template);
		new Insertion.Bottom($('cont_ami_'+num_id), nouveau);
	}else ModalInfo(cl_app_attention, cl_app_add_ami_max_err);
}
								
/*-----------------------------------------------------------------------------------------------*/
// Verification de formulaires
/*-----------------------------------------------------------------------------------------------*/
//-- Verif Nouvelle alerte --//
var frmAlerte = function(form) {
	param_form = { mep: 'modalbox', autoScroll: false, action: 'submit'};
	//altPays: {alerte:cl_app_pays_obl},
	champs_form = {
		altEmail: {type:'mel', alerte:cl_app_email_obl},
		//altRegion: {alerte:cl_app_region_obl},
		altDateDepart: {alerte:cl_app_datedep_obl}
	};
	return formVerif(form.id, champs_form, param_form, {titreMessage: cl_app_err_suiv, titreAttention:cl_app_attention});
};