Index: Scheduling/trunk/cs/bsdx0200GUISourceCode/CGDocument.cs
===================================================================
--- Scheduling/trunk/cs/bsdx0200GUISourceCode/CGDocument.cs	(revision 1073)
+++ Scheduling/trunk/cs/bsdx0200GUISourceCode/CGDocument.cs	(revision 1083)
@@ -272,7 +272,10 @@
         //sam: This is a test that duplicates RefreshDocument, but without the UpdateAllViews,
         // as that has to be done synchornously.
-        //XXXXXX: Needs to be refactored obviously, but now for testing.
+        //XXX: Needs to be refactored obviously, but now for testing.
+        //XXX: Tested extensively enough. Less refactoring now. 2011-01-26
         public void RefreshDocumentAsync()
         {
+            Debug.WriteLine("IN REFERSH DOCUMENT ASYNC\n\n");
+
             bool bRet = false;
             if (m_sResourcesArray.Count == 0)
@@ -423,6 +426,4 @@
                     m_DocManager.ConnectInfo.LoadConnectInfo();
                 }
-
-                m_pAvArray.Clear();
 
                 ArrayList saryApptTypes = new ArrayList();
@@ -464,57 +465,65 @@
                 string sAccessRuleList;
 
-
-                foreach (DataRow rTemp in rAvailabilitySchedule.Rows)
-                {
-                    //get StartTime, EndTime and Slots 
-                    dStart = (DateTime)rTemp["START_TIME"];
-                    dEnd = (DateTime)rTemp["END_TIME"];
-
-                    //TODO: Fix this slots datatype problem
-                    string sSlots = rTemp["SLOTS"].ToString();
-                    nSlots = Convert.ToInt16(sSlots);
-
-                    sResourceList = rTemp["RESOURCE"].ToString();
-                    sAccessRuleList = rTemp["ACCESS_TYPE"].ToString();
-
-                    string sNote = rTemp["NOTE"].ToString();
-
-                    if ((nSlots < -1000) || (sAccessRuleList == ""))
+                //smh: moved clear availabilities down here.
+                //smh: Temporary solution to make sure that people don't touch the availability table at the same time!!!
+                //NOTE: This lock makes sure that availabilities aren't queried for slots when the array is an intermediate
+                //state. The other place that has this lock is SlotsAvailable function.
+                lock (this.m_pAvArray)
+                {
+                    m_pAvArray.Clear();
+
+                    foreach (DataRow rTemp in rAvailabilitySchedule.Rows)
                     {
-                        nApptTypeID = 0;
-                    }
-                    else
-                    {
-                        foreach (DataRow rType in rTypeSchedule.Rows)
+                        //get StartTime, EndTime and Slots 
+                        dStart = (DateTime)rTemp["START_TIME"];
+                        dEnd = (DateTime)rTemp["END_TIME"];
+
+                        //TODO: Fix this slots datatype problem
+                        string sSlots = rTemp["SLOTS"].ToString();
+                        nSlots = Convert.ToInt16(sSlots);
+
+                        sResourceList = rTemp["RESOURCE"].ToString();
+                        sAccessRuleList = rTemp["ACCESS_TYPE"].ToString();
+
+                        string sNote = rTemp["NOTE"].ToString();
+
+                        if ((nSlots < -1000) || (sAccessRuleList == ""))
                         {
-
-                            dTypeStart = (DateTime)rType["StartTime"];
-                            dTypeEnd = (DateTime)rType["EndTime"];
-                            //if start & end times overlap, then
-                            string sTypeResource = rType["ResourceName"].ToString();
-                            if ((dTypeStart.DayOfYear == dStart.DayOfYear) && (sResourceList == sTypeResource))
+                            nApptTypeID = 0;
+                        }
+                        else
+                        {
+                            foreach (DataRow rType in rTypeSchedule.Rows)
                             {
-                                crRectA.Y = GetTotalMinutes(dStart);
-                                crRectA.Height = GetTotalMinutes(dEnd) - crRectA.Top;
-                                crRectB.Y = GetTotalMinutes(dTypeStart);
-                                crRectB.Height = GetTotalMinutes(dTypeEnd) - crRectB.Top;
-                                bIsect = crRectA.IntersectsWith(crRectB);
-                                if (bIsect == true)
+
+                                dTypeStart = (DateTime)rType["StartTime"];
+                                dTypeEnd = (DateTime)rType["EndTime"];
+                                //if start & end times overlap, then
+                                string sTypeResource = rType["ResourceName"].ToString();
+                                if ((dTypeStart.DayOfYear == dStart.DayOfYear) && (sResourceList == sTypeResource))
                                 {
-                                    //TODO: This code:
-                                    //	nApptTypeID = (int) rType["AppointmentTypeID"];
-                                    //Causes this exception:
-                                    //Unhandled Exception: System.InvalidCastException: Specified cast is not valid.
-                                    string sTemp = rType["AppointmentTypeID"].ToString();
-                                    nApptTypeID = Convert.ToInt16(sTemp);
-                                    break;
+                                    crRectA.Y = GetTotalMinutes(dStart);
+                                    crRectA.Height = GetTotalMinutes(dEnd) - crRectA.Top;
+                                    crRectB.Y = GetTotalMinutes(dTypeStart);
+                                    crRectB.Height = GetTotalMinutes(dTypeEnd) - crRectB.Top;
+                                    bIsect = crRectA.IntersectsWith(crRectB);
+                                    if (bIsect == true)
+                                    {
+                                        //TODO: This code:
+                                        //	nApptTypeID = (int) rType["AppointmentTypeID"];
+                                        //Causes this exception:
+                                        //Unhandled Exception: System.InvalidCastException: Specified cast is not valid.
+                                        string sTemp = rType["AppointmentTypeID"].ToString();
+                                        nApptTypeID = Convert.ToInt16(sTemp);
+                                        break;
+                                    }
                                 }
-                            }
-                        }//end foreach datarow rType
-                    }
-
-                    AddAvailability(dStart, dEnd, nApptTypeID, nSlots, false, sResourceList, sAccessRuleList, sNote);
-                }//end foreach datarow rTemp
-
+                            }//end foreach datarow rType
+                        }
+
+
+                        AddAvailability(dStart, dEnd, nApptTypeID, nSlots, false, sResourceList, sAccessRuleList, sNote);
+                    }//end foreach datarow rTemp
+                }//end lock
                 return true;
             }
