function comments_add_comment(type_id, target_id) {
  // Get comment / clear
  var comment = $F("comment_text");
  var comment_datestr = $('comment_datestr').value;
  
  if(comment == '') {
    alert('Please enter a comment');
    return;
  }
  
  $('comment_text').value = 'Posting your comment...';
  $('comment_text').disabled = true;
  $('comment_submit_button').disabled = true;
  
  new Ajax.Request('ajax.php', 
  {
    parameters: { 
      _action: 'comment_add',
      target_type_id: type_id, 
      target_id: target_id, 
      comment: comment
    }, 
    onSuccess: function(response) {
      // Returns comment_id and html
      var json_response = response.responseText.evalJSON();
  
      // Store New Text
      $('dbd_comments_'+comment_datestr+'_items').innerHTML = json_response.html + $('dbd_comments_'+comment_datestr+'_items').innerHTML;
      $('dbd_comments_'+comment_datestr).show();
      $('dbd_comments_'+comment_datestr+'_count').value = $('dbd_comments_'+comment_datestr+'_count').value*1 + 1;
      
      // Hide empty string if we have comments
      $('dbd_comments_count').value = $('dbd_comments_count').value*1 + 1;
      $('dbd_comments_empty').hide();      
      
      // Re-enable buttons
      $('comment_text').value = '';
      $('comment_text').disabled = false;
      $('comment_submit_button').disabled = false;
    },
    onFailure: function(){ 
      alert("Couldn't add comment...") 
    }
  });
}

function comments_delete_comment(comment_id) {
  if(!confirm("Are you sure you want to delete your comment?"))
    return;
	
  delete_str = $('comment_datestar_'+comment_id).value;
  
  $('comment_option_'+comment_id).innerHTML = 'Deleting comment...';
	
  new Ajax.Request('ajax.php', 
  {
    parameters: { 
      _action: 'comment_delete',
      comment_id: comment_id
    },
    onSuccess: function(response) {
      $('comment_'+comment_id).hide();
        
      // If Comment Has Count, Hide Day Label If Too much delete
      if($('dbd_comments_'+delete_str+'_count') != null) {  
        $('dbd_comments_'+delete_str+'_count').value = $('dbd_comments_'+delete_str+'_count').value*1 - 1;
        if($('dbd_comments_'+delete_str+'_count').value <= 0) {
          $('dbd_comments_'+delete_str+'_count').value = 0;
          $('dbd_comments_'+delete_str).hide();
        }
      }
        
      // Show empty string if we have no more comments
      if($('dbd_comments_count') != null) {
        $('dbd_comments_count').value = $('dbd_comments_count').value*1 - 1;
        if($('dbd_comments_count').value <= 0) {
          $('dbd_comments_count').value = 0;
          $('dbd_comments_empty').show();
        }
      }
    },
    onFailure: function(){ 
      alert('Couldn\'t add comment...') 
    }
  });
}

