//placer dynamiquement les div pellicule et etat_chargement après la balise body
if ($('div#pellicule').length == 0)
{
	$('body').prepend('<div id="pellicule"></div>');
	$('div#pellicule').css('display','none');
}
if ($('div#etat_chargement').length == 0)
{
	$('body').prepend('<div id="etat_chargement"></div>');
	$('div#etat_chargement').css('display','none');
}
if ($.browser.msie && $.browser.version == '6.0')
{
	$('div#pellicule').css('width',$(document).width()).css('height',$('html').height());
	$(window).scroll(function(){
		$('div#pellicule').css('top',$('html').scrollTop());
		$('div#etat_chargement').css('top',$('html').scrollTop()+350);
	});
}

//fonction de vérification du formulaire
function verif_commentaire_non_connecte()
{
	if ($('div#container_form_comment form input#nom').val() == '')
	{
		alert("Veuillez saisir votre nom.");
		$('div#container_form_comment form input#nom').focus().select();
		return false;
	}

	if ($('div#container_form_comment form input#prenom').val() == '')
	{
		alert("Veuillez saisir votre pr\351nom.");
		$('div#container_form_comment form input#prenom').focus().select();
		return false;
	}
	
	if ($('div#container_form_comment form textarea#commentaire_produit').val() == '')
	{
		alert("Veuillez saisir votre commentaire.");
		$('div#container_form_comment form textarea#commentaire_produit').focus().select();
		return false;
	}
	
	var email = $('div#container_form_comment form input#email').val();
	var reponse = $('div#container_form_comment form input#reponse:checked').length;
	if (email.length == 0 && reponse != 0) 
	{
		alert("Veuillez saisir votre email.");
		$('div#container_form_comment form input#email').focus().select();
		return false;
	}
	
	if (reponse != 0)
	{
		email = email.replace(/(,|;)/, ".");
		email = email.toLowerCase();
		$('div#container_form_comment form input#email').val(email);
		
		var email_reg_w3c = /^[-a-z0-9!#$%&'*+\/=?^_`{|}~]+(\.[-a-z0-9!#$%&'*+\/=?^_`{|}~]+)*@(([a-z0-9]([-a-z0-9]*[a-z0-9]+)?){1,63}\.)+([a-z0-9]([-a-z0-9]*[a-z0-9]+)?){2,63}$/i;
		if(!email_reg_w3c.test(email))
		{
			alert("Votre email est invalide.");
			$('div#container_form_comment form input#email').focus().select();
			return false;
		}
		else
		{	
			if(email.search(/[éèëêàâäîïöôüû]/)!=-1 && reponse != 0)
			{
				alert("Votre email est invalide.");
				$('div#container_form_comment form input#email').focus().select();
				return false;
			}
		}
	}

	return true;
}

//activer / désactiver le champ mail
$('input:checkbox').click(function(){
	if ($('input#email:disabled').length != 0)
	{
		$(this).blur();
		$('input#email:disabled').removeAttr('disabled').focus().select();
	}
	else
	{
		$('input#email').attr('disabled','disabled').val('');
	}
});

//quand on clique sur le lien laisse un commentaire ... déplier/plier le formulaire
$('a#laisser_commentaire').click(function(){
	if ($('div#container_form_comment').css('height') != '0px')
	{
		$('div#container_form_comment').animate({
			height: "0px"
		},800,function(){
			$('div#container_form_comment').css('overflow','hidden');
		});
	}
	else
	{
		var h = 326;
		if ($.browser.msie)
			h = 330;
			
		h += $('div#container_form_comment form p.important:first').height() - 32;
		$('div#container_form_comment').animate({
			height: "50%"
		},800,function(){
			$('div#container_form_comment').css('overflow','visible');
			$('textarea#commentaire_produit').focus().select();
		});
	}
});

//quand on clique sur le bouton fermer
$('div#etat_chargement div.previsualisation a').click(function(){
	$(this).blur();
	$('div#etat_chargement').fadeOut("slow",function(){
		$('div#pellicule').fadeOut(function(){
			$('div#pellicule').css('display','none');
			$('div#etat_chargement').css('display','none');
		});
	});
});

//fonction appelée à la soumission du formulaire de commentaire dans fiche_produit.php
function submit_comment(comment_produit_lib,le_formulaire){
	if (verif_commentaire_non_connecte())
	{
		$('div#pellicule:not(:visible)').toggle().css('opacity','0.8');
		//lancement ajax
		$('div#etat_chargement').show().html('');
		
		$.ajax({
			url: "ajout_commentaire_produit.php",
			data: le_formulaire.serialize(),
			type: "POST",
			success: function(msg){
				//message à afficher quand c'est fait
				$('div#etat_chargement').html(msg);
				$('div#etat_chargement div.previsualisation').prepend('<p class="message">'+comment_produit_lib+'</p>');
				$('div#container_form_comment').animate({
					height: "0px"
				},800,function(){
					$('div#container_form_comment').css('overflow','hidden');
				});
			},
			error: function(){
				alert('Erreur!');
				$('div#etat_chargement').html('');
				$('div#container_form_comment').animate({
					height: "0px"
				},800,function(){
					$('div#container_form_comment').css('overflow','hidden');
				});
				$('div#etat_chargement').fadeOut("slow",function(){
					$('div#pellicule').fadeOut(function(){
						$('div#pellicule').css('display','none');
						$('div#etat_chargement').css('display','none');
					});
				});
			}
		});
		
	}
}