private bool OpenLocalCoupon(SportCoupon coupon, bool xmlFileDownloaded, XmlDocument eventData)
        {
            try
            {
                OC.Downloading = true;                
                eventData.Load(OC.FilePath + @"\" + coupon.Stem);
                
                OC.Downloading = false;
                xmlFileDownloaded = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to open " + coupon.Stem + " From " + OC.FilePath + "    \n" + ex);
                OC.Downloading = false;
            }

            return xmlFileDownloaded;
        }
        private bool DownloadCoupon(SportCoupon coupon, bool xmlFileDownloaded, XmlDocument eventData)
        {
            try
            {
                OC.Downloading = true;
                eventData.Load(OC.FileURL + coupon.Stem); 
                OC.Downloading = false;
                xmlFileDownloaded = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to download " + coupon.Stem + " Coupon from website, check if website is online \n \n \n" + ex);
                OC.Downloading = false;
            }

            return xmlFileDownloaded;
        }
        private void PopulateEventGroup(SportCoupon coupon, XmlNode _eventGroupNode)
        {
            EventGroup eventGroup = new EventGroup();
            eventGroup.Id = Int32.Parse(_eventGroupNode.Attributes["id"].InnerText);
            eventGroup.Name = _eventGroupNode.Attributes["name"].InnerText;

            // Populate Events
            foreach (XmlNode _eventNode in _eventGroupNode)
            {
                PopulateEvent(eventGroup, _eventNode);
            }

            App.Current.Dispatcher.Invoke((Action)delegate
            {
                // if EventGroup isn't on coupon, add it
                if (coupon.EventGroups.Count < eventGroup.Id + 1)
                {
                    coupon.EventGroups.Add(eventGroup);
                    AddStatusUpdate("New Event downloaded. Adding to " + coupon.Name + " coupon", eventGroup.Name);

                }

                // Else update the current eventgroup on the coupon
                else
                {
                    AddStatusUpdate("Update Found for " + coupon.Name, eventGroup.Name);
                    coupon.EventGroups[eventGroup.Id] = eventGroup;

                }

                RefreshCouponsView();
                coupon.LastDownload = DateTime.Now;

            });
        }
        private void UpdateCoupon(SportCoupon coupon, XmlNode eventFile)
        {            
            coupon.LastModified = DateTime.Parse(eventFile.Attributes["lastmod"].InnerText);

            // Take note of the selected Event before recreating all coupons
            if (SelectedEvent != null && SelectedEventGroup != null && SelectedCoupon != null)
            {
                selectedEventGroupid = selectedEventGroup.Id;
                selectedEventid = SelectedEvent.Id;
                selectedCouponId = SelectedCoupon.Id;
            }            
                    
            // Populate each eventgroup on the coupon
            foreach (XmlNode _eventGroupNode in eventFile)
            {
                PopulateEventGroup(coupon, _eventGroupNode);
            }

            RefreshCouponsView();
        }
 private static void DownloadCopyOfXml(SportCoupon coupon, XmlDocument eventData)
 {
     try
     {
         String path = "C:\\OddsChecker\\" + DateTime.Today.Date.ToString().Replace('/', '-').Substring(0, 10) + "\\";
         bool exists = System.IO.Directory.Exists(path);
         if (!exists)
             System.IO.Directory.CreateDirectory(path);
         eventData.Save(path + coupon.Stem + ".xml");
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.ToString());
     }
     
 }
        private void CheckForCouponUpdates(SportCoupon coupon)
        {
            if (coupon.EventGroups.Count < 1)
                AddStatusUpdate("Downloading Coupon",coupon.Stem); 
           
            bool xmlFileDownloadSuccessfully = false;
            XmlDocument eventData = new XmlDocument();

            // if Online mode

            if (OC.OnlineMode)
                        xmlFileDownloadSuccessfully = DownloadCoupon(coupon, xmlFileDownloadSuccessfully, eventData);
            else
                        xmlFileDownloadSuccessfully = OpenLocalCoupon(coupon, xmlFileDownloadSuccessfully, eventData);
                   
            

            if (xmlFileDownloadSuccessfully && eventData.DocumentElement.SelectSingleNode("/data").HasChildNodes)
            {
                    XmlNode XMLEventFileNode = eventData.DocumentElement.SelectSingleNode("/data/eventfile");
                    DateTime xmlLastUpdated = DateTime.Parse(XMLEventFileNode.Attributes["lastmod"].InnerText);

                    if (coupon.LastModified < DateTime.Parse("01/01/1000"))
                    {
                        if (OC.OnlineMode)
                        {
                            coupon.Id = Int32.Parse(XMLEventFileNode.Attributes["id"].InnerText);
                            DownloadCopyOfXml(coupon, eventData);
                        }
                        else
                            coupon.Id = (int)OC.CurrentCoupon - 1;
    
                        
                        coupon.Name = XMLEventFileNode.Attributes["name"].InnerText;
                        UpdateCoupon(coupon, XMLEventFileNode);
                    }
                    else
                    {
                        if (xmlLastUpdated > coupon.LastModified)
                        {
                            AddStatusUpdate("New prices found, Downloading.", coupon.Name);
                            UpdateCoupon(coupon, XMLEventFileNode);
                            DownloadCopyOfXml(coupon, eventData);
                        }
                        else
                        {
                            AddStatusUpdate("File Skipped, no new updates", coupon.Name);
                        }

                    }          
                }
                else
                {
                    AddStatusUpdate("Error Downloading coupon or Coupon has no data", coupon.Name);
                }
          
            
        }