protected void btnDelete_Click(object sender, EventArgs e)
    {
        training_id = Convert.ToInt32(Request.QueryString["trainingID"]);
        qLrn_Training training = new qLrn_Training(training_id);
        training.Available = "No";
        training.MarkAsDelete = 1;
        training.Update();

        if (Request.QueryString["edit-mode"] == "in-place")
            Response.Redirect(Request.QueryString["returnURL"]);
        else
            Response.Redirect("trainings-list.aspx");
    }
    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");
            }
        }
    }