function getComments(pageNum) {

   $.ajax({
     type: "POST",
     url: "ajax.php",
     async: false,
     data: "action=GetComments&scriptname="+encodeURIComponent(scriptName)+"&pagenum="+pageNum,
     success: function(msg) {
        try {
           ret = eval("("+msg+")");
           $("#comments").html('');
           if (pageNum == 0 && ret.length == 0) {
              $("#comments").append("<h3>No comments yet. Add one below.</h3>");
              return;
           }
           if (pageNum > 0) {
              $("#comments").append('<b><a href="javascript:getComments('+(pageNum-1)+')">Newer Comments...</a></b> ');
           }
           if (ret.length == 10) {
              $("#comments").append('<b><a href="javascript:getComments('+(pageNum+1)+')">Older Comments...</a></b> ');
           }
           for (i=0; i<ret.length; i++) {
              name = ret[i]["name"];
              posted = ret[i]["posted"];
              comment = ret[i]["comment"];
              id = ret[i]["id"];
              ip = ret[i]["ip"];
              $("#comments").append('<div class="comment"><a name="comment'+id+'">From <span style="font-weight: bold">'+name+'</span> at '+posted+'</a> (<a href="#comment'+id+'">link</a> / <a href="javascript:quote('+id+')">quote</a>): <span class="comment_text" id="comment_text'+id+'">'+comment+'</span></div>');
              //$("#comments").append('<div class="comment"><a name="comment'+id+'">From <span style="font-weight: bold" title="'+ip+'">'+name+'</span> at '+posted+'</a> (<a href="#comment'+id+'">link</a>): <span class="comment_text">'+comment+'</span></div>');
           }
        } catch (err) {
           alert("Error loading comments: "+err);
        }
     }
   });

   $.ajax({
     type: "POST",
     url: "ajax.php",
     async: false,
     data: "action=GetRecentComments",
     success: function(msg) {
        try {
           ret = eval("("+msg+")");
           if (ret.length == 0) {
              return;
           }
           $("#recentcomments").html('');
           for (i=0; i<ret.length; i++) {
              name = ret[i]["name"];
              posted = ret[i]["posted"];
              scriptname = decodeURIComponent(ret[i]["scriptname"]);
              comment = ret[i]["comment"];
              id = ret[i]["id"];
              if (comment.length > 100) {
                 comment = comment.substring(0,99)+"...";
              }
              $("#recentcomments").append('<div class="recentcomment"><a href="'+scriptname+'#comment'+id+'">'+posted+' from <b>'+name+'</b></a>: <em>'+comment+'</em></div>');
           }
        } catch (err) {
           alert("Error loading recent comments: "+err);
        }
     }
   });

}

function extraCaptcha() {

   var recaptcha_response_field = $("input[name=recaptcha_response_field]").val();
   var machination = prompt("We're fighting spam. Please retype the two words (you entered: \""+recaptcha_response_field+"\")", "");
   $("input[name=machination]").val(machination);

}


function newComment() {
   var name = encodeURIComponent($("#comment_name").val());
   var comment = encodeURIComponent(commenteditor.getContent());
   var recaptcha_challenge_field = $("input[name=recaptcha_challenge_field]").val();
   var machination = $("input[name=machination]").val();
   $.ajax({
      type: "POST",
      url: "ajax.php",
      async: true,
      data: "action=NewComment&scriptname="+encodeURIComponent(scriptName)+"&name="+name+"&comment="+comment+"&recaptcha_challenge_field="+recaptcha_challenge_field+"&machination="+machination,
      success: function(msg) {
         Recaptcha.reload();
         ret = eval("("+msg+")");
         if (!ret) {
            alert("You have to actually write a comment.");
            return;
         }
         getComments(0);
         commenteditor.setContent('');
         window.location='#comments';
      }
   });
}

function quote(id) {

   curcomment = commenteditor.getContent();
   quoted = $("#comment_text"+id).html();
   thisquote = "<blockquote><em>"+quoted+"</em></blockquote>(your response)";
   commenteditor.setContent(thisquote+curcomment);
   window.location="#commentform";
}
