public static void SaveFrequencies(CalendarDataContext ctx, CalendarEvent newItem, CalendarEvent oldItem) {
			SiteData site = SiteData.CurrentSite;
			DateTime todayDate = DateTime.UtcNow.Date;

			DateTime newStartDateTime = GetStartDateTimeFromItem(newItem);
			DateTime newEndDateTime = GetEndDateTimeFromItem(newItem);

			List<DateTime> eventDates = GetSequenceDates(todayDate, false, newItem);

			if (newItem.EventStartDate.Date <= todayDate.AddYears(-1)
				|| newItem.EventStartDate.Date >= todayDate.AddMonths(6)) {
				eventDates = eventDates.Union(GetSequenceDates(todayDate, true, newItem)).Distinct().ToList();
			}

			List<DateTime> eventUTCDates = (from d in eventDates select site.ConvertSiteTimeToUTC(d)).ToList();

			if (oldItem.SiteID == Guid.Empty && oldItem.CalendarFrequencyID == Guid.Empty) {
				InsertEventsFromList(ctx, newItem, eventDates);
			} else {
				DateTime oldStartDateTime = GetStartDateTimeFromItem(oldItem);

				int iDays = GetDateDiffDays(newStartDateTime, oldStartDateTime);

				TimeSpan newTS = new TimeSpan();
				if (newItem.EventStartTime.HasValue) {
					newTS = newItem.EventStartTime.Value;
				}
				TimeSpan oldTS = new TimeSpan();
				if (oldItem.EventStartTime.HasValue) {
					oldTS = oldItem.EventStartTime.Value;
				}
				int iMin = GetDateDiffMinutes(newTS, oldTS);

				DateTime dateMax = (from d in eventUTCDates
									orderby d.Date descending
									where d.Date >= DateTime.UtcNow.Date
									select d).FirstOrDefault();

				if (dateMax == DateTime.MinValue) {
					dateMax = DateTime.UtcNow.Date;
				}
				dateMax = site.ConvertSiteTimeToUTC(dateMax).Date;

				DateTime dStart = site.ConvertSiteTimeToUTC(newStartDateTime).Date;
				DateTime dEnd = site.ConvertSiteTimeToUTC(newEndDateTime).Date;

				int iFuture = (from d in eventUTCDates
							   where d.Date >= DateTime.UtcNow.Date
							   select d).Count();

				int iMaxRange = (from c in ctx.carrot_CalendarEvents
								 where c.CalendarEventProfileID == newItem.CalendarEventProfileID
										&& c.EventDate.Date >= dateMax.Date
								 select c.CalendarEventID).Count();

				if (oldItem.Frequency != newItem.Frequency || oldItem.RecursEvery != newItem.RecursEvery
					|| iMin != 0 || iDays != 0 || (iMaxRange != iFuture && iFuture > 0)
					|| oldItem.EventEndDate.Date != newItem.EventEndDate.Date
					|| oldItem.EventStartDate.Date != newItem.EventStartDate.Date) {
					var lstEvents = (from c in ctx.carrot_CalendarEvents
									 orderby c.EventDate
									 where c.CalendarEventProfileID == newItem.CalendarEventProfileID
									 select c).ToList();

					if (iDays != 0) {
						lstEvents.ForEach(x => x.EventDate = site.ConvertUTCToSiteTime(x.EventDate).Date);

						if (newItem.Frequency == FrequencyType.Daily || newItem.Frequency == FrequencyType.Once) {
							lstEvents.ForEach(x => x.EventDate = x.EventDate.Date.AddDays(iDays));
						}

						if (newItem.Frequency == FrequencyType.Weekly) {
							int iDayShift = iDays;
							if (newItem.EventRepeatPattern != null) {
								iDayShift = 7 * GetDateDiffWeeks(newItem.EventStartDate, oldItem.EventStartDate);
							}
							lstEvents.ForEach(x => x.EventDate = x.EventDate.Date.AddDays(iDayShift));
						}

						if (newItem.Frequency == FrequencyType.Monthly || newItem.Frequency == FrequencyType.Yearly) {
							int iMonths = GetDateDiffMonths(newItem.EventStartDate, oldItem.EventStartDate);

							lstEvents.ForEach(x => x.EventDate = x.EventDate.AddMonths(iMonths).Date);
						}

						if (newItem.Frequency == oldItem.Frequency && (newItem.Frequency == FrequencyType.Monthly || newItem.Frequency == FrequencyType.Yearly)) {
							lstEvents.ForEach(x => x.EventDate = CorrectMonthlyYearlyDate(x.EventDate, newItem.EventStartDate.Day).Date);
						}

						lstEvents.ForEach(x => x.EventDate = site.ConvertSiteTimeToUTC(x.EventDate));
					}

					var lstDel = (from l in lstEvents
								  orderby l.EventDate
								  where l.EventDate.Date < dStart || l.EventDate.Date > dEnd
									|| !eventUTCDates.Contains(l.EventDate)
								  select l).ToList();

					lstDel.RemoveAll(l => eventUTCDates.Contains(l.EventDate));

					ctx.carrot_CalendarEvents.DeleteAllOnSubmit(lstDel);

					var lstExist = (from l in lstEvents select site.ConvertUTCToSiteTime(l.EventDate).Date).Distinct().ToList();

					var lstAdd = (from d in eventDates
								  where !lstExist.Contains(d.Date)
								  select d).ToList();

					InsertEventsFromList(ctx, newItem, lstAdd);
				}
			}
		}
		protected static List<DateTime> GetSequenceDates(DateTime testDate, bool createRecentOnly, CalendarEvent eventProfile) {
			SiteData site = SiteData.CurrentSite;

			List<DateTime> eventDates = new List<DateTime>();
			DateTime backportDate = testDate.AddMonths(-6).Date;
			DateTime endDateRange = testDate.AddYears(5).Date;

			DateTime dateToAdd = GetStartDateTimeFromItem(eventProfile).Date;

			DateTime oldDate = eventProfile.EventStartDate;
			int iOldDaysInMonth = DateTime.DaysInMonth(oldDate.Year, oldDate.Month);
			int iDayOfMonth = oldDate.Day;

			switch (eventProfile.Frequency) {
				case FrequencyType.Once:
					eventDates.Add(dateToAdd.Date);
					break;

				case FrequencyType.Weekly:
					if (eventProfile.EventRepeatPattern == null) {
						while (eventDates.Count < 2000 && dateToAdd.Date <= endDateRange && dateToAdd.Date <= eventProfile.EventEndDate) {
							if (!createRecentOnly || (createRecentOnly && dateToAdd.Date >= backportDate)) {
								eventDates.Add(dateToAdd.Date);
							}
							dateToAdd = dateToAdd.AddDays(7 * eventProfile.RecursEvery);
						}
					} else {
						while (eventDates.Count < 5000 && dateToAdd.Date <= endDateRange && dateToAdd.Date <= eventProfile.EventEndDate) {
							if (eventProfile.DaysValid.Contains(dateToAdd.DayOfWeek)) {
								if (!createRecentOnly || (createRecentOnly && dateToAdd.Date >= backportDate)) {
									eventDates.Add(dateToAdd.Date);
								}
							}

							if (eventProfile.RecursEvery > 1) {
								if (oldDate.DayOfWeek == dateToAdd.AddDays(1).DayOfWeek) {
									dateToAdd = dateToAdd.AddDays((7 * (eventProfile.RecursEvery - 1)) + 1);
								} else {
									dateToAdd = dateToAdd.AddDays(1);
								}
							} else {
								dateToAdd = dateToAdd.AddDays(1);
							}
						}
					}

					break;

				case FrequencyType.Daily:

					while (eventDates.Count < 5000 && dateToAdd.Date <= endDateRange && dateToAdd.Date <= eventProfile.EventEndDate) {
						if (!createRecentOnly || (createRecentOnly && dateToAdd.Date >= backportDate)) {
							eventDates.Add(dateToAdd.Date);
						}
						dateToAdd = dateToAdd.AddDays(1);
					}
					break;

				case FrequencyType.Yearly:

					while (eventDates.Count < 25 && dateToAdd.Date <= endDateRange && dateToAdd.Date <= eventProfile.EventEndDate) {
						if (!createRecentOnly || (createRecentOnly && dateToAdd.Date >= backportDate)) {
							eventDates.Add(dateToAdd.Date);
						}

						dateToAdd = dateToAdd.AddYears(1 * eventProfile.RecursEvery);
						dateToAdd = CorrectMonthlyYearlyDate(dateToAdd, iDayOfMonth);
					}
					break;

				case FrequencyType.Monthly:

					while (eventDates.Count < 360 && dateToAdd.Date <= endDateRange && dateToAdd.Date <= eventProfile.EventEndDate) {
						if (!createRecentOnly || (createRecentOnly && dateToAdd.Date >= backportDate)) {
							eventDates.Add(dateToAdd.Date);
						}

						dateToAdd = dateToAdd.AddMonths(1 * eventProfile.RecursEvery);
						dateToAdd = CorrectMonthlyYearlyDate(dateToAdd, iDayOfMonth);
					}
					break;
			}

			return eventDates;
		}
		protected static void InsertEventsFromList(CalendarDataContext ctx, CalendarEvent item, List<DateTime> lstDates) {
			SiteData site = SiteData.CurrentSite;

			foreach (DateTime date in lstDates) {
				carrot_CalendarEvent evt = new carrot_CalendarEvent {
					CalendarEventID = Guid.NewGuid(),
					CalendarEventProfileID = item.CalendarEventProfileID,
					EventDate = site.ConvertSiteTimeToUTC(date),
					IsCancelled = false
				};

				ctx.carrot_CalendarEvents.InsertOnSubmit(evt);
			}
		}
		protected static DateTime GetEndDateTimeFromItem(CalendarEvent item) {
			DateTime newStartDateTime = item.EventStartDate;
			if (item.EventStartTime != null) {
				newStartDateTime = CalendarHelper.GetFullDateTime(item.EventEndDate, item.EventStartTime);
			}

			return newStartDateTime;
		}
		protected void btnSave_Click(object sender, EventArgs e) {
			bool bAdd = false;

			using (CalendarDataContext db = CalendarDataContext.GetDataContext()) {
				var currItem = (from c in db.carrot_CalendarEventProfiles
								where c.CalendarEventProfileID == ItemGuid
								select c).FirstOrDefault();

				var origItem = new CalendarEvent(currItem);

				if (currItem == null) {
					bAdd = true;
					ItemGuid = Guid.NewGuid();
					currItem = new carrot_CalendarEventProfile();
					currItem.CalendarEventProfileID = ItemGuid;
					currItem.SiteID = SiteID;
					currItem.IsHoliday = false;
					currItem.IsAnnualHoliday = false;
					currItem.RecursEvery = 1;
				}

				currItem.CalendarFrequencyID = new Guid(ddlRecurr.SelectedValue);
				currItem.CalendarEventCategoryID = new Guid(ddlCategory.SelectedValue);

				currItem.EventRepeatPattern = null;

				List<string> days = CalendarHelper.GetCheckedItemStringByValue(rpDays, true, "chkDay");

				if (CalendarFrequencyHelper.GetFrequencyTypeByID(currItem.CalendarFrequencyID) == CalendarFrequencyHelper.FrequencyType.Weekly
					&& days.Count > 0) {
					int dayMask = (from d in days select int.Parse(d)).Sum();

					if (dayMask > 0) {
						currItem.EventRepeatPattern = dayMask;
					}
				}

				currItem.EventTitle = txtEventTitle.Text;
				currItem.EventDetail = reContent.Text;
				currItem.RecursEvery = int.Parse(txtRecursEvery.Text);

				currItem.IsPublic = chkIsPublic.Checked;
				currItem.IsAllDayEvent = chkIsAllDayEvent.Checked;
				currItem.IsCancelled = chkIsCancelled.Checked;
				currItem.IsCancelledPublic = chkIsCancelledPublic.Checked;

				currItem.EventStartDate = Convert.ToDateTime(txtEventStartDate.Text);
				currItem.EventStartTime = CalendarHelper.GetTimeSpanFromTextbox(txtEventStartTime);

				currItem.EventEndDate = Convert.ToDateTime(txtEventEndDate.Text);
				currItem.EventEndTime = CalendarHelper.GetTimeSpanFromTextbox(txtEventEndTime);

				if (bAdd) {
					db.carrot_CalendarEventProfiles.InsertOnSubmit(currItem);
				}

				CalendarFrequencyHelper.SaveFrequencies(db, new CalendarEvent(currItem), origItem);

				db.SubmitChanges();
			}

			Response.Redirect(CreateLink(ModuleName, String.Format("id={0}", ItemGuid)));
		}
        protected void btnSave_Click(object sender, EventArgs e)
        {
            bool bAdd = false;

            using (CalendarDataContext db = CalendarDataContext.GetDataContext()) {
                var currItem = (from c in db.carrot_CalendarEventProfiles
                                where c.CalendarEventProfileID == ItemGuid
                                select c).FirstOrDefault();

                var origItem = new CalendarEvent(currItem);

                if (currItem == null)
                {
                    bAdd     = true;
                    ItemGuid = Guid.NewGuid();
                    currItem = new carrot_CalendarEventProfile();
                    currItem.CalendarEventProfileID = ItemGuid;
                    currItem.SiteID          = SiteID;
                    currItem.IsHoliday       = false;
                    currItem.IsAnnualHoliday = false;
                    currItem.RecursEvery     = 1;
                }

                currItem.CalendarFrequencyID     = new Guid(ddlRecurr.SelectedValue);
                currItem.CalendarEventCategoryID = new Guid(ddlCategory.SelectedValue);

                currItem.EventRepeatPattern = null;

                List <string> days = CalendarHelper.GetCheckedItemStringByValue(rpDays, true, "chkDay");

                if (CalendarFrequencyHelper.GetFrequencyTypeByID(currItem.CalendarFrequencyID) == CalendarFrequencyHelper.FrequencyType.Weekly &&
                    days.Count > 0)
                {
                    int dayMask = (from d in days select int.Parse(d)).Sum();

                    if (dayMask > 0)
                    {
                        currItem.EventRepeatPattern = dayMask;
                    }
                }

                currItem.EventTitle  = txtEventTitle.Text;
                currItem.EventDetail = reContent.Text;
                currItem.RecursEvery = int.Parse(txtRecursEvery.Text);

                currItem.IsPublic          = chkIsPublic.Checked;
                currItem.IsAllDayEvent     = chkIsAllDayEvent.Checked;
                currItem.IsCancelled       = chkIsCancelled.Checked;
                currItem.IsCancelledPublic = chkIsCancelledPublic.Checked;

                currItem.EventStartDate = Convert.ToDateTime(txtEventStartDate.Text);
                currItem.EventStartTime = CalendarHelper.GetTimeSpanFromTextbox(txtEventStartTime);

                currItem.EventEndDate = Convert.ToDateTime(txtEventEndDate.Text);
                currItem.EventEndTime = CalendarHelper.GetTimeSpanFromTextbox(txtEventEndTime);

                if (bAdd)
                {
                    db.carrot_CalendarEventProfiles.InsertOnSubmit(currItem);
                }

                CalendarFrequencyHelper.SaveFrequencies(db, new CalendarEvent(currItem), origItem);

                db.SubmitChanges();
            }

            Response.Redirect(CreateLink(ModuleName, String.Format("id={0}", ItemGuid)));
        }