function getForm(){
	$.post(
      '/remote/generate-form-commentaire-produit/', 
      {id_produit: $('input#id_produit').val()}, 
      function(data){
      		$('.conteneurFormCommentaireProduit').append(data);
      		$('.conteneurFormCommentaireProduit').slideDown('slow');
      		$('#formCommentaireProduit').submit(submitForm);
    	}
  );
	return false;
}

function updateForm(form) 
{
    $('#formCommentaireProduit').remove();
    $('.conteneurFormCommentaireProduit').append(form);
		$('#formCommentaireProduit').submit(submitForm);
}

function submitForm(){
	$('.remote-error').remove();
	$.post(
  		'/remote/commentaire-produit/',
  		$('#formCommentaireProduit').serialize(),
      function( data ){
    		  //console.log( data );
    			if (data.valide) {
      				$('#formCommentaireProduit').slideUp('slow', function(){
        					$(this).remove();
        					$('.callFormCommentaireProduit').remove();
        					$('.conteneurFormCommentaireProduit').append('<div class="remote-response">' + data.response + '</div>');
        					$('.remote-response').hide().slideDown('slow');
      				});
    			} else {
    				  updateForm(data.form);
    			}
  		},
  		'json'
	);
	return false;
}

$(document).ready(function(){
  	$('.callFormCommentaireProduit').click(function(){
    		if (!$('#formCommentaireProduit').length)
    		    getForm();
    		else if ($('.conteneurFormCommentaireProduit').css('display') == 'block')
    			  $('.conteneurFormCommentaireProduit').slideUp('slow');
    		else
    			  $('.conteneurFormCommentaireProduit').slideDown('slow');
    		return false;
  	});
});
