/// <summary>
		/// Метод-callback для кнопки сохранения/создания объекта.
		/// </summary>
		void saveButton_Click(object sender, RoutedEventArgs e)
		{
			if(currentObject is Teacher)
			{
				if(CheckDataBoxes(ObjectType.Teacher))
				{
					int flag = 0;
					Teacher t = (Teacher) currentObject;
					t.Login = infoBlock3.Text;
					t.Pass = infoBlock5.Text;
					t.Name = infoBlock2.Text;
					
					if(!t.IsValid())
					{
						if(MTSystem.CheckLogin(infoBlock3.Text))
						{
							MessageBox.Show("Логин уже зарегистрирован!");
							return;
						}
						MTSystem.CreateClient((User)t);
						t.Id = MTSystem.GetLastInsertId("users");
						MTSystem.addTeacher(t);
						flag = 1;
					}
					// Алгоритмы сохранения данных без дублирования
					int count = 0;
					List<int> groups = new List<int>();
					for(int i = 0; i<t.GroupIdsCount; i++) groups.Add(t.getGroupId(i));
				
					t.ClearGroupIds();
					
					for(int i = 0; i<infoBigGrid.Items.Count; i++) 
						t.addGroupId(Convert.ToInt32(((SampleGrid)infoBigGrid.Items[i]).P1));
					
					for(int i = 0; i<groups.Count; i++)
					{
						for(int j = 0; j<t.GroupIdsCount; j++)
						{
							if(groups[i] == t.getGroupId(j))
							{
								count++;
								break;
							}
						}
						if(count == 0) // Если старая группа не найдена в новых, то ее удалили
						{
							Group g = MTSystem.findGroupById(groups[i]);
							g.deleteTeacherId(t.Id);
							MTSystem.SaveGroup(g);
						}
						count = 0;
					}
					
					for(int i = 0; i<t.GroupIdsCount; i++)
					{
						for(int j = 0; i<groups.Count; j++)
						{
							if(t.getGroupId(i) == groups[j])
							{
								count++;
								break;
							}
						}
						if(count == 0) // Если новая группа не найдена в старых, то ее добавили
						{
							Group g = MTSystem.findGroupById(t.getGroupId(i));
							g.addTeacherId(t.Id);
							MTSystem.SaveGroup(g);
						}
						count = 0;
					}
					
					if(MTSystem.SaveClient((User)t))
					{
						if(flag == 1)
							MessageBox.Show("Преподаватель создан успешно!");
						else
							MessageBox.Show("Преподаватель сохранен!");
					}
					else
					{
						if(flag == 1)
							MessageBox.Show("Ошибка при создании!");
						else
							MessageBox.Show("Ошибка при сохранении!");
					}
					
					SaveWindowDataEvent(ObjectType.Teacher);
					Close();
				}
			}
			else if(currentObject is Admin)
			{
				
				if(CheckDataBoxes(ObjectType.Admin))
				{
					Admin t = (Admin) currentObject;
					t.Login = infoBlock3.Text;
					t.Pass = infoBlock5.Text;
					t.Name = infoBlock2.Text;
					if(t.IsValid())
					{
						if(MTSystem.SaveClient((User)t))
							MessageBox.Show("Администратор сохранен!");
						else
							MessageBox.Show("Ошибка сохранения!");
					}
					else
					{
						MTSystem.CreateClient((User)t);
						MessageBox.Show("Администратор создан успешно!");
					}
						
					SaveWindowDataEvent(ObjectType.Admin);
					Close();
				}
			}
			else if(currentObject is Group)
			{
				if(CheckDataBoxes(ObjectType.Group))
				{
					Group t = (Group) currentObject;
					t.Name = infoBlock2.Text;
					t.PlanHours = Convert.ToInt32(infoBlock3.Text);
					int flag = 0;
					if(cbBlock4.SelectedItem.ToString() == "Не выбрано")
					{
						GroupType grouptype = new GroupType();
						grouptype.Id = 0;
						grouptype.Name = "Не выбрано";
						t.Type = grouptype;
					}
					else t.Type = MTSystem.findGroupTypeByName(cbBlock4.SelectedItem.ToString());
					t.Color.Color = groupColorPicker.SelectedColor;
					if(cbBlock7.SelectedItem.ToString() == "Не выбрано")
					{
						GroupLevel grouplevel = new GroupLevel();
						grouplevel.Id = 0;
						grouplevel.Name = "Не выбрано";
						t.Level = grouplevel;
					}
					else t.Level = MTSystem.findGroupLevelByName(cbBlock7.SelectedItem.ToString());
					
					if(!t.IsValid())
					{
						MTSystem.CreateGroup(t);
						t.Id = MTSystem.GetLastInsertId("groups");
						MTSystem.addGroup(t);
						flag = 1;
					}
					
					// Алгоритмы сохранения данных без дублирования
					List<int> teachers = new List<int>();
					for(int i = 0; i<t.TeachersCount; i++) teachers.Add(t.getTeacherId(i));
					
					int count = 0;
					
					List<int> students = new List<int>();
					for(int i = 0; i<t.StudentsCount; i++) students.Add(t.getStudentId(i));
					
					t.ClearStudentIds();
					for(int i = 0; i<infoBigGrid.Items.Count; i++)
					{
						SampleGrid sg = (SampleGrid) infoBigGrid.Items[i];
						Student s = sg.toStudent();
						if(sg.P5 == "Ходит")
						{
							if(!s.checkGroupId(t.Id))
							{
								s.addGroupId(t.Id);
								MTSystem.SaveStudent(s);
							}
						}
						else
						{
							if(s.checkGroupId(t.Id))
							{
								s.deleteGroupId(t.Id);
								MTSystem.SaveStudent(s);
							}
						}
						t.addStudentId(s.Id);
					}
					
					for(int i = 0; i<students.Count; i++)
					{
						for(int j = 0; j<t.StudentsCount; j++)
						{
							if(students[i] == t.getStudentId(j))
							{
								count = 1;
								break;
							}
						}
						if(count == 0)
						{
							Student s = MTSystem.LoadStudent(students[i]);
							if(s != null)
							{
								if(s.checkGroupId(t.Id))
								{
									s.deleteGroupId(t.Id);
									MTSystem.SaveStudent(s);
								}
							}
						}
						count = 0;
					}
					t.ClearTeacherIds();
					
					for(int i = 0; i<infoSmallGrid.Items.Count; i++)
						t.addTeacherId(Convert.ToInt32(((SampleGrid)infoSmallGrid.Items[i]).P1));
					
					for(int i = 0; i<teachers.Count; i++)
					{
						for(int j = 0; j<t.TeachersCount; j++)
						{
							if(teachers[i] == t.getTeacherId(j))
							{
								count++;
								break;
							}
						}
						if(count == 0) // Если старый препод не найден в новых, то его удалили
						{
							MTSystem.findTeacherById(teachers[i]).deleteGroupId(t.Id);
						}
						count = 0;
					}
					
					for(int i = 0; i<t.TeachersCount; i++)
					{
						for(int j = 0; j<teachers.Count; j++)
						{
							if(t.getTeacherId(i) == teachers[j]) 
							{
								count++;
								break;
							}
						}
						if(count == 0) // Если новый препод не найден в старых, то его добавили
						{
							MTSystem.findTeacherById(t.getTeacherId(i)).addGroupId(t.Id);
						}
						count = 0;
					}
					
					if(MTSystem.SaveGroup(t))
					{
						if(flag == 1)
							MessageBox.Show("Группа создана успешно!");
						else
							MessageBox.Show("Группа сохранена!");
					}
					else
					{
						if(flag == 1)
							MessageBox.Show("Ошибка при создании!");
						else
							MessageBox.Show("Ошибка при сохранении!");
					}
					SaveWindowDataEvent(ObjectType.Group);
					Close();
				}
			
			}
			else if(currentObject is Student)
			{
				if(CheckDataBoxes(ObjectType.Student))
				{
					Student t = (Student) currentObject;
					t.FullName = infoBlock2.Text;
					t.Age = Convert.ToInt32(infoBlock3.Text);
					if(cbBlock4.SelectedItem.ToString() == "Не выбрано")
					{
						StudentStatus status = new StudentStatus();
						status.Id = 0;
						status.Name = "Не выбрано";
						t.Status = status;
					}
					else t.Status = MTSystem.findStudentStatusByName(cbBlock4.SelectedItem.ToString());
					int flag = 0;
					if(!t.IsValid())
					{
						MTSystem.CreateStudent(t);
						t.Id = MTSystem.GetLastInsertId("students");
						flag = 1;
					}
						// Алгоритмы сохранения данных без дублирования
					List<int> groups = new List<int>();
					for(int i = 0; i<t.GroupIdsCount; i++) groups.Add(t.getGroupId(i));
					
					t.ClearGroupIds();
					for(int i = 0; i<infoBigGrid.Items.Count; i++)
					{
						t.addGroupId(Convert.ToInt32(((SampleGrid)infoBigGrid.Items[i]).P1));
					}
					int count = 0;
					for(int i = 0; i<t.GroupIdsCount; i++)
					{
						for(int j = 0; i<groups.Count; j++)
						{
							if(t.getGroupId(i) == groups[j])
							{
								count++;
								break;
							}
						}
						if(count == 0) // Если новая группа не найдена в старых, то ее добавили
						{
							Group g = MTSystem.findGroupById(t.getGroupId(i));
							if(!g.checkStudentId(t.Id))
							{
								g.addStudentId(t.Id);
								MTSystem.SaveGroup(g);
							}
						}
						count = 0;
					}
					
					t.ClearAchievements();
					for(int i = 0; i<infoSmallGrid.Items.Count; i++)
					{
						Achievement a = new Achievement();
						a.Type = Convert.ToInt32(((SampleGrid)infoSmallGrid.Items[i]).P1);
						if(((SampleGrid)infoSmallGrid.Items[i]).P3 == "Участвовал") a.Place = 0;
						else a.Place = Convert.ToInt32(((SampleGrid)infoSmallGrid.Items[i]).P3);
						t.addAchievement(a);
					}
					
					if(MTSystem.SaveStudent(t))
					{
						if(flag == 1)
							MessageBox.Show("Ученик создан успешно!");
						else
							MessageBox.Show("Ученик сохранен!");
					}
					else
					{
						if(flag == 1)
							MessageBox.Show("Ошибка при создании!");
						else
							MessageBox.Show("Ошибка при сохранении!");
					}
					SaveWindowDataEvent(ObjectType.Student);
					Close();
				}
			}
		}
		/// <summary>
		/// Метод-callback для эвента подтверждения удаления.
		/// Обрабатывает результат окна подтверждения.
		/// </summary>
		/// <param name="confirm">Результат окна подтверждения</param>
		void DeleteWindow_CallBack(bool confirm)
		{
			if(confirm == false) return;
			if(currentObject is Teacher)
			{
				Teacher t = (Teacher) currentObject;
				for(int i = 0; i<t.GroupIdsCount; i++)
				{
					Group g = MTSystem.findGroupById(t.getGroupId(i));
					g.deleteTeacherId(t.Id);
					MTSystem.SaveGroup(g);
				}
				int id = t.Id;
				MTSystem.deleteTeacher(t);
				if(MTSystem.DeleteClient(id))
				{
					MessageBox.Show("Удаление прошло успешно!");
					SaveWindowDataEvent(ObjectType.Teacher);
					Close();
					return;
				}
				MessageBox.Show("Ошибка удаления!");
				return;
			}
			if(currentObject is Admin )
			{
				Admin adm = (Admin) currentObject;
				if(MTSystem.DeleteClient(adm.Id))
				{
					MessageBox.Show("Удаление прошло успешно!");
					SaveWindowDataEvent(ObjectType.Admin);
					Close();
					return;
				}
				MessageBox.Show("Ошибка удаления!");
				return;
				
			}
			if(currentObject is Student)
			{
				Student s = (Student) currentObject;
				for(int i = 0; i<MTSystem.GroupsCount; i++)
				{
					Group g = MTSystem.getGroup(i);
					if(g.deleteStudentId(s.Id))
						MTSystem.SaveGroup(g);
				}

				if(MTSystem.DeleteStudent(s.Id))
				{
					MessageBox.Show("Удаление прошло успешно!");
					SaveWindowDataEvent(ObjectType.Student);
					Close();
					return;
				}
				MessageBox.Show("Ошибка удаления!");
				return;
			}
			if(currentObject is Group)
			{
				Group g = (Group) currentObject;
				for(int i = 0; i<g.TeachersCount; i++)
				{
					Teacher t = MTSystem.findTeacherById(g.getTeacherId(i));
					t.deleteGroupId(g.Id);
					MTSystem.SaveClient(t);
				}
				for(int i = 0; i<g.StudentsCount; i++)
				{
					Student s = MTSystem.LoadStudent(g.getStudentId(i));
					if(s.deleteGroupId(g.Id))
						MTSystem.SaveStudent(s);
				}
				int id = g.Id;
				MTSystem.deleteGroup(g);
				if(MTSystem.DeleteGroup(id))
				{
					for(int i = 0; i<MTSystem.SchedulesCount; i++) // Обновление данных расписаний
					{
						for(int j = 0; j<MTSystem.getSchedule(i).RowCount; j++)
						{
							for(int k = 0; k<MTSystem.getSchedule(i).getRow(j).GroupIds.Count; k++)
							{
								if(MTSystem.getSchedule(i).getRow(j).GroupIds[k] == id)
								{
									MTSystem.getSchedule(i).getRow(j).GroupIds[k] = 0;
								}
							}
						}
						MTSystem.SaveSchedule(MTSystem.getSchedule(i));
					}
					MessageBox.Show("Удаление прошло успешно!");
					SaveWindowDataEvent(ObjectType.Group);
					Close();
					return;
				}
				MessageBox.Show("Ошибка удаления!");
				return;
			}
			MessageBox.Show("Ошибка удаления!");
		}
		private CalendarDateRange currentDateRange = null; //Период для вычисления некоторых данных.
		public EditWindow(Object obj)
		{
			currentObject = obj;
			InitializeComponent(); // Конструктор окна в случае редактирования
			deleteButton.IsEnabled = true;
			if(obj is Admin)
			{
				Title = "Редактировать администратора";				
				Admin adm = (Admin)obj;
				SetConfiguration(ObjectType.Admin);
				ClearInfoBlocks();
				infoBlock1.Text = adm.Id.ToString();
				infoBlock2.Text = adm.Name;
				infoBlock3.Text = adm.Login;
				infoBlock5.Text = adm.Pass;
			}
			else if(obj is Group)
			{
				Title = "Редактировать группу";	
				Group gr = (Group)obj;
				SetConfiguration(ObjectType.Group);
				ClearInfoBlocks();
				infoBlock1.Text = gr.Id.ToString();
				infoBlock2.Text = gr.Name;
				infoBlock3.Text = gr.PlanHours.ToString();
				CountDataFields();
				groupColorPicker.SelectedColor = gr.Color.Color;
				
				for(int i = 0; i<gr.TeachersCount; i++)
				{
					infoSmallGrid.Items.Add(MTSystem.findTeacherById(gr.getTeacherId(i)).toSample());
				}
				List<int> studentIds = new List<int>();
				for(int i = 0; i<gr.StudentsCount; i++)
				{
					studentIds.Add(gr.getStudentId(i));
				}
				List<Student> students = MTSystem.LoadStudents(studentIds);
				for(int i = 0; i<studentIds.Count; i++)
				{
					SampleGrid sg = students[i].toSample();
					sg.P5 = (students[i].checkGroupId(gr.Id))?"Ходит":"Выбыл";
					infoBigGrid.Items.Add(sg);
				}
			}
			else if(obj is Student)
			{
				Title = "Редактировать ученика";	
				Student st = (Student)obj;
				SetConfiguration(ObjectType.Student);
				ClearInfoBlocks();
				infoBlock1.Text = st.Id.ToString();
				infoBlock2.Text = st.FullName;
				infoBlock3.Text = st.Age.ToString();
				CountDataFields();
				
				for(int i = 0; i<st.AchievementsCount; i++)
				{
					infoSmallGrid.Items.Add(st.getAchievement(i).toSample());
				}
				
				for(int i = 0; i<st.GroupIdsCount; i++)
				{
					infoBigGrid.Items.Add(MTSystem.findGroupById(st.getGroupId(i)).toSample());
				}
				
			}
			else if(obj is Teacher)
			{
				Title = "Редактировать преподавателя";	
				Teacher tc = (Teacher)obj;
				SetConfiguration(ObjectType.Teacher);
				ClearInfoBlocks();
				infoBlock1.Text = tc.Id.ToString();
				infoBlock2.Text = tc.Name;
				infoBlock3.Text = tc.Login;
				infoBlock5.Text = tc.Pass;
				
				for(int i = 0; i<tc.GroupIdsCount; i++)
				{
					infoBigGrid.Items.Add(MTSystem.findGroupById(tc.getGroupId(i)).toSample());
				}
				
			}
			
		}