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 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 ImageButton1_Command(object sender, CommandEventArgs e)
        {
            var masterPage = (IControlRoomMaster)((BaseControlRoomPage)Page).Master;
            lblError.Text= string.Empty;
            Page.Validate();
            if (Page.IsValid)
            {
                var patron = Patron.FetchObject(int.Parse(PID.Text));
                var pgm = Programs.FetchObject(patron.ProgID);

                if (e.CommandName.ToLower() == "save" || e.CommandName.ToLower() == "saveandback")
                {
                    //var p = new Patron();
                    var o = new PatronPoints();
                    var sBadges= string.Empty;
                    var pa = new AwardPoints(patron.PID);
                    var points = int.Parse(NumPoints.Text);

                    if (!IsAdd())
                    {
                        //p = Patron.FetchObject(int.Parse(PID.Text));
                        o = PatronPoints.FetchObject(int.Parse(PatronPointsID));
                        o.Delete();

                        var pl = PatronReadingLog.FetchObject(o.LogID);
                        if (pl != null && pl.HasReview)
                        {
                            var r = PatronReview.FetchObjectByLogId(o.LogID);
                            if (r != null) r.Delete();
                        }
                        if (pl != null) pl.Delete();
                    }

                    //o.PID = patron.PID;
                    //o.NumPoints = int.Parse(NumPoints.Text);
                    //o.AwardDate = DateTime.Parse(AwardDate.Text);
                    //o.AwardReasonCd = int.Parse(AwardReasonCd.SelectedValue);
                    //o.AwardReason = PatronPoints.PointAwardReasonCdToDescription((PointAwardReason)o.AwardReasonCd);
                    //o.isReading = o.isBookList = o.isEvent = o.isGame = o.isGameLevelActivity = false;
                    if ((PointAwardReason)o.AwardReasonCd == PointAwardReason.Reading)
                    {
                        var pc = new ProgramGamePointConversion();
                        pc.FetchByActivityId(pgm.PID, 1);//ActivityType.Pages);
                        var pages = 1;
                        try { Convert.ToInt32(pc.ActivityCount * points / pc.PointCount);} catch{}
                        sBadges = pa.AwardPointsToPatron(points, PointAwardReason.Reading,
                                                            0,
                                                            ActivityType.Pages, pages, "", "", "", forceDate: DateTime.Parse(AwardDate.Text));
                    }

                    if ((PointAwardReason)o.AwardReasonCd == PointAwardReason.EventAttendance)
                    {
                        var ds = Event.GetEventByEventCode(pa.pgm.StartDate.ToShortDateString(),
                                                   DateTime.Now.ToShortDateString(), EventCode.Text);
                        var EID = 0;
                        if (ds.Tables[0].Rows.Count == 0)
                        {
                            EID = 0;
                        }
                        else
                        {
                            EID = (int)ds.Tables[0].Rows[0]["EID"];
                        }
                        sBadges = pa.AwardPointsToPatron(points, PointAwardReason.EventAttendance,
                                                     eventCode: EventCode.Text, eventID: EID, forceDate: DateTime.Parse(AwardDate.Text));
                    }
                    if ((PointAwardReason)o.AwardReasonCd == PointAwardReason.BookListCompletion)
                    {
                        var BLID = 0;
                        int.TryParse(lblBookList.Text, out BLID);
                        sBadges = pa.AwardPointsToPatron(points, PointAwardReason.BookListCompletion,
                                                            bookListID: BLID, forceDate: DateTime.Parse(AwardDate.Text));

                    }
                    if ((PointAwardReason)o.AwardReasonCd == PointAwardReason.GameCompletion)
                    {
                        var GID = 0;
                        int.TryParse(lblGame.Text, out GID);
                        sBadges = pa.AwardPointsToPatron(points, PointAwardReason.GameCompletion, GID, forceDate: DateTime.Parse(AwardDate.Text));
                    }
                    if ((PointAwardReason)o.AwardReasonCd == PointAwardReason.MiniGameCompletion)
                    {
                        var MGID = 0;
                        int.TryParse(lblMGame.Text, out MGID);
                        sBadges = pa.AwardPointsToPatron(points, PointAwardReason.MiniGameCompletion, MGID, forceDate: DateTime.Parse(AwardDate.Text));
                    }

                    PatronPointsID = PatronPoints.GetLastPatronEntryID(int.Parse(PatronID)).ToString();

                    LoadControl();
                    masterPage.PageMessage = SRPResources.AddedOK;

                    if (e.CommandName.ToLower() == "saveandback")
                    {
                        Response.Redirect("PatronLog.aspx");
                    }

                }

            }
        }