protected void btnMakeAvailableCampaigns_Click(object sender, EventArgs e)
    {
        int training_id = Convert.ToInt32(Request.QueryString["trainingID"]);

        qSoc_Feed feed = new qSoc_Feed();
        feed.ScopeID = Convert.ToInt32(Context.Items["ScopeID"]);
        feed.CreatedBy = Convert.ToInt32(Context.Items["UserID"]);
        feed.LastModifiedBy = Convert.ToInt32(Context.Items["UserID"]);
        feed.Available = "Yes";
        feed.MarkAsDelete = 0;
        feed.LastModified = DateTime.Now;
        feed.Created = DateTime.Now;
        feed.OwnerID = Convert.ToInt32(Context.Items["UserID"]);
        feed.ReferenceID = training_id;
        feed.ContentTypeID = Convert.ToInt32(qSoc_ContentType.Types.Training);
        feed.Type = "training";
        feed.Title = txtTitle.Text;
        feed.Description = txtDescription.Text;
        feed.VisibleFeed = false;
        feed.VisibleOwnerFeed = false;
        feed.VisibleOwnerProfile = false;
        feed.VisibleExplore = false;
        feed.VisibleCampaign = false;
        feed.UploadedFrom = "manager";
        feed.Insert();

        Response.Redirect("~/manage/site/learning/training-edit.aspx?trainingID=" + training_id);
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        comment_id = Convert.ToInt32(Request.QueryString["commentID"]);
        qSoc_Comment2 comment = new qSoc_Comment2(comment_id);
        comment.Available = "No";
        comment.MarkAsDelete = 1;
        comment.Update();

        qSoc_Feed feed = new qSoc_Feed((int)qSoc_ContentType.Types.Shout, comment_id);

        if (feed != null)
        {
            if (feed.FeedID > 0)
            {
                feed.Available = "No";
                feed.MarkAsDelete = 1;
                feed.LastModified = DateTime.Now;
                feed.LastModifiedBy = Convert.ToInt32(Context.Items["UserID"]);
                feed.Update();
            }
        }

        if (Request.QueryString["edit-mode"] == "in-place")
            Response.Redirect(Request.QueryString["returnURL"]);
        else
            Response.Redirect("comments-list.aspx");
    }
    protected void btnMakeAvailableCampaigns_Click(object sender, EventArgs e)
    {
        int comment_id = Convert.ToInt32(Request.QueryString["commentID"]);

        qSoc_Feed feed = new qSoc_Feed((int)qSoc_ContentType.Types.Shout, comment_id);

        if (feed != null)
        {
            if (feed.FeedID > 0)
            {
                feed.Available = "Yes";
                feed.Update();
            }
        }

        qSoc_Comment2 comment = new qSoc_Comment2(comment_id);
        comment.Available = "Yes";
        comment.Update();

        Response.Redirect("~/manage/site/content/comment-edit.aspx?commentID=" + comment_id);
    }
    protected void btnSave_OnClick(object sender, System.EventArgs e)
    {
        Page.Validate("form");

        if (Page.IsValid)
        {
            int user_id = Convert.ToInt32(Context.Items["UserID"]);

            comment_id = Convert.ToInt32(Request.QueryString["commentID"]);
            qSoc_Comment2 comment = new qSoc_Comment2(comment_id);
            comment.LastModified = DateTime.Now;
            comment.LastModifiedBy = Convert.ToInt32(Context.Items["UserID"]);
            comment.Comment = txtComment.Text;
            comment.Available = rblAvailable.SelectedValue;
            comment.Update();

            string user_name = (new qPtl_User(user_id)).UserName;

            // add keywords
            string owner_keywords = string.Empty;
            qPtl_KeywordReference.DeleteKeywordReferencesByContent((int)qSoc_ContentType.Types.Shout, comment_id);
            foreach (ListItem item in cblKeywords.Items)
            {
                if (item.Selected)
                {
                    if (!String.IsNullOrEmpty(owner_keywords))
                        owner_keywords += "," + item.Text;
                    else
                        owner_keywords += item.Text;
                    qPtl_KeywordReference keyword = new qPtl_KeywordReference();
                    keyword.Available = "Yes";
                    keyword.ScopeID = Convert.ToInt32(Context.Items["ScopeID"]);
                    keyword.KeywordID = Convert.ToInt32(item.Value);
                    keyword.ContentTypeID = (int)qSoc_ContentType.Types.Shout;
                    keyword.ReferenceID = comment_id;
                    keyword.Created = DateTime.Now;
                    keyword.LastModified = DateTime.Now;
                    keyword.CreatedBy = Convert.ToInt32(Context.Items["UserID"]);
                    keyword.LastModifiedBy = Convert.ToInt32(Context.Items["UserID"]);
                    keyword.MarkAsDelete = 0;
                    keyword.Insert();
                }
            }

            // update feed information
            qSoc_Feed feed = new qSoc_Feed((int)qSoc_ContentType.Types.Shout, comment_id);

            if (feed != null)
            {
                if (feed.FeedID > 0)
                {
                    feed.Body = txtComment.Text;
                    int length_comment = comment.Comment.Length;
                    int max_length = 250;
                    if (length_comment < max_length)
                        max_length = length_comment;
                    feed.Title = feed.Body.Substring(0, max_length);
                    feed.Available = rblAvailable.SelectedValue;
                    feed.Update();
                }
            }

            lblMessage.Text = "*** Record Successfully Updated ***";
            lblMessageBottom.Text = "*** Record Successfully Updated ***";
            if (Request.QueryString["edit-mode"] == "in-place")
                Response.Redirect(Request.QueryString["returnURL"]);
            else
                Response.Redirect("comments-list.aspx");
        }
    }
    protected void populateTopics(int video_id, int content_type_id)
    {
        var topics = qSoc_Topic.GetTopics();
        qSoc_Feed feed = new qSoc_Feed((int)qSoc_ContentType.Types.Poll, poll_id);
        string reserved_keywords = string.Empty;
        if (!string.IsNullOrEmpty(feed.ReservedKeywords))
        {
            reserved_keywords = feed.ReservedKeywords;
        }

        if (topics != null)
        {
            foreach (qSoc_Topic topic in topics)
            {
                ListItem topic_item = new ListItem(topic.Name, topic.TopicID.ToString());

                if (!String.IsNullOrEmpty(reserved_keywords))
                {
                    if (reserved_keywords.Contains(topic.Name))
                    {
                        topic_item.Selected = true;
                    }
                }
                chkTopics.Items.Add(topic_item);
            }
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        plhAvailableTimes.Visible = false;

        if (Page.IsPostBack)
        {
            // re-bind choices        if (String.IsNullOrEmpty(Convert.ToString(poll_id)))
            if (!String.IsNullOrEmpty(Request.QueryString["pollID"]))
                poll_id = (Int32)ViewState["vsPollID"];
        }

        if (!Page.IsPostBack)
        {

            populateThemes();

            if (!String.IsNullOrEmpty(Request.QueryString["pollID"]))
            {
                poll_id = Convert.ToInt32(Request.QueryString["pollID"]);

                ViewState.Add("vsPollID", poll_id);

                populateKeywords(poll_id, (int)qSoc_ContentType.Types.Poll);
                populateTopics(poll_id, (int)qSoc_ContentType.Types.Poll);

                qSoc_Poll2 poll = new qSoc_Poll2(poll_id);

                lblTitle.Text = "Edit poll (ID: " + poll.PollID + ")";
                txtQuestion.Text = poll.Question;
                rblAvailable.SelectedValue = poll.Available;

                if (!String.IsNullOrEmpty(Convert.ToString(poll.ThemeID)))
                    ddlTheme.SelectedValue = Convert.ToString(poll.ThemeID);

                if (poll.PollType == "Fact")
                {
                    ddlPollType.SelectedValue = "Fact";
                }
                else if (poll.PollType == "Opinion")
                {
                    if (poll.ExperienceType == "Poll")
                    {
                        ddlPollType.SelectedValue = "Opinion";
                    }
                    else
                    {
                        ddlPollType.SelectedValue = "Think";
                    }
                }

                rdtStartTime.SelectedDate = poll.StartDate;
                rdtEndTime.SelectedDate = poll.EndDate;

                poll_type = poll.ExperienceType;
                BindChoices();

                if (poll.ExperienceType == "Think")
                {
                    plhAddResponse.Visible = false;
                    repChoices.Visible = false;
                    repNoEditChoices.Visible = true;

                }
                else
                {
                    repChoices.Visible = true;
                    repNoEditChoices.Visible = false;
                }

                ddlPollType.Enabled = false;
                lblPollTypeMessage.Text = "*** You cannot change the poll type once the poll has been created ***";
                lblPollTypeMessage.Visible = true;
                lblMessage.Visible = true;

                if (rblAvailable.SelectedValue == "Yes")
                {
                    // disabled since no longer using start dates
                    //plhAvailableTimes.Visible = true;
                    //rfvStartTime.Enabled = true;
                    //rfvEndTime.Enabled = true;
                    plhHighlightedPoll.Visible = true;
                }
                else
                {
                    plhAvailableTimes.Visible = false;
                    rfvStartTime.Enabled = false;
                    rfvEndTime.Enabled = false;
                    plhHighlightedPoll.Visible = false;
                }

                // see if in feed
                qSoc_Feed feed = new qSoc_Feed((int)qSoc_ContentType.Types.Poll, poll_id);
                plhExistingFeedItem.Visible = false;

                if (feed != null)
                {
                    if (feed.FeedID > 0 && feed.MarkAsDelete == 0 && feed.VisibleFeed == true)
                    {
                        chkDisplayInFeed.Checked = true;
                        plhExistingFeedItem.Visible = true;
                    }
                    if (feed.FeedID > 0 && feed.MarkAsDelete == 0 && feed.VisibleExplore == true)
                    {
                        chkDisplayInExplore.Checked = true;
                    }
                    string reserved_keywords = string.Empty;
                    if (!string.IsNullOrEmpty(feed.ReservedKeywords))
                    {
                        reserved_keywords = feed.ReservedKeywords;
                    }
                }
            }
            else
            {
                lblTitle.Text = "New poll";
                plhTools.Visible = false;
                rblAvailable.SelectedValue = "Yes";
                populateKeywords(0, (int)qSoc_ContentType.Types.Poll);
                plhChoices.Visible = false;
                plhAdditionalInfo.Visible = false;
                lblMessage.Text = "*** You must first enter and save the basic poll info and then you can add response choices";
                rfvAvailable.Enabled = false;
                rfvEndTime.Enabled = false;
                rfvStartTime.Enabled = false;
                plhDisplayInSiteSettings.Visible = false;
            }

            if (Convert.ToString(Request.QueryString["mode"]) == "add-successful")
            {
                lblMessage.Text = "*** Record Successfully Added - you can now configure additional poll info ***";
                lblMessageBottom.Text = "*** Record Successfully Added - you can now configure additional poll info ***";
            }

            if (Request.QueryString["mode"] == "edit-choice")
            {
                int poll_choice_id = Convert.ToInt32(Request.QueryString["pollChoiceID"]);
                ViewState.Add("vsPollChoiceID", poll_choice_id);

                qSoc_PollChoice2 choice = new qSoc_PollChoice2(poll_choice_id);
                txtChoice.Text = choice.Choice;
                txtMediaChoiceHTML.Text = choice.ChoiceMediaHTML;
                rblIsCorrect.SelectedValue = choice.Correct;
                if (ddlPollType.SelectedValue == "Fact")
                    plhChoiceCorrect.Visible = true;
                else
                    plhChoiceCorrect.Visible = false;
                txtFeedbackTitle.Text = choice.FeedbackTitle;
                txtFeedbackText.Text = choice.FeedbackDescription;
                txtFeedbackLink.Text = choice.FeedbackUrl;

                plhEditChoice.Visible = true;
            }

            var highlighted_poll = qSoc_Poll2.GetHighlightedPoll();

            if (highlighted_poll != null)
            {
                if (highlighted_poll.PollID == poll_id)
                {
                    chkHighlightedPoll.Checked = true;
                    lblHighlightedMessage.Text = "This is the current highlighted poll ID: " + highlighted_poll.PollID;
                }
                else
                    lblHighlightedMessage.Text = "Check to set as the highlighted poll";
            }
            else
                lblHighlightedMessage.Text = "Check to set as the highlighted poll";
        }
    }
    protected void btnSave_OnClick(object sender, System.EventArgs e)
    {
        Page.Validate("form");

        if (Page.IsValid)
        {

            int user_id = Convert.ToInt32(Context.Items["UserID"]);

            if (!String.IsNullOrEmpty(Request.QueryString["pollID"]))
            {
                poll_id = Convert.ToInt32(Request.QueryString["pollID"]);
                qSoc_Poll2 poll = new qSoc_Poll2(poll_id);

                poll.Question = txtQuestion.Text;
                poll.LastModified = DateTime.Now;
                poll.LastModifiedBy = user_id;
                if (!String.IsNullOrEmpty(ddlTheme.SelectedValue))
                    poll.ThemeID = Convert.ToInt32(ddlTheme.SelectedValue);
                else
                    poll.ThemeID = 0;

                DateTime start_time = new DateTime();
                DateTime end_time = new DateTime();
                if (rblAvailable.SelectedValue == "Yes")
                {
                    poll.Available = "Yes";
                    start_time = DateTime.Now;
                    end_time = DateTime.Now.AddYears(10);
                    poll.StartDate = start_time;
                    poll.EndDate = end_time;

                    /*
                    // code no longer used since use defaults for setting these values
                    if (!String.IsNullOrEmpty(Convert.ToString(rdtStartTime.SelectedDate)))
                        poll.StartDate = Convert.ToDateTime(rdtStartTime.SelectedDate);
                    if (!String.IsNullOrEmpty(Convert.ToString(rdtEndTime.SelectedDate)))
                        poll.EndDate = Convert.ToDateTime(rdtEndTime.SelectedDate);
                     */
                }
                else
                {
                    poll.Available = "No";
                    start_time = DateTime.Now.AddDays(-2);
                    end_time = DateTime.Now.AddDays(-1);
                    poll.StartDate = start_time;
                    poll.EndDate = end_time;

                    /*
                    // code no longer used since use defaults for setting these values
                    if (!String.IsNullOrEmpty(Convert.ToString(rdtStartTime.SelectedDate)))
                        poll.StartDate = null;
                    if (!String.IsNullOrEmpty(Convert.ToString(rdtEndTime.SelectedDate)))
                        poll.EndDate = null;
                    */
                }

                poll.Update();
            }
            else
            {
                qSoc_Poll2 poll = new qSoc_Poll2();
                poll.ScopeID = 1;
                poll.Created = DateTime.Now;
                poll.CreatedBy = user_id;
                poll.LastModified = DateTime.Now;
                poll.LastModifiedBy = user_id;
                poll.Available = "Yes";
                poll.MarkAsDelete = 0;
                poll.Question = txtQuestion.Text;
                if (ddlPollType.SelectedValue == "Think")
                {
                    poll.PollType = "Opinion";
                    poll.ExperienceType = "Think";
                }
                else if (ddlPollType.SelectedValue == "Fact")
                {
                    poll.PollType = "Fact";
                    poll.ExperienceType = "Poll";
                }
                else if (ddlPollType.SelectedValue == "Opinion")
                {
                    poll.PollType = "Opinion";
                    poll.ExperienceType = "Poll";
                }
                poll.Available = "No";

                if (!String.IsNullOrEmpty(ddlTheme.SelectedValue))
                    poll.ThemeID = Convert.ToInt32(ddlTheme.SelectedValue);

                // default to start time is today and end time is 20 years from now
                poll.StartDate = DateTime.Now;
                poll.EndDate = DateTime.Now.AddYears(20);

                poll.Insert();
                /*
                if (!String.IsNullOrEmpty(Convert.ToString(rdtStartTime.SelectedDate)))
                    poll.StartDate = Convert.ToDateTime(rdtStartTime.SelectedDate);
                if (!String.IsNullOrEmpty(Convert.ToString(rdtEndTime.SelectedDate)))
                    poll.EndDate = Convert.ToDateTime(rdtEndTime.SelectedDate);
                 */

                poll_id = poll.PollID;

                if (ddlPollType.SelectedValue == "Think")
                {
                    int curr_choice_id = createNewChoice(poll_id, "Agree", false);
                    curr_choice_id = createNewChoice(poll_id, "Disagree", false);
                }
            }

            // process if highlighted poll
            qDbs_SQLcode sql = new qDbs_SQLcode();
            string sqlCode = string.Empty;
            sqlCode = "UPDATE qSoc_Polls SET Highlighted = null";

            if (lblHighlightedMessage.Text.Contains("current") && chkHighlightedPoll.Checked == false)
            {
                sql.ExecuteSQL(sqlCode);        // // clear all highlighted options
            }
            else if (chkHighlightedPoll.Checked && lblHighlightedMessage.Text.Contains("current"))
            {
                sql.ExecuteSQL(sqlCode);        // clear all highlighted options
                qSoc_Poll2 poll = new qSoc_Poll2(poll_id);
                poll.Highlighted = true;
                poll.Update();
            }
            else if (chkHighlightedPoll.Checked && lblHighlightedMessage.Text.Contains("Check"))
            {
                sql.ExecuteSQL(sqlCode);        // clear all highlighted options
                qSoc_Poll2 poll = new qSoc_Poll2(poll_id);
                poll.Highlighted = true;
                poll.Update();
            }

            // add keywords
            string owner_keywords = string.Empty;
            qPtl_KeywordReference.DeleteKeywordReferencesByContent((int)qSoc_ContentType.Types.Poll, poll_id);
            foreach (ListItem item in cblKeywords.Items)
            {
                if (item.Selected)
                {
                    if (!String.IsNullOrEmpty(owner_keywords))
                        owner_keywords += "," + item.Text;
                    else
                        owner_keywords += item.Text;
                    qPtl_KeywordReference keyword = new qPtl_KeywordReference();
                    keyword.Available = "Yes";
                    keyword.ScopeID = Convert.ToInt32(Context.Items["ScopeID"]);
                    keyword.KeywordID = Convert.ToInt32(item.Value);
                    keyword.ContentTypeID = (int)qSoc_ContentType.Types.Poll;
                    keyword.ReferenceID = poll_id;
                    keyword.Created = DateTime.Now;
                    keyword.LastModified = DateTime.Now;
                    keyword.CreatedBy = Convert.ToInt32(Context.Items["UserID"]);
                    keyword.LastModifiedBy = Convert.ToInt32(Context.Items["UserID"]);
                    keyword.MarkAsDelete = 0;
                    keyword.Insert();
                }
            }

            string reserved_keywords = string.Empty;
            foreach (ListItem item in chkTopics.Items)
            {
                if (item.Selected)
                {
                    if (!String.IsNullOrEmpty(reserved_keywords))
                        reserved_keywords += "," + item.Text;
                    else
                        reserved_keywords += item.Text;
                }
            }

            qSoc_Feed feed = new qSoc_Feed((int)qSoc_ContentType.Types.Poll, poll_id);

            if (!chkDisplayInFeed.Checked && !chkDisplayInExplore.Checked)
            {
                if (feed != null)
                {
                    if (feed.FeedID > 0)
                    {
                        feed.MarkAsDelete = 1;
                        feed.Available = "No";
                        feed.ReservedKeywords = reserved_keywords;
                        if (!chkDisplayInFeed.Checked)
                            feed.VisibleFeed = false;
                        if (!chkDisplayInExplore.Checked)
                            feed.VisibleExplore = false;
                        feed.Update();
                    }
                }
            }
            else if (chkDisplayInFeed.Checked || chkDisplayInExplore.Checked || !string.IsNullOrEmpty(reserved_keywords))
            {
                qSoc_Poll2 poll = new qSoc_Poll2(poll_id);

                int p_user_id = user_id;
                if (post_as_user_id > 0)
                    p_user_id = post_as_user_id;

                qPtl_User user = new qPtl_User(p_user_id);
                var u_space = qSoc_UserSpace_View.GetUserSpaces(poll.CreatedBy);

                // evaluate title and description
                string p_title = q_Helper.replaceSpecialCharacters(poll.Question);
                string p_description = string.Empty;
                // add choices to description field
                var choices = qSoc_PollChoice2.GetAvailablePollChoices(poll_id);

                string choices_html = string.Empty;
                int i = 1;
                if (choices != null)
                {
                    foreach (var c in choices)
                    {
                        if (!String.IsNullOrEmpty(choices_html))
                            choices_html += "<br>";
                        choices_html += i + ") " + c.Choice;
                        i++;
                    }
                }

                if (!String.IsNullOrEmpty(choices_html))
                {
                    p_description = choices_html;
                }

                if (feed != null)
                {
                    if (feed.FeedID > 0)
                    {
                        feed.CreatedBy = user.UserID;
                        feed.Available = poll.Available;
                        if (chkMoveToTop.Checked)
                            feed.LastModified = DateTime.Now;
                        feed.LastModifiedBy = user.UserID;
                        feed.MarkAsDelete = poll.MarkAsDelete;
                        feed.OwnerMarkAsDelete = poll.MarkAsDelete;
                        feed.ContentTypeID = (int)qSoc_ContentType.Types.Poll;
                        feed.ReferenceID = poll_id;
                        feed.OwnerID = user.UserID;
                        feed.OwnerName = user.UserName;
                        feed.OwnerProfilePic = user.ProfilePict;
                        feed.Type = "poll";
                        feed.Title = p_title;
                        feed.Description = p_description;
                        feed.Body = p_description;
                        feed.OwnerRole = user.HighestRole;
                        feed.OwnerRoleID = user.HighestRank;
                        feed.OwnerKeywords = owner_keywords;
                        feed.ReservedKeywords = reserved_keywords;
                        feed.UploadedFrom = "manager";
                        if (!String.IsNullOrEmpty(ddlTheme.SelectedValue))
                        {
                            feed.ThemeID = Convert.ToInt32(ddlTheme.SelectedValue);
                            feed.ThemeName = Convert.ToString(ddlTheme.SelectedItem);
                        }
                        if (chkDisplayInFeed.Checked)
                            feed.VisibleFeed = true;
                        else
                            feed.VisibleFeed = false;
                        if (chkDisplayInExplore.Checked)
                            feed.VisibleExplore = true;
                        else
                            feed.VisibleExplore = false;
                        feed.Update();
                    }
                    else
                    {
                        // create new feed item
                        if (u_space != null)
                        {
                            foreach (var s in u_space)
                            {
                                feed.SpaceID = s.SpaceID;
                                feed.SpaceName = s.SpaceShortName;
                            }
                        }
                        feed.ScopeID = Convert.ToInt32(Context.Items["ScopeID"]);
                        feed.Available = poll.Available;
                        feed.Created = DateTime.Now;
                        feed.CreatedBy = user.UserID;
                        feed.LastModified = DateTime.Now;
                        feed.LastModifiedBy = user.UserID;
                        feed.MarkAsDelete = poll.MarkAsDelete;
                        feed.OwnerMarkAsDelete = poll.MarkAsDelete;
                        feed.ContentTypeID = (int)qSoc_ContentType.Types.Poll;
                        feed.ReferenceID = poll_id;
                        feed.OwnerID = user.UserID;
                        feed.OwnerName = user.UserName;
                        feed.OwnerProfilePic = user.ProfilePict;
                        feed.Type = "poll";
                        feed.Title = p_title;
                        feed.Description = p_description;
                        feed.Body = p_description;
                        feed.OwnerRole = user.HighestRole;
                        feed.OwnerRoleID = user.HighestRank;

                        feed.VisibleAll = true;
                        feed.VisibleFriends = true;
                        feed.VisibleFollowers = true;
                        feed.VisibleSpace = true;
                        feed.VisiblePrivate = true;
                        feed.VisibleFeed = true;
                        feed.VisibleOwnerFeed = true;
                        feed.VisibleOwnerProfile = true;
                        if (!String.IsNullOrEmpty(ddlTheme.SelectedValue))
                        {
                            feed.ThemeID = Convert.ToInt32(ddlTheme.SelectedValue);
                            feed.ThemeName = Convert.ToString(ddlTheme.SelectedItem);
                        }
                        feed.ReservedKeywords = reserved_keywords;
                        if (!String.IsNullOrEmpty(owner_keywords))
                        {
                            feed.OwnerKeywords = owner_keywords;
                        }
                        feed.UploadedFrom = "manager";
                        if (chkDisplayInFeed.Checked)
                            feed.VisibleFeed = true;
                        else
                            feed.VisibleFeed = false;
                        if (chkDisplayInExplore.Checked)
                            feed.VisibleExplore = true;
                        else
                            feed.VisibleExplore = false;
                        feed.Insert();
                    }
                }
            }

            // redirect to page to add poll + keywords
            if (!String.IsNullOrEmpty(Request.QueryString["pollID"]))
            {
                Response.Redirect("poll-edit.aspx?pollID=" + poll_id);
            }
            else
            {
                Response.Redirect(Request.Url.ToString() + "?mode=add-successful&pollID=" + poll_id);
            }
        }
        else
        {
            lblMessage.Text = "*** A problem has occurred -- make sure all the required information has been entered ***";
            lblMessageBottom.Text = "*** A problem has occurred -- make sure all the required information has been entered ***";
        }
    }
    protected void btnSave_OnClick(object sender, System.EventArgs e)
    {
        int user_id = Convert.ToInt32(Context.Items["UserID"]);

        if (!String.IsNullOrEmpty(Request.QueryString["articleID"]))
        {
            article_id = Convert.ToInt32(Request.QueryString["articleID"]);
            qLrn_Article article = new qLrn_Article(article_id);
            article.Title = txtTitle.Text;
            article.Description = txtSummary.Text;
            article.Body = reContent.Content;
            article.LastModified = DateTime.Now;
            article.LastModifiedBy = user_id;
            if (!String.IsNullOrEmpty(ddlAuthor.SelectedValue))
            {
                article.AuthorID = Convert.ToInt32(ddlAuthor.SelectedValue);
                article.OrganizationAuthorID = Convert.ToInt32(ddlAuthor.SelectedValue);
            }
            if (!String.IsNullOrEmpty(ddlLanguage.SelectedValue))
                article.Language = ddlLanguage.SelectedValue;
            else
                article.Language = "en";
            if (!String.IsNullOrEmpty(ddlTheme.SelectedValue))
                article.ThemeID = Convert.ToInt32(ddlTheme.SelectedValue);
            article.Available = rblAvailable.SelectedValue;
            if (plhArticleType.Visible == true)
                article.ArticleType = ddlArticleType.SelectedValue;
            article.Update();
        }
        else
        {
            qLrn_Article article = new qLrn_Article();
            article.ScopeID = 1;
            article.Created = DateTime.Now;
            article.CreatedBy = user_id;
            article.LastModified = DateTime.Now;
            article.LastModifiedBy = user_id;
            article.Available = "Yes";
            article.MarkAsDelete = 0;
            article.Title = txtTitle.Text;
            article.Description = txtSummary.Text;
            article.Body = reContent.Content;
            article.LastModified = DateTime.Now;
            article.LastModifiedBy = user_id;
            if (!String.IsNullOrEmpty(ddlAuthor.SelectedValue))
            {
                article.AuthorID = Convert.ToInt32(ddlAuthor.SelectedValue);
                article.OrganizationAuthorID = Convert.ToInt32(ddlAuthor.SelectedValue);
            }
            if (!String.IsNullOrEmpty(ddlLanguage.SelectedValue))
                article.Language = ddlLanguage.SelectedValue;
            else
                article.Language = "en";
            if (!String.IsNullOrEmpty(ddlTheme.SelectedValue))
                article.ThemeID = Convert.ToInt32(ddlTheme.SelectedValue);
            if (plhArticleType.Visible == true)
                article.ArticleType = ddlArticleType.SelectedValue;
            article.Available = rblAvailable.SelectedValue;
            article.Insert();

            article_id = article.ArticleID;
        }

        string user_name = (new qPtl_User(user_id)).UserName;

        // add keywords
        string owner_keywords = string.Empty;
        qPtl_KeywordReference.DeleteKeywordReferencesByContent((int)qSoc_ContentType.Types.Article, article_id);
        foreach (ListItem item in cblKeywords.Items)
        {
            if (item.Selected)
            {
                if (!String.IsNullOrEmpty(owner_keywords))
                    owner_keywords += "," + item.Text;
                else
                    owner_keywords += item.Text;
                qPtl_KeywordReference keyword = new qPtl_KeywordReference();
                keyword.Available = "Yes";
                keyword.ScopeID = Convert.ToInt32(Context.Items["ScopeID"]);
                keyword.KeywordID = Convert.ToInt32(item.Value);
                keyword.ContentTypeID = (int)qSoc_ContentType.Types.Article;
                keyword.ReferenceID = article_id;
                keyword.Created = DateTime.Now;
                keyword.LastModified = DateTime.Now;
                keyword.CreatedBy = Convert.ToInt32(Context.Items["UserID"]);
                keyword.LastModifiedBy = Convert.ToInt32(Context.Items["UserID"]);
                keyword.MarkAsDelete = 0;
                keyword.Insert();
            }
        }

        string reserved_keywords = string.Empty;
        foreach (ListItem item in chkTopics.Items)
        {
            if (item.Selected)
            {
                if (!String.IsNullOrEmpty(reserved_keywords))
                    reserved_keywords += "," + item.Text;
                else
                    reserved_keywords += item.Text;
            }
        }

        qSoc_Feed feed = new qSoc_Feed((int)qSoc_ContentType.Types.Article, article_id);

        if (!chkDisplayInFeed.Checked && !chkDisplayInExplore.Checked)
        {
            if (feed != null)
            {
                if (feed.FeedID > 0)
                {
                    feed.MarkAsDelete = 1;
                    feed.Available = "No";
                    feed.ReservedKeywords = reserved_keywords;
                    if (!chkDisplayInFeed.Checked)
                        feed.VisibleFeed = false;
                    if (!chkDisplayInExplore.Checked)
                        feed.VisibleExplore = false;
                    feed.Update();
                }
            }
        }
        else if (chkDisplayInFeed.Checked || chkDisplayInExplore.Checked || !string.IsNullOrEmpty(reserved_keywords))
        {
            qLrn_Article article = new qLrn_Article(article_id);

            int p_user_id = user_id;
            if (post_as_user_id > 0)
                p_user_id = post_as_user_id;

            qPtl_User user = new qPtl_User(p_user_id);
            var u_space = qSoc_UserSpace_View.GetUserSpaces(article.CreatedBy);

            // evaluate title and description
            string p_title = q_Helper.replaceSpecialCharacters(article.Title);
            string p_description = q_Helper.replaceSpecialCharacters(article.Description);

            if (feed != null)
            {
                if (feed.FeedID > 0)
                {
                    feed.CreatedBy = user.UserID;
                    feed.Available = article.Available;
                    if (chkMoveToTop.Checked)
                        feed.LastModified = DateTime.Now;
                    feed.LastModifiedBy = user.UserID;
                    feed.MarkAsDelete = article.MarkAsDelete;
                    feed.OwnerMarkAsDelete = article.MarkAsDelete;
                    feed.ContentTypeID = (int)qSoc_ContentType.Types.Article;
                    feed.ReferenceID = article_id;
                    feed.OwnerID = user.UserID;
                    feed.OwnerName = user.UserName;
                    feed.OwnerProfilePic = user.ProfilePict;
                    feed.Type = "article";
                    feed.Title = p_title;
                    feed.Description = p_description;
                    feed.Body = p_description;
                    feed.OwnerRole = user.HighestRole;
                    feed.OwnerRoleID = user.HighestRank;
                    feed.OwnerKeywords = owner_keywords;
                    feed.ReservedKeywords = reserved_keywords;
                    feed.UploadedFrom = "manager";
                    if (!String.IsNullOrEmpty(ddlTheme.SelectedValue))
                    {
                        feed.ThemeID = Convert.ToInt32(ddlTheme.SelectedValue);
                        feed.ThemeName = Convert.ToString(ddlTheme.SelectedItem);
                    }
                    if (chkDisplayInFeed.Checked)
                        feed.VisibleFeed = true;
                    else
                        feed.VisibleFeed = false;
                    if (chkDisplayInExplore.Checked)
                        feed.VisibleExplore = true;
                    else
                        feed.VisibleExplore = false;
                    feed.Update();
                }
                else
                {
                    // create new feed item
                    if (u_space != null)
                    {
                        foreach (var s in u_space)
                        {
                            feed.SpaceID = s.SpaceID;
                            feed.SpaceName = s.SpaceShortName;
                        }
                    }
                    feed.ScopeID = Convert.ToInt32(Context.Items["ScopeID"]);
                    feed.Available = article.Available;
                    feed.Created = DateTime.Now;
                    feed.CreatedBy = user.UserID;
                    feed.LastModified = DateTime.Now;
                    feed.LastModifiedBy = user.UserID;
                    feed.MarkAsDelete = article.MarkAsDelete;
                    feed.OwnerMarkAsDelete = article.MarkAsDelete;
                    feed.ContentTypeID = (int)qSoc_ContentType.Types.Article;
                    feed.ReferenceID = article_id;
                    feed.OwnerID = user.UserID;
                    feed.OwnerName = user.UserName;
                    feed.OwnerProfilePic = user.ProfilePict;
                    feed.Type = "article";
                    feed.Title = p_title;
                    feed.Description = p_description;
                    feed.Body = p_description;
                    feed.OwnerRole = user.HighestRole;
                    feed.OwnerRoleID = user.HighestRank;

                    feed.VisibleAll = true;
                    feed.VisibleFriends = true;
                    feed.VisibleFollowers = true;
                    feed.VisibleSpace = true;
                    feed.VisiblePrivate = true;
                    feed.VisibleFeed = true;
                    feed.VisibleOwnerFeed = true;
                    feed.VisibleOwnerProfile = true;
                    if (!String.IsNullOrEmpty(ddlTheme.SelectedValue))
                    {
                        feed.ThemeID = Convert.ToInt32(ddlTheme.SelectedValue);
                        feed.ThemeName = Convert.ToString(ddlTheme.SelectedItem);
                    }
                    feed.ReservedKeywords = reserved_keywords;
                    if (!String.IsNullOrEmpty(owner_keywords))
                    {
                        feed.OwnerKeywords = owner_keywords;
                    }
                    feed.UploadedFrom = "manager";
                    if (chkDisplayInFeed.Checked)
                        feed.VisibleFeed = true;
                    else
                        feed.VisibleFeed = false;
                    if (chkDisplayInExplore.Checked)
                        feed.VisibleExplore = true;
                    else
                        feed.VisibleExplore = false;
                    feed.Insert();
                }
            }
        }

        // redirect to page to add theme + keywords
        //Response.Redirect("~/qLrn/articles-list.aspx");
        if (!String.IsNullOrEmpty(Request.QueryString["articleID"]))
        {
            lblMessage.Text = "*** Record Successfully Updated ***";
            lblMessageBottom.Text = "*** Record Successfully Updated ***";
            if (Request.QueryString["edit-mode"] == "in-place")
            {
                string return_redirect = Request.QueryString["returnURL"];
                if (!String.IsNullOrEmpty(Request.QueryString["location"]))
                    return_redirect += "&location=" + Request.QueryString["location"];
                Response.Redirect(return_redirect);
            }
            else
                Response.Redirect("articles-list.aspx");
        }
        else
        {
            Response.Redirect(Request.Url.ToString() + "?mode=add-successful&articleID=" + article_id);
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            populateThemes();
            populateAuthors();

            if (!String.IsNullOrEmpty(Request.QueryString["articleID"]))
            {
                article_id = Convert.ToInt32(Request.QueryString["articleID"]);
                ViewState.Add("vsArticleID", article_id);
                reContent.ImageManager.MaxUploadFileSize = 4194304;

                populateKeywords(article_id, (int)qSoc_ContentType.Types.Article);
                populateTopics(article_id, (int)qSoc_ContentType.Types.Article);

                qLrn_Article article = new qLrn_Article(article_id);
                qSoc_ContentType content = new qSoc_ContentType((int)qSoc_ContentType.Types.Article);

                qPtl_User posted_by = new qPtl_User(article.CreatedBy);
                owner = posted_by.UserName;
                ViewState.Add("vsOwner", owner);
                owner_id = article.CreatedBy;
                ViewState.Add("vsOwnerID", owner_id);
                lblPostedTime.Text = " at " + article.Created;

                lblTitle.Text = "Edit Article (ID: " + article.ArticleID + ")";
                txtTitle.Text = article.Title;
                txtSummary.Text = article.Description;
                reContent.Content = article.Body;
                if (!String.IsNullOrEmpty(Convert.ToString(article.AuthorID)))
                    ddlAuthor.SelectedValue = Convert.ToString(article.AuthorID);
                if (!String.IsNullOrEmpty(Convert.ToString(article.ThemeID)))
                    ddlTheme.SelectedValue = Convert.ToString(article.ThemeID);
                ddlArticleType.SelectedValue = article.ArticleType;
                if (!String.IsNullOrEmpty(article.Language))
                    ddlLanguage.SelectedValue = article.Language;
                else
                    ddlLanguage.SelectedValue = "en";

                rblAvailable.SelectedValue = article.Available;
                plhPostedBy.Visible = true;
                hplPreviewArticle.Visible = true;
                hplPreviewArticle.NavigateUrl = "/social/learning/article-details.aspx?articleID=" + article.ArticleID;
                hplPreviewArticle.Target = "_blank";

                if (!String.IsNullOrEmpty(Request.QueryString["location"]))
                {
                    if (Request.QueryString["location"] == "my-health-care")
                        plhArticleType.Visible = true;
                }

                if (Convert.ToString(Request.QueryString["mode"]) == "add-successful")
                {
                    lblMessage.Text = "*** Record Successfully Added ***";
                }

                // see if in feed
                qSoc_Feed feed = new qSoc_Feed((int)qSoc_ContentType.Types.Article, article_id);
                plhExistingFeedItem.Visible = false;

                if (feed != null)
                {
                    if (feed.FeedID > 0 && feed.MarkAsDelete == 0 && feed.VisibleFeed == true)
                    {
                        chkDisplayInFeed.Checked = true;
                        plhExistingFeedItem.Visible = true;
                    }
                    if (feed.FeedID > 0 && feed.MarkAsDelete == 0 && feed.VisibleExplore == true)
                    {
                        chkDisplayInExplore.Checked = true;
                    }
                    string reserved_keywords = string.Empty;
                    if (!string.IsNullOrEmpty(feed.ReservedKeywords))
                    {
                        reserved_keywords = feed.ReservedKeywords;
                    }
                }
            }

            else
            {
                lblTitle.Text = "New Article";
                btnDelete.Visible = false;
                populateKeywords(0, (int)qSoc_ContentType.Types.Article);
                plhPostedBy.Visible = false;
                rblAvailable.SelectedValue = "Yes";
                plhTools.Visible = false;
            }
        }

        if (!String.IsNullOrEmpty(health_active))
            if (health_active == "true")
                plhArticleType.Visible = true;

        if (String.IsNullOrEmpty(Convert.ToString(article_id)))
            article_id = (Int32)ViewState["vsArticleID"];
        if (String.IsNullOrEmpty(Convert.ToString(owner_id)))
            owner_id = (Int32)ViewState["vsOwnerID"];
        if (String.IsNullOrEmpty(owner))
            owner = (String)ViewState["vsOwner"];
    }
    protected void btnSave_OnClick(object sender, System.EventArgs e)
    {
        Page.Validate("form");

        if (Page.IsValid)
        {

            int user_id = Convert.ToInt32(Context.Items["UserID"]);
            video_id = Convert.ToInt32(Request.QueryString["videoID"]);

            string user_name = (new qPtl_User(user_id)).UserName;

            qSoc_Video video = new qSoc_Video(video_id);
            video.Title = txtTitle.Text;
            video.Description = txtDescription.Text;
            if (!String.IsNullOrEmpty(ddlTheme.SelectedValue))
                video.ThemeID = Convert.ToInt32(ddlTheme.SelectedValue);
            video.Status = "Approved";
            video.Created = video.Created;
            video.CreatedBy = video.CreatedBy;
            video.Approved = DateTime.Now;
            video.Source = lblVideoType.Text;
            video.LastModified = DateTime.Now;
            video.LastModifiedBy = user_id;
            video.Length = video.Length;
            video.EmbedCode = txtEmbed.Text;
            if (txtPreviewImage.Visible == true)
                video.ThumbnailURL = txtPreviewImage.Text;
            video.SourceVideoID = txtSourceVideoID.Text;
            if (!String.IsNullOrEmpty(ddlExternalSource.SelectedValue))
                video.ExternalSourceName = ddlExternalSource.SelectedValue;
            string author = string.Empty;
            author = ddlAuthor.SelectedValue;
            if (!String.IsNullOrEmpty(author))
                video.AuthorID = Convert.ToInt32(ddlAuthor.SelectedValue);
            video.Available = rblAvailable.SelectedValue;

            // process if highlighted video
            qDbs_SQLcode sql = new qDbs_SQLcode();
            string sqlCode = string.Empty;
            sqlCode = "UPDATE qSoc_Videos SET HighlightedWF = null";

            qSoc_HighlightedVideo.Delete(video_id);

            if (lblHighlightedMessage.Text.Contains("current") && chkHighlightedVideo.Checked == false)
            {
                sql.ExecuteSQL(sqlCode);
                video.HighlightedWF = null;
            }
            else if (chkHighlightedVideo.Checked && lblHighlightedMessage.Text.Contains("current"))
            {
                sql.ExecuteSQL(sqlCode);
                qSoc_HighlightedVideo high_video = new qSoc_HighlightedVideo();
                high_video = qSoc_HighlightedVideo.GetLatest();
                high_video.Available = "No";
                high_video.Created = DateTime.Now;
                high_video.CreatedBy = user_id;
                high_video.LastModified = DateTime.Now;
                high_video.LastModifiedBy = user_id;
                high_video.MarkAsDelete = 1;
                high_video.Status = "Hidden";
                high_video.Update();
            }
            else if (chkHighlightedVideo.Checked && lblHighlightedMessage.Text.Contains("Check"))
            {
                sql.ExecuteSQL(sqlCode);
                qSoc_HighlightedVideo new_highlighted = new qSoc_HighlightedVideo();
                new_highlighted.VideoID = video_id;
                new_highlighted.ScopeID = Convert.ToInt32(Context.Items["ScopeID"]);
                new_highlighted.Available = "Yes";
                new_highlighted.Created = DateTime.Now;
                new_highlighted.CreatedBy = user_id;
                new_highlighted.LastModified = DateTime.Now;
                new_highlighted.LastModifiedBy = user_id;
                new_highlighted.MarkAsDelete = 0;
                new_highlighted.Status = "Visible";
                new_highlighted.Insert();
                video.HighlightedWF = "Yes";
            }
            video.Update();

            // add keywords
            string owner_keywords = string.Empty;
            qPtl_KeywordReference.DeleteKeywordReferencesByContent((int)qSoc_ContentType.Types.Video, video_id);
            foreach (ListItem item in cblKeywords.Items)
            {
                if (item.Selected)
                {
                    if (!String.IsNullOrEmpty(owner_keywords))
                        owner_keywords += "," + item.Text;
                    else
                        owner_keywords += item.Text;
                    qPtl_KeywordReference keyword = new qPtl_KeywordReference();
                    keyword.Available = "Yes";
                    keyword.ScopeID = Convert.ToInt32(Context.Items["ScopeID"]);
                    keyword.KeywordID = Convert.ToInt32(item.Value);
                    keyword.ContentTypeID = (int)qSoc_ContentType.Types.Video;
                    keyword.ReferenceID = video_id;
                    keyword.Created = DateTime.Now;
                    keyword.LastModified = DateTime.Now;
                    keyword.CreatedBy = Convert.ToInt32(Context.Items["UserID"]);
                    keyword.LastModifiedBy = Convert.ToInt32(Context.Items["UserID"]);
                    keyword.MarkAsDelete = 0;
                    keyword.Insert();
                }
            }

            string reserved_keywords = string.Empty;
            foreach (ListItem item in chkTopics.Items)
            {
                if (item.Selected)
                {
                    if (!String.IsNullOrEmpty(reserved_keywords))
                        reserved_keywords += "," + item.Text;
                    else
                        reserved_keywords += item.Text;
                }
            }

            qSoc_Feed feed = new qSoc_Feed((int)qSoc_ContentType.Types.Video, video.VideoID);

            if (!chkDisplayInFeed.Checked && !chkDisplayInExplore.Checked)
            {
                if (feed != null)
                {
                    if (feed.FeedID > 0)
                    {
                        feed.MarkAsDelete = 1;
                        feed.Available = "No";
                        feed.ReservedKeywords = reserved_keywords;
                        if (!chkDisplayInFeed.Checked)
                            feed.VisibleFeed = false;
                        if (!chkDisplayInExplore.Checked)
                            feed.VisibleExplore = false;
                        feed.Update();
                    }
                }
            }
            else if (chkDisplayInFeed.Checked || chkDisplayInExplore.Checked || !string.IsNullOrEmpty(reserved_keywords))
            {
                int p_user_id = user_id;
                if (post_as_user_id > 0)
                    p_user_id = post_as_user_id;

                qPtl_User user = new qPtl_User(p_user_id);
                var u_space = qSoc_UserSpace_View.GetUserSpaces(video.CreatedBy);

                if (feed != null)
                {
                    if (feed.FeedID > 0)
                    {
                        feed.Available = video.Available;
                        feed.ScopeID = Convert.ToInt32(Context.Items["ScopeID"]);
                        feed.CreatedBy = user.UserID;
                        if (chkMoveToTop.Checked)
                            feed.LastModified = DateTime.Now;
                        feed.LastModified = video.LastModified;
                        feed.LastModifiedBy = user.UserID;
                        feed.MarkAsDelete = video.MarkAsDelete;
                        feed.OwnerMarkAsDelete = video.MarkAsDelete;
                        feed.ContentTypeID = (int)qSoc_ContentType.Types.Video;
                        feed.ReferenceID = video.VideoID;
                        feed.OwnerID = user.UserID;
                        feed.OwnerName = user.UserName;
                        feed.OwnerProfilePic = user.ProfilePict;
                        feed.Type = "video";
                        feed.Title = video.Title;
                        feed.Description = video.Description;
                        feed.OwnerRole = user.HighestRole;
                        feed.OwnerRoleID = user.HighestRank;
                        feed.OwnerKeywords = owner_keywords;
                        feed.UploadedFrom = "manager";
                        feed.ReservedKeywords = reserved_keywords;
                        feed.Thumbnail = video.ThumbnailURL;
                        if (!String.IsNullOrEmpty(ddlTheme.SelectedValue))
                        {
                            feed.ThemeID = Convert.ToInt32(ddlTheme.SelectedValue);
                            feed.ThemeName = Convert.ToString(ddlTheme.SelectedItem);
                        }
                        if (video.Source == "internal")
                        {
                            feed.VideoKey = video.ViddlerID;
                        }
                        else
                        {
                            string embed_code = Convert.ToString(video.EmbedCode);
                            feed.Embed = embed_code;
                            feed.VideoKey = null;
                        }
                        if (chkDisplayInFeed.Checked)
                            feed.VisibleFeed = true;
                        else
                            feed.VisibleFeed = false;
                        if (chkDisplayInExplore.Checked)
                            feed.VisibleExplore = true;
                        else
                            feed.VisibleExplore = false;
                        feed.Update();
                    }
                    else
                    {
                        // create new feed item
                        if (u_space != null)
                        {
                            foreach (var s in u_space)
                            {
                                feed.SpaceID = s.SpaceID;
                                feed.SpaceName = s.SpaceShortName;
                            }
                        }
                        feed.Available = video.Available;
                        feed.ScopeID = Convert.ToInt32(Context.Items["ScopeID"]);
                        feed.Created = DateTime.Now;    //video.Created;
                        feed.CreatedBy = user.UserID;
                        feed.LastModified = DateTime.Now;
                        feed.LastModifiedBy = user.UserID;
                        feed.MarkAsDelete = video.MarkAsDelete;
                        feed.OwnerMarkAsDelete = video.MarkAsDelete;
                        feed.ContentTypeID = (int)qSoc_ContentType.Types.Video;
                        feed.ReferenceID = video.VideoID;
                        feed.OwnerID = user.UserID;
                        feed.OwnerName = user.UserName;
                        feed.OwnerProfilePic = user.ProfilePict;
                        feed.Type = "video";
                        feed.Title = video.Title;
                        feed.Description = video.Description;
                        feed.OwnerRole = user.HighestRole;
                        feed.OwnerRoleID = user.HighestRank;
                        feed.OwnerKeywords = owner_keywords;
                        feed.UploadedFrom = "manager";
                        feed.ReservedKeywords = reserved_keywords;
                        feed.Thumbnail = video.ThumbnailURL;
                        if (!String.IsNullOrEmpty(ddlTheme.SelectedValue))
                        {
                            feed.ThemeID = Convert.ToInt32(ddlTheme.SelectedValue);
                            feed.ThemeName = Convert.ToString(ddlTheme.SelectedItem);
                        }
                        if (video.Source == "internal")
                        {
                            feed.VideoKey = video.ViddlerID;
                        }
                        else
                        {
                            string embed_code = Convert.ToString(video.EmbedCode);
                            feed.Embed = embed_code;
                        }

                        feed.VisibleAll = true;
                        feed.VisibleFriends = true;
                        feed.VisibleFollowers = true;
                        feed.VisibleSpace = true;
                        feed.VisiblePrivate = true;
                        feed.VisibleFeed = true;
                        feed.VisibleOwnerFeed = true;
                        feed.VisibleOwnerProfile = true;
                        if (!String.IsNullOrEmpty(owner_keywords))
                        {
                            feed.OwnerKeywords = owner_keywords;
                        }
                        if (chkDisplayInFeed.Checked)
                            feed.VisibleFeed = true;
                        else
                            feed.VisibleFeed = false;
                        if (chkDisplayInExplore.Checked)
                            feed.VisibleExplore = true;
                        else
                            feed.VisibleExplore = false;
                        feed.Insert();

                    }
                }
            }

            // see if there is a pending task for this video
            if (video.Status == "Approved")
            {
                qPtl_Task task = new qPtl_Task((int)qSoc_ContentType.Types.Video, video_id);

                if (task != null)
                {
                    if (task.TaskID > 0)
                    {
                        task.PercentCompleted = 100;
                        task.Status = "Completed";
                        task.Update();
                    }
                }
            }

            // redirect to page to add theme + keywords
            //  Response.Redirect("~/qSoc/videos-list.aspx");
            lblMessage.Text = "*** Record Successfully Updated ***";
            lblMessageBottom.Text = "*** Record Successfully Updated ***";
        }
    }
    protected void btnSave_OnClick(object sender, System.EventArgs e)
    {
        Page.Validate("form");

        if (Page.IsValid)
        {

            int user_id = Convert.ToInt32(Context.Items["UserID"]);

            if (!String.IsNullOrEmpty(Request.QueryString["linkID"]))
            {
                link_id = Convert.ToInt32(Request.QueryString["linkID"]);
                qPtl_Link link = new qPtl_Link(link_id);

                link.Title = txtName.Text;
                link.Description = txtSummary.Text;
                link.Available = rblAvailable.SelectedValue;
                link.LastModified = DateTime.Now;
                link.LastModifiedBy = user_id;
                link.URL = txtURL.Text;
                if (!String.IsNullOrEmpty(ddlAuthor.SelectedValue))
                    link.AuthorID = Convert.ToInt32(ddlAuthor.SelectedValue);
                if (!String.IsNullOrEmpty(ddlTheme.SelectedValue))
                    link.ThemeID = Convert.ToInt32(ddlTheme.SelectedValue);
                link.Type = ddlType.SelectedValue;
                if (Convert.ToString(ddlType.SelectedValue).Contains("Internal"))
                    link.Source = "internal";
                else
                    link.Source = "external";
                link.UploadedFrom = "manager";
                link.Language = ddlLanguage.SelectedValue;
                link.LinkType = ddlLinkType.SelectedValue;
                link.Update();
            }
            else
            {
                qPtl_Link link = new qPtl_Link();
                link.ScopeID = 1;
                link.Created = DateTime.Now;
                link.CreatedBy = user_id;
                link.LastModified = DateTime.Now;
                link.LastModifiedBy = user_id;
                link.Available = "Yes";
                link.MarkAsDelete = 0;
                link.Title = txtName.Text;
                link.Description = txtSummary.Text;
                link.URL = txtURL.Text;
                link.Available = rblAvailable.SelectedValue;
                link.Language = ddlLanguage.SelectedValue;
                link.LinkType = ddlLinkType.SelectedValue;
                if (!String.IsNullOrEmpty(ddlAuthor.SelectedValue))
                    link.AuthorID = Convert.ToInt32(ddlAuthor.SelectedValue);
                if (!String.IsNullOrEmpty(ddlTheme.SelectedValue))
                    link.ThemeID = Convert.ToInt32(ddlTheme.SelectedValue);
                link.Type = ddlType.SelectedValue;

                if (Convert.ToString(ddlType.SelectedValue).Contains("Internal"))
                    link.Source = "internal";
                else
                    link.Source = "external";
                link.UploadedFrom = "manager";
                link.Insert();

                link_id = link.LinkID;
            }

            // add keywords
            string owner_keywords = string.Empty;
            qPtl_KeywordReference.DeleteKeywordReferencesByContent((int)qSoc_ContentType.Types.Link, link_id);
            foreach (ListItem item in cblKeywords.Items)
            {
                if (item.Selected)
                {
                    if (!String.IsNullOrEmpty(owner_keywords))
                        owner_keywords += "," + item.Text;
                    else
                        owner_keywords += item.Text;
                    qPtl_KeywordReference keyword = new qPtl_KeywordReference();
                    keyword.Available = "Yes";
                    keyword.ScopeID = Convert.ToInt32(Context.Items["ScopeID"]);
                    keyword.KeywordID = Convert.ToInt32(item.Value);
                    keyword.ContentTypeID = (int)qSoc_ContentType.Types.Link;
                    keyword.ReferenceID = link_id;
                    keyword.Created = DateTime.Now;
                    keyword.LastModified = DateTime.Now;
                    keyword.CreatedBy = Convert.ToInt32(Context.Items["UserID"]);
                    keyword.LastModifiedBy = Convert.ToInt32(Context.Items["UserID"]);
                    keyword.MarkAsDelete = 0;
                    keyword.Insert();
                }
            }

            string reserved_keywords = string.Empty;
            foreach (ListItem item in chkTopics.Items)
            {
                if (item.Selected)
                {
                    if (!String.IsNullOrEmpty(reserved_keywords))
                        reserved_keywords += "," + item.Text;
                    else
                        reserved_keywords += item.Text;
                }
            }

            qSoc_Feed feed = new qSoc_Feed((int)qSoc_ContentType.Types.Link, link_id);

            if (!chkDisplayInFeed.Checked && !chkDisplayInExplore.Checked)
            {
                if (feed != null)
                {
                    if (feed.FeedID > 0)
                    {
                        feed.MarkAsDelete = 1;
                        feed.Available = "No";
                        feed.ReservedKeywords = reserved_keywords;
                        if (!chkDisplayInFeed.Checked)
                            feed.VisibleFeed = false;
                        if (!chkDisplayInExplore.Checked)
                            feed.VisibleExplore = false;
                        feed.Update();
                    }
                }
            }
            else if (chkDisplayInFeed.Checked || chkDisplayInExplore.Checked || !string.IsNullOrEmpty(reserved_keywords))
            {
                qPtl_Link link = new qPtl_Link(link_id);

                int p_user_id = user_id;
                if (post_as_user_id > 0)
                    p_user_id = post_as_user_id;

                qPtl_User user = new qPtl_User(p_user_id);
                var u_space = qSoc_UserSpace_View.GetUserSpaces(link.CreatedBy);

                // evaluate title and description
                string p_title = q_Helper.replaceSpecialCharacters(link.Title);
                string p_description = q_Helper.replaceSpecialCharacters(link.Description);

                if (feed != null)
                {
                    if (feed.FeedID > 0)
                    {
                        feed.CreatedBy = user.UserID;
                        feed.Available = link.Available;
                        if (chkMoveToTop.Checked)
                            feed.LastModified = DateTime.Now;
                        feed.LastModifiedBy = user.UserID;
                        feed.MarkAsDelete = link.MarkAsDelete;
                        feed.OwnerMarkAsDelete = link.MarkAsDelete;
                        feed.ContentTypeID = (int)qSoc_ContentType.Types.Link;
                        feed.ReferenceID = link_id;
                        feed.OwnerID = user.UserID;
                        feed.OwnerName = user.UserName;
                        feed.OwnerProfilePic = user.ProfilePict;
                        feed.Type = "link";
                        feed.Title = p_title;
                        feed.URL = link.URL;
                        feed.Description = p_description;
                        feed.Body = p_description;
                        feed.OwnerRole = user.HighestRole;
                        feed.OwnerRoleID = user.HighestRank;
                        feed.OwnerKeywords = owner_keywords;
                        feed.ReservedKeywords = reserved_keywords;
                        feed.UploadedFrom = "manager";
                        if (!String.IsNullOrEmpty(ddlTheme.SelectedValue))
                        {
                            feed.ThemeID = Convert.ToInt32(ddlTheme.SelectedValue);
                            feed.ThemeName = Convert.ToString(ddlTheme.SelectedItem);
                        }
                        if (chkDisplayInFeed.Checked)
                            feed.VisibleFeed = true;
                        else
                            feed.VisibleFeed = false;
                        if (chkDisplayInExplore.Checked)
                            feed.VisibleExplore = true;
                        else
                            feed.VisibleExplore = false;
                        feed.Update();
                    }
                    else
                    {
                        // create new feed item
                        if (u_space != null)
                        {
                            foreach (var s in u_space)
                            {
                                feed.SpaceID = s.SpaceID;
                                feed.SpaceName = s.SpaceShortName;
                            }
                        }
                        feed.ScopeID = Convert.ToInt32(Context.Items["ScopeID"]);
                        feed.Available = link.Available;
                        feed.Created = DateTime.Now;
                        feed.CreatedBy = user.UserID;
                        feed.LastModified = DateTime.Now;
                        feed.LastModifiedBy = user.UserID;
                        feed.MarkAsDelete = link.MarkAsDelete;
                        feed.OwnerMarkAsDelete = link.MarkAsDelete;
                        feed.ContentTypeID = (int)qSoc_ContentType.Types.Link;
                        feed.ReferenceID = link_id;
                        feed.OwnerID = user.UserID;
                        feed.OwnerName = user.UserName;
                        feed.OwnerProfilePic = user.ProfilePict;
                        feed.Type = "link";
                        feed.Title = p_title;
                        feed.Description = p_description;
                        feed.Body = p_description;
                        feed.OwnerRole = user.HighestRole;
                        feed.OwnerRoleID = user.HighestRank;
                        feed.URL = link.URL;

                        feed.VisibleAll = true;
                        feed.VisibleFriends = true;
                        feed.VisibleFollowers = true;
                        feed.VisibleSpace = true;
                        feed.VisiblePrivate = true;
                        feed.VisibleFeed = true;
                        feed.VisibleOwnerFeed = true;
                        feed.VisibleOwnerProfile = true;
                        if (!String.IsNullOrEmpty(ddlTheme.SelectedValue))
                        {
                            feed.ThemeID = Convert.ToInt32(ddlTheme.SelectedValue);
                            feed.ThemeName = Convert.ToString(ddlTheme.SelectedItem);
                        }
                        feed.ReservedKeywords = reserved_keywords;
                        if (!String.IsNullOrEmpty(owner_keywords))
                        {
                            feed.OwnerKeywords = owner_keywords;
                        }
                        feed.UploadedFrom = "manager";
                        if (chkDisplayInFeed.Checked)
                            feed.VisibleFeed = true;
                        else
                            feed.VisibleFeed = false;
                        if (chkDisplayInExplore.Checked)
                            feed.VisibleExplore = true;
                        else
                            feed.VisibleExplore = false;
                        feed.Insert();
                    }
                }
            }

            // redirect to page to add link + keywords
            if (!String.IsNullOrEmpty(Request.QueryString["linkID"]))
            {
                //lblMessage.Text = "*** Record Successfully Updated ***";
                //lblMessageBottom.Text = "*** Record Successfully Updated ***";
                Response.Redirect("links-list.aspx");
            }
            else
            {
                Response.Redirect(Request.Url.ToString() + "?mode=add-successful&linkID=" + link_id);
            }
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            populateTrainingTypes();
            populateDesignTemplates();
            populateAuthors();

            int curr_user_id = Convert.ToInt32(Context.Items["UserID"]);

            final_manage_url = manage_url + "/public/launch-as-user.aspx?key=" + key + "&userID=" + curr_user_id;

            training_id = Convert.ToInt32(Request.QueryString["trainingID"]);

            if (!String.IsNullOrEmpty(Request.QueryString["trainingID"]))
            {
                populateKeywords(training_id, (int)qSoc_ContentType.Types.Training);
                populateTopics(training_id, (int)qSoc_ContentType.Types.Training);

                ViewState.Add("vsTrainingID", training_id);

                qLrn_Training_View training = new qLrn_Training_View(training_id);

                lblTitle.Text = "Edit Training (ID: " + training.TrainingID + ")";
                txtTitle.Text = training.Title;
                txtDescription.Text = training.Description;
                rblAvailable.SelectedValue = training.Available;
                if (!String.IsNullOrEmpty(Convert.ToString(training.PersonAuthorID)))
                    ddlAuthors.SelectedValue = Convert.ToString(training.PersonAuthorID);

                if (Convert.ToString(Request.QueryString["mode"]) == "add-successful")
                {
                    lblMessage.Text = "*** Record Successfully Added ***";
                }

                ddlTrainingTypes.Enabled = false;
                ddlTrainingTypes.SelectedValue = Convert.ToString(training.TrainingTypeID);
                lblTrainingType.Text = "<i>NOTE: Once set, this cannot be changed</i>";
                if (training.TrainingTypeName == "Internal")
                {
                    plhInternalTraining.Visible = true;
                    ddlDesignTemplates.SelectedValue = Convert.ToString(training.DesignThemeID);
                    rfvTrainingLink.Enabled = false;
                }
                else if (training.TrainingTypeName == "External")
                {
                    plhExternalTraining.Visible = true;
                    rfvDesignTemplate.Enabled = false;
                }
                else if (training.TrainingTypeName == "In Person")
                {
                    plhInPersonTraining.Visible = true;
                }
                plhMetaData.Visible = true;

                // see if in feed
                qSoc_Feed feed = new qSoc_Feed((int)qSoc_ContentType.Types.Training, training_id);
                plhExistingFeedItem.Visible = false;

                if (feed != null)
                {
                    if (feed.FeedID > 0 && feed.MarkAsDelete == 0 && feed.VisibleFeed == true)
                    {
                        chkDisplayInFeed.Checked = true;
                        plhExistingFeedItem.Visible = true;
                    }
                    if (feed.FeedID > 0 && feed.MarkAsDelete == 0 && feed.VisibleExplore == true)
                    {
                        chkDisplayInExplore.Checked = true;
                    }
                    string reserved_keywords = string.Empty;
                    if (!string.IsNullOrEmpty(feed.ReservedKeywords))
                    {
                        reserved_keywords = feed.ReservedKeywords;
                    }
                    if (feed.FeedID > 0)
                    {
                        btnMakeAvailableCampaigns.Visible = false;
                        lblExistsFeed.Text = "<i class=\"icon-check\"></i> This training is available for use in campaigns";
                    }
                }
            }
            else
            {
                lblTitle.Text = "New Training";
                btnDelete.Visible = false;
                rblAvailable.SelectedValue = "No";
                plhTools.Visible = false;
                populateKeywords(0, (int)qSoc_ContentType.Types.Training);
                plhMetaData.Visible = false;
            }
        }

        if (String.IsNullOrEmpty(Convert.ToString(training_id)))
            training_id = (Int32)ViewState["vsTrainingID"];
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {

            plhURL.Visible = false;

            populateThemes();
            populateAuthors();

            if (!String.IsNullOrEmpty(Request.QueryString["linkID"]))
            {
                link_id = Convert.ToInt32(Request.QueryString["linkID"]);

                populateKeywords(link_id, (int)qSoc_ContentType.Types.Link);
                populateTopics(link_id, (int)qSoc_ContentType.Types.Link);

                qPtl_Link link = new qPtl_Link(link_id);

                lblTitle.Text = "Edit Link (ID: " + link.LinkID + ")";
                txtName.Text = link.Title;
                txtSummary.Text = link.Description;
                txtURL.Text = link.URL;
                rblAvailable.SelectedValue = link.Available;
                if (!String.IsNullOrEmpty(Convert.ToString(link.AuthorID)))
                    ddlAuthor.SelectedValue = Convert.ToString(link.AuthorID);
                if (!String.IsNullOrEmpty(Convert.ToString(link.ThemeID)))
                    ddlTheme.SelectedValue = Convert.ToString(link.ThemeID);
                ddlLanguage.SelectedValue = link.Language;
                ddlLinkType.SelectedValue = link.LinkType;

                ddlType.SelectedValue = link.Type;
                if (link.Type == "Internal Document")
                {
                    btnEnableDocumentTools.Visible = true;
                    plhURL.Visible = true;
                    txtURL.Enabled = false;
                    plhDocumentTools.Visible = false;
                }
                else
                {
                    btnEnableDocumentTools.Visible = false;
                    plhURL.Visible = true;
                    plhDocumentTools.Visible = false;
                }

                // see if in feed
                qSoc_Feed feed = new qSoc_Feed((int)qSoc_ContentType.Types.Link, link_id);
                plhExistingFeedItem.Visible = false;

                if (feed != null)
                {
                    if (feed.FeedID > 0 && feed.MarkAsDelete == 0 && feed.VisibleFeed == true)
                    {
                        chkDisplayInFeed.Checked = true;
                        plhExistingFeedItem.Visible = true;
                    }
                    if (feed.FeedID > 0 && feed.MarkAsDelete == 0 && feed.VisibleExplore == true)
                    {
                        chkDisplayInExplore.Checked = true;
                    }
                    string reserved_keywords = string.Empty;
                    if (!string.IsNullOrEmpty(feed.ReservedKeywords))
                    {
                        reserved_keywords = feed.ReservedKeywords;
                    }
                }
            }
            else
            {
                lblTitle.Text = "New link";
                plhTools.Visible = false;
                rblAvailable.SelectedValue = "Yes";
                populateKeywords(0, (int)qSoc_ContentType.Types.Article);
            }

            if (Convert.ToString(Request.QueryString["mode"]) == "add-successful")
            {
                lblMessage.Text = "*** Record Successfully Added ***";
                lblMessageBottom.Text = "*** Record Successfully Added ***";
            }
        }
    }
    protected void btnSave_OnClick(object sender, System.EventArgs e)
    {
        Page.Validate("form");

        if (Page.IsValid)
        {

            int user_id = Convert.ToInt32(Context.Items["UserID"]);

            if (!String.IsNullOrEmpty(Request.QueryString["tipID"]))
            {
                tip_id = Convert.ToInt32(Request.QueryString["tipID"]);
                qSoc_Tip tip = new qSoc_Tip(tip_id);
                tip.Name = txtTitle.Text;
                tip.Summary = txtSummary.Text;
                tip.Text = txtTip.Text;
                tip.LearnMoreURL = txtURL.Text;
                tip.LastModified = DateTime.Now;
                tip.LastModifiedBy = user_id;
                tip.Type = ddlTipType.SelectedValue;
                if (!String.IsNullOrEmpty(ddlTheme.SelectedValue))
                    tip.ThemeID = Convert.ToInt32(ddlTheme.SelectedValue);
                tip.Available = rblAvailable.SelectedValue;
                tip.Author = txtAuthor.Text;
                tip.Update();
            }
            else
            {
                qSoc_Tip tip = new qSoc_Tip();
                tip.ScopeID = 1;
                tip.Created = DateTime.Now;
                tip.CreatedBy = user_id;
                tip.LastModified = DateTime.Now;
                tip.LastModifiedBy = user_id;
                tip.Available = "Yes";
                tip.MarkAsDelete = 0;
                tip.Name = txtTitle.Text;
                tip.Summary = txtSummary.Text;
                tip.Text = txtTip.Text;
                tip.LearnMoreURL = txtURL.Text;
                tip.LastModified = DateTime.Now;
                tip.LastModifiedBy = user_id;
                tip.Type = ddlTipType.SelectedValue;
                if (!String.IsNullOrEmpty(ddlTheme.SelectedValue))
                    tip.ThemeID = Convert.ToInt32(ddlTheme.SelectedValue);
                tip.Available = rblAvailable.SelectedValue;
                tip.Author = txtAuthor.Text;
                tip.Insert();

                tip_id = tip.TipID;
            }

            string user_name = (new qPtl_User(user_id)).UserName;

            // add keywords
            string owner_keywords = string.Empty;
            qPtl_KeywordReference.DeleteKeywordReferencesByContent((int)qSoc_ContentType.Types.Tip, tip_id);
            foreach (ListItem item in cblKeywords.Items)
            {
                if (item.Selected)
                {
                    if (!String.IsNullOrEmpty(owner_keywords))
                        owner_keywords += "," + item.Text;
                    else
                        owner_keywords += item.Text;
                    qPtl_KeywordReference keyword = new qPtl_KeywordReference();
                    keyword.Available = "Yes";
                    keyword.ScopeID = Convert.ToInt32(Context.Items["ScopeID"]);
                    keyword.KeywordID = Convert.ToInt32(item.Value);
                    keyword.ContentTypeID = (int)qSoc_ContentType.Types.Tip;
                    keyword.ReferenceID = tip_id;
                    keyword.Created = DateTime.Now;
                    keyword.LastModified = DateTime.Now;
                    keyword.CreatedBy = Convert.ToInt32(Context.Items["UserID"]);
                    keyword.LastModifiedBy = Convert.ToInt32(Context.Items["UserID"]);
                    keyword.MarkAsDelete = 0;
                    keyword.Insert();
                }
            }

            string reserved_keywords = string.Empty;
            foreach (ListItem item in chkTopics.Items)
            {
                if (item.Selected)
                {
                    if (!String.IsNullOrEmpty(reserved_keywords))
                        reserved_keywords += "," + item.Text;
                    else
                        reserved_keywords += item.Text;
                }
            }

            qSoc_Feed feed = new qSoc_Feed((int)qSoc_ContentType.Types.Tip, tip_id);

            if (!chkDisplayInFeed.Checked && !chkDisplayInExplore.Checked)
            {
                if (feed != null)
                {
                    if (feed.FeedID > 0)
                    {
                        feed.MarkAsDelete = 1;
                        feed.Available = "No";
                        feed.ReservedKeywords = reserved_keywords;
                        if (!chkDisplayInFeed.Checked)
                            feed.VisibleFeed = false;
                        if (!chkDisplayInExplore.Checked)
                            feed.VisibleExplore = false;
                        feed.Update();
                    }
                }
            }
            else if (chkDisplayInFeed.Checked || chkDisplayInExplore.Checked || !string.IsNullOrEmpty(reserved_keywords))
            {
                qSoc_Tip tip = new qSoc_Tip(tip_id);

                int p_user_id = user_id;
                if (post_as_user_id > 0)
                    p_user_id = post_as_user_id;

                qPtl_User user = new qPtl_User(p_user_id);
                var u_space = qSoc_UserSpace_View.GetUserSpaces(tip.CreatedBy);

                // evaluate title and description
                string p_title = q_Helper.replaceSpecialCharacters(tip.Name);
                string p_description = q_Helper.replaceSpecialCharacters(tip.Text);

                if (feed != null)
                {
                    if (feed.FeedID > 0)
                    {
                        feed.CreatedBy = user.UserID;
                        feed.Available = tip.Available;
                        if (chkMoveToTop.Checked)
                            feed.LastModified = DateTime.Now;
                        feed.LastModifiedBy = user.UserID;
                        feed.MarkAsDelete = tip.MarkAsDelete;
                        feed.OwnerMarkAsDelete = tip.MarkAsDelete;
                        feed.ContentTypeID = (int)qSoc_ContentType.Types.Tip;
                        feed.ReferenceID = tip_id;
                        feed.OwnerID = user.UserID;
                        feed.OwnerName = user.UserName;
                        feed.OwnerProfilePic = user.ProfilePict;
                        feed.Type = "tip";
                        feed.Title = p_title;
                        feed.Description = p_description;
                        feed.Body = p_description;
                        feed.OwnerRole = user.HighestRole;
                        feed.OwnerRoleID = user.HighestRank;
                        feed.OwnerKeywords = owner_keywords;
                        feed.ReservedKeywords = reserved_keywords;
                        feed.UploadedFrom = "manager";
                        if (!String.IsNullOrEmpty(ddlTheme.SelectedValue))
                        {
                            feed.ThemeID = Convert.ToInt32(ddlTheme.SelectedValue);
                            feed.ThemeName = Convert.ToString(ddlTheme.SelectedItem);
                        }
                        if (chkDisplayInFeed.Checked)
                            feed.VisibleFeed = true;
                        else
                            feed.VisibleFeed = false;
                        if (chkDisplayInExplore.Checked)
                            feed.VisibleExplore = true;
                        else
                            feed.VisibleExplore = false;
                        feed.Update();
                    }
                    else
                    {
                        // create new feed item
                        if (u_space != null)
                        {
                            foreach (var s in u_space)
                            {
                                feed.SpaceID = s.SpaceID;
                                feed.SpaceName = s.SpaceShortName;
                            }
                        }
                        feed.ScopeID = Convert.ToInt32(Context.Items["ScopeID"]);
                        feed.Available = tip.Available;
                        feed.Created = DateTime.Now;
                        feed.CreatedBy = user.UserID;
                        feed.LastModified = DateTime.Now;
                        feed.LastModifiedBy = user.UserID;
                        feed.MarkAsDelete = tip.MarkAsDelete;
                        feed.OwnerMarkAsDelete = tip.MarkAsDelete;
                        feed.ContentTypeID = (int)qSoc_ContentType.Types.Tip;
                        feed.ReferenceID = tip_id;
                        feed.OwnerID = user.UserID;
                        feed.OwnerName = user.UserName;
                        feed.OwnerProfilePic = user.ProfilePict;
                        feed.Type = "tip";
                        feed.Title = p_title;
                        feed.Description = p_description;
                        feed.Body = p_description;
                        feed.OwnerRole = user.HighestRole;
                        feed.OwnerRoleID = user.HighestRank;

                        feed.VisibleAll = true;
                        feed.VisibleFriends = true;
                        feed.VisibleFollowers = true;
                        feed.VisibleSpace = true;
                        feed.VisiblePrivate = true;
                        feed.VisibleFeed = true;
                        feed.VisibleOwnerFeed = true;
                        feed.VisibleOwnerProfile = true;
                        if (!String.IsNullOrEmpty(ddlTheme.SelectedValue))
                        {
                            feed.ThemeID = Convert.ToInt32(ddlTheme.SelectedValue);
                            feed.ThemeName = Convert.ToString(ddlTheme.SelectedItem);
                        }
                        feed.ReservedKeywords = reserved_keywords;
                        if (!String.IsNullOrEmpty(owner_keywords))
                        {
                            feed.OwnerKeywords = owner_keywords;
                        }
                        feed.UploadedFrom = "manager";
                        if (chkDisplayInFeed.Checked)
                            feed.VisibleFeed = true;
                        else
                            feed.VisibleFeed = false;
                        if (chkDisplayInExplore.Checked)
                            feed.VisibleExplore = true;
                        else
                            feed.VisibleExplore = false;
                        feed.Insert();
                    }
                }
            }

            if (!String.IsNullOrEmpty(Request.QueryString["tipID"]))
            {
                lblMessage.Text = "*** Record Successfully Updated ***";
                lblMessageBottom.Text = "*** Record Successfully Updated ***";
                if (Request.QueryString["edit-mode"] == "in-place")
                    Response.Redirect(Request.QueryString["returnURL"]);
                else
                    Response.Redirect("tips-list.aspx");
            }
            else
            {
                Response.Redirect(Request.Url.ToString() + "?mode=add-successful&tipID=" + tip_id);
            }
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            populateThemes();

            if (!String.IsNullOrEmpty(Request.QueryString["tipID"]))
            {
                tip_id = Convert.ToInt32(Request.QueryString["tipID"]);
                ViewState.Add("vsTipID", tip_id);

                populateKeywords(tip_id, (int)qSoc_ContentType.Types.Tip);
                populateTopics(tip_id, (int)qSoc_ContentType.Types.Tip);

                qSoc_Tip tip = new qSoc_Tip(tip_id);
                qSoc_ContentType content = new qSoc_ContentType((int)qSoc_ContentType.Types.Tip);

                lblTitle.Text = "Edit Tip (ID: " + tip.TipID + ")";
                txtTitle.Text = tip.Name;
                txtSummary.Text = tip.Summary;
                txtTip.Text = tip.Text;
                txtURL.Text = tip.LearnMoreURL;
                txtAuthor.Text = tip.Author;
                ddlTipType.SelectedValue = tip.Type;
                if (!String.IsNullOrEmpty(Convert.ToString(tip.ThemeID)))
                    ddlTheme.SelectedValue = Convert.ToString(tip.ThemeID);

                rblAvailable.SelectedValue = tip.Available;

                if (Convert.ToString(Request.QueryString["mode"]) == "add-successful")
                {
                    lblMessage.Text = "*** Record Successfully Added ***";
                }

                // see if in feed
                qSoc_Feed feed = new qSoc_Feed((int)qSoc_ContentType.Types.Tip, tip_id);
                plhExistingFeedItem.Visible = false;

                if (feed != null)
                {
                    if (feed.FeedID > 0 && feed.MarkAsDelete == 0 && feed.VisibleFeed == true)
                    {
                        chkDisplayInFeed.Checked = true;
                        plhExistingFeedItem.Visible = true;
                    }
                    if (feed.FeedID > 0 && feed.MarkAsDelete == 0 && feed.VisibleExplore == true)
                    {
                        chkDisplayInExplore.Checked = true;
                    }
                    string reserved_keywords = string.Empty;
                    if (!string.IsNullOrEmpty(feed.ReservedKeywords))
                    {
                        reserved_keywords = feed.ReservedKeywords;
                    }
                }
            }

            else
            {
                lblTitle.Text = "New Article";
                btnDelete.Visible = false;
                populateKeywords(0, (int)qSoc_ContentType.Types.Article);
                rblAvailable.SelectedValue = "Yes";
                plhTools.Visible = false;
            }
        }

        if (String.IsNullOrEmpty(Convert.ToString(tip_id)))
            tip_id = (Int32)ViewState["vsTipID"];
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {

            if (!String.IsNullOrEmpty(Request.QueryString["commentID"]))
            {
                comment_id = Convert.ToInt32(Request.QueryString["commentID"]);
                ViewState.Add("vsCommentID", comment_id);

                populateKeywords(comment_id, (int)qSoc_ContentType.Types.Tip);

                qSoc_Comment2 comment = new qSoc_Comment2(comment_id);
                qSoc_ContentType content = new qSoc_ContentType((int)qSoc_ContentType.Types.Shout);
                qPtl_User posted_by = new qPtl_User(comment.ActorID);

                lblTitle.Text = "Edit Thought (ID: " + comment.CommentID + ")";
                lblPostedBy.Text = posted_by.UserName;
                txtComment.Text = comment.Comment;

                rblAvailable.SelectedValue = comment.Available;

                // see if in feed
                qSoc_Feed feed = new qSoc_Feed((int)qSoc_ContentType.Types.Shout, comment_id);

                if (feed != null)
                {
                    if (feed.FeedID > 0 && feed.Available == "Yes")
                    {
                        btnMakeAvailableCampaigns.Visible = false;
                        lblExistsFeed.Text = "<i class=\"icon-check\"></i> This training is available for use in campaigns";
                    }
                }
            }
        }

        if (String.IsNullOrEmpty(Convert.ToString(comment_id)))
            comment_id = (Int32)ViewState["vsCommentID"];
    }
    protected void btnSave_OnClick(object sender, System.EventArgs e)
    {
        int user_id = Convert.ToInt32(Context.Items["UserID"]);

        if (!String.IsNullOrEmpty(Request.QueryString["BlogID"]))
        {
            blog_id = Convert.ToInt32(Request.QueryString["BlogID"]);
            qSoc_Blog2 blog = new qSoc_Blog2(blog_id);
            blog.Title = txtTitle.Text;
            blog.Summary = txtSummary.Text;
            blog.Text = reContent.Content;
            blog.LastModified = DateTime.Now;
            blog.LastModifiedBy = user_id;
            if (!String.IsNullOrEmpty(ddlAuthor.SelectedValue))
                blog.AuthorID = Convert.ToInt32(ddlAuthor.SelectedValue);
            if (!String.IsNullOrEmpty(ddlTheme.SelectedValue))
                blog.ThemeID = Convert.ToInt32(ddlTheme.SelectedValue);
            blog.Available = rblAvailable.SelectedValue;
            blog.Type = rblType.SelectedValue;
            blog.ApprovedStatus = ddlStatus.SelectedValue;
            if (ddlStatus.SelectedValue == "Approved")
            {
                blog.ApprovedBy = user_id;
                blog.Approved = DateTime.Now;
            }
            blog.Update();
        }
        else
        {
            qSoc_Blog2 blog = new qSoc_Blog2();
            blog.ScopeID = 1;
            blog.Created = DateTime.Now;
            blog.CreatedBy = user_id;
            blog.LastModified = DateTime.Now;
            blog.LastModifiedBy = user_id;
            blog.Available = "Yes";
            blog.MarkAsDelete = 0;
            blog.UserID = user_id;
            blog.Title = txtTitle.Text;
            blog.Summary = txtSummary.Text;
            blog.Text = reContent.Content;
            blog.LastModified = DateTime.Now;
            blog.LastModifiedBy = user_id;
            if (!String.IsNullOrEmpty(ddlAuthor.SelectedValue))
                blog.AuthorID = Convert.ToInt32(ddlAuthor.SelectedValue);
            if (!String.IsNullOrEmpty(ddlTheme.SelectedValue))
                blog.ThemeID = Convert.ToInt32(ddlTheme.SelectedValue);
            blog.Available = rblAvailable.SelectedValue;
            blog.Type = rblType.SelectedValue;
            blog.ApprovedStatus = ddlStatus.SelectedValue;
            if (ddlStatus.SelectedValue == "Approved")
            {
                blog.ApprovedBy = user_id;
                blog.Approved = DateTime.Now;
            }
            blog.UploadedFrom = "manager";
            blog.Insert();

            blog_id = blog.BlogID;
        }

        string user_name = (new qPtl_User(user_id)).UserName;

        // add keywords
        string owner_keywords = string.Empty;
        qPtl_KeywordReference.DeleteKeywordReferencesByContent((int)qSoc_ContentType.Types.Story, blog_id);
        foreach (ListItem item in cblKeywords.Items)
        {
            if (item.Selected)
            {
                if (!String.IsNullOrEmpty(owner_keywords))
                    owner_keywords += "," + item.Text;
                else
                    owner_keywords += item.Text;
                qPtl_KeywordReference keyword = new qPtl_KeywordReference();
                keyword.Available = "Yes";
                keyword.ScopeID = Convert.ToInt32(Context.Items["ScopeID"]);
                keyword.KeywordID = Convert.ToInt32(item.Value);
                keyword.ContentTypeID = (int)qSoc_ContentType.Types.Story;
                keyword.ReferenceID = blog_id;
                keyword.Created = DateTime.Now;
                keyword.LastModified = DateTime.Now;
                keyword.CreatedBy = Convert.ToInt32(Context.Items["UserID"]);
                keyword.LastModifiedBy = Convert.ToInt32(Context.Items["UserID"]);
                keyword.MarkAsDelete = 0;
                keyword.Insert();
            }
        }

        string reserved_keywords = string.Empty;
        foreach (ListItem item in chkTopics.Items)
        {
            if (item.Selected)
            {
                if (!String.IsNullOrEmpty(reserved_keywords))
                    reserved_keywords += "," + item.Text;
                else
                    reserved_keywords += item.Text;
            }
        }

        // process if highlighted blog
        qDbs_SQLcode sql = new qDbs_SQLcode();
        string sqlCode = string.Empty;
        sqlCode = "UPDATE qSoc_Blogs SET Highlighted = null";

        if (lblHighlightedMessage.Text.Contains("current") && chkHighlightedItem.Checked == false)
        {
            sql.ExecuteSQL(sqlCode);        // clear all highlighted options
        }
        else if (chkHighlightedItem.Checked && lblHighlightedMessage.Text.Contains("current"))
        {
            sql.ExecuteSQL(sqlCode);        // clear all highlighted options
            qSoc_Blog2 curr_item = new qSoc_Blog2(blog_id);
            curr_item.Highlighted = true;
            curr_item.Update();
        }
        else if (chkHighlightedItem.Checked && lblHighlightedMessage.Text.Contains("Check"))
        {
            sql.ExecuteSQL(sqlCode);        // clear all highlighted options
            qSoc_Blog2 blog = new qSoc_Blog2(blog_id);
            blog.Highlighted = true;
            blog.Update();
        }

        qSoc_Feed feed = new qSoc_Feed((int)qSoc_ContentType.Types.Story, blog_id);

        if (!chkDisplayInFeed.Checked && !chkDisplayInExplore.Checked)
        {
            if (feed != null)
            {
                if (feed.FeedID > 0)
                {
                    feed.MarkAsDelete = 1;
                    feed.Available = "No";
                    feed.ReservedKeywords = reserved_keywords;
                    if (!chkDisplayInFeed.Checked)
                        feed.VisibleFeed = false;
                    if (!chkDisplayInExplore.Checked)
                        feed.VisibleExplore = false;
                    feed.Update();
                }
            }
        }
        else if (chkDisplayInFeed.Checked || chkDisplayInExplore.Checked || !string.IsNullOrEmpty(reserved_keywords))
        {
            qSoc_Blog2 blog = new qSoc_Blog2(blog_id);

            int p_user_id = user_id;
            if (post_as_user_id > 0)
                p_user_id = post_as_user_id;

            qPtl_User user = new qPtl_User(p_user_id);
            var u_space = qSoc_UserSpace_View.GetUserSpaces(blog.CreatedBy);

            // evaluate title and description
            string p_title = blog.Title.Replace("'", "\"");
            p_title = q_Helper.replaceSpecialCharacters(p_title);
            string p_description = q_Helper.replaceSpecialCharacters(blog.Summary);

            if (feed != null)
            {
                if (feed.FeedID > 0)
                {
                    feed.CreatedBy = user.UserID;
                    feed.Available = blog.Available;
                    if (chkMoveToTop.Checked)
                        feed.LastModified = DateTime.Now;
                    feed.LastModifiedBy = user.UserID;
                    feed.MarkAsDelete = blog.MarkAsDelete;
                    feed.OwnerMarkAsDelete = blog.MarkAsDelete;
                    feed.ContentTypeID = (int)qSoc_ContentType.Types.Story;
                    feed.ReferenceID = blog_id;
                    feed.OwnerID = user.UserID;
                    feed.OwnerName = user.UserName;
                    feed.OwnerProfilePic = user.ProfilePict;
                    feed.Type = "blog";
                    feed.Title = p_title;
                    feed.Description = p_description;
                    feed.Body = p_description;
                    feed.OwnerRole = user.HighestRole;
                    feed.OwnerRoleID = user.HighestRank;
                    feed.OwnerKeywords = owner_keywords;
                    feed.ReservedKeywords = reserved_keywords;
                    feed.UploadedFrom = "manager";
                    if (!String.IsNullOrEmpty(ddlTheme.SelectedValue))
                    {
                        feed.ThemeID = Convert.ToInt32(ddlTheme.SelectedValue);
                        feed.ThemeName = Convert.ToString(ddlTheme.SelectedItem);
                    }
                    if (chkDisplayInFeed.Checked)
                        feed.VisibleFeed = true;
                    else
                        feed.VisibleFeed = false;
                    if (chkDisplayInExplore.Checked)
                        feed.VisibleExplore = true;
                    else
                        feed.VisibleExplore = false;
                    feed.Update();
                }
                else
                {
                    // create new feed item
                    if (u_space != null)
                    {
                        foreach (var s in u_space)
                        {
                            feed.SpaceID = s.SpaceID;
                            feed.SpaceName = s.SpaceShortName;
                        }
                    }
                    feed.ScopeID = Convert.ToInt32(Context.Items["ScopeID"]);
                    feed.Available = blog.Available;
                    feed.Created = DateTime.Now;
                    feed.CreatedBy = user.UserID;
                    feed.LastModified = DateTime.Now;
                    feed.LastModifiedBy = user.UserID;
                    feed.MarkAsDelete = blog.MarkAsDelete;
                    feed.OwnerMarkAsDelete = blog.MarkAsDelete;
                    feed.ContentTypeID = (int)qSoc_ContentType.Types.Story;
                    feed.ReferenceID = blog_id;
                    feed.OwnerID = user.UserID;
                    feed.OwnerName = user.UserName;
                    feed.OwnerProfilePic = user.ProfilePict;
                    feed.Type = "blog";
                    feed.Title = p_title;
                    feed.Description = p_description;
                    feed.Body = p_description;
                    feed.OwnerRole = user.HighestRole;
                    feed.OwnerRoleID = user.HighestRank;

                    feed.VisibleAll = true;
                    feed.VisibleFriends = true;
                    feed.VisibleFollowers = true;
                    feed.VisibleSpace = true;
                    feed.VisiblePrivate = true;
                    feed.VisibleFeed = true;
                    feed.VisibleOwnerFeed = true;
                    feed.VisibleOwnerProfile = true;
                    if (!String.IsNullOrEmpty(ddlTheme.SelectedValue))
                    {
                        feed.ThemeID = Convert.ToInt32(ddlTheme.SelectedValue);
                        feed.ThemeName = Convert.ToString(ddlTheme.SelectedItem);
                    }
                    feed.ReservedKeywords = reserved_keywords;
                    if (!String.IsNullOrEmpty(owner_keywords))
                    {
                        feed.OwnerKeywords = owner_keywords;
                    }
                    feed.UploadedFrom = "manager";
                    if (chkDisplayInFeed.Checked)
                        feed.VisibleFeed = true;
                    else
                        feed.VisibleFeed = false;
                    if (chkDisplayInExplore.Checked)
                        feed.VisibleExplore = true;
                    else
                        feed.VisibleExplore = false;
                    feed.Insert();
                }
            }
        }

        //Response.Redirect("~/qSoc/blogs-list.aspx");
        if (!String.IsNullOrEmpty(Request.QueryString["BlogID"]))
        {
            lblMessage.Text = "*** Record Successfully Updated ***";
            lblMessageBottom.Text = "*** Record Successfully Updated ***";
        }
        else
        {
            if (!String.IsNullOrEmpty(Request.QueryString["type"]))
                Response.Redirect(Request.Url.ToString() + "&mode=add-successful&BlogID=" + blog_id);
            else
                Response.Redirect(Request.Url.ToString() + "?mode=add-successful&BlogID=" + blog_id);
        }
    }
    protected void btnSave_OnClick(object sender, System.EventArgs e)
    {
        Page.Validate("form");

        if (Page.IsValid)
        {

            int user_id = Convert.ToInt32(Context.Items["UserID"]);

            if (!String.IsNullOrEmpty(Request.QueryString["trainingID"]))
            {
                training_id = Convert.ToInt32(Request.QueryString["trainingID"]);
                qLrn_Training training = new qLrn_Training(training_id);
                training.Title = txtTitle.Text;
                training.Description = txtDescription.Text;
                training.LastModified = DateTime.Now;
                training.LastModifiedBy = user_id;
                training.Available = rblAvailable.SelectedValue;
                if (!String.IsNullOrEmpty(ddlAuthors.SelectedValue))
                    training.PersonAuthorID = Convert.ToInt32(ddlAuthors.SelectedValue);
                if (Convert.ToString(ddlTrainingTypes.SelectedItem) == "Internal")
                {
                    training.DesignThemeID = Convert.ToInt32(ddlDesignTemplates.SelectedValue);
                }
                else if (Convert.ToString(ddlTrainingTypes.SelectedItem) == "External")
                {
                    training.Link = txtLink.Text;
                }
                else if (Convert.ToString(ddlTrainingTypes.SelectedItem) == "In Person")
                {
                    // do something
                }
                training.Update();
            }
            else
            {
                qLrn_Training training = new qLrn_Training();
                training.ScopeID = Convert.ToInt32(Context.Items["ScopeID"]);
                training.Created = DateTime.Now;
                training.CreatedBy = user_id;
                training.LastModified = DateTime.Now;
                training.LastModifiedBy = user_id;
                training.Available = "Yes";
                training.MarkAsDelete = 0;
                training.Title = txtTitle.Text;
                training.Description = txtDescription.Text;
                training.LastModified = DateTime.Now;
                training.LastModifiedBy = user_id;
                training.Available = rblAvailable.SelectedValue;
                training.TrainingTypeID = Convert.ToInt32(ddlTrainingTypes.SelectedValue);
                if (!String.IsNullOrEmpty(ddlAuthors.SelectedValue))
                    training.PersonAuthorID = Convert.ToInt32(ddlAuthors.SelectedValue);
                if (Convert.ToString(ddlTrainingTypes.SelectedItem) == "Internal")
                {
                    training.DesignThemeID = Convert.ToInt32(ddlDesignTemplates.SelectedValue);
                }
                else if (Convert.ToString(ddlTrainingTypes.SelectedItem) == "External")
                {
                    training.Link = txtLink.Text;
                }
                else if (Convert.ToString(ddlTrainingTypes.SelectedItem) == "In Person")
                {
                    // do something
                }
                training.Insert();

                training_id = training.TrainingID;
            }

            // add keywords
            string owner_keywords = string.Empty;
            qPtl_KeywordReference.DeleteKeywordReferencesByContent((int)qSoc_ContentType.Types.Training, training_id);
            foreach (ListItem item in cblKeywords.Items)
            {
                if (item.Selected)
                {
                    if (!String.IsNullOrEmpty(owner_keywords))
                        owner_keywords += "," + item.Text;
                    else
                        owner_keywords += item.Text;
                    qPtl_KeywordReference keyword = new qPtl_KeywordReference();
                    keyword.Available = "Yes";
                    keyword.ScopeID = Convert.ToInt32(Context.Items["ScopeID"]);
                    keyword.KeywordID = Convert.ToInt32(item.Value);
                    keyword.ContentTypeID = (int)qSoc_ContentType.Types.Training;
                    keyword.ReferenceID = training_id;
                    keyword.Created = DateTime.Now;
                    keyword.LastModified = DateTime.Now;
                    keyword.CreatedBy = Convert.ToInt32(Context.Items["UserID"]);
                    keyword.LastModifiedBy = Convert.ToInt32(Context.Items["UserID"]);
                    keyword.MarkAsDelete = 0;
                    keyword.Insert();
                }
            }

            string reserved_keywords = string.Empty;
            foreach (ListItem item in chkTopics.Items)
            {
                if (item.Selected)
                {
                    if (!String.IsNullOrEmpty(reserved_keywords))
                        reserved_keywords += "," + item.Text;
                    else
                        reserved_keywords += item.Text;
                }
            }

            qSoc_Feed feed = new qSoc_Feed((int)qSoc_ContentType.Types.Training, training_id);

            if (!chkDisplayInFeed.Checked && !chkDisplayInExplore.Checked)
            {
                if (feed != null)
                {
                    if (feed.FeedID > 0)
                    {
                        if (feed.Type == "training" && feed.Available == "Yes" && feed.VisibleFeed == false && feed.VisibleExplore == false)
                        {
                            // situation when the training needs to be available for campaigns but is not visible otherwise
                            feed.MarkAsDelete = 1;
                            feed.Available = "Yes";
                            feed.VisibleFeed = false;
                            feed.VisibleExplore = false;
                        }
                        else
                        {
                            feed.MarkAsDelete = 1;
                            feed.Available = "No";
                            feed.ReservedKeywords = reserved_keywords;
                            if (!chkDisplayInFeed.Checked)
                                feed.VisibleFeed = false;
                            if (!chkDisplayInExplore.Checked)
                                feed.VisibleExplore = false;
                        }

                        feed.Update();
                    }
                }
            }
            else if (chkDisplayInFeed.Checked || chkDisplayInExplore.Checked || !string.IsNullOrEmpty(reserved_keywords))
            {
                qLrn_Training training = new qLrn_Training(training_id);

                int p_user_id = user_id;
                if (post_as_user_id > 0)
                    p_user_id = post_as_user_id;

                qPtl_User user = new qPtl_User(p_user_id);
                var u_space = qSoc_UserSpace_View.GetUserSpaces(training.CreatedBy);

                // evaluate title and description
                string p_title = q_Helper.replaceSpecialCharacters(training.Title);
                string p_description = q_Helper.replaceSpecialCharacters(training.Description);

                // add first slide image to the description
                var slides = qLrn_TrainingSlide_View.GetAvailableSlidesInOrder(training.TrainingID);
                string slide_icon = string.Empty;
                int s_count = 0;
                foreach (var s in slides)
                {
                    if (s_count == 0)
                    {
                        string slide_image_path = Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["Learning_SlideIconBasePath"]);
                        if (!String.IsNullOrEmpty(slide_image_path))
                        {
                            slide_icon = "<img src=\"" + slide_image_path + "/" + s.TrainingID + "/" + s.SlideID + ".png?width=200&height=133\">";
                        }

                        s_count++;
                    }
                }
                string body = p_description;
                if (!String.IsNullOrEmpty(slide_icon))
                {
                    body = slide_icon + "<br>" + p_description;
                }

                if (feed != null)
                {
                    if (feed.FeedID > 0)
                    {
                        feed.CreatedBy = user.UserID;
                        feed.Available = training.Available;
                        if (chkMoveToTop.Checked)
                            feed.LastModified = DateTime.Now;
                        feed.LastModifiedBy = user.UserID;
                        feed.MarkAsDelete = training.MarkAsDelete;
                        feed.OwnerMarkAsDelete = training.MarkAsDelete;
                        feed.ContentTypeID = (int)qSoc_ContentType.Types.Training;
                        feed.ReferenceID = training_id;
                        feed.OwnerID = user.UserID;
                        feed.OwnerName = user.UserName;
                        feed.OwnerProfilePic = user.ProfilePict;
                        feed.Type = "training";
                        feed.Title = p_title;
                        feed.Description = p_description;
                        feed.Body = body;
                        feed.OwnerRole = user.HighestRole;
                        feed.OwnerRoleID = user.HighestRank;
                        feed.OwnerKeywords = owner_keywords;
                        feed.ReservedKeywords = reserved_keywords;
                        feed.UploadedFrom = "manager";
                        if (chkDisplayInFeed.Checked)
                            feed.VisibleFeed = true;
                        else
                            feed.VisibleFeed = false;
                        if (chkDisplayInExplore.Checked)
                            feed.VisibleExplore = true;
                        else
                            feed.VisibleExplore = false;
                        feed.Update();
                    }
                    else
                    {
                        // create new feed item
                        if (u_space != null)
                        {
                            foreach (var s in u_space)
                            {
                                feed.SpaceID = s.SpaceID;
                                feed.SpaceName = s.SpaceShortName;
                            }
                        }
                        feed.ScopeID = Convert.ToInt32(Context.Items["ScopeID"]);
                        feed.Available = training.Available;
                        feed.Created = DateTime.Now;
                        feed.CreatedBy = user.UserID;
                        feed.LastModified = DateTime.Now;
                        feed.LastModifiedBy = user.UserID;
                        feed.MarkAsDelete = training.MarkAsDelete;
                        feed.OwnerMarkAsDelete = training.MarkAsDelete;
                        feed.ContentTypeID = (int)qSoc_ContentType.Types.Training;
                        feed.ReferenceID = training_id;
                        feed.OwnerID = user.UserID;
                        feed.OwnerName = user.UserName;
                        feed.OwnerProfilePic = user.ProfilePict;
                        feed.Type = "training";
                        feed.Title = p_title;
                        feed.Description = p_description;
                        feed.Body = p_description;
                        feed.OwnerRole = user.HighestRole;
                        feed.OwnerRoleID = user.HighestRank;

                        feed.VisibleAll = true;
                        feed.VisibleFriends = true;
                        feed.VisibleFollowers = true;
                        feed.VisibleSpace = true;
                        feed.VisiblePrivate = true;
                        feed.VisibleFeed = true;
                        feed.VisibleOwnerFeed = true;
                        feed.VisibleOwnerProfile = true;
                        feed.ReservedKeywords = reserved_keywords;
                        if (!String.IsNullOrEmpty(owner_keywords))
                        {
                            feed.OwnerKeywords = owner_keywords;
                        }
                        feed.UploadedFrom = "manager";
                        if (chkDisplayInFeed.Checked)
                            feed.VisibleFeed = true;
                        else
                            feed.VisibleFeed = false;
                        if (chkDisplayInExplore.Checked)
                            feed.VisibleExplore = true;
                        else
                            feed.VisibleExplore = false;
                        feed.Insert();
                    }
                }
            }

            string user_name = (new qPtl_User(user_id)).UserName;

            if (!String.IsNullOrEmpty(Request.QueryString["trainingID"]))
            {
                lblMessage.Text = "*** Record Successfully Updated ***";
                lblMessageBottom.Text = "*** Record Successfully Updated ***";
                if (Request.QueryString["edit-mode"] == "in-place")
                    Response.Redirect(Request.QueryString["returnURL"]);
                else
                    Response.Redirect("trainings-list.aspx");
            }
            else
            {
                //Response.Redirect(Request.Url.ToString() + "?mode=add-successful&questionCategoryID=" + tip_id);
                Response.Redirect("trainings-list.aspx");
            }
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        video_id = Convert.ToInt32(Request.QueryString["videoID"]);

        if (!Page.IsPostBack)
        {

            populateThemes();
            populateAuthors();
            populateKeywords(video_id, (int)qSoc_ContentType.Types.Video);
            populateTopics(video_id, (int)qSoc_ContentType.Types.Video);

            qSoc_Video video = new qSoc_Video(video_id);
            txtTitle.Text = video.Title;
            txtDescription.Text = video.Description;
            ddlStatus.SelectedValue = video.ApprovedStatus;
            ddlTheme.SelectedValue = Convert.ToString(video.ThemeID);
            ddlAuthor.SelectedValue = Convert.ToString(video.AuthorID);
            owner_id = video.UserID;
            ViewState.Add("vsOwnerID", owner_id);
            rblAvailable.SelectedValue = video.Available;
            txtPreviewImage.Visible = true;
            txtPreviewImage.Text = video.ThumbnailURL;
            txtPreviewImage.Enabled = false;

            qPtl_User posted_by = new qPtl_User(video.UserID);
            owner = posted_by.UserName;
            ViewState.Add("vsOwner", owner);
            qPtl_User approved_by = new qPtl_User(video.ApprovedBy);

            lblApprovedBy.Text = "Approved by " + approved_by.UserName + " at " + video.Approved;
            lblPostedTime.Text = " at " + video.Created;

            lblVideoType.Text = video.Source;

            lblTitle.Text = "Edit Video (ID: " + video.VideoID + ")";

            // see if in feed
            qSoc_Feed feed = new qSoc_Feed((int)qSoc_ContentType.Types.Video, video.VideoID);

            if (feed != null)
            {
                if (feed.FeedID > 0 && feed.MarkAsDelete == 0 && feed.VisibleFeed == true)
                {
                    chkDisplayInFeed.Checked = true;
                    plhExistingFeedItem.Visible = true;
                }
                if (feed.FeedID > 0 && feed.MarkAsDelete == 0 && feed.VisibleExplore == true)
                {
                    chkDisplayInExplore.Checked = true;
                }
                string reserved_keywords = string.Empty;
                if (!string.IsNullOrEmpty(feed.ReservedKeywords))
                {
                    reserved_keywords = feed.ReservedKeywords;
                }
            }

            if (!String.IsNullOrEmpty(video.ThumbnailURL))
            {
                imgThumbnail.Visible = true;
                imgThumbnail.ImageUrl = video.ThumbnailURL;
                imgThumbnail.Width = 100;
            }

            if (!String.IsNullOrEmpty(video.ViddlerID) && video.Source == "internal")
            {
                litVideo.Text = "video successfully uploaded -- embed tags here";
                plhVideo.Visible = true;
                plhExternalVideo.Visible = false;
                plhVideoProcessing.Visible = false;
                viddler_id = video.ViddlerID;
                ViewState.Add("vsViddlerID", viddler_id);
                txtEmbed.Visible = false;
                rfvExternalSource.Enabled = false;
                plhAdditionalExternalInfo.Visible = false;
                btnEnableEmbed.Visible = false;
                rfvEmbedCode.Enabled = false;
                rfvExternalSource.Enabled = false;

                string viddler_embed = "<iframe id=\"Iframe1\" src=\"//www.viddler.com/embed/" + viddler_id + "/?f=1&player=full&secret=90387241\" width=\"347\" height=\"288\" frameborder=\"0\" mozallowfullscreen=\"true\" webkitallowfullscreen=\"true\"></iframe>";

                litViddlerEmbed.Text = viddler_embed;
                txtEmbedTag.Text = viddler_embed;
            }
            else if (video.Source == "external")
            {
                plhVideo.Visible = false;
                plhVideoProcessing.Visible = false;
                plhExternalVideo.Visible = true;
                litExternalEmbed.Text = video.EmbedCode;
                txtEmbed.Text = video.EmbedCode;
                txtEmbed.Enabled = false;
                plhAdditionalExternalInfo.Visible = true;
                txtSourceVideoID.Text = video.SourceVideoID;
                ddlExternalSource.SelectedValue = video.ExternalSourceName;
                btnEnableEmbed.Visible = true;
                if (video.ExternalSourceName == "YouTube")
                {
                    string image_url = "http://img.youtube.com/vi/" + video.SourceVideoID + "/1.jpg";
                    txtPreviewImage.Text = image_url;
                    imgThumbnail.ImageUrl = image_url;
                    txtPreviewImage.Visible = true;
                    imgThumbnail.Visible = true;
                    txtPreviewImage.Enabled = false;
                }
                else
                    imgThumbnail.ImageUrl = video.ThumbnailURL;
            }
            else
            {
                // viddler video before processing is complete -- initial view
                litVideo.Text = "This video is still being processed. Check back again soon...";
                plhVideo.Visible = false;
                plhExternalVideo.Visible = true;
                plhVideoProcessing.Visible = true;
                txtEmbed.Visible = false;
                btnEnableEmbed.Visible = false;
                btnEnableThumbnail.Visible = false;
                txtPreviewImage.Visible = false;
                txtPreviewImage.Enabled = false;
                plhAdditionalExternalInfo.Visible = false;
                rfvExternalSource.Enabled = false;
                txtSourceVideoID.Enabled = false;
                rfvEmbedCode.Enabled = false;
                rfvExternalSource.Enabled = false;
            }

            qSoc_HighlightedVideo high_video = new qSoc_HighlightedVideo();
            high_video = qSoc_HighlightedVideo.GetLatest();

            if (high_video.VideoID == video_id)
            {
                chkHighlightedVideo.Checked = true;
                lblHighlightedMessage.Text = "This is the current highlighted video ID: " + high_video.HighlightedVideoID;
            }
            else
                lblHighlightedMessage.Text = "Check to set as the highlighted video";
        }

        if (String.IsNullOrEmpty(viddler_id))
            viddler_id = (String)ViewState["vsViddlerID"];
        if (String.IsNullOrEmpty(Convert.ToString(owner_id)))
            owner_id = (Int32)ViewState["vsOwnerID"];
        if (String.IsNullOrEmpty(owner))
            owner = (String)ViewState["vsOwner"];
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            int curr_campaign_id = 0;

            if (campaign_id > 0)
                curr_campaign_id = campaign_id;

            if (campaign_id == 0)
                curr_campaign_id = Convert.ToInt32(Request.QueryString["campaignID"]);

            if (curr_campaign_id > 0)
                Session["CampaignID"] = curr_campaign_id;

            if (curr_campaign_id == 0)
                curr_campaign_id = Convert.ToInt32(Session["CampaignID"]);

            int curr_user_id = Convert.ToInt32(Context.Items["UserID"]);

            bool available_actions = false;

            if (curr_campaign_id > 0)
            {
                var actions = qSoc_CampaignAction.GetCampaignActions(campaign_id);

                if (actions != null)
                {
                    if (actions.Count > 0)
                    {
                        available_actions = true;
                        int i = 1;
                        foreach (var a in actions)
                        {
                            Literal action = new Literal();

                            int curr_feed_id = 0;
                            string keyword_title = string.Empty;
                            string activity_type = string.Empty;
                            if (a.FeedID == 0)
                            {
                                keyword_title = a.ActionName;
                                qSoc_ContentType c_type = new qSoc_ContentType(a.ContentTypeID);
                                activity_type = c_type.Name.ToLower();
                            }
                            else
                            {
                                qSoc_Feed feed = new qSoc_Feed(a.FeedID);
                                if (!String.IsNullOrEmpty(feed.Title))
                                    keyword_title = feed.Title.Replace("&#39;", "");
                                curr_feed_id = feed.FeedID;
                                activity_type = feed.Type;
                            }
                            if (!String.IsNullOrEmpty(keyword_title))
                            {
                                keyword_title = Uri.EscapeDataString(keyword_title).Replace("'", @"\'").Replace(@"""", @"\""");
                                keyword_title = keyword_title.Replace("%2C", "");
                            }
                            else
                            {
                                keyword_title = "Activity";
                            }

                            string action_html = "<tr>";

                            action_html += "<td><font color=\"gray\">" + i + ") " + a.ActionName + "</font></td>";
                            action_html += "<td class=\"hidden-1024\">" + a.ActionType + "</td>";
                            action_html += "<td class=\"hidden-480\">" + a.Points + "</td>";
                            action_html += "</tr>";

                            action.Text = action_html;

                            pnlActivities.Controls.Add(action);
                            i++;
                        }
                    }
                }
            }

            // if no actions then add empty table
            if (available_actions == false)
            {
                Literal no_action = new Literal();
                no_action.Text = "<tr><td>No activities available</td><td></td><td></td></tr>";
                pnlActivities.Controls.Add(no_action);
            }
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            rblType.Enabled = false;

            if (Request.QueryString["type"] == "story")
            {
                //litBackTop.Text = "tst";    //<a href="\blogs-list.aspx?type=story\" class=\"btn\"><i class=\"icon-circle-arrow-left\"></i>&nbsp;&nbsp;Back to Stories</a>";
                btnSave_top.Text = "SAVE STORY";
                btnSave.Text = "SAVE STORY";
            }
            else if (Request.QueryString["type"] == "blog")
            {
                //litBackTop.Text = "";
                btnSave_top.Text = "SAVE BLOG";
                btnSave.Text = "SAVE BLOG";
            }
            else
            {
                btnSave_top.Text = "SAVE BLOG";
                btnSave.Text = "SAVE BLOG";
            }

            populateThemes();
            populateAuthors();

            // load styles for this project
            string css_text_file = string.Empty;
            if (!String.IsNullOrEmpty(ConfigurationManager.AppSettings["CmsTextCSS"]))
                css_text_file = Convert.ToString(ConfigurationManager.AppSettings["CmsTextCSS"]);

            reContent.CssFiles.Add(css_text_file);

            if (!String.IsNullOrEmpty(Request.QueryString["BlogID"]))
            {

                blog_id = Convert.ToInt32(Request.QueryString["BlogID"]);
                ViewState.Add("vsBlogID", blog_id);

                populateKeywords(blog_id, (int)qSoc_ContentType.Types.Story);
                populateTopics(blog_id, (int)qSoc_ContentType.Types.Story);

                qSoc_Blog2 blog = new qSoc_Blog2(blog_id);
                qSoc_ContentType content = new qSoc_ContentType((int)qSoc_ContentType.Types.Story);

                qPtl_User posted_by = new qPtl_User(blog.UserID);
                owner = posted_by.UserName;
                ViewState.Add("vsOwner", owner);
                owner_id = blog.CreatedBy;
                ViewState.Add("vsOwnerID", owner_id);
                lblPostedTime.Text = " at " + blog.Created;
                ddlStatus.SelectedValue = blog.ApprovedStatus;

                qPtl_User approved_by = new qPtl_User(blog.ApprovedBy);
                lblApprovedBy.Text = "Approved by " + approved_by.UserName + " at " + blog.Approved;
                lblPostedTime.Text = " at " + blog.Created;

                if (Request.QueryString["type"] == "story")
                    lblTitle.Text = "Edit Story (ID: " + blog.BlogID + ")";
                else
                    lblTitle.Text = "Edit Blog (ID: " + blog.BlogID + ")";

                txtTitle.Text = blog.Title;
                txtSummary.Text = blog.Summary;
                reContent.Content = blog.Text;
                if (!String.IsNullOrEmpty(Convert.ToString(blog.AuthorID)))
                    ddlAuthor.SelectedValue = Convert.ToString(blog.AuthorID);
                if (!String.IsNullOrEmpty(Convert.ToString(blog.ThemeID)))
                    ddlTheme.SelectedValue = Convert.ToString(blog.ThemeID);

                rblAvailable.SelectedValue = blog.Available;
                rblType.SelectedValue = blog.Type;

                plhPostedBy.Visible = true;
                hplPreviewArticle.Visible = false;
                hplPreviewArticle.Target = "_blank";

                if (Convert.ToString(Request.QueryString["mode"]) == "add-successful")
                {
                    lblMessage.Text = "*** Record Successfully Added ***";
                }

                // see if in feed
                qSoc_Feed feed = new qSoc_Feed((int)qSoc_ContentType.Types.Story, blog_id);
                plhExistingFeedItem.Visible = false;

                if (feed != null)
                {
                    if (feed.FeedID > 0 && feed.MarkAsDelete == 0 && feed.VisibleFeed == true)
                    {
                        chkDisplayInFeed.Checked = true;
                        plhExistingFeedItem.Visible = true;
                    }
                    if (feed.FeedID > 0 && feed.MarkAsDelete == 0 && feed.VisibleExplore == true)
                    {
                        chkDisplayInExplore.Checked = true;
                    }
                    string reserved_keywords = string.Empty;
                    if (!string.IsNullOrEmpty(feed.ReservedKeywords))
                    {
                        reserved_keywords = feed.ReservedKeywords;
                    }
                }
            }
            else
            {
                if (Request.QueryString["type"] == "story")
                {
                    lblTitle.Text = "New Story";
                    rblType.SelectedValue = "story";
                }
                else
                {
                    lblTitle.Text = "New Blog";
                    rblType.SelectedValue = "blog";
                }
                btnDelete.Visible = false;
                populateKeywords(0, (int)qSoc_ContentType.Types.Story);
                plhPostedBy.Visible = false;
                rblAvailable.SelectedValue = "Yes";
                ddlStatus.SelectedValue = "Approved";
                plhTools.Visible = false;
            }
        }

        var highlighted_item = qSoc_Blog2.GetHighlightedBlog();

        if (highlighted_item != null)
        {
            if (highlighted_item.BlogID == blog_id)
            {
                chkHighlightedItem.Checked = true;
                lblHighlightedMessage.Text = "This is the current highlighted blog ID: " + highlighted_item.BlogID;
            }
            else
                lblHighlightedMessage.Text = "Check to set as the highlighted blog";
        }
        else
            lblHighlightedMessage.Text = "Check to set as the highlighted blog";

        if (String.IsNullOrEmpty(Convert.ToString(blog_id)))
            blog_id = (Int32)ViewState["vsBlogID"];
        if (String.IsNullOrEmpty(Convert.ToString(owner_id)))
            owner_id = (Int32)ViewState["vsOwnerID"];
        if (String.IsNullOrEmpty(owner))
            owner = (String)ViewState["vsOwner"];
    }