@@ -531,4 +540,16 @@
         }
 
+        /// <summary>
+        /// Adds Availability to Availability Array held by document
+        /// </summary>
+        /// <param name="StartTime">Self-Explan</param>
+        /// <param name="EndTime">Self-Explan</param>
+        /// <param name="nType"></param>
+        /// <param name="nSlots"></param>
+        /// <param name="UpdateView"></param>
+        /// <param name="sResourceList"></param>
+        /// <param name="sAccessRuleList"></param>
+        /// <param name="sNote"></param>
+        /// <returns></returns>
         public int AddAvailability(DateTime StartTime, DateTime EndTime, int nType, int nSlots, bool UpdateView, string sResourceList, string sAccessRuleList, string sNote)
         {
@@ -579,11 +600,4 @@
             //TODO:  Test that resource is not currently in list, that it IS a resource, etc
             this.m_sResourcesArray.Add(sResource);
-            //SAM: removing: Remove UpdateAllViews: Redraws all the open views. But does not call server.
-            //this.UpdateAllViews();
-        }
-
-        public void ClearResources()
-        {
-            this.m_sResourcesArray.Clear();
         }
 
@@ -604,37 +618,51 @@
             crRectB.Height = GetTotalMinutes(dSelEnd) - crRectB.Y;
 
