function show_commentaires() {
	$.getJSON("/index-notes.php", { a: "v", c: 3 }, function(json){
		$('div#last-commentaires').html(json.commentaires);
		$('div#last-commentaires').show();
	});
}

function show_commentaires_all() {
	$.getJSON("/index-notes.php", { a: "v", c: 15 }, function(json){
		$('div#last-commentaires').html(json.commentaires);
		$('div#last-commentaires').show();
	});
}

function add_commentaire(nom, prenom, note, commentaire, secu) {
	$.getJSON("/index-notes.php", { a: "a", n: nom, p: prenom, no: note, c: commentaire, s: secu }, function(json){
		if (json.commentaires != true) {
			alert(json.commentaires);
		} else {
			$('div#add-commentaires').hide();
			show_commentaires();
			get_note_moyenne();
			alert('Merci pour votre témoignage');
		}
	});
}

function get_note_moyenne() {
	$.getJSON("/index-notes.php", { a: "avg" }, function(json){
		$('span#note-annuelle').html(json.note_moyenne);
	});
}


function reload_captcha() {
	var img_reload = '/captcha-image?'+(new Date().getTime());
	$('img#captcha-image').attr('src', img_reload);
}
	
$(document).ready(function() {	

	get_note_moyenne();
	reload_captcha();
	
	// Formulaire
	$('span#note-container img').bind('mouseover', function() {
		$('span#note-container img[id*="star-"]').attr('src', '/img/empty-star.png');
		for (var i=1; i<=$(this).attr('id').substring(5, $(this).attr('id').length); i++) {
			$('img#star-'+i).attr('src', '/img/star.png');
		}
		$('input#note').val($(this).attr('id').substring(5, $(this).attr('id').length));
	});
	$('input[type="image"]').click(function() {
		var nom = $('input#nom').val();
		var prenom = $('input#prenom').val();
		var commentaire = $('textarea#commentaire').val();
		var note = $('input#note').val();
		var secu = $('input#secu').val();
		
		if (nom == '') {
			alert('Veuillez renseigner la première lettre de votre nom');
			return false;
		} else if (prenom == '') {
			alert('Veuillez renseigner votre prénom');
			return false;
		} else if (commentaire == '') {
			alert('Veuillez saisir un commentaire');
			return false;
		} else if (note < 1 || note > 5) {
			alert('La note est incorrecte');
			return false;
		} else if (secu == '' || secu < 2) {
			alert('La vérification de sécurité est incorrecte');
			reload_captcha();
			return false;
		}
		add_commentaire(nom, prenom, note, commentaire, secu);
		reload_captcha();
	});
	// Fin Formulaire
});
