function doComment(id){
    $("#form_"+id).ajaxForm({
        beforeSubmit: function() {
            $("#reply_"+id).html("Обработка...");
        },
        success: function(data) {
            $("#reply_"+id).html("");
            $("#children_"+id).append(data);
        }
    });
    return false;   
 }

function delComment(id)
{
	var delForm = $('#delform_'+id);
	var caMess = $('#caMess'+id);
	var delInd = $('#delInd'+id);
	var delAct = $('#delAct'+id);
	var updAct = $('#updAct'+id);

        $('#delform_' + id + ' input[name="delete"]').val('on');

    delForm.ajaxForm({
        beforeSubmit: function()
        {
            if (confirm('Вы действительно хотите удалить комментарий?'))
            {
            	delInd.html('<img border="0" alt="Подождите..." src="/ayaco/images/loading.gif" width="11" height="11" class="aIndLoading">');
                return true;
            }
            
            return false;
        },
        success: function(data)
        {
			delInd.empty();
			
            var result = eval('('+data+')');
            
    		if (result.ajaxResult == 0)
    		{
    			$('#mess'+id).html(result.mess);
    			delAct.remove();
    			updAct.remove();
    		} else
    		{
    			alert(result.mess);
    		}
        },
        error: function()
        {
			delInd.empty();
    		alert('Ошибка удаления. Превышено время ожидания.');
        }
    });
    
    delForm.submit();
    
    return false;   
}

function delTreeComment(id)
{
	var delForm = $('#delform_'+id);
	var caMess = $('#caMess'+id);
	var delInd = $('#delInd'+id);
	var delAct = $('#delAct'+id);
	var updAct = $('#updAct'+id);

        $('#delform_' + id + ' input[name="delete"]').val('tree');

    delForm.ajaxForm({
        beforeSubmit: function()
        {
            if (confirm('Вы действительно хотите удалить всю ветку комментариев?'))
            {
            	delInd.html('<img border="0" alt="Подождите..." src="/ayaco/images/loading.gif" width="11" height="11" class="aIndLoading">');
                return true;
            }

            return false;
        },
        success: function(data)
        {
            delInd.empty();

            var result = eval('('+data+')');

    		if (result.ajaxResult == 0)
    		{
                    delForm.parent().parent().parent().remove();
    		} else
    		{
                    alert(result.mess);
    		}
        },
        error: function()
        {
		delInd.empty();
    		alert('Ошибка удаления. Превышено время ожидания.');
        }
    });

    delForm.submit();

    return false;
}

function editComment(id)
{
	var eQForm = $('#eQForm');
	var delInd = $('#delInd'+id);
	
	$('#eCId').val(id);

	eQForm.ajaxForm({
        beforeSubmit: function()
        {
            delInd.html('<img border="0" alt="Подождите..." src="/ayaco/images/loading.gif" width="11" height="11" class="aIndLoading">');
            return true;
        },
        success: function(data)
        {
            delInd.empty();
			
            var result = eval('('+data+')');
            
    		if (result.ajaxResult == 0)
    		{
    			jQuery("#carea_33").val(result.mess);
    			jQuery('#uCId').val(id);
    			
    			var popUpEditWindow = jQuery("#popUpEditWindow");
    			
    			popUpEditWindow.jqm();
    			popUpEditWindow.jqmShow();
    			
    		} else
    		{
    			alert(result.mess);
    		}
        },
        error: function()
        {
			delInd.empty();
    		alert('Ошибка редактирования. Превышено время ожидания.');
        }
    });
    
	eQForm.submit();
    
    return false;
}

function updateComment()
{
	if (!commentsCheck('33')) {
		return
	};
	
	
	var editForm = $('#editForm');

	var id = $('#uCId').val();
	var delInd = $('#delInd'+id);
	
	var popUpEditWindow = jQuery("#popUpEditWindow");
	
	editForm.ajaxForm({
        beforeSubmit: function()
        {
			popUpEditWindow.jqmHide();
			delInd.html('<img border="0" alt="Подождите..." src="/ayaco/images/loading.gif" width="11" height="11" class="aIndLoading">');
            return true;
        },
        success: function(data)
        {
			delInd.empty();
			
            var result = eval('('+data+')');
            
    		if (result.ajaxResult == 0)
    		{
    			popUpEditWindow.jqmHide();
    			$('#mess'+id).html(result.mess);
    		} else
    		{
    			popUpEditWindow.jqmShow();
    			alert(result.mess);
    		}
        },
        error: function()
        {
			delInd.empty();
			popUpEditWindow.jqmShow();
    		alert('Ошибка редактирования. Превышено время ожидания.');
        }
    });
    
	editForm.submit();
    
    return false;
}

