public ActionResult Edit(int id = 0)
        {
            ProgramViewModel ViewModel = new ProgramViewModel();
            Program program = db.Programs.Find(id);
            ViewModel.program = program;

            GetEligibleLists(program);
            ViewBag.StatusList = new SelectList(db.ProgramStatus, "id", "name", program.status_id);
            ViewBag.TypeList = new SelectList(db.ProgramTypes, "id", "name", program.type_id);
            ViewBag.ActionList = GetActionList(program);

            if (program == null)
            {
                Session["FlashMessage"] = "Program not found";
                return View(ViewModel);
            }

            ViewModel.attachments = new List<ProgramAttachment>(program.ProgramAttachments);
            for (int i = program.ProgramAttachments.Count(); i < ViewModel.no_of_attachments; i++)
            {
                ViewModel.attachments.Add(new ProgramAttachment());
            }

            ViewModel.options = new List<ProgramOptionValue>(program.ProgramOptionValues);
            for (int i = program.ProgramOptionValues.Count(); i < ViewModel.no_of_options; i++)
            {
                ViewModel.options.Add(new ProgramOptionValue());
            }

            ViewModel.app_attachments = new List<ProgramApplicationAttachment>(program.ProgramApplicationAttachments);
            for (int i = program.ProgramApplicationAttachments.Count(); i < ViewModel.no_of_app_attachments; i++)
            {
                ViewModel.app_attachments.Add(new ProgramApplicationAttachment());
            }

            return View(ViewModel);
        }
        public ActionResult Edit(ProgramViewModel ViewModel)
        {
            Program program = ViewModel.program;
            GetEligibleLists(program);
            ViewBag.StatusList = new SelectList(db.ProgramStatus, "id", "name", program.status_id);
            ViewBag.TypeList = new SelectList(db.ProgramTypes, "id", "name", program.type_id);
            ViewBag.ActionList = GetActionList(program);

            //check appointment required, session exist when published
            try
            {
                ProgramStatus PublishedStatus = db.ProgramStatus.Where(m => m.name == "Opened").FirstOrDefault();
                if (program.status_id == PublishedStatus.id)
                {
                    if (program.require_appointment)
                    {
                        if (!db.Appointments.Any(o => o.AppointmentConcerns.Any(c => c.program_id == program.id)))
                        {
                            ProgramStatus DraftedStatus = db.ProgramStatus.Where(m => m.name == "Drafted").FirstOrDefault();
                            program.status_id = DraftedStatus.id;
                            Session["FlashMessage"] = "Appointment timeslot(s) must be created before publishing. Program Status is saved as 'Drafted'.";
                        }
                    }
                    else
                    {
                        program.published = DateTime.Now;
                        program.published_by = User.Identity.Name;
                    }
                }
            }
            catch (Exception e)
            {
                Session["FlashMessage"] = "Opened Status not found." + e.Message;
                return View(ViewModel);
            }

            //check external link to add http
            if (program.apply_action == "link" && !program.apply_link.StartsWith("http", true, null))
            {
                program.apply_link = "http://" + program.apply_link;
            }

            program.eligible_academic_career = String.IsNullOrEmpty(Request["academic_career"]) ? "" : Request["academic_career"];
            program.eligible_academic_organization = String.IsNullOrEmpty(Request["academic_organization"]) ? "" : Request["academic_organization"];
            program.eligible_academic_plan = String.IsNullOrEmpty(Request["academic_plan"]) ? "" : Request["academic_plan"];
            program.eligible_program_status = String.IsNullOrEmpty(Request["program_status"]) ? "" : Request["program_status"];
            program.eligible_academic_level = String.IsNullOrEmpty(Request["academic_level"]) ? "" : Request["academic_level"];

            if (ModelState.IsValid)
            {
                program.modified = DateTime.Now;
                program.modified_by = User.Identity.Name;
                try
                {
                    db.Entry(program).State = EntityState.Modified;
                    db.SaveChanges();
                }
                catch (Exception e)
                {
                    Session["FlashMessage"] = "Failed to update program." + e.Message;
                }

                foreach (ProgramAttachment attachment in ViewModel.attachments)
                {
                    if (attachment.id != 0)
                    {
                        ProgramAttachment att = db.ProgramAttachments.Find(attachment.id);
                        if (String.IsNullOrEmpty(attachment.filename))
                        {
                            var path = Server.MapPath("~/App_Data/" + att.filepath);
                            var filepath = Path.Combine(path, att.filename);
                            if (System.IO.File.Exists(filepath))
                            {
                                try
                                {
                                    System.IO.File.Delete(filepath);
                                }
                                catch (Exception e)
                                {
                                    Session["FlashMessage"] = "Failed to delete attachment." + e.Message;
                                }
                            }

                            db.ProgramAttachments.Remove(att);
                        }
                        else
                        {
                            if (att.filename != attachment.filename || att.filepath != attachment.filepath)
                            {
                                var path = Server.MapPath("~/App_Data/" + att.filepath);
                                var filepath = Path.Combine(path, att.filename);
                                if (System.IO.File.Exists(filepath))
                                {
                                    try
                                    {
                                        System.IO.File.Delete(filepath);
                                    }
                                    catch (Exception e)
                                    {
                                        Session["FlashMessage"] = "Failed to delete attachment." + e.Message;
                                    }
                                }
                                var sourcePath = Server.MapPath("~/App_Data/" + attachment.filepath);
                                var sourceFilepath = Path.Combine(sourcePath, attachment.filename);
                                var destPath = Server.MapPath("~/App_Data/" + "Attachments/Program/" + program.id);
                                var destFilepath = Path.Combine(destPath, attachment.filename);
                                try
                                {
                                    Directory.CreateDirectory(destPath);
                                }
                                catch (Exception e)
                                {
                                    Session["FlashMessage"] = "Failed to create directory." + e.Message;
                                }
                                try
                                {
                                    System.IO.File.Move(sourceFilepath, destFilepath);
                                }
                                catch (Exception e)
                                {
                                    Session["FlashMessage"] = "Failed to move file." + e.Message;
                                }

                                attachment.program_id = program.id;
                                attachment.filepath = "Attachments/Program/" + program.id;
                            }
                            try
                            {
                                db.Entry(att).CurrentValues.SetValues(attachment);
                            }
                            catch (Exception e)
                            {
                                Session["FlashMessage"] = "Failed to update attachment object values." + e.Message;
                            }
                        }
                    }
                    else if (!String.IsNullOrEmpty(attachment.filename))
                    {
                        var sourcePath = Server.MapPath("~/App_Data/" + attachment.filepath);
                        var sourceFilepath = Path.Combine(sourcePath, attachment.filename);
                        var destPath = Server.MapPath("~/App_Data/" + "Attachments/Program/" + program.id);
                        var destFilepath = Path.Combine(destPath, attachment.filename);
                        try
                        {
                            Directory.CreateDirectory(destPath);
                        }
                        catch (Exception e)
                        {
                            Session["FlashMessage"] = "Failed to create directory." + e.Message;
                        }
                        try
                        {
                            System.IO.File.Move(sourceFilepath, destFilepath);
                        }
                        catch (Exception e)
                        {
                            Session["FlashMessage"] = "Failed to move file." + e.Message;
                        }

                        attachment.program_id = program.id;
                        attachment.filepath = "Attachments/Program/" + program.id;
                        db.ProgramAttachments.Add(attachment);
                    }
                }

                //clear temp files uploaded but not used
                if (Directory.Exists(Server.MapPath("~/App_Data/Temp/Program/" + program.id)))
                {
                    var files = Directory.GetFiles(Server.MapPath("~/App_Data/Temp/Program/" + program.id));
                    foreach (var file in files)
                    {
                        System.IO.File.Delete(file);
                    }
                }

                foreach (ProgramOptionValue option in ViewModel.options)
                {
                    if (option.id != 0)
                    {
                        if (!String.IsNullOrEmpty(option.name))
                        {
                            db.Entry(option).State = EntityState.Modified;
                        }
                        else
                        {
                            ProgramOptionValue opt = db.ProgramOptionValues.Find(option.id);
                            db.ProgramOptionValues.Remove(opt);
                        }
                    }
                    else if (!String.IsNullOrEmpty(option.name))
                    {
                        option.program_id = program.id;
                        db.ProgramOptionValues.Add(option);
                    }
                }

                foreach (ProgramApplicationAttachment app_attachment in ViewModel.app_attachments)
                {
                    if (app_attachment.id != 0)
                    {
                        if (!String.IsNullOrEmpty(app_attachment.name))
                        {
                            db.Entry(app_attachment).State = EntityState.Modified;
                        }
                        else
                        {
                            ProgramApplicationAttachment att = db.ProgramApplicationAttachments.Find(app_attachment.id);
                            db.ProgramApplicationAttachments.Remove(att);
                        }
                    }
                    else if (!String.IsNullOrEmpty(app_attachment.name))
                    {
                        app_attachment.program_id = program.id;
                        db.ProgramApplicationAttachments.Add(app_attachment);
                    }
                }
                try
                {
                    db.SaveChanges();
                }
                catch (Exception e)
                {
                    Session["FlashMessage"] = "Failed to add/edit options/attachments to program. <br/><br/>" + e.Message;
                }
                return RedirectToAction("Index");
            }

            return View(program);
        }
        public ActionResult Create()
        {
            ProgramViewModel ViewModel = new ProgramViewModel();
            Program program = new Program();

            //default values for create
            program.application_start_time = DateTime.Now.Date;
            program.application_end_time = DateTime.Now.Date.AddDays(7).AddHours(23).AddMinutes(59);
            program.eligible_academic_career = "UGRD";
            program.eligible_academic_organization = "CHEM,LIFS,MATH,PHYS,SSCI";
            program.eligible_program_status = "AC,LA";
            GetEligibleLists(program);
            int draftedStatusId = 0;
            try
            {
                draftedStatusId = db.ProgramStatus.Where(m => m.name == "Drafted").FirstOrDefault().id;
            }
            catch (Exception e)
            {
                Session["FlashMessage"] = "Drafted Status not found." + e.Message;
            }
            ViewBag.StatusList = new SelectList(db.ProgramStatus, "id", "name", draftedStatusId);
            ViewBag.TypeList = new SelectList(db.ProgramTypes, "id", "name");
            ViewBag.ActionList = GetActionList(program);

            ViewModel.attachments = new List<ProgramAttachment>();
            for (int i = 0; i < ViewModel.no_of_attachments; i++)
            {
                ViewModel.attachments.Add(new ProgramAttachment());
            }

            ViewModel.program = program;
            return View(ViewModel);
        }