protected void Page_Load(object sender, EventArgs e)
    {
        // Fetch current page url and extract a_id from it.
        Uri    myUri = new Uri(HttpContext.Current.Request.Url.AbsoluteUri);
        string c_id  = HttpUtility.ParseQueryString(myUri.Query).Get("c_id");
        string a_id  = HttpUtility.ParseQueryString(myUri.Query).Get("a_id");

        DBConnectionSQLServer dbConnectionSQLServer = ((SiteMaster)this.Master).dbConnectionSQLServer;

        string sql = "DELETE FROM ACTIVITY_COMMENT WHERE A_COM_ID = @c_id;";

        SqlCommand command2;

        //SqlDataReader dataReader2;

        try
        {
            command2 = new SqlCommand(sql, dbConnectionSQLServer.cn);
            //dataReader2 = command2.ExecuteReader();

            command2.Parameters.AddWithValue("@c_id", c_id);

            command2.ExecuteNonQuery();

            //dataReader2.Close();
            command2.Dispose();
            //dbConnectionSQLServer.cn.Close();
        }
        catch (Exception ex)
        {
            HttpContext.Current.Response.Write(ex.Message + " in Delete_Comment().</br>");
        }

        //HttpContext.Current.Response.Redirect("Activity_Locations.aspx?a_id=" + a_id);
    }
    protected ArrayList getCategories()
    {
        DBConnectionSQLServer dbConnectionSQLServer = ((SiteMaster)this.Master).dbConnectionSQLServer;

        string    subCatName     = "Dummy subcategory name";
        ArrayList categoriesList = new ArrayList();


        string sql = "SELECT SUB_CAT_NAME FROM SUB_CAT;";

        SqlCommand    command;
        SqlDataReader dataReader;

        try
        {
            if (dbConnectionSQLServer != null && dbConnectionSQLServer.cn.State == ConnectionState.Closed)
            {
                dbConnectionSQLServer.cn.Open();
            }


            command    = new SqlCommand(sql, dbConnectionSQLServer.cn);
            dataReader = command.ExecuteReader();

            while (dataReader.Read())
            {
                subCatName = (String)dataReader.GetValue(0);
                categoriesList.Add(FirstCharToUpper(subCatName.ToLower()));
            }

            dataReader.Close();
            command.Dispose();
            dbConnectionSQLServer.cn.Close();
        }

        catch (Exception ex)
        {
            Response.Write(ex.Message + " in getCategories().</br>");
        }

        char[] charsToTrim = { '-', ' ' };

        return(categoriesList);
    }
    protected void LoadActivities()
    {
        DBConnectionSQLServer dbConnectionSQLServer = ((SiteMaster)this.Master).dbConnectionSQLServer;

        // Flag variable to check how many rows have been used in the loop.
        int i = 0;

        // Flag variable to check if the connected user has preferences that match the activities or not.
        // 0 for found matches, 1 for no matches found.
        int connectedButNoPreferenceMatch = 0;

        // Variables used in the html building.
        string    absolutePath         = Server.MapPath("/images/").Replace("\\", "/");
        string    activityName         = "Dummy activity name";
        string    activityLocationName = "Dummy activity's location name";
        string    activityDescription  = "Dummy activity description";
        string    sub_cat_name         = "Dummy sub-category name";
        ArrayList categoriesList       = new ArrayList();

        int a_id = -1;

        // String variables to store the SQL queries.
        string sql, sql1, sql2;

        sql1 = "SELECT A_NAME, CI.CI_NAME, A_DESCRIPTION, A_ID, SC.SUB_CAT_NAME FROM ACTIVITY AS A JOIN LOCALITY AS L ON A.A_LOC_ID = L.LOC_ID JOIN CITY AS CI ON L.LOC_CI_ID = CI.CI_ID JOIN SUB_CAT AS SC ON A.A_SUB_CAT_ID = SC.SUB_CAT_ID; ";

        sql            = sql1;
        categoriesList = getCategories();

/*
 *      // If no one is connected.
 *      if (Session["id"] == null)
 *      {
 *          sql = sql1;
 *      }
 *      else // If someone is connected.
 *      {
 *          sql2 = "SELECT A_NAME, CI.CI_NAME, A_DESCRIPTION, A_ID FROM ACTIVITY AS A JOIN LOCALITY AS L ON A.A_LOC_ID = L.LOC_ID JOIN CITY AS CI ON L.LOC_CI_ID = CI.CI_ID JOIN PREFERENCE AS P ON A.A_SUB_CAT_ID = P.PREF_SUB_CAT_ID JOIN PERSON AS PE ON P.PREF_P_ID = PE.P_ID WHERE P.PREF_P_ID =" + Session["id"] + "; ";
 *
 *          sql = sql2;
 *      }
 *
 */

        try
        {
            if (dbConnectionSQLServer != null && dbConnectionSQLServer.cn.State == ConnectionState.Closed)
            {
                dbConnectionSQLServer.cn.Open();
            }

            SqlCommand    command;
            SqlDataReader dataReader;

            command    = new SqlCommand(sql, dbConnectionSQLServer.cn);
            dataReader = command.ExecuteReader();

            HtmlGenericControl ul = new HtmlGenericControl("ul");


            // Check if the connected user has preferences that match events.
            // If he doesn't, make the SQL query to fetch all activities.

            if (Session["id"] != null && !dataReader.HasRows)
            {
                dataReader.Close();
                command.Dispose();

                sql = sql1;

                command    = new SqlCommand(sql, dbConnectionSQLServer.cn);
                dataReader = command.ExecuteReader();

                // Make this variable 1 to say that there are no preferences that match the activities.
                connectedButNoPreferenceMatch = 1;
            }

            for (int k = 0; k < categoriesList.Count; k++)
            {
                HtmlGenericControl liCategory = new HtmlGenericControl("li");

                HtmlGenericControl aCategory = new HtmlGenericControl("a");
                aCategory.InnerText = (string)categoriesList.ToArray()[k].ToString();
                aCategory.Attributes.Add("class", "btn btn-default");
                aCategory.Attributes.Add("data-filter", "." + (string)categoriesList.ToArray()[k].ToString().ToLower().Replace(" ", String.Empty));

                liCategory.Controls.Add(aCategory);
                portfolioFilter.Controls.Add(liCategory);
            }

            while (dataReader.Read())
            {
                activityName         = (string)dataReader.GetValue(0);
                activityLocationName = (string)dataReader.GetValue(1);
                activityDescription  = truncate((string)dataReader.GetValue(2), 65);
                a_id         = (int)dataReader.GetValue(3);
                sub_cat_name = (string)dataReader.GetValue(4);

                string imgSrc;

                if (File.Exists(absolutePath + "activities/" + "event_" + a_id + ".jpg"))
                {
                    imgSrc = "images/activities/" + "event_" + a_id + ".jpg";
                }
                else
                {
                    imgSrc = "images/activities/defaultEvent.png";
                }

                HtmlGenericControl portfolioItem = new HtmlGenericControl("div");
                portfolioItem.Attributes.Add("class", "col-xs-12 col-sm-6 col-md-3 portfolio-item " + sub_cat_name.ToLower().Replace(" ", String.Empty) + "");
                portfolioItem.InnerHtml = "<div class=\"portfolio wrapper\">" +
                                          "<div class=\"portfolio-single\">" +
                                          "<div class=\"portfolio-thumb\">" +
                                          "<img src=\"" + imgSrc + "\" class=\"img-responsive\" alt=\"" + activityLocationName.ToLower() + "\">" +
                                          "</div>" +
                                          "<div class=\"portfolio-view\">" +
                                          "<ul class=\"nav nav-pills\">" +
                                          "<li><a href=\"Activity.aspx?a_id=" + a_id + "\"><i class=\"fa fa-link\"></i></a></li>" +
                                          "<li><a href=\"" + imgSrc + "\" data-lightbox=\"example-set\"><i class=\"fa fa-eye\"></i></a></li>" +
                                          "</ul>" +
                                          "</div>" +
                                          "</div>" +
                                          "<div class=\"portfolio-info \">" +
                                          "<h2>" + activityName + "</h2>" +
                                          "<p> " + activityDescription + "</p>" +
                                          "</div>" +
                                          "</div>";

                portfolioItems.Controls.Add(portfolioItem);

/*
 *                  // For every 3 activities found, make a new <ul>.
 *                  if ((i % 3) == 0)
 *                  {
 *                      ul = new HtmlGenericControl("ul");
 *                      ul.Attributes.Add("class", "cityContainer list-unstyled row");
 *                      ulContainer.Controls.Add(ul);
 *
 *                  }
 *
 *                  ul.Controls.Add(li);
 */

// Increase row count.
                i++;
            }

            dataReader.Close();
            command.Dispose();
            dbConnectionSQLServer.cn.Close();
        }

        catch (Exception ex)
        {
            Response.Write(ex.Message + " in LoadActivities().</br>");
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        // Fetch current page url and extract a_id from it.
        Uri    myUri            = new Uri(HttpContext.Current.Request.Url.AbsoluteUri);
        string a_id             = HttpUtility.ParseQueryString(myUri.Query).Get("a_id");
        string commentToAdd     = HttpUtility.ParseQueryString(myUri.Query).Get("commentToAdd");
        string replyToCommentID = HttpUtility.ParseQueryString(myUri.Query).Get("replyToCommentID");

        DBConnectionSQLServer dbConnectionSQLServer = ((SiteMaster)this.Master).dbConnectionSQLServer;

        //DBConnectionSQLServer dbConnectionSQLServer = new DBConnectionSQLServer();

        string commentText = "Dummy commentText";
        string username    = "******";

        DateTime commentPubDate;

        int c_id    = -1;
        int u_id    = -1;
        int sqlA_ID = -1;

        string absolutePath = Server.MapPath("/images/").Replace("\\", "/");
        string sql;

        if (Session["id"] != null)
        {
            u_id = (int)Session["id"];

            sql = "SELECT P_USERNAME FROM PERSON WHERE P_ID = " + Session["id"] + ";";

            SqlCommand    command;
            SqlDataReader dataReader;

            try
            {
                command    = new SqlCommand(sql, dbConnectionSQLServer.cn);
                dataReader = command.ExecuteReader();

                while (dataReader.Read())
                {
                    username = (string)dataReader.GetValue(0);
                }

                dataReader.Close();
                command.Dispose();
                dbConnectionSQLServer.cn.Close();
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message + " in submitCommentButton_Click().</br>");
            }


            sql = "INSERT INTO ACTIVITY_COMMENT VALUES (@u_id, @a_id, @commentToAdd, SYSDATETIME(), @replyToCommentID);";

            SqlCommand command2;
            //SqlDataReader dataReader2;

            try
            {
                if (dbConnectionSQLServer != null && dbConnectionSQLServer.cn.State == ConnectionState.Closed)
                {
                    dbConnectionSQLServer.cn.Open();
                }


                command2 = new SqlCommand(sql, dbConnectionSQLServer.cn);
                //dataReader2 = command2.ExecuteReader();

                command2.Parameters.AddWithValue("@u_id", u_id);
                command2.Parameters.AddWithValue("@a_id", a_id);
                command2.Parameters.AddWithValue("@commentToAdd", commentToAdd);

                if (replyToCommentID == "NULL")
                {
                    command2.Parameters.AddWithValue("@replyToCommentID", DBNull.Value);
                }
                else
                {
                    command2.Parameters.AddWithValue("@replyToCommentID", replyToCommentID);
                }

                command2.ExecuteNonQuery();

                //dataReader2.Close();
                command2.Dispose();
                dbConnectionSQLServer.cn.Close();
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message + " in submitCommentButton_Click() in insertion part.</br>");
            }
        }
        else
        {
            Response.Write("You can't submit a comment since you're not loged-in.<br />");
        }

        //Response.Redirect("Activity_Locations.aspx?a_id=" + a_id);
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        // Fetch current page url and extract a_id from it.
        Uri    myUri    = new Uri(HttpContext.Current.Request.Url.AbsoluteUri);
        string a_id     = HttpUtility.ParseQueryString(myUri.Query).Get("a_id");
        string username = HttpUtility.ParseQueryString(myUri.Query).Get("username");

        string absolutePath = Server.MapPath("/images/").Replace("\\", "/");
        string relativePath = "images/";
        string currentPage  = "";
        string errorMessage = "No error here.";

        // Flag variable to check if all is good in the sql part.
        bool allGood = true;

        DBConnectionSQLServer dbConnectionSQLServer = ((SiteWOform)this.Master).dbConnectionSQLServer;

        string sql;
        int    sqlU_ID = -1;
        int    sqlA_ID = -1;

        if (dbConnectionSQLServer != null && dbConnectionSQLServer.cn.State == ConnectionState.Closed)
        {
            dbConnectionSQLServer.cn.Open();
        }

        SqlCommand    command;
        SqlDataReader dataReader;

        if (username == null)
        {
            if (a_id == null)
            {
                errorMessage = "Both the username and the activity ID are null.";
                mostInnerContainer.Controls.Clear();
            }
            else
            {
                errorMessage = "The username is null.";
                mostInnerContainer.Controls.Clear();
            }
        }
        else if (username != null && a_id == null)
        {
            // If an activity has the same username as the one specified.
            sql = "SELECT P_ID FROM PERSON WHERE P_USERNAME = '******';";

            try
            {
                command    = new SqlCommand(sql, dbConnectionSQLServer.cn);
                dataReader = command.ExecuteReader();

                while (dataReader.Read())
                {
                    sqlU_ID = (int)dataReader.GetValue(0);
                }


                // If no rows are found, or if session[id] is not equal
                // to the specified U_ID, it's not all good.
                int seissionWithoutNull = -1;

                if (Session["id"] != null)
                {
                    seissionWithoutNull = (int)Session["id"];
                }

                if (!dataReader.HasRows)
                {
                    allGood      = false;
                    errorMessage = "There is no username called " + username + ".";
                    mostInnerContainer.Controls.Clear();
                }
                else if (seissionWithoutNull != sqlU_ID)
                {
                    allGood      = false;
                    errorMessage = "There is no match between the session ID and the user's ID.<br />In other words please log-in or if you did try to modify your own profile picture =)";
                    mostInnerContainer.Controls.Clear();
                }


                dataReader.Close();
                command.Dispose();
                dbConnectionSQLServer.cn.Close();
            }

            catch (Exception ex)
            {
                Response.Write(ex.Message + " in Page_Load() 1.</br>");
            }
        }
        else
        {
            // If an activity has the same username as the one specified.
            sql = "SELECT S.SUB_P_ID, A.A_ID FROM ACTIVITY AS A JOIN SUBSCRIBE AS S ON A.A_ID = S.SUB_A_ID JOIN PERSON AS P ON S.SUB_P_ID = P.P_ID WHERE A.a_id = " + a_id + " AND P.P_USERNAME = '******';";

            try
            {
                command    = new SqlCommand(sql, dbConnectionSQLServer.cn);
                dataReader = command.ExecuteReader();

                while (dataReader.Read())
                {
                    sqlU_ID = (int)dataReader.GetValue(0);
                    sqlA_ID = (int)dataReader.GetValue(1);
                }


                // If no rows are found, or if session[id] is not equal
                // to the specified U_ID, it's not all good.
                int seissionWithoutNull = -1;

                if (Session["id"] != null)
                {
                    seissionWithoutNull = (int)Session["id"];
                }

                if (!dataReader.HasRows)
                {
                    allGood      = false;
                    errorMessage = "There is no match between the username and the activity ID.<br />In other words, try to modify your own activity =)";
                    mostInnerContainer.Controls.Clear();
                }
                else if (seissionWithoutNull != sqlU_ID)
                {
                    allGood      = false;
                    errorMessage = "There is no match between the session ID and the user's ID.";
                    mostInnerContainer.Controls.Clear();
                }



                dataReader.Close();
                command.Dispose();
                dbConnectionSQLServer.cn.Close();
            }

            catch (Exception ex)
            {
                Response.Write(ex.Message + " in Page_Load() 2.</br>");
            }
        }

        // If there is a username in the URL.
        if (username != null && allGood)
        {     // If there is an event ID in the URL.
            if (a_id != null)
            { // If there is a already a user's event photo stored.
                if (File.Exists(absolutePath + "activities/" + "event_" + a_id + ".jpg"))
                {
                    // Display it instead of the default picture.
                    target.Attributes.Remove("src");
                    target.Attributes.Add("src", relativePath + "activities/" + "event_" + a_id + ".jpg" + "?ver=" + File.GetLastWriteTime(Server.MapPath("/" + relativePath)).ToFileTime());

                    target.Attributes.Remove("style");

                    CropDiv.Attributes.Remove("style");
                }
                // If there is NO user's event photo stored.
                else
                {
                    target.Attributes.Remove("src");
                    target.Attributes.Add("src", relativePath + "activities/defaultEvent.png");

                    // Hide Crop Button
                    CropDiv.Attributes.Remove("style");
                    CropDiv.Attributes.Add("style", "display: none;");

                    //javascriptPlaceHolder.Controls.Add();
                    //javascriptPlaceHolder.Controls.Clear();
                }

                currentPage = "activity page.";


                HtmlGenericControl h3Title = new HtmlGenericControl("h3");
                h3Title.InnerHtml = "Upload and crop a photo for your " + currentPage;

                title.Controls.Add(h3Title);
            }
            // If there is NO event ID in the URL.
            else
            {   // If the image in JCrop.aspx is the default image.
                if (target.Attributes["src"] == "images/people/profile-generic.jpg")
                {
                    // If there is a already a user's profile photo stored.
                    if (File.Exists(absolutePath + "people/" + username + ".jpg"))
                    {
                        // Display it instead of the default picture.
                        target.Attributes.Remove("src");
                        target.Attributes.Add("src", relativePath + "people/" + username + ".jpg" + "?ver=" + File.GetLastWriteTime(Server.MapPath("/" + relativePath)).ToFileTime());

                        CropDiv.Attributes.Remove("style");
                    }
                    // If there is NO user's profile photo stored.
                    else
                    {
                        // Hide Crop Button
                        CropDiv.Attributes.Remove("style");
                        CropDiv.Attributes.Add("style", "display: none;");
                    }
                }

                //if (!File.Exists(absolutePath + "people/" + username + ".jpg") && !File.Exists(absolutePath + "people/" + username + "_uploaded.jpg"))
                //{
                //    Response.Write("somekeyx1 <br />");


                //    ScriptManager.RegisterStartupScript(Page, typeof(Page), "somekeyx1", "simulatex1Click();", true);
                //}
                //else
                //{
                //    Response.Write("somekeyx2 <br />");


                //    ScriptManager.RegisterStartupScript(Page, typeof(Page), "somekeyx2", "simulatex2Click();", true);
                //}


                currentPage = "profile page.";

                HtmlGenericControl h3Title = new HtmlGenericControl("h3");
                h3Title.InnerHtml = "Upload and crop a photo for your " + currentPage;

                title.Controls.Add(h3Title);
            }
        }
        else
        {
            // if the username and the activity ID are both null, don't display anything.
            //mostInnerContainer.Attributes.Add("style", "display: none;");

            HtmlGenericControl pErrorBr = new HtmlGenericControl("p");
            pErrorBr.InnerHtml = " ";

            HtmlGenericControl h2ErrorMessage = new HtmlGenericControl("h3");
            h2ErrorMessage.InnerText = "Error:";

            HtmlGenericControl pErrorMessage = new HtmlGenericControl("p");
            pErrorMessage.InnerHtml = errorMessage;

            mostInnerContainer.Controls.Add(pErrorBr);
            mostInnerContainer.Controls.Add(h2ErrorMessage);
            mostInnerContainer.Controls.Add(pErrorMessage);
        }

        // If the user has uploaded a photo
        if (IsPostBack && File1.PostedFile != null)
        {
            if (File1.PostedFile.FileName.Length > 0)
            {
                Upload_Click(sender, e);
            }
        }

        HtmlGenericControl pGoBack = new HtmlGenericControl("p");

        pGoBack.InnerText = "<< Go back to the " + currentPage;

        HtmlGenericControl aGoBack = new HtmlGenericControl("a");

        aGoBack.Attributes.Add("href", GoBack());

        aGoBack.Controls.Add(pGoBack);
        goBack.Controls.Add(aGoBack);
    }