context: using System;
  using System.Data;
  using System.Drawing;
  using System.Drawing.Printing;
  using System.Collections;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Windows.Forms;
  using OpenDental.UI;
  using OpenDentBusiness;
  
  namespace OpenDental{
  	/// <summary>
  	/// Summary description for FormBasicTemplate.
  	/// </summary>
  	public class FormSchedule:System.Windows.Forms.Form {
  		private OpenDental.UI.ODGrid gridMain;
  		private OpenDental.UI.Button butRefresh;
  		private ValidDate textDateTo;
  		private Label label2;
  		private ValidDate textDateFrom;
  		private Label label1;
  		private ListBox listProv;
  		private Label labelProv;
  		private CheckBox checkWeekend;
  		private GroupBox groupCopy;
  		private OpenDental.UI.Button butCopyDay;
  		private TextBox textClipboard;
  		private Label label3;
  		private OpenDental.UI.Button butRepeat;
  		private Label label4;
  		private TextBox textRepeat;
  		private OpenDental.UI.Button butPaste;
  		private OpenDental.UI.Button butCopyWeek;
  		private OpenDental.UI.Button butPrint;
  		/// <summary>
  		/// Required designer variable.
  		/// </summary>
  		private System.ComponentModel.Container components = null;
  		private DateTime DateCopyStart;
  		private OpenDental.UI.Button butDelete;
  		private DateTime DateCopyEnd;
  		private CheckBox checkPractice;
  		private ListBox listEmp;
  		private Label label5;
  		private CheckBox checkReplace;
  		private GroupBox groupPaste;
  		///<summary>This tracks whether the provList or empList has been click on since the last refresh.  Forces user to refresh before deleting or pasting so that the list exactly matches the grid.</summary>
  		private bool ProvsChanged;
  		private PrintDocument pd;
  		private bool headingPrinted;
  		private int pagesPrinted;
  		private int headingPrintH;
  		private Label labelClinic;
  		private ComboBox comboClinic;
  		bool changed;
  		private List<Clinic> _listClinics;
  		private List<Provider> _listProvs;
  		private List<Employee> _listEmps;
  
  		///<summary></summary>
  		public FormSchedule()
  		{
  			//
  			// Required for Windows Form Designer support
  			//
  			InitializeComponent();
  			Lan.F(this);
  		}
  
  		/// <summary>
  		/// Clean up any resources being used.
  		/// </summary>
  		protected override void Dispose( bool disposing )
  		{
  			if( disposing )
  			{
  				if(components != null)
  				{
  					components.Dispose();
  				}
  			}
  			base.Dispose( disposing );
  		}
  
  		#region Windows Form Designer generated code
  		/// <summary>
  		/// Required method for Designer support - do not modify
  		/// the contents of this method with the code editor.
  		/// </summary>
  		private void InitializeComponent()
  		{
  			System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FormSchedule));
  			this.label2 = new System.Windows.Forms.Label();
  			this.label1 = new System.Windows.Forms.Label();
  			this.listProv = new System.Windows.Forms.ListBox();
  			this.labelProv = new System.Windows.Forms.Label();
  			this.checkWeekend = new System.Windows.Forms.CheckBox();
  			this.groupCopy = new System.Windows.Forms.GroupBox();
  			this.butCopyWeek = new OpenDental.UI.Button();
  			this.butCopyDay = new OpenDental.UI.Button();
  			this.textClipboard = new System.Windows.Forms.TextBox();
  			this.label3 = new System.Windows.Forms.Label();
  			this.label4 = new System.Windows.Forms.Label();
  			this.textRepeat = new System.Windows.Forms.TextBox();
  			this.checkPractice = new System.Windows.Forms.CheckBox();
  			this.listEmp = new System.Windows.Forms.ListBox();
  			this.label5 = new System.Windows.Forms.Label();
  			this.checkReplace = new System.Windows.Forms.CheckBox();
  			this.groupPaste = new System.Windows.Forms.GroupBox();
  			this.butRepeat = new OpenDental.UI.Button();
  			this.butPaste = new OpenDental.UI.Button();
  			this.labelClinic = new System.Windows.Forms.Label();
  			this.comboClinic = new System.Windows.Forms.ComboBox();
  			this.butDelete = new OpenDental.UI.Button();
  			this.butPrint = new OpenDental.UI.Button();
  			this.textDateTo = new OpenDental.ValidDate();
  			this.textDateFrom = new OpenDental.ValidDate();
  			this.butRefresh = new OpenDental.UI.Button();
  			this.gridMain = new OpenDental.UI.ODGrid();
  			this.groupCopy.SuspendLayout();
  			this.groupPaste.SuspendLayout();
  			this.SuspendLayout();
  			// 
  			// label2
  			// 
  			this.label2.Location = new System.Drawing.Point(102, 75);
  			this.label2.Name = "label2";
  			this.label2.Size = new System.Drawing.Size(85, 15);
  			this.label2.TabIndex = 9;
  			this.label2.Text = "To Date";
  			this.label2.TextAlign = System.Drawing.ContentAlignment.BottomLeft;
  			// 
  			// label1
  			// 
  			this.label1.Location = new System.Drawing.Point(16, 75);
  			this.label1.Name = "label1";
  			this.label1.Size = new System.Drawing.Size(86, 15);
  			this.label1.TabIndex = 7;
  			this.label1.Text = "From Date";
  			this.label1.TextAlign = System.Drawing.ContentAlignment.BottomLeft;
  			// 
  			// listProv
  			// 
  			this.listProv.Location = new System.Drawing.Point(18, 144);
  			this.listProv.Name = "listProv";
  			this.listProv.SelectionMode = System.Windows.Forms.SelectionMode.MultiExtended;
  			this.listProv.Size = new System.Drawing.Size(82, 290);
  			this.listProv.TabIndex = 23;
  			this.listProv.Click += new System.EventHandler(this.listProv_Click);
  			// 
  			// labelProv
  			// 
  			this.labelProv.Location = new System.Drawing.Point(15, 124);
  			this.labelProv.Name = "labelProv";
  			this.labelProv.Size = new System.Drawing.Size(87, 18);
  			this.labelProv.TabIndex = 22;
  			this.labelProv.Text = "Providers";
  			this.labelProv.TextAlign = System.Drawing.ContentAlignment.BottomLeft;
  			// 
  			// checkWeekend
  			// 
  			this.checkWeekend.Location = new System.Drawing.Point(29, 436);
  			this.checkWeekend.Name = "checkWeekend";
  			this.checkWeekend.Size = new System.Drawing.Size(143, 18);
  			this.checkWeekend.TabIndex = 24;
  			this.checkWeekend.Text = "Show Weekends";
  			this.checkWeekend.UseVisualStyleBackColor = true;
  			this.checkWeekend.Click += new System.EventHandler(this.checkWeekend_Click);
  			// 
  			// groupCopy
  			// 
  			this.groupCopy.Controls.Add(this.butCopyWeek);
  			this.groupCopy.Controls.Add(this.butCopyDay);
  			this.groupCopy.Controls.Add(this.textClipboard);
  			this.groupCopy.Controls.Add(this.label3);
  			this.groupCopy.Location = new System.Drawing.Point(23, 484);
  			this.groupCopy.Name = "groupCopy";
  			this.groupCopy.Size = new System.Drawing.Size(158, 111);
  			this.groupCopy.TabIndex = 25;
  			this.groupCopy.TabStop = false;
  			this.groupCopy.Text = "Copy";
  			// 
  			// butCopyWeek
  			// 
  			this.butCopyWeek.AdjustImageLocation = new System.Drawing.Point(0, 0);
  			this.butCopyWeek.Autosize = true;
  			this.butCopyWeek.BtnShape = OpenDental.UI.enumType.BtnShape.Rectangle;
  			this.butCopyWeek.BtnStyle = OpenDental.UI.enumType.XPStyle.Silver;
  			this.butCopyWeek.CornerRadius = 4F;
  			this.butCopyWeek.Location = new System.Drawing.Point(6, 83);
  			this.butCopyWeek.Name = "butCopyWeek";
  			this.butCopyWeek.Size = new System.Drawing.Size(75, 24);
  			this.butCopyWeek.TabIndex = 28;
  			this.butCopyWeek.Text = "Copy Week";
  			this.butCopyWeek.Click += new System.EventHandler(this.butCopyWeek_Click);
  			// 
  			// butCopyDay
  			// 
  			this.butCopyDay.AdjustImageLocation = new System.Drawing.Point(0, 0);
  			this.butCopyDay.Autosize = true;
  			this.butCopyDay.BtnShape = OpenDental.UI.enumType.BtnShape.Rectangle;
  			this.butCopyDay.BtnStyle = OpenDental.UI.enumType.XPStyle.Silver;
  			this.butCopyDay.CornerRadius = 4F;
  			this.butCopyDay.Location = new System.Drawing.Point(6, 56);
  			this.butCopyDay.Name = "butCopyDay";
  			this.butCopyDay.Size = new System.Drawing.Size(75, 24);
  			this.butCopyDay.TabIndex = 27;
  			this.butCopyDay.Text = "Copy Day";
  			this.butCopyDay.Click += new System.EventHandler(this.butCopyDay_Click);
  			// 
  			// textClipboard
  			// 
  			this.textClipboard.Location = new System.Drawing.Point(6, 33);
  			this.textClipboard.Name = "textClipboard";
  			this.textClipboard.ReadOnly = true;
  			this.textClipboard.Size = new System.Drawing.Size(146, 20);
  			this.textClipboard.TabIndex = 26;
  			// 
  			// label3
  			// 
  			this.label3.Location = new System.Drawing.Point(3, 16);
  			this.label3.Name = "label3";
  			this.label3.Size = new System.Drawing.Size(146, 14);
  			this.label3.TabIndex = 8;
  			this.label3.Text = "Clipboard Contents";
  			this.label3.TextAlign = System.Drawing.ContentAlignment.BottomLeft;
  			// 
  			// label4
  			// 
  			this.label4.Location = new System.Drawing.Point(70, 65);
  			this.label4.Name = "label4";
  			this.label4.Size = new System.Drawing.Size(37, 14);
  			this.label4.TabIndex = 32;
  			this.label4.Text = "#";
  			this.label4.TextAlign = System.Drawing.ContentAlignment.BottomRight;
  			// 
  			// textRepeat
  			// 
  			this.textRepeat.Location = new System.Drawing.Point(110, 62);
  			this.textRepeat.Name = "textRepeat";
  			this.textRepeat.Size = new System.Drawing.Size(39, 20);
  			this.textRepeat.TabIndex = 31;
  			this.textRepeat.Text = "1";
  			// 
  			// checkPractice
  			// 
  			this.checkPractice.Checked = true;
  			this.checkPractice.CheckState = System.Windows.Forms.CheckState.Checked;
  			this.checkPractice.Location = new System.Drawing.Point(18, 111);
  			this.checkPractice.Name = "checkPractice";
  			this.checkPractice.Size = new System.Drawing.Size(169, 18);
  			this.checkPractice.TabIndex = 28;
  			this.checkPractice.Text = "Show Practice Notes";
  			this.checkPractice.UseVisualStyleBackColor = true;
  			this.checkPractice.Click += new System.EventHandler(this.checkPractice_Click);
  			// 
  			// listEmp
  			// 
  			this.listEmp.Location = new System.Drawing.Point(106, 144);
  			this.listEmp.Name = "listEmp";
  			this.listEmp.SelectionMode = System.Windows.Forms.SelectionMode.MultiExtended;
  			this.listEmp.Size = new System.Drawing.Size(82, 290);
  			this.listEmp.TabIndex = 30;
  			this.listEmp.Click += new System.EventHandler(this.listEmp_Click);
  			// 
  			// label5
  			// 
  			this.label5.Location = new System.Drawing.Point(103, 124);
  			this.label5.Name = "label5";
  			this.label5.Size = new System.Drawing.Size(87, 18);
  			this.label5.TabIndex = 29;
  			this.label5.Text = "Employees";
  			this.label5.TextAlign = System.Drawing.ContentAlignment.BottomLeft;
  			// 
  			// checkReplace
  			// 
  			this.checkReplace.Checked = true;
  			this.checkReplace.CheckState = System.Windows.Forms.CheckState.Checked;
  			this.checkReplace.Location = new System.Drawing.Point(6, 14);
  			this.checkReplace.Name = "checkReplace";
  			this.checkReplace.Size = new System.Drawing.Size(146, 18);
  			this.checkReplace.TabIndex = 31;
  			this.checkReplace.Text = "Replace Existing";
  			this.checkReplace.UseVisualStyleBackColor = true;
  			// 
  			// groupPaste
  			// 
  			this.groupPaste.Controls.Add(this.butRepeat);
  			this.groupPaste.Controls.Add(this.label4);
  			this.groupPaste.Controls.Add(this.checkReplace);
  			this.groupPaste.Controls.Add(this.textRepeat);
  			this.groupPaste.Controls.Add(this.butPaste);
  			this.groupPaste.Location = new System.Drawing.Point(23, 596);
  			this.groupPaste.Name = "groupPaste";
  			this.groupPaste.Size = new System.Drawing.Size(158, 87);
  			this.groupPaste.TabIndex = 32;
  			this.groupPaste.TabStop = false;
  			this.groupPaste.Text = "Paste";
  			// 
  			// butRepeat
  			// 
  			this.butRepeat.AdjustImageLocation = new System.Drawing.Point(0, 0);
  			this.butRepeat.Autosize = true;
  			this.butRepeat.BtnShape = OpenDental.UI.enumType.BtnShape.Rectangle;
  			this.butRepeat.BtnStyle = OpenDental.UI.enumType.XPStyle.Silver;
  			this.butRepeat.CornerRadius = 4F;
  			this.butRepeat.Location = new System.Drawing.Point(6, 59);
  			this.butRepeat.Name = "butRepeat";
  			this.butRepeat.Size = new System.Drawing.Size(75, 24);
  			this.butRepeat.TabIndex = 30;
  			this.butRepeat.Text = "Repeat";
  			this.butRepeat.Click += new System.EventHandler(this.butRepeat_Click);
  			// 
  			// butPaste
  			// 
  			this.butPaste.AdjustImageLocation = new System.Drawing.Point(0, 0);
  			this.butPaste.Autosize = true;
  			this.butPaste.BtnShape = OpenDental.UI.enumType.BtnShape.Rectangle;
  			this.butPaste.BtnStyle = OpenDental.UI.enumType.XPStyle.Silver;
  			this.butPaste.CornerRadius = 4F;
  			this.butPaste.Location = new System.Drawing.Point(6, 32);
  			this.butPaste.Name = "butPaste";
  			this.butPaste.Size = new System.Drawing.Size(75, 24);
  			this.butPaste.TabIndex = 29;
  			this.butPaste.Text = "Paste";
  			this.butPaste.Click += new System.EventHandler(this.butPaste_Click);
  			// 
  			// labelClinic
  			// 
  			this.labelClinic.Location = new System.Drawing.Point(19, 35);
  			this.labelClinic.Name = "labelClinic";
  			this.labelClinic.Size = new System.Drawing.Size(58, 13);
  			this.labelClinic.TabIndex = 34;
  			this.labelClinic.Text = "Clinic";
  			this.labelClinic.TextAlign = System.Drawing.ContentAlignment.BottomLeft;
  			// 
  			// comboClinic
  			// 
  			this.comboClinic.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
  			this.comboClinic.Location = new System.Drawing.Point(18, 51);
  			this.comboClinic.MaxDropDownItems = 30;
  			this.comboClinic.Name = "comboClinic";
  			this.comboClinic.Size = new System.Drawing.Size(170, 21);
  			this.comboClinic.TabIndex = 35;
  			this.comboClinic.SelectionChangeCommitted += new System.EventHandler(this.comboClinic_SelectionChangeCommitted);
  			// 
  			// butDelete
  			// 
  			this.butDelete.AdjustImageLocation = new System.Drawing.Point(0, 0);
  			this.butDelete.Autosize = true;
  			this.butDelete.BtnShape = OpenDental.UI.enumType.BtnShape.Rectangle;
  			this.butDelete.BtnStyle = OpenDental.UI.enumType.XPStyle.Silver;
  			this.butDelete.CornerRadius = 4F;
  			this.butDelete.Image = global::OpenDental.Properties.Resources.deleteX;
  			this.butDelete.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
  			this.butDelete.Location = new System.Drawing.Point(29, 454);
  			this.butDelete.Name = "butDelete";
  			this.butDelete.Size = new System.Drawing.Size(103, 24);
  			this.butDelete.TabIndex = 27;
  			this.butDelete.Text = "Clear Week";
  			this.butDelete.Click += new System.EventHandler(this.butDelete_Click);
  			// 
  			// butPrint
  			// 
  			this.butPrint.AdjustImageLocation = new System.Drawing.Point(0, 0);
  			this.butPrint.Autosize = true;
  			this.butPrint.BtnShape = OpenDental.UI.enumType.BtnShape.Rectangle;
  			this.butPrint.BtnStyle = OpenDental.UI.enumType.XPStyle.Silver;
  			this.butPrint.CornerRadius = 4F;
  			this.butPrint.Image = global::OpenDental.Properties.Resources.butPrint;
  			this.butPrint.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
  			this.butPrint.Location = new System.Drawing.Point(551, 666);
  			this.butPrint.Name = "butPrint";
  			this.butPrint.Size = new System.Drawing.Size(90, 24);
  			this.butPrint.TabIndex = 26;
  			this.butPrint.Text = "Print";
  			this.butPrint.Click += new System.EventHandler(this.butPrint_Click);
  			// 
  			// textDateTo
  			// 
  			this.textDateTo.Location = new System.Drawing.Point(105, 91);
  			this.textDateTo.Name = "textDateTo";
  			this.textDateTo.Size = new System.Drawing.Size(82, 20);
  			this.textDateTo.TabIndex = 10;
  			// 
  			// textDateFrom
  			// 
  			this.textDateFrom.Location = new System.Drawing.Point(18, 91);
  			this.textDateFrom.Name = "textDateFrom";
  			this.textDateFrom.Size = new System.Drawing.Size(82, 20);
  			this.textDateFrom.TabIndex = 8;
  			// 
  			// butRefresh
  			// 
  			this.butRefresh.AdjustImageLocation = new System.Drawing.Point(0, 0);
  			this.butRefresh.Autosize = true;
  			this.butRefresh.BtnShape = OpenDental.UI.enumType.BtnShape.Rectangle;
  			this.butRefresh.BtnStyle = OpenDental.UI.enumType.XPStyle.Silver;
  			this.butRefresh.CornerRadius = 4F;
  			this.butRefresh.Location = new System.Drawing.Point(57, 8);
  			this.butRefresh.Name = "butRefresh";
  			this.butRefresh.Size = new System.Drawing.Size(75, 24);
  			this.butRefresh.TabIndex = 11;
  			this.butRefresh.Text = "Refresh";
  			this.butRefresh.Click += new System.EventHandler(this.butRefresh_Click);
  			// 
  			// gridMain
  			// 
  			this.gridMain.HScrollVisible = false;
  			this.gridMain.Location = new System.Drawing.Point(207, 8);
  			this.gridMain.Name = "gridMain";
  			this.gridMain.ScrollValue = 0;
  			this.gridMain.SelectionMode = OpenDental.UI.GridSelectionMode.OneCell;
  			this.gridMain.Size = new System.Drawing.Size(761, 652);
  			this.gridMain.TabIndex = 0;
  			this.gridMain.Title = "Schedule";
  			this.gridMain.TranslationName = null;
  			this.gridMain.CellDoubleClick += new OpenDental.UI.ODGridClickEventHandler(this.gridMain_CellDoubleClick);
  			// 
  			// FormSchedule
  			// 
  			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  			this.ClientSize = new System.Drawing.Size(974, 695);
  			this.Controls.Add(this.comboClinic);
  			this.Controls.Add(this.labelClinic);
  			this.Controls.Add(this.groupCopy);
  			this.Controls.Add(this.groupPaste);
  			this.Controls.Add(this.listEmp);
  			this.Controls.Add(this.label5);
  			this.Controls.Add(this.checkPractice);
  			this.Controls.Add(this.butDelete);
  			this.Controls.Add(this.butPrint);
  			this.Controls.Add(this.textDateTo);
  			this.Controls.Add(this.textDateFrom);
  			this.Controls.Add(this.checkWeekend);
  			this.Controls.Add(this.listProv);
  			this.Controls.Add(this.labelProv);
  			this.Controls.Add(this.butRefresh);
  			this.Controls.Add(this.label2);
  			this.Controls.Add(this.label1);
  			this.Controls.Add(this.gridMain);
  			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
  			this.MaximizeBox = false;
  			this.MinimizeBox = false;
  			this.Name = "FormSchedule";
  			this.ShowInTaskbar = false;
  			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
  			this.Text = "Schedule";
  			this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.FormSchedule_FormClosing);
  			this.Load += new System.EventHandler(this.FormSchedule_Load);
  			this.groupCopy.ResumeLayout(false);
  			this.groupCopy.PerformLayout();
  			this.groupPaste.ResumeLayout(false);
  			this.groupPaste.PerformLayout();
  			this.ResumeLayout(false);
  			this.PerformLayout();
  
  		}
  		#endregion
  
  		private void FormSchedule_Load(object sender,EventArgs e) {
  			if(!Security.IsAuthorized(Permissions.Schedules,DateTime.MinValue,true)) {
  				//gridMain.Enabled=false;
  				butDelete.Enabled=false;
  				groupCopy.Enabled=false;
  				groupPaste.Enabled=false;
  				if(PrefC.GetBool(PrefName.DistributorKey)) {//if this is OD HQ
  					checkPractice.Checked=false;
  					checkPractice.Enabled=false;
  				}
  			}
  			DateTime dateFrom=new DateTime(DateTime.Today.Year,DateTime.Today.Month,1);
  			textDateFrom.Text=dateFrom.ToShortDateString();
  			textDateTo.Text=dateFrom.AddMonths(12).AddDays(-1).ToShortDateString();
  			//Get available clinics and then filter the employee list box by those clinics.
  			if(PrefC.GetBool(PrefName.EasyNoClinics)) {
  				labelClinic.Visible=false;
  				comboClinic.Visible=false;
  			}
  			else {//clinics
  				_listClinics=Clinics.GetForUserod(Security.CurUser);
  				comboClinic.Items.Clear();
  				if(!Security.CurUser.ClinicIsRestricted) {
  					comboClinic.Items.Add(Lan.g(this,"Headquarters"));
  					comboClinic.SelectedIndex=0;
  				}
  				for(int i=0;i<_listClinics.Count;i++) {
  					int curIndex=comboClinic.Items.Add(_listClinics[i].Description);
  					if(_listClinics[i].ClinicNum==FormOpenDental.ClinicNum) {
  						comboClinic.SelectedIndex=curIndex;
  					}
  				}
  			}
  			FillEmployeesAndProviders();
  			FillGrid();
  		}
  
  		///<summary>Fills the employee box based on what clinic is selected.  Set selectAll to true to have all employees in the list box selected by default.</summary>
  		private void FillEmployeesAndProviders() {
  			listProv.Items.Clear();
  			//Do NOT filter providers by clinic yet.  This will be implemented in phase 2.
  			_listProvs=ProviderC.GetListShort();
  			for(int i=0;i<_listProvs.Count;i++) {
  				listProv.Items.Add(_listProvs[i].Abbr);
  				listProv.SetSelected(i,true);
  			}
  			listEmp.Items.Clear();
  			//Filter employees by the currently selected clinic.
  			if(PrefC.GetBool(PrefName.EasyNoClinics)) {
  				_listEmps=Employees.GetListShort();
  				for(int i=0;i<_listEmps.Count;i++) {
  					listEmp.Items.Add(_listEmps[i].FName);
  					listEmp.SetSelected(i,true);
  				}
  			}
  			else {//Using clinics.
  				//Psuedo code for filtering providers by clinics.  We are not going to filter the provider list until phase 2.
  				//_listProvs=new List<Provider>();
  				//if(comboClinic.SelectedIndex==0 && !Security.CurUser.ClinicIsRestricted) {
  				//	_listProvs=Providers.GetProvsByClinic(0);
  				//}
  				//else if(comboClinic.SelectedIndex==0 && Security.CurUser.ClinicIsRestricted) {
  				//	_listProvs=Providers.GetProvsByClinic(_listClinics[comboClinic.SelectedIndex].ClinicNum);
  				//}
  				//else {
  				//	_listProvs=Providers.GetProvsByClinic(_listClinics[comboClinic.SelectedIndex-1].ClinicNum);
  				//}
  				//Fill the employee list box.
  				_listEmps=new List<Employee>();
  				if(comboClinic.SelectedIndex==0 && !Security.CurUser.ClinicIsRestricted) {//Headquarters is selected.
  					_listEmps=Employees.GetEmpsForClinic(0);//Only get employees that are not assigned to a clinic.
  				}
  				else if(Security.CurUser.ClinicIsRestricted) {
  					_listEmps=Employees.GetEmpsForClinic(_listClinics[comboClinic.SelectedIndex].ClinicNum);
  				}
  				else {
  					_listEmps=Employees.GetEmpsForClinic(_listClinics[comboClinic.SelectedIndex-1].ClinicNum);//Subtract 1, because Headquarters is the first option.
  				}
  				for(int i=0;i<_listEmps.Count;i++) {
  					listEmp.Items.Add(_listEmps[i].FName);
  					listEmp.SetSelected(i,true);
  				}
  			}
  		}
  
  		private void FillGrid(){
  			DateCopyStart=DateTime.MinValue;
  			DateCopyEnd=DateTime.MinValue;
  			textClipboard.Text="";
  			if(textDateFrom.errorProvider1.GetError(textDateFrom)!=""
  				|| textDateTo.errorProvider1.GetError(textDateTo)!="")
  			{
  				//MsgBox.Show(this,"Please fix errors first.");
  				return;
  			}
  			ProvsChanged=false;
  			List<long> provNums=new List<long>();
  			for(int i=0;i<listProv.SelectedIndices.Count;i++){
  				provNums.Add(_listProvs[listProv.SelectedIndices[i]].ProvNum);
  			}
  			List<long> empNums=new List<long>();
  			for(int i=0;i<listEmp.SelectedIndices.Count;i++){
  				empNums.Add(_listEmps[listEmp.SelectedIndices[i]].EmployeeNum);
  			}
  			DataTable table=Schedules.GetPeriod(PIn.Date(textDateFrom.Text),PIn.Date(textDateTo.Text),provNums,
  				empNums,checkPractice.Checked);
  			gridMain.BeginUpdate();
  			gridMain.Columns.Clear();
  			int colW;
  			if(checkWeekend.Checked){
  				colW=(gridMain.Width-20)/7;
  			}
  			else{
  				colW=(gridMain.Width-20)/5;
  			}
  			ODGridColumn col;
  			if(checkWeekend.Checked){
  				col=new ODGridColumn(Lan.g("TableSchedule","Sunday"),colW);
  				gridMain.Columns.Add(col);
  			}
  			col=new ODGridColumn(Lan.g("TableSchedule","Monday"),colW);
  			gridMain.Columns.Add(col);
  			col=new ODGridColumn(Lan.g("TableSchedule","Tuesday"),colW);
  			gridMain.Columns.Add(col);
  			col=new ODGridColumn(Lan.g("TableSchedule","Wednesday"),colW);
  			gridMain.Columns.Add(col);
  			col=new ODGridColumn(Lan.g("TableSchedule","Thursday"),colW);
  			gridMain.Columns.Add(col);
  			col=new ODGridColumn(Lan.g("TableSchedule","Friday"),colW);
  			gridMain.Columns.Add(col);
  			if(checkWeekend.Checked){
  				col=new ODGridColumn(Lan.g("TableSchedule","Saturday"),colW);
  				gridMain.Columns.Add(col);
  			}
  			gridMain.Rows.Clear();
  			ODGridRow row;
  			for(int i=0;i<table.Rows.Count;i++){
  				row=new ODGridRow();
  				if(checkWeekend.Checked){
  					row.Cells.Add(table.Rows[i][0].ToString());
  				}
  				row.Cells.Add(table.Rows[i][1].ToString());
  				row.Cells.Add(table.Rows[i][2].ToString());
  				row.Cells.Add(table.Rows[i][3].ToString());
  				row.Cells.Add(table.Rows[i][4].ToString());
  				row.Cells.Add(table.Rows[i][5].ToString());
  				if(checkWeekend.Checked){
  					row.Cells.Add(table.Rows[i][6].ToString());
  				}
  				gridMain.Rows.Add(row);
  			}
  			gridMain.EndUpdate();
  			//Set today red
  			if(!checkWeekend.Checked && (DateTime.Today.DayOfWeek==DayOfWeek.Sunday || DateTime.Today.DayOfWeek==DayOfWeek.Saturday)){
  				return;
  			}
  			if(DateTime.Today>PIn.Date(textDateTo.Text) || DateTime.Today<PIn.Date(textDateFrom.Text)){
  				return;
  			}
  			int colI=(int)DateTime.Today.DayOfWeek;
  			if(!checkWeekend.Checked){
  				colI--;
  			}
  			gridMain.Rows[Schedules.GetRowCal(PIn.Date(textDateFrom.Text),DateTime.Today)].Cells[colI].ColorText=Color.Red;
  		}
  
  		private void comboClinic_SelectionChangeCommitted(object sender,EventArgs e) {
  			FillEmployeesAndProviders();
  			FillGrid();
  		}
  
  		private void butRefresh_Click(object sender,EventArgs e) {
  			if(textDateFrom.errorProvider1.GetError(textDateFrom)!=""
  				|| textDateTo.errorProvider1.GetError(textDateTo)!="")
  			{
  				MsgBox.Show(this,"Please fix errors first.");
  				return;
  			}
  			FillGrid();
  		}
  
  		private void checkWeekend_Click(object sender,EventArgs e) {
  			FillGrid();
  		}
  
  		private void checkPractice_Click(object sender,EventArgs e) {
  			FillGrid();
  		}
  
  		private void gridMain_CellDoubleClick(object sender,ODGridClickEventArgs e) {
  			if(!Security.IsAuthorized(Permissions.Schedules,DateTime.MinValue)) {
  				return;
  			}
  			if(textDateFrom.errorProvider1.GetError(textDateFrom)!=""
  				|| textDateTo.errorProvider1.GetError(textDateTo)!="")
  			{
  				MsgBox.Show(this,"Please fix errors first.");
  				return;
  			}
  			int clickedCol=e.Col;
  			if(!checkWeekend.Checked){
  				clickedCol++;
  			}
  			//the "clickedCell" is in terms of the entire 7 col layout.
  			Point clickedCell=new Point(clickedCol,e.Row);
  			DateTime selectedDate=Schedules.GetDateCal(PIn.Date(textDateFrom.Text),e.Row,clickedCol);
  			if(selectedDate<PIn.Date(textDateFrom.Text) || selectedDate>PIn.Date(textDateTo.Text)){
  				return;
  			}
  			//MessageBox.Show(selectedDate.ToShortDateString());
  			long clinicNum=0; 
  			if(Security.CurUser.ClinicIsRestricted) {
  				clinicNum=_listClinics[comboClinic.SelectedIndex].ClinicNum;
  			}
  			else if(comboClinic.SelectedIndex > 0) {
  				clinicNum=_listClinics[comboClinic.SelectedIndex-1].ClinicNum;//Subtract 1, because Headquarters is the first option.
  			}
  			FormScheduleDayEdit FormS=new FormScheduleDayEdit(selectedDate,clinicNum);
  			FormS.ShowDialog();
  			if(FormS.DialogResult!=DialogResult.OK){
  				return;
  			}
  			FillGrid();
  			if(checkWeekend.Checked){
  				gridMain.SetSelected(clickedCell);
  			}
  			else{
  				gridMain.SetSelected(new Point(clickedCell.X-1,clickedCell.Y));
  			}
  			changed=true;
  		}
  
  		private void listProv_Click(object sender,EventArgs e) {
  			ProvsChanged=true;
  		}
  
  		private void listEmp_Click(object sender,EventArgs e) {
  			ProvsChanged=true;
  		}
  
  		private void butDelete_Click(object sender,EventArgs e) {
  			if(textDateFrom.errorProvider1.GetError(textDateFrom)!=""
  				|| textDateTo.errorProvider1.GetError(textDateTo)!="") 
  			{
  				MsgBox.Show(this,"Please fix errors first.");
  				return;
  			}
  			if(gridMain.SelectedCell.X==-1) {
  
gt: 				MsgBox.Show(this,"Please select a date first.");