/* Envoi email */
$(document).ready(function() {
  var defautNom = $('#toolMail #nomExpediteur').attr('value');
  var defautMail = $('#toolMail #emailExpediteur').attr('value');
  
  // Suppression de la valeur par defaut quand on clique dans un champ
  $('#toolMail :input').focus(function(){
  	$(this).attr('value', '');
  });
  
  $("#toolMail #formEnvoiMailAmi").submit(function() {
    // On recupere l'URL d'envoi du mail
    var urlMail = $('#formEnvoiMailAmi #urlAjax').attr('value');
    var data = {
    	fromName: $("#nomExpediteur").attr("value"),
    	from: $("#emailExpediteur").attr("value"),
    	to: $("#emailDestinataire").attr("value"),
    	titreArticle: $("#titre").attr("value"),
    	urlPage: $("#url").attr("value")
    }
    console.log(data);
    $.post(urlMail, data, function(ret) {
    	alert(ret);
    }, 'text');
    /*$.ajax({
        url: urlMail+'?fromName=' + $("#nomExpediteur").attr("value") + '&from=' + $("#emailExpediteur").attr("value") + '&to=' + $("#emailDestinataire").attr("value") + '&titreArticle=' + $("#titre").attr("value") + '&urlPage=' + $("#url").attr("value") + '&message=' + $("#emailMessage").attr("value"),
        type: 'GET',
        timeout: 2000,
        error: function(){
            alert('Echec de l\'envoi du mail');
        },
        success: function(html){
            alert(html);
        }
    });*/
    return false;
  });
  
  // Champ mail newsletter
  var champMail = $('form#newsletterForm input[name="email"]');
  var defaultMailValue = champMail.val();
  champMail.focus(function() {
	 $(this).attr('value', '');
  });
  // Inscription newsletter
  $('form#newsletterForm').submit(function() {
	  var reg = /^[a-z0-9._-]+@[a-z0-9.-]{2,}[.][a-z]{2,4}$/;
	  var email = $(this).find('input[name="email"]').val();
	  if(!reg.test(email)) {
		  alert('Le format de votre adresse e-mail est invalide !');
		  return false;
	  }
  });
});
