$(document).ready(function(){
	$("form").submit(function(){
		var ermail=    /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;//email     
		var ernumb = /^[-]?\d*\.?\d*$/;//chiffres
		var ertel = /\b.{10}\b/;//telephone 10 caractères
		var ercp = /\b.{5}\b/;//cp 5 chiffres
		
		var tmp = true;
		
		$('form').find(".obligatoir").each(function(){
			$this=$(this);
			var input_id = $this.parents('p').find('input').attr('id');
			var input_name = $this.parents('p').find('input').attr('name');
			var legend = $this.parents('p').find('label').text();
			var valeur = $this.parents('p').find('input').val();
			/////////////////////////////////////////////////////////////////////////
			if(valeur ==''){//verification de tous les champs
				alert('Merci de remplir le champs "'+legend+'" !');
				stopform(input_id);
				tmp = false;
				return false;
			}
			if(input_name == 'contact_cp') {//verification du champs Code postal                 
           		if(!valeur.match(ernumb)) { 
					alert("Le code postal n'est pas valide!");
					stopform(input_id);
					tmp = false;
					return false;
           		} 
         	}
			if(input_name == 'contact_email') {//verification du champs Email                  
           		if(!valeur.match(ermail)) { 
					alert("Votre Email n'est pas valide!");
					stopform(input_id);
					tmp = false;
					return false;
           		} 
         	}  
         	if(input_name =='contact_tel') {//verification du champs Téléphone  
           		if((!valeur.match(ernumb)) || (!valeur.match(ertel)))    { 
					alert("Le numero de telephone n'est pas valide!");
					stopform(input_id);
					tmp = false;
					return false;
           		}
         	}
			/////////////////////////////////////////////////////////////////////////
		});
		return tmp;
		
		function stopform(input_id){
			var notvalid = $("form #"+input_id);
			notvalid.focus();
			notvalid.parents('p').addClass('onfocus');			
			}
	});
});
