public static bool StoreData_TrunkStaticEcce_RemoteDB(DataClasses.GrucoxTrunkStaticEcce data, int UserID)
        {
            DataClasses.GrucoxDataClassesRemoteDBDataContext dc = new DataClasses.GrucoxDataClassesRemoteDBDataContext();

            var matchedItem = (from sessions in dc.GetTable<DataClasses.GrucoxTrunkStaticEcce>()          //**************************************
                               where sessions.Id == data.Id                              //******Check if customer already exists
                               select sessions).SingleOrDefault();                                     //**************************************

            if (matchedItem == null)                                                //If customer does not exist
            {
                try
                {
                    dc.GrucoxTrunkStaticEcces.InsertOnSubmit(data);
                    dc.GrucoxTrunkStaticEcces.Context.SubmitChanges();                      //Add the new customer to the Customers Table
                    return true;
                }
                catch (Exception ex)
                {
                    return false;
                }
            }
            else                                                                    //Customer already exists
            {
                try
                {
                    dc.SubmitChanges();
                    return true;
                }
                catch (Exception ex)
                {
                    return false;
                }
            }
        }
        public static DataClasses.CustomProgram GetCustomProgramRemotePB(int UserID)
        {
            DataClasses.GrucoxDataClassesRemoteDBDataContext dc = new DataClasses.GrucoxDataClassesRemoteDBDataContext();
            DataClasses.CustomProgram customProgramPB = new DataClasses.CustomProgram();
            List<DataClasses.GrucoxCustomProgramSession> ListcustomProgramPB = new List<DataClasses.GrucoxCustomProgramSession>();

            try
            {
                ListcustomProgramPB = (from sessions in dc.GetTable<DataClasses.GrucoxCustomProgramSession>()
                                       where sessions.LiveUserID == UserID
                                       select sessions).ToList<DataClasses.GrucoxCustomProgramSession>();
            }
            catch (Exception)
            {
                //For Diagnostics - Should be removed
                DataComms.CreateThreadInstance("DB Access during GetCustomProgramRemotePB");
            }
            finally
            {
                customProgramPB.LeftAvgConcP = ListcustomProgramPB.Max(z => z.LeftAvgConcP).HasValue ? (double)ListcustomProgramPB.Max(z => z.LeftAvgConcP) : 0.0;
                customProgramPB.LeftAvgEcceP = ListcustomProgramPB.Max(z => z.LeftAvgEcceP).HasValue ? (double)ListcustomProgramPB.Max(z => z.LeftAvgEcceP) : 0.0;
                customProgramPB.RightAvgConcP = ListcustomProgramPB.Max(z => z.RightAvgConcP).HasValue ? (double)ListcustomProgramPB.Max(z => z.RightAvgConcP) : 0.0;
                customProgramPB.RightAvgEcceP = ListcustomProgramPB.Max(z => z.RightAvgEcceP).HasValue ? (double)ListcustomProgramPB.Max(z => z.RightAvgEcceP) : 0.0;
            }
            return customProgramPB;
        }
        public static bool TryRegisterNewSystem(ref DataClasses.GrucoxSystemInfo SystemInfo, string HardwareID)
        {
            DataClasses.GrucoxDataClassesRemoteDBDataContext dc = new DataClasses.GrucoxDataClassesRemoteDBDataContext();
            List<DataClasses.GrucoxSystemInfo> ListSystemInfo = new List<DataClasses.GrucoxSystemInfo>();

            try
            {
                var matchedItem = (from systems in dc.GetTable<DataClasses.GrucoxSystemInfo>()
                                       where systems.HardwareID == HardwareID
                                       select systems).SingleOrDefault();

                if (matchedItem == null)                                                //If customer does not exist
                {
                    try
                    {
                        dc.GrucoxSystemInfos.InsertOnSubmit(SystemInfo);
                        dc.GrucoxSystemInfos.Context.SubmitChanges();                      //Add the new customer to the Customers Table
                        return true;
                    }
                    catch (Exception ex)
                    {
                        return false;
                    }
                }
                else                                                                    //Customer already exists
                {
                    try
                    {
                        matchedItem.IPAddress = SystemInfo.IPAddress;
                        dc.SubmitChanges();
                        SystemInfo = matchedItem;
                        return true;
                    }
                    catch (Exception ex)
                    {
                        return false;
                    }

                }
            }
            catch (Exception)
            {
                //For Diagnostics - Should be removed
                DataComms.CreateThreadInstance("DB Access during RegisterNewSystem");
                return false;
            }
        }
        public static bool StoreDataVO2Max_RemoteDB(DataClasses.VO2MaxProgram1 vO2MaxProgram1, int UserID)
        {
            GrucoxVO2MaxProgramSession VO2MaxProgramSession = new GrucoxVO2MaxProgramSession();
            VO2MaxProgramSession.DeviceUID = (new Grucox.DataManager(CalibrationSettings.Default.DataManagerConnectionString)).GetDeviceMacAddress();
            VO2MaxProgramSession.LiveUserID = UserID;
            VO2MaxProgramSession.SessionDate = DateTime.Now.Subtract(TimeSpan.FromMinutes(10));
            VO2MaxProgramSession.SessionDuration = TimeSpan.FromSeconds(vO2MaxProgram1.SessionDuration.Value);
            VO2MaxProgramSession.LeftMaxConcP = Convert.ToDecimal(vO2MaxProgram1.LeftMaxConcP);
            VO2MaxProgramSession.LeftMaxEcceP = Convert.ToDecimal(vO2MaxProgram1.LeftMaxEcceP);
            VO2MaxProgramSession.RightMaxConcP = Convert.ToDecimal(vO2MaxProgram1.RightMaxConcP);
            VO2MaxProgramSession.RightMaxEcceP = Convert.ToDecimal(vO2MaxProgram1.RightMaxEcceP);
            VO2MaxProgramSession.LeftAvgConcP = Convert.ToDecimal(vO2MaxProgram1.LeftAvgConcP);
            VO2MaxProgramSession.LeftAvgEcceP = Convert.ToDecimal(vO2MaxProgram1.LeftAvgEcceP);
            VO2MaxProgramSession.RightAvgConcP = Convert.ToDecimal(vO2MaxProgram1.RightAvgConcP);
            VO2MaxProgramSession.RightAvgEcceP = Convert.ToDecimal(vO2MaxProgram1.RightAvgEcceP);
            VO2MaxProgramSession.LeftStrengthIndex = Convert.ToDecimal(vO2MaxProgram1.LeftStrengthIndex);
            VO2MaxProgramSession.LeftEnduranceIndex = Convert.ToDecimal(vO2MaxProgram1.LeftEnduranceIndex);
            VO2MaxProgramSession.RightStrengthIndex = Convert.ToDecimal(vO2MaxProgram1.RightStrengthIndex);
            VO2MaxProgramSession.RightEnduranceIndex = Convert.ToDecimal(vO2MaxProgram1.RightEnduranceIndex);
            VO2MaxProgramSession.DistConc = Convert.ToDecimal(vO2MaxProgram1.DistConc);
            VO2MaxProgramSession.DistEcce = Convert.ToDecimal(vO2MaxProgram1.DistEcce);
            VO2MaxProgramSession.EnergyBurned = Convert.ToDecimal(vO2MaxProgram1.EnergyBurned);
            VO2MaxProgramSession.VO2MaxProgramEnum = vO2MaxProgram1.VO2MaxProgramEnum.Value;
            VO2MaxProgramSession.SessionDescription = "VO2 Max Program " + vO2MaxProgram1.VO2MaxProgramEnum.Value;

            DataClasses.GrucoxDataClassesRemoteDBDataContext dc = new DataClasses.GrucoxDataClassesRemoteDBDataContext();

            var matchedItem = (from sessions in dc.GetTable<DataClasses.GrucoxVO2MaxProgramSession>()           //**************************************
                               where sessions.ItemID == VO2MaxProgramSession.ItemID                             //******Check if customer already exists
                               select sessions).SingleOrDefault();                                              //**************************************

            if (matchedItem == null)                                                //If customer does not exist
            {
                try
                {
                    dc.GrucoxVO2MaxProgramSessions.InsertOnSubmit(VO2MaxProgramSession);
                    dc.GrucoxVO2MaxProgramSessions.Context.SubmitChanges();                      //Add the new customer to the Customers Table
                    return true;
                }
                catch (Exception ex)
                {
                    return false;
                }
            }
            else                                                                    //Customer already exists
            {
                try
                {
                    dc.SubmitChanges();
                    return true;
                }
                catch (Exception ex)
                {
                    return false;
                }

            }
        }
        private static bool StoreData_RemoteDB1(DataClasses.GrucoxTrunkCustomSession customProgram, int UserID)
        {
            DataClasses.GrucoxDataClassesRemoteDBDataContext dc = new DataClasses.GrucoxDataClassesRemoteDBDataContext();

            try
            {
                var matchedItem = (from sessions in dc.GetTable<DataClasses.GrucoxTrunkCustomSession>()           //**************************************
                                   where sessions.Id == customProgram.Id                                          //******Check if customer already exists
                                   select sessions).SingleOrDefault();                                            //**************************************

                if (matchedItem == null)                                                       //If customer does not exist
                {
                    dc.GrucoxTrunkCustomSessions.InsertOnSubmit(customProgram);
                    dc.GrucoxTrunkCustomSessions.Context.SubmitChanges();                      //Add the new customer to the Customers Table
                    return true;
                }
                else                                                                           //Customer already exists
                {
                    dc.SubmitChanges();
                    return true;
                }
            }
            catch (Exception)
            {
                //For Diagnostics - Should be removed
                DataComms.CreateThreadInstance("DB Access / Upload Error for Custom Trunk Program");
                return false;
            }
        }
        public static List<DataClasses.GrucoxTrunkStaticEcce> GetTruncStaticEcce(int UserID, string TestMode)
        {
            DataClasses.GrucoxDataClassesRemoteDBDataContext dc = new DataClasses.GrucoxDataClassesRemoteDBDataContext();

            try
            {
                return (from sessions in dc.GetTable<DataClasses.GrucoxTrunkStaticEcce>()
                        where sessions.UserId == UserID && sessions.TestMode.Contains(TestMode) 
                        select sessions).ToList<DataClasses.GrucoxTrunkStaticEcce>();
            }
            catch (Exception)
            {
                return new List<DataClasses.GrucoxTrunkStaticEcce>();
            }
        }
        private static bool StoreDataDynamicStrength_RemoteDB(DataClasses.DynamicStrengthTest dynamicStrengthTest, int UserID)
        {
            GrucoxDynamicStrengthTestSession DynamicStrengthTestSession = new GrucoxDynamicStrengthTestSession();
            DynamicStrengthTestSession.DeviceUID = (new Grucox.DataManager(CalibrationSettings.Default.DataManagerConnectionString)).GetDeviceMacAddress();
            DynamicStrengthTestSession.LiveUserID = UserID;
            DynamicStrengthTestSession.SessionDate = DateTime.Now.Subtract(TimeSpan.FromMinutes(10));
            DynamicStrengthTestSession.LeftMaxTorqueConc = Convert.ToDecimal(dynamicStrengthTest.LeftMaxTorqueConc);
            DynamicStrengthTestSession.LeftMaxTorqueEcce = Convert.ToDecimal(dynamicStrengthTest.LeftMaxTorqueEcce);
            DynamicStrengthTestSession.RightMaxTorqueConc = Convert.ToDecimal(dynamicStrengthTest.RightMaxTorqueConc);
            DynamicStrengthTestSession.RightMaxTorqueEcce = Convert.ToDecimal(dynamicStrengthTest.RightMaxTorqueEcce);
            DynamicStrengthTestSession.SpeedLeftMaxConc = Convert.ToDecimal(dynamicStrengthTest.SpeedLeftMaxConc);
            DynamicStrengthTestSession.SpeedLeftMaxEcce = Convert.ToDecimal(dynamicStrengthTest.SpeedLeftMaxEcce);
            DynamicStrengthTestSession.SpeedRightMaxConc = Convert.ToDecimal(dynamicStrengthTest.SpeedRightMaxConc);
            DynamicStrengthTestSession.SpeedRightMaxEcce = Convert.ToDecimal(dynamicStrengthTest.SpeedRightMaxEcce);

            DataClasses.GrucoxDataClassesRemoteDBDataContext dc = new DataClasses.GrucoxDataClassesRemoteDBDataContext();

            var matchedItem = (from sessions in dc.GetTable<DataClasses.GrucoxDynamicStrengthTestSession>()          //**************************************
                               where sessions.ItemID == dynamicStrengthTest.id                                       //******Check if customer already exists
                               select sessions).SingleOrDefault();                                                   //**************************************

            if (matchedItem == null)                                                //If customer does not exist
            {
                try
                {
                    dc.GrucoxDynamicStrengthTestSessions.InsertOnSubmit(DynamicStrengthTestSession);
                    dc.GrucoxDynamicStrengthTestSessions.Context.SubmitChanges();                      //Add the new customer to the Customers Table
                    return true;
                }
                catch (Exception ex)
                {
                    return false;
                }
            }
            else                                                                    //Customer already exists
            {
                try
                {
                    dc.SubmitChanges();
                    return true;
                }
                catch (Exception ex)
                {
                    return false;
                }
            }
        }
        public static DataClasses.DynamicStrengthTest GetDynamicStrengthRemotePB(int UserID)
        {
            DataClasses.GrucoxDataClassesRemoteDBDataContext dc = new DataClasses.GrucoxDataClassesRemoteDBDataContext();
            List<DataClasses.GrucoxDynamicStrengthTestSession> ListDynamicStrengthPB = new List<DataClasses.GrucoxDynamicStrengthTestSession>();
            DataClasses.DynamicStrengthTest DynamicStrengthPB = new DynamicStrengthTest();

            try
            {
                ListDynamicStrengthPB = (from sessions in dc.GetTable<DataClasses.GrucoxDynamicStrengthTestSession>()
                                         where sessions.LiveUserID == UserID
                                         select sessions).ToList<DataClasses.GrucoxDynamicStrengthTestSession>();
            }
            catch (Exception)
            {
                //For Diagnostics - Should be removed
                DataComms.CreateThreadInstance("DB Access during GetDynamicStrengthRemotePB");
                DynamicStrengthPB.LeftMaxTorqueConc = 0;
                DynamicStrengthPB.LeftMaxTorqueEcce = 0;
                DynamicStrengthPB.RightMaxTorqueConc = 0;
                DynamicStrengthPB.RightMaxTorqueEcce = 0;
                DynamicStrengthPB.SpeedLeftMaxConc = 0;
                DynamicStrengthPB.SpeedLeftMaxEcce = 0;
                DynamicStrengthPB.SpeedRightMaxConc = 0;
                DynamicStrengthPB.SpeedRightMaxEcce = 0;
            }
            finally
            {
                if (ListDynamicStrengthPB.Count > 0)
                {
                    DynamicStrengthPB.LeftMaxTorqueConc = (double)(ListDynamicStrengthPB.First(x => x.LeftMaxTorqueConc == ListDynamicStrengthPB.Max(i => i.LeftMaxTorqueConc))).LeftMaxTorqueConc;
                    DynamicStrengthPB.LeftMaxTorqueEcce = (double)(ListDynamicStrengthPB.First(x => x.LeftMaxTorqueEcce == ListDynamicStrengthPB.Max(i => i.LeftMaxTorqueEcce))).LeftMaxTorqueEcce;
                    DynamicStrengthPB.RightMaxTorqueConc = (double)(ListDynamicStrengthPB.First(x => x.RightMaxTorqueConc == ListDynamicStrengthPB.Max(i => i.RightMaxTorqueConc))).RightMaxTorqueConc;
                    DynamicStrengthPB.RightMaxTorqueEcce = (double)(ListDynamicStrengthPB.First(x => x.RightMaxTorqueEcce == ListDynamicStrengthPB.Max(i => i.RightMaxTorqueEcce))).RightMaxTorqueEcce;
                    DynamicStrengthPB.SpeedLeftMaxConc = (int)(ListDynamicStrengthPB.First(x => x.LeftMaxTorqueConc == ListDynamicStrengthPB.Max(i => i.LeftMaxTorqueConc))).SpeedLeftMaxConc;
                    DynamicStrengthPB.SpeedLeftMaxEcce = (int)(ListDynamicStrengthPB.First(x => x.LeftMaxTorqueEcce == ListDynamicStrengthPB.Max(i => i.LeftMaxTorqueEcce))).SpeedLeftMaxEcce;
                    DynamicStrengthPB.SpeedRightMaxConc = (int)(ListDynamicStrengthPB.First(x => x.RightMaxTorqueConc == ListDynamicStrengthPB.Max(i => i.RightMaxTorqueConc))).SpeedRightMaxConc;
                    DynamicStrengthPB.SpeedRightMaxEcce = (int)(ListDynamicStrengthPB.First(x => x.RightMaxTorqueEcce == ListDynamicStrengthPB.Max(i => i.RightMaxTorqueEcce))).SpeedRightMaxEcce;
                }
                else
                {
                    DynamicStrengthPB.LeftMaxTorqueConc = 0;
                    DynamicStrengthPB.LeftMaxTorqueEcce = 0;
                    DynamicStrengthPB.RightMaxTorqueConc = 0;
                    DynamicStrengthPB.RightMaxTorqueEcce = 0;
                    DynamicStrengthPB.SpeedLeftMaxConc = 0;
                    DynamicStrengthPB.SpeedLeftMaxEcce = 0;
                    DynamicStrengthPB.SpeedRightMaxConc = 0;
                    DynamicStrengthPB.SpeedRightMaxEcce = 0;
                }
            }
            return DynamicStrengthPB;
        }
        public static float[] GetStaticStrengthRemotePB(int UserID)
        {
            DataClasses.GrucoxDataClassesRemoteDBDataContext dc = new DataClasses.GrucoxDataClassesRemoteDBDataContext();
            List<DataClasses.GrucoxStaticStrengthTestSession> ListStaticStrength_PB = new List<DataClasses.GrucoxStaticStrengthTestSession>();
            float[] RetVal = new float[2];

            try
            {
                ListStaticStrength_PB = (from sessions in dc.GetTable<DataClasses.GrucoxStaticStrengthTestSession>()
                                         where sessions.LiveUserID == UserID
                                         select sessions).ToList<DataClasses.GrucoxStaticStrengthTestSession>();
            }
            catch (Exception)
            {
                //For Diagnostics - Should be removed
                DataComms.CreateThreadInstance("DB Access during GetStaticStrengthRemotePB");
                RetVal[0] = 0;
                RetVal[1] = 0;
            }
            finally
            {
                if (ListStaticStrength_PB.Count > 0)
                {
                    RetVal[0] = (float)ListStaticStrength_PB.Max(z => z.LeftMaxTorque);
                    RetVal[1] = (float)ListStaticStrength_PB.Max(z => z.RightMaxTorque);
                }
                else
                {
                    RetVal[0] = 0;
                    RetVal[1] = 0;
                }
            }
            return RetVal;
        }
        public static bool StoreData_FTPTest_RemoteDB(DataClasses.FTPTestConcForward FTPTestConcForwards, int UserID)
        {
            DataClasses.GrucoxFTPTestSession FTPtest = new GrucoxFTPTestSession();
            FTPtest.DeviceUID = (new Grucox.DataManager(CalibrationSettings.Default.DataManagerConnectionString)).GetDeviceMacAddress();
            FTPtest.LiveUserID = UserID;
            FTPtest.SessionDate = DateTime.Now.Subtract(TimeSpan.FromMinutes(10));
            FTPtest.FTPConcForwards = Convert.ToDecimal(FTPTestConcForwards.FTPConcForwards.Value);
            DataClasses.GrucoxDataClassesRemoteDBDataContext dc;
            var matchedItem = new object();

            try
            {
                dc = new DataClasses.GrucoxDataClassesRemoteDBDataContext();

                matchedItem = (from sessions in dc.GetTable<DataClasses.GrucoxFTPTestSession>()            //**************************************
                               where sessions.ItemID == FTPtest.ItemID                                                  //******Check if customer already exists
                               select sessions).SingleOrDefault();                                              //**************************************
            }
            catch (Exception)
            {
                return false;
            }

            if (matchedItem == null)                                                //If customer does not exist
            {
                try
                {
                    dc.GrucoxFTPTestSessions.InsertOnSubmit(FTPtest);
                    dc.GrucoxFTPTestSessions.Context.SubmitChanges();                      //Add the new customer to the Customers Table
                    return true;
                }
                catch (Exception ex)
                {
                    return false;
                }
            }
            else                                                                    //Customer already exists
            {
                try
                {
                    dc.SubmitChanges();
                    return true;
                }
                catch (Exception ex)
                {
                    return false;
                }
            }
        }
        public static float GetFTP_RemotePB(int UserID)
        {
            float RetVal = 0;
            DataClasses.GrucoxDataClassesRemoteDBDataContext dc = new DataClasses.GrucoxDataClassesRemoteDBDataContext();

            try
            {
                List<DataClasses.GrucoxFTPTestSession> ListFTP_PB = (from sessions in dc.GetTable<DataClasses.GrucoxFTPTestSession>()
                                                                     where sessions.LiveUserID == UserID
                                                                     select sessions).ToList<DataClasses.GrucoxFTPTestSession>();

                if (ListFTP_PB.Count > 0)
                    RetVal = (float)ListFTP_PB.Max(z => z.FTPConcForwards);
                else
                    RetVal = 0;

                return RetVal;
            }
            catch (Exception)
            {
                //For Diagnostics - Should be removed
                DataComms.CreateThreadInstance("DB Access during GetFTP_RemotePB");
                return 0;
            }
        }
        public static bool StoreData_VO2MaxTest_RemoteDB(DataClasses.VO2MaxTestConcForward vO2MaxTestConcForw, int UserID)
        {
            GrucoxVO2MaxTestSession VO2MaxTestSession = new GrucoxVO2MaxTestSession();
            VO2MaxTestSession.DeviceUID = (new Grucox.DataManager(CalibrationSettings.Default.DataManagerConnectionString)).GetDeviceMacAddress();
            VO2MaxTestSession.LiveUserID = UserID;
            VO2MaxTestSession.SessionDate = DateTime.Now.Subtract(TimeSpan.FromMinutes(10));
            VO2MaxTestSession.VO2Max = Convert.ToDecimal(vO2MaxTestConcForw.VO2Max.Value);
            VO2MaxTestSession.VO2MaxRel = Convert.ToDecimal(vO2MaxTestConcForw.VO2MaxRel.Value);
            VO2MaxTestSession.MaxConcPower = Convert.ToDecimal(vO2MaxTestConcForw.MaxConcPower.Value);

            DataClasses.GrucoxDataClassesRemoteDBDataContext dc = new DataClasses.GrucoxDataClassesRemoteDBDataContext();

            var matchedItem = (from sessions in dc.GetTable<DataClasses.GrucoxVO2MaxTestSession>()          //**************************************
                               where sessions.ItemID == VO2MaxTestSession.ItemID                              //******Check if customer already exists
                               select sessions).SingleOrDefault();                                     //**************************************

            if (matchedItem == null)                                                //If customer does not exist
            {
                try
                {
                    dc.GrucoxVO2MaxTestSessions.InsertOnSubmit(VO2MaxTestSession);
                    dc.GrucoxVO2MaxTestSessions.Context.SubmitChanges();                      //Add the new customer to the Customers Table
                    return true;
                }
                catch (Exception ex)
                {
                    return false;
                }
            }
            else                                                                    //Customer already exists
            {
                try
                {
                    dc.SubmitChanges();
                    return true;
                }
                catch (Exception ex)
                {
                    return false;
                }
            }
        }
 public static List<DataClasses.User> GetUsersRemoteDB()
 {
     DataClasses.GrucoxDataClassesRemoteDBDataContext dc = new DataClasses.GrucoxDataClassesRemoteDBDataContext();
     try
     {
         return (from sessions in dc.GetTable<DataClasses.User>()
                 select sessions).ToList<DataClasses.User>();
     }
     catch (Exception)
     {
         return new List<DataClasses.User>();
     }
 }
        private static bool StoreData_RemoteDB1(DataClasses.CustomProgram customProgram, int UserID)
        {
            GrucoxCustomProgramSession CustomProgramSession = new GrucoxCustomProgramSession();
            CustomProgramSession.DeviceUID = (new Grucox.DataManager(CalibrationSettings.Default.DataManagerConnectionString)).GetDeviceMacAddress();
            CustomProgramSession.LiveUserID = UserID;
            CustomProgramSession.SessionDate = DateTime.Now.Subtract(TimeSpan.FromMinutes(10));
            CustomProgramSession.SessionDuration = TimeSpan.FromSeconds(customProgram.SessionDuration.Value);
            CustomProgramSession.LeftMaxConcP = Convert.ToDecimal(customProgram.LeftMaxConcP);
            CustomProgramSession.LeftMaxEcceP = Convert.ToDecimal(customProgram.LeftMaxEcceP);
            CustomProgramSession.RightMaxConcP = Convert.ToDecimal(customProgram.RightMaxConcP);
            CustomProgramSession.RightMaxEcceP = Convert.ToDecimal(customProgram.RightMaxEcceP);
            CustomProgramSession.LeftAvgConcP = Convert.ToDecimal(customProgram.LeftAvgConcP);
            CustomProgramSession.LeftAvgEcceP = Convert.ToDecimal(customProgram.LeftAvgEcceP);
            CustomProgramSession.RightAvgConcP = Convert.ToDecimal(customProgram.RightAvgConcP);
            CustomProgramSession.RightAvgEcceP = Convert.ToDecimal(customProgram.RightAvgEcceP);
            CustomProgramSession.LeftStrengthIndex = Convert.ToDecimal(customProgram.LeftStrengthIndex);
            CustomProgramSession.LeftEnduranceIndex = Convert.ToDecimal(customProgram.LeftEnduranceIndex);
            CustomProgramSession.RightStrengthIndex = Convert.ToDecimal(customProgram.RightStrengthIndex);
            CustomProgramSession.RightEnduranceIndex = Convert.ToDecimal(customProgram.RightEnduranceIndex);
            CustomProgramSession.DistConc = Convert.ToDecimal(customProgram.DistConc);
            CustomProgramSession.DistEcce = Convert.ToDecimal(customProgram.DistEcce);
            CustomProgramSession.EnergyBurned = Convert.ToDecimal(customProgram.EnergyBurned);
            CustomProgramSession.SessionDescription = "Custom Program";

            DataClasses.GrucoxDataClassesRemoteDBDataContext dc = new DataClasses.GrucoxDataClassesRemoteDBDataContext();

            try
            {
                var matchedItem = (from sessions in dc.GetTable<DataClasses.GrucoxCustomProgramSession>()           //**************************************
                                   where sessions.ItemID == CustomProgramSession.ItemID                               //******Check if customer already exists
                                   select sessions).SingleOrDefault();                                 //**************************************

                if (matchedItem == null)                                                //If customer does not exist
                {
                    dc.GrucoxCustomProgramSessions.InsertOnSubmit(CustomProgramSession);
                    dc.GrucoxCustomProgramSessions.Context.SubmitChanges();                      //Add the new customer to the Customers Table
                    return true;
                }
                else                                                                    //Customer already exists
                {
                    dc.SubmitChanges();
                    return true;
                }
            }
            catch (Exception)
            {
                //For Diagnostics - Should be removed
                DataComms.CreateThreadInstance("DB Access / Upload Error for Custom Program");
                return false;
            }
        }
        public static Classes.PowerIndexPreviousBest GetRehabProgramBilateralRemotePB(int UserID)
        {
            DataClasses.GrucoxDataClassesRemoteDBDataContext dc = new DataClasses.GrucoxDataClassesRemoteDBDataContext();
            Classes.PowerIndexPreviousBest rehabProgramPB = new Classes.PowerIndexPreviousBest();
            List<DataClasses.GrucoxRehabBilateralSession> ListRehabProgramPB = new List<DataClasses.GrucoxRehabBilateralSession>();

            try
            {
                ListRehabProgramPB = (from sessions in dc.GetTable<DataClasses.GrucoxRehabBilateralSession>()
                                      where sessions.LiveUserID == UserID
                                      select sessions).ToList<DataClasses.GrucoxRehabBilateralSession>();
            }
            catch (Exception)
            {
                //For Diagnostics - Should be removed
                DataComms.CreateThreadInstance("DB Access during GetRehabProgramBilateralRemotePB");
            }
            finally
            {
                rehabProgramPB.LeftAvgConcP = ListRehabProgramPB.Max(z => z.LeftAvgConcP).HasValue ? (double)ListRehabProgramPB.Max(z => z.LeftAvgConcP) : 0.0;         //If you cast a value to a double, it may not be null
                rehabProgramPB.LeftAvgEcceP = ListRehabProgramPB.Max(z => z.LeftAvgEcceP).HasValue ? (double)ListRehabProgramPB.Max(z => z.LeftAvgEcceP) : 0.0;
                rehabProgramPB.RightAvgConcP = ListRehabProgramPB.Max(z => z.RightAvgConcP).HasValue ? (double)ListRehabProgramPB.Max(z => z.RightAvgConcP) : 0.0;
                rehabProgramPB.RightAvgEcceP = ListRehabProgramPB.Max(z => z.RightAvgEcceP).HasValue ? (double)ListRehabProgramPB.Max(z => z.RightAvgEcceP) : 0.0;

                if (rehabProgramPB.RightAvgConcP == 0 || rehabProgramPB.LeftAvgConcP == 0)
                    rehabProgramPB.AvgConcPowerIndex = 0;
                else
                    rehabProgramPB.AvgConcPowerIndex = (double)((rehabProgramPB.RightAvgConcP + rehabProgramPB.LeftAvgConcP) / 2);

                if (rehabProgramPB.RightAvgEcceP == 0 || rehabProgramPB.LeftAvgEcceP == 0)
                    rehabProgramPB.AvgEccePowerIndex = 0;
                else
                    rehabProgramPB.AvgEccePowerIndex = (double)((rehabProgramPB.RightAvgEcceP + rehabProgramPB.LeftAvgEcceP) / 2);

                if (rehabProgramPB.LeftAvgConcP == 0 || rehabProgramPB.LeftAvgEcceP == 0)
                    rehabProgramPB.AvgLeftPowerIndex = 0;
                else
                    rehabProgramPB.AvgLeftPowerIndex = (double)((rehabProgramPB.LeftAvgConcP + rehabProgramPB.LeftAvgEcceP) / 2);

                if (rehabProgramPB.RightAvgEcceP == 0 || rehabProgramPB.RightAvgConcP == 0)
                    rehabProgramPB.AvgRightPowerIndex = 0;
                else
                    rehabProgramPB.AvgRightPowerIndex = (double)((rehabProgramPB.RightAvgEcceP + rehabProgramPB.RightAvgConcP) / 2);
            }
            return rehabProgramPB;     
        }