/*
* @auteur jean-rodolphe Lesueur <jrl@free.fr>
*/
/***********************
* fonctions utilitaires
************************/
function trim(s)
	{
	var res='';
	if (typeof s == 'string' && s.lenth!=0)
		{
		var d=0, f=s.length;
		while (d<=f && s.charAt(d)==' ') d++;
		while (f>=d && s.charAt(f-1)==' ') f--;
		res = s.substr(d,f-d);
		}
	return res;
	}

function noWhiteSpace(s)
	{
	if (typeof s != 'string') return '';
	r = ''; for (var i=0;i<s.length;i++) if (s.charAt(i)!=' ') r+=s.charAt(i);
	return r;
	}

initFormCheck = function()
	{ 
	this.aCheckField = new Array();
	
	this.addCheck = function(oField, fActionDefaut, fCheck, fActionNotChecked)
		{
		this.aCheckField.push(oField);
		oField.check = fCheck;
		oField.actionDefaut = fActionDefaut;
		oField.actionNotChecked = fActionNotChecked;
		}
  this.out = document.getElementById('msg') || null;
	this.check = function()
		{
		res = true;
		try
			{
			var i=0,oField;
			while (res && i<this.aCheckField.length)
				{
				oField = this.aCheckField[i];
				if (oField.actionDefaut!=null) oField.actionDefaut();
				if (oField.check()) i++;
				else
					{
					if (oField.actionNotChecked!=null) oField.actionNotChecked();
					res=false;
					}
				}
			}
			catch (e)
	      {
	      alert('Echec de la vérification du formulaire');
	      res = true;
	    	}
		return res;
		}
	}

/*************************************************************
* Action à appliquer au champ avant de vérifier son contenu
**************************************************************/
defaut   = function(){this.value=trim(this.value)};

/***************************************
* Test de contenu de champ de formulaire
****************************************/
test2car = function(){return this.value.length>=2};
testMotDePasse = function(){return this.value.length>=3};
testMail  = function(){return this.value.match(/^[-_a-zA-Z0-9\.]+@[-_a-zA-Z0-9]+\.[-_a-zA-Z0-9.]{2,}$/)};
test10a20car = function(){return noWhiteSpace(this.value).match(/^.{10,20}$/)}
/*****************************************************************
* Action à appliquer sur un champ dont le contenu n'est pas valide
******************************************************************/	
notOk    = function(s)
	{
	var msg = s;
	return function()
		{
		this.onfocus = function()
				{
				if (this.form.out) this.form.out.innerHTML = msg;
				this.onblur = function() { if (this.form.out) this.form.out.innerHTML ='&nbsp;'; this.onblur = null; }
				this.onfocus=null;
				}
		this.focus();
		}
	}

/****************************
* Initialisation du document
*****************************/
/* Pris en charge par composantPHP
window.onload = function()
	{
	var	f    = document.forms.inscription;

	initFormCheck.apply(f);
	f.addCheck(f.prenom,defaut,test2car,notOk("Entrez votre prénom, SVP."));
	f.addCheck(f.nom,defaut,test2car,notOk("Entrez votre nom, SVP."));
	f.addCheck(f.email,defaut,testMail,notOk("Entrez votre email, SVP."));
	f.addCheck(f.motDePasse,null,testMotDePasse,notOk("Entrez votre mot de passe, SvP., au moins 3 caractères"));
	f.nom.focus();
	f.onsubmit = function () {return f.check();}
	}
*/
