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 digioz;

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

    const long MAX_LENGTH = 40000;

    protected void Page_Load(object sender, EventArgs e)
    {
        // Check for Login ----------------------------------------------------------

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

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

        //javascript to count the number of characters in the message box
        //txtMessage.Attributes.Add("onTextChanged","javascript:countChars();");
        txtMessage.Attributes.Add("onkeypress", "countChars('" + txtMessage.ClientID + "','" + lblCharCount.ClientID + "'," + MAX_LENGTH + ")");

    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        csMode = Convert.ToString(Request.QueryString["mode"]);
        int liUserID = Convert.ToInt16(Session["userid"]);
        string lsSubject = Utility.ConvertHtmlToPlainText(txtSubject.Text);
        string lsMessage = Utility.ConvertHtmlToPlainText(txtMessage.Text);
        DateTime ldCurrentDateTime = DateTime.Now;
        string lsDateTime = ldCurrentDateTime.ToString();
        string lsTmpRURL = Convert.ToString(Session["referringURL"]);

        if (lsSubject == String.Empty)
        {
            lblPrompt.Text = "Please enter a subject.";
            txtSubject.Focus();
            return;
        }
        else if (lsMessage == String.Empty)
        {
            lblPrompt.Text = "Please enter a message.";
            txtMessage.Focus();
            return;
        }
        else if (txtTo.Text == string.Empty)
        {
            lblPrompt.Text = "Please enter a recipient.";
            txtTo.Focus();
            return;
        }
        int liToUser = UserManagement.GetUserByUsername(txtTo.Text).ID;
        string lsFromUser = UserManagement.GetUserByID(liUserID).Username.ToString();

        PrivateMessage loPrivateMessage = new PrivateMessage();

        loPrivateMessage.PriMsg_title = lsSubject;
        loPrivateMessage.PriMsg_message = lsMessage;
        loPrivateMessage.Sent_time = Convert.ToDateTime(lsDateTime);
        loPrivateMessage.Create_time = Convert.ToDateTime(lsDateTime);
        loPrivateMessage.To_userID = liToUser;
        loPrivateMessage.To_username = txtTo.Text;
        loPrivateMessage.From_userID = liUserID;
        loPrivateMessage.From_username = lsFromUser;

        if (csMode == "reply")
        {
            PrivateMessageManagement.InsertPrivateMessage(loPrivateMessage, "reply");
        }
        else 
        {
            PrivateMessageManagement.InsertPrivateMessage(loPrivateMessage, "post");
        }

        Response.Redirect("Default.aspx?page=" + Server.UrlEncode(digioz.Encryption64.Encrypt("Private Messages", csURLEncryptionKey)) + "&msgview=I");
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx?page=" + Server.UrlEncode(digioz.Encryption64.Encrypt("Private Messages", csURLEncryptionKey)) + "&msgview=I");
    }
}
