
var initFormTri = function(formTri){
	$(formTri+' select').each(function(){
		var select = $(this);
		$(this).siblings('.jNiceSelectWrapper').each(function(){
			$('ul li a',$(this)).click(function(){
				//alert(select.val());
				document.form_search.submit();
			});
		});
	});
};
var initFormNavGuide = function(formTri){
	$(formTri+' select').each(function(){
		var select = $(this);
		$(this).siblings('.jNiceSelectWrapper').each(function(){
			$('ul li a',$(this)).click(function(){
				//alert(select.val());
				window.location.href=select.val();
			});
		});
	});
};

var initSlideShow = function(){
	var callbackSlideShow = function(carousel){
		$('.jcarousel-control').css('display','block');
		$('.jcarousel-control a').each(function(i){
			$(this).addClass('jcarousel-control-'+(i+1))
				   .bind('click',function(){
				        carousel.scroll(jQuery.jcarousel.intval(i+1));
				        $('.jcarousel-control a').removeClass('on');
				        $(this).addClass('on');
				        return false;
		    });
		    if(i==0) $(this).addClass('on');
		});
	}
	var itemLoadCallbackSlideShow = function(carousel){
		var index = 1-jQuery.jcarousel.intval(carousel.list.css(carousel.lt))/$('.jcarousel-clip').width();
		$('.jcarousel-control a').removeClass('on');
		$('.jcarousel-control-'+index).addClass('on');
	}
	$('.slideshow').jcarousel({
		start : 1,
		visible : 1,
		scroll: 1,
		initCallback: callbackSlideShow,
		itemLoadCallback : itemLoadCallbackSlideShow
	});
};

var checkEmail = function(email){
	return email.match(new RegExp('^[a-z0-9._-]+@[a-z0-9._-]{2,}[.][a-z]{2,4}$','i'));
}

var checkTelephone = function(tel){
	return tel.match(new RegExp('^0[1-68]([-. ]?[0-9]{2}){4}$','i'));
}

var initFormContact = function(){
	//close
	$('.erreur .close').click(function(){
		$('.erreur').fadeOut('slow');
	});
	//submit
	var form = document.form_contact;
	if(form){
		$(form).submit(function(){
			if (
					($.trim($('#prenom').val()) == "") 
					||
					($.trim($('#nom').val()) == "") 
					||
					($.trim($('#email').val()) == "") 
					||
					($.trim($('#telephone').val()) == "") 
					||
					(
								($('#mlle:checked').length == 0) 
										&&
								($('#mme:checked').length == 0)
										&&
								($('#mr:checked').length == 0)
										
					)
				
				) {  	
					$('.erreur div').html("Votre saisie est incomplète. <br/>Merci de renseigner tous les champs obligatoires.");
					$('.erreur').fadeIn('slow');  
					return false;
			}
			if (!(checkEmail($('#email').val()))) {
					$('.erreur div').html("Votre e-mail est incorrecte. <br/>Merci de vérifier votre saisie.");
					$('.erreur').fadeIn('slow');  
					return false;								
														
			}
			if (!(checkTelephone($('#telephone').val()))) {
					$('.erreur div').html("Votre numéro de téléphone est incorrect. <br/>Merci de vérifier votre saisie.");
					$('.erreur').fadeIn('slow');  
					return false;								
														
			}
			
			var mycivilite = ""
			mycivilite = ($('#mr:checked').length == 0) ? mycivilite : "Mr";
			mycivilite = ($('#mlle:checked').length == 0) ? mycivilite : "Mlle";
			mycivilite = ($('#mme:checked').length == 0) ? mycivilite : "Mme";
			
			var is_newsletter = ($('#is_newsletter:checked').length == 0) ? 0 : 1;
			
			var params = {
				'prenom' : $.trim($('#prenom').val()),
				'nom' : $.trim($('#nom').val()),
				'email' : $.trim($('#email').val()),
				'telephone' : $.trim($('#telephone').val()),
				'message' : $.trim($('#message').val()),
				'civilite' : mycivilite,
				'is_newsletter' : is_newsletter,
				'isformnewsletter' :  $.trim($('#isformnewsletter').val())
			}
			
			$.ajax({
				data : params,
				url  : '/process/nous_contacter.php',
				complete : function(xhr, status){
					if(status=="success"){
						$('.erreur div').html("Merci.<br/>Votre demande nous a bien été transmise.");
						$('.erreur').fadeIn('slow');
						form.reset();
					}
					else {
						$('.erreur div').html("Une erreur est survenue. Veuillez réessayer ou nous contacter par téléphone.");
						$('.erreur').fadeIn('slow');
					}
				},
				type : 'post'
			});
			return(false);
		});
	}
};
