/*
Cette fonction teste tous les champs d'un formulaire de saisie.
*/
function verifieFormulaire( champs )
{
	var err = '';	// message d'erreur affiché

	for ( i = 0;i < champs.length;i++ )
	{
		if ( champs[ i ][ 1 ] )
		{
			switch ( champs[ i ][ 0 ] )
			{

				case 'egal':
					err = verifieChamp2( champs[ i ][ 0 ], champs[ i ][ 1 ], champs[ i ][ 2 ], champs[ i ][ 3 ], champs[ i ][ 4 ] );
					break;
			
				case 'lie':
					err = verifieChamp2( champs[ i ][ 0 ], champs[ i ][ 1 ], champs[ i ][ 2 ], champs[ i ][ 3 ], champs[ i ][ 4 ] );
					break;
			
				case 'compare_date':
					err = verifieChamp2( champs[ i ][ 0 ], champs[ i ][ 1 ], champs[ i ][ 2 ], champs[ i ][ 3 ], champs[ i ][ 4 ] );
					break;
			
				case 'critere':
					err = verifieChamp2( champs[ i ][ 0 ], champs[ i ][ 1 ], champs[ i ][ 2 ], champs[ i ][ 3 ], champs[ i ][ 4 ] );
					break;
			
				default:
					err = verifieChamp1( champs[ i ][ 0 ], champs[ i ][ 1 ], champs[ i ][ 2 ], champs[ i ][ 3 ], champs[ i ][ 4 ] );
					break;
			}
		}

		if ( err != '' )
		{
			alert( err );
			return false; // on abandonne la vérification
		}
	}

	return true;
}

/*
	author 		:hpheng
	copyright	:keyrus
	version		:0.9
	desc		:check email regex / accepte extension de plus de 3chars
*/
function isEmail( aValue, iNullAllowed )
{
	//get length of string
	iLength = aValue.length;

	//check if null
	if ( iLength == 0 && iNullAllowed )
	{
		return true;
	}

	re = /^[a-zA-Z0-9_.\-]+\@[a-zA-Z0-9_.\-]+\.[a-zA-Z0-9_\-]+$/;
	iSearch = re.exec( aValue );

	if ( iSearch == null )
	{
		return false;
	}

	return true;

}

/*
Cette fonction teste un champ d'un formulaire de saisie
*/
function verifieChamp1( type, champ, libelle, isOblig, langue )
{
	var msg = '';			// message en retour
	var chaine = champ.value;	// chaine saisie

	//Test Champ obligatoire

	if ( isOblig == 1 )
	{
		if ( ( chaine == '' ) || hasOnlySpaces( chaine ) )
		{ //champ vide

			if ( langue == 'fr' )
			{
				msg = 'Le champ \'' + libelle + '\' doit être renseigné.';
			}
			else
			{
				msg = 'The field \'' + libelle + '\' is mandatory.';
			}

			champ.focus();
			return msg;
		}
	}

	switch ( type )
	{

			case 'pwd':
			//Test mot de passe

			if ( !( verifiePassword( chaine ) ) )
			{

				if ( langue == 'fr' )
				{
					msg = 'Le champ \'' + libelle + '\' ne doit pas contenir d\'espaces.';
				}
				else
				{
					msg = 'The fields \'' + libelle + '\' does no content spaces.';
				}

				champ.focus();
				return msg;
			}

			break;

			case 'url':
			//Test adresse internet

			if ( !( verifieUrl( chaine ) ) )
			{
				if ( langue == 'fr' )
				{
					msg = 'L\'adresse \'' + libelle + '\' n\'est pas correcte.';
				}
				else
				{
					msg = 'Address \'' + libelle + '\' is not valid.';
				}

				champ.focus();
				return msg;
			}

			break;

			case 'email':
			//Test d'adresse email

			if ( ! ( ( chaine == '' ) || verifieEmail( chaine ) ) )
			{
				if ( langue == 'fr' )
				{
					msg = 'L\'adresse mail \'' + libelle + '\' n\'est pas correcte.';
				}
				else
				{
					msg = 'E-mail \'' + libelle + '\' is not valid.';
				}

				champ.focus();
				return msg;
			}

			break;

			case 'date':
			//Test de date

			if ( !( verifieDate( chaine ) ) )
			{
				if ( langue == 'fr' )
				{
					msg = 'La date \'' + libelle + '\' n\'est pas valide (jj/mm/aaaa).';
				}
				else
				{
					msg = 'The date \'' + libelle + '\' is not valid (dd/mm/yyyy).';
				}

				champ.focus();
				return msg;
			}

			break;

			case 'tel' :
			//test sur le format de téléphone ou fax 00 00 00 00 00

			if ( !( verifieTelFax( chaine ) ) )
			{
				if ( langue == 'fr' )
				{
					msg = 'Le champ \'' + libelle + '\'  doit etre au format 00 00 00 00 00';
				}
				else
				{
					msg = 'The field \'' + libelle + '\'  must be in format 00 00 00 00 00';
				}

				champ.focus();
				return msg;
			}

			break;

			case 'entier' :
			//test sur les valeurs entières

			if ( !( verifieEntier( chaine ) ) )
			{
				if ( langue == 'fr' )
				{
					msg = 'Le champ \'' + libelle + '\'  doit etre un entier.';
				}
				else
				{
					msg = 'The field \'' + libelle + '\'  must be integer.';
				}

				champ.focus();
				return msg;
			}

			break;

			case 'decimal' :
			//test sur les valeurs dédcimales.

			if ( !( verifieDecimal( chaine ) ) )
			{

				if ( langue == 'fr' )
				{
					msg = 'Le champ \'' + libelle + '\'  doit etre un décimal.';
				}
				else
				{
					msg = 'The field \'' + libelle + '\'  must be a float.';
				}

				champ.focus();
				return msg;
			}

			break;

			case 'fichier' :
			// test sur les noms de fichier

			if ( !( verifieFichier( chaine ) ) )
			{
				if ( langue == 'fr' )
				{
					msg = 'Le champ \'' + libelle + '\' n\'est pas valide.';
				}
				else
				{
					msg = 'The field \'' + libelle + '\' is invalid.';
				}

				champ.focus();
				return msg;
			}

			break;
	}

	return '';
}


