protected void submitButton_Click(object sender, EventArgs e)
        {
            #region Event Attendance
            // Logging event attendance
            string codeValue = codeEntryField.Text;
            int patronId = ((Patron)Session[SessionKey.Patron]).PID;
            var pointsAward = new AwardPoints(patronId);

            if(codeValue.Length == 0) {
                Session[SessionKey.PatronMessage] = "Please enter a code.";
                Session[SessionKey.PatronMessageLevel] = PatronMessageLevels.Danger;
                Session[SessionKey.PatronMessageGlyphicon] = "remove";
                return;
            } else {
                // verify event code was not previously redeemed
                if(PatronPoints.HasRedeemedKeywordPoints(patronId, codeValue)) {
                    Session[SessionKey.PatronMessage] = "You've already redeemed this code!";
                    Session[SessionKey.PatronMessageLevel] = PatronMessageLevels.Warning;
                    Session[SessionKey.PatronMessageGlyphicon] = "exclamation-sign";
                    return;
                }

                // get event for that code, get the # points
                var ds = Event.GetEventByEventCode(pointsAward.pgm.StartDate.ToShortDateString(),
                                                   DateTime.Now.ToShortDateString(), codeValue);
                if(ds.Tables[0].Rows.Count == 0) {
                    Session[SessionKey.PatronMessage] = "Sorry, that's an invalid code.";
                    Session[SessionKey.PatronMessageLevel] = PatronMessageLevels.Warning;
                    Session[SessionKey.PatronMessageGlyphicon] = "exclamation-sign";
                    return;
                }
                var EID = (int)ds.Tables[0].Rows[0]["EID"];
                var evt = Event.GetEvent(EID);
                var points = evt.NumberPoints;
                //var newPBID = 0;

                var earnedBadges = pointsAward.AwardPointsToPatron(points: points,
                                                                   reason: PointAwardReason.EventAttendance,
                                                                   eventCode: codeValue,
                                                                   eventID: EID);

                if(!string.IsNullOrWhiteSpace(earnedBadges)) {
                    new SessionTools(Session).EarnedBadges(earnedBadges);
                }

                // set message and earned badges
                string earnedMessage = new PointCalculation().EarnedMessage(earnedBadges, points);
                if(string.IsNullOrEmpty(earnedMessage)) {
                    Session[SessionKey.PatronMessage] = "<strong>Excellent!</strong> Your secret code has been recorded.";
                } else {
                    Session[SessionKey.PatronMessage] = string.Format("<strong>Excellent!</strong> Your secret code has been recorded. <strong>{0}</strong>",
                                                                      earnedMessage);
                }
                Session[SessionKey.PatronMessageLevel] = PatronMessageLevels.Success;
                Session[SessionKey.PatronMessageGlyphicon] = "barcode";
                this.codeEntryField.Text = string.Empty;
            }
            #endregion
        }
        protected void SubmitActivity()
        {
            var txtCount = readingActivityField.Text.Trim();
            var intCount = 0;
            if(txtCount.Length == 0 || !int.TryParse(txtCount, out intCount) || intCount < 0) {
                Session[SessionKey.PatronMessage] = "You must enter how much you've read as a positive whole number.";
                Session[SessionKey.PatronMessageLevel] = PatronMessageLevels.Danger;
                Session[SessionKey.PatronMessageGlyphicon] = "remove";
                return;
            }

            var selectedActivityType = activityTypeSelector.SelectedValue;

            // check that we aren't over the max
            int maxAmountForLogging = 0;
            switch(int.Parse(selectedActivityType)) {
                case 0:
                    maxAmountForLogging = SRPSettings.GetSettingValue("MaxBook").SafeToInt();
                    break;
                case 1:
                    maxAmountForLogging = SRPSettings.GetSettingValue("MaxPage").SafeToInt();
                    break;
                //case 2: maxAmountForLogging = SRPSettings.GetSettingValue("MaxPar").SafeToInt();
                //    break;
                case 3:
                    maxAmountForLogging = SRPSettings.GetSettingValue("MaxMin").SafeToInt();
                    break;
                default:
                    maxAmountForLogging = SRPSettings.GetSettingValue("MaxMin").SafeToInt();
                    break;
            }
            if(intCount > maxAmountForLogging) {
                Session[SessionKey.PatronMessage] = string.Format("That's an awful lot of reading! You can only submit {0} {1} at a time.",
                                                                  maxAmountForLogging,
                                                                  ((ActivityType)int.Parse(selectedActivityType)).ToString());
                Session[SessionKey.PatronMessageLevel] = PatronMessageLevels.Warning;
                Session[SessionKey.PatronMessageGlyphicon] = "exclamation-sign";
                return;
            }

            var patronId = ((Patron)Session[SessionKey.Patron]).PID;
            var programGameId = int.Parse(ViewState["ProgramGameId"].ToString());

            var pa = new AwardPoints(patronId);
            var points = 0;

            // convert pages/minutes/etc. to points
            var pc = new ProgramGamePointConversion();
            pc.FetchByActivityId(programGameId, int.Parse(activityTypeSelector.SelectedValue));
            // round up to ensure they get at least 1 point
            decimal computedPoints = intCount * pc.PointCount / pc.ActivityCount;
            points = (int)Math.Ceiling(computedPoints);

            // ensure they aren't over teh day total
            var allPointsToday = PatronPoints.GetTotalPatronPoints(patronId, DateTime.Now);
            int maxPointsPerDayForLogging = SRPSettings.GetSettingValue("MaxPtsDay").SafeToInt();
            if(intCount + allPointsToday > maxPointsPerDayForLogging) {
                Session[SessionKey.PatronMessage] = "Sorry but you have already reached the maximum amount of points that you can log in a day. Keep reading and come back tomorrow!";
                Session[SessionKey.PatronMessageLevel] = PatronMessageLevels.Warning;
                Session[SessionKey.PatronMessageGlyphicon] = "exclamation-sign";
                return;
            }

            var earnedBadges = pa.AwardPointsToPatron(points: points,
                reason: PointAwardReason.Reading,
                MGID: 0,
                readingActivity: (ActivityType)pc.ActivityTypeId,
                readingAmount: intCount,
                author: authorField.Text,
                title: titleField.Text);

            // clear out the form
            var bookButton = activityTypeSelector.Items.Count == 1
                             && int.Parse(activityTypeSelector.Items[0].Value) == (int)ActivityType.Books;

            if(!bookButton) {
                readingActivityField.Text = string.Empty;
            }
            authorField.Text = string.Empty;
            titleField.Text = string.Empty;

            // set message and earned badges
            string earnedMessage = new PointCalculation().EarnedMessage(earnedBadges, points);
            if(string.IsNullOrEmpty(earnedMessage)) {
                Session[SessionKey.PatronMessage] = "<strong>Good job!</strong> Your reading activity has been logged.";
            } else {
                Session[SessionKey.PatronMessage] = string.Format("<strong>Good job!</strong> Your reading activity has been logged. <strong>{0}</strong>",
                                                                  earnedMessage);
            }
            Session[SessionKey.PatronMessageLevel] = PatronMessageLevels.Success;
            Session[SessionKey.PatronMessageGlyphicon] = "thumbs-up";
            new SessionTools(Session).EarnedBadges(earnedBadges);
        }
        protected void SubmitActivity()
        {
            var txtCount = readingActivityField.Text.Trim();
            var intCount = 0;
            if (txtCount.Length == 0 || !int.TryParse(txtCount, out intCount) || intCount < 0)
            {
                Session[SessionKey.PatronMessage] = StringResources.getString("readinglog-entry-invalid");
                Session[SessionKey.PatronMessageLevel] = PatronMessageLevels.Danger;
                Session[SessionKey.PatronMessageGlyphicon] = "remove";
                return;
            }

            var selectedActivityType = activityTypeSelector.SelectedValue;

            // check that we aren't over the max
            int maxAmountForLogging = 0;
            switch (int.Parse(selectedActivityType))
            {
                case 0:
                    maxAmountForLogging = SRPSettings.GetSettingValue("MaxBook").SafeToInt();
                    break;
                case 1:
                    maxAmountForLogging = SRPSettings.GetSettingValue("MaxPage").SafeToInt();
                    break;
                //case 2: maxAmountForLogging = SRPSettings.GetSettingValue("MaxPar").SafeToInt();
                //    break;
                case 3:
                    maxAmountForLogging = SRPSettings.GetSettingValue("MaxMin").SafeToInt();
                    break;
                default:
                    maxAmountForLogging = SRPSettings.GetSettingValue("MaxMin").SafeToInt();
                    break;
            }
            if (intCount > maxAmountForLogging)
            {
                Session[SessionKey.PatronMessage] = string.Format(StringResources.getString("readinglog-entry-limit"),
                                                                  maxAmountForLogging,
                                                                  ((ActivityType)int.Parse(selectedActivityType)).ToString());
                Session[SessionKey.PatronMessageLevel] = PatronMessageLevels.Warning;
                Session[SessionKey.PatronMessageGlyphicon] = "exclamation-sign";
                return;
            }

            var patronId = ((Patron)Session[SessionKey.Patron]).PID;
            var programGameId = int.Parse(ViewState[ProgramGameIdKey].ToString());

            var pa = new AwardPoints(patronId);
            var points = 0;

            // convert pages/minutes/etc. to points
            var pc = new ProgramGamePointConversion();
            pc.FetchByActivityId(programGameId, int.Parse(activityTypeSelector.SelectedValue));
            // round up to ensure they get at least 1 point
            decimal computedPoints = intCount * pc.PointCount / pc.ActivityCount;
            points = (int)Math.Ceiling(computedPoints);


            // ensure they aren't over teh day total
            var allPointsToday = PatronPoints.GetTotalPatronPointsOnDate(patronId, DateTime.Now);
            int maxPointsPerDayForLogging = SRPSettings.GetSettingValue("MaxPtsDay").SafeToInt();
            if (intCount + allPointsToday > maxPointsPerDayForLogging)
            {
                Session[SessionKey.PatronMessage] = StringResources.getString("readinglog-daily-limit");
                Session[SessionKey.PatronMessageLevel] = PatronMessageLevels.Warning;
                Session[SessionKey.PatronMessageGlyphicon] = "exclamation-sign";
                return;
            }

            var review = "";

            if (ViewState[PatronCanReviewKey] as bool? == true)
            {
                review = reviewField.Text;
            }

            var earnedBadges = pa.AwardPointsToPatron(points: points,
                reason: PointAwardReason.Reading,
                MGID: 0,
                readingActivity: (ActivityType)pc.ActivityTypeId,
                readingAmount: intCount,
                author: authorField.Text,
                title: titleField.Text,
                review: review);

            // clear out the form
            var bookButton = activityTypeSelector.Items.Count == 1
                             && int.Parse(activityTypeSelector.Items[0].Value) == (int)ActivityType.Books;

            if (!bookButton)
            {
                readingActivityField.Text = string.Empty;
            }
            authorField.Text = string.Empty;
            titleField.Text = string.Empty;
            reviewField.Text = string.Empty;

            // set message and earned badges
            string earnedMessage = new PointCalculation().EarnedMessage(earnedBadges, points);
            if (string.IsNullOrEmpty(earnedMessage))
            {
                Session[SessionKey.PatronMessage] = "<strong>Good job!</strong> Your reading activity has been logged.";
            }
            else {
                Session[SessionKey.PatronMessage] = string.Format("<strong>Good job!</strong> Your reading activity has been logged. <strong>{0}</strong>",
                                                                  earnedMessage);
            }
            Session[SessionKey.PatronMessageLevel] = PatronMessageLevels.Success;
            Session[SessionKey.PatronMessageGlyphicon] = "thumbs-up";
            new SessionTools(Session).EarnedBadges(earnedBadges);
        }
        protected void submitButton_Click(object sender, EventArgs e) {

            #region Event Attendance
            // Logging event attendance
            string codeValue = Logic.Code.SanitizeCode(codeEntryField.Text);
            codeEntryField.Text = codeValue;
            int patronId = (int)ViewState["SubmitAsPatronId"];
            var pointsAward = new AwardPoints(patronId);
            var patron = Patron.FetchObject(patronId);
            var patronName = DisplayHelper.FormatName(patron.FirstName, patron.LastName, patron.Username);

            if(codeValue.Length == 0) {
                Session[SessionKey.PatronMessage] = "Please enter a code.";
                Session[SessionKey.PatronMessageLevel] = PatronMessageLevels.Danger;
                Session[SessionKey.PatronMessageGlyphicon] = "remove";
                return;
            } else { 
                // verify event code was not previously redeemed
                if(PatronPoints.HasRedeemedKeywordPoints(patronId, codeValue)) {
                    Session[SessionKey.PatronMessage] = string.Format("{0} has already redeemed this code!", patronName);
                    Session[SessionKey.PatronMessageLevel] = PatronMessageLevels.Warning;
                    Session[SessionKey.PatronMessageGlyphicon] = "exclamation-sign";
                    return;
                }

                // get event for that code, get the # points
                var ds = Event.GetEventByEventCode(codeValue);
                if(ds.Tables[0].Rows.Count == 0) {
                    Session[SessionKey.PatronMessage] = "Sorry, that's an invalid code.";
                    Session[SessionKey.PatronMessageLevel] = PatronMessageLevels.Warning;
                    Session[SessionKey.PatronMessageGlyphicon] = "exclamation-sign";
                    return;
                }
                var EID = (int)ds.Tables[0].Rows[0]["EID"];
                var evt = Event.GetEvent(EID);
                var points = evt.NumberPoints;
                //var newPBID = 0;

                var earnedBadges = pointsAward.AwardPointsToPatron(points: points,
                                                                   reason: PointAwardReason.EventAttendance,
                                                                   eventCode: codeValue,
                                                                   eventID: EID);

                // don't show badge earnings to the parent
                //if(!string.IsNullOrWhiteSpace(earnedBadges)) {
                //    new SessionTools(Session).EarnedBadges(earnedBadges);
                //}

                // set message and earned badges
                string earnedMessage = new PointCalculation().EarnedMessage(earnedBadges, points, patronName);
                if(string.IsNullOrEmpty(earnedMessage)) {
                    Session[SessionKey.PatronMessage] = string.Format("<strong>Excellent!</strong> Secret code recorded for {0}.", patronName);
                } else {
                    Session[SessionKey.PatronMessage] = string.Format("<strong>Excellent!</strong> Secret code recorded. <strong>{0}</strong>",
                                                                      earnedMessage);
                }
                Session[SessionKey.PatronMessageLevel] = PatronMessageLevels.Success;
                Session[SessionKey.PatronMessageGlyphicon] = "barcode";
                this.codeEntryField.Text = string.Empty;
            }
            #endregion

        }
        protected void submitButton_Click(object sender, EventArgs e)
        {
            #region Event Attendance
            // Logging event attendance
            string codeValue = Logic.Code.SanitizeCode(codeEntryField.Text);
            codeEntryField.Text = codeValue;
            var patron = (Patron)Session[SessionKey.Patron];
            var pointsAward = new AwardPoints(patron.PID);

            if (codeValue.Length == 0)
            {
                new SessionTools(Session).AlertPatron("Please enter a code.",
                    PatronMessageLevels.Danger,
                    "remove");
                return;
            }
            else
            {
                // verify event code was not previously redeemed
                if (PatronPoints.HasRedeemedKeywordPoints(patron.PID, codeValue))
                {
                    new SessionTools(Session).AlertPatron("You've already redeemed this code!",
                        PatronMessageLevels.Warning,
                        "exclamation-sign");
                    return;
                }

                // get event for that code, get the # points
                var ds = Event.GetEventByEventCode(codeValue);
                if (ds.Tables[0].Rows.Count == 0)
                {
                    new SessionTools(Session).AlertPatron("Sorry, that's an invalid code.",
                        PatronMessageLevels.Warning,
                        "exclamation-sign");
                    var invalidCodeCount = (int?)Session[SessionKey.InvalidCodeCount] ?? 0;
                    invalidCodeCount++;
                    Session[SessionKey.InvalidCodeCount] = invalidCodeCount;
                    if (patron == null)
                    {
                        this.Log().Info("Invalid code '{0}' entered by {1}/{2} from {3}, invalid code(s) this session: {4}",
                            codeValue,
                            patron.PID,
                            patron.Username,
                            new WebTools().RemoteUserAddress(Request),
                            invalidCodeCount);
                    }
                    else
                    {
                        this.Log().Info("Invalid code '{0}' entered by unknown patron from {1}, invalid code(s) this session: {2}",
                            codeValue,
                            new WebTools().RemoteUserAddress(Request),
                            invalidCodeCount);
                    }
                    return;
                }
                var EID = (int)ds.Tables[0].Rows[0]["EID"];
                var evt = Event.GetEvent(EID);
                var points = evt.NumberPoints;
                //var newPBID = 0;

                var earnedBadges = pointsAward.AwardPointsToPatron(points: points,
                                                                   reason: PointAwardReason.EventAttendance,
                                                                   eventCode: codeValue,
                                                                   eventID: EID);

                if (!string.IsNullOrWhiteSpace(earnedBadges))
                {
                    new SessionTools(Session).EarnedBadges(earnedBadges);
                }

                string userMessage = null;
                // set message and earned badges
                string earnedMessage = new PointCalculation().EarnedMessage(earnedBadges, points);
                if (string.IsNullOrEmpty(earnedMessage))
                {
                    userMessage = "<strong>Excellent!</strong> Your secret code has been recorded.";
                }
                else
                {
                    userMessage = string.Format("<strong>Excellent!</strong> Your secret code has been recorded. <strong>{0}</strong>",
                                                earnedMessage);
                }
                new SessionTools(Session).AlertPatron(userMessage,
                    PatronMessageLevels.Success,
                    "barcode");
                this.codeEntryField.Text = string.Empty;
            }
            #endregion

        }