var commentform = document.getElementById('addcommentform');
var commentformbutton = document.getElementById('commentformbutton');

function openreplycomment(i)
{
  var commentform2 = document.getElementById('addcommentform_' + i);
  var commentformbutton2 = document.getElementById('commentformbutton_' + i);
  if (commentformbutton2 && commentform2) {
    if (commentform2.style.display=='none') {
      commentform2.style.display = '';
      commentformbutton2.className = 'hide_add_comment';
    } else {
      commentform2.style.display = 'none';
      commentformbutton2.className = 'show_add_comment';
    }
  }
}

function opencomment()
{
  commentform = document.getElementById('addcommentform');
  commentformbutton = document.getElementById('commentformbutton');
  if (commentformbutton && commentform) {
    if (commentform.style.display=='none') {
      commentform.style.display = '';
      commentformbutton.className = 'hide_add_comment';
    } else {
      commentform.style.display = 'none';
      commentformbutton.className = 'show_add_comment';
    }
  }
}
function showAnswerForm(id)
{
  var formdiv = document.getElementById( 'qest' + id );
  if (formdiv) {
    formdiv.style.display = (formdiv.style.display=='none' ? '' : 'none');
    document.getElementById('dashed' + id).className = (formdiv.style.display=='none' ? 'show_add_comment' : 'hide_add_comment');
    getNewImage(id);
  }
}

function sendComment(parent_id)
{
  var e = 0;
  //var parentId = $('#parent_id').val();
  var parentAppendix = '';
  if (parent_id > 0) {
    parentAppendix = '_' + parent_id;
  }

  var commentAuthorName = $('#message_name').val();
  var commentContent = $('#message_content' + parentAppendix).val();
  var commentSecureCode = $('#message_code').val();
  var commentObjectId = $('#object_id').val();
  var commentObjectType = $('#object_type').val();
  var commentMark = $('input:radio[name=message_mark]:checked').val();
  var existsUser = $('#exists_user').val();

  if ((existsUser == 0 || existsUser == 'undefined') && commentAuthorName.length == 0) {
    e = 1;
    alert2(ATEXT['comment_no_name'], 'error');
  }
  if ((existsUser == 0 || existsUser == 'undefined') && commentSecureCode.length == 0) {
    e = 1;
    alert2(ATEXT['capcha_empty'], 'error');
  }
  if (commentContent.length == 0) {
    e = 1;
    alert2(ATEXT['comment_no_text'], 'error');
  }

  if (e == 0) {
// disable controls
    $('#message_content' + parentAppendix).attr("disabled", "true");
    $('#buttonCommentSubmit' + parentAppendix).attr("disabled", "true");
    $('#buttonCommentSubmit' + parentAppendix).val('Сохраняем комментарий...');
    //
    $.ajax({
      type: 'POST',
      url: '/gate/addcomments',
      data: 'parent_id=' + parent_id + '&' + 'author=' + commentAuthorName + '&content=' + commentContent + '&code=' + commentSecureCode + '&id=' + commentObjectId + '&type=' + commentObjectType + '&mark=' + commentMark,
      success: function(requestData){
        var data = eval('(' + requestData + ')');

        $('#buttonCommentSubmit' + parentAppendix).attr('disabled', '');
        $('#message_content' + parentAppendix).attr('disabled', '');
        $('#buttonCommentSubmit' + parentAppendix).val('Отправить');
        $('#message_content' + parentAppendix).val('');
                          $('#commentList' + parentAppendix).append(data.html);

        // update counter
        var a = $('#commentsCount').html();
        var b = parseInt(a) + 1;
        $('#commentsCount').html(b)
      }
    });
  }
}