function reply(id) {
  addComment = document.getElementById('addComment');
  addComment.style.display = 'none';
  document.getElementById('add_comment_img').src = "http://lh3.ggpht.com/_vqAAFYgeDm8/TTM7SyeAoCI/AAAAAAAAGlw/hUEwnEysZRY/s800/add_a_reply.png";
  comment = document.getElementById('obj_' + id);
  comment.parentNode.insertBefore(addComment,comment.nextSibling);
  document.getElementById('replyId').value = id;
  Effect.SlideDown(addComment);
}

function checkAndSubmitComment() {
  password = ''; random = '';
  if (document.getElementById('password') != null) { password = document.getElementById('password').value; }
  if (document.getElementById('random') != null) { random = document.getElementById('random').value; }
  saveCookies();
  Effect.Appear("working");
  new Ajax.Request(checkCaptchaEndpoint,
  {
    method:'get',
    parameters: { password: password, random: random },
    onSuccess: function(transport){
      var response = '' + (transport.responseText || 'no response text');
      if (response.trim() == 'invalid') {
        Effect.Fade("working");
        alert('Oops, this is an invalid captcha entry.  Please try again.');
      } else {
        checkAndSubmitCommentAJAX();
      }
    },
    onFailure: function(){ 
      Effect.Fade("working");
      alert('Looks like our AJAX service is down... try back later!') 
    }
  });
}

function checkAndSubmitCommentAJAX() {
  new Ajax.Request(addCommentEndpoint,
  {
    method:'post',
    parameters: {  ajax: "true",
                              password: document.getElementById('password').value, 
                              random: document.getElementById('random').value, 
                              text: document.getElementById('text').value, 
                              name: document.getElementById('name').value, 
                              email: document.getElementById('email').value, 
                              url: document.getElementById('url').value, 
                              replyId: document.getElementById('replyId').value, 
    },
    onSuccess: function(transport){
      var response = '' + (transport.responseText || 'no response text');
      if (response.startsWith("Error") || response.startsWith("Thank you so much for commenting")) {
        Effect.Fade("working");
        alert(response.trim());
        new Effect.SlideUp("addComment", { queue: { position: 'end', scope: 'newcomment' } });
      } else {
        replaceCommentForm(response.trim());
      }
    },
    onFailure: function(){ 
      Effect.Fade("working");
      alert('Looks like our AJAX service is down... try back later!') 
    }
  });
}

function replaceCommentForm(id) {
  new Ajax.Request(getCommentEndpoint,
  {
    method:'post',
    parameters: {  comment_id: id,
    },
    onSuccess: function(transport){
      var response = '' + (transport.responseText || 'no response text');
      if (response.startsWith("Error")) {
        //Effect.Fade("working");
        Effect.SlideUp("addComment");
        alert(response.trim());
      } else {
        newComment = document.getElementById('newComment');
        newComment.innerHTML = response.trim();
        newComment.className = "comment_indent";
        addComment = document.getElementById('addComment');
        addComment.parentNode.insertBefore(newComment, addComment.nextSibling);
        //document.getElementById('commentThoughts').style.display = "none";
        new Effect.SlideUp("addComment", { queue: { position: 'end', scope: 'newcomment' } });
        //new Effect.SlideDown("newCommentSpacer", { queue: { position: 'end', scope: 'newcomment' } });
        //new Effect.ScrollTo("commentBottom", { queue: { position: 'end', scope: 'newcomment' } });
        new Effect.Grow("newComment", { queue: { position: 'end', scope: 'newcomment' } });
        //new Effect.SlideUp("newCommentSpacer", { queue: { position: 'end', scope: 'newcomment' } });
      }
    },
    onFailure: function(){ 
      //Effect.Fade("working");
      alert('Looks like our AJAX service is down... try back later!') 
    }
  });
}

    var responseIds = [];
    function boxClicked(box) {
      if (box.checked) {
        if (responseIds.indexOf(box.id) == -1) { responseIds.push(box.id); }
      } else {
        var index = responseIds.indexOf(box.id);
        if (index != -1) { responseIds.splice(index,1); }
      }
    }
    function respond(commentId) {
      if (commentId && responseIds.indexOf(commentId) != -1) { responseIds.push(commentId); }

      //setCommentInput();

      toggleObject('add_comment', 'add_comment_link');
      location.hash = 'add_comment_link';
    }
    function setResponseIds() {
      if (responseIds.length > 0) {
        document.getElementById('response_comment').value = responseIds.join(",");
      }
    }

    function saveCookies() {
      var name = document.getElementById('name').value;
      var email = document.getElementById('email').value;
      var url = document.getElementById('url').value;
      createCookie("name", name, 90);
      createCookie("email", email, 90);
      createCookie("url", url, 90);
    }
    function loadCookies() {
      var name = readCookie('name');
      var email = readCookie('email');
      var url = readCookie('url');
      document.getElementById('name').value = name;
      document.getElementById('email').value = email;
      document.getElementById('url').value = url;
    }

$j(document).ready(loadCookies);
