} // getHtmlContent

        public UserFeedbackFormInfo createUserFeedbackForm(CmsPage page, int identifier, CmsLanguage lang)
        {
            UserFeedbackFormInfo d = new UserFeedbackFormInfo();
            string sql             = "insert into userfeedbackform (pageid, identifier, LangCode, EmailAddressesToNotify, ThankyouMessage, FormFieldDisplayWidth, TextAreaQuestion) values (";

            sql  = sql + page.ID.ToString() + "," + identifier.ToString() + ",'" + dbEncode(lang.shortCode) + "',";
            sql += "'" + dbEncode(d.EmailAddressesToNotify) + "', ";
            sql += "'" + dbEncode(d.ThankyouMessage) + "', ";
            sql += "" + d.FormFieldDisplayWidth.ToString() + ", ";
            sql += "'" + dbEncode(d.TextAreaQuestion) + "' ";
            sql += "); ";

            int newId = this.RunInsertQuery(sql);

            if (newId > -1)
            {
                page.setLastUpdatedDateTimeToNow();

                return(d);
            }
            else
            {
                return(new UserFeedbackFormInfo());
            }
        }
        public override void RenderInEditMode(HtmlTextWriter writer, CmsPage page, int identifier, CmsLanguage langToRenderFor, string[] paramList)
        {
            UserFeedbackDb       db       = new UserFeedbackDb();
            UserFeedbackFormInfo formInfo = db.getUserFeedbackFormInfo(page, identifier, langToRenderFor, true);
            string ControlId = "UserFeedbackInfo_" + page.ID.ToString() + "_" + identifier.ToString() + "_" + langToRenderFor.shortCode + "_";

            string action = PageUtils.getFromForm(ControlId + "Action", "");

            if (action.Trim().ToLower() == "update")
            {
                formInfo.EmailAddressesToNotify = PageUtils.getFromForm(ControlId + "EmailAddressesToNotify", formInfo.EmailAddressesToNotify);
                formInfo.ThankyouMessage        = PageUtils.getFromForm(ControlId + "ThankyouMessage", formInfo.ThankyouMessage);
                formInfo.TextAreaQuestion       = PageUtils.getFromForm(ControlId + "TextAreaQuestion", formInfo.TextAreaQuestion);
                db.saveUpdatedUserFeedbackFormInfo(page, identifier, langToRenderFor, formInfo);
            }

            StringBuilder html = new StringBuilder();

            html.Append("<table>");
            html.Append("<tr>");
            html.Append("<td valign=\"top\">");
            html.Append("Email addresses to notify of form submissions: ");
            html.Append("</td>");
            html.Append("<td valign=\"top\">");
            html.Append(PageUtils.getInputTextHtml(ControlId + "EmailAddressesToNotify", ControlId + "EmailAddressesToNotify", formInfo.EmailAddressesToNotify, 50, 255));
            html.Append("<br>");
            html.Append("<font size=\"1\">(seperate addresses with semi-colons (;)</font>");
            html.Append("</td>");
            html.Append("</tr>");

            html.Append("<tr>");
            html.Append("<td valign=\"top\">");
            html.Append("Text area question text: ");
            html.Append("</td>");
            html.Append("<td valign=\"top\">");
            html.Append(PageUtils.getInputTextHtml(ControlId + "TextAreaQuestion", ControlId + "TextAreaQuestion", formInfo.TextAreaQuestion, 50, 255));
            html.Append("</td>");
            html.Append("</tr>");

            html.Append("<tr>");
            html.Append("<td valign=\"top\">HTML message to display to user upon successful submission:");
            html.Append("</td>");
            html.Append("<td valign=\"top\">");
            html.Append(PageUtils.getTextAreaHtml(ControlId + "ThankyouMessage", ControlId + "ThankyouMessage", formInfo.ThankyouMessage, 30, 5));
            html.Append("</td>");
            html.Append("</tr>");
            html.Append("</table>");

            html.Append(PageUtils.getHiddenInputHtml(ControlId + "Action", "update"));


            writer.Write(html.ToString());
        } // RenderEdit
        public UserFeedbackFormInfo getUserFeedbackFormInfo(CmsPage page, int identifier, CmsLanguage lang, bool createNewIfDoesNotExist)
        {
            if (page.ID < 0 || identifier < 0)
            {
                return(new UserFeedbackFormInfo());
            }

            string sql = "select EmailAddressesToNotify, ThankyouMessage, FormFieldDisplayWidth, TextAreaQuestion from userfeedbackform c ";

            sql += " where c.pageid = " + page.ID.ToString() + " and c.identifier = " + identifier.ToString() + " and c.LangCode = '" + dbEncode(lang.shortCode) + "';";
            DataSet ds = this.RunSelectQuery(sql);

            if (this.hasSingleRow(ds))
            {
                DataRow dr = ds.Tables[0].Rows[0];
                UserFeedbackFormInfo info = new UserFeedbackFormInfo();
                info.EmailAddressesToNotify = dr["EmailAddressesToNotify"].ToString();
                info.ThankyouMessage        = dr["ThankyouMessage"].ToString();
                info.FormFieldDisplayWidth  = Convert.ToInt32(dr["FormFieldDisplayWidth"]);
                info.TextAreaQuestion       = dr["TextAreaQuestion"].ToString();

                return(info);
            }
            else
            {
                if (createNewIfDoesNotExist)
                {
                    return(createUserFeedbackForm(page, identifier, lang));
                }
                else
                {
                    throw new Exception("getUserFeedbackFormInfo database error: placeholder does not exist");
                }
            }
            return(new UserFeedbackFormInfo());
        } // getHtmlContent
        public bool saveUpdatedUserFeedbackFormInfo(CmsPage page, int identifier, CmsLanguage lang, UserFeedbackFormInfo formInfo)
        {
            string sql = "update userfeedbackform set ";

            sql += " EmailAddressesToNotify = '" + dbEncode(formInfo.EmailAddressesToNotify) + "', ";
            sql += " ThankyouMessage = '" + dbEncode(formInfo.ThankyouMessage) + "', ";
            sql += " FormFieldDisplayWidth = " + formInfo.FormFieldDisplayWidth + ", ";
            sql += " TextAreaQuestion = '" + dbEncode(formInfo.TextAreaQuestion) + "' ";

            sql += " where pageid= " + page.ID.ToString();
            sql += " AND identifier = " + identifier.ToString();
            sql += " AND LangCode = '" + dbEncode(lang.shortCode) + "';";

            int numAffected = this.RunUpdateQuery(sql);

            if (numAffected > 0)
            {
                return(page.setLastUpdatedDateTimeToNow());
            }
            else
            {
                return(false);
            }
        }
        } // RenderView

        private bool sendAdministratorNotification(UserFeedbackFormInfo formInfo, UserFeedbackSubmittedData submittedData)
        {
            if (formInfo.EmailAddressesToNotify.Trim() == "")
            {
                return(true);
            }

            string smtpServer = CmsConfig.getConfigValue("smtpServer", "smtp.hatfieldgroup.com");

            string toAddress = formInfo.EmailAddressesToNotify;

            string[] toArray = toAddress.Split(new char[] { ';', ',' }, StringSplitOptions.RemoveEmptyEntries);

            string fromAddress = submittedData.EmailAddress;

            string subject = CmsContext.currentPage.Title;

            string host = CmsConfig.getConfigValue("SiteName", "");

            if (HttpContext.Current != null && HttpContext.Current.Request != null)
            {
                host = HttpContext.Current.Request.Url.Host;
            }

            string body = "The following feedback was sent from the " + host + " website" + Environment.NewLine + Environment.NewLine;

            body += "Date: " + DateTime.Now.ToString("MMM dd yyyy hh:mm tt") + Environment.NewLine;
            body += "From: " + submittedData.Name + " (" + submittedData.EmailAddress + ")" + Environment.NewLine;
            body += "Location: " + submittedData.Location + Environment.NewLine;
            body += "Referencing Location: " + submittedData.ReferringUrl + Environment.NewLine + Environment.NewLine;
            body += submittedData.TextAreaQuestion + ":" + Environment.NewLine + Environment.NewLine;
            body += submittedData.TextAreaValue;

            try
            {
                // SmtpMail.SmtpServer = smtpServer;

                Hatfield.Web.Portal.Net.SmtpDirect.SmtpServerHostName = smtpServer;

                MailMessage msg = new MailMessage();
                msg.From = new MailAddress(fromAddress);

                foreach (string to in toArray)
                {
                    msg.To.Add(to);
                }

                msg.IsBodyHtml = false;

                msg.Subject = subject;
                msg.Body    = body;
                bool b = Hatfield.Web.Portal.Net.SmtpDirect.Send(msg);
                if (!b)
                {
                    return(true);
                }
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
            }

            return(false);
        }
        public override void RenderInViewMode(HtmlTextWriter writer, CmsPage page, int identifier, CmsLanguage langToRenderFor, string[] paramList)
        {
            UserFeedbackDb       db       = new UserFeedbackDb();
            UserFeedbackFormInfo formInfo = db.getUserFeedbackFormInfo(page, identifier, langToRenderFor, true);
            string ControlId = "UserFeedbackInfo_" + page.ID.ToString() + "_" + identifier.ToString() + "_" + langToRenderFor.shortCode + "_";

            string _errorMessage = "";
            string action        = PageUtils.getFromForm(ControlId + "Action", "");
            UserFeedbackSubmittedData submittedData = new UserFeedbackSubmittedData();
            bool formValuesLoadedFromSession        = false;

            if (action.Trim().ToLower() == "send")
            {
                // -- get the spam question index
                int spamQuestionIndex = (PageUtils.getFromForm(ControlId + "spamQuestionIndex", SpamTestQuestion.GetRandomQuestionIndex()));
                if (spamQuestionIndex >= SpamTestQuestion.Questions.Length || spamQuestionIndex < 0)
                {
                    spamQuestionIndex = SpamTestQuestion.GetRandomQuestionIndex();
                }

                SpamTestQuestion questionToAnswer = SpamTestQuestion.Questions[spamQuestionIndex];

                string spamQuestionAnswer = (PageUtils.getFromForm(ControlId + "spamQuestionAnswer", ""));

                submittedData.Name          = (PageUtils.getFromForm(ControlId + "Name", ""));
                submittedData.Name          = submittedData.Name.Trim();
                submittedData.EmailAddress  = PageUtils.getFromForm(ControlId + "Email", "");
                submittedData.EmailAddress  = submittedData.EmailAddress.Trim();
                submittedData.Location      = PageUtils.getFromForm(ControlId + "Location", "");
                submittedData.Location      = submittedData.Location.Trim();
                submittedData.TextAreaValue = PageUtils.getFromForm(ControlId + "Comments", "");
                submittedData.TextAreaValue = submittedData.TextAreaValue.Trim();
                submittedData.ReferringUrl  = PageUtils.getFromForm(ControlId + "Referer", "");

                // -- validate user submitted values
                if (questionToAnswer.Answer != spamQuestionAnswer)
                {
                    _errorMessage = "Your answer to the math question was incorrect. Please try again.";
                }
                else if (submittedData.Name == "")
                {
                    _errorMessage = getErrorEnterNameText(langToRenderFor);
                }
                else if (submittedData.EmailAddress == "")
                {
                    _errorMessage = getErrorEnterEmailText(langToRenderFor);
                }
                else if (!PageUtils.isValidEmailAddress(submittedData.EmailAddress))
                {
                    _errorMessage = getErrorEnterValidEmailText(langToRenderFor);
                }
                else if (submittedData.TextAreaValue == "")
                {
                    _errorMessage = getErrorEnterTextAreaQuestionText(langToRenderFor) + formInfo.TextAreaQuestion;
                }
                else
                {
                    // -- save the submitted value
                    submittedData.dateTimeSubmitted = DateTime.Now;
                    submittedData.TextAreaQuestion  = formInfo.TextAreaQuestion;

                    if (db.saveUserFeedbackSubmittedData(submittedData))
                    {
                        // -- success
                        //    save submitted values to the current session
                        if (HttpContext.Current != null && HttpContext.Current.Session != null)
                        {
                            System.Web.SessionState.HttpSessionState session = System.Web.HttpContext.Current.Session;
                            session[ControlId + "Name"]     = submittedData.Name;
                            session[ControlId + "Email"]    = submittedData.EmailAddress;
                            session[ControlId + "Location"] = submittedData.Location;
                        }
                        //    send notification email message
                        sendAdministratorNotification(formInfo, submittedData);
                        //   output the Thankyou message
                        writer.Write("<p><strong>" + formInfo.ThankyouMessage + "</strong></p>");
                        return;
                    }
                    else
                    {
                        _errorMessage = getErrorSavingText(langToRenderFor);
                    }
                }
            } // if save posted values
            else
            {
                // -- get previously submitted values from the current session
                if (System.Web.HttpContext.Current != null && System.Web.HttpContext.Current.Session != null)
                {
                    System.Web.SessionState.HttpSessionState session = System.Web.HttpContext.Current.Session;
                    if (session[ControlId + "Name"] != null)
                    {
                        submittedData.Name          = session[ControlId + "Name"].ToString();
                        formValuesLoadedFromSession = true;
                    }
                    if (session[ControlId + "Email"] != null)
                    {
                        submittedData.EmailAddress  = session[ControlId + "Email"].ToString();
                        formValuesLoadedFromSession = true;
                    }
                    if (session[ControlId + "Location"] != null)
                    {
                        submittedData.Location      = session[ControlId + "Location"].ToString();
                        formValuesLoadedFromSession = true;
                    }
                }
            }


            StringBuilder html = new StringBuilder();

            if (_errorMessage != "")
            {
                html.Append("<p class=\"FormErrorMessage\">" + _errorMessage + "</p>");
            }

            string formId = "UserFeedback";

            html.Append(page.getFormStartHtml(formId));

            html.Append("<em>" + getCompleteAllText(langToRenderFor) + "</em> ");
            if (formValuesLoadedFromSession)
            {
                html.Append(getValuesPreloadedText(langToRenderFor));
            }

            html.Append("<table>");
            html.Append("<tr>");
            html.Append("<td valign=\"top\">" + getNameText(langToRenderFor) + ":");
            html.Append("</td>");
            html.Append("<td valign=\"top\">");
            html.Append(PageUtils.getInputTextHtml(ControlId + "Name", ControlId + "Name", submittedData.Name, formInfo.FormFieldDisplayWidth, 255));
            html.Append("</td>");
            html.Append("</tr>");

            html.Append("<tr>");
            html.Append("<td valign=\"top\">" + getEmailText(langToRenderFor) + ":");
            html.Append("</td>");
            html.Append("<td valign=\"top\">");
            html.Append(PageUtils.getInputTextHtml(ControlId + "Email", ControlId + "Email", submittedData.EmailAddress, formInfo.FormFieldDisplayWidth, 255));
            html.Append("</td>");
            html.Append("</tr>");

            html.Append("<tr>");
            html.Append("<td valign=\"top\">" + getLocationText(langToRenderFor) + ":");
            html.Append("</td>");
            html.Append("<td valign=\"top\">");
            html.Append(PageUtils.getInputTextHtml(ControlId + "Location", ControlId + "Location", submittedData.Location, formInfo.FormFieldDisplayWidth, 255));
            html.Append("</td>");
            html.Append("</tr>");

            html.Append("<tr>");
            html.Append("<td valign=\"top\">");
            html.Append(formInfo.TextAreaQuestion);
            html.Append("</td>");
            html.Append("<td valign=\"top\">");
            html.Append(PageUtils.getTextAreaHtml(ControlId + "Comments", ControlId + "Comments", "", formInfo.FormFieldDisplayWidth, 7));
            html.Append("</td>");
            html.Append("</tr>");

            // -- spam stop question
            int qIndex = SpamTestQuestion.GetRandomQuestionIndex();
            SpamTestQuestion spamQuestion = SpamTestQuestion.Questions[qIndex];

            html.Append("<tr>");
            html.Append("<td valign=\"top\">");
            html.Append(spamQuestion.Question);
            html.Append("</td>");
            html.Append("<td valign=\"top\">");
            html.Append(PageUtils.getInputTextHtml(ControlId + "spamQuestionAnswer", ControlId + "spamQuestionAnswer", "", 5, 255));
            html.Append("</td>");
            html.Append("</tr>");

            html.Append("</table>");

            html.Append(PageUtils.getHiddenInputHtml(ControlId + "spamQuestionIndex", qIndex.ToString()));
            html.Append(PageUtils.getHiddenInputHtml(ControlId + "Action", "send"));
            html.Append(PageUtils.getHiddenInputHtml(ControlId + "Referer", PageUtils.getFromForm("r", "(unknown)")));

            html.Append("<input type=\"submit\" value=\"" + getSubmitButtonText(langToRenderFor) + "\">");
            html.Append(page.getFormCloseHtml(formId));

            writer.Write(html.ToString());
        } // RenderView