ANEOL = {

	validation: {//{{{
		validate: function(){//{{{
			var ok=true;
  			var i=0;			
			while(ok && i < arguments.length){
				ok = arguments[i](arguments[i+1]);
				i = i+2;
			}
			return ok;
		},//}}}
		execOnReturn: function(e, func){//{{{
			var key = (e.keyPress) ? e.keyPress : e.which;
			if(key == 13){
		   		func();		
			}
		},//}}}
		required: function(o){//{{{
			if(o.field.value == ""){
				if(o.msg == undefined){
					if(o.label == undefined){
						o.label = o.field.name;
					}
					o.msg = "Le champs "+o.label+" est obligatoire !";
				}
				ANEOL.visualisation.displayMsg(o.msg);
				o.field.focus();
				return false;
			}
			return true
		},//}}}
		email: function(o){//{{{
			if(o.field.value !=""){
				var reg = /^[a-z0-9._-]+@[a-z0-9.-]{2,}[.][a-z]{2,3}$/;
				if(reg.exec(o.field.value)==null){
					if(o.msg == undefined){
						if(o.label == undefined){
							o.label = o.field.name;
						}
						o.msg = "Le champs "+o.label+" n'est pas valide !";
					}
					ANEOL.visualisation.displayMsg(o.msg);
					o.field.focus();
					return false;
				}
			}		
			return true;
		},//}}}
		max: function(o){//{{{
			if(o.field.value !=""){
				if(o.field.value.length > o.size){
					if(o.msg == undefined){
						if(o.label == undefined){
							o.label = o.field.name;
						}
						o.msg = "Le champs "+o.label+" ne doit pas dépasser "+o.size+" caractères !";
					}
					ANEOL.visualisation.displayMsg(o.msg);
					o.field.focus();
					return false;
				}
			}		
			return true;
		}//}}}
	},//}}}
	visualisation:{//{{{
		showElement: function (id, animate){//{{{
			try{
				var elt = document.getElementById(id);
				elt.style.display = "block";
				elt.style.visibility = "visible";
				if(animate == true){
					var attributes = { 
						opacity: {from:0.0, to:1.0}
					}; 
					var anim = new YAHOO.util.Anim(elt, attributes); 
					anim.animate();
				}
			}catch(e){ }
		},//}}}
		hideElement: function (id, animate){//{{{
			try{
				var elt = document.getElementById(id);
				if(animate == true){
					var anim = new YAHOO.util.Anim(elt, { opacity: {from:1.0, to:0.0} }); 
					anim.onComplete.subscribe(function(){
						elt.style.display = "none";
						elt.style.visibility = "hidden";
					});
					anim.animate();
				}else{
					elt.style.display = "none";
					elt.style.visibility = "hidden";
				}
			}catch(e){ }
		},//}}}
		showHide: function (id){//{{{
				var elt = document.getElementById(id);
				if(elt.style.display == "none"){
					ANEOL.visualisation.showElement(id, true);
				}else{
					ANEOL.visualisation.hideElement(id, true);
				}
		},//}}}
		displayMsg: function (text, className){//{{{
			var msg = document.getElementById('msg');
			if(msg != undefined){			
				msg.style.display = 'block';
				msg.style.visibility = 'visible';
				msg.innerHTML = text;
				if(className != undefined){
					msg.className = className;
				}else{
					msg.className = 'error';
				}
			}else{
				msg.style.display = 'none';
				msg.style.visibility = 'hidden';
				msg.innerHTML = "";
			}
		}//}}}
	},//}}}
	internationalisation:{//{{{
		FrenchCalendar:{
			"title":"Sélectionnez une date:",
			"close":true,
			"LOCALE_WEEKDAYS": "short",
			"START_WEEKDAY": 1, 
			"MULTI_SELECT": false,
			"DATE_FIELD_DELIMITER": "/",
			"MDY_DAY_POSITION": 1, 
			"MDY_MONTH_POSITION": 2, 
			"MDY_YEAR_POSITION": 3, 
			"MD_DAY_POSITION": 1, 
			"MD_MONTH_POSITION": 2, 
			"MONTHS_SHORT":   ["Janv", "Févr", "Mars", "Avr", "Mai", "Juin", "Juil", "Août", "Sept", "Oct", "Nov", "Déc"],
			"MONTHS_LONG":    ["Janvier", "Février", "Mars", "Avril", "Mai", "Juin", "Juillet", "Août", "Septembre", "Octobre", "Novembre", "Décembre"],
			"WEEKDAYS_1CHAR": ["D", "L", "M", "M", "J", "V", "S"],
			"WEEKDAYS_SHORT": ["Di", "Lu", "Ma", "Me", "Je", "Ve", "Sa"],
			"WEEKDAYS_MEDIUM":["Dim", "Lun", "Mar", "Mer", "Jeu", "Ven", "Sam"],
			"WEEKDAYS_LONG":  ["Dimanche", "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi"]
		}
	},//}}}
	editor:{//{{{
		edit: function(id){//{{{
			window.open('/element/edit/'+id+".html", 'edit_elt', 'menubar=no, status=no, scrollbars=auto, menubar=no, width=700, height=780');
		}//}}}
	},//}}}
	map:{//{{{
		getMapFromAdress: function (adr_comp, adr_ville){//{{{
			var trouve = false;
			var nbRech = 0;
			var geoCallback = function(point){
				if(nbRech < 2){
					if(point != null){
						var map = new GMap2(document.getElementById("map"));
						map.enableScrollWheelZoom();
						map.addControl(new GSmallMapControl());
						if(nbRech == 0){
							map.setCenter(point, 13);
						}else{
							map.setCenter(point, 16);
						}
						var marker = new GMarker(point);
						map.addOverlay(marker);
						nbRech++;
					}else{
						geo.getLatLng(adr_ville, geoCallback);
					}
				}
			};
			var geo = new GClientGeocoder();
			geo.getLatLng(adr_comp, geoCallback);
		},//}}}
		convertSexatoDeg: function (chaine){//{{{
			var stripped = chaine.substr(1,chaine.length-3);
			var vals = stripped.split('Â°');
			var degs = parseInt(vals[0]);
			var vals2 = vals[1].split("'");
			var mins = parseInt(vals2[0]);
			var secs = parseFloat(vals2[1].replace(',','.'));
			return degs + (mins/60) + (secs/3600);
		},//}}}
		getMapFromLL: function (latitude, longitude){//{{{
			var map = new GMap2(document.getElementById("map"));
			map.enableScrollWheelZoom();
			map.addControl(new GSmallMapControl());
			var point = new GLatLng(convertSexatoDeg(latitude), convertSexatoDeg(longitude));
			map.setCenter(point, 13);
			var marker = new GMarker(point);
			map.addOverlay(marker);
		}//}}}
	},//}}}
	DataTable: function(o) {//{{{
		if(o == undefined){
			o = {};	
		}
		this.first = o.first || 0;
		this.action = o.action || '';
		this.tableElt = o.tableElt || '';
		this.listeNbpp = o.listeNbpp || [10,20,30];
		this.nbpp = o.nbpp || this.listeNbpp[0];
		this.name = o.name || '';
		this.url = o.url || '/index.php';
		this.params = o.params || {};

		this.get = function(first){//{{{
			var that = this; 
			if(first != undefined){
				that.first = first;
			}
			var handler = {
				success: function(o) {
					var te = document.getElementById(that.tableElt);				 
					te.innerHTML = o.responseText;					 
				},
				failure: function(o){
					ANEOL.visualisation.displayMsg(o.statusText);
				}
			};
			var trans = YAHOO.util.Connect.asyncRequest('POST', this.url, handler, "action="+this.action+"&DataTableParams="+YAHOO.lang.JSON.stringify(that));
		};//}}}
		this.setNbpp = function(nb){//{{{
			this.nbpp = nb;
			this.get(Math.floor(this.first / this.nbpp) * this.nbpp); 
		}//}}}
		this.unselectLAll = function(){//{{{
			var table = document.getElementById(this.name+'_table');
			var lignes = table.getElementsByTagName('tr');
			for(var i=0;i<lignes.length;i++){
				var tr = lignes[i];
				if(tr.className != 'th'){
					(i%2) ? tr.className='regular' : tr.className='alternate';
				}
			}
		};//}}}
		this.selectLine = function(elt){//{{{
			this.unselectLAll(elt);
			while(elt != undefined && elt.nodeName != 'TR'){
				elt = elt.parentNode;
			}
			if(elt != undefined){
				elt.className='selected';
			}
		};//}}}
		this.orderBy = function(field){//{{{
			if(this.params.orderField == field){
				if(this.params.orderDirection == 'asc'){
					this.params.orderDirection = 'desc';
				}else{
					this.params.orderDirection = 'asc';
				}
			}else{
				this.params.orderField = field;
				this.params.orderDirection = 'asc';
			}
		};//}}}

	},//}}}
	DataPickerPanel: function(o){//{{{
		if(o == undefined){
			o = {};	
		}
		// parametres
		this.panelAction = o.panelAction || '';
		this.dataTableAction = o.dataTableAction || '';				
		this.autoCompAction = o.autoCompAction || '';
		this.container = o.container || '';
		this.name = o.name || '';
		this.title = o.title || '';
		this.params = o.params || '';
		this.url = o.url || '';

		// variables d'instance
		this.panel = undefined;
		this.dt = undefined;
		this.autoComp = undefined;
		this.caller = o.caller || undefined;

		this.init = function(){//{{{
			this.getPanel();
			this.getHtml();
		};//}}}
		this.getPanel = function(){//{{{
			this.panel = new YAHOO.widget.Panel(this.container, { 
				fixedcenter: true,  
				constraintoviewport: true,  
				close:true,  
				visible:true,  
				draggable:true} ); 
			this.panel.setHeader(this.title); 
		};//}}}
		this.getHtml = function(){//{{{
			var that = this; 
			var handler = {
				success: function(o){
					that.panel.setBody(o.responseText); 
					that.panel.render(); 
					that.getDataTable();
					that.getAutoComplete();
				},
				failure: function(o){ ANEOL.visualisation.displayMsg(o.statusText); }
			};
			var trans = YAHOO.util.Connect.asyncRequest('POST', this.url, handler, "action="+this.panelAction+"&name="+this.name);
		}; //}}}
		this.getDataTable = function(){//{{{
			this.dt = new ANEOL.DataTable({action:this.dataTableAction, tableElt:'dpp_dt_cts', name:this.name+'.dt', url:this.url, params:{lettre:'%'} });
			this.dt.selectId = function(id, elt){
				var autoCompField = document.getElementById("auto_comp_field");
				autoCompField.value = elt;
				var autoCompHidden = document.getElementById("auto_comp_hidden");
				autoCompHidden.value = id;
			};
			this.dt.get();
		};//}}}
		this.getAutoComplete = function(){//{{{
			var ds = new YAHOO.util.XHRDataSource(this.url+"?action="+this.autoCompAction+"&");
			ds.responseType = YAHOO.util.XHRDataSource.TYPE_JSON;
			ds.responseSchema = {resultsList:'results', fields:['nom', 'id', 'prenom'] };

			this.autoComp = new YAHOO.widget.AutoComplete("auto_comp_field","auto_comp_container", ds);
			this.autoComp.queryQuestionMark = false;
			this.autoComp.itemSelectEvent.subscribe(this.autoCompItemSelectHandler);

			this.autoComp.resultTypeList = false;
			this.autoComp.formatResult = function(oResultItem, sQuery, sResultMatch) { 
				return oResultItem.nom+' '+oResultItem.prenom;
			}; 

			var autoComp = document.getElementById('auto_comp_field');
			autoComp.focus();
		}//}}}
		this.autoCompItemSelectHandler = function(sType, aArgs){//{{{
			var autoCompHidden = document.getElementById('auto_comp_hidden');
			autoCompHidden.value = aArgs[2].id;
		}//}}}
		this.validateSelection = function(){//{{{
			var autoCompField = document.getElementById("auto_comp_field");
			var autoCompHidden = document.getElementById("auto_comp_hidden");
			if(autoCompHidden.value != '' && autoCompField.value != ''){
				this.callback(autoCompHidden.value);
				this.panel.hide();
			}
		};//}}}
		this.callback = function(id){//{{{
		};//}}}

	}//}}}
	
};

