var Aksform = {
	_alerts: [],
	_js_verif_messages: [],
	_all_champs: [],
	_checkboxes: [],
	
	_GetAlerts: function()
	{
		var alerts = new Ajax.Request('/js/ajaks/aksform_alerts.cfg.php?aksform_js=1', {
			method: 'get',
			asynchronous: false
		});
		
		return eval('(' + alerts.transport.responseText + ')');
	},
	
	_Verif: function(the_form) {
		/******* Récupération des messages d'alerte ********/
		if(Aksform._alerts == '') Aksform._alerts = Aksform._GetAlerts();
		var alerts = Aksform._alerts;
		//
		
		/******* Récupération de champs id et nom du formulaire ********/
		var id_form = the_form.getAttribute('id');
		var name_form = the_form.getAttribute('name');
		if(id_form == null || name_form == null) throw 'Name ou Id manquant dans la balise form !';
		//

		var is_ok = true;
		/******* Existance champs avec name "need[" ********/
		// Sauvegarde des champs obligatoires pour ne pas reparcourir le formulaire à chaque soumission
		if(Aksform._all_champs.size() == 0) Aksform._all_champs = $$('#' + id_form + ' [name^="need["]');
		//
		var count_champs = Aksform._all_champs.size();
		if(count_champs != 0) 
		{
			var all_champs = Aksform._all_champs;
			var passwords = [];

			/******* Parcours des champs ********/
			for(var i = 0; i < count_champs; i++) 
			{
				// Nom du champ
				var name_champs = all_champs[i].getAttribute('name').replace(/need\[(.*)\]/, "$1");
				var name_champs_orig = all_champs[i].getAttribute('name');
				//
	
				// Pointeur sur le champs id
				if(all_champs[i].getAttribute('id') != null)
				{
					var id_champs = all_champs[i].getAttribute('id');
					var champs = $(id_champs);
				} else {
					all_champs[i].setAttribute('id', name_champs);
					var champs = $(name_champs);
					//throw 'Id manquait dans le champ de formulaire ' + name_champs + '!';
				}
				//
	
				/******* Récupération du libellé ********/
				var the_labels = $$('#' + id_form + ' label[for="' + id_champs + '"]');
				if(id_champs != '' && the_labels != '')
				{
					//var txt_label = the_labels[0].firstChild.nodeValue;
					var txt_label = the_labels[0].innerHTML;
				} else if(champs.getAttribute("title") != null) {
					var txt_label = champs.getAttribute('title');
				} else {
					var txt_label = name_champs.replace('_', ' ');
				}
				//
	
				/******* Vérification ********/
				var tag = champs.tagName;
				var typeAttribute = "text";
				if(champs.getAttribute("type") != null) typeAttribute = champs.getAttribute("type")
				if((tag == "INPUT" && (typeAttribute == "text" || typeAttribute == "password")) || tag == "TEXTAREA") 
				{
					// Type du champs pour les caractères interdits
					var password = false;
					if(typeAttribute == "password") password = true;
					// Nombre de caractères minimum
					var minlength = undefined;
					if(champs.getAttribute("minlength") != null) minlength = champs.getAttribute("minlength");
					// Nombre de caractères maximum
					var maxlength = undefined;
					if(champs.getAttribute("maxlength") != null) maxlength = champs.getAttribute("maxlength");
					// Vérification caractères interdits et mots trop long
					var check = true;
					if(champs.getAttribute("check") != null) check = false;
					//					
					
					// Chaîne
					if(!js_chaine(name_form, name_champs_orig, txt_label, true, password, minlength, maxlength, undefined, check)) is_ok = false;
					//
					
					// Email
					if(name_champs.toLowerCase().match('mail'))
					{
						//
						if(!js_email(name_form, name_champs_orig, txt_label, undefined, undefined)) is_ok = false;
						//
					}
					//
					
					// Similitude passwords
					if(password) 
					{
						passwords[passwords.size()] = name_champs_orig;
						if(is_ok && passwords.size() == 2) 
						{
							if(!js_CheckDiff(name_form, passwords[0], passwords[1], txt_label, undefined)) is_ok = false;
						}
					}
					//
				}
				
				if(tag == "INPUT" && typeAttribute == "checkbox") 
				{
					// Vérification si la checkbox a déjà été vérifiée
					var c = Aksform._checkboxes;
					var isset_checkbox = false;
					for(var i = 0; i < c.size(); i++) 
					{
						if(c[i] == name_champs_orig) isset_checkbox = true;
					}
					if(!isset_checkbox) c.push(name_champs_orig);
					if(typeof(c[name_champs_orig]) != 'object') c[name_champs_orig] = [];
					else {
						var isset_checkbox_label = false;
						for(var i = 0; i < c[name_champs_orig].size(); i++) 
						{
							if(c[name_champs_orig][i] == txt_label) isset_checkbox_label = true;
						}
						if(!isset_checkbox_label) c[name_champs_orig].push(isset_checkbox_label);
					}
					//
					//
					var i = 0;
					var last_checkbox = false;
					var is_ok_tmp = false;
					c[name_champs_orig].each(function(el) {
						if(i == c[name_champs_orig].size()) last_checkbox = true;
						if(!is_ok_tmp && !js_multi_checkbox(name_form, el, c[name_champs_orig][i] + ',' , undefined, last_checkbox)) is_ok_tmp = false;
						else is_ok_tmp = true;
						i++;
					});
					is_ok = is_ok_tmp;
					//
					
					Aksform._checkboxes = c;
				}
				
				if(tag == "INPUT" && typeAttribute == "radio") 
				{
					//
					if(!js_radio(name_form, name_champs_orig, txt_label, undefined, undefined, undefined)) is_ok = false;
					//
				}
				
				if(tag == "SELECT") 
				{
					//
					if(!js_combo(name_form, name_champs_orig, txt_label, undefined)) is_ok = false;
					//
				}
				//
				
				if(!is_ok) 
				{
					// Affichage de l'aide à côté du champs mal renseigné
					if(!alerts[name_champs]) var alert_champs = '<em>' + Aksform._js_verif_messages + '</em>';
					else var alert_champs = alerts[name_champs];
					Akstips._Form(all_champs[i], alert_champs);
					//
					break;
				}
			}
			//
		}
		//
		
		// On compte les clics
		//if(!CompteClic(the_form)) is_ok = false;
		//
		
		return is_ok;
	},
	
	load: function() {
		new Event.observe(window, 'load', function() {
			$$('form').each( function(el) {
				new Event.observe(el, 'submit', function(e){ if(!Aksform._Verif(el)) Event.stop(e); });
			});
		});
	}
}

Aksform.load();
