public ActionResult Create(CreateAppointmentRequestModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    //deconstructs the string down to a collection of student IDs
                    List<String> attendeesID = new List<string>();
                    if (model.Attendees != null)
                    {
                        String EmployeeID = model.Attendees;
                        String[] attendees = EmployeeID.Split(',');

                        for (int i = 0; i < attendees.Count() - 1; i++)
                        {
                            if (i != 0)
                            {
                                String id = attendees[i].Remove(0, attendees[i].Length - 10);
                                attendeesID.Add(id.Remove(id.Length - 1));
                            }
                            else
                            {
                                String id = attendees[i].Remove(0, attendees[i].Length - 10);
                                attendeesID.Add(id.Remove(id.Length - 1));
                            }
                        }
                    }
                    model._appointment.employeeid = model._appointment.employeeid.Remove(0, model._appointment.employeeid.Length - 10);
                    model._appointment.employeeid = model._appointment.employeeid.Remove(model._appointment.employeeid.Length - 1);

                    model.repeatingType = new String[] { "Not Repeating", "Daily (Business Days)", "Weekly", "Bi-Weekly", "Monthly", "Yearly" };
                    IEnumerable<campu> campus = db.campus;
                    List<String> list = new List<String>();
                    foreach (campu camp in campus)
                    {
                        list.Add(camp.cname);
                    }
                    IEnumerable<student> students = db.students;
                    List<AutoCompletePOCO> AutoCompleteID = new List<AutoCompletePOCO>();
                    foreach (student stud in students)
                    {
                        AutoCompletePOCO poco = new AutoCompletePOCO()
                        {
                            value = stud.fname + " " + stud.lname + " (" + stud.studentid + ")",
                            Label = stud.fname + " " + stud.lname + " (" + stud.studentid + ")",
                            Email = stud.email,
                            Role = "Student"
                        };
                        AutoCompleteID.Add(poco);
                    }
                    IEnumerable<employee> employees = db.employees;
                    List<AutoCompletePOCO> EmpAutoCom = new List<AutoCompletePOCO>();
                    foreach (employee emp in employees)
                    {
                        AutoCompletePOCO poco = new AutoCompletePOCO()
                        {
                            value = emp.fname + " " + emp.lname + " (" + emp.employeeid + ")",
                            Label = emp.fname + " " + emp.lname + " (" + emp.employeeid + ")",
                            Email = emp.email,
                            Role = emp.role
                        };
                        AutoCompleteID.Add(poco);
                        EmpAutoCom.Add(poco);
                    }
                    model.AttendeesAutoComplete = AutoCompleteID;
                    model.EmployeeID = EmpAutoCom;
                    String[] AppointmentType = new String[3] { "Advisement", "Personal", "Office" };
                    model._campus = list;
                    model.appoingmentType = AppointmentType;

                    DateTime date1 = DateTime.Parse(model.startTime);
                    TimeSpan time1 = new TimeSpan(date1.Hour, date1.Minute, date1.Second);
                    DateTime date2 = DateTime.Parse(model.endTime);
                    TimeSpan time2 = new TimeSpan(date2.Hour, date2.Minute, date2.Second);
                    model._appointment.starttime = model._appointment.starttime.Add(time1);
                    model._appointment.endtime = model._appointment.endtime.Add(time2);
                    //based on whether the appointment is repeating will use one fo the following sets of code to create the appoinment/series of appointments
                    if (model.repeating.Trim().Equals("Not Repeating"))
                    {
                        model._appointment.appointmentid = Guid.NewGuid();
                        db.appointments.AddObject(model._appointment);
                        db.SaveChanges();

                        if (attendeesID.Count > 0)
                        {
                            List<Attendee> Attendee = new List<Models.Attendee>();
                            foreach (String id in attendeesID)
                            {
                                Attendee attendee = new Attendee()
                                {
                                    id = Guid.NewGuid(),
                                    appointmentid = model._appointment.appointmentid,
                                    attendee1 = id
                                };

                                student stud = db.students.SingleOrDefault(s => s.studentid == id);

                                if (stud != null)
                                {
                                    attendee.confirmed = true;
                                }

                                Attendee check = db.Attendees.SingleOrDefault(a => a.attendee1 == attendee.attendee1 && a.appointmentid == attendee.appointmentid);
                                if (check == null)
                                {
                                    if (!attendee.attendee1.Equals(model._appointment.employeeid))
                                        Attendee.Add(attendee);
                                }
                            }
                            foreach (Attendee attend in Attendee)
                            {
                                Attendee check = db.Attendees.SingleOrDefault(a => a.attendee1 == attend.attendee1 && a.appointmentid == attend.appointmentid);
                                if (check == null)
                                db.Attendees.AddObject(attend);
                                db.SaveChanges();
                            }
                        }
                    }
                    else if (model.repeating.Trim().Equals("Daily (Business Days)"))
                    {
                        DateTime endRepeatingDate = model._appointment.endtime;
                        model._appointment.endtime = model._appointment.starttime;
                        model._appointment.endtime = model._appointment.endtime.Add(-time1);
                        model._appointment.endtime = model._appointment.endtime.Add(time2);
                        model._appointment.repeating = Guid.NewGuid();

                        while (model._appointment.starttime.Date <= endRepeatingDate.Date)
                        {
                            if ((model._appointment.starttime.DayOfWeek != DayOfWeek.Saturday) && (model._appointment.starttime.DayOfWeek != DayOfWeek.Sunday))
                            {
                                appointment appointment = new appointment()
                                    {
                                        allday = model._appointment.allday,
                                        appointmenttype = model._appointment.appointmenttype,
                                        cname = model._appointment.cname,
                                        description = model._appointment.description,
                                        employeeid = model._appointment.employeeid,
                                        endtime = model._appointment.endtime,
                                        repeating = model._appointment.repeating,
                                        starttime = model._appointment.starttime,
                                        subject = model._appointment.subject
                                    };
                                appointment.appointmentid = Guid.NewGuid();
                                model._appointment.appointmentid = appointment.appointmentid;
                                db.appointments.AddObject(appointment);
                                db.SaveChanges();

                                if (attendeesID.Count > 0)
                                {
                                    List<Attendee> Attendee = new List<Models.Attendee>();
                                    foreach (String id in attendeesID)
                                    {
                                        Attendee attendee = new Attendee()
                                        {
                                            id = Guid.NewGuid(),
                                            appointmentid = appointment.appointmentid,
                                            attendee1 = id
                                        };

                                        student stud = db.students.SingleOrDefault(s => s.studentid == id);

                                        if (stud != null)
                                        {
                                            attendee.confirmed = true;
                                        }

                                        Attendee check = db.Attendees.SingleOrDefault(a => a.attendee1 == attendee.attendee1 && a.appointmentid == attendee.appointmentid);
                                        if (check == null)
                                        {
                                            if (!attendee.attendee1.Equals(model._appointment.employeeid))
                                                Attendee.Add(attendee);
                                        }
                                    }
                                    foreach (Attendee attend in Attendee)
                                    {
                                        Attendee check = db.Attendees.SingleOrDefault(a => a.attendee1 == attend.attendee1 && a.appointmentid == attend.appointmentid);
                                        if (check == null)
                                        db.Attendees.AddObject(attend);
                                        db.SaveChanges();
                                    }
                                }
                            }
                            model._appointment.starttime = model._appointment.starttime.AddDays(1);
                            model._appointment.endtime = model._appointment.endtime.AddDays(1);
                        }

                    }
                    else if (model.repeating.Trim().Equals("Weekly"))
                    {
                        DateTime endRepeatingDate = model._appointment.endtime;
                        model._appointment.endtime = model._appointment.starttime;
                        model._appointment.endtime = model._appointment.endtime.Add(-time1);
                        model._appointment.endtime = model._appointment.endtime.Add(time2);
                        model._appointment.repeating = Guid.NewGuid();

                        while (model._appointment.starttime.Date <= endRepeatingDate.Date)
                        {
                                appointment appointment = new appointment()
                                {
                                    allday = model._appointment.allday,
                                    appointmenttype = model._appointment.appointmenttype,
                                    cname = model._appointment.cname,
                                    description = model._appointment.description,
                                    employeeid = model._appointment.employeeid,
                                    endtime = model._appointment.endtime,
                                    repeating = model._appointment.repeating,
                                    starttime = model._appointment.starttime,
                                    subject = model._appointment.subject
                                };
                                appointment.appointmentid = Guid.NewGuid();
                                model._appointment.appointmentid = appointment.appointmentid;
                                db.appointments.AddObject(appointment);
                                db.SaveChanges();

                                if (attendeesID.Count > 0)
                                {
                                    List<Attendee> Attendee = new List<Models.Attendee>();
                                    foreach (String id in attendeesID)
                                    {
                                        Attendee attendee = new Attendee()
                                        {
                                            id = Guid.NewGuid(),
                                            appointmentid = appointment.appointmentid,
                                            attendee1 = id
                                        };

                                        student stud = db.students.SingleOrDefault(s => s.studentid == id);

                                        if (stud != null)
                                        {
                                            attendee.confirmed = true;
                                        }

                                        Attendee check = db.Attendees.SingleOrDefault(a => a.attendee1 == attendee.attendee1 && a.appointmentid == attendee.appointmentid);
                                        if (check == null)
                                        {
                                            if (!attendee.attendee1.Equals(model._appointment.employeeid))
                                                Attendee.Add(attendee);
                                        }
                                    }
                                    foreach (Attendee attend in Attendee)
                                    {
                                        Attendee check = db.Attendees.SingleOrDefault(a => a.attendee1 == attend.attendee1 && a.appointmentid == attend.appointmentid);
                                        if (check == null)
                                        db.Attendees.AddObject(attend);
                                        db.SaveChanges();
                                    }
                            }
                            model._appointment.starttime = model._appointment.starttime.AddDays(7);
                            model._appointment.endtime = model._appointment.endtime.AddDays(7);
                        }

                    }
                    else if (model.repeating.Trim().Equals("Bi-Weekly"))
                    {
                        DateTime endRepeatingDate = model._appointment.endtime;
                        model._appointment.endtime = model._appointment.starttime;
                        model._appointment.endtime = model._appointment.endtime.Add(-time1);
                        model._appointment.endtime = model._appointment.endtime.Add(time2);
                        model._appointment.repeating = Guid.NewGuid();

                        while (model._appointment.starttime.Date <= endRepeatingDate.Date)
                        {
                            appointment appointment = new appointment()
                            {
                                allday = model._appointment.allday,
                                appointmenttype = model._appointment.appointmenttype,
                                cname = model._appointment.cname,
                                description = model._appointment.description,
                                employeeid = model._appointment.employeeid,
                                endtime = model._appointment.endtime,
                                repeating = model._appointment.repeating,
                                starttime = model._appointment.starttime,
                                subject = model._appointment.subject
                            };
                            appointment.appointmentid = Guid.NewGuid();
                            model._appointment.appointmentid = appointment.appointmentid;
                            db.appointments.AddObject(appointment);
                            db.SaveChanges();

                            if (attendeesID.Count > 0)
                            {
                                List<Attendee> Attendee = new List<Models.Attendee>();
                                foreach (String id in attendeesID)
                                {
                                    Attendee attendee = new Attendee()
                                    {
                                        id = Guid.NewGuid(),
                                        appointmentid = appointment.appointmentid,
                                        attendee1 = id
                                    };

                                    student stud = db.students.SingleOrDefault(s => s.studentid == id);

                                    if (stud != null)
                                    {
                                        attendee.confirmed = true;
                                    }

                                    Attendee check = db.Attendees.SingleOrDefault(a => a.attendee1 == attendee.attendee1 && a.appointmentid == attendee.appointmentid);
                                    if (check == null)
                                    {
                                        if (!attendee.attendee1.Equals(model._appointment.employeeid))
                                            Attendee.Add(attendee);
                                    }
                                }
                                foreach (Attendee attend in Attendee)
                                {
                                    Attendee check = db.Attendees.SingleOrDefault(a => a.attendee1 == attend.attendee1 && a.appointmentid == attend.appointmentid);
                                    if (check == null)
                                    db.Attendees.AddObject(attend);
                                    db.SaveChanges();
                                }
                            }
                            model._appointment.starttime = model._appointment.starttime.AddDays(14);
                            model._appointment.endtime = model._appointment.endtime.AddDays(14);
                        }

                    }
                    else if (model.repeating.Trim().Equals("Monthly"))
                    {
                        DateTime endRepeatingDate = model._appointment.endtime;
                        model._appointment.endtime = model._appointment.starttime;
                        model._appointment.endtime = model._appointment.endtime.Add(-time1);
                        model._appointment.endtime = model._appointment.endtime.Add(time2);
                        model._appointment.repeating = Guid.NewGuid();

                        while (model._appointment.starttime.Date <= endRepeatingDate.Date)
                        {
                            appointment appointment = new appointment()
                            {
                                allday = model._appointment.allday,
                                appointmenttype = model._appointment.appointmenttype,
                                cname = model._appointment.cname,
                                description = model._appointment.description,
                                employeeid = model._appointment.employeeid,
                                endtime = model._appointment.endtime,
                                repeating = model._appointment.repeating,
                                starttime = model._appointment.starttime,
                                subject = model._appointment.subject
                            };
                            appointment.appointmentid = Guid.NewGuid();
                            model._appointment.appointmentid = appointment.appointmentid;
                            db.appointments.AddObject(appointment);
                            db.SaveChanges();

                            if (attendeesID.Count > 0)
                            {
                                List<Attendee> Attendee = new List<Models.Attendee>();
                                foreach (String id in attendeesID)
                                {
                                    Attendee attendee = new Attendee()
                                    {
                                        id = Guid.NewGuid(),
                                        appointmentid = appointment.appointmentid,
                                        attendee1 = id
                                    };

                                    student stud = db.students.SingleOrDefault(s => s.studentid == id);

                                    if (stud != null)
                                    {
                                        attendee.confirmed = true;
                                    }

                                    Attendee check = db.Attendees.SingleOrDefault(a => a.attendee1 == attendee.attendee1 && a.appointmentid == attendee.appointmentid);
                                    if (check == null)
                                    {
                                        if (!attendee.attendee1.Equals(model._appointment.employeeid))
                                            Attendee.Add(attendee);
                                    }
                                }
                                foreach (Attendee attend in Attendee)
                                {
                                    Attendee check = db.Attendees.SingleOrDefault(a => a.attendee1 == attend.attendee1 && a.appointmentid == attend.appointmentid);
                                    if (check == null)
                                    db.Attendees.AddObject(attend);
                                    db.SaveChanges();
                                }
                            }
                            model._appointment.starttime = model._appointment.starttime.AddMonths(1);
                            model._appointment.endtime = model._appointment.endtime.AddMonths(1);
                        }

                    }
                    else if (model.repeating.Trim().Equals("Yearly"))
                    {
                        DateTime endRepeatingDate = model._appointment.endtime;
                        model._appointment.endtime = model._appointment.starttime;
                        model._appointment.endtime = model._appointment.endtime.Add(-time1);
                        model._appointment.endtime = model._appointment.endtime.Add(time2);
                        model._appointment.repeating = Guid.NewGuid();

                        while (model._appointment.starttime.Date <= endRepeatingDate.Date)
                        {
                            appointment appointment = new appointment()
                            {
                                allday = model._appointment.allday,
                                appointmenttype = model._appointment.appointmenttype,
                                cname = model._appointment.cname,
                                description = model._appointment.description,
                                employeeid = model._appointment.employeeid,
                                endtime = model._appointment.endtime,
                                repeating = model._appointment.repeating,
                                starttime = model._appointment.starttime,
                                subject = model._appointment.subject
                            };
                            appointment.appointmentid = Guid.NewGuid();
                            model._appointment.appointmentid = appointment.appointmentid;
                            db.appointments.AddObject(appointment);
                            db.SaveChanges();

                            if (attendeesID.Count > 0)
                            {
                                List<Attendee> Attendee = new List<Models.Attendee>();
                                foreach (String id in attendeesID)
                                {
                                    Attendee attendee = new Attendee()
                                    {
                                        id = Guid.NewGuid(),
                                        appointmentid = appointment.appointmentid,
                                        attendee1 = id
                                    };

                                    student stud = db.students.SingleOrDefault(s => s.studentid == id);

                                    if (stud != null)
                                    {
                                        attendee.confirmed = true;
                                    }
                                    Attendee check = db.Attendees.SingleOrDefault(a => a.attendee1 == attendee.attendee1 && a.appointmentid == attendee.appointmentid);
                                    if (check == null)
                                    {
                                        if (!attendee.attendee1.Equals(model._appointment.employeeid))
                                            Attendee.Add(attendee);
                                    }
                                }
                                foreach (Attendee attend in Attendee)
                                {
                                    Attendee check = db.Attendees.SingleOrDefault(a => a.attendee1 == attend.attendee1 && a.appointmentid == attend.appointmentid);
                                    if (check == null)
                                    db.Attendees.AddObject(attend);
                                    db.SaveChanges();
                                }
                            }
                            model._appointment.starttime = model._appointment.starttime.AddYears(1);
                            model._appointment.endtime = model._appointment.endtime.AddYears(1);
                        }

                    }

                    employee creator = db.employees.Single(emp => emp.employeeid == User.Identity.Name);
                    if (model.repeating.Trim().Equals("Not Repeating"))
                    {
                        ConfirmationEmail(creator, model._appointment);
                    }
                    else
                    {
                        ConfirmationEmailSeries(creator, model._appointment);
                    }

                    if (model.emailAll)
                    {
                        if (model.repeating.Trim().Equals("Not Repeating"))
                        {
                            EmailAttendees(creator, model._appointment);
                        }
                        else
                        {
                            EmailAttendeesSeries(creator, model._appointment);
                        }
                    }
                    return RedirectToAction("Details/" + model._appointment.appointmentid, "Calendar");
                }
                return View(model);
            }
            catch (Exception ex)
            {
                return View(model);
            }
        }
        /// <summary>
        /// populates the reate page
        /// </summary>
        /// <param name="id"></param>
        /// <param name="subject"></param>
        /// <returns></returns>
        public ActionResult Create(String id, String subject)
        {
            appointment appointment = new appointment() { starttime = DateTime.Now, endtime = DateTime.Now.AddHours(.5) };
            employee employee = db.employees.Single(emp => emp.employeeid == User.Identity.Name);
            appointment.employeeid = employee.fname + " " + employee.lname + " (" + employee.employeeid + ")";
            if (subject != null)
                appointment.subject = subject;
            IEnumerable<campu> campus = db.campus;
            List<String> list = new List<String>();
            foreach (campu camp in campus)
            {
                list.Add(camp.cname);
            }
            IEnumerable<student> students = db.students;
            List<AutoCompletePOCO> AutoCompleteID = new List<AutoCompletePOCO>();
            foreach (student stud in students)
            {
                AutoCompletePOCO poco = new AutoCompletePOCO()
                {
                    value = stud.fname + " " + stud.lname + " (" + stud.studentid + ")",
                    Label = stud.fname + " " + stud.lname + " (" + stud.studentid + ")",
                    Email = stud.email,
                    Role = "Student"
                };
                AutoCompleteID.Add(poco);
            }
            IEnumerable<employee> employees = db.employees;
            List<AutoCompletePOCO> EmployeeID = new List<AutoCompletePOCO>();
            foreach (employee emp in employees)
            {
                AutoCompletePOCO poco = new AutoCompletePOCO()
                {
                    value = emp.fname + " " + emp.lname + " (" + emp.employeeid + ")",
                    Label = emp.fname + " " + emp.lname + " (" + emp.employeeid + ")",
                    Email = emp.email,
                    Role = emp.role
                };
                AutoCompleteID.Add(poco);
                EmployeeID.Add(poco);
            }
            String[] AppointmentType = new String[3] { "Advisement", "Personal", "Office" };

            CreateAppointmentRequestModel model = new CreateAppointmentRequestModel() { _appointment = appointment, _campus = list, AttendeesAutoComplete = AutoCompleteID, EmployeeID = EmployeeID, startTime = DateTime.Now.ToShortTimeString(), endTime = DateTime.Now.AddHours(.5).ToShortTimeString(), appoingmentType = AppointmentType, emailAll = true, repeatingType = new String[] { "Not Repeating", "Daily (Business Days)", "Weekly", "Bi-Weekly", "Monthly", "Yearly" } };
            if (id != null)
            {
                student student = db.students.Single(s => s.studentid == id);
                model.Attendees = student.fname + " " + student.lname + " (" + student.studentid + "), ";
            }
            return View(model);
        }