$(document).ready(function() {
  $("#buttonPostNewComment").click(validateComment);

  /*$("#postedCommentName").Watermark("Votre pseudo...");
  $("#postedComment").Watermark("Votre message...");

 $("#buttonPostNewComment").click(function(){
    var q = $('#buttonPostNewComment').val();
    if(q=='Votre pseudo...' || q=='')
    return false;
  });
*/
  $('#lettres_chiffres').click(regencaptcha);
  regencaptcha(6);

});


regencaptcha = function(captchaLength){
  $.ajax({
    type:"GET",
    url:'/bourse/rest/',
    data:"method=regenerateCaptcha",
    success:function(msg){
      if($(msg).find('status').text() == 'success'){
        var im = $(msg).find('image').text();
        $('#lettres_chiffres').html($(im));
        if(captchaLength){
          $("#captchaLength").html(captchaLength);
        }
      }
    }
  });
  return true;
}


validateComment = function(){
  var pseudo = $("#postedCommentName").val();
  var commentText = $("#postedComment").val();
  /*pseudo = jQuery.trim(pseudo);
  commentText = jQuery.trim(commentText);
  if(pseudo == '' || pseudo.length < 3 || pseudo == 'Votre pseudo...'){
    alert('le pseudo est vide ou trop court (3 chars min)');
    return false;
  }
  else if(commentText.length < 3 || commentText == 'Votre message...' ){
    alert('le texte est vide ou trop court (3 chars min)');
    return false;
  }
  */
  $.ajax({
    type:"GET",
    url:'/bourse/rest/',
    data:"method=captchaCheck&pass="+$("#captchaVerification").val(),
    success:function(msg){
      if($(msg).find('status').text() == 'success'){
         $("#comment_form").submit();
      }
      else{
        $("#captchaVerification").css('background-color', '#FF8888');
        alert("Le code de sécurité est incorrect");
      }
    }
  });
  return true;
}