-            //			//loop thru m_pAvArray
-            //			//Compare the start time and end time of eachblock
-            while (i < m_pAvArray.Count)
-            {
-                pAv = (CGAvailability)m_pAvArray[i];
-                dStart = pAv.StartTime;
-                dEnd = pAv.EndTime;
-                if ((sResource == pAv.ResourceList) &&
-                    ((dSelStart.Date == dStart.Date) || (dSelStart.Date == dEnd.Date)))
-                {
-                    crRectA.Y = (dStart.Date < dSelStart.Date) ? 0 : GetTotalMinutes(dStart);
-                    crRectA.Height = (dEnd.Date > dSelEnd.Date) ? 1440 : GetTotalMinutes(dEnd);
-                    crRectA.Height = crRectA.Height - crRectA.Y;
-                    bIsect = crRectA.IntersectsWith(crRectB);
-                    if (bIsect != false)
+            //NOTE: What's this lock? This lock makes sure that nobody is editing the availability array
+            //when we are looking at it. Since the availability array could potentially be updated on
+            //a different thread, we are can be potentially left stuck with an empty array.
+            //
+            //The other place that uses this lock is the RefershAvailabilitySchedule method
+            //
+            //This is a temporary fix until I figure out how to divorce the availbilities here from those drawn
+            //on the calendar. Appointments are cloned b/c they are in an object that supports that; and b/c I
+            //don't need to suddenly query them at runtime like I do with Availabilities.
+
+            lock (this.m_pAvArray)
+            {
+                //loop thru m_pAvArray
+                //Compare the start time and end time of eachblock
+                while (i < m_pAvArray.Count)
+                {
+                    pAv = (CGAvailability)m_pAvArray[i];
+                    dStart = pAv.StartTime;
+                    dEnd = pAv.EndTime;
+                    if ((sResource == pAv.ResourceList) &&
+                        ((dSelStart.Date == dStart.Date) || (dSelStart.Date == dEnd.Date)))
                     {
-                        nSlots = pAv.Slots;
-                        if (nSlots < 1)
+                        crRectA.Y = (dStart.Date < dSelStart.Date) ? 0 : GetTotalMinutes(dStart);
+                        crRectA.Height = (dEnd.Date > dSelEnd.Date) ? 1440 : GetTotalMinutes(dEnd);
+                        crRectA.Height = crRectA.Height - crRectA.Y;
+                        bIsect = crRectA.IntersectsWith(crRectB);
+                        if (bIsect != false)
                         {
-                            nAvailableSlots = 0;
-                            break;
-                        }
-                        if (nSlots < nAvailableSlots)
-                        {
-                            nAvailableSlots = nSlots;
-                            sAccessType = pAv.AccessTypeName;
-                            sAvailabilityMessage = pAv.Note;
-
-                        }
-                    }
-                }
-                i++;
-            }
+                            nSlots = pAv.Slots;
+                            if (nSlots < 1)
+                            {
+                                nAvailableSlots = 0;
+                                break;
+                            }
+                            if (nSlots < nAvailableSlots)
+                            {
+                                nAvailableSlots = nSlots;
+                                sAccessType = pAv.AccessTypeName;
+                                sAvailabilityMessage = pAv.Note;
+
+                            }
+                        }//end if
+                    }//end if
+                    i++;
+                }//end while
+            }//end lock
+
             if (nAvailableSlots == 999)
             {
@@ -759,4 +787,5 @@
             aCopy.HealthRecordNumber = rApptInfo.HealthRecordNumber;
             aCopy.AccessTypeID = rApptInfo.AccessTypeID;
+            aCopy.WalkIn = bWalkin ? true : false;
 
             string sSql = "BSDX ADD NEW APPOINTMENT^" + sStart + "^" + sEnd + "^" + sPatID + "^" + sResource + "^" + sLen + "^" + sNote + "^" + sApptID;
@@ -773,8 +802,12 @@
             aCopy.AppointmentKey = nApptID;
             this.m_appointments.AddAppointment(aCopy);
-
-            bool bRet = RefreshAvailabilitySchedule();
-
-            UpdateAllViews();
+            
+            
+            //Have make appointment from CGView responsible for requesting an update for the avialability.
+            //bool bRet = RefreshAvailabilitySchedule();
+
+            //Sam: don't think this is needed as it is called from CGView.
+            //Make CGView responsible for all drawing.
+            //UpdateAllViews();
 
             return nApptID;
@@ -822,9 +855,5 @@
             string sErrorID = r["ERRORID"].ToString();
 
-            if (this.m_appointments.AppointmentTable.ContainsKey(nApptID))
-            {
-                bool bRet = RefreshSchedule();
-                UpdateAllViews();
-            }
+
         }
 
