//This is the javascript code requested by formulaire

//Function to validate email
//XXX: the regexp comes from the quickform email rule
function valider_email(value)
{
	//Email format
	var email_format = /^((\"[^\"\f\n\r\t\v\b]+\")|([\w\!\#\$\%\&\'\*\+\-\~\/\^\`\|\{\}]+(\.[\w\!\#\$\%\&\'\*\+\-\~\/\^\`\|\{\}]+)*))@((\[(((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9])))\])|(((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9])))|((([A-Za-z0-9\-])+\.)+[A-Za-z\-]+))$/;
	//Check if email is valid
	if (!email_format.test(value))
		return false;
	else
		return true;
}

//Function that do the check on submit
function formulaire_onsubmit()
{
	var msg_begin = 'Le ou les champs suivants ne sont pas correctement remplis :\n';
	var msg_end = 'Veuillez le ou les corriger, puis validez à nouveau le formulaire.';
	var msg = new Array();
	//Check email format
	if (!valider_email($('#email').attr('value')))
		msg.push('le champ email ne correspond pas à un email valide');
	//Check if confirm and email are identical
	if ($('#email').attr('value') != $('#confirm').attr('value'))
		msg.push('les champs email et confirmation ne correspondent pas');
	if (msg.length)
	{
		var error = '';
		for (var i = 0; i < msg.length; i++)
			error += ' - '+msg[i]+'\n';
		alert(msg_begin+error+msg_end);
		return false;
	}
	else
		return true;
}

//Add active javascript function on form element
$('#birthcalendar').ready(
	function ()
	{
		$('#birthcalendar').hide('slow');
		$('#birthday').click(
			function ()
			{
				$('#birthcalendar').show('slow');
				return false;
			}
		);
		$('#birthday').focus(
			function ()
			{
				$('#birthcalendar').show('slow');
				return false;
			}
		);
		$('#birthcalendar_submit').click(
			function ()
			{
				//Update birthday value
				$('#birthday').attr('value', $('#birthcalendar_date').attr('value')+'/'+
					$('#birthcalendar_month').attr('value')+'/'+$('#birthcalendar_year').attr('value')
				);
				//Hide the birthday calendar
				$('#birthcalendar').hide('slow');
				//Cancel submit
				return false;
			}
		);
	}
);
$('#charte').ready(
	function ()
	{
		//Hide the birthday calendar
		$('#charte').hide('slow');
		//Add click function
		$('div.charte a').click(
			function ()
			{
				//Toggle graphical charte
				$('#charte').toggle('slow');
				//Cancel submit
				return false;
			}
		);
	}
);
$('#reglement').ready(
	function ()
	{
		//Hide the birthday calendar
		$('#reglement').hide('slow');
		//Add click function
		$('div.reglement a').click(
			function ()
			{
				//Toggle graphical reglement
				$('#reglement').toggle('slow');
				//Cancel submit
				return false;
			}
		);
	}
);

