/// Function Header*******************************************************
    /// Function Name: BindDetails
    /// Function Type: Function
    /// Functionality: Function used to bind the details of question
    /// Input: 
    /// Output: 
    /// Return Value:
    /// Note: 
    /// ********************************************************************
    private void BindDetails()
    {
        BLL.CompanyManager oCompanyManager = new BLL.CompanyManager();
        Entity.CompanyInfo oCompanyInfo = new Entity.CompanyInfo();
        if (Request.QueryString["qid"] != null)
        {
            oCompanyInfo = oCompanyManager.GetQuestionsForHealthCoachFromUserByQuestionId(Convert.ToInt32(Request.QueryString["qid"]));
            if (oCompanyInfo != null)
            {
                lblCategory.Text = oCompanyInfo.StrCategoryName + " - " + oCompanyInfo.StrCategoryLevel;
                lblQuestion.Text = oCompanyInfo.StrQuestion;
                lblSubject.Text = oCompanyInfo.StrSubject;
                lblComments.Text = oCompanyInfo.StrComments;
                lblUserId.Text = oCompanyInfo.StrUserName;

                rdoQuestionType.SelectedValue = oCompanyInfo.StrQuestionType;
                lblUserEmail.Text = oCompanyInfo.StrUserName;
                lblUserEmail.Text = oCompanyManager.GetUserDetailsByUsername(lblUserEmail.Text).StrUserFullName + "(" + oCompanyInfo.StrUserName + ")";
                GVAnswer.DataSource = oCompanyManager.GetAnswerofPrivateQuestionByQuestionId(Convert.ToInt32(Request.QueryString["qid"]));
                GVAnswer.DataBind();

                List<Entity.CompanyInfo> Info = oCompanyManager.GetAnswerofPrivateQuestionByQuestionId(Convert.ToInt32(Request.QueryString["qid"]));
                if (Info.Count > 0)
                {
                    txtAnswer.Text = oCompanyManager.GetAnswerofPrivateQuestionByQuestionId(Convert.ToInt32(Request.QueryString["qid"])).ElementAt(0).StrAnswer;

                }

                if (GVAnswer.Rows.Count > 0)
                    trAnswer.Visible = true;
                else
                    trAnswer.Visible = false;
                //
            }
        }

        oCompanyInfo = null;
        oCompanyManager = null;
    }
    /// Function Header*******************************************************
    /// Function Name: SaveDetails
    /// Function Type: Function
    /// Functionality: Function used to save the details of questions with answer provided by admin
    /// Input: 
    /// Output: 
    /// Return Value:
    /// Note: 
    /// ********************************************************************
    private void SaveDetails()
    {
        string strContent = string.Empty;

        BLL.TemplateLib oTemplateLib = new BLL.TemplateLib();

        Entity.CompanyInfo oCompanyInfo = new Entity.CompanyInfo();
        oCompanyInfo.StrUserName = AppLib.GetLoggedInUserName();
        oCompanyInfo.IntQuestionId = Convert.ToInt32(Request.QueryString["qid"]);
        oCompanyInfo.StrAnswer = txtAnswer.Text.Trim();
        oCompanyInfo.DtAnsweredOn = DateTime.Now;
        oCompanyInfo.StrQuestionType = rdoQuestionType.SelectedValue;
        oCompanyInfo.StrAnsweredBy = AppLib.GetLoggedInUserName();
        oCompanyInfo.StrQuestion = lblQuestion.Text;
        oCompanyInfo.StrUserId = lblUserId.Text;
        BLL.CompanyManager oCompanyManager = new BLL.CompanyManager();
        oCompanyManager.SaveAnswerFromHealthCoach(oCompanyInfo);

        strContent = oTemplateLib.GetTemplateDetailsByTemplateTemplateName("HealthCoachSubmitQuestionEmail").TextAreaHTML;

        try
        {

           // strContent = strContent.Replace("[Name]", oCompanyManager.GetUserDetailsByUsername(lblUserEmail.Text).StrUserFullName);
            strContent = strContent.Replace("[Name]", oCompanyManager.GetUserDetailsByUsername(oCompanyInfo.StrUserId).StrUserFullName);

            strContent = strContent.Replace("[Category]", lblCategory.Text);
            strContent = strContent.Replace("[Question]", lblQuestion.Text);
            strContent = strContent.Replace("[Subject]", lblSubject.Text);
            strContent = strContent.Replace("[Comments]", lblComments.Text);
            strContent = strContent.Replace("[QuestionType]", rdoQuestionType.SelectedValue);
            strContent = strContent.Replace("[Answer]", txtAnswer.Text);
            strContent = strContent.Replace("[SiteUrl]", AppConfig.GetBaseSiteUrl());
            strContent = strContent.Replace("[SiteName]", AppConfig.GetSiteName());
            strContent = strContent.Replace("[SitePhone]", AppConfig.GetSITEPHONE());
           // AppLib.SendMailToUser(lblUserEmail.Text, lblSubject.Text, strContent, AppConfig.GetAdminEmail());
           AppLib.SendMailToUser(oCompanyInfo.StrUserId, lblSubject.Text, strContent, AppConfig.GetAdminEmail());
            //AppLib.SendMailToUser(oCompanyInfo.StrUserId, lblSubject.Text, strContent, "*****@*****.**");

            //AppLib.SendMailToUser(AppConfig.GetAdminEmail(), lblSubject.Text, "Dear Admin, Following are the contents that were sent to the user. <br />" + strContent, lblUserEmail.Text);
        }
        catch { }
        finally
        {
            oTemplateLib = null;
            oCompanyManager = null;
            oCompanyInfo = null;
        }

        Response.Redirect(AppConfig.GetBaseSiteUrl() + "BackOffice/UserUtilities/ManageQuestionforHealthCoach.aspx?msg=up", true);
    }
    private void SendContactUsEmail()
    {
        string strContent = string.Empty;

        BLL.TemplateLib oTemplateLib = new BLL.TemplateLib();

        strContent = oTemplateLib.GetTemplateDetailsByTemplateTemplateName("HealthCoachSubmitQuestionEmail").TextAreaHTML;

        Entity.CompanyInfo oCompanyInfo = new Entity.CompanyInfo();
        oCompanyInfo.StrCategoryLevel = ddlLevel.SelectedItem.Text;
        oCompanyInfo.StrUserName = AppLib.GetLoggedInUserName();
        oCompanyInfo.StrCategoryName = ddlCategory.SelectedItem.Text;
        oCompanyInfo.StrQuestion = txtQuestion.Text.Trim();
        oCompanyInfo.StrSubject = txtsubject.Text.Trim();
        oCompanyInfo.StrComments = txtComments.Text.Trim();
        oCompanyInfo.StrQuestionType = rdoQuestionType.SelectedItem.Text;
        oCompanyInfo.StrAnswer = string.Empty;
        oCompanyInfo.DtCreatedOn = DateTime.Now;
        oCompanyInfo.ChrIsAnsweredGiven = 'N';
        oCompanyInfo.ChrIsDeleted = 'N';

        BLL.CompanyManager oCompanyManager = new BLL.CompanyManager();
        oCompanyManager.SaveQuestionToHealthCoachFromUser(oCompanyInfo);

        lblMsg.Text = "Your question has been successfully sent to health coach. Health Coach will approach you soon";
        strContent = strContent.Replace("[Name]", oCompanyManager.GetUserDetailsByUsername(AppLib.GetLoggedInUserName()).StrUserFullName);
        strContent = strContent.Replace("[Email]", AppLib.GetLoggedInUserName());
        strContent = strContent.Replace("[Category]", ddlCategory.SelectedItem.Text + " - " + ddlLevel.SelectedItem.Text);
        strContent = strContent.Replace("[Question]", txtQuestion.Text.Trim());

        strContent = strContent.Replace("[Subject]", txtsubject.Text);
        strContent = strContent.Replace("[Comments]", txtComments.Text);
        strContent = strContent.Replace("[QuestionType]", rdoQuestionType.SelectedItem.Text);

        strContent = strContent.Replace("[SiteUrl]", AppConfig.GetBaseSiteUrl());
        strContent = strContent.Replace("[SiteName]", AppConfig.GetSiteName());
        strContent = strContent.Replace("[SitePhone]", AppConfig.GetSITEPHONE());

        ///Code for Forum

        if (rdoQuestionType.SelectedValue == "Public")
        {

            string msg = txtQuestion.Text;
            msg = msg.Trim();
            msg = msg.Replace("<", "&lt;").Replace(">", "&gt;");
            msg = aspnetforum.Utils.Formatting.FilterBadWords(msg);

            bool isModer = false;

            if (msg == "") return;

            string catgory = ddlCategory.SelectedItem.Text;
            string lvl = ddlLevel.SelectedValue;
            string totalvalue = catgory + " - " + lvl;
            providerFactory = aspnetforum.Utils.DB.CreateDBProviderFactory();
            cn = aspnetforum.Utils.DB.CreateConnection();
            cmd = providerFactory.CreateCommand();
            cmd.Connection = cn;
            this.cn.Open();
            this.cmd.CommandText = "SELECT ForumID FROM Forums WHERE Title='" + totalvalue + "'";

            object _forumID = this.cmd.ExecuteScalar();

            if (_addTopic || _changeTopic) //creating a new topic or editing topic title
            {
                string subj = txtsubject.Text;
                subj = subj.Trim();
                if (subj == "") return;
                subj = aspnetforum.Utils.Formatting.FilterBadWords(subj);

                if (_addTopic)
                {
                    string CurrentUserID = Membership.GetUser().ProviderUserKey.ToString();
                    this.cmd.Parameters.Clear();
                    this.cmd.CommandText = "INSERT INTO ForumTopics (ForumID, UserID, Subject, Visible) " +
                        "VALUES (?, convert(uniqueidentifier, ?), ?, ?)";
                    aspnetforum.Utils.DB.FillCommandParamaters(ref cmd, _forumID, CurrentUserID, subj, !_premoderated);
                    this.cmd.ExecuteNonQuery();
                    this.cmd.Parameters.Clear();

                    this.cmd.CommandText = "SELECT MAX(TopicID) FROM ForumTopics WHERE Subject=?";
                    aspnetforum.Utils.DB.FillCommandParamaters(ref cmd, subj);
                    _topicID = Convert.ToInt32(this.cmd.ExecuteScalar());
                    this.cmd.Parameters.Clear();

                    //CREATE A POLL (if specified)
                    string pollQuestion = "";//tbPollQuestion.Text.Trim();
                    if (pollQuestion.Length > 0)
                    {
                        //add a poll
                        this.cmd.Parameters.Clear();
                        this.cmd.CommandText = "INSERT INTO ForumPolls (TopicID, Question) " +
                            "VALUES (?, ?)";
                        aspnetforum.Utils.DB.FillCommandParamaters(ref cmd, _topicID, pollQuestion);
                        this.cmd.ExecuteNonQuery();
                        this.cmd.Parameters.Clear();

                        //now get the ID of the poll we just created
                        this.cmd.CommandText = "SELECT MAX(PollID) FROM ForumPolls WHERE TopicID=?";
                        aspnetforum.Utils.DB.FillCommandParamaters(ref cmd, _topicID);
                        int pollID = Convert.ToInt32(this.cmd.ExecuteScalar());
                        this.cmd.Parameters.Clear();

                        int i = 0;
                        while (Request.Form["PollOption" + i] != null && Request.Form["PollOption" + i].Trim().Length > 0)
                        {
                            //add option
                            this.cmd.CommandText = "INSERT INTO ForumPollOptions (PollID, OptionText) " +
                            "VALUES (?, ?)";
                            aspnetforum.Utils.DB.FillCommandParamaters(ref cmd, pollID, Request.Form["PollOption" + i]);
                            this.cmd.ExecuteNonQuery();
                            this.cmd.Parameters.Clear();
                            i++;
                        }
                    }
                }
                else if (_changeTopic) //edit topic subj
                {
                    this.cmd.Parameters.Clear();
                    this.cmd.CommandText = "UPDATE ForumTopics SET Subject = ? WHERE TopicID = ?";
                    aspnetforum.Utils.DB.FillCommandParamaters(ref cmd, subj, _topicID);
                    this.cmd.ExecuteNonQuery();
                    this.cmd.Parameters.Clear();
                }
            }

            // Inserting or updating?
            if (_isEditing)
            {
                //if moderatro, admin or message author
                //if (bmoderator || _messageAuthorID == CurrentUserID)
                //{
                //    // Record last editor and date at the end of the message.
                //    msg += "\r\n<em>edited by " + Session["aspnetforumUserName"] +
                //            " on " + aspnetforum.Utils.GetCurrTime().ToShortDateString() + "</em>";

                //    this.cmd.CommandText = "UPDATE ForumMessages SET Body=?, Visible=? " +
                //        "WHERE MessageID=" + _messageId;
                //    aspnetforum.Utils.DB.FillCommandParamaters(ref cmd, msg, !_premoderated);
                //    this.cmd.ExecuteNonQuery();
                //    this.cmd.Parameters.Clear();

                //    SaveAttachments(_messageId);
                //}
            }
            else //inserting
            {
                this.cmd.CommandText = "INSERT INTO ForumMessages (TopicID, UserID, Body, CreationDate, Visible) " +
                    "VALUES (?, convert(uniqueidentifier, ?), ?, ?, ?)";
                aspnetforum.Utils.DB.FillCommandParamaters(ref cmd, _topicID, CurrentUserID, msg, aspnetforum.Utils.GetCurrTime(), !_premoderated);
                this.cmd.ExecuteNonQuery();
                this.cmd.Parameters.Clear();

                //incrementing repliescount (well... actually - re-calculating it)
                this.cmd.CommandText = "SELECT Count(MessageID) FROM ForumMessages WHERE TopicID=" + _topicID;
                object res = this.cmd.ExecuteScalar();
                this.cmd.CommandText = "UPDATE ForumTopics SET RepliesCount=" + res.ToString() + " WHERE TopicID=" + _topicID;
                this.cmd.ExecuteNonQuery();

                //incrementing PostsCount in Users table only for new messages, not edits.
                //only for registered users (if guestmode is on)
                if (CurrentUserID != "")
                {
                    this.cmd.CommandText = "UPDATE ForumUsers SET PostsCount=PostsCount+1 WHERE UserID='" + CurrentUserID + "'";
                    this.cmd.ExecuteNonQuery();
                }

                //get last message's ID
                this.cmd.CommandText = "SELECT MAX(MessageID) FROM ForumMessages WHERE TopicID=" + _topicID + " AND UserID='" + CurrentUserID + "'";
                aspnetforum.Utils.DB.FillCommandParamaters(ref cmd, true);
                res = this.cmd.ExecuteScalar();
                int lastmsgid = (res == null || res == DBNull.Value) ? 0 : Convert.ToInt32(res);

                //updating LastMessageID in Topics table
                this.cmd.CommandText = "UPDATE ForumTopics SET LastMessageID=" + lastmsgid + " WHERE TopicID=" + _topicID;
                this.cmd.ExecuteNonQuery();

                SaveAttachments(lastmsgid);
            }

            //saving notifications settings
            if (_mailNotificationsEnabled)
            {
                this.cmd.CommandText = "DELETE FROM ForumSubscriptions WHERE UserID='" + CurrentUserID + "' AND TopicID=" + _topicID;
                this.cmd.ExecuteNonQuery();
                //if(this.cbSubscribe.Checked)
                //{
                this.cmd.CommandText = "INSERT INTO ForumSubscriptions (UserID, TopicID) VALUES ('" + CurrentUserID + "', " + _topicID + ")";
                this.cmd.ExecuteNonQuery();
                //}
            }

            //sending notifications
            if (_mailNotificationsEnabled)
            {
                string url = Request.Url.ToString().ToLower();
                url = url.Substring(0, url.IndexOf("addpost.aspx"));

                if (_addTopic)
                {
                    if (_forumID != null)
                    {
                        aspnetforum.SendNotifications.SendNewTopicNotificationEmails(Convert.ToInt32(_forumID), CurrentUserID, url);
                    }

                }
                else
                {
                    aspnetforum.SendNotifications.SendNewMsgNotificationEmails(_topicID, CurrentUserID, url);
                }
            }

        }

        try
        {
            AppLib.SendMailToUser(AppLib.GetLoggedInUserName(), txtsubject.Text, strContent, AppConfig.GetAdminEmail());

            AppLib.SendMailToUser(AppConfig.GetAdminEmail(), txtsubject.Text, "Dear Admin, Following are the contents that were sent to the user. <br />" + strContent, AppLib.GetLoggedInUserName());
        }
        catch { }

        oTemplateLib = null;
        oCompanyManager = null;
        oCompanyInfo = null;

        txtQuestion.Text = string.Empty;
        txtsubject.Text = string.Empty;
        txtComments.Text = string.Empty; ;
        rdoQuestionType.SelectedIndex = 0;
    }