/// <summary>
        /// Clears the current page display and replaces it with an xhtml message.
        /// </summary>
        private void ShowConfirmationMessage(string confirmationXHtml)
        {
            this.EnsureChildControls();
            this.Controls.Clear();

            // Add header template
            if (ConfirmationHeaderTemplate != null)
            {
                XhtmlContainer header = new XhtmlContainer();
                ConfirmationHeaderTemplate.InstantiateIn(header);
                this.Controls.Add(header);
            }

            // Add content template
            if (ConfirmationTemplate == null)
            {
                ConfirmationTemplate = new DefaultTemplate(this.Service, confirmationXHtml);
            }

            XhtmlContainer confirmation = new XhtmlContainer();

            ConfirmationTemplate.InstantiateIn(confirmation);
            this.Controls.Add(confirmation);

            // If the confirmation contains a Literal with the id "serviceName", replace it with the current service name
            Literal serviceName = confirmation.FindControl("ConfirmationServiceName") as Literal;

            if (serviceName != null)
            {
                serviceName.Text = Service.Name;
            }

            // Add footer template
            if (ConfirmationFooterTemplate != null)
            {
                XhtmlContainer footer = new XhtmlContainer();
                ConfirmationFooterTemplate.InstantiateIn(footer);
                this.Controls.Add(footer);
            }
        }
        /// <summary>
        /// Display a message indicating that activation has failed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DisplayMessage_DeactivationFailed(object sender, EventArgs e)
        {
            this.EnsureChildControls();
            this.Controls.Clear();

            if (FailureTemplate == null)
            {
                FailureTemplate = new DefaultTemplate(this.Service, Resources.EmailDeactivationFailure);
            }

            XhtmlContainer container = new XhtmlContainer();

            FailureTemplate.InstantiateIn(container);
            this.Controls.Add(container);

            // If the template contains a Literal with the id "FailureServiceName", replace it with the current service name
            Literal serviceName = container.FindControl("FailureServiceName") as Literal;

            if (serviceName != null)
            {
                serviceName.Text = Service.Name;
            }
        }
        /// <summary>
        /// Display a message indicating that activation has failed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DisplayMessage_ActivationFailed(object sender, EventArgs e)
        {
            this.EnsureChildControls();
            this.Controls.Clear();

            if (FailureTemplate == null) FailureTemplate = new DefaultTemplate(this.Service, Resources.EmailActivationFailure);

            XhtmlContainer container = new XhtmlContainer();
            FailureTemplate.InstantiateIn(container);
            this.Controls.Add(container);

            // If the intro contains a Literal with the id "FailureServiceName", replace it with the current service name
            Literal serviceName = container.FindControl("FailureServiceName") as Literal;
            if (serviceName != null) serviceName.Text = Service.Name;
        }
        /// <summary>
        /// Build the email address entry form
        /// </summary>
        protected override void CreateChildControls()
        {
            if (this.Service == null || this.Service.Id <= 0 || String.IsNullOrEmpty(this.Service.Name))
            {
                // Add header template
                if (NotFoundHeaderTemplate != null)
                {
                    XhtmlContainer header = new XhtmlContainer();
                    NotFoundHeaderTemplate.InstantiateIn(header);
                    this.Controls.Add(header);
                }

                // Add no service template
                if (NotFoundTemplate == null)
                {
                    NotFoundTemplate = new DefaultTemplate(this.Service, Resources.EmailSubscribeNoService);
                }

                XhtmlContainer template = new XhtmlContainer();
                NotFoundTemplate.InstantiateIn(template);
                this.Controls.Add(template);

                // Add footer template
                if (NotFoundFooterTemplate != null)
                {
                    XhtmlContainer footer = new XhtmlContainer();
                    NotFoundFooterTemplate.InstantiateIn(footer);
                    this.Controls.Add(footer);
                }
            }
            else
            {
                bool IsNewSubscription = true;

                // Get activation code from querystring
                string subscriptionCode = this.Context.Request.QueryString[this.CodeParameter];
                // Did we find a valid subscription code?
                if (!string.IsNullOrEmpty(subscriptionCode))
                {
                    // This is a change of an existing subscription.
                    IsNewSubscription = false;
                }

                // Add header template
                if (SubscribeHeaderTemplate != null)
                {
                    XhtmlContainer header = new XhtmlContainer();
                    SubscribeHeaderTemplate.InstantiateIn(header);
                    this.Controls.Add(header);
                }

                // Add intro template
                if (IntroTemplate == null)
                {
                    if (IsNewSubscription)
                    {
                        IntroTemplate = new DefaultTemplate(this.Service, Resources.EmailSubscribeIntro);
                    }
                    else
                    {
                        IntroTemplate = new DefaultTemplate(this.Service, Resources.SubscriptionOptionsUpdateIntro);
                    }
                }

                XhtmlContainer intro = new XhtmlContainer();
                IntroTemplate.InstantiateIn(intro);
                this.Controls.Add(intro);

                // If the intro contains a Literal with the id "serviceName", replace it with the current service name
                Literal serviceName = intro.FindControl("IntroServiceName") as Literal;
                if (serviceName != null)
                {
                    serviceName.Text = Service.Name;
                }

                // validation messages - added in at this point to work around a .NET bug
                this.Controls.Add(new EsccValidationSummary());

                // Add form header template
                if (FormHeaderTemplate != null)
                {
                    XhtmlContainer header = new XhtmlContainer();
                    FormHeaderTemplate.InstantiateIn(header);
                    this.Controls.Add(header);
                }

                if (IsNewSubscription)
                {
                    // Display controls suitable for a new subscription.

                    // email box
                    this.email           = new TextBox();
                    this.email.MaxLength = 255;    // e-GIF
                    this.email.ID        = "sub1"; // don't call the box "email", spammers look for that
                    this.email.CssClass  = "email";

                    FormPart emailPart = new FormPart(Resources.EmailEntryPrompt, this.email);
                    emailPart.Required = true;
                    this.Controls.Add(emailPart);

                    // Confirm email box
                    this.confirmEmail           = new TextBox();
                    this.confirmEmail.MaxLength = 255; // e-GIF
                    this.confirmEmail.ID        = "sub2";
                    this.confirmEmail.CssClass  = "email";

                    FormPart confirmPart = new FormPart(Resources.EmailConfirmEntryPrompt, this.confirmEmail);
                    confirmPart.Required = true;
                    this.Controls.Add(confirmPart);

                    // validate email
                    EsccRequiredFieldValidator vrEmail = new EsccRequiredFieldValidator(this.email.ID, Resources.EmailRequiredError);
                    this.Controls.Add(vrEmail);

                    EmailValidator vrxEmail = new EmailValidator(this.email.ID, Resources.EmailInvalidError);
                    this.Controls.Add(vrxEmail);

                    // validate confirmation of email - no need for an EmailValidator because it must match the one above
                    EsccRequiredFieldValidator vrConfirm = new EsccRequiredFieldValidator(this.confirmEmail.ID, Resources.EmailConfirmRequiredError);
                    this.Controls.Add(vrConfirm);

                    EsccCustomValidator vMatchConfirm = new EsccCustomValidator(this.confirmEmail.ID, Resources.EmailConfirmMismatchError);
                    vMatchConfirm.ServerValidate += new ServerValidateEventHandler(vMatchConfirm_ServerValidate);
                    this.Controls.Add(vMatchConfirm);

                    // validate that email is not already subscribed to this service
                    EsccCustomValidator vSubscriptionExists = new EsccCustomValidator(this.confirmEmail.ID, Resources.EmailAlreadySubscribed);
                    vSubscriptionExists.ServerValidate += new ServerValidateEventHandler(vSubscriptionExists_ServerValidate);
                    this.Controls.Add(vSubscriptionExists);
                }
                else
                {
                    // Display controls suitable for a subscription update.

                    // Add the subscription email address as information feedback.
                    this.emailReadOnly      = new Label();
                    this.emailReadOnly.ID   = "sub3"; // don't call the box "email", spammers look for that
                    this.emailReadOnly.Text = this.GetEmailAddressForExistingSubscription(new Guid(subscriptionCode));

                    FormPart emailPart = new FormPart(Properties.Resources.EmailEntryPrompt, this.emailReadOnly);
                    this.Controls.Add(emailPart);
                }

                // Add extra options template
                if (FormExtraOptionsTemplate != null)
                {
                    XhtmlContainer extraOptions = new XhtmlContainer();
                    FormExtraOptionsTemplate.InstantiateIn(extraOptions);
                    this.Controls.Add(extraOptions);
                }

                // Add form footer template
                if (FormFooterTemplate != null)
                {
                    XhtmlContainer footer = new XhtmlContainer();
                    FormFooterTemplate.InstantiateIn(footer);
                    this.Controls.Add(footer);
                }

                // Submit button
                EsccButton submitButton = new EsccButton();
                submitButton.Text     = Resources.SubscribeButtonText;
                submitButton.CssClass = "button";
                submitButton.Click   += new EventHandler(submitButton_Click);

                // Update button
                EsccButton updateButton = new EsccButton();
                updateButton.Text     = Resources.SubscriptionOptionsUpdateButtonText;
                updateButton.CssClass = "button buttonBigger";
                updateButton.Click   += new EventHandler(updateButton_Click);

                FormButtons buttons = new FormButtons();
                if (IsNewSubscription)
                {
                    buttons.Controls.Add(submitButton);
                }
                else
                {
                    buttons.Controls.Add(updateButton);
                }
                this.Controls.Add(buttons);

                // Add footer template
                if (SubscribeFooterTemplate != null)
                {
                    XhtmlContainer footer = new XhtmlContainer();
                    SubscribeFooterTemplate.InstantiateIn(footer);
                    this.Controls.Add(footer);
                }
            }
        }
        /// <summary>
        /// Clears the current page display and replaces it with an xhtml message.
        /// </summary>
        private void ShowConfirmationMessage(string confirmationXHtml)
        {
            this.EnsureChildControls();
            this.Controls.Clear();

            // Add header template
            if (ConfirmationHeaderTemplate != null)
            {
                XhtmlContainer header = new XhtmlContainer();
                ConfirmationHeaderTemplate.InstantiateIn(header);
                this.Controls.Add(header);
            }

            // Add content template
            if (ConfirmationTemplate == null) ConfirmationTemplate = new DefaultTemplate(this.Service, confirmationXHtml);

            XhtmlContainer confirmation = new XhtmlContainer();
            ConfirmationTemplate.InstantiateIn(confirmation);
            this.Controls.Add(confirmation);

            // If the confirmation contains a Literal with the id "serviceName", replace it with the current service name
            Literal serviceName = confirmation.FindControl("ConfirmationServiceName") as Literal;
            if (serviceName != null) serviceName.Text = Service.Name;

            // Add footer template
            if (ConfirmationFooterTemplate != null)
            {
                XhtmlContainer footer = new XhtmlContainer();
                ConfirmationFooterTemplate.InstantiateIn(footer);
                this.Controls.Add(footer);
            }
        }
        /// <summary>
        /// Build the email address entry form
        /// </summary>
        protected override void CreateChildControls()
        {
            if (this.Service == null || this.Service.Id <= 0 || String.IsNullOrEmpty(this.Service.Name))
            {
                // Add header template
                if (NotFoundHeaderTemplate != null)
                {
                    XhtmlContainer header = new XhtmlContainer();
                    NotFoundHeaderTemplate.InstantiateIn(header);
                    this.Controls.Add(header);
                }

                // Add no service template
                if (NotFoundTemplate == null) NotFoundTemplate = new DefaultTemplate(this.Service, Resources.EmailSubscribeNoService);

                XhtmlContainer template = new XhtmlContainer();
                NotFoundTemplate.InstantiateIn(template);
                this.Controls.Add(template);

                // Add footer template
                if (NotFoundFooterTemplate != null)
                {
                    XhtmlContainer footer = new XhtmlContainer();
                    NotFoundFooterTemplate.InstantiateIn(footer);
                    this.Controls.Add(footer);
                }
            }
            else
            {
                bool IsNewSubscription = true;

                // Get activation code from querystring
                string subscriptionCode = this.Context.Request.QueryString[this.CodeParameter];
                // Did we find a valid subscription code?
                if (!string.IsNullOrEmpty(subscriptionCode))
                {
                    // This is a change of an existing subscription.
                    IsNewSubscription = false;
                }

                // Add header template
                if (SubscribeHeaderTemplate != null)
                {
                    XhtmlContainer header = new XhtmlContainer();
                    SubscribeHeaderTemplate.InstantiateIn(header);
                    this.Controls.Add(header);
                }

                // Add intro template
                if (IntroTemplate == null)
                {
                    if (IsNewSubscription)
                    {
                        IntroTemplate = new DefaultTemplate(this.Service, Resources.EmailSubscribeIntro);
                    }
                    else
                    {
                        IntroTemplate = new DefaultTemplate(this.Service, Resources.SubscriptionOptionsUpdateIntro);
                    }
                }

                XhtmlContainer intro = new XhtmlContainer();
                IntroTemplate.InstantiateIn(intro);
                this.Controls.Add(intro);

                // If the intro contains a Literal with the id "serviceName", replace it with the current service name
                Literal serviceName = intro.FindControl("IntroServiceName") as Literal;
                if (serviceName != null) serviceName.Text = Service.Name;

                // validation messages - added in at this point to work around a .NET bug
                this.Controls.Add(new EsccValidationSummary());

                // Add form header template
                if (FormHeaderTemplate != null)
                {
                    XhtmlContainer header = new XhtmlContainer();
                    FormHeaderTemplate.InstantiateIn(header);
                    this.Controls.Add(header);
                }

                if (IsNewSubscription)
                {
                    // Display controls suitable for a new subscription.

                    // email box
                    this.email = new TextBox();
                    this.email.MaxLength = 255; // e-GIF
                    this.email.ID = "sub1"; // don't call the box "email", spammers look for that
                    this.email.CssClass = "email";

                    FormPart emailPart = new FormPart(Resources.EmailEntryPrompt, this.email);
                    emailPart.Required = true;
                    this.Controls.Add(emailPart);

                    // Confirm email box
                    this.confirmEmail = new TextBox();
                    this.confirmEmail.MaxLength = 255; // e-GIF
                    this.confirmEmail.ID = "sub2";
                    this.confirmEmail.CssClass = "email";

                    FormPart confirmPart = new FormPart(Resources.EmailConfirmEntryPrompt, this.confirmEmail);
                    confirmPart.Required = true;
                    this.Controls.Add(confirmPart);

                    // validate email
                    EsccRequiredFieldValidator vrEmail = new EsccRequiredFieldValidator(this.email.ID, Resources.EmailRequiredError);
                    this.Controls.Add(vrEmail);

                    EmailValidator vrxEmail = new EmailValidator(this.email.ID, Resources.EmailInvalidError);
                    this.Controls.Add(vrxEmail);

                    // validate confirmation of email - no need for an EmailValidator because it must match the one above
                    EsccRequiredFieldValidator vrConfirm = new EsccRequiredFieldValidator(this.confirmEmail.ID, Resources.EmailConfirmRequiredError);
                    this.Controls.Add(vrConfirm);

                    EsccCustomValidator vMatchConfirm = new EsccCustomValidator(this.confirmEmail.ID, Resources.EmailConfirmMismatchError);
                    vMatchConfirm.ServerValidate += new ServerValidateEventHandler(vMatchConfirm_ServerValidate);
                    this.Controls.Add(vMatchConfirm);

                    // validate that email is not already subscribed to this service
                    EsccCustomValidator vSubscriptionExists = new EsccCustomValidator(this.confirmEmail.ID, Resources.EmailAlreadySubscribed);
                    vSubscriptionExists.ServerValidate += new ServerValidateEventHandler(vSubscriptionExists_ServerValidate);
                    this.Controls.Add(vSubscriptionExists);
                }
                else
                {
                    // Display controls suitable for a subscription update.

                    // Add the subscription email address as information feedback.
                    this.emailReadOnly = new Label();
                    this.emailReadOnly.ID = "sub3"; // don't call the box "email", spammers look for that
                    this.emailReadOnly.Text = this.GetEmailAddressForExistingSubscription(new Guid(subscriptionCode));

                    FormPart emailPart = new FormPart(Properties.Resources.EmailEntryPrompt, this.emailReadOnly);
                    this.Controls.Add(emailPart);
                }

                // Add extra options template
                if (FormExtraOptionsTemplate != null)
                {
                    XhtmlContainer extraOptions = new XhtmlContainer();
                    FormExtraOptionsTemplate.InstantiateIn(extraOptions);
                    this.Controls.Add(extraOptions);
                }

                // Add form footer template
                if (FormFooterTemplate != null)
                {
                    XhtmlContainer footer = new XhtmlContainer();
                    FormFooterTemplate.InstantiateIn(footer);
                    this.Controls.Add(footer);
                }

                // Submit button
                EsccButton submitButton = new EsccButton();
                submitButton.Text = Resources.SubscribeButtonText;
                submitButton.CssClass = "button";
                submitButton.Click += new EventHandler(submitButton_Click);

                // Update button
                EsccButton updateButton = new EsccButton();
                updateButton.Text = Resources.SubscriptionOptionsUpdateButtonText;
                updateButton.CssClass = "button buttonBigger";
                updateButton.Click += new EventHandler(updateButton_Click);

                FormButtons buttons = new FormButtons();
                if (IsNewSubscription) buttons.Controls.Add(submitButton); else buttons.Controls.Add(updateButton);
                this.Controls.Add(buttons);

                // Add footer template
                if (SubscribeFooterTemplate != null)
                {
                    XhtmlContainer footer = new XhtmlContainer();
                    SubscribeFooterTemplate.InstantiateIn(footer);
                    this.Controls.Add(footer);
                }
            }
        }