private void add_Click(object sender, EventArgs e)
        {
            bool infill;

            if (lesson.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                booking l = lesson.getBooking();
                if (!checkForClash(l, -1, out infill))
                {
                    // Add some clever scaling and positioning here!!!
                    l.rect.Width  = dx((int)((float)xPixelDiv * (l.endTime - l.startTime)) * 4);
                    l.rect.Height = dy(yPixelDiv);
                    l.rect.Top    = py((int)(yPixelDiv * ((float)l.row + 1f)));
                    l.rect.Left   = px((int)((l.startTime - 8.75f) * ((float)xPixelDiv * 4f)));
                    l.infill      = infill;
                    if (infill)
                    {
                        l.rect.Text = l.lesson + "\nMulti Group " + "\n" + l.staff + " [" + l.room + "]";
                    }
                    bookingList.Add(l);
                    lastItem = currentRoom = l.room;
                    displayLessons(lastItem);
                    updateCombo();
                }
            }
        }
        private void update(int index)
        {
            bool    infill;
            booking l = lesson.getBooking();

            if (!checkForClash(l, bookingList[index].id, out infill))
            {
                // Add some clever scaling and positioning here!!!
                bookingList[index].rect.Width     = dx((int)((float)xPixelDiv * (l.endTime - l.startTime)) * 4);
                bookingList[index].rect.Height    = dy(yPixelDiv);
                bookingList[index].rect.Top       = py((int)(yPixelDiv * ((float)l.row + 1f)));
                bookingList[index].rect.Left      = px((int)((l.startTime - 8.75f) * ((float)xPixelDiv * 4f)));
                bookingList[index].rect.FillColor = l.rect.FillColor;
                bookingList[index].rect.Text      = l.rect.Text;
                bookingList[index].lesson         = l.lesson;
                bookingList[index].group          = l.group;
                bookingList[index].level          = l.level;
                bookingList[index].room           = l.room;
                bookingList[index].row            = l.row;
                bookingList[index].startTime      = l.startTime;
                bookingList[index].endTime        = l.endTime;
                bookingList[index].year           = l.year;
                bookingList[index].staff          = l.staff;
                bookingList[index].day            = l.day;
                bookingList[index].infillRoom     = l.infillRoom;
                bookingList[index].infill         = infill;
                //Am i leaving a rect object in memory?
                lastItem = currentRoom = l.room;
                displayLessons(lastItem);
                updateCombo();
            }
        }
        private bool checkForClash(booking newBooking, int index, out bool infill)
        {
            bool clash = false;

            infill = false;
            int clashIndex = -1;

            clashIndex = groupClash(newBooking, index);
            if (clashIndex != -1)
            {
                MessageBox.Show("The group is already booked for a lesson at that time " + bookingList[clashIndex].lesson + " in " + bookingList[clashIndex].room, "Group Clash", MessageBoxButtons.OK);
                return(true);
            }

            clashIndex = roomClash(newBooking, index);
            if (clashIndex != -1)
            {
                if (bookingList[clashIndex].startTime == newBooking.startTime && bookingList[clashIndex].endTime == newBooking.endTime && bookingList[clashIndex].staff == newBooking.staff)
                {
                    if (
                        MessageBox.Show("Booking matches group " + bookingList[clashIndex].level
                                        + " " + bookingList[clashIndex].year + " " + bookingList[clashIndex].group
                                        + " with staff member " + bookingList[clashIndex].staff
                                        + "\nDo you want to merge with this group?  Select No to manually correct the problem.", "Clash", MessageBoxButtons.YesNo)
                        == DialogResult.Yes
                        )
                    {
                        bookingList[clashIndex].rect.Parent = null;
                        bookingList[clashIndex].rect.Text   = bookingList[clashIndex].lesson + "\nMulti Group " + "\n" + bookingList[clashIndex].staff + " [" + bookingList[clashIndex].room + "]";
                        bookingList[clashIndex].infill      = true;
                        infill = true;
                        return(false);
                    }
                    else
                    {
                        return(true);
                    }
                }
            }

            clashIndex = staffClash(newBooking, index);
            if (clashIndex != -1)
            {
                MessageBox.Show("Staff member " + bookingList[clashIndex].staff + " is already booked for a lesson at that time "
                                + bookingList[clashIndex].lesson + " in " + bookingList[clashIndex].room, "Group Clash", MessageBoxButtons.OK);
                return(true);
            }

            return(clash);
        }
        public void setBooking(booking b)
        {
            lesson.Text       = b.lesson;
            day.SelectedIndex = day.FindStringExact(b.day);

            startHour.Value     = (int)b.startTime;
            startMinute.Value   = (int)((b.startTime - (float)startHour.Value) * 60f);
            endHour.Value       = (int)b.endTime;
            endMinute.Value     = (int)((b.endTime - (float)endHour.Value) * 60f);
            year.SelectedIndex  = year.FindStringExact(b.year);
            level.SelectedIndex = level.FindStringExact(b.level);
            group.SelectedIndex = group.FindStringExact(b.group);
            room.SelectedIndex  = room.FindStringExact(b.room);
            infill.Checked      = b.infill;
            staff.SelectedIndex = staff.FindStringExact(b.staff);
        }
        private int staffClash(booking newBooking, int index)
        {
            int  foundIndex = 0;
            bool clash      = false;

            if (newBooking.staff == "NS")
            {
                return(-1);                           // NS = not staffed so don't check for a clash
            }
            foreach (booking b in bookingList)
            {
                if (index == -1 || index != b.id)
                {
                    if (b.staff == newBooking.staff)
                    {
                        if (b.day == newBooking.day)
                        {
                            if (b.startTime >= newBooking.startTime && b.startTime < newBooking.endTime)
                            {
                                clash = true; break;
                            }
                            if (b.endTime > newBooking.startTime && b.endTime <= newBooking.endTime)
                            {
                                clash = true; break;
                            }
                            if (b.startTime == newBooking.startTime && b.endTime == newBooking.endTime)
                            {
                                clash = true; break;
                            }
                        }
                    }
                }

                foundIndex++;
            }

            if (clash)
            {
                return(foundIndex);
            }
            else
            {
                return(-1);
            }
        }
        private int groupClash(booking newBooking, int index)
        {
            int  foundIndex = 0;
            bool clash      = false;

            foreach (booking b in bookingList)
            {
                if (index == -1 || index != b.id)
                {
                    if (b.day == newBooking.day)
                    {
                        if (b.level + b.year + b.group == newBooking.level + newBooking.year + newBooking.group)
                        {
                            if (b.startTime >= newBooking.startTime && b.startTime < newBooking.endTime)
                            {
                                clash = true; break;
                            }
                            if (b.endTime > newBooking.startTime && b.endTime <= newBooking.endTime)
                            {
                                clash = true; break;
                            }
                            if (b.startTime == newBooking.startTime && b.endTime == newBooking.endTime)
                            {
                                clash = true; break;
                            }
                        }
                    }
                }

                foundIndex++;
            }

            if (clash)
            {
                return(foundIndex);
            }
            else
            {
                return(-1);
            }
        }
        public void formclicked(object sender, EventArgs e)
        {
            if (booking.selected != -1)
            {
                int index = bookingList.FindIndex(
                    delegate(booking sb)
                {
                    return(sb.id == booking.selected);
                });

                booking b = bookingList[index];

                lesson.setBooking(bookingList[booking.selected]);
                lesson.enableDelete();

                DialogResult r = lesson.ShowDialog();

                if (r == DialogResult.No)
                {
                    int index2 = bookingList.FindIndex(
                        delegate(booking sb)
                    {
                        return(sb.startTime == b.startTime &&
                               sb.endTime == b.endTime &&
                               sb.row == b.row &&
                               sb.staff == b.staff &&
                               sb.room == sb.room &&
                               sb.id != b.id);
                    });

                    if (index2 == -1)
                    {
                        bookingList[index].rect.Dispose();
                        bookingList.RemoveAt(index);
                    }
                    else
                    {
                        if (MessageBox.Show("The lesson you are trying to delete is linked to another do you want to delete both?", "Linked Lesson", MessageBoxButtons.YesNo) == DialogResult.Yes)
                        {
                            bookingList[index2].rect.Dispose();
                            bookingList.RemoveAt(index2);
                            index = bookingList.FindIndex(
                                delegate(booking sb)
                            {
                                return(sb.id == b.id);
                            });
                            bookingList[index].rect.Dispose();
                            bookingList.RemoveAt(index);
                        }
                        else
                        {
                            bookingList[index2].infill    = false;
                            bookingList[index2].rect.Text = bookingList[index2].lesson + "\n" + bookingList[index2].level + "-Yr " + bookingList[index2].year + "\n Group " + bookingList[index2].group + "\n" + bookingList[index2].staff + " [" + bookingList[index2].room + "]";

                            bookingList[index].rect.Dispose();
                            bookingList.RemoveAt(index);
                        }
                    }
                    booking.selected = -1;
                }
                if (r == DialogResult.OK)
                {
                    update(booking.selected);
                    booking.selected = -1;
                }
                displayLessons(lastItem);
                updateCombo();
            }
        }