public ActionResult AddTitle(DiaryTitle DiaryTitle, int id)
        {
            iDiary iDiary = new iDiary();

            try
            {
                DiaryTitle.AddDiaryTitle();
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return Redirect(Request.UrlReferrer.ToString());
        }
        public ActionResult AddText(DiaryText DiaryText, int id)
        {
            iDiary iDiary = new iDiary();

            try
            {
                DiaryText.LeadTypeID = iDiary.GetLeadTypeIDbyLeadID(DiaryText.LeadTypeID);
                DiaryText.AddDiaryText();
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return Redirect(Request.UrlReferrer.ToString());
        }
 public ActionResult Calendar()
 {
     iDiary Diary = new iDiary();
     Diary.GetAllUsers();
     return PartialView(Diary);
 }
        /// <summary>
        /// Generate the Calendar Model on the fly
        /// </summary>
        /// <param name="diary">iDiary object from the DB</param>
        /// <returns>Model with data formatted as per the UI control</returns>
        private List<Calendar> ToCalendarist(iDiary diary)
        {
            List<Calendar> CalendarTasks = new List<Calendar>();

            for (int i = 0; i < diary.Appointments.Count; i++)
            {
                if (diary.Appointments[i].Completed)
                {
                    CalendarTasks.Add(new Calendar
                    {
                        id = diary.Appointments[i].DiaryAppointmentID,
                        title = diary.Appointments[i].DiaryAppointmentTitle + " User: "******" Customer: " + diary.Appointments[i].CustomerName,
                        start = diary.Appointments[i].DueDate.ToString("s"),
                        end = diary.Appointments[i].EndDate.ToString("s"),
                        allDay = diary.Appointments[i].AllDayEvent,
                        url = "/iDiary/Edit/" + diary.Appointments[i].DiaryAppointmentID.ToString() + "/",
                        className = "label label-success",
                        User = diary.Appointments[i].ResourceInfo,
                        editable = false,
                        backgroundColor = diary.DiaryPriorities[i].DiaryPriorityColor,
                        tooltip = "<p>Lead ID: <a target='_blank' href='" + System.Configuration.ConfigurationManager.AppSettings["AquariumURL"].ToString() + "/CustomersLeadDetails2.aspx?lid=" + diary.Appointments[i].LeadID.ToString() + "'>" + diary.Appointments[i].LeadID.ToString() + "</a><br/>Customer: " + diary.Appointments[i].CustomerName + "<br/>Date: " + diary.Appointments[i].EndDate.ToString() + "</p>"
                    });
                }
                else
                {
                    if (diary.Appointments[i].EndDate.Date == DateTime.Now.Date)
                    {
                        CalendarTasks.Add(new Calendar
                        {
                            id = diary.Appointments[i].DiaryAppointmentID,
                            title = diary.Appointments[i].DiaryAppointmentTitle + " User: "******" Customer: " + diary.Appointments[i].CustomerName,
                            start = diary.Appointments[i].DueDate.ToString("s"),
                            end = diary.Appointments[i].EndDate.ToString("s"),
                            allDay = diary.Appointments[i].AllDayEvent,
                            url = "/iDiary/Edit/" + diary.Appointments[i].DiaryAppointmentID.ToString() + "/",
                            className = "label label-important",
                            User = diary.Appointments[i].ResourceInfo,
                            editable = true,
                            backgroundColor = diary.DiaryPriorities[i].DiaryPriorityColor,
                            tooltip = "<p>Lead ID: <a target='_blank' href='" + System.Configuration.ConfigurationManager.AppSettings["AquariumURL"].ToString() + "/CustomersLeadDetails2.aspx?lid=" + diary.Appointments[i].LeadID.ToString() + "'>" + diary.Appointments[i].LeadID.ToString() + "</a><br/>Customer: " + diary.Appointments[i].CustomerName + "<br/>Date: " + diary.Appointments[i].EndDate.ToString() + "<br/><a href='/iDiary/Complete/" + diary.Appointments[i].DiaryAppointmentID.ToString() + "/'>Mark Complete</a></p>"
                        });
                    }
                    else
                    {
                        CalendarTasks.Add(new Calendar
                        {
                            id = diary.Appointments[i].DiaryAppointmentID,
                            title = diary.Appointments[i].DiaryAppointmentTitle + " User: "******" Customer: " + diary.Appointments[i].CustomerName,
                            start = diary.Appointments[i].DueDate.ToString("s"),
                            end = diary.Appointments[i].EndDate.ToString("s"),
                            allDay = diary.Appointments[i].AllDayEvent,
                            url = "/iDiary/Edit/" + diary.Appointments[i].DiaryAppointmentID.ToString() + "/",
                            className = "label label-warning",
                            User = diary.Appointments[i].ResourceInfo,
                            editable = true,
                            backgroundColor = diary.DiaryPriorities[i].DiaryPriorityColor,
                            tooltip = "<p>Lead ID: <a target='_blank' href='" + System.Configuration.ConfigurationManager.AppSettings["AquariumURL"].ToString() + "/CustomersLeadDetails2.aspx?lid=" + diary.Appointments[i].LeadID.ToString() + "'>" + diary.Appointments[i].LeadID.ToString() + "</a><br/>Customer: " + diary.Appointments[i].CustomerName + "<br/>Date: " + diary.Appointments[i].EndDate.ToString() + "<br/><a href='/iDiary/Complete/" + diary.Appointments[i].DiaryAppointmentID.ToString() + "/'>Mark Complete</a></p>"
                        });
                    }
                }

            }

            return CalendarTasks;
        }
        public ActionResult List(int ID, double EndDate, int completed)
        {
            iDiary Diary = new iDiary();

            Diary.StartDate = DateTime.Now.AddMonths(-6);
            Diary.EndDate = UnixTimeStampToDateTime(EndDate);

            try
            {
                if (!Diary.GetAppointments(ID, completed))
                {
                    //ModelState.AddModelError("", "Error!");
                    Diary.Appointments = new List<DiaryAppointment>();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return PartialView(Diary);
        }
        public ActionResult Index()
        {
            iDiary Diary = new iDiary();

            return View(Diary);
        }
        public ActionResult GetDiaryTexts(int ID, int LeadID)
        {
            iDiary Diary = new iDiary();
            try
            {
                int LeadTypeID = Diary.GetLeadTypeIDbyLeadID(LeadID);
                Diary.GetAllDiaryTexts(ID, LeadTypeID);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return Json(Diary.DiaryTexts.AllDiaryTexts.ToArray(), JsonRequestBehavior.AllowGet);
        }
        public JsonResult GetCalendar(double start, double end, int ID, int completed)
        {
            iDiary Diary = new iDiary();
            var events = new List<Calendar>();

            Diary.StartDate = UnixTimeStampToDateTime(start);
            Diary.EndDate = UnixTimeStampToDateTime(end);

            try
            {
                if (!Diary.GetAppointments(ID, completed))
                {
                    //ModelState.AddModelError("", "Error!");
                    Diary.Appointments = new List<DiaryAppointment>();
                }

                events = ToCalendarist(Diary);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            var rows = events.ToArray();
            return Json(rows, JsonRequestBehavior.AllowGet);
        }
        public ActionResult Edit(iDiary iDiary)
        {
            iDiary.Appointments[0].CreatedBy = iDiary.Reminders[0].ClientPersonnelID;

            try
            {
                if (iDiary.AddDiaryAppointment())
                {
                    iDiary.AddDiaryLog(iDiary.Appointments[0].DiaryAppointmentID, 3, DateTime.Now, iDiary.Appointments[0].CreatedBy, "Task Updated");
                    iDiary.GetAllLeads();
                    iDiary.GetAllUsers();
                    iDiary.GetAllTimeshiftValues();
                    iDiary.GetAllDiaryTitles();
                    iDiary.GetDefaultDiaryText();
                    iDiary.GetAllDiaryLogsbyID(iDiary.Appointments[0].DiaryAppointmentID);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return View(iDiary);
        }
        public ActionResult Edit(int id)
        {
            iDiary Diary = new iDiary();

            try
            {
                if (Diary.GetAppointmentbyID(id))
                {
                    String.Format("{0:d/M/yyyy HH:mm}", Diary.Appointments[0].EndDate);
                    String.Format("{0:d/M/yyyy HH:mm}", Diary.Appointments[0].DueDate);

                    Diary.GetAllDiaryLogsbyID(id);
                }
                else
                {
                    Diary.Appointments = new List<DiaryAppointment>();
                    Diary.Reminders = new List<AquariumDiaryManagement.Reminder>();

                    Diary.Appointments.Add(new DiaryAppointment());
                    Diary.Reminders.Add(new AquariumDiaryManagement.Reminder());

                    Diary.Appointments[0].EndDate = DateTime.Now.AddDays(7);
                    Diary.Appointments[0].DueDate = DateTime.Now;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return View(Diary);
        }
        public ActionResult Create(iDiary iDiary)
        {
            iDiary.Appointments[0].CreatedBy = iDiary.Reminders[0].ClientPersonnelID;
            try
            {
                if (iDiary.AddDiaryAppointment())
                {
                    ModelState.Clear();
                    iDiary.Appointments.Clear();
                    iDiary.Reminders.Clear();
                    iDiary.ReminderValues = new List<string>();
                    iDiary.Appointments = new List<DiaryAppointment>();
                    iDiary.Reminders = new List<AquariumDiaryManagement.Reminder>();
                    iDiary.DiaryTexts.AllDiaryTexts = new List<DiaryText>();
                    iDiary.DiaryTitles.AllDiaryTitles = new List<DiaryTitle>();

                    iDiary.Appointments.Add(new DiaryAppointment());
                    iDiary.Reminders.Add(new AquariumDiaryManagement.Reminder());

                    iDiary.Appointments[0].EndDate = DateTime.Now.AddDays(7);
                    iDiary.Appointments[0].DueDate = DateTime.Now;

                    iDiary.GetAllLeads();
                    iDiary.GetAllUsers();
                    iDiary.GetAllTimeshiftValues();
                    iDiary.GetAllDiaryTitles();
                    iDiary.GetDefaultDiaryText();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return View(iDiary);
        }
        public ActionResult Create()
        {
            iDiary Diary = new iDiary();

            Diary.ReminderValues = new List<string>();
            Diary.Appointments = new List<DiaryAppointment>();
            Diary.Reminders = new List<AquariumDiaryManagement.Reminder>();
            Diary.DiaryPriorities = new List<DiaryPriority>();
            DiaryPriority dp = new DiaryPriority();

            try
            {
                Diary.Appointments.Add(new DiaryAppointment());
                Diary.Reminders.Add(new AquariumDiaryManagement.Reminder());
                dp.GetAllDiaryPriorities();
                Diary.DiaryPriorities.Add(dp);

                Diary.Appointments[0].EndDate = DateTime.Now.AddDays(7);
                Diary.Appointments[0].DueDate = DateTime.Now;
                Diary.GetAllLeads();
                Diary.GetAllUsers();
                Diary.GetAllTimeshiftValues();
                Diary.GetAllDiaryTitles();
                Diary.GetDefaultDiaryText();
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return View(Diary);
        }
        public ActionResult Complete(int id)
        {
            iDiary Diary = new iDiary();

            try
            {
                if (Diary.GetAppointmentbyID(id))
                {
                    Diary.Appointments[0].Completed = true;
                    Diary.Appointments[0].EndDate = DateTime.Now;

                    Diary.AddDiaryAppointment();
                    Diary.AddDiaryLog(Diary.Appointments[0].DiaryAppointmentID, 5, DateTime.Now, Diary.Appointments[0].CreatedBy, "Task Completed");
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return RedirectToAction("Index", "iDiary");
        }