Index: Scheduling/trunk/cs/bsdx0200GUISourceCode/CGView.cs
===================================================================
--- Scheduling/trunk/cs/bsdx0200GUISourceCode/CGView.cs	(revision 1097)
+++ Scheduling/trunk/cs/bsdx0200GUISourceCode/CGView.cs	(revision 1106)
@@ -617,10 +617,11 @@
             this.tvSchedules.Location = new System.Drawing.Point(0, 0);
             this.tvSchedules.Name = "tvSchedules";
-            this.tvSchedules.Size = new System.Drawing.Size(128, 358);
+            this.tvSchedules.Size = new System.Drawing.Size(128, 393);
             this.tvSchedules.Sorted = true;
             this.tvSchedules.TabIndex = 1;
             this.tvSchedules.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.tvSchedules_AfterSelect);
-            this.tvSchedules.Click += new System.EventHandler(this.tvSchedules_Click);
+            this.tvSchedules.NodeMouseClick += new System.Windows.Forms.TreeNodeMouseClickEventHandler(this.tvSchedules_NodeMouseClick);
             this.tvSchedules.DoubleClick += new System.EventHandler(this.tvSchedules_DoubleClick);
+            this.tvSchedules.MouseEnter += new System.EventHandler(this.tvSchedules_MouseEnter);
             // 
             // contextMenu1
@@ -662,7 +663,7 @@
             this.panelRight.Controls.Add(this.panelClip);
             this.panelRight.Dock = System.Windows.Forms.DockStyle.Right;
-            this.panelRight.Location = new System.Drawing.Point(941, 0);
+            this.panelRight.Location = new System.Drawing.Point(996, 0);
             this.panelRight.Name = "panelRight";
-            this.panelRight.Size = new System.Drawing.Size(128, 358);
+            this.panelRight.Size = new System.Drawing.Size(128, 393);
             this.panelRight.TabIndex = 3;
             this.panelRight.Visible = false;
@@ -730,5 +731,5 @@
             this.panelTop.Location = new System.Drawing.Point(128, 0);
             this.panelTop.Name = "panelTop";
-            this.panelTop.Size = new System.Drawing.Size(813, 24);
+            this.panelTop.Size = new System.Drawing.Size(868, 24);
             this.panelTop.TabIndex = 6;
             // 
@@ -737,5 +738,5 @@
             this.dateTimePicker1.Dock = System.Windows.Forms.DockStyle.Right;
             this.dateTimePicker1.DropDownAlign = System.Windows.Forms.LeftRightAlignment.Right;
-            this.dateTimePicker1.Location = new System.Drawing.Point(607, 0);
+            this.dateTimePicker1.Location = new System.Drawing.Point(662, 0);
             this.dateTimePicker1.Name = "dateTimePicker1";
             this.dateTimePicker1.Size = new System.Drawing.Size(206, 20);
@@ -760,5 +761,5 @@
             this.panelCenter.Location = new System.Drawing.Point(136, 24);
             this.panelCenter.Name = "panelCenter";
-            this.panelCenter.Size = new System.Drawing.Size(802, 310);
+            this.panelCenter.Size = new System.Drawing.Size(857, 345);
             this.panelCenter.TabIndex = 7;
             // 
@@ -846,7 +847,7 @@
             this.panelBottom.Controls.Add(this.statusBar1);
             this.panelBottom.Dock = System.Windows.Forms.DockStyle.Bottom;
-            this.panelBottom.Location = new System.Drawing.Point(136, 334);
+            this.panelBottom.Location = new System.Drawing.Point(136, 369);
             this.panelBottom.Name = "panelBottom";
-            this.panelBottom.Size = new System.Drawing.Size(802, 24);
+            this.panelBottom.Size = new System.Drawing.Size(857, 24);
             this.panelBottom.TabIndex = 8;
             // 
@@ -856,5 +857,5 @@
             this.statusBar1.Location = new System.Drawing.Point(0, 0);
             this.statusBar1.Name = "statusBar1";
-            this.statusBar1.Size = new System.Drawing.Size(802, 24);
+            this.statusBar1.Size = new System.Drawing.Size(857, 24);
             this.statusBar1.SizingGrip = false;
             this.statusBar1.TabIndex = 0;
