function valideMail(email) 	{
	var at = email.lastIndexOf("@");

	// Make sure the at (@) sybmol exists and  
	// it is not the first or last character
	if (at < 1 || (at + 1) === email.length)
		return false;

	// Make sure there aren't multiple periods together
	if (/(\.{2,})/.test(email))
		return false;

	// Break up the local and domain portions
	var local = email.substring(0, at);
	var domain = email.substring(at + 1);

	// Check lengths
	if (local.length < 1 || local.length > 64 || domain.length < 4 || domain.length > 255)
		return false;

	// Make sure local and domain don't start with or end with a period
	if (/(^\.|\.$)/.test(local) || /(^\.|\.$)/.test(domain))
		return false;

	// Check for quoted-string addresses
	// Since almost anything is allowed in a quoted-string address,
	// we're just going to let them go through
	if (!/^"(.+)"$/.test(local)) {
		// It's a dot-string address...check for valid characters
		if (!/^[-a-zA-Z0-9!#$%*\/?|^{}`~&'+=_\.]*$/.test(local))
			return false;
	}

	// Make sure domain contains only valid characters and at least one period
	if (!/^[-a-zA-Z0-9\.]*$/.test(domain) || domain.indexOf(".") === -1)
		return false;	

	return true;
}
$(document).ready(function() {
	$('#envoyer').click(function(e)	{
		$('#msg').html('');
		message = '';
		if($('#nom').val()=='')
		{
			if(message == '')	message = 'Champs obligatoires: Nom';
			else	message += ', Nom';
			$('#nom').css('background','#db7897');
		}
		else	$('#nom').css('background','');
		if($('#message').val()=='')
		{
			if(message == '')	message = 'Champs obligatoires: Message';
			else	message += ', Message';
			$('#message').css('background','#db7897');
		}
		else	$('#message').css('background','');
		
		if($('#mail').val()=='')
		{
			if(message == '')	message = 'Champs obligatoires: Email';
			else	message += ', Email';
			$('#mail').css('background','#db7897');
		}
		else
		{
			if(!valideMail($('#mail').val()))
			{
				if(message == '')	message = 'Email invalide';
				else	message += '<br /><br />Email invalide';
				$('#mail').css('background','#db7897');
			}
			else	$('#mail').css('background','');
		}
		if(message != '')
		{
			$('#msg').html(message).css('color','#db7897').css('font-weight','bold');
			return false;
		}
		else
		{
			try{
				$.ajax({
					url: 'mail.php',
					data: 'nom='+$('#nom').val()+'&mail='+$('#mail').val()+'&tel='+$('#tel').val()
								+'&sujet='+$('#sujet').val()+'&msg='+$('#message').val()
								+'&action=send',
					type: 'post',
					cache: false,
					dataType: 'html',
					success: function (data) {
						$('#msg').html("Message envoyé avec succès").css('color','#db7897').css('font-weight','bold');
					},
					error: function (xhr) {
						$('#msg').html(xhr.status + ': ' + xhr.statusText).css('color','#db7897').css('font-weight','bold');
					}
				});
			}
			catch(e)	{	alert(e);	}
		}
	});
});
