using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Collections.Specialized;

public partial class controls_PrivateMessageView : System.Web.UI.UserControl
{
    public string csEncryptionKey = ConfigurationManager.AppSettings["DBEncryptKey"];
    public string csEncryptionVector = ConfigurationManager.AppSettings["DBEncryptVector"];
    public string csURLEncryptionKey = ConfigurationManager.AppSettings["URLEncryptKey"];

    private int ciPriMsg_ID;
    private string csMsgview;
    public string csPageProfile;

    Boolean cbOutbox = false;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Convert.ToString(Request.QueryString["msgview"]) != null)
        {
            csMsgview = Convert.ToString(Request.QueryString["msgview"]);
            if (csMsgview == "O")
            {
                cbOutbox = true;
            }
            else
            {
                cbOutbox = false;
            }
        }

        // Check for Login ----------------------------------------------------------

        if (Session["userid"] == null || Convert.ToString(Session["userid"]) == String.Empty)
        {
            Response.Redirect("Login.aspx");
        }

        int liUserID = Convert.ToInt16(Session["userid"]);

        //encrypt link to user profile page
        csPageProfile = Server.UrlEncode(digioz.Encryption64.Encrypt("Profile", csURLEncryptionKey));

        //enable links

        lblInbox.Text = "<a href=\"?page=" + Server.UrlEncode(digioz.Encryption64.Encrypt("Private Messages", csURLEncryptionKey)) + "&msgview=I\">Inbox</a>";
        lblOutbox.Text = "<a href=\"?page=" + Server.UrlEncode(digioz.Encryption64.Encrypt("Private Messages", csURLEncryptionKey)) + "&msgview=O\">Outbox</a>";
        lblSaved.Text = "<a href=\"?page=" + Server.UrlEncode(digioz.Encryption64.Encrypt("Private Messages", csURLEncryptionKey)) + "&msgview=V\">Save Messages</a>";
        lblWrite.Text = "<a href=\"?page=" + Server.UrlEncode(digioz.Encryption64.Encrypt("Private Message New", csURLEncryptionKey)) + "\">New Message</a>";

        ciPriMsg_ID = Convert.ToInt16(Request.QueryString["MsgID"]);
        if (Convert.ToString(Request.QueryString["msgview"]) != null)
        {
            csMsgview = Convert.ToString(Request.QueryString["msgview"]);
        }
        else
        {
            csMsgview = "I";
        }

        PrivateMessageList loPrivateMessage = PrivateMessageManagement.GetPrivateMessage(ciPriMsg_ID, csMsgview);

        if (loPrivateMessage.Count == 0)
        {
            lblInfoMessage.Text = "The message could not be retrieved.";
        }
        else
        {
            lstMessage.DataSource = loPrivateMessage;
            lstMessage.DataBind();
        }

        //Confirm delete
        btnDelete.Attributes.Add("onclick", "return ConfirmOnDelete();"); //attach the JavaScript function

    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        StringCollection scPriMsg_ID = new StringCollection();
        scPriMsg_ID.Add(ciPriMsg_ID.ToString());

        PrivateMessageManagement.deletePrivateMessage(scPriMsg_ID);
        if (cbOutbox)
        {
            Response.Redirect("Default.aspx?page=" + Server.UrlEncode(digioz.Encryption64.Encrypt("Private Messages", csURLEncryptionKey)) + "&msgview=O");
        }
        else
        {
            Response.Redirect("Default.aspx?page=" + Server.UrlEncode(digioz.Encryption64.Encrypt("Private Messages", csURLEncryptionKey)) + "&msgview=I");
        }
    }
}
