// =================================================================================
// SCRIPT DE VERIFICATION DES FORMULAIRES ET AUTRES CHAINES
// Réalisation Cyril C. et Mathias B.
// =================================================================================
// js_chaine
// js_checkbox
// js_CheckLen
// js_CheckDiff
// js_combo
// js_email
// js_number
// js_radio
// function CompteClic(formulaire) 

// erreur_champ
// =================================================================================

// LEXIQUE
// FormName			= DEFINI le Nom du FORMULAIRE
// StringField		= DEFINI le Nom du CHAMP A VERIFIER
// FieldName		= DEFINI le Nom du champ a afficher en cas de RETOUR D'ERREUR
// $js_opt 			= DEFINI SI CE CHAMP EST OBLIGATOIRE OU NON (par defaut = FALSE)
// $js_type			= DEFINI le type du champ par defaut = FALSE - SI TRUE => PASSWORD
// $js_opt_car_min 	= DEFINI le nombre de carateres minimum
// $js_opt_car_max	= DEFIN le nombre de caracteres maximum
// $js_opt_anchor	= DEFINI LE NOM D'UN "ANCRE" AU SEIN DE LA PAGE -> RENVOI DESSUS
// $js_check		= DEFINI si il suffit juste d'avoir saisi quelques chose dedans

// =================================================================================

// js_chaine
// =================================================================================
function js_chaine(FormName, StringField, FieldName, js_opt, js_type, js_opt_car_min, js_opt_car_max, js_opt_anchor, js_check)
{
	a=document.forms[FormName].elements[StringField];
	b=document.forms[FormName].elements[StringField].value;

	retour=true;
	message='';

	// DEFINI LE NOMBRE DE CARACTERES MINIMUM : js_opt_car_min (par defaut = 1)
	car_min=typeof(js_opt_car_min)=='undefined'?1:js_opt_car_min;

	// Si obligatoire
	if (js_opt)
	{
		if (b.length<=0)
		{
			message='Champ '+FieldName+' : Entrez une chaine de caractères !';
			retour=false;
		}
	} // Si obligatoire

	if (b.length>0 && b.length < car_min && retour)
	{
		message='Champ '+FieldName+' : Entrez une chaine un peu plus longue !';
		retour=false;
	}
	else if (a.length>=js_opt_car_max && typeof(js_opt_car_max)!='undefined')
	{
		message='Champ '+FieldName+' : Entrez une chaine de moins de '+js_opt_car_max+' caractères !';
		retour=false;
	}
	else
	{
		if (!js_check)
		{
			for (i=0;i<b.length;i++)
			{
				if (b.charAt(i)=='<' || b.charAt(i)=='>' || b.charAt(i)=='\\' || b.charAt(i)=='"')
				{
					message='Champ '+FieldName+' contient un des caractères interdits  ( <,>,\\," ) !';
					retour=false;
				}
				else
				{
					if (js_type)
					{
						if (b.charAt(i)=='\'')
						{
							message='Champ '+FieldName+' contient un des caractères interdits ( <,>,\\,",\' ) !';
							retour=false;
						}
					}
				}
			}	
		}

		if (!js_check)
		{

			if (retour)
			{
				pluslongmot=0;
				longueurmot=0;
	
				for (i=0;i<b.length;i++)
				{
					if (b.charAt(i)==' ' || b.charAt(i)=='\t' || b.charAt(i)=='\n')
					{
						if (pluslongmot<longueurmot)
						pluslongmot= longueurmot;
						longueurmot= 0;
					}
					else
					{
						longueurmot++;
					}
				}
				if (pluslongmot>26 || longueurmot>26)
				{
					message='Champ '+FieldName+' contient au moins un mot trop grand !';
					retour=false;
				}
			}
		}
	}

	if (!retour) erreur_champ(FormName,a,js_opt_anchor,message); else a.style.backgroundColor='#f0f0FF';
	return retour;
}
// -- FIN DU SCRIPT --
// =================================================================================

// js_checkbox
// =================================================================================
function js_checkbox(FormName, StringField, FieldName, js_opt_anchor)
{
	a=document.forms[FormName].elements[StringField];
	if (!a.checked)
	{
		erreur_champ(FormName,a,js_opt_anchor,'Champ '+FieldName+' : Champ obligatoire !');
		return false;
	}
	else
	{
		a.style.backgroundColor='#f0f0FF';
		return true;
	}
}
// -- FIN DU SCRIPT --
// =================================================================================

// js_CheckLen
// =================================================================================
function CheckLen(FormName, StringField, Target, js_opt_car_max)
{
	a = document.forms[FormName].elements[StringField];
	if (a.value.length > js_opt_car_max )
	{
		a.value = a.value.substring(0,js_opt_car_max);
		CharsLeft = js_opt_car_max;
	}
	else
	{
		CharsLeft = a.value.length;
	}
	document.forms[FormName].elements[Target].value = js_opt_car_max - CharsLeft;
}
// -- FIN DU SCRIPT --
// =================================================================================

// js_CheckDiff
// =================================================================================
function js_CheckDiff(FormName, StringField1, StringField2, FieldName, js_opt_anchor)
{
	a = document.forms[FormName].elements[StringField2];
	StringOne = document.forms[FormName].elements[StringField1].value;
	StringTwo = document.forms[FormName].elements[StringField2].value;

	retour=true;
	message='';

	if( StringOne != StringTwo )
	{
		message = 'Champ '+FieldName+' : Les champs sont différents ! !';
		retour=false;
	}
	if (!retour) erreur_champ(FormName,a,js_opt_anchor,message); else a.style.backgroundColor='#f0f0FF';
	return retour;
}
	