function replyComment(id, isQuoted)
{
        if (isQuoted == 0)
        {
            jQuery('#carea_66').val('');
        } else
        {
            var temp = jQuery('#mess' + id).clone();
            jQuery('div.ax', temp).remove();
            jQuery('#carea_66').val('[quote]' + temp.text().replace(/^\s\s*/, '').replace(/\s\s*$/, '') + '[/quote]\n' );
        }

	jQuery('#rCId').val(id);
	jQuery('#rrCId').val(id);
    			
	var popUpReplyWindow = jQuery("#popUpReplyWindow");
    			
	popUpReplyWindow.jqm();
	popUpReplyWindow.jqmShow();
    			
    return false;
}

function doReplyComment()
{
	
	if (!commentsCheck('66')) {
		return
	};
	
	var replyForm = $('#replyForm');

	var id = $('#rCId').val();
	var delInd = $('#delInd'+id);
	
	var popUpReplyWindow = jQuery("#popUpReplyWindow");
	
	replyForm.ajaxForm({
        beforeSubmit: function()
        {
			popUpReplyWindow.jqmHide();
			delInd.html('<img border="0" alt="Подождите..." src="/ayaco/images/loading.gif" width="11" height="11" class="aIndLoading">');
            return true;
        },
        success: function(data)
        {
			delInd.empty();
			
            var result = eval('('+data+')');
            
    		if (result.ajaxResult == 0)
    		{
    			popUpReplyWindow.jqmHide();
                $('#children_' + id).append(result.mess);
    		} else
    		{
    			popUpReplyWindow.jqmShow();
    			alert(result.mess);
    		}
        },
        error: function()
        {
			delInd.empty();
			popUpReplyWindow.jqmShow();
    		alert('Ошибка. Превышено время ожидания.');
        }
    });
    
	replyForm.submit();
    
    return false;
}

function doVote(n, sign) {
	$("#divComment"+n).html("обработка..");
	$.post("/",
	  {voteCommentAjax: n, event:sign},
	  function(data){
	  $("#divComment"+n).hide();
	    $("#resultComment"+n).html(data);
	  }
	);
	return false;
}

String.prototype.replaceAll = function(search, replace){
  return this.split(search).join(replace);
}

var smyleys = [":arrow:", ":idea:", ":!:", ":wink:", ":roll:", ":twisted:", ":evil:", ":cry:", ":P", ":x", ":lol:", "8)", ":?", ":shock:", ":o", ":(", ":)", ":?:", ":oops:", ":D", "*SCRATCH*", "*WRITE*", "*WALL*", ";D", "*YES*", "*BYE*", "*HI*", "*YAHOO*", "*DANCE*", "*DONT_KNOW*", "*CRAZY*", "*NO*", "*PARDON*", "*ROFL*", "*BRAVO*", "*SORRY*", "*WASSUP*", "*OK*", "%)", '\m/', "*HELP*", "@=", "*IN LOVE*", "*DRINK*", "*THUMBS UP*", ":rose:", "*KISSING*", "*STOP*", "*TIRED*", "*KISSED*", "[:-}", "]:->", "*JOKINGLY*", ":-\\", ":-|", ">:o", ":-X", ":'(", ":-*", "=-O", ":-[", ":-D", "8-)", ":-P", ";-)", ":-(", ":-)", "O:-)"]


function commentsCheck(fid) {
	var ctext = document.getElementById('carea_'+fid).value;

	for (var i=0; i<smyleys.length; i++) {
		ctext = ctext.replaceAll(smyleys[i], '');
	}

	if (countwords(ctext)<3) {
		alert('Сообщение должно содержать не менее трех слов!');
		return false;
	}
	
	return true;
}