protected void DvItemCommand(object sender, DetailsViewCommandEventArgs e)
        {
            string returnURL = "~/ControlRoom/Modules/Programs/ProgramList.aspx";

            if (e.CommandName.ToLower() == "back")
            {
                Response.Redirect(returnURL);
            }
            if (e.CommandName.ToLower() == "refresh")
            {
                try
                {
                    odsData.DataBind();
                    dv.DataBind();
                    dv.ChangeMode(DetailsViewMode.Edit);

                    var masterPage = (IControlRoomMaster)Master;
                    if (masterPage != null)
                    {
                        masterPage.PageMessage = SRPResources.RefreshOK;
                    }
                }
                catch (Exception ex)
                {
                    var masterPage = (IControlRoomMaster)Master;
                    masterPage.PageError = String.Format(SRPResources.ApplicationError1, ex.Message);
                }
            }
            if (e.CommandName.ToLower() == "add" || e.CommandName.ToLower() == "addandback")
            {
                try
                {
                    var obj = new DAL.Programs();
                    //obj.GenNotificationFlag = ((CheckBox)((DetailsView)sender).FindControl("TabContainer1").FindControl("TabPanel2").FindControl("GenNotificationFlag")).Checked;

                    obj.AdminName = ((TextBox)((DetailsView)sender).FindControl("AdminName")).Text;
                    obj.Title     = ((TextBox)((DetailsView)sender).FindControl("Title")).Text;
                    obj.TabName   = ((TextBox)((DetailsView)sender).FindControl("TabName")).Text;
                    //obj.POrder = FormatHelper.SafeToInt(((TextBox)((DetailsView)sender).FindControl("POrder")).Text);
                    obj.IsActive  = ((CheckBox)((DetailsView)sender).FindControl("IsActive")).Checked;
                    obj.IsHidden  = ((CheckBox)((DetailsView)sender).FindControl("IsHidden")).Checked;
                    obj.StartDate = FormatHelper.SafeToDateTime(((TextBox)((DetailsView)sender).FindControl("StartDate")).Text);
                    obj.EndDate   = FormatHelper.SafeToDateTime(((TextBox)((DetailsView)sender).FindControl("EndDate")).Text);
                    //obj.MaxAge = FormatHelper.SafeToInt(((TextBox)((DetailsView)sender).FindControl("MaxAge")).Text);
                    //obj.MaxGrade = FormatHelper.SafeToInt(((TextBox)((DetailsView)sender).FindControl("MaxGrade")).Text);
                    obj.LoggingStart     = FormatHelper.SafeToDateTime(((TextBox)((DetailsView)sender).FindControl("LoggingStart")).Text);
                    obj.LoggingEnd       = FormatHelper.SafeToDateTime(((TextBox)((DetailsView)sender).FindControl("LoggingEnd")).Text);
                    obj.CompletionPoints = 0;
                    //obj.ParentalConsentFlag = ((CheckBox)((DetailsView)sender).FindControl("ParentalConsentFlag")).Checked;
                    //obj.ParentalConsentText = ((CKEditor.NET.CKEditorControl)((DetailsView)sender).FindControl("ParentalConsentText")).Text;
                    //obj.PatronReviewFlag = ((CheckBox)((DetailsView)sender).FindControl("PatronReviewFlag")).Checked;
                    //obj.LogoutURL = ((TextBox)((DetailsView)sender).FindControl("LogoutURL")).Text;
                    //obj.ProgramGameID = FormatHelper.SafeToInt(((DropDownList) ((DetailsView) sender).FindControl("ProgramGameID")).SelectedValue);
                    //obj.HTML1 = ((CKEditor.NET.CKEditorControl)((DetailsView)sender).FindControl("HTML1")).Text;
                    //obj.HTML2 = ((CKEditor.NET.CKEditorControl)((DetailsView)sender).FindControl("HTML2")).Text;
                    //obj.HTML3 = ((CKEditor.NET.CKEditorControl)((DetailsView)sender).FindControl("HTML3")).Text;
                    //obj.HTML4 = ((CKEditor.NET.CKEditorControl)((DetailsView)sender).FindControl("HTML4")).Text;
                    //obj.HTML5 = ((CKEditor.NET.CKEditorControl)((DetailsView)sender).FindControl("HTML5")).Text;
                    //obj.HTML6 = ((CKEditor.NET.CKEditorControl)((DetailsView)sender).FindControl("HTML6")).Text;
                    //obj.BannerImage = ((TextBox)((DetailsView)sender).FindControl("BannerImage")).Text;
                    //obj.RegistrationBadgeID = FormatHelper.SafeToInt(((DropDownList) ((DetailsView) sender).FindControl("RegistrationBadgeID")).SelectedValue);

                    obj.AddedDate   = DateTime.Now;
                    obj.AddedUser   = ((SRPUser)Session[SessionData.UserProfile.ToString()]).Username; //"N/A";  // Get from session
                    obj.LastModDate = obj.AddedDate;
                    obj.LastModUser = obj.AddedUser;

                    if (obj.IsValid(BusinessRulesValidationMode.INSERT))
                    {
                        obj.Insert();

                        File.Copy(Server.MapPath("~/css/program/default.css"), Server.MapPath("~/css/program/" + obj.PID.ToString() + ".css"), true);
                        File.Copy(Server.MapPath("~/images/banners/default.png"), Server.MapPath("~/images/banners/" + obj.PID.ToString() + ".png"), true);
                        File.Copy(Server.MapPath("~/resources/program.default.en-US.txt"), Server.MapPath("~/resources/program." + obj.PID.ToString() + ".en-US.txt"), true);
                        //CompileResourceFile(Server.MapPath("~/resources/program." + obj.PID.ToString() + ".en-US.txt"));

                        foreach (ActivityType val in Enum.GetValues(typeof(ActivityType)))
                        {
                            var o = new ProgramGamePointConversion();
                            o.PGID           = obj.PID;
                            o.ActivityTypeId = (int)val;
                            o.ActivityCount  = 1;
                            o.PointCount     = 0;
                            o.AddedDate      = obj.AddedDate;
                            o.AddedUser      = obj.AddedUser;
                            o.LastModDate    = o.AddedDate;
                            o.LastModUser    = o.AddedUser;

                            o.Insert();
                        }

                        if (e.CommandName.ToLower() == "addandback")
                        {
                            Response.Redirect(returnURL);
                        }

                        lblPK.Text = obj.PID.ToString();

                        odsData.DataBind();
                        dv.DataBind();
                        dv.ChangeMode(DetailsViewMode.Edit);

                        var masterPage = (IControlRoomMaster)Master;
                        masterPage.PageMessage = SRPResources.AddedOK;
                    }
                    else
                    {
                        var    masterPage = (IControlRoomMaster)Master;
                        string message    = String.Format(SRPResources.ApplicationError1, "<ul>");
                        foreach (BusinessRulesValidationMessage m in obj.ErrorCodes)
                        {
                            message = string.Format(String.Format("{0}<li>{{0}}</li>", message), m.ErrorMessage);
                        }
                        message = string.Format("{0}</ul>", message);
                        masterPage.PageError = message;
                    }
                }
                catch (Exception ex)
                {
                    var masterPage = (IControlRoomMaster)Master;
                    masterPage.PageError = String.Format(SRPResources.ApplicationError1, ex.Message);
                }
            }
            if (e.CommandName.ToLower() == "save" || e.CommandName.ToLower() == "saveandback")
            {
                try
                {
                    var obj = new DAL.Programs();
                    int pk  = int.Parse(((TextBox)((DetailsView)sender).FindControl("TabContainer1").FindControl("TabPanel1").FindControl("PID")).Text);
                    obj.Fetch(pk);

                    obj.AdminName = ((TextBox)((DetailsView)sender).FindControl("TabContainer1").FindControl("TabPanel1").FindControl("AdminName")).Text;
                    obj.Title     = ((TextBox)((DetailsView)sender).FindControl("TabContainer1").FindControl("TabPanel1").FindControl("Title")).Text;
                    obj.TabName   = ((TextBox)((DetailsView)sender).FindControl("TabContainer1").FindControl("TabPanel1").FindControl("TabName")).Text;
                    //obj.POrder = FormatHelper.SafeToInt(((TextBox)((DetailsView)sender).FindControl("POrder")).Text);
                    obj.IsActive            = ((CheckBox)((DetailsView)sender).FindControl("TabContainer1").FindControl("TabPanel1").FindControl("IsActive")).Checked;
                    obj.IsHidden            = ((CheckBox)((DetailsView)sender).FindControl("TabContainer1").FindControl("TabPanel1").FindControl("IsHidden")).Checked;
                    obj.StartDate           = FormatHelper.SafeToDateTime(((TextBox)((DetailsView)sender).FindControl("TabContainer1").FindControl("TabPanel1").FindControl("StartDate")).Text);
                    obj.EndDate             = FormatHelper.SafeToDateTime(((TextBox)((DetailsView)sender).FindControl("TabContainer1").FindControl("TabPanel1").FindControl("EndDate")).Text);
                    obj.MaxAge              = FormatHelper.SafeToInt(((TextBox)((DetailsView)sender).FindControl("TabContainer1").FindControl("TabPanel1").FindControl("MaxAge")).Text);
                    obj.MaxGrade            = FormatHelper.SafeToInt(((TextBox)((DetailsView)sender).FindControl("TabContainer1").FindControl("TabPanel1").FindControl("MaxGrade")).Text);
                    obj.LoggingStart        = FormatHelper.SafeToDateTime(((TextBox)((DetailsView)sender).FindControl("TabContainer1").FindControl("TabPanel1").FindControl("LoggingStart")).Text);
                    obj.LoggingEnd          = FormatHelper.SafeToDateTime(((TextBox)((DetailsView)sender).FindControl("TabContainer1").FindControl("TabPanel1").FindControl("LoggingEnd")).Text);
                    obj.ParentalConsentFlag = ((CheckBox)((DetailsView)sender).FindControl("TabContainer1").FindControl("TabPanel1").FindControl("ParentalConsentFlag")).Checked;
                    obj.ParentalConsentText = ((CKEditor.NET.CKEditorControl)((DetailsView)sender).FindControl("TabContainer1").FindControl("TabPanel1").FindControl("ParentalConsentText")).Text;
                    obj.PatronReviewFlag    = ((CheckBox)((DetailsView)sender).FindControl("TabContainer1").FindControl("TabPanel1").FindControl("PatronReviewFlag")).Checked;
                    obj.LogoutURL           = ((TextBox)((DetailsView)sender).FindControl("TabContainer1").FindControl("TabPanel1").FindControl("LogoutURL")).Text;

                    obj.CompletionPoints = FormatHelper.SafeToInt(((TextBox)((DetailsView)sender).FindControl("TabContainer1").FindControl("TabPanel2").FindControl("CompletionPoints")).Text);
                    obj.ProgramGameID    = FormatHelper.SafeToInt(((DropDownList)((DetailsView)sender).FindControl("TabContainer1").FindControl("TabPanel2").FindControl("ProgramGameID")).Text);
                    //obj.ProgramGameID = 0;
                    obj.RegistrationBadgeID = FormatHelper.SafeToInt(((DropDownList)((DetailsView)sender).FindControl("TabContainer1").FindControl("TabPanel2").FindControl("RegistrationBadgeID")).Text);
                    //obj.RegistrationBadgeID = 0;
                    obj.HTML1 = ((CKEditor.NET.CKEditorControl)((DetailsView)sender).FindControl("TabContainer1").FindControl("TabPanel2").FindControl("HTML1")).Text;
                    obj.HTML2 = ((CKEditor.NET.CKEditorControl)((DetailsView)sender).FindControl("TabContainer1").FindControl("TabPanel2").FindControl("HTML2")).Text;

                    obj.HTML3 = ((CKEditor.NET.CKEditorControl)((DetailsView)sender).FindControl("TabContainer1").FindControl("TabPanel3").FindControl("HTML3")).Text;
                    obj.HTML4 = ((CKEditor.NET.CKEditorControl)((DetailsView)sender).FindControl("TabContainer1").FindControl("TabPanel3").FindControl("HTML4")).Text;

                    obj.HTML5 = ((CKEditor.NET.CKEditorControl)((DetailsView)sender).FindControl("TabContainer1").FindControl("TabPanel4").FindControl("HTML5")).Text;
                    obj.HTML6 = ((CKEditor.NET.CKEditorControl)((DetailsView)sender).FindControl("TabContainer1").FindControl("TabPanel4").FindControl("HTML6")).Text;

                    obj.BannerImage = "";//((TextBox)((DetailsView)sender).FindControl("BannerImage")).Text;


                    obj.LastModDate = DateTime.Now;
                    obj.LastModUser = ((SRPUser)Session[SessionData.UserProfile.ToString()]).Username;  //"N/A";  // Get from session

                    if (obj.IsValid(BusinessRulesValidationMode.UPDATE))
                    {
                        obj.Update();

                        var rptr = ((Repeater)((DetailsView)sender).FindControl("TabContainer1").FindControl("TabPanel5").FindControl("rptr"));
                        if (rptr != null)
                        {
                            foreach (RepeaterItem item in rptr.Items)
                            {
                                var PGCID = int.Parse(((TextBox)item.FindControl("PGCID")).Text);
                                var ActivityCountTxt = ((TextBox)item.FindControl("ActivityCount")).Text;
                                var PointCountTxt = ((TextBox)item.FindControl("PointCount")).Text;
                                int ActivityCount = 1, PointCount = 0;
                                int.TryParse(ActivityCountTxt, out ActivityCount);
                                int.TryParse(PointCountTxt, out PointCount);

                                var o = ProgramGamePointConversion.FetchObject(PGCID);
                                if (o == null)
                                {
                                    continue;
                                }
                                o.ActivityCount = ActivityCount;
                                o.PointCount    = PointCount;
                                o.LastModDate   = obj.LastModDate;
                                o.LastModUser   = obj.LastModUser;
                                o.Update();
                            }
                        }


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

                        odsData.DataBind();
                        dv.DataBind();
                        dv.ChangeMode(DetailsViewMode.Edit);

                        var masterPage = (IControlRoomMaster)Master;
                        masterPage.PageMessage = SRPResources.SaveOK;
                    }
                    else
                    {
                        var    masterPage = (IControlRoomMaster)Master;
                        string message    = String.Format(SRPResources.ApplicationError1, "<ul>");
                        foreach (BusinessRulesValidationMessage m in obj.ErrorCodes)
                        {
                            message = string.Format(String.Format("{0}<li>{{0}}</li>", message), m.ErrorMessage);
                        }
                        message = string.Format("{0}</ul>", message);
                        masterPage.PageError = message;
                    }
                }
                catch (Exception ex)
                {
                    var masterPage = (IControlRoomMaster)Master;
                    masterPage.PageError = String.Format(SRPResources.ApplicationError1, ex.Message);
                }
            }
            if (e.CommandName.ToLower() == "gen")
            {
                int PK = int.Parse(lblPK.Text);
                var ds = ProgramCodes.GetProgramStats(PK);

                int TotalCodes = (int)ds.Tables[0].Rows[0]["TotalCodes"];
                var txt        = (TextBox)dv.FindControl("TabContainer1").FindControl("TabPanel6").FindControl("txtGen");
                int addl       = int.Parse(txt.Text);

                ProgramCodes.Generate(TotalCodes + 1, TotalCodes + addl, PK);

                odsData.DataBind();
                dv.DataBind();
                dv.ChangeMode(DetailsViewMode.Edit);

                var masterPage = (IControlRoomMaster)Master;
                masterPage.PageMessage = "Additional codes generated!";
            }
            if (e.CommandName.ToLower() == "exp")
            {
                int PK = int.Parse(lblPK.Text);

                var ds = ProgramCodes.GetExportList(PK);

                string excelFilename = Server.MapPath("~/ProgramRewardCodes.xlsx");
                CreateExcelFile.CreateExcelDocument(ds, excelFilename);

                Response.ContentType = "application/vnd.ms-excel";
                Response.AppendHeader("Content-Disposition", "attachment; filename=ProgramRewardCodes.xlsx");
                EnableViewState = false;
                Response.BinaryWrite(File.ReadAllBytes(excelFilename));
                File.Delete(excelFilename);
                Response.End();


                var masterPage = (IControlRoomMaster)Master;
                masterPage.PageMessage = "Codes Exported!";
            }
        }
        protected void DvItemCommand(object sender, DetailsViewCommandEventArgs e)
        {
            string returnURL = "~/ControlRoom/Modules/Programs/ProgramList.aspx";
            if (e.CommandName.ToLower() == "back")
            {
                Response.Redirect(returnURL);
            }
            if (e.CommandName.ToLower() == "refresh")
            {
                try
                {
                    odsData.DataBind();
                    dv.DataBind();
                    dv.ChangeMode(DetailsViewMode.Edit);

                    var masterPage = (IControlRoomMaster)Master;
                    if (masterPage != null)
                        masterPage.PageMessage = SRPResources.RefreshOK;
                }
                catch (Exception ex)
                {
                    var masterPage = (IControlRoomMaster)Master;
                    masterPage.PageError = String.Format(SRPResources.ApplicationError1, ex.Message);
                }
            }
            if (e.CommandName.ToLower() == "add" || e.CommandName.ToLower() == "addandback")
            {
                try
                {
                    var program = new DAL.Programs();
                    //obj.GenNotificationFlag = ((CheckBox)((DetailsView)sender).FindControl("tc1").FindControl("tp2").FindControl("GenNotificationFlag")).Checked;

                    program.AdminName = ((TextBox)((DetailsView)sender).FindControl("AdminName")).Text;
                    program.Title = ((TextBox)((DetailsView)sender).FindControl("Title")).Text;
                    program.TabName = ((TextBox)((DetailsView)sender).FindControl("TabName")).Text;
                    //obj.POrder = FormatHelper.SafeToInt(((TextBox)((DetailsView)sender).FindControl("POrder")).Text);
                    program.IsActive = ((CheckBox)((DetailsView)sender).FindControl("IsActive")).Checked;
                    program.IsHidden = ((CheckBox)((DetailsView)sender).FindControl("IsHidden")).Checked;
                    program.StartDate = FormatHelper.SafeToDateTime(((TextBox)((DetailsView)sender).FindControl("StartDate")).Text);
                    program.EndDate = FormatHelper.SafeToDateTime(((TextBox)((DetailsView)sender).FindControl("EndDate")).Text);
                    //obj.MaxAge = FormatHelper.SafeToInt(((TextBox)((DetailsView)sender).FindControl("MaxAge")).Text);
                    //obj.MaxGrade = FormatHelper.SafeToInt(((TextBox)((DetailsView)sender).FindControl("MaxGrade")).Text);
                    program.LoggingStart = FormatHelper.SafeToDateTime(((TextBox)((DetailsView)sender).FindControl("LoggingStart")).Text);
                    program.LoggingEnd = FormatHelper.SafeToDateTime(((TextBox)((DetailsView)sender).FindControl("LoggingEnd")).Text);
                    program.CompletionPoints = 0;
                    //obj.ParentalConsentFlag = ((CheckBox)((DetailsView)sender).FindControl("ParentalConsentFlag")).Checked;
                    //obj.ParentalConsentText = ((HtmlTextArea)((DetailsView)sender).FindControl("ParentalConsentText")).Text;
                    //obj.PatronReviewFlag = ((CheckBox)((DetailsView)sender).FindControl("PatronReviewFlag")).Checked;
                    //obj.LogoutURL = ((TextBox)((DetailsView)sender).FindControl("LogoutURL")).Text;
                    //obj.ProgramGameID = FormatHelper.SafeToInt(((DropDownList) ((DetailsView) sender).FindControl("ProgramGameID")).SelectedValue);
                    //obj.HTML1 = ((HtmlTextArea)((DetailsView)sender).FindControl("HTML1")).Text;
                    //obj.HTML2 = ((HtmlTextArea)((DetailsView)sender).FindControl("HTML2")).Text;
                    //obj.HTML3 = ((HtmlTextArea)((DetailsView)sender).FindControl("HTML3")).Text;
                    //obj.HTML4 = ((HtmlTextArea)((DetailsView)sender).FindControl("HTML4")).Text;
                    //obj.HTML5 = ((HtmlTextArea)((DetailsView)sender).FindControl("HTML5")).Text;
                    //obj.HTML6 = ((HtmlTextArea)((DetailsView)sender).FindControl("HTML6")).Text;
                    //obj.BannerImage = ((TextBox)((DetailsView)sender).FindControl("BannerImage")).Text;
                    //obj.RegistrationBadgeID = FormatHelper.SafeToInt(((DropDownList) ((DetailsView) sender).FindControl("RegistrationBadgeID")).SelectedValue);

                    program.AddedDate = DateTime.Now;
                    program.AddedUser = ((SRPUser)Session[SessionData.UserProfile.ToString()]).Username;  //"N/A";  // Get from session
                    program.LastModDate = program.AddedDate;
                    program.LastModUser = program.AddedUser;

                    if (program.IsValid(BusinessRulesValidationMode.INSERT))
                    {
                        program.Insert();

                        File.Copy(Server.MapPath("~/css/program/default.css"), Server.MapPath("~/css/program/" + program.PID.ToString() + ".css"), true);
                        File.Copy(Server.MapPath("~/images/meadow.jpg"), Server.MapPath("~/images/banners/" + program.PID.ToString() + ".jpg"), true);
                        File.Copy(Server.MapPath("~/images/[email protected]"), Server.MapPath("~/images/banners/" + program.PID.ToString() + ".jpg"), true);
                        File.Copy(Server.MapPath("~/resources/program.default.en-US.txt"), Server.MapPath("~/resources/program." + program.PID.ToString() + ".en-US.txt"), true);
                        //CompileResourceFile(Server.MapPath("~/resources/program." + obj.PID.ToString() + ".en-US.txt"));

                        foreach (ActivityType val in Enum.GetValues(typeof(ActivityType)))
                        {
                            var o = new ProgramGamePointConversion();
                            o.PGID = program.PID;
                            o.ActivityTypeId = (int)val;
                            o.ActivityCount = 1;
                            o.PointCount = 0;
                            o.AddedDate = program.AddedDate;
                            o.AddedUser = program.AddedUser;
                            o.LastModDate = o.AddedDate;
                            o.LastModUser = o.AddedUser;

                            o.Insert();

                            string cacheValue = string.Format("{0}.{1}.{2}",
                                  CacheKey.PointGameConversionStub,
                                  o.PGID,
                                  o.ActivityTypeId);
                            new SessionTools(Session).RemoveCache(Cache, cacheValue);
                        }

                        if (e.CommandName.ToLower() == "addandback")
                        {
                            Response.Redirect(returnURL);
                        }

                        lblPK.Text = program.PID.ToString();

                        odsData.DataBind();
                        dv.DataBind();
                        dv.ChangeMode(DetailsViewMode.Edit);

                        var masterPage = (IControlRoomMaster)Master;
                        masterPage.PageMessage = SRPResources.AddedOK;
                    }
                    else
                    {
                        var masterPage = (IControlRoomMaster)Master;
                        string message = String.Format(SRPResources.ApplicationError1, "<ul>");
                        foreach (BusinessRulesValidationMessage m in program.ErrorCodes)
                        {
                            message = string.Format(String.Format("{0}<li>{{0}}</li>", message), m.ErrorMessage);
                        }
                        message = string.Format("{0}</ul>", message);
                        masterPage.PageError = message;
                    }
                }
                catch (Exception ex)
                {
                    var masterPage = (IControlRoomMaster)Master;
                    masterPage.PageError = String.Format(SRPResources.ApplicationError1, ex.Message);
                }
            }
            if (e.CommandName.ToLower() == "save" || e.CommandName.ToLower() == "saveandback")
            {
                try
                {
                    var program = new DAL.Programs();
                    int pk = int.Parse(((TextBox)((DetailsView)sender).FindControl("tc1").FindControl("tp1").FindControl("PID")).Text);
                    program.Fetch(pk);

                    program.AdminName = ((TextBox)((DetailsView)sender).FindControl("tc1").FindControl("tp1").FindControl("AdminName")).Text;
                    program.Title = ((TextBox)((DetailsView)sender).FindControl("tc1").FindControl("tp1").FindControl("Title")).Text;
                    program.TabName = ((TextBox)((DetailsView)sender).FindControl("tc1").FindControl("tp1").FindControl("TabName")).Text;
                    //obj.POrder = FormatHelper.SafeToInt(((TextBox)((DetailsView)sender).FindControl("POrder")).Text);
                    program.IsActive = ((CheckBox)((DetailsView)sender).FindControl("tc1").FindControl("tp1").FindControl("IsActive")).Checked;
                    program.IsHidden = ((CheckBox)((DetailsView)sender).FindControl("tc1").FindControl("tp1").FindControl("IsHidden")).Checked;
                    program.StartDate = FormatHelper.SafeToDateTime(((TextBox)((DetailsView)sender).FindControl("tc1").FindControl("tp1").FindControl("StartDate")).Text);
                    program.EndDate = FormatHelper.SafeToDateTime(((TextBox)((DetailsView)sender).FindControl("tc1").FindControl("tp1").FindControl("EndDate")).Text);
                    program.MaxAge = FormatHelper.SafeToInt(((TextBox)((DetailsView)sender).FindControl("tc1").FindControl("tp1").FindControl("MaxAge")).Text);
                    program.MaxGrade = FormatHelper.SafeToInt(((TextBox)((DetailsView)sender).FindControl("tc1").FindControl("tp1").FindControl("MaxGrade")).Text);
                    program.LoggingStart = FormatHelper.SafeToDateTime(((TextBox)((DetailsView)sender).FindControl("tc1").FindControl("tp1").FindControl("LoggingStart")).Text);
                    program.LoggingEnd = FormatHelper.SafeToDateTime(((TextBox)((DetailsView)sender).FindControl("tc1").FindControl("tp1").FindControl("LoggingEnd")).Text);
                    program.ParentalConsentFlag = ((CheckBox)((DetailsView)sender).FindControl("tc1").FindControl("tp1").FindControl("ParentalConsentFlag")).Checked;
                    program.ParentalConsentText = ((HtmlTextArea)((DetailsView)sender).FindControl("tc1").FindControl("tp1").FindControl("ParentalConsentText")).InnerHtml;
                    program.PatronReviewFlag = ((CheckBox)((DetailsView)sender).FindControl("tc1").FindControl("tp1").FindControl("PatronReviewFlag")).Checked;
                    program.RequireBookDetails = ((CheckBox)((DetailsView)sender).FindControl("tc1").FindControl("tp1").FindControl("RequireBookDetails")).Checked;
                    program.LogoutURL = ((TextBox)((DetailsView)sender).FindControl("tc1").FindControl("tp1").FindControl("LogoutURL")).Text;

                    program.CompletionPoints = FormatHelper.SafeToInt(((TextBox)((DetailsView)sender).FindControl("tc1").FindControl("tp2").FindControl("CompletionPoints")).Text);
                    program.ProgramGameID = FormatHelper.SafeToInt(((DropDownList)((DetailsView)sender).FindControl("tc1").FindControl("tp2").FindControl("ProgramGameID")).Text);
                    //obj.ProgramGameID = 0;
                    program.RegistrationBadgeID = FormatHelper.SafeToInt(((DropDownList)((DetailsView)sender).FindControl("tc1").FindControl("tp2").FindControl("RegistrationBadgeID")).Text);
                    //obj.RegistrationBadgeID = 0;
                    program.HTML1 = ((HtmlTextArea)((DetailsView)sender).FindControl("tc1").FindControl("tp2").FindControl("HTML1")).InnerHtml;
                    program.HTML2 = ((HtmlTextArea)((DetailsView)sender).FindControl("tc1").FindControl("tp2").FindControl("HTML2")).InnerHtml;

                    program.HTML3 = ((HtmlTextArea)((DetailsView)sender).FindControl("tc1").FindControl("tp3").FindControl("HTML3")).InnerHtml;
                    program.HTML4 = ((HtmlTextArea)((DetailsView)sender).FindControl("tc1").FindControl("tp3").FindControl("HTML4")).InnerHtml;

                    program.HTML5 = ((HtmlTextArea)((DetailsView)sender).FindControl("tc1").FindControl("tp4").FindControl("HTML5")).InnerHtml;
                    program.HTML6 = ((HtmlTextArea)((DetailsView)sender).FindControl("tc1").FindControl("tp4").FindControl("HTML6")).InnerHtml;

                    program.BannerImage = string.Empty;//((TextBox)((DetailsView)sender).FindControl("BannerImage")).Text;

                    program.PreTestEndDate = ((TextBox)((DetailsView)sender).FindControl("tc1").FindControl("tp5").FindControl("PreTestEndDate")).Text.SafeToDateTime();
                    program.PostTestStartDate = ((TextBox)((DetailsView)sender).FindControl("tc1").FindControl("tp5").FindControl("PostTestStartDate")).Text.SafeToDateTime();
                    program.PreTestID = ((DropDownList)((DetailsView)sender).FindControl("tc1").FindControl("tp5").FindControl("PreTestID")).SelectedValue.SafeToInt();
                    program.PostTestID = ((DropDownList)((DetailsView)sender).FindControl("tc1").FindControl("tp5").FindControl("PostTestID")).SelectedValue.SafeToInt();
                    program.PreTestMandatory = ((CheckBox)((DetailsView)sender).FindControl("tc1").FindControl("tp5").FindControl("PreTestMandatory")).Checked;

                    program.GoalDefault = FormatHelper.SafeToInt(((TextBox)((DetailsView)sender).FindControl("tc1").FindControl("tp1").FindControl("GoalDefault")).Text);
                    program.GoalMin = FormatHelper.SafeToInt(((TextBox)((DetailsView)sender).FindControl("tc1").FindControl("tp1").FindControl("GoalMin")).Text);
                    program.GoalMax = FormatHelper.SafeToInt(((TextBox)((DetailsView)sender).FindControl("tc1").FindControl("tp1").FindControl("GoalMax")).Text);
                    program.GoalIntervalId = FormatHelper.SafeToInt(((DropDownList)((DetailsView)sender).FindControl("tc1").FindControl("tp1").FindControl("GoalIntervalId")).SelectedValue);

                    program.HideSchoolInRegistration = ((CheckBox)((DetailsView)sender).FindControl("tc1").FindControl("tp1").FindControl("HideSchoolInRegistration")).Checked;
                    program.DisplayDailyImage = ((CheckBox)((DetailsView)sender).FindControl("tc1").FindControl("tp1").FindControl("DisplayDailyImage")).Checked;

                    program.LastModDate = DateTime.Now;
                    program.LastModUser = ((SRPUser)Session[SessionData.UserProfile.ToString()]).Username;  //"N/A";  // Get from session

                    if (program.IsValid(BusinessRulesValidationMode.UPDATE))
                    {
                        program.Update();

                        var rptr = ((Repeater)((DetailsView)sender).FindControl("tc1").FindControl("tp5").FindControl("rptr"));
                        if (rptr != null)
                        {
                            foreach (RepeaterItem item in rptr.Items)
                            {
                                var PGCID = int.Parse(((TextBox)item.FindControl("PGCID")).Text);
                                var ActivityCountTxt = ((TextBox)item.FindControl("ActivityCount")).Text;
                                var PointCountTxt = ((TextBox)item.FindControl("PointCount")).Text;
                                int ActivityCount = 1, PointCount = 0;
                                int.TryParse(ActivityCountTxt, out ActivityCount);
                                int.TryParse(PointCountTxt, out PointCount);

                                var o = ProgramGamePointConversion.FetchObject(PGCID);
                                if (o == null)
                                    continue;
                                o.ActivityCount = ActivityCount;
                                o.PointCount = PointCount;
                                o.LastModDate = program.LastModDate;
                                o.LastModUser = program.LastModUser;
                                o.Update();

                                string cacheValue = string.Format("{0}.{1}.{2}",
                                                                  CacheKey.PointGameConversionStub,
                                                                  o.PGID,
                                                                  o.ActivityTypeId);
                                new SessionTools(Session).RemoveCache(Cache, cacheValue);
                            }

                        }


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

                        odsData.DataBind();
                        dv.DataBind();
                        dv.ChangeMode(DetailsViewMode.Edit);

                        var masterPage = (IControlRoomMaster)Master;
                        masterPage.PageMessage = SRPResources.SaveOK;
                    }
                    else
                    {
                        var masterPage = (IControlRoomMaster)Master;
                        string message = String.Format(SRPResources.ApplicationError1, "<ul>");
                        foreach (BusinessRulesValidationMessage m in program.ErrorCodes)
                        {
                            message = string.Format(String.Format("{0}<li>{{0}}</li>", message), m.ErrorMessage);
                        }
                        message = string.Format("{0}</ul>", message);
                        masterPage.PageError = message;
                    }
                }
                catch (Exception ex)
                {
                    var masterPage = (IControlRoomMaster)Master;
                    masterPage.PageError = String.Format(SRPResources.ApplicationError1, ex.Message);
                }
            }
            if (e.CommandName.ToLower() == "gen")
            {

                int PK = int.Parse(lblPK.Text);
                var ds = ProgramCodes.GetProgramStats(PK);

                int TotalCodes = (int)ds.Tables[0].Rows[0]["TotalCodes"];
                var txt = (TextBox)dv.FindControl("tc1").FindControl("tp6").FindControl("txtGen");
                int addl = int.Parse(txt.Text);

                string result = ProgramCodes.Generate(TotalCodes + 1, TotalCodes + addl, PK);

                odsData.DataBind();
                dv.DataBind();
                dv.ChangeMode(DetailsViewMode.Edit);

                var masterPage = (IControlRoomMaster)Master;
                masterPage.PageMessage = result;

            }
            if (e.CommandName.ToLower() == "exp")
            {

                int PK = int.Parse(lblPK.Text);

                var ds = ProgramCodes.GetExportList(PK);

                string excelFilename = Server.MapPath("~/ProgramRewardCodes.xlsx");
                CreateExcelFile.CreateExcelDocument(ds, excelFilename);

                Response.ContentType = "application/vnd.ms-excel";
                Response.AppendHeader("Content-Disposition", "attachment; filename=ProgramRewardCodes.xlsx");
                EnableViewState = false;
                Response.BinaryWrite(File.ReadAllBytes(excelFilename));
                File.Delete(excelFilename);
                Response.End();


                var masterPage = (IControlRoomMaster)Master;
                masterPage.PageMessage = "Codes Exported!";

            }
        }