function showCommentDialog()
{
  document.forms['commentform'].reset();
  $('#save').attr("disabled", 0);
  $('#cancel').attr("disabled", 0);
  
  $("#commentform").show("slow");
}

function cancelComment()
{
  document.forms['commentform'].reset();
  $("#commentform").hide("slow");
}

function submitComment()
{
  $('#save').attr("disabled", 1);
  $('#cancel').attr("disabled", 1);

  var url = '/blog/api/addcomment.xml';

  var author = $('#author').attr('value');
  var email = $('#email').attr('value');
  var comment = $('#comment').attr('value');
  var title = $('#title').attr('value');
  var curl = $('#url').attr('value');
  
  if (author == '') {
    alert("You must include your name to post a comment.");
    $('#save').attr("disabled", 0);
    $('#cancel').attr("disabled", 0);
  } else if (comment == '') {
    alert("You must include a comment to post a comment.");
    $('#save').attr("disabled", 0);
    $('#cancel').attr("disabled", 0);
  } else {
    $.post(url, { title: title, author: author, email: email, url: curl, comment: comment }, function(xml) { submitCommentCallback(xml); }, "xml");
  }
}

function submitCommentCallback(xml)
{
  document.forms['commentform'].reset();
  $('#save').attr("disabled", 0);
  $('#cancel').attr("disabled", 0);
  $("#commentform").hide("slow");
  
  $("#comments").slideUp("slow");
  $("#comments").empty();
  
  var title = $('#title').attr('value');
  
  $.get('/blog/api/comments.xml', { title: title }, function(xml) { updateCommentsCallback(xml); });
}

function updateCommentsCallback(xml)
{
  $(xml).find('comment').each(function() {
     var author = $('author', this).text();
     var date = $('date', this).text();
     var url = $('url', this).text();
     var comment = $('response', this).text();
     
     appendComment(author, date, url, comment);
  });
  
  $('#comments').slideDown("slow");
}

function appendComment(author, date, url, comment)
{
  var html = '<div class="comment"><h5>' + author + ' - ' + date + '</h5>';
  if (url) {
    html += '<a href="' + url + '" class="external">' + url + '</a><br />';
  }
  html += comment;
  html += '</div>';

  $('#comments').append(html);
}