/*
 *      formulaire_contact.js
 *      
 *      Auteur : Marc FREREBEAU <marc.frerebeau@agama.fr>
 *      Société : AGAMA
 *      Licence : GPL
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 *      
 *      
 *      Application : Monplannnig
 *      Version : 2.0 
 *      
 *      Nom module : contact
 *      Description :
 *      	validation du formulaire de contact	
 *      Nécessite : 
 *      	validation_champs.js	
 *      Date : 17/03/2010
 *      Version : 0.1
 */

Event.observe( window, 'load', init_formulaire, false );

/*
* Fonction init_formulaire
* 
* 	
* Description :
* Initialise les zones de notifications
* 
* Entrées :
* @Néant
* 
* Sorties :
* @Néant
* 
* Valeur de retour :
* @Néant
* 
* Auteurs : Marc FREREBEAU
* Date dernière modification : 24/01/2011
* Commentaires :
*		
*/
function init_formulaire()
{
	if( $('notification_format').getAttribute( 'etat' ) == "yes" )
		Element.show( 'notification_format' );
	if( $('notification_preference_contact').getAttribute( 'etat' ) == "yes" )
		Element.show( 'notification_format' );
}

//////////////////////////////////////////////////////////////////////
// Fonction envoyer_formulaire_contact
//
// Description :
//		Valide le formulaire et l'envoie le cas échéant.
// Entrées :
//		@Néant
// Sorties :
//		@Néant
// Valeur de retour :
//		*Néant
// Auteurs : Marc FREREBEAU
// Date dernière modification : 06/04/2010
// Commentaires :
//		
//////////////////////////////////////////////////////////////////////
function envoyer_formulaire_contact()
{
	var form = $('formulaire_contact');
	if( valider_formulaire_contact() )
		// envoie le formulaire
		form.submit();
	else
		document.location = "#";
}

//////////////////////////////////////////////////////////////////////
// Fonction valider_formulaire_contact
//
// Description :
//		Valide le formulaire
// Entrées :
//		@form (objet) : formulaire
// Sorties :
//		@Néant
// Valeur de retour :
//		* true : le formulaire peut être envoyé
//		* false : formulaire ne doit pas être envoyé
// Auteurs : Marc FREREBEAU
// Date dernière modification : 06/04/2010
// Commentaires :
//		
//////////////////////////////////////////////////////////////////////
function valider_formulaire_contact()
{
	var notification_format = false;
	var notification_preference_contact = true;
	
	if( ( $F('nom') == '' ) || !lib_validation_est_nom( $F('nom') ) )
	{
		notification_format = true;
		$('nom').className = 'erreur';
	}
	else
		$('nom').className = '';
		
	if( ( $F('prenom') != '' ) && !lib_validation_est_nom( $F('prenom') ) )
	{
		notification_format = true;
		$('prenom').className = 'erreur';
	}
	else
		$('prenom').className = '';
		
	if( ( $F('telephone') != '' ) && !lib_validation_est_telephone( $F('telephone') ) )
	{
		notification_format = true;
		$('telephone').className = 'erreur';
	}
	else
		$('telephone').className = '';
		
	if( ( $F('email') == '' ) || !lib_validation_est_email( $F('email') ) )
	{
		notification_format = true;
		$('email').className = 'erreur';
	}
	else
		$('email').className = '';

	var liste = document.getElementsByTagName('input');
	for (var i = 0; i < liste.length ; i++)
	{
		if( ( liste[i].type == 'checkbox' ) && ( liste[i].checked == true ) )
		{
			notification_preference_contact = false;
			break;
		}
	}
	if( notification_preference_contact )
		$('preference_contact').className = 'erreur';
	else
		$('preference_contact').className = '';

	if( $F('message') == '' ) 
	{
		notification_format = true;
		$('message').className = 'erreur';
	}
	else
		$('message').className = '';
		
	if( notification_format )
		Element.show('notification_format');
	else
		Element.hide('notification_format');

	if( notification_preference_contact )
		Element.show('notification_preference_contact');
	else
		Element.hide('notification_preference_contact');

	if( notification_preference_contact || notification_format )
		return false;
		
	return true;
}

