(function($) {
	$(document).ready(function(){
		var message = "";
		var lightboxHover = false;
		
		function validForm() {
			message="";
			if($('#email').val().length > 0){

				if($('#email').val().length == 0){
					message += 'Merci de saisir votre email<br />\n';
				}
				else {
					// check email format
					var re=RegExp("^([a-zA-Z0-9_-])+([.]?[a-zA-Z0-9_-]{1,})*@([a-zA-Z0-9-_]{2,}[.])+[a-zA-Z]{2,3}$");
					if(!re.test($('#email').val()))
					{
						message += 'Votre email n\'est pas valide<br />\n';
					}
					/*else if ($('#email').val()!=$('#confirm_email').val()) {
					message += 'Veuillez verifier la saisie de vos email<br />\n';
					}*/
					// check wether the email adress  already exists
				}
			}
			else if ($('#email').val()=="") {
				message += 'Merci de saisir votre email<br />\n';
			}
			
			if(message.length > 0){
				$("#msgError").html(message);
				$("#msgError").fadeIn(400);
			}
			else {
				$("#confirmationBox").fadeIn(400);
				$("#msgError").fadeOut(300);
				$("#form_collect_box").fadeOut(300);
				$("#collectMailBox, .TableGenerale").css("opacity","0.5");
				val = $("#form_collect").serialize();

				$.ajax({
					type: 'POST',
					processData: true,
					url: URLBASE+"saveForm.php",
					data: val,
					dataType: 'php',
					success: function(data){
                        return false;
					}
				});
			}
		}
		
		$("#link_valider").click(function(){
			validForm();
			return false;
		});
		
		$(".input_focus").focus(function(){
			title = $(this).attr('title');
			value = $(this).attr('value');
			if(value==title) {
					$(this).attr('value', '');
					$(this).css('color', '#333');
				}
			});

			$(".input_focus").blur(function(){
				title = $(this).attr('title');
				value = $(this).attr('value');

				if(value=='') {
					$(this).attr('value', title);
					$(this).css('color', '#888');
				}
			});
			
			
			$(".link_close_lightbox").click(function(){
				$("#confirmationBox").fadeOut(300);
                $("#form_collect_box").fadeOut(300);
				$("#top_logo, .TableGenerale").css("opacity","1");
				return false;
			})
			
			$(".link_close_lightbox").click(function(){
				$("#confirmationBox").fadeOut(300);
				$("#form_collect_box").fadeOut(300);
				 $("#custom_overlay").fadeOut(300);
				$("#top_logo, .TableGenerale").css("opacity","1");
				return false;
			})
			
			$("body, #custom_overlay").click(function(){
				if(!lightboxHover) {
					$("#confirmationBox").fadeOut(300);
					$("#form_collect_box").fadeOut(300);
					$("#custom_overlay").fadeOut(300);
					$("#top_logo, .TableGenerale").css("opacity","1");
				}
			});
			 
			$("#confirmationBox, #form_collect_box").hover(function(){
				lightboxHover = true;
			},function(){
				lightboxHover = false;
			})
			
			
			
			$('#email').keypress(function(e) {
				if ((e.which == 13 || e.keyCode == 13) && ($.browser.msie) ) {
					validForm ();
					return false; 
				}
				//$('#email').css('background-color','#fff');
			});
			
			if(CookieLightbox!="vue") {
			    $("#custom_overlay").css({"display":"block","opacity":"0.5"});
			    $("#form_collect_box").css({"display":"block"});
			
			}
			
		
	});
})(jQuery);