// -- FIN DU SCRIPT --
// =================================================================================

// js_combo
// =================================================================================
function js_combo(FormName, StringField, FieldName, js_opt_anchor)
{
	a=document.forms[FormName].elements[StringField];
	b=document.forms[FormName].elements[StringField].value;
	b=document.forms[FormName].elements[StringField].options[document.forms[FormName].elements[StringField].options.selectedIndex].value;

	retour=true;
	message='';

	if (b=="0" && retour)
	{
		message='Champ '+FieldName+' : Sélectionnez une valeur !';
		retour=false;
	}
	if (!retour) erreur_champ(FormName,a,js_opt_anchor,message); else a.style.backgroundColor='#f0f0FF';
	return retour;
}
// -- FIN DU SCRIPT --
// =================================================================================

// js_email
// =================================================================================
function isEmail(str)
{
	// are regular expressions supported?
	var supported = 0;
	if (window.RegExp) {
		var tempStr = "a";
		var tempReg = new RegExp(tempStr);
		if (tempReg.test(tempStr)) supported = 1;
	}
	if (!supported)
	return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
	var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
	var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
	return (!r1.test(str) && r2.test(str));
}
function js_email(FormName, StringField, FieldName, js_opt, js_opt_anchor)
{

	a=document.forms[FormName].elements[StringField];
	b=document.forms[FormName].elements[StringField].value;

	retour=true;
	message='';

	// Si obligatoire
	if (js_opt)
	{
		if (b.length<=0)
		{
			message='Champ '+FieldName+' : Entrez une chaine de caractères !';
			retour=false;
		}
	} // Si obligatoire

	if (b.length>0 && retour)
	{
		if (!isEmail(b))
		{
			message='Champ '+FieldName+' : Adresse email non valide !';
			retour=false;
		}
		else if (b.length <= 2)
		{
			message='Champ '+FieldName+' : Entrez une chaine un peu plus longue !';
			retour=false;
		}
	}

	if (!retour) erreur_champ(FormName,a,js_opt_anchor,message); else a.style.backgroundColor='#f0f0FF';
	return retour;
}
// -- FIN DU SCRIPT --
// =================================================================================

// js_number
// =================================================================================
function js_number(FormName, StringField, FieldName, js_opt, js_opt_car_min, js_opt_car_max, js_opt_anchor)
{
	a=document.forms[FormName].elements[StringField];
	b=document.forms[FormName].elements[StringField].value;

	retour=true;
	message='';

	// DEFINI LE NOMBRE DE CARACTERES MINIMUM : js_opt_car_min (par defaut = 1)
	car_min=typeof(js_opt_car_min)=='undefined'?1:js_opt_car_min;

	// Si obligatoire
	if (js_opt)
	{
		if (b.length<=0)
		{
			message='Champ '+FieldName+' : Entrez une chaine de caractères !';
			retour=false;
		}
	} // Si obligatoire

	if (b.length>0 && b.length < car_min && retour)
	{
		message='Champ '+FieldName+' : Entrez une chaine un peu plus longue !';
		retour=false;
	}
	else if (a.length>=js_opt_car_max && typeof(js_opt_car_max)!='undefined')
	{
		message='Champ '+FieldName+' : Entrez une chaine de moins de '+js_opt_car_max+' caractères !';
		retour=false;
	}
	else
	{
		testm = true;
		for (var j=0; j<(b.length); j++)
		{
			if (b.charAt(j)<'0' || b.charAt(j)>'9')
				testm= false;
		}
		if (testm==false)
		{
			message='Champ '+FieldName+' : Nombre incorrect !';
			retour=false;
		}
	}

	if (!retour) erreur_champ(FormName,a,js_opt_anchor,message); else a.style.backgroundColor='#f0f0FF';
	return retour;
}
// -- FIN DU SCRIPT --
// =================================================================================

// js_radio
// =================================================================================
function js_radio(FormName, StringField, FieldName, js_opt, js_NbFields, js_opt_anchor)
{
	a=document.forms[FormName].elements[StringField];

	retour=true;
	message='';
	// au lieu de js_NbFields utiliser a[i].length (fait gagner un argument)
	for (var i=0; i<js_NbFields; i++)
	{
		if (a[i].checked!=0) retour= false;
	}
	// modif CC : pour gagner 3 lignes :)
	if (!retour)
	{
		message='Champ '+FieldName+' : Nombre incorrect !';
		erreur_champ(FormName,a,js_opt_anchor,message);
	}
	else
	{
		a.style.backgroundColor='#f0f0FF';
	}
	return retour;
}
// -- FIN DU SCRIPT --
// =================================================================================

// -- DEBUT DU SCRIPT --
// =================================================================================
var nbclic=0 // Initialisation à 0 du nombre de clic
function CompteClic(FormName)
{
	// Fonction appelée par le bouton
	nbclic++; // nbclic+1
	retour=true;
	if (nbclic>1)
	{ // Plus de 1 clic
	 alert("Vous avez déjà cliqué ce bouton.\nLe formulaire est en cours de traitement... Patience");
	 retour=false;
	}
	return retour;
}
// -- FIN DU SCRIPT --
// =================================================================================

// erreur_champ
// =================================================================================
function erreur_champ(FormName,champ,ancre,message)
{
	document.forms[FormName].elements['valid'].value=" Continuer";

	if (typeof(ancre)!='undefined') location.href='#'+ancre;
	// champ.style.backgroundColor='#FFC0C0';
	champ.style.backgroundColor='#F2B48A';
	alert(message);
	if (!champ.options) champ.select();
	champ.focus();
}

