public ActionResult Create(AEvent tourEvent, string TourNameOptions)
 {
     try
     {
         if (ModelState.IsValid)
         {
             BTourGuideOp tourOp = new BTourGuideOp();
             List<ATour> tours = tourOp.GetTours();
             ATour tour = tours.Single(x => x.TourName == TourNameOptions);
             tourEvent.TourID = tour.TourID;
             tourEvent.TourName = TourNameOptions;
             tourOp.AddEvent(tourEvent);
             return RedirectToAction("Index");
         }
         else
         {
             ViewBag.TourNameOptions = Lists.CreateTourList();
             return View(tourEvent);
         }
     }
     catch(Exception e)
     {
         TempData["CreateException"] = "Error in event creation: " + e.Message;
         return View();
     }
 }
 public ActionResult AddEvent(string id, EventDetails eventDetails)
 {
     try
     {
         if (ModelState.IsValid)
         {
             BTourGuideOp tourOp = new BTourGuideOp();
             AEvent tourEvent = new AEvent();
             tourEvent.TourID = eventDetails.tourInfo.TourID;
             tourEvent.TourName = eventDetails.tourInfo.TourName;
             tourEvent.TourDate = eventDetails.eventInfo.TourDate;
             tourEvent.TourGuide = eventDetails.eventInfo.TourGuide;
             tourEvent.IsOn = eventDetails.eventInfo.IsOn;
             tourEvent.TourOriginalDate = eventDetails.eventInfo.TourOriginalDate;
             tourOp.AddEvent(tourEvent);
             return RedirectToAction("Index");
         }
         else
         {
             return View(eventDetails);
         }
     }
     catch (Exception e)
     {
         TempData["AddEventException"] = "Error in adding event: " + e.Message;
         return View(eventDetails);
     }
 }
 public ActionResult Delete(string tourid, AEvent tourEvent)
 {
     try
     {
         BTourGuideOp tourOp = new BTourGuideOp();
         tourOp.DeleteEvent(tourid, tourEvent.TourDate);
         return RedirectToAction("Index");
     }
     catch(Exception e)
     {
         TempData["DeleteException"] = "Error in event deletion: " + e.Message;
         return View();
     }
 }
 public bool AddEvent(AEvent tourEvent)
 {
     using (DataClassesTourGuideDataContext dc = new DataClassesTourGuideDataContext())
     {
         Event newEvent = new Event();
         newEvent.TourID = System.Guid.Parse(tourEvent.TourID);
         newEvent.TourDate = tourEvent.TourDate;
         newEvent.TourGuide = tourEvent.TourGuide;
         newEvent.IsOn = (byte)(tourEvent.IsOn? 1 : 0);
         dc.Events.InsertOnSubmit(newEvent);
         dc.SubmitChanges();
         return true;
     }
 }
 public ActionResult AddEvent(string id)
 {
     BTourGuideOp tourOp = new BTourGuideOp();
     ATour tour = tourOp.GetTourByID(id);
     AEvent tourEvent = new AEvent();
     tourEvent.TourName = tour.TourName;
     tourEvent.TourDate = DateTime.Now;
     tourEvent.TourOriginalDate = DateTime.Now;
     tourEvent.TourGuide = "";
     tourEvent.TourID = tour.TourID;
     tourEvent.IsOn = false;
     EventDetails eventDetails = new EventDetails();
     eventDetails.tourInfo = tour;
     eventDetails.eventInfo = tourEvent;
     return View(eventDetails);
 }
 public ActionResult Edit(string tourid, AEvent tourEvent)
 {
     try
     {
         if (ModelState.IsValid)
         {
             BTourGuideOp tourOp = new BTourGuideOp();
             tourEvent.TourID = tourid;
             tourEvent.TourOriginalDate = tourEvent.TourOriginalDate;
             tourOp.EditEvent(tourEvent);
             return RedirectToAction("Index", new { edited="Yes" });
         }
         else
             return View(tourEvent);
     }
     catch(Exception e)
     {
         TempData["EditException"] = "Error in event edit: " + e.Message;
         return View(tourEvent);
     }
 }
 public bool EditEvent(AEvent tourEvent)
 {
     using (DataClassesTourGuideDataContext dc = new DataClassesTourGuideDataContext())
     {
     if (tourEvent.TourDate == tourEvent.TourOriginalDate) // The TourDate is not changed - save the other changes
     {
             Event row = (from c in dc.Events
                          where (c.TourID.ToString() == tourEvent.TourID && c.TourDate == tourEvent.TourDate)
                          select c).FirstOrDefault<Event>();
             row.TourGuide = tourEvent.TourGuide;
         row.IsOn = (byte)(tourEvent.IsOn?1:0);
             dc.SubmitChanges();
             return true;
     }
     else // The TourDate has been changed - delete the event and create a new one with the new date. Copy the registrations with the new date
     {
             AddEvent(tourEvent); // Adding the new event (the same event with the new date)
             // Adding new registrations to the new event (with the new date)
             List<Registration> registrations = (from r in dc.Registrations
                                                 where (r.TourID.ToString() == tourEvent.TourID && r.TourDate == tourEvent.TourOriginalDate)
                                                 select r).ToList();
             foreach (Registration reg in registrations)
             {
                 AReg newReg = new AReg();
                 newReg.IsSentEmail = reg.IsSentEmail==1?true:false;
                 newReg.IsPaid = reg.IsPaid==1?true:false;
                 newReg.Attended = reg.Attended==1?true:false;
                 newReg.RegFirstName = reg.RegFirstName;
                 newReg.RegLastName = reg.RegLastName;
                 newReg.RegBirthday = reg.RegBirthday;
                 newReg.TourDate = tourEvent.TourDate;
                 newReg.TourID = tourEvent.TourID;
                 newReg.UserID = reg.UserID.ToString();
                 newReg.WillAttend = reg.WillAttend==1?true:false;
                 AddReg(newReg);
             }
             DeleteEvent(tourEvent.TourID, tourEvent.TourOriginalDate); // this function also deletes the previous date regisrations
             dc.SubmitChanges();
             return true;
       }
     }
 }