using System;
using System.Web.UI.WebControls;
using digioz;
using digioz.PictureModule;
using System.Reflection;

public partial class controls_Profile : System.Web.UI.UserControl
{
    private PortalSettings coPortalSettings;
    private string csUserToDisplay = String.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        coPortalSettings = PortalSettingsManagement.GetPortalSettings();
        GetUserToDisplay();
        
        if (!IsPostBack)
        {


            if (Request.QueryString["tab"] != null)
            {
                MultiView1.ActiveViewIndex = Int32.Parse(Request.QueryString["tab"]);
                LoadView();
                                                 
           }
            else
            {
                LoadTabProfile();
            }
            
            }
        }
       
   

    protected void Menu1_MenuItemClick(object sender, MenuEventArgs e)
    {
        MultiView1.ActiveViewIndex = Int32.Parse(e.Item.Value);
        LoadView();
    }

    protected void LoadView()
    {    

        if (MultiView1.ActiveViewIndex == 0)
        {
            LoadTabProfile();
        }
        else if (MultiView1.ActiveViewIndex == 1)
        {
            LoadTabFriends();
        }
        else if (MultiView1.ActiveViewIndex == 2)
        {
            LoadTabPhotos();
        }
        else if (MultiView1.ActiveViewIndex == 3)
        {
            LoadTabVideos();
        }
        else if (MultiView1.ActiveViewIndex == 4)
        {
            LoadTabGroups();
        }
        else if (MultiView1.ActiveViewIndex == 5)
        {
            LoadTabFavorites();
        }
        else if (MultiView1.ActiveViewIndex == 6)
        {
            LoadTabFriendsNeedApprove();
        }
    }

    private void LoadTabProfile()
    {
        if (Session["username"] == null || Convert.ToString(Session["username"]) != csUserToDisplay)
        {
            imageUpload.Visible = false;
        }

        //Avatar Upload 
        imageUpload.NavigateUrl = "../Default.aspx?page=" + Server.UrlEncode(digioz.Encryption64.Encrypt("Avatar Upload", coPortalSettings.URLEncryptionKey));
        string userID;

        userID = UserManagement.GetUserByUsername(csUserToDisplay).ID.ToString();

        UserProfile loUserProfile = null;
        string avatar = "pictures/avatar/NoAvatar.gif";

        if (Session["username"] != null && csUserToDisplay == Session["username"].ToString())
        {
            loUserProfile = UserProfileManagement.GetUserProfile(Convert.ToString(Session["username"]));
            if (loUserProfile.Avatar != String.Empty)
            {
                avatar = loUserProfile.Avatar;
            }
            lblUsername.Text = "<b>Username:</b> " + loUserProfile.Username;
            lblEmail.Text = "<b>Email:</b> ";
            lblBirthday.Text = "<b>Birthday:</b> ";
            lblAddress.Text = "<b>Address:</b> ";
            lblAddress2.Text = "<b>Address 2:</b> ";
            lblCity.Text = "<b>City:</b> ";
            lblState.Text = "<b>State:</b> ";
            lblZip.Text = "<b>Zip:</b> ";
            lblCountry.Text = "<b>Country:</b> ";
            lblSignature.Text = "<b>Signature:</b> ";
            lblPassword.Text = "<b>Password:</b> ";
            lblVerify.Text = "<b>Verify:</b> ";

            txtEmail.Visible = true;
            txtBirthday.Visible = true;
            txtAddress.Visible = true;
            txtAddress2.Visible = true;
            txtCity.Visible = true;
            txtState.Visible = true;
            txtZip.Visible = true;
            txtCountry.Visible = true;
            txtSignature.Visible = true;
            lblPassword.Visible = true;
            lblVerify.Visible = true;
            txtPassword.Visible = true;
            txtVerify.Visible = true;
            lblNote.Visible = true;
            btnSaveProfile.Visible = true;
            btnAddFriend.Visible = false;
            btnRemoveFriend.Visible = false;

            txtEmail.Text = loUserProfile.Email;
            if (loUserProfile.Birthday.Equals(DateTime.MinValue))
            {
                txtBirthday.Text = "";
            }
            else
            {
                txtBirthday.Text = loUserProfile.Birthday.ToString();
            } 
            txtAddress.Text = loUserProfile.Address;
            txtAddress2.Text = loUserProfile.Address2;
            txtCity.Text = loUserProfile.City;
            txtState.Text = loUserProfile.State;
            txtZip.Text = loUserProfile.Zip;
            txtCountry.Text = loUserProfile.Country;
            txtSignature.Text = loUserProfile.Signature.Replace("<br />", "\n");
        }
        else
        {
            loUserProfile = UserProfileManagement.GetUserProfile(csUserToDisplay);
            if (Menu1.Items[Menu1.Items.Count - 1].Text.Contains("Approved"))
            {
                Menu1.Items.RemoveAt(Menu1.Items.Count - 1);
            }

            lblPassword.Visible = false;
            lblVerify.Visible = false;
            lblNote.Visible = false;
            btnSaveProfile.Visible = false;
            lblUsername.Text = "<b>Username:</b> " + loUserProfile.Username;
            lblEmail.Text = "<b>Email:</b> " + loUserProfile.Email;
            
            if (Session["username"] == null)
            {
                btnAddFriend.Visible = false;
                btnRemoveFriend.Visible = false;
            }
            else
            {
                // Info to show for logged on visitors only
                bool inFriendList = FriendsManagement.IsInFriendList(UserManagement.GetUserByUsername(Session["username"].ToString()).ID.ToString(), UserManagement.GetUserByUsername(csUserToDisplay).ID.ToString());
                btnAddFriend.Visible = !inFriendList;
                btnRemoveFriend.Visible = inFriendList;

                if (loUserProfile.Birthday.Equals(DateTime.MinValue))
                {
                    lblBirthday.Text = "<b>Birthday:</b> " + "";
                }
                else
                {
                    lblBirthday.Text = "<b>Birthday:</b> " + loUserProfile.Birthday.ToString();
                } 
                lblAddress.Text = "<b>Address:</b> " + loUserProfile.Address;
                lblAddress2.Text = "<b>Address 2:</b> " + loUserProfile.Address2;

                lblCity.Text = "<b>City:</b> " + loUserProfile.City;
                lblState.Text = "<b>State:</b> " + loUserProfile.State;
                lblZip.Text = "<b>Zip:</b> " + loUserProfile.Zip;
                lblCountry.Text = "<b>Country:</b> " + loUserProfile.Country;
            }

            lblSignature.Text = "<b>Signature:</b> " + loUserProfile.Signature;
            if (loUserProfile.Avatar != String.Empty)
            {
                avatar = loUserProfile.Avatar;
            }
        }

        if (loUserProfile.Avatar != String.Empty)
        {
            Image1.ImageUrl = "~/" + avatar;
        }
    }
    private void LoadTabFriends()
    {
        gvFriendList.DataSource = FriendsManagement.GetFiendsInfoUser(UserManagement.GetUserByUsername(csUserToDisplay).ID.ToString());
        gvFriendList.DataBind();

        if (gvFriendList.Rows.Count == 0)
        {
            lblPrompt.Text = "No Friends!<br>";
            lblPrompt.Visible = true;
        }
        else
        {
            lblPrompt.Visible = false;
        }
    }
    private void LoadTabFriendsNeedApprove()
    {
        gvNeedApproveFriends.DataSource = FriendsManagement.GetNeedApprovedFriends(UserManagement.GetUserByUsername(csUserToDisplay).ID.ToString());
        gvNeedApproveFriends.DataBind();
        if (gvNeedApproveFriends.Rows.Count == 0)
        {
            lblNeedApproveFriendsPrompt.Text = "No Need Approved Friends!<br>";
            lblNeedApproveFriendsPrompt.Visible = true;
        }
        else
        {
            lblNeedApproveFriendsPrompt.Visible = false;
        }
    }
    private void LoadTabGroups()
    {
    }
    private void LoadTabFavorites()
    {
        int currentpage = 1;
        int currentpage2 = 1;

        if (!(string.IsNullOrEmpty(LabelPageNum1PicFav.Text)))
        {
            currentpage = Convert.ToInt32(LabelPageNum1PicFav.Text);
        }
        else
        {
            LabelPageNum1PicFav.Text = "1";
            LabelPageNum2PicFav.Text = "1";
        }

        PagedDataSource ps = new PagedDataSource();
        ps.DataSource = PictureInfoManagement.GetFavoritePictureInfoUserList(UserManagement.GetUserByUsername(csUserToDisplay).ID.ToString());

        ps.AllowPaging = true;
        ps.PageSize = 6;
        ps.CurrentPageIndex = currentpage - 1;
        ButtonFirstPage1PicFav.Enabled = true;
        ButtonPrePage1PicFav.Enabled = true;
        ButtonNextPage1PicFav.Enabled = true;
        ButtonLastPage1PicFav.Enabled = true;
        ButtonFirstPage2PicFav.Enabled = true;
        ButtonPrePage2PicFav.Enabled = true;
        ButtonNextPage2PicFav.Enabled = true;
        ButtonLastPage2PicFav.Enabled = true;

        if (currentpage == 1)
        {
            ButtonFirstPage1PicFav.Enabled = false;
            ButtonPrePage1PicFav.Enabled = false;
            ButtonFirstPage2PicFav.Enabled = false;
            ButtonPrePage2PicFav.Enabled = false;
        }

        if (currentpage == ps.PageCount)
        {
            ButtonNextPage1PicFav.Enabled = false;
            ButtonNextPage2PicFav.Enabled = false;
            ButtonLastPage1PicFav.Enabled = false;
            ButtonLastPage2PicFav.Enabled = false;
        }

        LabelPageAmount2PicFav.Text = Convert.ToString(ps.PageCount);
        LabelPageAmount1PicFav.Text = Convert.ToString(ps.PageCount);
        DataListFavoritePics.DataSource = ps;
        DataListFavoritePics.DataBind();

        if (!(string.IsNullOrEmpty(LabelPageNum1VideoFav.Text)))
        {
            currentpage2 = Convert.ToInt32(LabelPageNum1VideoFav.Text);
        }
        else
        {
            LabelPageNum1VideoFav.Text = "1";
            LabelPageNum2VideoFav.Text = "1";
        }

        PagedDataSource ps2 = new PagedDataSource();
        ps2.DataSource = digioz.VideoModule.VideoInfoManagement.GetFavoriteVideoInfoUserList(UserManagement.GetUserByUsername(csUserToDisplay).ID.ToString());
        ps2.AllowPaging = true;
        ps2.PageSize = 6;
        ps2.CurrentPageIndex = currentpage - 1;
        ButtonFirstPage1VideoFav.Enabled = true;
        ButtonPrePage1VideoFav.Enabled = true;
        ButtonNextPage1VideoFav.Enabled = true;
        ButtonLastPage1VideoFav.Enabled = true;
        ButtonFirstPage2VideoFav.Enabled = true;
        ButtonPrePage2VideoFav.Enabled = true;
        ButtonNextPage2VideoFav.Enabled = true;
        ButtonLastPage2VideoFav.Enabled = true;

        if (currentpage2 == 1)
        {
            ButtonFirstPage1VideoFav.Enabled = false;
            ButtonPrePage1VideoFav.Enabled = false;
            ButtonFirstPage2VideoFav.Enabled = false;
            ButtonPrePage2VideoFav.Enabled = false;
        }

        if (currentpage2 == ps2.PageCount)
        {
            ButtonNextPage1VideoFav.Enabled = false;
            ButtonNextPage2VideoFav.Enabled = false;
            ButtonLastPage1VideoFav.Enabled = false;
            ButtonLastPage2VideoFav.Enabled = false;
        }

        LabelPageAmount2VideoFav.Text = Convert.ToString(ps2.PageCount);
        LabelPageAmount1VideoFav.Text = Convert.ToString(ps2.PageCount);
        DataListFavoriteVideos.DataSource = ps2;
        DataListFavoriteVideos.DataBind();

    }
    private void LoadTabPhotos()
    {
        int currentpage = 1;

        if (!(string.IsNullOrEmpty(LabelPageNum1.Text)))
        {
            currentpage = Convert.ToInt32(LabelPageNum1.Text);
        }
        else
        {
            LabelPageNum1.Text = "1";
            LabelPageNum2.Text = "1";
        }

        PagedDataSource ps = new PagedDataSource();
        ps.DataSource = PictureInfoManagement.GetPictureInfoUserList(UserManagement.GetUserByUsername(csUserToDisplay).ID.ToString());
        ps.AllowPaging = true;
        ps.PageSize = 9;
        ps.CurrentPageIndex = currentpage - 1;
        ButtonFirstPage1.Enabled = true;
        ButtonPrePage1.Enabled = true;
        ButtonNextPage1.Enabled = true;
        ButtonLastPage1.Enabled = true;
        ButtonFirstPage2.Enabled = true;
        ButtonPrePage2.Enabled = true;
        ButtonNextPage2.Enabled = true;
        ButtonLastPage2.Enabled = true;

        if (currentpage == 1)
        {
            ButtonFirstPage1.Enabled = false;
            ButtonPrePage1.Enabled = false;
            ButtonFirstPage2.Enabled = false;
            ButtonPrePage2.Enabled = false;
        }

        if (currentpage == ps.PageCount)
        {
            ButtonNextPage1.Enabled = false;
            ButtonNextPage2.Enabled = false;
            ButtonLastPage1.Enabled = false;
            ButtonLastPage2.Enabled = false;
        }

        LabelPageAmount2.Text = Convert.ToString(ps.PageCount);
        LabelPageAmount1.Text = Convert.ToString(ps.PageCount);
        DataListPhotos.DataSource = ps;
        DataListPhotos.DataBind();
    }
    private void LoadTabVideos()
    {
        int currentpage = 1;

        if (!(string.IsNullOrEmpty(LabelPageNum1Video.Text)))
        {
            currentpage = Convert.ToInt32(LabelPageNum1Video.Text);
        }
        else
        {
            LabelPageNum1Video.Text = "1";
            LabelPageNum2Video.Text = "1";
        }

        PagedDataSource ps = new PagedDataSource();
        ps.DataSource = digioz.VideoModule.VideoInfoManagement.GetVideoInfoUserList(UserManagement.GetUserByUsername(csUserToDisplay).ID.ToString());
        ps.AllowPaging = true;
        ps.PageSize = 9;
        ps.CurrentPageIndex = currentpage - 1;
        ButtonFirstPage1Video.Enabled = true;
        ButtonPrePage1Video.Enabled = true;
        ButtonNextPage1Video.Enabled = true;
        ButtonLastPage1Video.Enabled = true;
        ButtonFirstPage2Video.Enabled = true;
        ButtonPrePage2Video.Enabled = true;
        ButtonNextPage2Video.Enabled = true;
        ButtonLastPage2Video.Enabled = true;

        if (currentpage == 1)
        {
            ButtonFirstPage1Video.Enabled = false;
            ButtonPrePage1Video.Enabled = false;
            ButtonFirstPage2Video.Enabled = false;
            ButtonPrePage2Video.Enabled = false;
        }

        if (currentpage == ps.PageCount)
        {
            ButtonNextPage1Video.Enabled = false;
            ButtonNextPage2Video.Enabled = false;
            ButtonLastPage1Video.Enabled = false;
            ButtonLastPage2Video.Enabled = false;
        }

        LabelPageAmount2Video.Text = Convert.ToString(ps.PageCount);
        LabelPageAmount1Video.Text = Convert.ToString(ps.PageCount);
        DataListVideos.DataSource = ps;
        DataListVideos.DataBind();
    }

    private void GetUserToDisplay()
    {
        csUserToDisplay = Request.QueryString["username"];

        try
        {
            csUserToDisplay = digioz.Encryption64.Decrypt(csUserToDisplay, coPortalSettings.URLEncryptionKey);
        }
        catch
        {
            csUserToDisplay = String.Empty;
        }

        if (csUserToDisplay == null || csUserToDisplay == String.Empty)
        {
            csUserToDisplay = Convert.ToString(Session["username"]);
        }
    }
   
    protected void ButtonFirstPage1PicFav_Click(object sender, EventArgs e)
    {
        LabelPageNum1PicFav.Text = "1";
        LabelPageNum2PicFav.Text = "1";
        LoadTabFavorites();
    }
    protected void ButtonFirstPage2PicFav_Click(object sender, EventArgs e)
    {
        LabelPageNum1PicFav.Text = "1";
        LabelPageNum2PicFav.Text = "1";
        LoadTabFavorites();
    }
    protected void ButtonPrePage1PicFav_Click(object sender, EventArgs e)
    {
        LabelPageNum1PicFav.Text = Convert.ToString(Convert.ToInt32(LabelPageNum1PicFav.Text) - 1);
        LabelPageNum2PicFav.Text = LabelPageNum1PicFav.Text;
        LoadTabFavorites();
    }
    protected void ButtonPrePage2PicFav_Click(object sender, EventArgs e)
    {
        LabelPageNum1PicFav.Text = Convert.ToString(Convert.ToInt32(LabelPageNum1PicFav.Text) - 1);
        LabelPageNum2PicFav.Text = LabelPageNum1PicFav.Text;
        LoadTabFavorites();
    }
    protected void ButtonNextPage1PicFav_Click(object sender, EventArgs e)
    {
        LabelPageNum1PicFav.Text = Convert.ToString(Convert.ToInt32(LabelPageNum1PicFav.Text) + 1);
        LabelPageNum2PicFav.Text = LabelPageNum1PicFav.Text;
        LoadTabFavorites();
    }
    protected void ButtonNextPage2PicFav_Click(object sender, EventArgs e)
    {
        LabelPageNum1PicFav.Text = Convert.ToString(Convert.ToInt32(LabelPageNum1PicFav.Text) + 1);
        LabelPageNum2PicFav.Text = LabelPageNum1PicFav.Text;
        LoadTabFavorites();
    }
    protected void ButtonLastPage1PicFav_Click(object sender, EventArgs e)
    {
        LabelPageNum1PicFav.Text = LabelPageAmount1PicFav.Text;
        LabelPageNum2PicFav.Text = LabelPageAmount1PicFav.Text;
        LoadTabFavorites();
    }
    protected void ButtonLastPage2PicFav_Click(object sender, EventArgs e)
    {
        LabelPageNum1PicFav.Text = LabelPageAmount1PicFav.Text;
        LabelPageNum2PicFav.Text = LabelPageAmount1PicFav.Text;
        LoadTabFavorites();
    }

    protected void ButtonFirstPage1VideoFav_Click(object sender, EventArgs e)
    {
        LabelPageNum1VideoFav.Text = "1";
        LabelPageNum2VideoFav.Text = "1";
        LoadTabFavorites();
    }
    protected void ButtonFirstPage2VideoFav_Click(object sender, EventArgs e)
    {
        LabelPageNum1VideoFav.Text = "1";
        LabelPageNum2VideoFav.Text = "1";
        LoadTabFavorites();
    }
    protected void ButtonPrePage1VideoFav_Click(object sender, EventArgs e)
    {
        LabelPageNum1VideoFav.Text = Convert.ToString(Convert.ToInt32(LabelPageNum1VideoFav.Text) - 1);
        LabelPageNum2VideoFav.Text = LabelPageNum1VideoFav.Text;
        LoadTabFavorites();
    }
    protected void ButtonPrePage2VideoFav_Click(object sender, EventArgs e)
    {
        LabelPageNum1VideoFav.Text = Convert.ToString(Convert.ToInt32(LabelPageNum1VideoFav.Text) - 1);
        LabelPageNum2VideoFav.Text = LabelPageNum1VideoFav.Text;
        LoadTabFavorites();
    }
    protected void ButtonNextPage1VideoFav_Click(object sender, EventArgs e)
    {
        LabelPageNum1VideoFav.Text = Convert.ToString(Convert.ToInt32(LabelPageNum1VideoFav.Text) + 1);
        LabelPageNum2VideoFav.Text = LabelPageNum1VideoFav.Text;
        LoadTabFavorites();
    }
    protected void ButtonNextPage2VideoFav_Click(object sender, EventArgs e)
    {
        LabelPageNum1VideoFav.Text = Convert.ToString(Convert.ToInt32(LabelPageNum1VideoFav.Text) + 1);
        LabelPageNum2VideoFav.Text = LabelPageNum1VideoFav.Text;
        LoadTabFavorites();
    }
    protected void ButtonLastPage1VideoFav_Click(object sender, EventArgs e)
    {
        LabelPageNum1VideoFav.Text = LabelPageAmount1VideoFav.Text;
        LabelPageNum2VideoFav.Text = LabelPageAmount1VideoFav.Text;
        LoadTabFavorites();
    }
    protected void ButtonLastPage2VideoFav_Click(object sender, EventArgs e)
    {
        LabelPageNum1VideoFav.Text = LabelPageAmount1VideoFav.Text;
        LabelPageNum2VideoFav.Text = LabelPageAmount1VideoFav.Text;
        LoadTabFavorites();
    }

    protected void btnSaveProfile_Click(object sender, EventArgs e)
    {
        digioz.UserProfile loUserProfile = new digioz.UserProfile();
        DateTime ldBirthday = new DateTime();

        if (txtPassword.Text != String.Empty || txtVerify.Text != String.Empty)
        {
            if (txtPassword.Text != txtVerify.Text)
            {
                lblNote.ForeColor = System.Drawing.Color.Red;
                lblNote.Text = "Password and Verify do not match!";
                return;
            }
        }

        try
        {
            ldBirthday = Convert.ToDateTime(txtBirthday.Text);
        }
        catch
        { }

        //Bug fix 2969918
        if (txtEmail.Text == string.Empty)
        {
            lblNote.ForeColor = System.Drawing.Color.Red;
            lblNote.Text = "Email can not be blank.";
            txtEmail.Focus();
            return;
        }

        loUserProfile.Username = Session["username"].ToString();
        loUserProfile.Email = txtEmail.Text;
        loUserProfile.Birthday = ldBirthday;
        loUserProfile.Address = txtAddress.Text;
        loUserProfile.Address2 = txtAddress2.Text;
        loUserProfile.City = txtCity.Text;
        loUserProfile.State = txtState.Text;
        loUserProfile.Zip = txtZip.Text;
        loUserProfile.Country = txtCountry.Text;
        loUserProfile.Signature = txtSignature.Text;

        UserProfileManagement.SaveUserProfile(loUserProfile, "Update");

        if (txtPassword.Text.Length > 0)
        {
            loUserProfile.Password = txtPassword.Text;
            UserManagement.SaveUserPassword(loUserProfile.Password, Session["userid"].ToString());
        }

        lblNote.ForeColor = System.Drawing.Color.Green;
        lblNote.Text = "Profile Changes Saved!";

        LoadTabProfile();
    }

    // Photos DataList section ---------------------------------------------

    protected void ButtonFirstPage1_Click(object sender, EventArgs e)
    {
        LabelPageNum1.Text = "1";
        LabelPageNum2.Text = "1";
        LoadTabPhotos();
    }
    protected void ButtonFirstPage2_Click(object sender, EventArgs e)
    {
        LabelPageNum1.Text = "1";
        LabelPageNum2.Text = "1";
        LoadTabPhotos();
    }
    protected void ButtonPrePage1_Click(object sender, EventArgs e)
    {
        LabelPageNum1.Text = Convert.ToString(Convert.ToInt32(LabelPageNum1.Text) - 1);
        LabelPageNum2.Text = LabelPageNum1.Text;
        LoadTabPhotos();
    }
    protected void ButtonPrePage2_Click(object sender, EventArgs e)
    {
        LabelPageNum1.Text = Convert.ToString(Convert.ToInt32(LabelPageNum1.Text) - 1);
        LabelPageNum2.Text = LabelPageNum1.Text;
        LoadTabPhotos();
    }
    protected void ButtonNextPage1_Click(object sender, EventArgs e)
    {
        LabelPageNum1.Text = Convert.ToString(Convert.ToInt32(LabelPageNum1.Text) + 1);
        LabelPageNum2.Text = LabelPageNum1.Text;
        LoadTabPhotos();
    }
    protected void ButtonNextPage2_Click(object sender, EventArgs e)
    {
        LabelPageNum1.Text = Convert.ToString(Convert.ToInt32(LabelPageNum1.Text) + 1);
        LabelPageNum2.Text = LabelPageNum1.Text;
        LoadTabPhotos();
    }
    protected void ButtonLastPage1_Click(object sender, EventArgs e)
    {
        LabelPageNum1.Text = LabelPageAmount1.Text;
        LabelPageNum2.Text = LabelPageAmount1.Text;
        LoadTabPhotos();
    }
    protected void ButtonLastPage2_Click(object sender, EventArgs e)
    {
        LabelPageNum1.Text = LabelPageAmount1.Text;
        LabelPageNum2.Text = LabelPageAmount1.Text;
        LoadTabPhotos();
    }

    // Videos DataList section ---------------------------------------------

    protected void ButtonFirstPage1Video_Click(object sender, EventArgs e)
    {
        LabelPageNum1Video.Text = "1";
        LabelPageNum2Video.Text = "1";
        LoadTabVideos();
    }
    protected void ButtonFirstPage2Video_Click(object sender, EventArgs e)
    {
        LabelPageNum1Video.Text = "1";
        LabelPageNum2Video.Text = "1";
        LoadTabVideos();
    }
    protected void ButtonPrePage1Video_Click(object sender, EventArgs e)
    {
        LabelPageNum1Video.Text = Convert.ToString(Convert.ToInt32(LabelPageNum1Video.Text) - 1);
        LabelPageNum2Video.Text = LabelPageNum1Video.Text;
        LoadTabVideos();
    }
    protected void ButtonPrePage2Video_Click(object sender, EventArgs e)
    {
        LabelPageNum1Video.Text = Convert.ToString(Convert.ToInt32(LabelPageNum1Video.Text) - 1);
        LabelPageNum2Video.Text = LabelPageNum1Video.Text;
        LoadTabVideos();
    }
    protected void ButtonNextPage1Video_Click(object sender, EventArgs e)
    {
        LabelPageNum1Video.Text = Convert.ToString(Convert.ToInt32(LabelPageNum1Video.Text) + 1);
        LabelPageNum2Video.Text = LabelPageNum1Video.Text;
        LoadTabVideos();
    }
    protected void ButtonNextPage2Video_Click(object sender, EventArgs e)
    {
        LabelPageNum1Video.Text = Convert.ToString(Convert.ToInt32(LabelPageNum1Video.Text) + 1);
        LabelPageNum2Video.Text = LabelPageNum1Video.Text;
        LoadTabVideos();
    }
    protected void ButtonLastPage1Video_Click(object sender, EventArgs e)
    {
        LabelPageNum1Video.Text = LabelPageAmount1Video.Text;
        LabelPageNum2Video.Text = LabelPageAmount1Video.Text;
        LoadTabVideos();
    }
    protected void ButtonLastPage2Video_Click(object sender, EventArgs e)
    {
        LabelPageNum1Video.Text = LabelPageAmount1Video.Text;
        LabelPageNum2Video.Text = LabelPageAmount1Video.Text;
        LoadTabVideos();
    }

    protected void gvFriendList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.gvFriendList.PageIndex = e.NewPageIndex;
        this.gvFriendList.DataBind();
    }
    protected void gvFriendNeedApproveList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.gvNeedApproveFriends.PageIndex = e.NewPageIndex;
        this.gvNeedApproveFriends.DataBind();
    }
    protected void gvNeedApproveFriends_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        LoadTabProfile();
        if (Session["username"] != null)
            if (e.CommandName == "Approve")
            {
                GridViewRow row = gvNeedApproveFriends.Rows[Convert.ToInt32(e.CommandArgument)];
                if (row != null)
                {
                    string userID = UserManagement.GetUserByID(Convert.ToInt32(row.Cells[0].Text)).ID.ToString();
                    string myUserID = UserManagement.GetUserByUsername(Session["username"].ToString()).ID.ToString();
                    if (!string.IsNullOrEmpty(userID))
                    {
                        FriendsManagement.RemoveFriendInfo(userID, myUserID);
                        FriendsManagement.AddFriendInfo(userID, myUserID, true);
                        FriendsManagement.AddFriendInfo(myUserID, userID, true);

                        int oldPageIndex = gvNeedApproveFriends.PageIndex;
                        LoadTabFriendsNeedApprove();
                        gvNeedApproveFriends.PageIndex = oldPageIndex;
                        gvNeedApproveFriends.DataBind();
                    }
                }
            }
    }

    public string ShowProfileLink(string psUsername, string psImageName, bool psImageInsert)
    {
        string lsProfileLink;

        if (psImageInsert == false)
        {
            lsProfileLink = "<a href=\"?page=" + Server.UrlEncode(digioz.Encryption64.Encrypt("Profile", coPortalSettings.URLEncryptionKey))
                                             + "&username=" + Server.UrlEncode(digioz.Encryption64.Encrypt(psUsername, coPortalSettings.URLEncryptionKey))
                                             + "\">" + psUsername + "</a>";
        }
        else
        {
            lsProfileLink = "<a href=\"?page=" + Server.UrlEncode(digioz.Encryption64.Encrypt("Profile", coPortalSettings.URLEncryptionKey))
                                             + "&username=" + Server.UrlEncode(digioz.Encryption64.Encrypt(psUsername, coPortalSettings.URLEncryptionKey))
                                             + "\"><img src=\"" + psImageName + "\" alt=\"\" /></a>";
        }

        return lsProfileLink;
    }

    protected void btnAddFriend_Click(object sender, EventArgs e)
    {
        if (Session["username"] != null)
        {
            LoadTabProfile();
            FriendsManagement.AddFriendInfo(UserManagement.GetUserByUsername(Session["username"].ToString()).ID.ToString(), UserManagement.GetUserByUsername(csUserToDisplay).ID.ToString(), false);
            btnAddFriend.Visible = false;
            btnRemoveFriend.Visible = true;
        }
    }
    protected void btnRemoveFriend_Click(object sender, EventArgs e)
    {
        if (Session["username"] != null)
        {
            FriendsManagement.RemoveFriendInfo(UserManagement.GetUserByUsername(Session["username"].ToString()).ID.ToString(), UserManagement.GetUserByUsername(csUserToDisplay).ID.ToString());
            FriendsManagement.RemoveFriendInfo(UserManagement.GetUserByUsername(csUserToDisplay).ID.ToString(), UserManagement.GetUserByUsername(Session["username"].ToString()).ID.ToString());
            btnAddFriend.Visible = true;
            btnRemoveFriend.Visible = false;
        }
    }
}
