function clearCommentErrors(){
  $('.error-highlight').html('');
  $('#replysubmit').attr("disabled","disabled");
};

function processJSONNewComment(data){
  $('#replysubmit').attr("disabled","disabled");
  if (!data.errors) {
    e_msg="Thank you for your comment";
    document.getElementById('commentform').innerHTML=e_msg;
  }
  else {
    $.each(data.errors, function(i, val) {
      var tmp=$('#comment-reply-form_error_'+i);
      tmp.html(val.join('&nbsp; '));
    });
    $('#replysubmit').removeAttr("disabled");
  }
};
function feature_comment(comment_id,flag,user) {
  if (comment_id && user) {
    var data = { comment_id:comment_id, user:user };
    var args = { type:"POST", 
      url:flag_url, 
      data:data, 
      success:function(msg){
        $('a#flag'+comment_id).html(msg);
      }
               };
    $.ajax(args);
  }
  else{return false;}
};

function vote_comment(vote_url,comment_id,direction){
  if (comment_id){
    var data =  {
        model:'puppy.comments.models.WnycComment',
        direction:direction,
        object_id:comment_id,
        template_object_name:'comment',
        allow_xmlhttprequest:'True'
    };
    var args = { 
        type:'POST',
        url:vote_url,
        data:data,
        success:function(){$('#voting'+comment_id).html('Thank You For Your Vote');} 
    };
    $.ajax(args);
  }
  else{return false;}
};

function flag_for_mod(flag_for_mod_url,comment_id){
  if (comment_id){
    var data = {comment_id:comment_id};
    var args = {
      type: 'POST',
      url: flag_for_mod_url,
      data: data,
      success: function(msg){
        $('#flagformod' + comment_id).html("Thank You For Notifying Us");
      }
    };
    $.ajax(args);
  }
  else {
    return false;
  }
};

$(document).ready(function(){

  var comment_options = { 
    dataType:           'json', 
    success:            processJSONNewComment,
    beforeSubmit:       clearCommentErrors,
    type:               'PUT'
  };
  $('#commentform').ajaxForm(comment_options);
  $('#replysubmit').removeAttr("disabled");
  $('#create').removeAttr("disabled");
  
});