/*
Cette fonction teste deux champs d'un formulaire de saisie
si ils doivent etre égaux ou s'il doivent etre saisis les deux !
*/
function verifieChamp2( type, champ1, champ2, message )
{

	if ( type != 'critere' )
	{
		var chaine1 = champ1.value;	// chaine saisie
		var chaine2 = champ2.value;	// chaine saisie
	}

	//Test mot de passe
	if ( type == 'egal' )
	{
		if ( chaine1 != chaine2 )
		{
			champ1.focus();
			return message;
		}
	}

	if ( type == 'lie' )
	{

		if ( ( chaine1 != '' ) && ( chaine2 == '' ) )
		{
			champ2.focus();
			return message;
		}

		if ( ( chaine2 != '' ) && ( chaine1 == '' ) )
		{
			champ1.focus();
			return message;
		}
	}

	if ( type == 'compare_date' )
	{

		if ( !( compareDate( chaine1, chaine2 ) ) )
		{
			champ1.focus();
			return message;
		}
	}

	if ( type == 'critere' )
	{
		var cpt = 0;
		var j = 1;

		while ( j < champ1.length )
		{

			if ( champ1[ j ][ 0 ].value != '-1' )
			{
				cpt++;
			}

			j++;
		}

		if ( cpt == 0 )
		{

			return message;
		}

	}

	return '';
}


/*
Cette fonction teste un champ de type password
- aucun espace
*/
function verifiePassword( chaine )
{
	return ( nbOccurences( chaine, ' ' ) == 0 );
}


/*
Cette fonction teste un champ de type URL
*/
function verifieUrl( chaine )
{
	var autorise = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789/.%?&#-_';

	//on parcours toute la chaine de caractères

	for ( var i = 0; i < chaine.length; i++ )
	{
		c = chaine.substr( i, 1 ); // ième caractère

		if ( autorise.indexOf( c, 0 ) == -1 )
			return false;
	}

	return true;
}


/*
Cette fonction teste un champ de type Email
*/
function verifieEmail( chaine )
{
	var autorise = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789@.-_';

	//on parcours toute la chaine de caractères

	for ( var i = 0; i < chaine.length; i++ )
	{
		c = chaine.substr( i, 1 ); // ième caractère

		if ( autorise.indexOf( c, 0 ) == -1 )
			return false;
	}

	//une seule occurence du '@' dans l'adresse mail
	if ( nbOccurences( chaine, '@' ) != 1 )
		return false;

	//on récupère la position du '@'
	var p = chaine.indexOf( '@', 0 );

	if ( p == 0 )
		return false;

	if ( p == chaine.length - 1 )
		return false;

	//au moins un point après le @
	if ( nbOccurences( chaine.substr( p ), '.' ) != 1 )
		return false;

	// extension après le dernier point
	if ( chaine.lastIndexOf( '.', 0 ) == chaine.length - 1 )
		return false;

	return true;
}


