private void AddNestedComments(string path, List <Comment> nestedComments, PlaceHolder phComments)
    {
        foreach (Comment comment in nestedComments)
        {
            CommentViewBase control = (CommentViewBase)LoadControl(path);
            if (comment.IsApproved || !BlogSettings.Instance.EnableCommentsModeration || (!comment.IsApproved && Page.User.Identity.IsAuthenticated))
            {
                control.Comment = comment;
                control.Post    = Post;

                if (comment.Comments.Count > 0)
                {
                    // find the next placeholder and add the subcomments to it
                    PlaceHolder phSubComments = control.FindControl("phSubComments") as PlaceHolder;
                    if (phSubComments != null)
                    {
                        AddNestedComments(path, comment.Comments, phSubComments);
                    }
                }

                phComments.Controls.Add(control);
            }
        }
    }
    /// <summary>
    /// Processes a callback event that targets a control.
    /// </summary>
    /// <param name="eventArgument">A string that represents an event argument to pass to the event handler.</param>
    public void RaiseCallbackEvent(string eventArgument)
    {
        if (!BlogSettings.Instance.IsCommentsEnabled)
        {
            return;
        }

        string[] args        = eventArgument.Split(new string[] { "-|-" }, StringSplitOptions.None);
        string   author      = args[0];
        string   email       = args[1];
        string   website     = args[2];
        string   country     = args[3];
        string   content     = args[4];
        bool     notify      = bool.Parse(args[5]);
        bool     isPreview   = bool.Parse(args[6]);
        string   sentCaptcha = args[7];
        //If there is no "reply to" comment, args[8] is empty
        Guid   replyToCommentID = String.IsNullOrEmpty(args[8]) ? Guid.Empty : new Guid(args[8]);
        string avatar           = args[9];

        //string storedCaptcha = hfCaptcha.Value;

        //if (sentCaptcha != storedCaptcha)
        //    return;

        Comment comment = new Comment();

        comment.Id          = Guid.NewGuid();
        comment.ParentId    = replyToCommentID;
        comment.Author      = Server.HtmlEncode(author);
        comment.Email       = email;
        comment.Content     = Server.HtmlEncode(content);
        comment.IP          = Request.UserHostAddress;
        comment.Country     = country;
        comment.DateCreated = DateTime.Now;
        comment.Parent      = Post;
        comment.IsApproved  = !BlogSettings.Instance.EnableCommentsModeration;
        comment.Avatar      = avatar.Trim();

        if (Page.User.Identity.IsAuthenticated)
        {
            comment.IsApproved = true;
        }

        if (website.Trim().Length > 0)
        {
            if (!website.ToLowerInvariant().Contains("://"))
            {
                website = "http://" + website;
            }

            Uri url;
            if (Uri.TryCreate(website, UriKind.Absolute, out url))
            {
                comment.Website = url;
            }
        }

        if (!isPreview)
        {
            if (notify && !Post.NotificationEmails.Contains(email))
            {
                Post.NotificationEmails.Add(email);
            }
            else if (!notify && Post.NotificationEmails.Contains(email))
            {
                Post.NotificationEmails.Remove(email);
            }

            Post.AddComment(comment);
            SetCookie(author, email, website, country);
        }

        string path = Utils.RelativeWebRoot + "themes/" + BlogSettings.Instance.Theme + "/CommentView.ascx";

        CommentViewBase control = (CommentViewBase)LoadControl(path);

        control.Comment = comment;
        control.Post    = Post;

        using (StringWriter sw = new StringWriter())
        {
            control.RenderControl(new HtmlTextWriter(sw));
            _Callback = sw.ToString();
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        NameInputId = "txtName" + DateTime.Now.Ticks.ToString();

        if (Post == null)
        {
            Response.Redirect(Utils.RelativeWebRoot);
        }

        if (!Page.IsPostBack && !Page.IsCallback)
        {
            if (Page.User.Identity.IsAuthenticated)
            {
                if (Request.QueryString["deletecomment"] != null)
                {
                    DeleteComment();
                }

                if (Request.QueryString["deletecommentandchildren"] != null)
                {
                    DeleteCommentAndChildren();
                }

                if (!string.IsNullOrEmpty(Request.QueryString["approvecomment"]))
                {
                    ApproveComment();
                }

                if (!string.IsNullOrEmpty(Request.QueryString["approveallcomments"]))
                {
                    ApproveAllComments();
                }
            }

            string path = Utils.RelativeWebRoot + "themes/" + BlogSettings.Instance.Theme + "/CommentView.ascx";

            if (NestingSupported)
            {
                // newer, nested comments
                AddNestedComments(path, Post.NestedComments, phComments);
            }
            else
            {
                // old, non nested code

                //Add approved Comments
                foreach (Comment comment in Post.Comments)
                {
                    if (comment.IsApproved || !BlogSettings.Instance.EnableCommentsModeration)
                    {
                        CommentViewBase control = (CommentViewBase)LoadControl(path);
                        control.Comment = comment;
                        control.Post    = Post;
                        phComments.Controls.Add(control);
                    }
                }

                //Add unapproved comments
                if (Page.User.Identity.IsAuthenticated)
                {
                    foreach (Comment comment in Post.Comments)
                    {
                        if (!comment.IsApproved)
                        {
                            CommentViewBase control = (CommentViewBase)LoadControl(path);
                            control.Comment = comment;
                            control.Post    = Post;
                            phComments.Controls.Add(control);
                        }
                    }
                }
            }


            //if (BlogSettings.Instance.IsCommentsEnabled)
            //{

            //    if (!Post.IsCommentsEnabled || (BlogSettings.Instance.DaysCommentsAreEnabled > 0 &&
            //       Post.DateCreated.AddDays(BlogSettings.Instance.DaysCommentsAreEnabled) < DateTime.Now.Date))
            //    {
            //        phAddComment.Visible = false;
            //        lbCommentsDisabled.Visible = true;
            //    }

            //    BindCountries();
            //    GetCookie();
            //    hfCaptcha.Value = Guid.NewGuid().ToString();
            //}
            //else
            //{
            //    phAddComment.Visible = false;
            //}
            //InititializeCaptcha();
        }


        Page.ClientScript.GetCallbackEventReference(this, "arg", null, string.Empty);
    }