@@ -864,5 +865,5 @@
             this.splitter1.Location = new System.Drawing.Point(128, 24);
             this.splitter1.Name = "splitter1";
-            this.splitter1.Size = new System.Drawing.Size(8, 334);
+            this.splitter1.Size = new System.Drawing.Size(8, 369);
             this.splitter1.TabIndex = 9;
             this.splitter1.TabStop = false;
@@ -871,7 +872,7 @@
             // 
             this.splitter2.Dock = System.Windows.Forms.DockStyle.Right;
-            this.splitter2.Location = new System.Drawing.Point(938, 24);
+            this.splitter2.Location = new System.Drawing.Point(993, 24);
             this.splitter2.Name = "splitter2";
-            this.splitter2.Size = new System.Drawing.Size(3, 334);
+            this.splitter2.Size = new System.Drawing.Size(3, 369);
             this.splitter2.TabIndex = 10;
             this.splitter2.TabStop = false;
@@ -901,5 +902,5 @@
             this.calendarGrid1.Resources = ((System.Collections.ArrayList)(resources.GetObject("calendarGrid1.Resources")));
             this.calendarGrid1.SelectedAppointment = 0;
-            this.calendarGrid1.Size = new System.Drawing.Size(802, 310);
+            this.calendarGrid1.Size = new System.Drawing.Size(857, 345);
             this.calendarGrid1.StartDate = new System.DateTime(2003, 1, 27, 0, 0, 0, 0);
             this.calendarGrid1.TabIndex = 0;
@@ -914,5 +915,5 @@
             // 
             this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
-            this.ClientSize = new System.Drawing.Size(1069, 358);
+            this.ClientSize = new System.Drawing.Size(1124, 393);
             this.Controls.Add(this.panelCenter);
             this.Controls.Add(this.panelBottom);
@@ -946,5 +947,4 @@
 		private CGDocumentManager	m_DocManager;
 		private int					m_nSlots;
-		bool						bSchedulesClicked = false;
 		private ArrayList			m_alSelectedTreeResourceArray = new ArrayList();
 		private string				m_sDocName;
@@ -1360,28 +1360,31 @@
 		}
 
-		void UpdateStatusBar(DateTime dStart, DateTime dEnd, string sAccessType, string sAvailabilityMessage)
-		{
-			string sMsg =  dStart.ToShortTimeString() + " to " + dEnd.ToShortTimeString();
-			if (m_nSlots > 0)
-			{
-				sMsg = sMsg + ": " + m_nSlots.ToString() + " slot";
-				sMsg = sMsg + ((m_nSlots > 1)?"s " : " ");
-				sMsg = sMsg + "available";
-				if (sAccessType != "")
-				{
-					sMsg = sMsg + " for " + sAccessType;
-				}
-				sMsg = sMsg + ".";
-				if (sAvailabilityMessage != "")
-				{
-					sMsg = sMsg + "  Note: " + sAvailabilityMessage;
-				}
-			}
+		void UpdateStatusBar(DateTime dStart, DateTime dEnd, CGAvailability av)
+		{
+            System.Text.StringBuilder sbMsg = new System.Text.StringBuilder(100);
+		    sbMsg.Append(dStart.ToShortTimeString() + " to " + dEnd.ToShortTimeString());
+			if (av != null && m_nSlots > 0)
+			{
+                sbMsg.Append(String.Format(" has {0} slot(s) available for {1}. ", m_nSlots.ToString(), av.AccessTypeName));
+            }
 			else
 			{
-				sMsg += ": No appointment slots available.";
-			}
-
-			this.statusBar1.Text = sMsg;
+				sbMsg.Append(": No appointment slots available. ");
+			}
+
+            if (av != null)
+            {
+                sbMsg.Append(String.Format("Source Block: {0} to {1} with {2} slot(s) of type {3}",
+                    av.StartTime.ToShortTimeString(),
+                    av.EndTime.ToShortTimeString(),
+                    av.Slots.ToString(),
+                    av.AccessTypeName));
+
+                sbMsg.Append(". ");
+
+                if (av.Note.Trim().Length > 0) sbMsg.Append("Note: " + av.Note + ".");
+            }
+
+            this.statusBar1.Text = sbMsg.ToString();
 		}
 