/*
Cette fonction teste un champ de type Date jj/mm/aaaa
*/
function verifieDate( chaine )
{
	var autorise = '0123456789/';

	if ( chaine != '' )
	{
		// 10 caractères

		if ( chaine.length != 10 )
			return false;

		// 2 caractères '/' en position 2 et 5
		if ( chaine.charAt( 2 ) != '/' )
			return false;

		if ( chaine.charAt( 5 ) != '/' )
			return false;

		if ( nbOccurences( chaine, '/' ) != 2 )
			return false;

		//on parcours toute la chaine de caractères
		for ( var i = 0; i < chaine.length; i++ )
		{
			c = chaine.substr( i, 1 ); // ième caractère

			if ( autorise.indexOf( c, 0 ) == -1 )
				return false;
		}

		// lecture du jour, mois et année
		var jj = chaine.substr( 0, 2 ).valueOf();

		var mm = chaine.substr( 3, 2 ).valueOf();

		var aaaa = chaine.substr( 6, 4 ).valueOf();

		if ( ( jj < 1 ) || ( jj > 31 ) )
			return false;

		if ( ( mm < 1 ) || ( mm > 12 ) )
			return false;

		if ( mm == 2 )
		{
			if ( ( aaaa % 4 == 0 ) && ( jj > 29 ) )
				return false; // année bissextile

			if ( ( aaaa % 4 != 0 ) && ( jj > 28 ) )
				return false; // année non bissextile
		} // et siècles ?

		if ( ( mm == 4 ) || ( mm = 6 ) || ( mm == 9 ) || ( mm = 11 ) )
		{
			if ( jj == 31 )
				return false;
		}
	}

	return true;
}


/*
Détermine si une chaine n'est que composée que d'espaces.
*/
function hasOnlySpaces( chaine )
{
	return ( nbOccurences( chaine, ' ' ) == chaine.length );
}


/*
Renvoie le nombre d'occurence d'un caractère dans une chaine de caractères
*/
function nbOccurences( chaine, ch )
{
	var nb = 0; // nombre d'occurences du caractère

	for ( var i = 0;i <= chaine.length;i++ )
	{
		if ( ( chaine.substr( i, 1 ) == ch ) )
		{
			nb++;
		} // trouvé
	}

	return nb;
}


function verifieTelFax( chaine )
{

	if ( chaine != '' )
	{
		var pos;
		pos = chaine.indexOf( ' ' );

		if ( chaine.length > 14 )
		{
			return false;
		}

		if ( pos == 0 )
		{
			return false;
		}

		arr_tel = chaine.split( ' ' );

		if ( arr_tel.length != 5 )
		{
			return false;
		}

		for ( i = 0;i < arr_tel.length;i++ )
		{
			if ( !( verifieEntier( arr_tel[ i ] ) ) )
			{
				return false;
			}
		}
	}

	return true;
}

/*
Cette fonction teste un champ de type entier
*/
function verifieEntier( chaine )
{

	var autorise = '0123456789';

	//on parcours toute la chaine de caractères

	for ( var i = 0; i < chaine.length; i++ )
	{
		c = chaine.substr( i, 1 ); // ième caractère

		if ( autorise.indexOf( c, 0 ) == -1 )
			return false;
	}

	return true;
}

/*
Cette fonction teste un champ de type décimal
*/
function verifieDecimal( chaine )
{

	var autorise = '0123456789.,';

	//on parcours toute la chaine de caractères

	for ( var i = 0; i < chaine.length; i++ )
	{
		c = chaine.substr( i, 1 ); // ième caractère

		if ( autorise.indexOf( c, 0 ) == -1 )
			return false;
	}

	return true;
}

//Fonction qui compare deux dates et qui indique
//false si la date de début est plus grande que la date de fin.
function compareDate( date_debut, date_fin )
{

	var oDate_1 = new Date();
	var oDate_2 = new Date();

	var arr_datedebut = date_debut.split( '/' );
	var arr_datefin = date_fin.split( '/' );

	// date de début :
	oDate_1.setDate( arr_datedebut[ 0 ] );
	oDate_1.setMonth( arr_datedebut[ 1 ] );
	oDate_1.setYear( arr_datedebut[ 2 ] );

	// date de fin :
	oDate_2.setDate( arr_datefin[ 0 ] );
	oDate_2.setMonth( arr_datefin[ 1 ] );
	oDate_2.setYear( arr_datefin[ 2 ] );

	if ( ( oDate_1.valueOf() - oDate_2.valueOf() ) > 0 )
	{
		return false;
	}

	return true;
}

/*
Définition:Cette fonction permet de vérifier que la longueur d'un champ texte saisi dans un TEXTAREA ne dépasse pas une taille limite
Paramètres en entrée:Champ : texte saisi;longueur : taille maximale autorisée
Paramètres en sortie:Aucun
*/
function verifieTextarea( champ, longueur )
{
	texte = champ.value;
	//alert(champ.value)

	if ( texte.length >= longueur )
	{
		texte = texte.substr( 0, ( longueur - 1 ) );
		champ.value = texte;
		window.alert( ' ' + longueur + ' ' );
		champ.focus()
		return false;
	}

	return true;
}

function verifieFichier( chaine )
{
	var autorise = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_';

	//on parcours toute la chaine de caractères

	for ( var i = 0; i < chaine.length; i++ )
	{
		c = chaine.substr( i, 1 ); // ième caractère

		if ( autorise.indexOf( c, 0 ) == -1 )
			return false;
	}

	return true;
}
