var str_0 = "le service destinataire du message";
var str_1 = "votre email ou votre n° de téléphone";
var str_2 = "votre email";
var str_3 = "votre n° de téléphone";
var str_4 = "votre message";
var str_5 = "Les éléments suivants sont manquants ou incorrects dans la saisie du formulaire\n\n";

function sendmail() {

	var isError = false;
	var errorArray = new Array();

	if( $F("services") == -1 ) {
		isError = true;
		errorArray.push( str_0 );
	};

	if( $F("email") == "" &&  $F("telephone") == "" ) {
		isError = true;
		errorArray.push( str_1 );
	};

	var filter  = /^([a-z0-9_\.\-])+\@(([a-z0-9\-])+\.)+([a-z0-9]{2,4})+$/i;
	if( $F("email") != "" &&  ! filter.test($F("email")) ) {
		isError = true;
		errorArray.push( str_2 );
	};

	var phone_filter  = /^(01|02|03|04|05|06|08)[0-9]{8}/gi;
	if( $F("telephone") != "" &  ! phone_filter.test( $F("telephone").replace(/ /g, '') ) ) {
		isError = true;
		errorArray.push( str_3 );
	};

	if( $F("message") == "" ) {
		isError = true;
		errorArray.push( str_4 );
	};

	if( isError ) {
		var errorStr = str_5;

		errorArray.each(function( str ) {
			errorStr += "- "+str+"\n";
		});

		alert(errorStr);
		return false;
	};

	$("ContactForm").hide();
	Effect.Appear( $("indicator") );

	var xhr = new Ajax.Request(
		'/action/sendmail.php',
			{
				method: 'post',
				parameters: Form.serialize("ContactForm"),

				onSuccess: function() { // en cas de succès status 200
					$("indicator").hide();
					Effect.Appear( $("success") );
				},

				onFailure: function() { // executé en cas d'erreur quelquonque
					$("indicator").hide();
					$("fail").show();
				},

				on500: function( xhr ) { // executé en cas d'echec de l'envoi
					$("indicator").hide();
					$("fail").innerHTML = xhr.responseText
					$("fail").show();
				}
			}
	);


	return false;
}

Event.observe( window , "load" , function() {

	$$(".text_field , textarea").each(function(elt) {

		elt.observe( "focus" , function() {
			this.addClassName('focused');
		});

		elt.observe( "blur" , function() {
			this.removeClassName('focused');
		});

	});

});
