$(document).ready(function() {
    // set focus
    $('input.initial_focus').focus();
    $('#comment_form').ajaxForm({
     dataType: 'json',
     beforeSubmit: function(data, set, opt) {
      $('input[type=submit]', set).attr('disabled', 'disabled');
      $('input[type=submit]', set).attr('value', 'Please wait...');
     },
     success: function(data, code, set) {
       if (data.status == 'ok') {
        set.clearForm();
        $('#comment_errors').html("<div>Thank you for your comment.</div>");
        $('#comment_form_wrapper').hide();
        $('a[href$=#comments]').text(""+data.count+" comments");
        var c = '<h3>'+data.params.subject+'</h3><p>From: '+data.params.name+', Just Posted</p><p>'+data.params.body+'</p>';
        var l = $('#comments > p:first');
        if (l === undefined || l.length == 0)
          $('#comments').prepend(c);
        else
          l.after(c);
       } else {
        $('#comment_form input[type=submit]').removeAttr('disabled').attr('value', 'Submit');
        $('#comment_errors').html("<div><h2>Please correct the following problems</h2><p>" + data.message + "</p></div>");
       }
     }
   });
});