CRL = {//{{{

	ContactWidget: function(o){//{{{
		if(o == undefined){
			o = {}
		}
		this.id_entite = o.id_entite || '';
		this.name = o.name || '';
		this.container = o.container || '';
		this.template = o.template || 'contact_widget.html';
		this.dt = null;
		this.frm = null;
		this.dpp = null;

		this.init = function(){//{{{
			this.getHtml();
			this.getDataTable();
		}//}}}
		this.getHtml = function(){//{{{
			var that = this; 
			var handler = {
				success: function(o) {
					var elt = document.getElementById(that.container);
					elt.innerHTML = o.responseText;					 
					that.frm = document.getElementById("contact_frm");
				},
				failure: function(o){ ANEOL.visualisation.displayMsg(o.statusText); }
			};
			var trans = YAHOO.util.Connect.asyncRequest('POST', '/admin.php', handler, "action=get_template&template="+this.template+"&name="+this.name);
		}//}}}
		this.getDataTable = function(){//{{{
			var that = this;
			this.dt = new ANEOL.DataTable({action:'get_contacts', tableElt:'dt_contacts', name:this.name+'.dt', url:'/admin.php', params:{id_entite:this.id_entite, parentW:this.name} });
			this.dt.get();
		}//}}}
		this.getDataPickerPanel = function(){//{{{
			var that = this;
			this.returnId = function(id){
				that.sel_contact_entite(id);
			};
			this.dpp = new ANEOL.DataPickerPanel({name:this.name+'.dpp', container:'contact_selector', url:'/admin.php', action:'get_contact_dpp', title:'Sélectionner un contact', caller:this });
			this.dpp.init();
		}//}}}
		this.add_contact_entite = function(){//{{{
			var that = this;
			this.frm.id_entite.value = this.id_entite;
			var ok = ANEOL.validation.validate(
				ANEOL.validation.required, {field: that.frm.nom} 
			);
			if(ok){
				YAHOO.util.Connect.setForm(this.frm); 
				var callback = {
					success: function(o){
						that.dt.get();
						that.reset_contact_entite_form();
					},
					failure: function(o){ ANEOL.visualisation.displayMsg('Somthing wrong happened ...'); }
				};
				var cObj = YAHOO.util.Connect.asyncRequest('POST', '/admin.php', callback);
			}
		};//}}}
		this.add_contact_from_liste = function(){

		}
		this.sel_contact_entite = function(id){//{{{
			var that = this;
			var callback = {
				success: function(o){ 
					var contact = YAHOO.lang.JSON.parse(o.responseText);		 
					that.frm.id_entite_contact.value = id;
					that.frm.nom.value = contact.nom;
					that.frm.prenom.value = contact.prenom;
					that.frm.adresse.value = contact.adresse;
					that.frm.cp.value = contact.cp;
					that.frm.ville.value = contact.ville;
					that.frm.commentaires.value = contact.commentaires;
					
					for(var i=0;i<that.frm.id_fonction.length;i++){
						var elt = that.frm.id_fonction[i];
						if(elt.value == contact.id_fonction){
							elt.selected = true;
							break;
						}
					}

					for(var i=0;i<that.frm.id_civilite.length;i++){
						var elt = that.frm.id_civilite[i];
						if(elt.value == contact.id_civilite){
							elt.selected = true;
							break;
						}
					}

				},
				failure: function(o){ ANEOL.visualisation.displayMsg('Somthing wrong happened ...'); }
			};
			var cObj = YAHOO.util.Connect.asyncRequest('POST', '/admin.php', callback,"action=sel_contact_entite&id_entite_contact="+id);
		};//}}}
		this.del_contact_entite = function(id){//{{{
			var that = this;
			if(confirm("Voulez vous supprimer ce contact ?")){
				var callback = {
					success: function(o){
						if(o.responseText != ""){
							if(confirm("Le contact n'est lié à rien d'autre, voulez vous le supprimer définitivement ?")){
								var cObj = YAHOO.util.Connect.asyncRequest('POST', '/admin.php', callback, "action=del_contact&id_contact="+o.responseText);
							}
						}
						that.dt.get(); 
					},
					failure: function(o){ ANEOL.visualisation.displayMsg('Somthing wrong happened ...'); }
				};
				var cObj = YAHOO.util.Connect.asyncRequest('POST', '/admin.php', callback, "action=del_contact_entite&id_entite_contact="+id);
			}
		};//}}}
		this.reset_contact_entite_form = function(){//{{{
			this.dt.unselectLAll();
			this.frm.reset();
			this.frm.id_entite_contact.value = "";
			this.frm.id_civilite[0].selected = true;
			this.frm.id_fonction[0].selected = true;
			this.frm.nom.focus();
		};//}}}
	},//}}}
	LienWidget: function(o){//{{{
		if(o == undefined){
			o = {}
		}
		this.id_entite = o.id_entite || '';
		this.name = o.name || '';
		this.container = o.container || '';
		this.template = o.template || 'lien_widget.html';
		this.dt = null;
		this.frm = null;

		this.init = function(){//{{{
			this.getHtml();
			this.getDataTable();
		}//}}}
		this.getHtml = function(){//{{{
			var that = this; 
			var handler = {
				success: function(o) {
					var elt = document.getElementById(that.container);
					elt.innerHTML = o.responseText;					 
					that.frm = document.getElementById("lien_frm");
				},
				failure: function(o){ ANEOL.visualisation.displayMsg(o.statusText); }
			};
			var trans = YAHOO.util.Connect.asyncRequest('POST', '/admin.php', handler, "action=get_template&template="+this.template+"&name="+this.name);
		}//}}}
		this.getDataTable = function(){//{{{
			var that = this;
			this.dt = new ANEOL.DataTable({action:'get_liens', tableElt:'dt_liens', name:this.name+'.dt', url:'/admin.php', params:{id_entite:this.id_entite, parentW:this.name} });
			this.dt.get();
		}//}}}
			this.add_lien_entite = function(){//{{{
				var that = this;
				var ok = ANEOL.validation.validate(
					ANEOL.validation.required, {field: this.frm.titre}, 
					ANEOL.validation.required, {field: this.frm.url} 
				);
				if(ok){
					YAHOO.util.Connect.setForm(this.frm); 
					var callback = {
						success: function(o){
							that.dt.get();
							this.frm.id_lien.value = "";
							this.frm.titre.value = "";
							this.frm.description.value = "";
							this.frm.url.value = "";
							this.frm.titre.focus();
						},
						failure: function(o){ ANEOL.visualisation.displayMsg('Somthing wrong happened ...'); }
					};
					var cObj = YAHOO.util.Connect.asyncRequest('POST', '/admin.php', callback);
				}
			 };//}}}
			this.del_lien_entite = function(id){//{{{
				var that = this;
				if(confirm("Voulez vous supprimer ce lien ?")){
					var callback = {
						success: function(o){ that.dt.get(); },
						failure: function(o){ ANEOL.visualisation.displayMsg('Somthing wrong happened ...'); }
					};
					var cObj = YAHOO.util.Connect.asyncRequest('POST', '/admin.php', callback, "action=del_lien_entite&id_lien="+id);
				}
			 };//}}}
			this.sel_lien_entite = function(id){//{{{
				var that = this;
				var callback = {
					success: function(o){ 
						var lien = YAHOO.lang.JSON.parse(o.responseText);		 
						that.frm.id_lien.value = lien.id_lien;
						that.frm.titre.value = lien.titre;
						that.frm.url.value = lien.url;
						that.frm.description.value = lien.description;
					},
					failure: function(o){ ANEOL.visualisation.displayMsg('Somthing wrong happened ...'); }
				};
				var cObj = YAHOO.util.Connect.asyncRequest('POST', '/admin.php', callback,"action=sel_lien_entite&id_lien="+id);
			 };//}}}
			this.reset_lien_entite_form = function (){//{{{
				this.dt.unselectLAll();
				this.frm.reset();
				this.frm.id_lien.value = "";
				this.frm.titre.focus();
			};//}}}
	}//}}}

};//}}}

