// Zijn er vertragingen voor deze gebruiker
    // op zijn opgeslagen route's?
    public string getDelays()
    {
        IList<BLLfavorites> routes = leesbareRoutes();

        // Zoek naar vertragingen
        Api api = new Api();

        foreach (var route in routes)
        {
            api.startStation = route.startStation;
            api.eindStation = route.eindStation;
        }

        XDocument doc = null;
        //Api irail = new Api();
        doc = api.getApiData("connections", "NL", "xml");

        Int16 test = 0;
        Boolean delayFound = false;
        IList<int> delays = null;

        int j = 0;

        foreach (var conn in doc.Elements("connections").Elements("connection"))
        {
            test = Convert.ToInt16(conn.Element("departure").Attribute("delay").Value);
            //delays.Add(Convert.ToInt16(conn.Element("departure").Attribute("delay").Value));
            //var test2 = conn.Element("departure").Attribute("delay").Value;
            if (test > 0)
            {
                delayFound = true;
            }
            j++;
        }

        if (delayFound == true)
        {
            return "Er zijn vertragingen gevonden";
        }
        else
        {
            return "Er zijn geen vertragingen gevonden";
        }
    }
    public string getStationName(string statID)
    {
        // Haal de stationslijst op
        XDocument doc = null;
        Api irail = new Api();
        doc = irail.getApiData("stations", "NL", "xml");

        // Zoek de naam van het station met het gegeven id
        List<Station> stations = (from stationss in doc.Elements("stations").Elements("station")
                                  where stationss.Attribute("id").Value == statID
                                  select new Station
                                  {
                                      stationNaam = stationss.Value,
                                      stationId =  stationss.Attribute("id").Value
                                  }).ToList();

        // Geef de naam van het station terug
        return stations[0].stationNaam;
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["longitude"] == null || Session["latitude"] == null)
        {
            if (Request.Form["txtLng"] == null || Request.Form["txtLat"] == null)
            {
                Response.Redirect("~/requestLocation.aspx");
            }
            else
            {
                Session["longitude"] = Request.Form["txtLng"];
                Session["latitude"] = Request.Form["txtLat"];
            }
        }

        double myX = 0;
        double myY = 0;

        try
        {
            myX = Double.Parse(Session["longitude"].ToString(), System.Globalization.CultureInfo.GetCultureInfo("en-us"));
            myY = Double.Parse(Session["latitude"].ToString(), System.Globalization.CultureInfo.GetCultureInfo("en-us"));
        }
        catch (Exception)
        {
            Response.Redirect("~/overview.aspx");
        }

        GLatLng p1 = new GLatLng();

        p1.lat = myY;
        p1.lng = myX;

        GMap1.setCenter(p1, 15);

        GMap1.addGMapUI(new GMapUI());

        GMap1.Height = 400;
        GMap1.Width = 800;

        GMapUIOptions options = new GMapUIOptions();
        options.maptypes_hybrid = true;
        options.keyboard = true;
        options.maptypes_physical = true;
        options.zoom_scrollwheel = true;

        GIcon icoon = new GIcon();
        icoon.image = "img/userLocation.png";
        icoon.iconSize = new GSize(32, 32);
        icoon.shadowSize = new GSize(60, 32);

        GMarker marker1 = new GMarker(p1, icoon);
        GMap1.addGMarker(marker1);

        GInfoWindow userWindow = new GInfoWindow();
        userWindow.gMarker = marker1;
        userWindow.html = "<h3>U bevindt zich hier</h3>";
        GMap1.addInfoWindow(userWindow);

        Api api = new Api();
        XDocument doc = api.getApiData("stations/", "NL", "xml");

        foreach (XElement d in doc.Descendants("stations").Descendants("station"))
        {
            string lat = d.Attribute("locationY").Value;
            string lng = d.Attribute("locationX").Value;

            double y = Double.Parse(lat, System.Globalization.CultureInfo.GetCultureInfo("en-us"));
            double x = Double.Parse(lng, System.Globalization.CultureInfo.GetCultureInfo("en-us"));

            double distance = GetDistanceBetweenPoints(myX, myY, x, y);

            Station s = new Station(d.Value.ToString(), y, x, distance);
            stations.Add(s);
        }

        var stationsQueryAble = stations.AsQueryable();
        stationsQueryAble = stationsQueryAble.OrderBy(stat => stat.afstandTotHuidigeLocatie);

        stations = stationsQueryAble.ToList();

        for (int i = 0; i < 5; i++)
        {
            GLatLng p2 = new GLatLng();
            p2.lat = stations[i].locY;
            p2.lng = stations[i].locX;
            GMarker marker = new GMarker(p2);
            GMap1.addGMarker(marker);

            GInfoWindow win1 = new GInfoWindow();

            win1.gMarker = marker;
            win1.html = "<h3><a href='overview.aspx?station=" + stations[i].stationNaam + "'>" + stations[i].stationNaam + " station</a></h3><p>afstand: " + Math.Round(stations[i].afstandTotHuidigeLocatie, 2) + "km</p>";
            GMap1.addInfoWindow(win1);
        }
    }
    protected void getList(string station)
    {
        try
        {
            string startStation = station;

            // XML ophalen
            XDocument doc = null;
            Api irail = new Api();
            doc = irail.getApiData("liveboard/?station=" + startStation + "&fast=true", "NL", "xml");

            // List leegmaken (eventuele data van vorige request zit er nog in)
            departures.Clear();

            foreach (XElement d in doc.Descendants("departure"))
            {
                // Vertraging parsen naar datetimes
                string strDelay = d.Attribute("delay").Value;

                if (strDelay == "cancel")
                {
                    strDelay = "0";
                }

                double dblDelay = Convert.ToDouble(strDelay);
                DateTime dtDelay = new DateTime();

                // Jaar op 1900 zetten, anders klaagt sql bij insert (jaar mag niet 0 zijn)
                dtDelay = dtDelay.AddYears(1900);
                dtDelay = dtDelay.AddSeconds(dblDelay);

                // Tijd parsen naar datetime
                XElement timeNode = d.Element("time");
                string strTime = timeNode.Attribute("formatted").Value;

                DateTime time = DateTime.Parse(strTime);
                // ToUniversalTime (anders wordt er 1u bijgeteld, zomer-winteruur?)
                time = time.ToUniversalTime();

                // Andere data opvangen
                string destinationStation = d.Element("station").Value;
                string platform = d.Element("platform").Value;
                string vehicle = d.Element("vehicle").Value;

                // Nieuw object maken met data
                Departure departure = new Departure(startStation, destinationStation, platform, vehicle, time, dtDelay);

                // Toevoegen aan list
                departures.Add(departure);
            }

            // Data tegen repeater smijten
            rptDepartures.DataSource = departures;
            rptDepartures.DataBind();
            overviewPanel.Visible = true;
        }
        catch (Exception ex)
        {
            feedback.Attributes["class"] = "alert-message block-message";
            lblFeedback.Text = "Er is een fout opgetreden bij het laden van het overzicht.";
        }
    }