function writeComment(district, agreement)
{
    if (document.getElementById('authorize_form_id'))
    {
        afterLogin = function ()
        {
            writeComment(district, agreement);
        }

        openAuthorize((district == 1? 'login_only' : 'login'));
        return;
    }
    
    if (agreement == 0)
    {
	agreement = ($('#id_agreement').is(':checked')? 1 : 0)
    }

    $('#comments_loading_id').css('height', $('#comments_id .comments_box').height()+20).css('width', $('#comments_id .comments_box').width());
    $('#comments_loading_id').show();
    $('#comments_pages_loading_id').show();
    
    $('#id_submit').attr('disabled', true);
    $('LABEL').removeClass('red');
    $('#id_box_errors').hide();
    $('#id_box_loading').show();

    $.post (
        '/modules/comments/comments.php',
        {
            operation: 'add',
            table: comments_table,
            topic_id: comments_topic_id,
	    flood: comments_flood,
	    order: comments_order,
            text: $('#id_text').val(),
	    agreement: agreement,
	    hash: HASH
        },
        function(data) {
	    result = jQuery.parseJSON(data);

	    if (result['errors'].length > 0)
	    {
		tmp = '';
		for (i = 0; i < result['errors'].length; i++)
		{
		    tmp += '<div>' + result['errors'][i] + '</div>';
		}
		$('#id_box_errors').show();
		$('#id_box_errors').html(tmp);

		$('#comments_loading_id').hide();
		$('#comments_pages_loading_id').hide();
	    }
	    if (result['idx'].length > 0) {
		for (i = 0; i < result['idx'].length; i++) {
		    $("LABEL[for='" + result['idx'][i] + "']").addClass("red");
		}
	    }

	    if (result['ready'] == 1)
	    {
		$('#id_text').val('');
		$('#id_agreement').attr('checked', '');
		$('#additional_images_id').html('');
		$('#additional_images_id').hide();
		$("label[for='id_agreement']").removeClass("bold");
		getCommentsPage('last');
	    }

	    $('#id_box_loading').hide();
	    $('#id_submit').attr('disabled', false);
        }
    );
}

function showHiddenComments()
{
    $('#comments_count_id .deleted').remove();
    comments_flood = 1;
    getCommentsPage(comments_page);
}

function getCommentsPage(page)
{
    $('#comments_id').show();
    $('#comments_loading_id').css('height', $('#comments_id .comments_box').height()+20).css('width', $('#comments_id .comments_box').width());
    $('#comments_loading_id').show();
    $('#comments_pages_loading_id').show();

    $.post (
        '/modules/comments/comments.php',
        {
            operation: 'page',
            table: comments_table,
            topic_id: comments_topic_id,
            page: page,
	    flood: comments_flood,
	    order: comments_order
        },
        function(data) {
	    comments_page = page;
	    $('#comments_id').html(data);
	    $('#comments_loading_id').hide();
	    $('#comments_pages_loading_id').hide();
	    $(".commentReprove").qtip({
		content: $('#id_comment_reprove'),
		style: { 
		    border: {
			width: 5,
			radius: 5,
			color: 'Black'
		    },
		    width: {
			max: 300
		    },
		    tip: true
		},
		position: {
		    corner: {
			target: 'bottomMiddle',
			tooltip: 'topMiddle'
		    }
		}
	    });
	}
    );
}

function floodComment(obj, id)
{
    var objP = obj.parentNode;
    $(objP).html('Ожидание...');

    $.post (
	'/modules/comments/comments.php',
	{
	    operation: 'flood',
	    id: id
	},
	function(data)
	{
	    $('#comment_' + id + '_id').addClass('flood');
	    $('#comment_' + id + '_id .red').removeClass('red');
	    $('#comment_' + id + '_id .avatara').html('<div class="silverSquare">&nbsp;</div>');
	    $(objP).html('Комментарий помечен');
	}
    );
}

function clearComment(obj, id)
{
    var objP = obj.parentNode;
    $(objP).html('Ожидание...');
    $.post (
	'/modules/comments/comments.php',
	{
	    operation: 'clear',
	    id: id
	},
	function(data)
	{
	    $('#comment_' + id + '_id .complainants').remove();
	    $('#comment_' + id + '_id').removeClass('red').removeClass('green');
	    $(objP).html('Замечания удалены');
	}
    );
}

function authComment(obj, comment_id)
{
    var objP = obj.parentNode;
    $(objP).html('Ожидание...');

    $.post (
	'/modules/comments/comments.php',
	{
	    operation: 'auth',
	    id: comment_id
	},
	function(data)
	{
	    $('#comment_' + comment_id + '_id').removeClass('flood');
	    $(objP).html('Комментарий авторизирован');
	}
    );
}

function reproveComment(obj, comment_id)
{
    var objP = obj.parentNode;
    $(objP).html('Ожидание...');
    $.post (
	'/modules/comments/comments.php',
	{
	    operation: 'reprove',
	    id: comment_id
	},
	function(data)
	{
	    $(objP).html('Замечание сделано');
	}
    );
}
