protected void bSaveWOD_Click(object sender, EventArgs e)
        {
            string status = string.Empty;

            try
            {
                JavaScriptSerializer serializer = new JavaScriptSerializer();
                Affine.Data.json.WOD wodJson    = serializer.Deserialize <Affine.Data.json.WOD>(atiWodJson.Value);
                aqufitEntities       entities   = new aqufitEntities();

                // TODO: check if there is already a WOD of the same name
                long wodTypeId = Convert.ToInt64(ddlWODType.SelectedValue);
                int  standard  = UserInfo.IsSuperUser ? 1 : 0;
                long userKey   = GroupSettings != null ? this.GroupSettings.Id : this.UserSettings.Id;
                WOD  wod       = new WOD()
                {
                    Difficulty      = wodJson.Difficulty,
                    Standard        = (short)standard,
                    Name            = Affine.Utils.Web.WebUtils.MakeWebSafeString(txtWorkoutName.Text),
                    WODType         = entities.WODTypes.First(t => t.Id == wodTypeId),
                    UserSettingsKey = userKey,
                    Description     = Affine.Utils.Web.WebUtils.MakeWebSafeString(txtDescription.Text),
                    UserName        = GroupSettings != null ? this.GroupSettings.UserName : this.UserSettings.UserName,
                    CreationDate    = DateTime.Now.ToUniversalTime()
                };
                if (wodTypeId == (int)Affine.Utils.WorkoutUtil.WodType.AMRAP)
                {
                    wod.TimeSpan = atiTimeSpan.Time;
                }
                entities.AddToWODs(wod);
                foreach (Affine.Data.json.WODSet setJson in wodJson.WODSets)
                {
                    WODSet wodSet = new WODSet()
                    {
                        WOD = wod
                    };
                    //Affine.Data.Exercise[] exerciseArray = entities.Exercises.Where(Affine.Utils.Linq.LinqUtils.BuildContainsExpression<Affine.Data.Exercise, long>(ex => ex.Id, setJson.WODExercises.Select(ee => ee.ExcercisKey ).ToArray() )).ToArray();
                    foreach (Affine.Data.json.WODExercise exJson in setJson.WODExercises)
                    {
                        double      mdist   = exJson.MenDist > 0 ? Utils.UnitsUtil.unitsToSystemDefualt(exJson.MenDist, Utils.UnitsUtil.ToUnit(exJson.MenDistUnits)) : -1;
                        double      wdist   = exJson.WomenDist > 0 ? Utils.UnitsUtil.unitsToSystemDefualt(exJson.WomenDist, Utils.UnitsUtil.ToUnit(exJson.WomenDistUnits)) : -1;
                        double      mweight = exJson.MenRx > 0 ? Utils.UnitsUtil.unitsToSystemDefualt(exJson.MenRx, Utils.UnitsUtil.ToUnit(exJson.MenRxUnits)) : -1;
                        double      wweight = exJson.WomenRx > 0 ? Utils.UnitsUtil.unitsToSystemDefualt(exJson.WomenRx, Utils.UnitsUtil.ToUnit(exJson.WomenRxUnits)) : -1;
                        WODExercise wodEx   = new WODExercise()
                        {
                            MenDist   = mdist,
                            WomenDist = wdist,
                            WODSet    = wodSet,
                            MenRx     = mweight,
                            WomenRx   = wweight,
                            Reps      = exJson.Reps,
                            Order     = exJson.Order,
                            Notes     = Affine.Utils.Web.WebUtils.MakeWebSafeString(exJson.Notes),
                            Exercise  = entities.Exercises.First(ee => ee.Id == exJson.ExcercisKey)
                        };
                    }
                }
                entities.SaveChanges();
                // Now we add to peoples favs
                // ** This no longer happens here.. it happens when the workout is "scheduled" for the group that it gets added to the list..
                WOD newWOD = entities.WODs.Where(w => w.UserSettingsKey == userKey).OrderByDescending(w => w.Id).First();
                if (GroupSettings == null)
                {   // simply add for this user..
                    User2WODFav fav = new User2WODFav()
                    {
                        UserSetting = entities.UserSettings.First(us => us.Id == UserSettings.Id),
                        WOD         = newWOD
                    };
                    entities.AddToUser2WODFav(fav);
                    entities.SaveChanges();
                }
                else
                {
                    User2WODFav fav = new User2WODFav()
                    {
                        UserSetting = entities.UserSettings.First(us => us.Id == GroupSettings.Id),
                        WOD         = newWOD
                    };
                    entities.AddToUser2WODFav(fav);
                    entities.SaveChanges();
                }
                if (GroupSettings != null)
                {   // if this was a group wod.. we want to bounce back to the scheduling page now...
                    Response.Redirect(ResolveUrl("~") + "group/" + GroupSettings.UserName + "?w=" + newWOD.Id, true);
                    return;
                }
                string baseUrl = ResolveUrl("~");
                RadAjaxManager1.ResponseScripts.Add("Aqufit.Windows.WorkoutSavedDialog.open(" + newWOD.Id + ");");
                //status = "Your Workout has been saved.  It will now show up in your Workout List";
            }
            catch (Exception ex)
            {
                status = "There was a problem saving your workout: " + ex.Message + " " + ex.InnerException;
            }
            RadAjaxManager1.ResponseScripts.Add(" UpdateStatus('" + status + "');");
        }
 protected void bAjaxPostback_Click(object sender, EventArgs e)
 {
     try
     {
         aqufitEntities entities = new aqufitEntities();
         switch (hiddenAjaxAction.Value)
         {
             case "addWorkout":
                 // few things to do here...
                 // 1) check to see if this route is already in the fav.
                 long wid = Convert.ToInt64(hiddenAjaxValue.Value);
                 User2WODFav check = entities.User2WODFav.FirstOrDefault(w => w.UserSetting.Id == this.UserSettings.Id && w.WOD.Id == wid);
                 if (check != null)
                 {
                     // TODO: postback a message saying that the route is already there.
                 //    status = "Route already in list.  TODO: dialog with a way to 'view my routes' | 'record workout for route'";
                 }
                 else
                 {   // add the WOD.
                     WOD wod = entities.WODs.First(w => w.Id == wid);
                     UserSettings us = entities.UserSettings.First( u => u.Id == UserSettings.Id );
                     User2WODFav fav = new User2WODFav()
                     {
                         UserSetting = us,
                         WOD = wod
                     };
                     entities.AddToUser2WODFav(fav);
                     entities.SaveChanges();
                     //TODO: dialog with a way to 'view my workouts' | 'record this workout'";
                 }
                 // TODO: should not see this for when the check is null above..
                 RadAjaxManager1.ResponseScripts.Add(" Aqufit.Windows.WorkoutAddedDialog.open(); ");
                 break;
             case "remWorkout":
                 long remid = Convert.ToInt64(hiddenAjaxValue.Value);
                 User2WODFav toRem = entities.User2WODFav.FirstOrDefault(w => w.UserSetting.Id == this.UserSettings.Id && w.WOD.Id == remid);
                 if (toRem != null)
                 {
                     // remove the route from fav... any workouts will still be logged though.
                     entities.DeleteObject(toRem);
                     entities.SaveChanges();
                 }
                 break;
             case "delStream":
                 long sid = Convert.ToInt64(hiddenAjaxValue.Value);
                 Affine.Data.Managers.LINQ.DataManager.Instance.deleteStream(UserSettings, sid);
                 break;
             case "delComment":
                 long cid = Convert.ToInt64(hiddenAjaxValue.Value);
                 Affine.Data.Managers.LINQ.DataManager.Instance.deleteComment(UserSettings, cid);
                 break;
             case "AddSuggestFriend":
                 try
                 {
                     long usid = Convert.ToInt64(hiddenAjaxValue.Value);
                     string status = Affine.Data.Managers.LINQ.DataManager.Instance.sendFriendRequest(UserSettings.Id, usid);
                     RadAjaxManager1.ResponseScripts.Add(" Aqufit.Page.atiProfile.ShowOk('A Friend request has been sent');");
                 }
                 catch (Exception ex)
                 {
                     RadAjaxManager1.ResponseScripts.Add("Aqufit.Page.Actions.ShowFail('Error: " + ex.Message + "');");
                 }
                 break;
         }
     }
     catch (Exception ex)
     {
         // TODO: better error handling
         RadAjaxManager1.ResponseScripts.Add( " alert('" + ex.Message + "');");
     }
 }
        protected void bAjaxPostback_Click(object sender, EventArgs e)
        {
            try
            {
                aqufitEntities entities = new aqufitEntities();
                switch (hiddenAjaxAction.Value)
                {
                case "addWorkout":
                    // few things to do here...
                    // 1) check to see if this route is already in the fav.
                    long        wid   = Convert.ToInt64(hiddenAjaxValue.Value);
                    User2WODFav check = entities.User2WODFav.FirstOrDefault(w => w.UserSetting.Id == this.UserSettings.Id && w.WOD.Id == wid);
                    if (check != null)
                    {
                        // TODO: postback a message saying that the route is already there.
                        //    status = "Route already in list.  TODO: dialog with a way to 'view my routes' | 'record workout for route'";
                    }
                    else
                    {       // add the WOD.
                        WOD          wod = entities.WODs.First(w => w.Id == wid);
                        UserSettings us  = entities.UserSettings.First(u => u.Id == UserSettings.Id);
                        User2WODFav  fav = new User2WODFav()
                        {
                            UserSetting = us,
                            WOD         = wod
                        };
                        entities.AddToUser2WODFav(fav);
                        entities.SaveChanges();
                        //TODO: dialog with a way to 'view my workouts' | 'record this workout'";
                    }
                    // TODO: should not see this for when the check is null above..
                    RadAjaxManager1.ResponseScripts.Add(" Aqufit.Windows.WorkoutAddedDialog.open(); ");
                    break;

                case "remWorkout":
                    long        remid = Convert.ToInt64(hiddenAjaxValue.Value);
                    User2WODFav toRem = entities.User2WODFav.FirstOrDefault(w => w.UserSetting.Id == this.UserSettings.Id && w.WOD.Id == remid);
                    if (toRem != null)
                    {
                        // remove the route from fav... any workouts will still be logged though.
                        entities.DeleteObject(toRem);
                        entities.SaveChanges();
                    }
                    break;

                case "delStream":
                    long sid = Convert.ToInt64(hiddenAjaxValue.Value);
                    Affine.Data.Managers.LINQ.DataManager.Instance.deleteStream(UserSettings, sid);
                    break;

                case "delComment":
                    long cid = Convert.ToInt64(hiddenAjaxValue.Value);
                    Affine.Data.Managers.LINQ.DataManager.Instance.deleteComment(UserSettings, cid);
                    break;

                case "AddSuggestFriend":
                    try
                    {
                        long   usid   = Convert.ToInt64(hiddenAjaxValue.Value);
                        string status = Affine.Data.Managers.LINQ.DataManager.Instance.sendFriendRequest(UserSettings.Id, usid);
                        RadAjaxManager1.ResponseScripts.Add(" Aqufit.Page.atiProfile.ShowOk('A Friend request has been sent');");
                    }
                    catch (Exception ex)
                    {
                        RadAjaxManager1.ResponseScripts.Add("Aqufit.Page.Actions.ShowFail('Error: " + ex.Message + "');");
                    }
                    break;
                }
            }
            catch (Exception ex)
            {
                // TODO: better error handling
                RadAjaxManager1.ResponseScripts.Add(" alert('" + ex.Message + "');");
            }
        }
        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Page_Load runs when the control is loaded
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// </history>
        /// -----------------------------------------------------------------------------
        protected void Page_Load(System.Object sender, System.EventArgs e)
        {
            base.Page_Load(sender, e);

            try
            {
                ServiceReference service = new ServiceReference("~/DesktopModules/ATI_Base/resources/services/StreamService.asmx");
                service.InlineScript = true;
                ScriptManager.GetCurrent(Page).Services.Add(service);
                imgAd.Src         = ResolveUrl("/portals/0/images/adTastyPaleo.jpg");
                imgCheck.ImageUrl = ResolveUrl("~/DesktopModules/ATI_Base/resources/images/iCheck.png");

                if (!Page.IsPostBack && !Page.IsCallback)
                {
                    baseUrl = ResolveUrl("~/");
                    long wId = 0;
                    if (HttpContext.Current.Items["w"] != null)
                    {
                        wId = Convert.ToInt64(HttpContext.Current.Items["w"]);
                    }
                    else if (Request["w"] != null)
                    {
                        wId = Convert.ToInt64(Request["w"]);
                    }

                    // Are we viewing a specific workout ?
                    if (wId > 0)
                    {
                        divMainLinks.Visible = true;

                        atiProfile.Visible     = false;
                        hiddenWorkoutKey.Value = "" + wId;
                        aqufitEntities entities = new aqufitEntities();
                        WOD            wod      = entities.WODs.Include("WODType").Include("WODSets").Include("WODSets.WODExercises").Include("WODSets.WODExercises.Exercise").FirstOrDefault(w => w.Id == wId);
                        if (base.UserSettings != null && wod.Standard == 0)
                        {
                            User2WODFav fav = entities.User2WODFav.FirstOrDefault(mr => mr.WOD.Id == wod.Id && mr.UserSetting.Id == UserSettings.Id);
                            bAddWorkout.Visible    = fav == null;
                            bRemoveWorkout.Visible = fav != null;
                        }
                        lWorkoutTitle.Text = wod.Name;

                        //  lWorkoutDescription.Text = wod.Description;

                        // constructWorkoutInfo(wod);
                        lWorkoutInfo.Text = wod.Description;
                        if (wod.Standard > 0)
                        {
                            imgCrossFit.Visible   = true;
                            imgCrossFit.Src       = ResolveUrl("~/DesktopModules/ATI_Base/resources/images/xfit.png");
                            atiProfileImg.Visible = false;
                        }
                        else
                        {
                            atiProfileImg.Settings = entities.UserSettings.FirstOrDefault(us => us.Id == wod.UserSettingsKey);
                        }
                        atiWorkoutPanel.Visible  = false;
                        atiWorkoutViewer.Visible = true;

                        // Get the leader board
                        IQueryable <Workout> stream = entities.UserStreamSet.OfType <Workout>().Where(w => w.WOD.Id == wId);
                        long typeId = entities.WODs.Where(w => w.Id == wId).Select(w => w.WODType.Id).FirstOrDefault();
                        switch (typeId)
                        {
                        case (long)Affine.Utils.WorkoutUtil.WodType.AMRAP:
                        case (long)Affine.Utils.WorkoutUtil.WodType.SCORE:
                            atiScoreRangePanel.Visible = true;
                            stream = stream.OrderByDescending(w => w.Score);
                            break;

                        case (long)Affine.Utils.WorkoutUtil.WodType.MAX_WEIGHT:
                            atiMaxRangePanel.Visible = true;
                            atiMaxWeightUnitsFirst.UnitList.Add(Affine.Utils.UnitsUtil.MeasureUnit.UNIT_LBS);
                            atiMaxWeightUnitsFirst.UnitList.Add(Affine.Utils.UnitsUtil.MeasureUnit.UNIT_KG);
                            atiMaxWeightUnitsFirst.Selected = WeightUnits;
                            atiMaxWeightUnitsLast.UnitList.Add(Affine.Utils.UnitsUtil.MeasureUnit.UNIT_LBS);
                            atiMaxWeightUnitsLast.UnitList.Add(Affine.Utils.UnitsUtil.MeasureUnit.UNIT_KG);
                            atiMaxWeightUnitsLast.Selected = WeightUnits;
                            stream = stream.OrderByDescending(w => w.Max);
                            break;

                        case (long)Affine.Utils.WorkoutUtil.WodType.TIMED:
                            atiTimeSpanePanel.Visible = true;
                            stream = stream.OrderBy(w => w.Duration);
                            break;

                        default:
                            stream = stream.OrderByDescending(w => w.TimeStamp);
                            break;
                        }
                        string js = string.Empty;
                        atiShareLink.ShareLink  = "http://" + Request.Url.Host + "/workout/" + wod.Id;
                        atiShareLink.ShareTitle = "FlexFWD.com crossfit WOD " + wod.Name;

                        workoutTabTitle.Text = "&nbsp;" + (string.IsNullOrWhiteSpace(wod.Name) ? "Untitled" : wod.Name) + "&nbsp;";
                        Affine.WebService.StreamService ss = new WebService.StreamService();
                        string jsonEveryone = ss.getStreamDataForWOD(wod.Id, -1, 0, 15, true, true, -1, -1, -1);

                        string jsonYou = string.Empty;

                        js += " Aqufit.Page." + atiWorkoutHighChart.ID + ".fromStreamData('" + jsonEveryone + "'); ";
                        if (base.UserSettings != null)
                        {
                            hlLogWorkout.HRef = baseUrl + UserSettings.UserName + "?w=" + wId;
                            hlWorkouts.HRef   = baseUrl + UserSettings.UserName + "/workout-history";
                            jsonYou           = ss.getStreamDataForWOD(wod.Id, base.UserSettings.Id, 0, 10, true, true, -1, -1, -1);
                            js += "Aqufit.Page.atiYouStreamScript.generateStreamDom('" + jsonYou + "');";
                            js += " Aqufit.Page." + atiWorkoutHighChart.ID + ".fromYourStreamData('" + jsonYou + "'); ";
                            // TODO: this could be improved on...
                            Workout thisWod = entities.UserStreamSet.OfType <Workout>().FirstOrDefault(w => w.UserSetting.Id == UserSettings.Id && w.WOD.Id == wId);
                            if (thisWod != null)
                            {   // graphs of this wod
                                hlGraph.HRef = ResolveUrl("~/") + UserSettings.UserName + "/workout/" + thisWod.Id;
                            }
                            else
                            {   // just grab any workout then..
                                Workout any = entities.UserStreamSet.OfType <Workout>().OrderByDescending(w => w.Id).FirstOrDefault(w => w.UserSetting.Id == UserSettings.Id);
                                if (any != null)
                                {
                                    hlGraph.HRef = ResolveUrl("~/") + UserSettings.UserName + "/workout/" + any.Id;
                                }
                                else
                                {   // no workouts ??? say what.. slack man :) hide graph
                                    hlGraph.Visible = false;
                                }
                            }
                        }
                        else
                        {
                            hlGraph.HRef                 = ResolveUrl("~/Login");
                            hlLogWorkout.HRef            = hlGraph.HRef;
                            hlWorkouts.HRef              = hlGraph.HRef;
                            atiPanelYourProgress.Visible = false;
                        }
                        hlCreateWOD.HRef  = baseUrl + "Profile/WorkoutBuilder";
                        hlMyWorkouts.HRef = baseUrl + "Profile/MyWorkouts";
                        js += " Aqufit.Page." + atiWorkoutHighChart.ID + ".drawChart(); ";



                        ScriptManager.RegisterStartupScript(this, Page.GetType(), "SimilarRouteList", "$(function(){ " + js + " Aqufit.Page.atiEveryoneStreamScript.generateStreamDom('" + jsonEveryone + "'); });", true);



                        YouTubeQuery query = new YouTubeQuery(YouTubeQuery.DefaultVideoUri);
                        //order results by the number of views (most viewed first)
                        query.OrderBy          = "viewCount";
                        query.NumberToRetrieve = 3;
                        query.SafeSearch       = YouTubeQuery.SafeSearchValues.Moderate;

                        YouTubeRequestSettings settings = new YouTubeRequestSettings(ConfigurationManager.AppSettings["youtubeApp"], ConfigurationManager.AppSettings["youtubeKey"]);
                        YouTubeRequest         request  = new YouTubeRequest(settings);
                        const int     NUM_ENTRIES       = 50;
                        IList <Video> videos            = new List <Video>();
                        IList <Video> groupVideo        = new List <Video>();
                        // first try to find videos with regard to users group
                        Feed <Video> videoFeed = null;

                        if (this.UserSettings != null)
                        {
                            long[] groupIds = entities.UserFriends.Where(f => (f.SrcUserSettingKey == UserSettings.Id || f.DestUserSettingKey == UserSettings.Id) && f.Relationship >= (int)Affine.Utils.ConstsUtil.Relationships.GROUP_OWNER).Select(f => f.SrcUserSettingKey == UserSettings.Id ? f.DestUserSettingKey : f.SrcUserSettingKey).ToArray();
                            Group  business = entities.UserSettings.OfType <Group>().Where(Utils.Linq.LinqUtils.BuildContainsExpression <Group, long>(us => us.Id, groupIds)).FirstOrDefault(g => g.GroupType.Id == 1);
                            if (business != null)
                            {   // TODO: need the business name...
                                query.Query = business.UserName;
                            }
                            else
                            {
                                UserSettings creator = entities.UserSettings.FirstOrDefault(u => u.Id == wod.UserSettingsKey);
                                if (creator is Group)
                                {   // TODO: need the business name...
                                    query.Query = creator.UserFirstName + " " + creator.UserLastName;
                                }
                                else
                                {
                                    query.Query = UserSettings.UserFirstName + " " + UserSettings.UserLastName;
                                }
                            }
                            videoFeed = request.Get <Video>(query);
                            foreach (Video v in videoFeed.Entries)
                            {
                                groupVideo.Add(v);
                            }
                        }
                        if (videos.Count < NUM_ENTRIES)
                        {   // now try the crossfit WOD name
                            query.NumberToRetrieve = NUM_ENTRIES - videos.Count;
                            query.Query            = "crossfit wod " + wod.Name;
                            videoFeed = request.Get <Video>(query);
                            foreach (Video v in videoFeed.Entries)
                            {
                                videos.Add(v);
                            }
                            if (videos.Count < NUM_ENTRIES)
                            {   // this is last resort .. just get videos about crossfit...
                                query.NumberToRetrieve = NUM_ENTRIES - videos.Count;
                                query.Query            = "crossfit wod";
                                videoFeed = request.Get <Video>(query);
                                foreach (Video v in videoFeed.Entries)
                                {
                                    videos.Add(v);
                                }
                            }
                        }

                        const int TAKE = 3;
                        if (videos.Count > TAKE)
                        {
                            Random random = new Random((int)DateTime.Now.Ticks);
                            int    rand   = random.Next(videos.Count - TAKE);
                            videos = videos.Skip(rand).Take(TAKE).ToList();
                            if (groupVideo.Count > 0)
                            {
                                // always replace one of the main videos with a gorup one.. (if possible)
                                rand      = random.Next(groupVideo.Count - 1);
                                videos[0] = groupVideo[rand];
                            }
                            atiYoutubeThumbList.VideoFeed = videos;
                        }
                        else
                        {
                            atiVideoPanel.Visible = false;
                        }
                    }
                    else
                    {
                        atiVideoPanel.Visible     = false;
                        atiPanelQuickView.Visible = false;
                        hlGraph.Visible           = false;
                        aqufitEntities entities      = new aqufitEntities();
                        var            exerciseArray = entities.Exercises.OrderBy(x => x.Name).Select(x => new{ Text = x.Name, Value = x.Id }).ToArray();
                        RadListBoxExcerciseSource.DataSource = exerciseArray;
                        RadListBoxExcerciseSource.DataBind();

                        string order = orderDate.Checked ? "date" : "popular";
                        if (Settings["Configure"] != null && Convert.ToString(Settings["Configure"]).Equals("ConfigureMyWorkouts"))
                        {
                            this.IsMyWorkouts          = true;
                            atiProfile.ProfileSettings = base.UserSettings;
                            atiProfile.IsOwner         = true;
                            atiProfile.IsSmall         = true;
                            atiWorkoutPanel.Visible    = true;
                            workoutTabTitle.Text       = "My Workouts";
                            liMyWorkouts.Visible       = false;
                            liFindWorkout.Visible      = true;
                            WebService.StreamService streamService = new WebService.StreamService();
                            string json = streamService.GetWorkouts(base.UserSettings.Id, 0, 30, order, null);
                            ScriptManager.RegisterStartupScript(this, Page.GetType(), "WorkoutList", "$(function(){ Aqufit.Page.atiWorkoutListScript.isMyRoutes = true; Aqufit.Page.atiWorkoutListScript.generateStreamDom('" + json + "'); });", true);
                        }
                        else
                        {
                            this.IsMyWorkouts        = false;
                            atiProfile.Visible       = false;
                            workoutTabTitle.Text     = "Workouts";
                            atiWorkoutPanel.Visible  = true;
                            atiWorkoutViewer.Visible = false;
                            WebService.StreamService streamService = new WebService.StreamService();
                            string json = streamService.GetWorkouts(-1, 0, 30, order, null);
                            ScriptManager.RegisterStartupScript(this, Page.GetType(), "WorkoutList2", "$(function(){ Aqufit.Page.atiWorkoutListScript.generateStreamDom('" + json + "'); });", true);
                        }
                    }
                }
            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }
        protected void bSaveWOD_Click(object sender, EventArgs e)
        {
            string status = string.Empty;
            try
            {
                JavaScriptSerializer serializer = new JavaScriptSerializer();
                Affine.Data.json.WOD wodJson = serializer.Deserialize<Affine.Data.json.WOD>(atiWodJson.Value);
                aqufitEntities entities = new aqufitEntities();

                // TODO: check if there is already a WOD of the same name
                long wodTypeId = Convert.ToInt64(ddlWODType.SelectedValue);
                int standard = UserInfo.IsSuperUser ? 1 : 0;
                long userKey = GroupSettings != null ? this.GroupSettings.Id : this.UserSettings.Id;
                WOD wod = new WOD()
                {
                    Difficulty = wodJson.Difficulty,
                    Standard = (short)standard,
                    Name = Affine.Utils.Web.WebUtils.MakeWebSafeString( txtWorkoutName.Text ),
                    WODType = entities.WODTypes.First(t => t.Id == wodTypeId),
                    UserSettingsKey = userKey,
                    Description = Affine.Utils.Web.WebUtils.MakeWebSafeString(txtDescription.Text),
                    UserName = GroupSettings != null ? this.GroupSettings.UserName : this.UserSettings.UserName,
                    CreationDate = DateTime.Now.ToUniversalTime()
                };
                if (wodTypeId == (int)Affine.Utils.WorkoutUtil.WodType.AMRAP)
                {
                    wod.TimeSpan = atiTimeSpan.Time;
                }
                entities.AddToWODs(wod);
                foreach (Affine.Data.json.WODSet setJson in wodJson.WODSets)
                {
                    WODSet wodSet = new WODSet()
                    {
                        WOD = wod
                    };
                    //Affine.Data.Exercise[] exerciseArray = entities.Exercises.Where(Affine.Utils.Linq.LinqUtils.BuildContainsExpression<Affine.Data.Exercise, long>(ex => ex.Id, setJson.WODExercises.Select(ee => ee.ExcercisKey ).ToArray() )).ToArray();
                    foreach (Affine.Data.json.WODExercise exJson in setJson.WODExercises)
                    {
                        double mdist = exJson.MenDist > 0 ? Utils.UnitsUtil.unitsToSystemDefualt(exJson.MenDist, Utils.UnitsUtil.ToUnit(exJson.MenDistUnits)) : -1;
                        double wdist = exJson.WomenDist > 0 ? Utils.UnitsUtil.unitsToSystemDefualt(exJson.WomenDist, Utils.UnitsUtil.ToUnit(exJson.WomenDistUnits)) : -1;
                        double mweight = exJson.MenRx > 0 ? Utils.UnitsUtil.unitsToSystemDefualt(exJson.MenRx, Utils.UnitsUtil.ToUnit(exJson.MenRxUnits)) : -1;
                        double wweight = exJson.WomenRx > 0 ? Utils.UnitsUtil.unitsToSystemDefualt(exJson.WomenRx, Utils.UnitsUtil.ToUnit(exJson.WomenRxUnits)) : -1;
                        WODExercise wodEx = new WODExercise()
                        {
                            MenDist = mdist,
                            WomenDist = wdist,
                            WODSet = wodSet,
                            MenRx = mweight,
                            WomenRx = wweight,
                            Reps = exJson.Reps,
                            Order = exJson.Order,
                            Notes = Affine.Utils.Web.WebUtils.MakeWebSafeString(exJson.Notes),
                            Exercise = entities.Exercises.First(ee => ee.Id == exJson.ExcercisKey)
                        };
                    }
                }
                entities.SaveChanges();
                // Now we add to peoples favs
                // ** This no longer happens here.. it happens when the workout is "scheduled" for the group that it gets added to the list..
                WOD newWOD = entities.WODs.Where(w => w.UserSettingsKey == userKey).OrderByDescending(w => w.Id).First();
                if (GroupSettings == null)
                {   // simply add for this user..
                    User2WODFav fav = new User2WODFav()
                    {
                        UserSetting = entities.UserSettings.First(us => us.Id == UserSettings.Id),
                        WOD = newWOD
                    };
                    entities.AddToUser2WODFav(fav);
                    entities.SaveChanges();
                }
                else
                {
                    User2WODFav fav = new User2WODFav()
                    {
                        UserSetting = entities.UserSettings.First(us => us.Id == GroupSettings.Id),
                        WOD = newWOD
                    };
                    entities.AddToUser2WODFav(fav);
                    entities.SaveChanges();
                }
                if (GroupSettings != null)
                {   // if this was a group wod.. we want to bounce back to the scheduling page now...
                    Response.Redirect(ResolveUrl("~") + "group/" + GroupSettings.UserName + "?w=" + newWOD.Id, true);
                    return;
                }
                string baseUrl = ResolveUrl("~");
                RadAjaxManager1.ResponseScripts.Add("Aqufit.Windows.WorkoutSavedDialog.open(" + newWOD.Id + ");");
                //status = "Your Workout has been saved.  It will now show up in your Workout List";
            }
            catch (Exception ex)
            {
                status = "There was a problem saving your workout: " + ex.Message + " " + ex.InnerException;
            }
            RadAjaxManager1.ResponseScripts.Add(" UpdateStatus('"+status+"');");
        }