function checkAndSubmitComment() {
  saveCookies();
  Effect.Appear("working");
  new Ajax.Request(checkCaptchaEndpoint,
  {
    method:'get',
    parameters: {password: document.getElementById('password').value, random: document.getElementById('random').value },
    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, 
    },
    onSuccess: function(transport){
      var response = '' + (transport.responseText || 'no response text');
      if (response.startsWith("Error")) {
        Effect.Fade("working");
        alert(response.trim());
      } 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");
        alert(response.trim());
      } else {
        document.getElementById('newComment').innerHTML = response.trim();
        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;
    }
