﻿
/**
 * Komentarų modulis
 *
 * @copyright Copyright (c) 2009, inaction.lt
 * @author    Benas Valančius <benas@inaction.lt>
 * @package   Framework
 *
 * $Id: comments.js 180 2009-01-30 18:10:03Z noen $
 */

var comments = {
    /**
     * Naujas komentaras
     */
    addComment: function()
    {
        var vars = getFormElements('commentsForm');

        if(vars.surname != '')
            return;

        if(vars.comment == '' || vars.name == '' || !form.checkemail(vars.email))
        {
            $('#errorMessage').show();
            return;
        }

        $('#commentsSubmiter').attr('disabled',true);

        new Ajax.Post(
        {
            url     : '/comments/addComment',
            data    : vars,
            json    : true,
            success : function(data)
            {
                if(data && data != '')
                {
                    $('#commentsBlock').html(data.html);
                    $('#commentsPage').html(data.page);
                }
                $('#commentsName').val('');
                $('#commentsEmail').val('');
                $('#commentsField').val('');
                $('#commentsSubmiter').attr('disabled',false);
                $('#errorMessage').hide();
            }
        });
    },

    /**
     * Delete comment
     *
     * @param integer itemID - Comment ID
     */
    deleteComment: function(itemID)
    {
        ecto.confirm(_('warning_delete_comment'), 'comments.deleteCommentContinue('+ itemID +')');
    },

    /**
     * Delete comment
     *
     * @param integer itemID - Comment ID
     */
    deleteCommentContinue: function(itemID)
    {
        var page = $('#commentsPage').html();

        new Ajax.Post(
        {
            url     : '/comments/deleteComment',
            data    : {id:itemID,page:page},
            json    : true,
            success : function(data)
            {
                if(data && data != '')
                {
                    if(data.redirect !== undefined)
                    {
                        window.location = data.redirect;
                        return;
                    }

                    $('#commentsBlock').html(data.html);
                    $('#commentsPage').html(data.page);
                }
                ecto.closeConfirm();
            }
        });
    },

    /**
     * Reset form
     */
    resetField: function()
    {
        $('#commentsName').val('');
        $('#commentsName').focus();
        $('#commentsEmail').val('');
        $('#commentsField').val('');
    }
};

