dojo.addOnLoad(function() {
  dojo.query("#previewComment").connect('onclick', previewComment);
  var node = dojo.query("#recaptcha")[0];
  if (node) {
    Recaptcha.create("6LcvKwUAAAAAANXCKOhywiLAvDcqGxzyTAzBL6aU",
      "recaptcha", {
         theme: "white"
      });
  }
});

function handleCommentPost(response, ioArgs) {
  var commentFormDiv = dojo.query("#commentFormDiv")[0];
  commentFormDiv.innerHTML = response;
  dojo.query("#previewComment").connect('onclick', previewComment);
  var node = dojo.query("#recaptcha")[0];
  if (node) {
    Recaptcha.create("6LcvKwUAAAAAANXCKOhywiLAvDcqGxzyTAzBL6aU",
      "recaptcha", {
         theme: "white"
      });
  }
}
function previewComment(event) {
  dojo.stopEvent(event);
  console.log(dojo.formToJson('commentForm'));
  var postVar = dojo.formToObject('commentForm');
  postVar.preview = 'Preview revised comment';
  dojo.xhrPost({
    url: '/comments/postfree/',
    load: handleCommentPost,
    content: postVar
  });
}
