public static Items GetTasksById(int id, string XmlFile)
        {
            Items item = new Items();

            using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (store.FileExists(XmlFile))
                {
                    //store.DeleteFile(XmlFile);
                    //XDocument doc = XDocument.Load(store.OpenFile(XmlFile, FileMode.Open));
                    using (var sr = new StreamReader(new IsolatedStorageFileStream(XmlFile, FileMode.Open, store)))
                    {
                        XDocument doc = XDocument.Load(sr);

                        foreach (XElement ele in doc.Descendants("task"))
                        {

                            if (ele != null && int.Parse(ele.Element("Id").Value) == id)
                            {
                                item.Id = int.Parse(ele.Element("Id").Value);
                                item.Text = (string)ele.Element("Text").Value;
                                item.User_Id = int.Parse(ele.Element("User_Id").Value);
                                item.shared = ele.Element("shared").Value == "true" ? true : false;
                                item.complete = ele.Element("complete").Value == "true" ? true : false;
                                break;
                            }
                        }

                    }


                }
            }
            return item;
        }
        private async void CheckNetworkAvailability()
        {
            online = NetworkInterface.NetworkInterfaceType.ToString()!="None";
 
            if (online)
            {
                MobileService = new MobileServiceClient(
                     "https://shopappdata.azure-mobile.net/",
                       "dkwwuiuHYYQwbozjKaWRJYYpEiTjFt73"
                );
                itemTable = MobileService.GetTable<Items>();
                
                buffer = XmlTaskService.GetTasks("buffer.xml");
                foreach (Items buffitem in buffer)
                {
                    try
                    {
                        await itemTable.InsertAsync(buffitem);
                        XmlTaskService.CreateTask(buffitem, "tasks.xml");
                    }
                    catch
                    { }
                }
                XmlTaskService.DeleteAllTasks("buffer.xml");
                buffer.Clear();

                buffer = XmlTaskService.GetTasks("checked.xml");
                foreach (Items buffitem in buffer)
                {
                    Debug.WriteLine("Item is "); Debug.WriteLine(buffitem.Text) ;
                    checked_item = XmlTaskService.GetTasksByText(buffitem.Text, "tasks.xml");
                    checked_item.shared = true;
                    Debug.WriteLine("checked"); Debug.WriteLine(checked_item.Text);
                    try
                    {
                        await itemTable.UpdateAsync(checked_item);
                    }
                    catch
                    { }
                }
                XmlTaskService.DeleteAllTasks("checked.xml");
                buffer.Clear();

                buffer = XmlTaskService.GetTasks("delete.xml");
                foreach (Items buffitem in buffer)
                {
                    checked_item = XmlTaskService.GetTasksByText(buffitem.Text, "tasks.xml");
                    try
                    {
                        await itemTable.DeleteAsync(checked_item);
                    }
                    catch
                    { }
                }
                XmlTaskService.DeleteAllTasks("delete.xml");
                buffer.Clear();
            }
        }
        private async void DeleteItem(object sender, System.Windows.Input.GestureEventArgs e)
        {
            string itemName = "";
            Items item = new Items();
            
            foreach (var objects in ((Grid)((Button)sender).Parent).Children)
            {
                if (objects is TextBlock)
                {
                    itemName = ((TextBlock)objects).Text;
                }
            }

            var parent = (ListBox)((Grid)((Button)sender).Parent).Parent;
            parent.Items.Remove(((Grid)((Button)sender).Parent));

            if (online)
            {
                item = XmlTaskService.GetTasksByText(itemName, "tasks.xml");
                await itemTable.DeleteAsync(item);
                XmlTaskService.DeleteTask(itemName, "tasks.xml");
                RefreshTodoItems();
            }
            else
            {
                item = XmlTaskService.GetTasksByText(itemName, "tasks.xml");
                XmlTaskService.CreateTask(item, "delete.xml");
            }
            // TODO: Use item id to remove the item form database
        }
        private async void ShareItemWithFriends(object sender, System.Windows.Input.GestureEventArgs e)
        {
            string itemName="";

            foreach (var objects in ((Grid)((Button)sender).Parent).Children) {
                if (objects is TextBlock)
                {
                    itemName = ((TextBlock)objects).Text;
                }
            }

            var solidBrush = new SolidColorBrush(Colors.Green);
            ((Button)sender).BorderBrush = solidBrush;
            ((Button)sender).Tap -= ShareItemWithFriends;

            Items item = new Items();
            
            if (online)
            {
                item = XmlTaskService.GetTasksByText(itemName, "tasks.xml");
                XmlTaskService.DeleteTask(item.Text, "tasks.xml");

                item.shared = true;
                XmlTaskService.CreateTask(item, "tasks.xml");

                await itemTable.UpdateAsync(item);

                item = XmlTaskService.GetTasksByText(itemName, "tasks.xml");
            }
            else
            {
                item = XmlTaskService.GetTasksByText(itemName, "tasks.xml");
                item.Text = itemName;
                XmlTaskService.CreateTask(item, "checked.xml");
            }
        	// TODO: Use item id to set the share flag to true in the database
        }
        public static bool CreateTask(Items t, string XmlFile)
        {

            var doc = ReadXml(XmlFile);
            try
            {
                if (doc.Element("tasks") != null)
                {
                    //this is an append
                    doc.Element("tasks").Add(
                        new XElement("task",
                                     new XElement("Text", t.Text),
                                     new XElement("shared", t.shared),
                                     new XElement("Id", t.Id),
                                     new XElement("User_Id", t.User_Id),
                                     new XElement("complete", "false")
                            ));
                    WriteXml(doc, XmlFile);
                }
                else
                {
                    doc.Add(
                           new XElement("tasks",
                                   new XElement("task",
                                         new XElement("Text", t.Text),
                                         new XElement("shared", "false"),
                                         new XElement("Id", t.Id),
                                         new XElement("User_Id", t.User_Id),
                                         new XElement("complete", "false")
                                   )
                      ));
                    WriteXml(doc, XmlFile);
                }
            }
            catch (Exception e)
            {
                return false;
            }
            return true;
        }
        public static void UpdateId(int id, Items item, string XmlFile)
        {


            using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (store.FileExists(XmlFile))
                {
                    //store.DeleteFile(XmlFile);
                    //XDocument doc = XDocument.Load(store.OpenFile(XmlFile, FileMode.Open));
                    using (var sr = new StreamReader(new IsolatedStorageFileStream(XmlFile, FileMode.Open, store)))
                    {
                        XDocument doc = XDocument.Load(sr);

                        foreach (var ele in doc.Descendants())
                        {
                            if (ele != null && (ele.Element("task").Element("Text").Value) == item.Text)
                            {
                                ele.Element("task").Element("Id").SetValue(id.ToString());
                                break;
                            }

                        }
                    }


                }
            }


        }