public ActionResult Record(int id = 0)
        {
            IQueryable<Application> applications;
            if (id == 1)
            {
                applications = db.Applications.Where(a => a.created_by == User.Identity.Name && a.Program.require_interview && a.Interviews.Where(i => i.InterviewStatus.display_to_student).Count() > 0);
                ViewBag.title = "My Interviews";
                ViewBag.norecord = "interview";
            }
            else if (id == 2)
            {
                applications = db.Applications.Where(a => a.student_id == User.Identity.Name && a.Program.require_appointment && a.Program.AppointmentConcerns.Any(c => c.Appointments.Any(p => p.student_id == User.Identity.Name)));
                ViewBag.title = "My Consultations";
                ViewBag.norecord = "consultation";
            }
            else
            {
                applications = db.Applications.Where(a => a.created_by == User.Identity.Name);
                ViewBag.title = "My Applications";
                ViewBag.norecord = "application";
            }
            List<ProgramAction> programactions = new List<ProgramAction>();
            foreach (var application in applications)
            {
                ProgramAction programaction = new ProgramAction();
                programaction.application = application;
                programaction.program = application.Program;
                programaction.student = application.StudentProfile;

                //check student eligibility
                programaction.eligible = true;
                //check program application period
                programaction.inperiod = (application.Program.application_start_time <= DateTime.Now && DateTime.Now <= application.Program.application_end_time);
                //check if before program start time
                programaction.beforestart = (DateTime.Now < application.Program.application_start_time);
                //check existing application
                programaction.existed = true;
                //check application status
                programaction.saved = programaction.application.ApplicationStatus.name == "Saved";
                //check program status
                programaction.open = application.Program.ProgramStatus.name == "Opened";
                programactions.Add(programaction);
            }

            return View(programactions.ToList());
        }
        public ActionResult Showall(int id = 0)
        {
            ViewBag.typeid = id;
            ViewBag.eligible = true;
            ViewBag.programList = new SelectList(db.Programs.Where(p => p.ProgramStatus.shown_to_student).OrderBy(p => p.name), "id", "name");
            ViewBag.programTypeList = new SelectList(db.ProgramTypes, "id", "name", id);
            //ViewBag.programStatusList = new SelectList(db.ProgramStatus.Where(s => s.shown_to_student), "id", "name");

            StudentProfile student = db.StudentProfiles.Find(User.Identity.Name);
            if (student != null)
            {
                DateTime date_to_shown = new DateTime(DateTime.Now.Year - 2, 9, 1, 0, 0, 0);
                var programs = db.Programs.Include(p => p.ProgramStatus).Include(p => p.ProgramType).Where(p => (p.ProgramStatus.shown_to_student)
                    && ((id == 0) || (p.ProgramType.id == id))
                    && (p.application_end_time >= date_to_shown)
                );
                var programType = db.ProgramTypes.Find(id);
                if (programType != null)
                {
                    ViewBag.title = programType.name + " Programs";
                }
                else
                {
                    ViewBag.title = "All Programs";
                }
                List<ProgramAction> programactions = new List<ProgramAction>();
                foreach (var program in programs)
                {
                    ProgramAction programaction = new ProgramAction();
                    programaction.program = program;

                    //check student eligibility
                    programaction.eligible = IsEligible(program, student);
                    //check program application period
                    programaction.inperiod = (program.application_start_time <= DateTime.Now && DateTime.Now <= program.application_end_time);
                    //check if before program start time
                    programaction.beforestart = (DateTime.Now < program.application_start_time);
                    //check existing application
                    programaction.existed = student.Applications.Any(a => a.program_id == program.id);
                    if (programaction.existed)
                    {
                        //check existing application id
                        programaction.application = student.Applications.Where(a => a.program_id == program.id).SingleOrDefault();
                        //check application status
                        programaction.saved = programaction.application.ApplicationStatus.name == "Saved";
                    }
                    else
                    {
                        programaction.saved = false;
                    }
                    //check program status
                    programaction.open = program.ProgramStatus.name == "Opened";
                    programactions.Add(programaction);
                }
                return View(programactions.ToList().Where(p => p.eligible && p.inperiod && p.open));
            }
            else
            {
                List<ProgramAction> programactions = new List<ProgramAction>();
                return View(programactions.ToList());
            }
        }
        public ActionResult Details(int id = 0)
        {
            //get program
            Program program = db.Programs.Find(id);
            if (program == null)
            {
                Session["FlashMessage"] = "Program not found.";
                if (User.IsInRole("Advising"))
                {
                    return RedirectToAction("Index");
                }
                else
                {
                    return RedirectToAction("Record");
                }
            }

            var student = db.StudentProfiles.Find(User.Identity.Name);

            //initialize Model
            ProgramAction programaction = new ProgramAction();
            programaction.program = program;

            if (student != null)
            {
                //check student eligibility
                programaction.eligible = IsEligible(program, student);
                //check program application period
                programaction.inperiod = (program.application_start_time <= DateTime.Now && DateTime.Now <= program.application_end_time);
                //check if before program start time
                programaction.beforestart = (DateTime.Now < program.application_start_time);
                //check existing application
                programaction.existed = student.Applications.Any(a => a.program_id == program.id);
                if (programaction.existed)
                {
                    //check existing application id
                    programaction.application = student.Applications.Where(a => a.program_id == program.id).SingleOrDefault();
                    //check application status
                    programaction.saved = programaction.application.ApplicationStatus.name == "Saved";
                }
                else
                {
                    programaction.saved = false;
                }
                //check program status
                programaction.open = program.ProgramStatus.name == "Opened";
            }
            else
            {
                programaction.eligible = false;
                programaction.inperiod = (program.application_start_time <= DateTime.Now && DateTime.Now <= program.application_end_time);
                programaction.beforestart = (DateTime.Now < program.application_start_time);
                programaction.existed = false;
                programaction.saved = false;
                programaction.open = program.ProgramStatus.name == "Opened";

            }
            return PartialView(programaction);
        }
        public ActionResult Showall(FormCollection Form, bool eligible, bool saved, bool submitted, int id = 0)
        {
            ViewBag.typeid = id;
            StudentProfile student = db.StudentProfiles.Find(User.Identity.Name);
            if (student != null)
            {
                DateTime date_to_shown = new DateTime(DateTime.Now.Year - 2, 9, 1, 0, 0, 0);
                var programs = db.Programs.Include(p => p.ProgramStatus).Include(p => p.ProgramType).Where(p => (p.ProgramStatus.shown_to_student)
                    && ((id == 0) || (p.ProgramType.id == id))
                    && (p.application_end_time >= date_to_shown)
                );
                var programType = db.ProgramTypes.Find(id);
                if (programType != null)
                {
                    ViewBag.title = programType.name + " Programs";
                }
                else
                {
                    ViewBag.title = "All Programs";
                }
                List<ProgramAction> programactions = new List<ProgramAction>();
                foreach (var program in programs)
                {
                    ProgramAction programaction = new ProgramAction();
                    programaction.program = program;

                    //check student eligibility
                    programaction.eligible = IsEligible(program, student);
                    //check program application period
                    programaction.inperiod = (program.application_start_time <= DateTime.Now && DateTime.Now <= program.application_end_time);
                    //check if before program start time
                    programaction.beforestart = (DateTime.Now < program.application_start_time);
                    //check existing application
                    programaction.existed = student.Applications.Any(a => a.program_id == program.id);
                    if (programaction.existed)
                    {
                        //check existing application id
                        programaction.application = student.Applications.Where(a => a.program_id == program.id).SingleOrDefault();
                        //check application status
                        programaction.saved = programaction.application.ApplicationStatus.name == "Saved";
                    }
                    else
                    {
                        programaction.saved = false;
                    }
                    //check program status
                    programaction.open = program.ProgramStatus.name == "Opened";
                    programactions.Add(programaction);
                }
                var selectedtype = Form["program_type"]!=null?Form["program_type"].Split(','):null;
                ViewBag.programList = new SelectList(db.Programs.Where(p => p.ProgramStatus.shown_to_student).OrderBy(p => p.name), "id", "name", Form["program"]);
                ViewBag.programTypeList = new MultiSelectList(db.ProgramTypes, "id", "name", selectedtype);
                //ViewBag.programStatusList = new SelectList(db.ProgramStatus.Where(s => s.shown_to_student), "id", "name", Form["program_status"]);

                return View(programactions.ToList().Where(p => (true)
                    && (String.IsNullOrEmpty(Form["program"]) || p.program.id.ToString() == Form["program"])
                    && (String.IsNullOrEmpty(Form["program_type"]) || selectedtype.Contains(p.program.type_id.ToString()))
                    && (String.IsNullOrEmpty(Form["program_status"]) || p.program.status_id.ToString() == Form["program_status"])
                    && ((!eligible && !saved && !submitted)
                    ||
                        (eligible && p.eligible && p.inperiod && p.open)
                        || (saved && (p.application != null && p.application.ApplicationStatus.name == "Saved"))
                        || (submitted && (p.application != null && p.application.ApplicationStatus.name != "Saved"))
                    )
                    ));
            }
            else
            {
                List<ProgramAction> programactions = new List<ProgramAction>();
                return View(programactions.ToList());
            }
        }
        public ActionResult MyProfile()
        {
            StudentProfileViewModel ViewModel = new StudentProfileViewModel();

            string studentid = User.Identity.Name;
            var studentprofile = db.StudentProfiles.Find(studentid);
            if (studentprofile == null)
            {
                Session["FlashMessage"] = "Student Profile not found";
                return RedirectToAction("Index", "Home");
            }
            var applications = db.Applications.ToList().Where(a => a.created_by == studentid.ToString());

            ViewModel.student = studentprofile;
            List<ProgramAction> programactions = new List<ProgramAction>();
            foreach (var application in applications)
            {
                ProgramAction programaction = new ProgramAction();
                programaction.application = application;
                programaction.program = application.Program;
                programaction.student = studentprofile;

                //check student eligibility
                programaction.eligible = true;
                //check program application period
                programaction.inperiod = (application.Program.application_start_time <= DateTime.Now && DateTime.Now <= application.Program.application_end_time);
                //check if before program start time
                programaction.beforestart = (DateTime.Now < application.Program.application_start_time);
                //check existing application
                programaction.existed = true;
                //check application status
                programaction.saved = programaction.application.ApplicationStatus.name == "Saved";
                //check program status
                programaction.open = application.Program.ProgramStatus.name == "Opened";
                programactions.Add(programaction);
            }
            ViewModel.programactions = programactions;
            return View(ViewModel);
        }
        public ActionResult Details(string id = null)
        {
            StudentProfile studentprofile = db.StudentProfiles.Find(id);
            if (studentprofile == null)
            {
                Session["FlashMessage"] = "Student Profile not found";
                return RedirectToAction("Index", "StudentProfile");
            }
            StudentProfileViewModel ViewModel = new StudentProfileViewModel();
            ViewModel.student = studentprofile;
            var applications = studentprofile.Applications;
            List<ProgramAction> programactions = new List<ProgramAction>();
            foreach (var application in applications)
            {
                ProgramAction programaction = new ProgramAction();
                programaction.application = application;
                programaction.program = application.Program;
                programaction.student = studentprofile;

                //check student eligibility
                programaction.eligible = true;
                //check program application period
                programaction.inperiod = (application.Program.application_start_time <= DateTime.Now && DateTime.Now <= application.Program.application_end_time);
                //check if before program start time
                programaction.beforestart = (DateTime.Now < application.Program.application_start_time);
                //check existing application
                programaction.existed = true;
                //check application status
                programaction.saved = programaction.application.ApplicationStatus.name == "Saved";
                //check program status
                programaction.open = application.Program.ProgramStatus.name == "Opened";
                programactions.Add(programaction);
            }
            ViewModel.programactions = programactions;
            ViewModel.particulartypes = db.StudentParticularTypes.ToList();
            ViewModel.experiencetypes = db.StudentExperienceTypes.ToList();
            ViewBag.profilepic = System.IO.File.Exists(Server.MapPath("~/Images/StudentProfile/" + studentprofile.id + ".jpg"));
            return PartialView(ViewModel);
        }