@@ -1475,4 +1478,5 @@
 				v.Activate();
 				v.dateTimePicker1.Value = dDate;
+                v.RequestRefreshGrid();
                 return;
 			}
@@ -1507,5 +1511,5 @@
             try
 			{
-				doc.OnOpenDocument();
+				doc.OnOpenDocument(dDate);
 			}
 				
@@ -1638,5 +1642,5 @@
 			try
 			{
-				doc.OnOpenDocument();
+				doc.OnOpenDocument(DateTime.Today);
 			}
 			catch (Exception ex)
@@ -2046,7 +2050,7 @@
                 //SMH: Takes too long to do.
 				//this.Document.RefreshDocument();
-				string sAccessType = "";
-				string sAvailabilityMessage = "";
-				m_nSlots = m_Document.SlotsAvailable(dStart, dEnd, sResource, out sAccessType, out sAvailabilityMessage);
+                CGAvailability resultantAvail;
+
+				m_nSlots = m_Document.SlotsAvailable(dStart, dEnd, sResource, this.calendarGrid1.TimeScale, out resultantAvail);
 
 				if (m_nSlots < 1)
@@ -2155,14 +2159,8 @@
 				}
 				
-				TimeSpan tsDuration = dEnd - dStart;
-				int nDuration = (int) tsDuration.TotalMinutes;
-				Debug.Assert(nDuration > 0);
-
-
                 //Sam: takes too long. Remove this call; deal with the issue of concurrent appointments another way.
                 //this.Document.RefreshDocument();
-				string sAccessType = "";
-				string sAvailabilityMessage = "";
-				m_nSlots = m_Document.SlotsAvailable(dStart, dEnd, sResource, out sAccessType, out sAvailabilityMessage);
+                CGAvailability resultantAvail;
+                m_nSlots = m_Document.SlotsAvailable(dStart, dEnd, sResource, this.calendarGrid1.TimeScale, out resultantAvail);
 
 				if (m_nSlots < 1)
@@ -2192,5 +2190,5 @@
 				dAppt.DocManager = this.m_DocManager;
 				string sNote = "";
-				dAppt.InitializePage(dPat.PatientIEN, dStart, nDuration, sResource, sNote);
+                dAppt.InitializePage(dPat.PatientIEN, dStart, dEnd, sResource, sNote, nAccessTypeID);
 
 				if (dAppt.ShowDialog(this) == DialogResult.Cancel)
@@ -2199,5 +2197,8 @@
 				}
 
-                CGAppointment appt = new CGAppointment();
+                CGAppointment appt = dAppt.Appointment;
+                    
+                // old way of making an appointment
+                    /*new CGAppointment();
 				appt.PatientID = Convert.ToInt32(dPat.PatientIEN);
 				appt.PatientName = dPat.PatientName;
@@ -2208,7 +2209,16 @@
 				appt.HealthRecordNumber = dPat.HealthRecordNumber;
 				appt.AccessTypeID = nAccessTypeID;
+                    */
 
 				//Call Document to add a new appointment. Document adds appointment to CGAppointments array.
 				this.Document.CreateAppointment(appt);
+
+                //Experimental now.
+                if (dAppt.PrintAppointmentSlip)
+                {
+                    System.Drawing.Printing.PrintDocument pd = new System.Drawing.Printing.PrintDocument();
+                    pd.PrintPage += (object s, System.Drawing.Printing.PrintPageEventArgs e) => CGDocumentManager.Current.PrintingObject.PrintAppointmentSlip(appt, e);
+                    pd.Print();
+                }
 
                 //Show the new set of appointments by calling UpdateArrays. Fetches Document's CGAppointments
@@ -2505,4 +2515,15 @@
                 calendarGrid1.Focus();
         }
+
+        /// <summary>
+        /// If mouse enters the Tree Section, check if the grid is on the active form first before stealing the focus
+        /// </summary>
+        /// <param name="sender"></param>
+        /// <param name="e"></param>
+        private void tvSchedules_MouseEnter(object sender, EventArgs e)
+        {
+            if (GetActiveWindow() == this.Handle)
+                tvSchedules.Focus();
+        }
         
         private void CGView_Load(object sender, System.EventArgs e)
@@ -2520,4 +2541,7 @@
             //Show the Form
             this.Activate();
