initMailForm = (function() {
	
//console.log(trads.mailOk[lg]);

//
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////// Some variables
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//	
	var requiredFieldsArray = new Array();
	setRequiredFields();
	
//
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////// Click Actions
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//	
	$('.mail-form').click(function() {
		$('#mail-form').css('display','block');
		initForm();
		launchLightbox();
	});
	
	
	$('.close-mail-form').click(function() {
		$('#mail-form').css('display','none');
		removeLightbox();
	});
	
	$('.valid-mail-form').click(function() {
		
		hideErrors();
		
		// check first field
		if(	($('#mail').val()!='') && (valideEmail($('#mail').val()))){
			
			// check the recipients
			var recipients = $('#recipients').val().split(',');
			var recipientsValid = true;
			recipients.forEach(function(y) { 
				if(!valideEmail(y)){
					recipientsValid= false;
				}
			});
			if(	($('#recipients').val()!='') && (recipientsValid == true))	{
				
				// check the message
				if($('#message').val()!=''){
					sendMail();
				}else{
					displayError('message');
				}
				
			}else{
				displayError('recipients');
			}
			
		}else{
			displayError('mail');
		}
	});

	
//
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////// Initialise les valeurs du form
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//
	function initForm(){
		$('#mail-form input[id=mail]').val("");
		$('#mail-form input[id=recipients]').val("");
		$('#mail-form #message').val("");
		$('#mail-form #form-holder').css({display:'block'});
		$('#mail-form').css({height: '290px'});
		$('#mail-form-confirm').css({display:"none"});
		
		hideErrors();
	}
//
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////// Add Lightbox
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//
	
	function launchLightbox(){
		$('body').append('<div id="lightbox"></div>');
		$('#lightbox').css({
			'opacity':0,
			'height':$(document).height(),
			'width':$(window).width()
			})
		$('#lightbox').fadeTo("slow", 0.8);
	}
	
//
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////// Remove Lightbox
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//
	
	function removeLightbox(){
		$('#lightbox').fadeTo("slow", 0, function () {
           $('#lightbox').remove();
    });
	}	
//
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////// Store the required fields
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//
	function setRequiredFields(){
		$(".required").each(function() {
			requiredFieldsArray[$(this).attr('id')] = $(this).val();
		});	
	}
});


//
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////// Check if all required filed are filled (don't check the value or format)
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//			
	function valideFormFilled(){
		formValid = true;
		$(".required").each(function() {
			
			if ($(this).val() == ""){
				displayError($(this).attr('id'));
				formValid = false;
				return formValid;
			}
		});
		return formValid;
	}
	
//
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////// Check the format
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//			
	function valideFormFormat(){
		formValid = true;
		
		$(".required").each(function() {	
			switch($(this).attr('id'))
			{
				case 'mail':
				if (!valideEmail($(this).val())){
					displayError($(this).attr('id'));
					formValid = false;
  					break;
				}
				default:
  					//code to be executed
			}	
		});
		return formValid;
	}
	
//
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////// Send the mail
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//	
				
	function sendMail(){
		var formValuesObj = new Object();
		formValuesObj['expediteur'] = $('#mail-form input[id=mail]').val();
		formValuesObj['object_mail'] = "Nouveau mail de Citroen";
		formValuesObj['destinataires'] = $('#mail-form input[id=recipients]').val().split(',');
		formValuesObj['url'] = document.location.href;
		formValuesObj['message'] = $('#mail-form #message').val();
		$.post("services/sendMailPreService.php", { jsonValues: toJSON(formValuesObj)  },
			function(data) {
				onPostSuccess();
		});
	}	
	
//
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////// On post success
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//	

	function onPostSuccess(pMessage){
		$('#mail-form #form-holder').css('display','none');
		$('#mail-form')	
			.animate({
			    height: '167px'
			  }, 'slow', function() {
				  $('#mail-form-confirm').css({display:"block"});
		  });
	}
	
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////Valid mail format
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//

	function valideEmail(pMail){
		var reg = new RegExp('^[a-z0-9]+([_|\.|-]{1}[a-z0-9]+)*@[a-z0-9]+([_|\.|-]{1}[a-z0-9]+)*[\.]{1}[a-z]{2,6}$', 'i');
		return(reg.test(pMail));
	}
	
//
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////// Display or hide errors
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//		
	
	function displayError(pDivId){
		$('#'+pDivId).parent().find('.error').css('display','block')
		$('#'+pDivId).parent().find('.error').animate({
		height: '20px'
		}, 'fast')
	}
	
	function hideErrors(){
		$(".error").each(function() {
		  $(this).css('display','none');
		});	
	}
	
//
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////// Fonction conversion au format Json.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//			
		
		function toJSON(obj)
		{
			if (typeof obj == 'undefined')
				return 'undefined';
			else if (obj === null)
				return 'null';

			var stringescape = {
				'\b': '\\b',
				'\t': '\\t',
				'\n': '\\n',
				'\f': '\\f',
				'\r': '\\r',
				'"' : '\\"',
				'\\': '\\\\'
		        }

			var json = null, i, l, v, a = [];
			switch (obj.constructor)
			{
				case Array:
					l = obj.length;
					for (i = 0; i < l; i++)
					{
						if ((v = toJSON(obj[i])) !== null)
							a.push(v);
					}
					json = '[' + a.join(',') + ']';
					break;
				case Object:
					for (i in obj)
					{
						if (obj.hasOwnProperty(i) && (v = toJSON(obj[i])) !== null)
							a.push(toJSON(String(i)) + ':' + v);
					}
					json = '{' + a.join(',') + '}';
					break;
				case String:
					json = '"' + obj.replace(/[\x00-\x1f\\"]/g, function($0)
					{
						var c;
						if ((c = stringescape[$0]))
							return c;
						c = $0.charCodeAt();
						return '\\u00' + Math.floor(c / 16).toString(16) + (c % 16).toString(16);
					}) + '"';
					break;
				case Number:
					json = isFinite(obj) ? String(obj) : 'null';
					break;
				case Boolean:
					json = String(obj);
					break;
			}

			return json;
		}
