private void RecyclePrivateMessage(string divider, string subjectPrefix)
    {
        string subject = lblSubject.Text;

        if (subject.IndexOf(subjectPrefix) != 0)
        {
            subject = subjectPrefix + subject;
        }

        PrivateMessage pm = PrivateMessagesManager.GetPrivateMessage(hidPrivateMessageId.Value);

        string parentPrivateMessage = hidPrivateMessageId.Value;
        User   fromUser             = MembershipManager.GetUserByName(lblFrom.Text);

        string from = pm.Sender.Person.FirstName + " " + pm.Sender.Person.LastName + " (" + pm.Sender.Name + ")";

        ResetAll();

        hidPrivateMessageId.Value = parentPrivateMessage;
        txtNewTo.Text             = fromUser.Person.FirstName + " " + fromUser.Person.LastName + " (" + fromUser.Name + "), ";;
        txtNewSubject.Text        = subject;
        txtNewBody.Text           = string.Format(RecyledPrivateMessage, divider, from, pm.SentDate.ToString(), pm.Recipients, txtNewSubject.Text, pm.Body);

        divNewPrivateMessage.Visible = true;
    }
    private void OpenPrivateMessage(string privateMessageId)
    {
        ResetAll();
        divList.Visible           = false;
        hidPrivateMessageId.Value = privateMessageId;

        PrivateMessage msg = PrivateMessagesManager.GetPrivateMessage(privateMessageId);

        if (msg != null)
        {
            User currentUser = MembershipManager.GetUserByName(Page.User.Identity.Name);
            if (currentUser.Id == msg.Recipient.Id || currentUser.Id == msg.Sender.Id)
            {
                if (msg.ReadDate == null)
                {
                    PrivateMessagesManager.MarkAsRead(msg);
                }

                divViewPrivateMessage.Visible = true;

                lblDateSent.Text = PrivateMessagesManager.FormatDateForDisplay(msg.SentDate);
                lblTo.Text       = msg.Recipients;
                lblFrom.Text     = msg.Sender.Name;
                lblSubject.Text  = msg.Subject;
                txtBody.Text     = msg.Body;
            }
            else
            {
                ((IFeedback)Page.Master).SetError(GetType(), ErrorPermissionDenied);
            }
        }
        else
        {
            ((IFeedback)Page.Master).SetError(GetType(), ErrorUnableToOpen);
        }
    }
    private void SendPrivateMessage()
    {
        if (string.IsNullOrEmpty(txtNewSubject.Text.Trim()))
        {
            ((IFeedback)Page.Master).SetError(GetType(), ErrorSubjectEmpty);
        }
        if (string.IsNullOrEmpty(txtNewBody.Text.Trim()))
        {
            ((IFeedback)Page.Master).SetError(GetType(), ErrorBodyEmpty);
        }
        else
        {
            bool illFormedRecipientString = false;

            List <User> users = new List <User>();

            foreach (string recipient in txtNewTo.Text.Split(','))
            {
                if (!string.IsNullOrEmpty(recipient.Trim()))
                {
                    if ((recipient.IndexOf("(") < 0 || recipient.IndexOf(")") < 0) ||
                        (recipient.LastIndexOf("(") != recipient.IndexOf("(")) ||
                        (recipient.LastIndexOf(")") != recipient.IndexOf(")")))
                    {
                        illFormedRecipientString = true;
                    }
                    else
                    {
                        string username = (recipient.Split('(')[1]).Split(')')[0];;
                        try
                        {
                            User user = MembershipManager.GetUserByName(username);
                            if (user == null)
                            {
                                illFormedRecipientString = true;
                            }
                            else if (!users.Contains(user))
                            {
                                users.Add(user);
                            }
                        }
                        catch (Exception)
                        {
                            illFormedRecipientString = true;
                        }
                    }
                }
            }

            if (illFormedRecipientString || users.Count == 0)
            {
                ((IFeedback)Page.Master).SetError(GetType(), ErrorIllFormedRecipientString);
            }
            else
            {
                var  recipients = new StringBuilder();
                bool first      = true;
                foreach (User user in users)
                {
                    if (first)
                    {
                        first = false;
                    }
                    else
                    {
                        recipients.Append(", ");
                    }

                    recipients.Append(user.Person.FirstName);
                    recipients.Append(" ");
                    recipients.Append(user.Person.LastName);
                    recipients.Append(" (");
                    recipients.Append(user.Name).Append(")");
                }

                foreach (User user in users)
                {
                    var pMsg = new PrivateMessage();
                    pMsg.Recipient  = user;
                    pMsg.Sender     = MembershipManager.GetUserByName(Page.User.Identity.Name);
                    pMsg.Recipients = recipients.ToString();
                    pMsg.Subject    = txtNewSubject.Text;
                    pMsg.Body       = txtNewBody.Text;
                    string parentPrivateMessageId = hidPrivateMessageId.Value;
                    if (!string.IsNullOrEmpty(parentPrivateMessageId))
                    {
                        PrivateMessage parentMessage = PrivateMessagesManager.GetPrivateMessage(parentPrivateMessageId);
                        if (parentMessage != null)
                        {
                            pMsg.ParentPrivateMessage = parentMessage;
                        }
                    }
                    PrivateMessagesManager.Send(pMsg);
                }
                OpenInbox();
            }
        }
    }