public ActionResult Create(AllocateRoom allocateroom)
        {
            // Validation Check
            string message = ValidationForAllocateRoom(allocateroom);

            if (message != "")
            {
                ViewBag.CourseId = new SelectList(db.Courses, "CourseId", "CourseCode", allocateroom.CourseId);
                ViewBag.RoomId = new SelectList(db.Rooms, "RoomId", "RoomNo", allocateroom.RoomId);
                ViewBag.DayId = new SelectList(db.Days, "DayId", "DayName", allocateroom.DayId);

                ViewBag.ErrorMessage = "Validation Error: " + message;

                return View(allocateroom);
            }

            // conflict

            message = GetAllRoomAllocationConflict(allocateroom);

            if (message != "")
            {
                ViewBag.CourseId = new SelectList(db.Courses, "CourseId", "CourseCode", allocateroom.CourseId);
                ViewBag.RoomId = new SelectList(db.Rooms, "RoomId", "RoomNo", allocateroom.RoomId);
                ViewBag.DayId = new SelectList(db.Days, "DayId", "DayName", allocateroom.DayId);

                ViewBag.OverlappingMessage = message;

                return View(allocateroom);
            }

            // get the department
            Course aCourse =
               (from cor in db.Courses where cor.CourseId == allocateroom.CourseId select cor).Single();

            Department aDepartment =
                (from dep in db.Departments where dep.DepartmentId == aCourse.DepartmentId select dep).Single();

            // now assign the department
            allocateroom.Department = aDepartment;

            if (ModelState.IsValid)
            {
                db.AllocateRooms.Add(allocateroom);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.CourseId = new SelectList(db.Courses, "CourseId", "CourseCode", allocateroom.CourseId);
            ViewBag.RoomId = new SelectList(db.Rooms, "RoomId", "RoomNo", allocateroom.RoomId);
            ViewBag.DayId = new SelectList(db.Days, "DayId", "DayName", allocateroom.DayId);

            return View(allocateroom);
        }
        private string ValidationForAllocateRoom(AllocateRoom allocateroom)
        {
            try
            {
                string message = "";

                if (allocateroom.CourseId < 1 || allocateroom.CourseId == null || allocateroom.DayId < 1 ||
                    allocateroom.DayId == null || allocateroom.RoomId < 1 || allocateroom.RoomId == null)
                {
                    message += "All field must be filled up";
                    return message;
                }

                if (!ValidationOfTimeString(allocateroom.StartingTime))
                {
                    message += "Invalid Starting time";
                    return message;
                }
                if (!ValidationOfTimeString(allocateroom.EndingTime))
                {
                    message += "Invalid Ending Time";
                    return message;
                }

                if (ConvertStringToTime(allocateroom.StartingTime) >= ConvertStringToTime(allocateroom.EndingTime))
                {
                    message += "Starting Time is not smaller than ending time";
                }

                return message;
            }
            catch (Exception)
            {
                return " ";
            }
        }
        private bool IsOverlapped(AllocateRoom newAllocatation, AllocateRoom oldAllocation)
        {
            int s1 = ConvertStringToTime(newAllocatation.StartingTime);
            int e1 = ConvertStringToTime(newAllocatation.EndingTime);

            int s2 = ConvertStringToTime(oldAllocation.StartingTime);
            int e2 = ConvertStringToTime(oldAllocation.EndingTime);

            if (s1 > s2)
            {
                int t1; int t2;
                t1 = s1; t2 = e1;
                s1 = s2; e1 = e2;
                s2 = t1; e2 = t2;
            }

            /* s1 ---------------e1 */
            /* ------ s2-----------------e2 */

            if (s1 <= s2 && s2 <= e1)
            {
                return true; // overlapped found
            }

            return false;
        }
        //private string GetTeacherConflict(AllocateRoom allocateroom)
        //{
        //    string message = "";
        //    Course aCourse = db.Courses.Find(allocateroom.CourseId);
        //    if (aCourse.Teacher != null)
        //    {
        //        List<Course> courses =
        //            (from c in db.Courses where c.Teacher.TeacherId == aCourse.Teacher.TeacherId select c).ToList();
        //        // get the enrollment list
        //        List<AllocateRoom> allocations = (
        //            from course in courses
        //            from a in db.AllocateRooms
        //            where a.CourseId == course.CourseId && a.DayId == allocateroom.DayId
        //            select a).ToList();
        //        message = allocations.Where(allocate => IsOverlapped(allocateroom, allocate)).Aggregate(message, (current, allocate) => current + GetOverlappingDetails(allocate));
        //    }
        //    return message;
        //}
        //private string GetCourseConflict(AllocateRoom allocateroom)
        //{
        //    string message = "";
        //    List<Course> courses =
        //            (from c in db.Courses where c.CourseId == allocateroom.CourseId select c).ToList();
        //    // get the enrollment list
        //    List<AllocateRoom> allocations = (
        //        from course in courses
        //        from a in db.AllocateRooms
        //        where a.CourseId == course.CourseId && a.DayId == allocateroom.DayId
        //        select a).ToList();
        //    message = allocations.Where(allocate => IsOverlapped(allocateroom, allocate)).Aggregate(message, (current, allocate) => current + GetOverlappingDetails(allocate));
        //    return message;
        //}
        private string GetTimeConflict(AllocateRoom allocateroom)
        {
            string message = "";

            List<AllocateRoom> allocations =
                (from a in db.AllocateRooms
                 where (a.DayId == allocateroom.DayId && a.RoomId == allocateroom.RoomId)
                 select a).ToList();

            foreach (AllocateRoom allocate in allocations)
            {
                if (IsOverlapped(allocateroom, allocate))
                {
                    message += GetOverlappingDetails(allocate);
                }
            }

            return message;
        }
        private string GetOverlappingDetails(AllocateRoom oldAllocation)
        {
            string message = "";
            Course course = (from c in db.Courses where c.CourseId == oldAllocation.CourseId select c).SingleOrDefault();

            if (course != null)
            {
                if( course.Teacher != null)
                    message += "Teacher: " +  course.Teacher.TeacherName + " ";
            }
            message += "Course: " + oldAllocation.Course.CourseName+ " ";
            message += "Staring Time: " + oldAllocation.StartingTime + " ";
            message += "Ending Time: " + oldAllocation.EndingTime + " ;";

            return message;
        }
        private string GetAllRoomAllocationConflict(AllocateRoom allocateroom)
        {
            string message = "";
            // time conflict
            string messageTimeConflict = GetTimeConflict(allocateroom);
            if (messageTimeConflict != "")
            {
                messageTimeConflict = "Room is busy. Details: " + messageTimeConflict;
            }

            // this course class is running in another room
            //string messageCourseConflict = GetCourseConflict(allocateroom);
            //if (messageCourseConflict != "")
            //{
            //   messageCourseConflict = " Course has class in other room. Details: " + messageCourseConflict;
            //}

            // This teacher is busy with other class
            //string messageTeacherConflict = GetTeacherConflict(allocateroom);
            //if (messageTeacherConflict != "")
            //{
            //    messageTeacherConflict = "This teacher is busy with other class. Details: " + messageTeacherConflict;
            //}
            message = messageTimeConflict;// +messageCourseConflict + messageTeacherConflict;

            return message;
        }