﻿var commentseed = 1;
var limit = 3;
var paginationTo = 0;
function postComment()
{
    
    var name = $("#txt_name").val();
    var location = $("#txt_location").val();
    var comment = $("#txt_comment").val();
    var nodeid = $("#nodeid").val();
    var priv = $('#chk_private').is(':checked');
    var challengeField = $("input#recaptcha_challenge_field").val();
    var responseField = $("input#recaptcha_response_field").val();
    

    $.get("AddComment.ashx", { id: nodeid, name: name, location: location, comment: comment, priv: priv, challengeField: challengeField, responseField: responseField },
      function(data) {

          if (data == "true") {


              $('#AddCommentForm').hide();
              //clear form
              $("#txt_name").val("");
              $("#txt_location").val("");
              $("#txt_comment").val("");
              $("#chk_private").attr('checked', false);
              $('#AddCommentThanks').show();
              
           }
           else if(data == "captcha") {
               $("#captchaStatus").html("Your captcha is incorrect. Please try again");
               Recaptcha.reload();
           }
            
          
      });
    
}
function addComments()
{

  $.get("comments.ashx", { id: $("#nodeid").val(), seed: commentseed, limit: limit },
      function(data) {
          
          if ($(".totalComments:first").text() == "0") {
              $(".pagination_to").text("0");
              $("a.nextcomments").hide();
          }
          else {
              if (parseInt(commentseed) <= parseInt($(".totalComments:first").text())) {
                  $(".blog_comments").append(data);

                  paginationTo = parseInt(paginationTo) + parseInt(limit);

                  totalComments = $(".totalComments:first").text();

                  commentseed = commentseed + parseInt(limit);



                  if (parseInt(paginationTo) > parseInt(totalComments)) {
                      paginationTo = totalComments;
                  }


                  $(".pagination_to").text("1-" + paginationTo);
                  var commentsLeft = limit;
                  if ((parseInt(totalComments) - parseInt(paginationTo)) < parseInt(limit)) {
                      commentsLeft = parseInt(totalComments) - parseInt(paginationTo);
                  }
                  
                  if (commentsLeft > 0) {
                      $("a.nextcomments").show();
                      $("a.nextcomments").text("Show Next " + commentsLeft + " Comment(s)");
                  }
                  else {
                      $("a.nextcomments").hide();
                  }

              }
          }

      });
}
function limitChars(textid, limit, infodiv)
{

  var text = $('#'+textid).val(); 
  var textlength = text.length;

  if(textlength > limit)
  {

       $('#' + infodiv).html('You cannot write more then '+limit+' characters!');
       $('#'+textid).val(text.substr(0,limit));
       return false;

   }
   else
   {

      $('#' + infodiv).html('You have '+ (limit - textlength) +' characters left.');
      return true;
    }

}


$(document).ready(function() {

  $(".addCommentLink").click(function() {
     $('#AddCommentForm').show();
     $('#AddCommentThanks').hide();
     $("#captchaStatus").html(" ");
     Recaptcha.reload();
     return false;

  }); 
  
  $(".closeCommentLink").click(function() {
     $('#AddCommentForm').hide();
     $('#AddCommentThanks').hide();
     return false;

  }); 

  addComments();
  
  $(".nextcomments").click(function() {
     addComments();
     return false;

  });
  
  $("#postCommentLink").click(function() {
     postComment();
     return false;

  });
  
  $('#txt_comment').keyup(function(){
     limitChars('txt_comment', 200, 'charlimitinfo');

  })
  $('#txt_comment').change(function(){
     limitChars('txt_comment', 200, 'charlimitinfo');

  })
  limitChars('txt_comment', 200, 'charlimitinfo');
});
