public string GetPlanInfoForPublish()
    {
        string      planInfo = "";
        PrizeMember member   = PrizeMemberAuthUtils.GetMemberData();

        if (member == null)
        {
            return(planInfo);
        }
        MemberExercisePlan myPlan = this.GetCurrentMemberPlanOrStartingPlan(member.UmbracoId);

        if (myPlan == null)
        {
            return(planInfo);
        }
        PrizeExercisePlan plan = this.GetExercisePlan(myPlan.ExercisePlanId);

        char c = PrizeMemberAuthUtils.GetMemberSetting(member.UserSettings, PrizeConstants.MemberSettings.ShowProgram);

        if (c != '0')
        {
            PrizePlanProgram level = (from tbl in db.PrizePlanPrograms
                                      where tbl.Id == plan.ProgramId
                                      select tbl).SingleOrDefault();
            planInfo += level.Name;
        }

        c = PrizeMemberAuthUtils.GetMemberSetting(member.UserSettings, PrizeConstants.MemberSettings.ShowLevel);
        if (c != '0')
        {
            PrizePlanLevel level = (from tbl in db.PrizePlanLevels
                                    where tbl.Id == plan.LevelId
                                    select tbl).SingleOrDefault();
            if (!planInfo.Equals(""))
            {
                planInfo += " ";
            }
            planInfo += level.Name;
        }

        return(planInfo);
    }
    public string GetPlanName(PrizeExercisePlan plan)
    {
        string           planName    = "";
        PrizePlanProgram planProgram = (from l in db.PrizePlanPrograms
                                        where l.Id == plan.ProgramId
                                        select l).FirstOrDefault();

        PrizePlanLocation planLocation = (from l in db.PrizePlanLocations
                                          where l.Id == plan.LocationId
                                          select l).FirstOrDefault();

        PrizePlanExperience planExp = (from l in db.PrizePlanExperiences
                                       where l.Id == plan.ExperienceId
                                       select l).FirstOrDefault();

        PrizePlanLevel planLevel = (from l in db.PrizePlanLevels
                                    where l.Id == plan.LevelId
                                    select l).FirstOrDefault();

        planName = planProgram.Name + "_" + planLocation.Name + "_" + planLevel.Name + "_" + planExp.Name;
        return(planName);
    }
    protected void Insert(object sender, EventArgs e)
    {
        using (DIYPTEntities db = new DIYPTEntities())
        {
            db.Database.Connection.Open();
            {
                string pname      = ddlProgram.SelectedItem.Text;
                string plevel     = ddlLevel.SelectedItem.Text;
                string plocation  = ddlLocation.SelectedItem.Text;
                string pexpericen = ddlExperience.SelectedItem.Text;

                PrizePlanProgram planprogram = (from c in db.PrizePlanPrograms
                                                where c.Name == pname
                                                select c).FirstOrDefault();

                int pid = planprogram.Id;


                PrizePlanLocation planlocation = (from d in db.PrizePlanLocations
                                                  where d.Name == plocation
                                                  select d).FirstOrDefault();

                int locationid = planlocation.Id;


                PrizePlanLevel planlevel = (from g in db.PrizePlanLevels
                                            where g.Name == plevel
                                            select g).FirstOrDefault();

                int levelid = planlevel.Id;

                PrizePlanExperience planexperience = (from f in db.PrizePlanExperiences
                                                      where f.Name == pexpericen
                                                      select f).FirstOrDefault();

                int expid = planexperience.Id;

                double dPrice;
                if (!double.TryParse(txtPrice.Text, out dPrice))
                {
                    dPrice = 0;
                }
                PrizeExercisePlan exerciseplan;
                int trialFlag = 0;

                if (cbTrialFlag.Checked)
                {
                    exerciseplan = (
                        from ExercisePlan in db.PrizeExercisePlans
                        join Program in db.PrizePlanPrograms on ExercisePlan.ProgramId equals Program.Id
                        join Location in db.PrizePlanLocations on ExercisePlan.LocationId equals Location.Id
                        join Experience in db.PrizePlanExperiences on ExercisePlan.ExperienceId equals Experience.Id
                        join Level in db.PrizePlanLevels on ExercisePlan.LevelId equals Level.Id
                        where Program.Name == pname &&
                        Level.Name == plevel && Location.Name == plocation && Experience.Name == pexpericen && ExercisePlan.IsTrialPlan == 1
                        select ExercisePlan).FirstOrDefault();
                    trialFlag = 1;
                }
                else
                {
                    exerciseplan = (
                        from ExercisePlan in db.PrizeExercisePlans
                        join Program in db.PrizePlanPrograms on ExercisePlan.ProgramId equals Program.Id
                        join Location in db.PrizePlanLocations on ExercisePlan.LocationId equals Location.Id
                        join Experience in db.PrizePlanExperiences on ExercisePlan.ExperienceId equals Experience.Id
                        join Level in db.PrizePlanLevels on ExercisePlan.LevelId equals Level.Id
                        where Program.Name == pname &&
                        Level.Name == plevel && Location.Name == plocation && Experience.Name == pexpericen && ExercisePlan.IsTrialPlan == 0
                        select ExercisePlan).FirstOrDefault();
                }



                if (exerciseplan != null)
                {
                    string errorText = "Exercise plan already exist!";
                    Response.Write(
                        @"<SCRIPT LANGUAGE=""JavaScript"">alert('" + errorText + "')</SCRIPT>");
                }
                else
                {
                    string planname = ddlProgram.SelectedItem.Text + "_" + ddlLocation.SelectedItem.Text
                                      + "_" + ddlExperience.SelectedItem.Text + "_" + ddlLevel.SelectedItem.Text;
                    PrizeExercisePlan addplan = new PrizeExercisePlan();
                    addplan.PlanName     = planname;
                    addplan.ProgramId    = pid;
                    addplan.LevelId      = levelid;
                    addplan.ExperienceId = expid;
                    addplan.LocationId   = locationid;
                    addplan.Price        = dPrice;
                    addplan.IsTrialPlan  = trialFlag;
                    addplan.Description  = "";
                    db.PrizeExercisePlans.Add(addplan);
                    db.SaveChanges();
                    this.BindGrid();
                    ddlProgram.SelectedIndex    = 0;
                    ddlLocation.SelectedIndex   = 0;
                    ddlLevel.SelectedIndex      = 0;
                    ddlExperience.SelectedIndex = 0;
                }
            }

            //   transaction.Complete();
            db.Database.Connection.Close();
        }
    }
    public bool ChangeMemberPlanProgram(int myPlanId, string sProgram)
    {
        bool          ret = false;
        DIYPTEntities db  = new DIYPTEntities();

        try
        {
            db.Database.Connection.Open();

            MemberExercisePlan myPlan = (from c in db.MemberExercisePlans
                                         where c.Id == myPlanId
                                         select c).FirstOrDefault();
            if (myPlan == null)
            {
                return(ret);
            }

            PrizeExercisePlan plan = (from c in db.PrizeExercisePlans
                                      where c.Id == myPlan.ExercisePlanId
                                      select c).FirstOrDefault();

            if (plan == null)
            {
                return(ret);
            }

            PrizePlanProgram program = (from c in db.PrizePlanPrograms
                                        where c.Name == sProgram
                                        select c).FirstOrDefault();
            if (program == null)
            {
                return(ret);
            }

            if (program.Id == plan.ProgramId)
            {
                return(true);
            }

            PrizeExercisePlan targetPlan;
            if (plan.IsTrialPlan == 1)
            {
                targetPlan = (from c in db.PrizeExercisePlans
                              where c.ProgramId == program.Id && c.LocationId == plan.LocationId && c.ExperienceId == plan.ExperienceId && c.LevelId == plan.LevelId && c.IsTrialPlan == 1
                              select c).FirstOrDefault();
            }
            else
            {
                targetPlan = (from c in db.PrizeExercisePlans
                              where c.ProgramId == program.Id && c.LocationId == plan.LocationId && c.ExperienceId == plan.ExperienceId && c.LevelId == plan.LevelId && c.IsTrialPlan == 0
                              select c).FirstOrDefault();
            }

            if (targetPlan == null)
            {
                return(ret);
            }

            IQueryable <PrizeExercisePlanWeek> targetPlanWeeks = (from c in db.PrizeExercisePlanWeeks
                                                                  where c.ExercisePlanId == targetPlan.Id
                                                                  orderby c.StartWeek
                                                                  select c);

            foreach (var targetPlanWeek in targetPlanWeeks)
            {
                IQueryable <MemberExercisePlanWeek> myPlanWeeks = (from c in db.MemberExercisePlanWeeks
                                                                   join d in db.PrizeExercisePlanWeeks on c.ExercisePlanWeekId equals d.Id
                                                                   where c.MemberExercisePlanId == myPlan.Id && c.Week >= targetPlanWeek.StartWeek && c.Week <= targetPlanWeek.EndWeek
                                                                   select c);
                foreach (var myPlanWeek in myPlanWeeks)
                {
                    myPlanWeek.ExercisePlanWeekId = targetPlanWeek.Id;
                }
            }

            myPlan.ExercisePlanId = targetPlan.Id;

            db.SaveChanges();

            return(true);
        }
        catch
        {
            return(false);
        }
        finally
        {
            db.Dispose();
        }
    }
    public int FindNewPlan(string sProgram, string sLocation, string sLevel, string sExp, bool bIsTrial = false)
    {
        int           newPlanId = -1;
        DIYPTEntities db        = new DIYPTEntities();

        try
        {
            db.Database.Connection.Open();
            PrizePlanProgram program = (from c in db.PrizePlanPrograms
                                        where c.Name == sProgram
                                        select c).FirstOrDefault();
            if (program == null)
            {
                return(PrizeErrorCode.ERROR_WRONG_PROGRAM);
            }


            PrizePlanLocation location = (from c in db.PrizePlanLocations
                                          where c.Name == sLocation
                                          select c).FirstOrDefault();
            if (location == null)
            {
                return(PrizeErrorCode.ERROR_WRONG_LOCATION);
            }

            PrizePlanLevel level = (from c in db.PrizePlanLevels
                                    where c.Name == sLevel
                                    select c).FirstOrDefault();
            if (level == null)
            {
                return(PrizeErrorCode.ERROR_WRONG_LEVEL);
            }

            PrizePlanExperience exp = (from c in db.PrizePlanExperiences
                                       where c.Name == sExp
                                       select c).FirstOrDefault();
            if (exp == null)
            {
                return(PrizeErrorCode.ERROR_WRONG_LEVEL);
            }

            PrizeExercisePlan plan;

            if (bIsTrial == false)
            {
                plan = (from c in db.PrizeExercisePlans
                        where c.ProgramId == program.Id && c.LocationId == location.Id &&
                        c.LevelId == level.Id && c.ExperienceId == exp.Id && c.IsTrialPlan == 0
                        select c).FirstOrDefault();
            }
            else
            {
                plan = (from c in db.PrizeExercisePlans
                        where c.ProgramId == program.Id && c.LocationId == location.Id &&
                        c.LevelId == level.Id && c.ExperienceId == exp.Id && c.IsTrialPlan == 1
                        select c).FirstOrDefault();
            }

            if (plan != null)
            {
                newPlanId = plan.Id;
            }

            return(newPlanId);
        }
        finally
        {
            db.Dispose();
        }
    }