+
+            //Set focus on the calendar grid
+            this.calendarGrid1.Focus();
 		}
 
@@ -2592,8 +2616,5 @@
 		}
 
-		private void tvSchedules_Click(object sender, System.EventArgs e)
-		{
-			bSchedulesClicked = true;
-		}
+
 
 		private void tvSchedules_DoubleClick(object sender, System.EventArgs e)
@@ -2636,10 +2657,7 @@
         }
 
+
 		private void tvSchedules_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
-		{
-			if (bSchedulesClicked == false)
-				return;
-			bSchedulesClicked = false;
-			
+		{	
 			m_alSelectedTreeResourceArray = new ArrayList();
 			string sResource = e.Node.FullPath;
@@ -2662,4 +2680,20 @@
 		}
 
+        /// <summary>
+        /// Makes sure that the node gets selected no matter where we click.
+        /// Incidentally, Invokes AfterSelect event.
+        /// </summary>
+        /// <param name="sender"></param>
+        /// <param name="e"></param>
+        private void tvSchedules_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
+        {
+            e.Node.TreeView.SelectedNode = e.Node;
+        }
+
+        /// <summary>
+        /// Useless code now... Good place to test something.
+        /// </summary>
+        /// <param name="sender"></param>
+        /// <param name="e"></param>
 		private void mnuTest1_Click(object sender, System.EventArgs e)
 		{
@@ -2718,8 +2752,7 @@
 		private void calendarGrid1_CGSelectionChanged(object sender, IndianHealthService.ClinicalScheduling.CGSelectionChangedArgs e)
 		{
-			string sAccessType = "";
-			string sAvailabilityMessage = "";
-			m_nSlots = m_Document.SlotsAvailable(e.StartTime, e.EndTime, e.Resource, out sAccessType, out sAvailabilityMessage);
-			UpdateStatusBar(e.StartTime, e.EndTime, sAccessType, sAvailabilityMessage);
+            CGAvailability resultantAvail;
+            m_nSlots = m_Document.SlotsAvailable(e.StartTime, e.EndTime, e.Resource, this.calendarGrid1.TimeScale, out resultantAvail);
+			UpdateStatusBar(e.StartTime, e.EndTime, resultantAvail);
 		}
 
@@ -2758,6 +2791,4 @@
 
 				//20040909 Cherokee Replaced this block with following
-				string sAccessType = "";
-				string sAvailabilityMessage = "";
 				//				if (m_Document.SlotsAvailable(e.StartTime, e.EndTime, e.Resource, out sAccessType, out sAvailabilityMessage) < 1)
 				//				{
@@ -2775,5 +2806,6 @@
 					bModSchedule =  (bool) this.m_htModifySchedule[e.Resource.ToString()];
 				}
-				bool bSlotsAvailable = (m_Document.SlotsAvailable(e.StartTime, e.EndTime, e.Resource, out sAccessType, out sAvailabilityMessage) > 0);
+                CGAvailability resultantAvail;
+                bool bSlotsAvailable = (m_Document.SlotsAvailable(e.StartTime, e.EndTime, e.Resource, this.calendarGrid1.TimeScale, out resultantAvail) > 0);
 				if (!((bSlotsAvailable) || (bModSchedule) || (bOverbook) ))
 				{
@@ -2966,6 +2998,4 @@
 			try
 			{
-				string sAccessType = "";
-				string sAvailabilityMessage = "";
 				bool	bSlotsAvailable;
 				bool	bOverbook;
@@ -2990,5 +3020,7 @@
 				bOverbook = (bool) this.m_htOverbook[e.Resource.ToString()];
 				bModSchedule =  (bool) this.m_htModifySchedule[e.Resource.ToString()];
-				bSlotsAvailable = (m_Document.SlotsAvailable(e.StartTime, e.EndTime, e.Resource, out sAccessType, out sAvailabilityMessage) > 0);
+                CGAvailability resultantAvail;
+
+                bSlotsAvailable = (m_Document.SlotsAvailable(e.StartTime, e.EndTime, e.Resource, this.calendarGrid1.TimeScale, out resultantAvail) > 0);
 
 				if (!((bSlotsAvailable) || (bModSchedule) || (bOverbook) ))
@@ -3339,4 +3371,8 @@
         }
 
+
+
+
+
         
 
