function stripHTML(html)
{
   var tmp = document.createElement("DIV");
   tmp.innerHTML = html;
   return tmp.textContent||tmp.innerText;
}

function commentProcess(body) {
  var converter = new Showdown.converter();
  
  return converter.makeHtml(stripHTML(body));
}

function commentPreview(requestURL) {
  var converter = new Showdown.converter();
  var form_data = { 
    static:$('#new_comment_static').val(),
    entry_id:$('#new_comment_entry_id').val(),
    parent_id:$('#new_comment_parent_id').val(),
    __lang:$('#new_comment_lang').val(),
    armor:mtCommentArmor(),
    preview:'1',
    url:$('#new_comment_url').val(),
    sid:$('#new_comment_sid').val(),
    bakecookie:$('#new_comment_bakecookie').val(),
    author:$('#new_comment_author').val(),
    email:$('#new_comment_email').val(),
    text:commentProcess($('#new_comment_comment').val())
   };
  
  $.ajax({
      type: 'POST',
      url: requestURL,
      data: form_data,
      timeout: 30000,
      async: true,
      success: function(data) {
         $('#comment_preview_display').empty().append(data);
         $('#comment_preview_display').show(200);
      }
    });
}

function commentSubmit(requestURL) {
  var form_data = { 
    static:$('#new_comment_static').val(),
    entry_id:$('#new_comment_entry_id').val(),
    parent_id:$('#new_comment_parent_id').val(),
    __lang:$('#new_comment_lang').val(),
    armor:mtCommentArmor(),
    preview:'',
    url:$('#new_comment_url').val(),
    sid:$('#new_comment_sid').val(),
    bakecookie:$('#new_comment_bakecookie').val(),
    author:$('#new_comment_author').val(),
    email:$('#new_comment_email').val(),
    text:commentProcess($('#new_comment_comment').val())
   };
  
  $.ajax({
      type: 'POST',
      url: requestURL,
      data: form_data,
      timeout: 30000,
      async: true,
      success: function(data) {
        $('#comments-form').hide();
        $('#comment_preview_display').empty().append(data);
      }
    });
    
    return false;
}

function clear_email() {
  if ($('#email').val() == "Your email address") {
    $('#email').val("");
  }
}
