$(document).ready(function() {
	
	$('#formNewsletter').submit(function(){
        $('#submitNewsletter').attr("disabled", "true"); 
        
		$.post('ressources/actions/newsletter.ajax.php',{
		 section: "newsletter", action: "subscribe", email: $('#email').val()},
		 function(data){
		    if(data == 0){
				$('#formNewsletter').find('div[class=errorFeedback]').remove();
				$('#formNewsletter').prepend('<div class="errorFeedback">L\'adresse e-mail est incorrecte ou déjà enregistrée.</div>');
				$('#submitNewsletter').removeAttr('disabled');
			}
			else if(data == 1){
				$('#formNewsletter').fadeOut('slow', function(){
					$(this).empty().append('<div class="validFeedback">Vous êtes bien inscrit(e) à la newsletter.<br>Merci.</div>').fadeIn();
				})
			}
		});
		return false;
	});
	
	$('.formPoll').each(function(index){
		$(this).submit(function(){
			$poll = $(this); 
			$idPoll = getCleanId($poll.attr("id"));
			$poll.find('div[class=errorFeedback]').remove();
			$('#submitPoll'+$idPoll).attr("disabled", "true");
	 		if ($('input[@name=poll'+$idPoll+']:checked').val() === undefined){
				$poll.prepend('<div class="errorFeedback">Veuillez choisir une option.</div>');
				$('#submitPoll'+$idPoll).removeAttr('disabled');
			}
			else{
				$.post('ressources/actions/poll.ajax.php',{
				 action: "vote", id_answer: $('input[@name=poll'+$idPoll+']:checked').val(), id_poll: $idPoll},
				 function(data){
				    if(data == 0){
						$poll.prepend('<div class="errorFeedback">Vous avez déjà voté à ce sondage.</div>');
						$('#submitPoll'+$idPoll).removeAttr('disabled');
					}
					else{
						$poll.fadeOut('slow',function(){
							$(this).empty().append('<div class="validFeedback">Merci de votre vote !</div>').fadeIn();
						});
					}
				});

			}
			return false;
	        
		});	
	});
	
	
	$('#email').focus(function(){
		if($(this).val() == 'Entrez votre adresse e-mail'){
			$(this).attr('value', '').removeClass('grey').addClass('black');
		}
	});
	$('#email').blur(function(){
		if($(this).val() == ''){
			$(this).attr('value', 'Entrez votre adresse e-mail').removeClass('black').addClass('grey');
		}
	})
});





