protected void submitButton_Click(object sender, EventArgs e) { if (validate()) { DateTime selectedDateTime = Convert.ToDateTime(RadDatePicker1.SelectedDate.Value.ToShortDateString() + " " + RadTimePicker1.SelectedDate.Value.ToShortTimeString()); int opponentId = Convert.ToInt32(Request.QueryString["opponentId"]); bool timeValid = true; using (FlexibleTennisLeagueDataContext dataContext = new FlexibleTennisLeagueDataContext()) { //Get opponents availability List <SelectAvailabilitiesByUserIdResult> availability = dataContext.SelectAvailabilitiesByUserId(opponentId).ToList(); if (availability.Count > 0) { //Find day of week for currently selected date and get opponents availability for that date. DateTime availabilityStart = selectedDateTime; DateTime availabilityEnd = selectedDateTime; bool unavailable = false; switch (selectedDateTime.DayOfWeek) { case DayOfWeek.Sunday: if (availability[0].SundayStart.HasValue) { availabilityStart = availability[0].SundayStart.Value; availabilityEnd = availability[0].SundayEnd.Value; } else if (availability[0].SundayUnavailable.HasValue) { unavailable = availability[0].SundayUnavailable.Value; } break; case DayOfWeek.Monday: if (availability[0].MondayStart.HasValue) { availabilityStart = availability[0].MondayStart.Value; availabilityEnd = availability[0].MondayEnd.Value; } else if (availability[0].MondayUnavailable.HasValue) { unavailable = availability[0].MondayUnavailable.Value; } break; case DayOfWeek.Tuesday: if (availability[0].TuesdayStart.HasValue) { availabilityStart = availability[0].TuesdayStart.Value; availabilityEnd = availability[0].TuesdayEnd.Value; } else if (availability[0].TuesdayUnavailable.HasValue) { unavailable = availability[0].TuesdayUnavailable.Value; } break; case DayOfWeek.Wednesday: if (availability[0].WednesdayStart.HasValue) { availabilityStart = availability[0].WednesdayStart.Value; availabilityEnd = availability[0].WednesdayEnd.Value; } else if (availability[0].WednesdayUnavailable.HasValue) { unavailable = availability[0].WednesdayUnavailable.Value; } break; case DayOfWeek.Thursday: if (availability[0].ThursdayStart.HasValue) { availabilityStart = availability[0].ThursdayStart.Value; availabilityEnd = availability[0].ThursdayEnd.Value; } else if (availability[0].ThursdayUnavailable.HasValue) { unavailable = availability[0].ThursdayUnavailable.Value; } break; case DayOfWeek.Friday: if (availability[0].FridayStart.HasValue) { availabilityStart = availability[0].FridayStart.Value; availabilityEnd = availability[0].FridayEnd.Value; } else if (availability[0].FridayUnavailable.HasValue) { unavailable = availability[0].FridayUnavailable.Value; } break; case DayOfWeek.Saturday: if (availability[0].SaturdayStart.HasValue) { availabilityStart = availability[0].SaturdayStart.Value; availabilityEnd = availability[0].SaturdayEnd.Value; } else if (availability[0].SaturdayUnavailable.HasValue) { unavailable = availability[0].SaturdayUnavailable.Value; } break; } //If availability data exists if (availabilityStart != selectedDateTime) { availabilityStart = Convert.ToDateTime(selectedDateTime.ToShortDateString() + " " + availabilityStart.ToShortTimeString()); availabilityEnd = Convert.ToDateTime(selectedDateTime.ToShortDateString() + " " + availabilityEnd.ToShortTimeString()); //If opponents availability conflicts currently selected date and time then show error if (selectedDateTime < availabilityStart || selectedDateTime >= availabilityEnd) { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.Append("radalert('The start time for the match is in conflict with your opponents availability. Please pick another time.')"); ScriptManager.RegisterStartupScript(this, this.GetType(), "radalert", "(function(){var f = function(){" + stringBuilder.ToString() + ";Sys.Application.remove_load(f);};Sys.Application.add_load(f);})()", true); timeValid = false; } } //If your opponent is unavailable for the whole day. else if (unavailable) { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.Append("radalert('The start time for the match is in conflict with your opponents availability. Please pick another time.')"); ScriptManager.RegisterStartupScript(this, this.GetType(), "radalert", "(function(){var f = function(){" + stringBuilder.ToString() + ";Sys.Application.remove_load(f);};Sys.Application.add_load(f);})()", true); timeValid = false; } } if (timeValid) { //Find appointments for both user and opponents that conflict with the time selected List <SelectAppointmentsByUserIdDateResult> appointments = dataContext.SelectAppointmentsByUserIdDate(StoredData.User.UserId, opponentId, selectedDateTime).ToList(); if (appointments.Count > 0) { StringBuilder stringBuilder = new StringBuilder(); if (appointments[0].UserId == StoredData.User.UserId) { stringBuilder.Append("radconfirm('You may have a conflict with one or more events in your schedule.<br>Do you want to continue?',confirmCallBackFn)"); } else { stringBuilder.Append("radconfirm('You may have a conflict with one or more events in your opponents schedule.<br>Do you want to continue?',confirmCallBackFn)"); } ScriptManager.RegisterStartupScript(this, this.GetType(), "radalert", "(function(){var f = function(){" + stringBuilder.ToString() + ";Sys.Application.remove_load(f);};Sys.Application.add_load(f);})()", true); timeValid = false; } } if (timeValid) { UpdateMatch(); } else { ScriptManager.RegisterStartupScript(this, this.GetType(), "EnableButton", "changeButtonState('false');", true); } } } }