protected void rptr_ItemCommand(object source, RepeaterCommandEventArgs e) {
            pnlList.Visible = false;
            lblFrom.Text = ((BaseSRPPage)this.Page).GetResourceString("notifications-from");

            var o = new Notifications();
            o.Fetch(int.Parse(e.CommandArgument.ToString()));

            lblTitle.Text = o.Subject;
            lblBody.Text = Server.HtmlDecode(o.Body);
            NID.Text = o.NID.ToString();
            lblReceived.Text = o.AddedDate.ToString();

            o.isUnread = false;
            o.Update();

            pnlDetail.Visible = true;
        }
        public static bool AwardBadgeToPatron(int badgeToAward, Patron patron, ref List<Badge> earnedBadges)
        {
            var now = DateTime.Now;

            // check if badge was already earned...
            var pbds = PatronBadges.GetAll(patron.PID);
            var a = pbds.Tables[0].AsEnumerable().Where(r => r.Field<int>("BadgeID") == badgeToAward);

            var newTable = new DataTable();
            try { newTable = a.CopyToDataTable(); } catch { }

            // badge not found, award it!
            if(newTable.Rows.Count == 0) {
                var pb = new PatronBadges { BadgeID = badgeToAward, DateEarned = now, PID = patron.PID };
                pb.Insert();

                var earnedBadge = Badge.GetBadge(badgeToAward);
                if(earnedBadge != null) {
                    earnedBadges.Add(earnedBadge);

                    //if badge generates notification, then generate the notification
                    if(earnedBadge.GenNotificationFlag) {
                        var not = new Notifications {
                            PID_To = patron.PID,
                            PID_From = 0,  //0 == System Notification
                            Subject = earnedBadge.NotificationSubject,
                            Body = earnedBadge.NotificationBody,
                            isQuestion = false,
                            AddedDate = now,
                            LastModDate = now,
                            AddedUser = patron.Username,
                            LastModUser = "******"
                        };
                        not.Insert();
                    }

                    //if badge generates prize, then generate the prize
                    if(earnedBadge.IncludesPhysicalPrizeFlag) {
                        var ppp = new DAL.PatronPrizes {
                            PID = patron.PID,
                            PrizeSource = 1,
                            BadgeID = badgeToAward,
                            PrizeName = earnedBadge.PhysicalPrizeName,
                            RedeemedFlag = false,
                            AddedUser = patron.Username,
                            LastModUser = "******",
                            AddedDate = now,
                            LastModDate = now
                        };

                        ppp.Insert();
                    }

                    // if badge generates award code, then generate the code
                    if(earnedBadge.AssignProgramPrizeCode) {
                        var rewardCode= string.Empty;
                        // get the Code value
                        // save the code value for the patron
                        rewardCode = ProgramCodes.AssignCodeForPatron(patron.ProgID, patron.ProgID);

                        // generate the notification
                        var not = new Notifications {
                            PID_To = patron.PID,
                            PID_From = 0,  //0 == System Notification
                            Subject = earnedBadge.PCNotificationSubject,
                            Body = earnedBadge.PCNotificationBody.Replace("{ProgramRewardCode}", rewardCode),
                            isQuestion = false,
                            AddedDate = now,
                            LastModDate = now,
                            AddedUser = patron.Username,
                            LastModUser = "******"
                        };
                        not.Insert();
                    }
                }

                return true;
            } else {
                return false;
            }
        }
        public static int Delete(Notifications o) {

            int iReturn = -1; //assume the worst

            SqlParameter[] arrParams = new SqlParameter[1];

            arrParams[0] = new SqlParameter("@NID", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.NID, o.NID.GetTypeCode()));

            try {

                iReturn = SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, "app_Notifications_Delete", arrParams);

            } catch(SqlException exx) {
                "GRA.SRP.DAL.Notifications".Log().Error("SQL error deleting notification {0}: {1} - {2}",
                                                        o.NID,
                                                        exx.Message,
                                                        exx.StackTrace);
                System.Diagnostics.Debug.Write(exx.Message);

            }

            return iReturn;

        }
        public static int Update(Notifications o) {

            int iReturn = -1; //assume the worst

            SqlParameter[] arrParams = new SqlParameter[21];

            arrParams[0] = new SqlParameter("@NID", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.NID, o.NID.GetTypeCode()));
            arrParams[1] = new SqlParameter("@PID_To", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.PID_To, o.PID_To.GetTypeCode()));
            arrParams[2] = new SqlParameter("@PID_From", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.PID_From, o.PID_From.GetTypeCode()));
            arrParams[3] = new SqlParameter("@isQuestion", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.isQuestion, o.isQuestion.GetTypeCode()));
            arrParams[4] = new SqlParameter("@Subject", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.Subject, o.Subject.GetTypeCode()));
            arrParams[5] = new SqlParameter("@Body", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.Body, o.Body.GetTypeCode()));
            arrParams[6] = new SqlParameter("@AddedDate", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.AddedDate, o.AddedDate.GetTypeCode()));
            arrParams[7] = new SqlParameter("@AddedUser", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.AddedUser, o.AddedUser.GetTypeCode()));
            arrParams[8] = new SqlParameter("@LastModDate", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.LastModDate, o.LastModDate.GetTypeCode()));
            arrParams[9] = new SqlParameter("@LastModUser", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.LastModUser, o.LastModUser.GetTypeCode()));

            arrParams[10] = new SqlParameter("@TenID", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.TenID, o.TenID.GetTypeCode()));
            arrParams[11] = new SqlParameter("@FldInt1", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.FldInt1, o.FldInt1.GetTypeCode()));
            arrParams[12] = new SqlParameter("@FldInt2", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.FldInt2, o.FldInt2.GetTypeCode()));
            arrParams[13] = new SqlParameter("@FldInt3", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.FldInt3, o.FldInt3.GetTypeCode()));
            arrParams[14] = new SqlParameter("@FldBit1", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.FldBit1, o.FldBit1.GetTypeCode()));
            arrParams[15] = new SqlParameter("@FldBit2", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.FldBit2, o.FldBit2.GetTypeCode()));
            arrParams[16] = new SqlParameter("@FldBit3", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.FldBit3, o.FldBit3.GetTypeCode()));
            arrParams[17] = new SqlParameter("@FldText1", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.FldText1, o.FldText1.GetTypeCode()));
            arrParams[18] = new SqlParameter("@FldText2", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.FldText2, o.FldText2.GetTypeCode()));
            arrParams[19] = new SqlParameter("@FldText3", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.FldText3, o.FldText3.GetTypeCode()));

            arrParams[20] = new SqlParameter("@isUnread", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.isUnread, o.isUnread.GetTypeCode()));

            try {

                iReturn = SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, "app_Notifications_Update", arrParams);

            } catch(SqlException exx) {

                System.Diagnostics.Debug.Write(exx.Message);

            }

            return iReturn;

        }
        public static int Insert(Notifications o) {

            SqlParameter[] arrParams = new SqlParameter[21];

            arrParams[0] = new SqlParameter("@PID_To", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.PID_To, o.PID_To.GetTypeCode()));
            arrParams[1] = new SqlParameter("@PID_From", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.PID_From, o.PID_From.GetTypeCode()));
            arrParams[2] = new SqlParameter("@isQuestion", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.isQuestion, o.isQuestion.GetTypeCode()));
            arrParams[3] = new SqlParameter("@Subject", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.Subject, o.Subject.GetTypeCode()));
            arrParams[4] = new SqlParameter("@Body", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.Body, o.Body.GetTypeCode()));
            arrParams[5] = new SqlParameter("@AddedDate", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.AddedDate, o.AddedDate.GetTypeCode()));
            arrParams[6] = new SqlParameter("@AddedUser", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.AddedUser, o.AddedUser.GetTypeCode()));
            arrParams[7] = new SqlParameter("@LastModDate", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.LastModDate, o.LastModDate.GetTypeCode()));
            arrParams[8] = new SqlParameter("@LastModUser", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.LastModUser, o.LastModUser.GetTypeCode()));

            arrParams[9] = new SqlParameter("@TenID", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.TenID, o.TenID.GetTypeCode()));
            arrParams[10] = new SqlParameter("@FldInt1", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.FldInt1, o.FldInt1.GetTypeCode()));
            arrParams[11] = new SqlParameter("@FldInt2", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.FldInt2, o.FldInt2.GetTypeCode()));
            arrParams[12] = new SqlParameter("@FldInt3", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.FldInt3, o.FldInt3.GetTypeCode()));
            arrParams[13] = new SqlParameter("@FldBit1", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.FldBit1, o.FldBit1.GetTypeCode()));
            arrParams[14] = new SqlParameter("@FldBit2", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.FldBit2, o.FldBit2.GetTypeCode()));
            arrParams[15] = new SqlParameter("@FldBit3", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.FldBit3, o.FldBit3.GetTypeCode()));
            arrParams[16] = new SqlParameter("@FldText1", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.FldText1, o.FldText1.GetTypeCode()));
            arrParams[17] = new SqlParameter("@FldText2", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.FldText2, o.FldText2.GetTypeCode()));
            arrParams[18] = new SqlParameter("@FldText3", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.FldText3, o.FldText3.GetTypeCode()));

            arrParams[19] = new SqlParameter("@isUnread", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.isUnread, o.isUnread.GetTypeCode()));

            arrParams[20] = new SqlParameter("@NID", GRA.SRP.Core.Utilities.GlobalUtilities.DBSafeValue(o.NID, o.NID.GetTypeCode()));
            arrParams[20].Direction = ParameterDirection.Output;

            SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, "app_Notifications_Insert", arrParams);

            o.NID = int.Parse(arrParams[20].Value.ToString());

            return o.NID;

        }
        public bool Fetch(int NID) {

            // declare reader

            SqlDataReader dr;

            SqlParameter[] arrParams = new SqlParameter[1];

            arrParams[0] = new SqlParameter("@NID", NID);

            dr = SqlHelper.ExecuteReader(conn, CommandType.StoredProcedure, "app_Notifications_GetByID", arrParams);

            if(dr.Read()) {

                // declare return value

                Notifications result = new Notifications();

                DateTime _datetime;

                int _int;

                //decimal _decimal;

                if(int.TryParse(dr["NID"].ToString(), out _int))
                    this.NID = _int;
                if(int.TryParse(dr["PID_To"].ToString(), out _int))
                    this.PID_To = _int;
                if(int.TryParse(dr["PID_From"].ToString(), out _int))
                    this.PID_From = _int;
                this.isQuestion = bool.Parse(dr["isQuestion"].ToString());
                this.Subject = dr["Subject"].ToString();
                this.Body = dr["Body"].ToString();
                if(DateTime.TryParse(dr["AddedDate"].ToString(), out _datetime))
                    this.AddedDate = _datetime;
                this.AddedUser = dr["AddedUser"].ToString();
                if(DateTime.TryParse(dr["LastModDate"].ToString(), out _datetime))
                    this.LastModDate = _datetime;
                this.LastModUser = dr["LastModUser"].ToString();

                if(int.TryParse(dr["TenID"].ToString(), out _int))
                    this.TenID = _int;
                if(int.TryParse(dr["FldInt1"].ToString(), out _int))
                    this.FldInt1 = _int;
                if(int.TryParse(dr["FldInt2"].ToString(), out _int))
                    this.FldInt2 = _int;
                if(int.TryParse(dr["FldInt3"].ToString(), out _int))
                    this.FldInt3 = _int;
                this.FldBit1 = bool.Parse(dr["FldBit1"].ToString());
                this.FldBit2 = bool.Parse(dr["FldBit2"].ToString());
                this.FldBit3 = bool.Parse(dr["FldBit3"].ToString());
                this.FldText1 = dr["FldText1"].ToString();
                this.FldText2 = dr["FldText2"].ToString();
                this.FldText3 = dr["FldText3"].ToString();

                this.isUnread = bool.Parse(dr["isUnread"].ToString());

                dr.Close();

                return true;

            }

            dr.Close();

            return false;

        }
        //protected void btnUnread_Click(object sender, EventArgs e)
        //{
        //    var o = Notifications.FetchObject(int.Parse(NID.Text));
        //    o.isUnread = true;
        //    o.Update();
        //    Response.Redirect("~/Mail/");
        //}

        protected void btnAskSubmit_Click(object sender, EventArgs e) {
            bool somethingHasError = false;
            if(string.IsNullOrWhiteSpace(txtSubject.Text)) {
                somethingHasError = true;
                this.SubjectHasError = "has-error";
            }

            if(string.IsNullOrWhiteSpace(txtBody.Text)) {
                somethingHasError = true;
                this.BodyHasError = "has-error";
            }

            if(!somethingHasError) {
                var o = new Notifications();
                o.PID_From = ((Patron)Session["Patron"]).PID;
                o.PID_To = 0;
                o.isQuestion = true;
                o.Subject = txtSubject.Text;
                o.Body = txtBody.Text;
                o.AddedDate = o.LastModDate = DateTime.Now;
                o.LastModUser = o.AddedUser = ((Patron)Session["Patron"]).Username;
                o.Insert();
                txtSubject.Text = txtBody.Text= string.Empty;

                pnlAsk.Visible = false;
                pnlList.Visible = true;
                new SessionTools(Session).AlertPatron(((BaseSRPPage)this.Page).GetResourceString("notifications-message-sent"),
                                                      glyphicon: "send");
                Response.Redirect("~/Mail/");
            }
        }
        public static int DrawWinners(int PDID, int numWinners, int additional = 0)
        {
            SqlParameter[] arrParams = new SqlParameter[3];
            arrParams[0] = new SqlParameter("@PDID", PDID);
            arrParams[1] = new SqlParameter("@NumWinners", numWinners);
            arrParams[2] = new SqlParameter("@Additional", additional);
            var ds = SqlHelper.ExecuteDataset(conn, CommandType.StoredProcedure, "app_PrizeDrawing_DrawWinners", arrParams);

            var ret = ds.Tables[0].Rows.Count;

            var pd = PrizeDrawing.FetchObject(PDID);
            var pt = PrizeTemplate.FetchObject(pd.TID);
            var now = DateTime.Now;

            for (var i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                int PID = (int)ds.Tables[0].Rows[i]["PatronID"];
                int NID = (int)ds.Tables[0].Rows[i]["NotificationID"];
                int PDWID = (int)ds.Tables[0].Rows[i]["PDWID"];

                // insert patron prize
                var pp = new PatronPrizes
                {
                    PID = PID,
                    PrizeSource = 0,
                    BadgeID = 0,
                    DrawingID = PDWID,
                    PrizeName = pd.PrizeName,
                    RedeemedFlag = false,
                    AddedDate = now,
                    LastModDate = now,
                    AddedUser = "******",
                    LastModUser = "******"
                };
                pp.Insert();

                if (pt.SendNotificationFlag)
                {
                    // generate notification
                    var not = new Notifications
                    {
                        PID_To = PID,
                        PID_From = 0,  //0 == System Notification
                        Subject = pt.NotificationSubject,
                        Body = pt.NotificationMessage,
                        isQuestion = false,
                        AddedDate = now,
                        LastModDate = now,
                        AddedUser = "******",
                        LastModUser = "******"
                    };
                    not.Insert();

                    var w = PrizeDrawingWinners.FetchObject(PDWID);
                    w.NotificationID = not.NID;
                    w.Update();
                }
                else
                {
                    var w = PrizeDrawingWinners.FetchObject(PDWID);
                    w.NotificationID = -1;
                    w.Update();
                }
            }

            return ret;

        }