internal CalendarEvent(carrot_CalendarEventProfile p)
        {
            if (p != null)
            {
                this.CalendarEventProfileID  = p.CalendarEventProfileID;
                this.CalendarFrequencyID     = p.CalendarFrequencyID;
                this.CalendarEventCategoryID = p.CalendarEventCategoryID;
                this.EventStartDate          = p.EventStartDate;
                this.EventStartTime          = p.EventStartTime;
                this.EventEndDate            = p.EventEndDate;
                this.EventEndTime            = p.EventEndTime;
                this.EventTitle         = p.EventTitle;
                this.EventDetail        = p.EventDetail;
                this.EventRepeatPattern = p.EventRepeatPattern;
                this.RecursEvery        = p.RecursEvery;
                this.IsCancelled        = p.IsCancelled;
                this.IsCancelledPublic  = p.IsCancelledPublic;
                this.IsAllDayEvent      = p.IsAllDayEvent;
                this.IsPublic           = p.IsPublic;
                this.IsAnnualHoliday    = p.IsAnnualHoliday;
                this.IsHoliday          = p.IsHoliday;

                this.SiteID = p.SiteID;

                this.Frequency = CalendarFrequencyHelper.GetFrequencyTypeByID(p.CalendarFrequencyID);
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            ItemGuid = ParmParser.GetGuidIDFromQuery();

            btnCopyButton.Visible   = !(ItemGuid == Guid.Empty);
            btnDeleteButton.Visible = !(ItemGuid == Guid.Empty);
            btnCopy.Visible         = !(ItemGuid == Guid.Empty);
            btnDelete.Visible       = !(ItemGuid == Guid.Empty);

            if (!IsPostBack)
            {
                CalendarHelper.BindRepeater(rpDays, CalendarHelper.DaysOfTheWeek);

                Dictionary <Guid, string> colors = (from c in CalendarHelper.GetCalendarCategories(SiteID)
                                                    select new KeyValuePair <Guid, string>(c.CalendarEventCategoryID, string.Format("{0}|{1}", c.CategoryBGColor, c.CategoryFGColor)))
                                                   .ToDictionary(kvp => kvp.Key, kvp => kvp.Value);

                CalendarHelper.BindDropDownList(ddlColors, colors);

                var freq = CalendarFrequencyHelper.GetCalendarFrequencies();
                var cat  = CalendarHelper.GetCalendarCategories(SiteID);

                txtEventStartDate.Text = SiteData.CurrentSite.Now.ToShortDateString();
                txtEventEndDate.Text   = SiteData.CurrentSite.Now.ToShortDateString();

                CalendarHelper.BindDropDownList(ddlRecurr, freq, CalendarFrequencyHelper.GetIDByFrequencyType(CalendarFrequencyHelper.FrequencyType.Once).ToString());
                CalendarHelper.BindDropDownList(ddlCategory, cat);

                var itm = CalendarHelper.GetProfile(ItemGuid);

                if (itm != null)
                {
                    selectedDatePattern = itm.EventRepeatPattern;
                    CalendarHelper.BindRepeater(rpDays, CalendarHelper.DaysOfTheWeek);

                    txtEventTitle.Text = itm.EventTitle;
                    reContent.Text     = itm.EventDetail;

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

                    txtEventStartDate.Text = itm.EventStartDate.ToShortDateString();
                    txtEventEndDate.Text   = itm.EventEndDate.ToShortDateString();

                    txtRecursEvery.Text = itm.RecursEvery.ToString();

                    CalendarHelper.SetTextboxToTimeSpan(txtEventStartTime, itm.EventStartTime);
                    CalendarHelper.SetTextboxToTimeSpan(txtEventEndTime, itm.EventEndTime);

                    ddlRecurr.SelectedValue   = itm.CalendarFrequencyID.ToString();
                    ddlCategory.SelectedValue = itm.CalendarEventCategoryID.ToString();
                }
            }
        }
        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)));
        }