/// <summary>
		/// Gets the previous visits.
		/// </summary>
		/// <param name="rvItemId">The rv item id.</param>
		/// <param name="so">The Sort Order.</param>
		/// <returns>RvPreviousVisitData[].</returns>
		public static RvPreviousVisitData[] GetPreviousVisits(int rvItemId, SortOrder so)
		{
			var rvVists = new List<RvPreviousVisitData>();
			using (var db = new RvPreviousVisitsContext(RvPreviousVisitsContext.DBConnectionString)) {
				IOrderedQueryable<RvPreviousVisitItem> qry = from x in db.RvPreviousVisitItems
															 where x.RvItemId == rvItemId
															 orderby x.Date
															 select x;

				if (qry.Any()) {
					IEnumerable<RvPreviousVisitItem> visits = so == SortOrder.DateNewestToOldest ? qry.ToArray().Reverse() : qry.ToArray();
					foreach (RvPreviousVisitItem v in visits) {
						rvVists.Add(RvPreviousVisitData.Copy(v));
					}
					return rvVists.ToArray();
				}
				return new RvPreviousVisitData[0];
			}
		}
		/// <summary>
		/// Checks the database and creates it if it doesnt exist.
		/// </summary>
		public static void CheckDatabase()
		{
			using (var db = new RvPreviousVisitsContext(RvPreviousVisitsContext.DBConnectionString)) {
                                if (db.DatabaseExists() == false) {
                                        db.CreateDatabase();
                                        DatabaseSchemaUpdater dbUpdater = db.CreateDatabaseSchemaUpdater();
                                        dbUpdater.DatabaseSchemaVersion = APP_VERSION;
                                        dbUpdater.Execute();
                                } else {
                                        var dbUpdater = db.CreateDatabaseSchemaUpdater();
                                        if (dbUpdater.DatabaseSchemaVersion < 2)  //update from 1.0 to 2.0 db version
		                        {
                                                dbUpdater.AddColumn<RvPreviousVisitItem>("Tracts");
                                                dbUpdater.DatabaseSchemaVersion = APP_VERSION;
                                                dbUpdater.Execute();
                                        }
                                }


			}
		}
 // Formerly the function known as GetReturnVisitByLastVisitDate
 public static int[] NoLongerInUse(SortOrder so, int maxReturnCount = 8)
 {
     try {
         using (var visitDb = new RvPreviousVisitsContext(RvPreviousVisitsContext.DBConnectionString)) {
             if (so != SortOrder.DateOldestToNewest) return new int[0];
             var qry = from x in visitDb.RvPreviousVisitItems
                 orderby x.Date
                 group x by x.RvItemId into dates
                 select dates;
             if (maxReturnCount == -1) maxReturnCount = qry.Count();
             if (qry.Any()) {
                 var rvList = new List<RvPreviousVisitItem>();
                 foreach (var rv in qry) {
                     var r = so == SortOrder.DateOldestToNewest ? rv.Last() : rv.First();
                     if (rvList.Count() == maxReturnCount) {
                         if (so == SortOrder.DateOldestToNewest && rvList.Select(x => x.Date > r.Date).Any()) {
                             rvList.Remove(rvList.Last());
                             rvList.Add(r);
                             rvList = rvList.OrderBy(s => s.Date).ToList();
                         } else if (rvList.Select(x => x.Date < r.Date).Any()) {
                             rvList.Remove(rvList.First());
                             rvList.Add(r);
                             rvList = rvList.OrderBy(s => s.Date).ToList();
                         }
                     } else {
                         rvList.Add(r);
                         rvList = rvList.OrderBy(s => s.Date).ToList();
                     }
                 }
                 return rvList.Select(r => r.RvItemId).ToArray();
             }
             //if (qry.Any()) {
             //	var rvs = new List<RvPreviousVisitItem>();
             //	int id = qry.First().RvItemId;
             //	rvs.Add(qry.First());
             //	foreach (var r in qry) {
             //		if (id != r.RvItemId) {
             //			id = r.RvItemId;
             //			rvs.Add(r);
             //		}
             //	}
             //	if (maxReturnCount < 0) maxReturnCount = rvs.Count();
             //	var sorted = rvs.OrderBy(s => s.Date).Take(maxReturnCount);
             //	return sorted.Select(i => i.RvItemId).ToArray();
             //}
             return new int[0];
         }
     } catch {
         return null;
     }
 }
 public static bool IsInitialCall(RvPreviousVisitData call)
 {
     //
     using (var db = new RvPreviousVisitsContext(RvPreviousVisitsContext.DBConnectionString)) {
         try {
             var qry = from x in db.RvPreviousVisitItems
                       where x.RvItemId == call.RvItemId
                       orderby x.Date
                       select x;
             if (qry.Any()) {
                 if (qry.Count() <= 1) return true;
                 var c = qry.ToArray().First();
                 if (c.ItemId == call.ItemId) return true;
             }
             return false;
         } catch (Exception) {
             return false;
         }
     }
 }
 public static RvPreviousVisitData[] GetCallsByDate(DateTime @from, DateTime tod)
 {
     //
     using (var db = new RvPreviousVisitsContext(RvPreviousVisitsContext.DBConnectionString)) {
         try { //new DateTime(tod.Year, tod.Month, tod.Day, 0,0,0).AddDays(1)
             @from  = new DateTime(@from.Year,@from.Month, @from.Day,0,0,0);
             tod = new DateTime(tod.Year, tod.Month, tod.Day, 0, 0, 0).AddDays(1);
             var calls = from x in db.RvPreviousVisitItems
                         where x.Date >= @from && x.Date < tod
                         select x;
             if (calls.Any()) {
                 return calls.Select(c => RvPreviousVisitData.Copy(c)).ToArray();
             }
         } catch {
             return null;
         }
     }
     return null;
 }
        /// <summary>
        /// Gets the call.
        /// </summary>
        /// <param name="callId">The call id.</param>
        /// <returns>RvPreviousVisitData.</returns>
        /// <exception cref="MyTimeDatabaseLib.RvPreviousVisitNotFoundException">The Call was not found.</exception>
        /// <exception cref="RvPreviousVisitNotFoundException">The Call was not found.</exception>
        public static RvPreviousVisitData GetCall(int callId)
        {
            using (var db = new RvPreviousVisitsContext(RvPreviousVisitsContext.DBConnectionString)) {
                try {
                    RvPreviousVisitItem call = db.RvPreviousVisitItems.Single(s => s.ItemId == callId);

                    return RvPreviousVisitData.Copy(call);
                } catch {
                    throw new RvPreviousVisitNotFoundException("The Call was not found.");
                }
            }
        }
        /// <summary>
        /// Deletes the call.
        /// </summary>
        /// <param name="callId">The call id.</param>
        public static bool DeleteCall(int callId)
        {
            using (var db = new RvPreviousVisitsContext(RvPreviousVisitsContext.DBConnectionString)) {
                try {
                    RvPreviousVisitItem call = db.RvPreviousVisitItems.Single(s => s.ItemId == callId);

                    db.RvPreviousVisitItems.DeleteOnSubmit(call);
                    db.SubmitChanges();
                    return ReturnVisitsInterface.DeleteCallFromRv(call.RvItemId, call.Date);
                } catch { return false; }
            }
        }
        /// <summary>
        /// Deletes all calls from rv.
        /// </summary>
        /// <param name="itemId">The item id.</param>
        public static bool DeleteAllCallsFromRv(int itemId)
        {
            using (var db = new RvPreviousVisitsContext(RvPreviousVisitsContext.DBConnectionString)) {
                try {
                    var calls = from x in db.RvPreviousVisitItems
                                where x.RvItemId == itemId
                                select x;

                    db.RvPreviousVisitItems.DeleteAllOnSubmit(calls);
                    db.SubmitChanges();
                    return true;
                } catch (Exception) { return false; }
            }
        }
        /// <summary>
        /// Saves the call.
        /// </summary>
        /// <param name="call">The call.</param>
        /// <exception cref="System.ArgumentNullException">rvItemId;Rv Item Id can't be null</exception>
        public static bool AddOrUpdateCall(ref RvPreviousVisitData call)
        {
            if (call.RvItemId < 0) throw new ArgumentNullException("call", "Rv Item Id can't be null");
            int itemId = call.ItemId;
            using (var db = new RvPreviousVisitsContext(RvPreviousVisitsContext.DBConnectionString)) {
                if (itemId > 0 && db.RvPreviousVisitItems.Any(s => s.ItemId == itemId)) {
                    RvPreviousVisitItem c = db.RvPreviousVisitItems.Single(s => s.ItemId == itemId);

                    c.RvItemId = call.RvItemId;
                    c.Magazines = call.Magazines;
                    c.Books = call.Books;
                    c.Brochures = call.Brochures;
                    c.Date = call.Date;
                    c.Notes = call.Notes;
                        c.Tracts = call.Tracts;

                    db.SubmitChanges();
                    return c.ItemId > 0 && ReturnVisitsInterface.UpdateLastVisitDate(call.RvItemId, call.Date); // existing call saved.
                }

                var cc = RvPreviousVisitData.Copy(call);

                db.RvPreviousVisitItems.InsertOnSubmit(cc);
                db.SubmitChanges();
                call.ItemId = cc.ItemId;
                return call.ItemId >= 0 && ReturnVisitsInterface.UpdateLastVisitDate(call.RvItemId, call.Date);
            }
        }