// JavaScript Document

/* ######################################################### */

function envoiAjaxUpdater(bouton, fonctions){
	bouton.observe('click', function(e){
		Event.stop(e);
		ajaxUpdater(Event.element(e).up('form'), $(Event.element(e).up('form').id+'_message'), fonctions);
		});
	}

/* ######################################################### */

function ajaxUpdater(form, message, fonctions){
	var url = $(form).readAttribute('action');
	var classe = '.'+form.id+'_data';
	var temp2 = "";
	$$(classe).each(function(elmt){
		temp2 += "&"+elmt.id+"="+elmt.value;
		});
	var params = temp2.substr(1);
	new Ajax.Updater( message, url,  {
		method: 'post',
		parameters: params,
		onComplete: fonctions,
		evalScripts: true
		});
	}

/* ######################################################### */

function formAjaxUpdater(){
	$$('.formAjaxUpdater').each(function(elmt){
		var ident = elmt.readAttribute('title');
		$(ident+'_button').observe('click', function(elmt3){
			Event.stop(elmt3);
			var url = elmt.readAttribute('action');
			var classe = '.'+ident+'_data';
			var temp2 = "";
			$$(classe).each(function(elmt2){
				temp2 += "&"+elmt2.id+"="+elmt2.value;
				});
			var params = temp2.substr(1);
			new Ajax.Updater( $(ident+'_message'), url,  {
				method: 'post',
				parameters: params,
				onComplete: '',
				evalScripts: true
				});
			});
		$(ident).observe('submit', function(elmt3){
			Event.stop(elmt3);
			var url = elmt.readAttribute('action');
			var classe = '.'+ident+'_data';
			var temp2 = "";
			$$(classe).each(function(elmt2){
				temp2 += "&"+elmt2.id+"="+elmt2.value;
				});
			var params = temp2.substr(1);
			new Ajax.Updater( $(ident+'_message'), url,  {
				method: 'post',
				parameters: params,
				onComplete: '',
				evalScripts: true
				});
			});
		});
	}

/* ######################################################### */

function redirection(url){
	document.location.href =  url;
	}

/* ######################################################### */

function rafraichir(){
	document.location.href =  window.location.href;
	}

/* ######################################################### */

function removeLink(){
	//
	$$('.removeLink').each(function(elmt){
		elmt.observe('click', function(e){
			Event.stop(e);
			if(confirm("Êtes-vous sûr(e) de vouloir supprimer ?" )) {
				var url = elmt.readAttribute('href');
				new Ajax.Request( url,  {
					method: 'post',
					parameters: { },
					onSuccess: function(){ document.location.href =  window.location.href; }
					});
				}
			});
		});
	}

/* ######################################################### */

function addEvent(obj, evnt, method){
	if(obj.addEventListener){
		obj.addEventListener(evnt, method, false);
		return false;
		} else if(obj.attachEvent){
		obj.attachEvent("on"+evnt, method);
		return false;
		} else {
		return false;
		}
	}

/* ######################################################### */

function hiddenElement(){
	$$('.hiddenElement').each(function(elmt){
		elmt.hide();
		});
	}