partial void LoginButton_TouchUpInside(UIButton sender)
        {
            View.EndEditing(true);

            String userId    = UserIDTextView.Text.Trim();
            String password  = PasswordTextView.Text;
            String dataToken = DataTokenTextView.Text.Trim();

            Boolean dataReady = userId.Length * password.Length * dataToken.Length != 0;

            if (!dataReady)
            {
                new UIAlertView("Invalid Data", "Please make sure all blanks are filled before continue", null, "OK", null).Show();
                return;
            }

            if (!CrossConnectivity.Current.ConnectionTypes.Contains(ConnectionType.Cellular) &&
                !CrossConnectivity.Current.ConnectionTypes.Contains(ConnectionType.WiFi))
            {
                new UIAlertView("Connection Unavailable", "Please enable cellular data or WiFi and try again.", null, "OK", null).Show();
                return;
            }

            String baseUrl;
            String dataSet;

            try
            {
                _resolver.ResolveFromToken(dataToken, out baseUrl, out dataSet);
            }
            catch (Exception e)
            {
                new UIAlertView("Invalid DataToken", "DataToken not recognized", null, "OK", null).Show();
                return;
            }

            System.Net.HttpWebResponse resp;
            try
            {
                var req = System.Net.WebRequest.CreateHttp(baseUrl + "api/v1/datasets/" + dataSet + "/");
                req.Method            = "GET";
                req.AllowAutoRedirect = false;
                String credential = userId + ":" + password;
                req.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(credential)));
                resp = (System.Net.HttpWebResponse)req.GetResponse();
            }
            catch (System.Net.WebException e)
            {
                resp = (System.Net.HttpWebResponse)e.Response;
                if (resp == null)
                {
                    new UIAlertView("Server Unavailable", "Cannot reach server \"" + baseUrl + "\". " + e.Message, null, "OK", null).Show();
                }
                else if (resp.StatusCode == System.Net.HttpStatusCode.Unauthorized)
                {
                    new UIAlertView("Bad Credentials", "Your user ID or password is not correct.", null, "OK", null).Show();
                }
                else if (resp.StatusCode == System.Net.HttpStatusCode.Forbidden)
                {
                    new UIAlertView("Permission Denied", "You may not have the permission to connect to the given dataset", null, "OK", null).Show();
                }
                else
                {
                    new UIAlertView("Server Not Recognized", "Server \"" + baseUrl + "\" is not recognized. " + e.Message, null, "OK", null).Show();
                }
                return;
            }

            if (resp.StatusCode == System.Net.HttpStatusCode.OK)
            {
                if (resp.GetResponseStream().CanRead)
                {
                    byte[] buffer = new byte[10001];
                    resp.GetResponseStream().Read(buffer, 0, 10000);
                    String responseStr = System.Text.Encoding.UTF8.GetString(buffer, 0, buffer.Length);
                    if (responseStr.Contains("auth-required"))
                    {
                        new UIAlertView("Wrong Credentials", "Your user ID or password is not correct", null, "OK", null).Show();
                        return;
                    }
                    else if (responseStr.Contains("permission-denied"))
                    {
                        new UIAlertView("Permission Denied", "You may not have the permission to connect to the given dataset", null, "OK", null).Show();
                        return;
                    }
                    else if (responseStr.Contains("dataset"))
                    {
                        AccountStorage.Set(userId, password, baseUrl, dataSet, dataToken);
                        PDashAPI.Controller.RefreshDataset();
                        if (OnLoginSuccess != null)
                        {
                            OnLoginSuccess(sender, new EventArgs());
                        }
                    }
                    else
                    {
                        new UIAlertView("Server Not Recognized", "Server \"" + baseUrl + "\" is not recognized.", null, "OK", null).Show();
                        return;
                    }
                }
                else
                {
                    new UIAlertView("Error", "Comunication with the server failed. Status code: " + resp.StatusCode + ", " + resp.StatusDescription, null, "OK", null).Show();
                    return;
                }
            }
            else
            {
                new UIAlertView("Invalid Data", "Server is not recognized. Please check the DataToken", null, "OK", null).Show();
                return;
            }
        }
        public async Task <int> CheckCredentials(string datatoken, string userid, string password)
        {
            //Check username and password
            System.Diagnostics.Debug.WriteLine("We are inside the outer task");
            ProgressDialog pd = new ProgressDialog(Activity);

            pd.SetMessage("Checking username and password");
            pd.SetCancelable(false);
            pd.Show();
            AlertDialog.Builder builder = new AlertDialog.Builder((Activity));
            await Task.Run(() =>
            {
                Debug.WriteLine("We are checking username");
                HttpWebResponse resp;
                try
                {
                    DataSetLocationResolver dslr = new DataSetLocationResolver();
                    dslr.ResolveFromToken(datatoken, out baseurl, out dataset);

                    System.Diagnostics.Debug.WriteLine("Base url :" + baseurl);

                    AccountStorage.SetContext(Activity);
                    AccountStorage.Set(userid, password, baseurl, dataset);


                    var req =
                        WebRequest.CreateHttp(AccountStorage.BaseUrl + "api/v1/datasets/" +
                                              AccountStorage.DataSet + "/");
                    req.Method            = "GET";
                    req.AllowAutoRedirect = false;
                    string credential     = userid + ":" + password;
                    req.Headers.Add("Authorization",
                                    "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(credential)));
                    // req.Get

                    resp = (HttpWebResponse)req.GetResponse();

                    if (resp.StatusCode == HttpStatusCode.OK)
                    {
                        if (resp.GetResponseStream().CanRead)
                        {
                            Stream data = resp.GetResponseStream();
                            var reader  = new StreamReader(data);

                            string responseStr = reader.ReadToEnd();
                            Debug.WriteLine(responseStr);

                            if (responseStr.Contains("auth-required"))
                            {
                                Debug.WriteLine("Wrong credentials 2");
                                AccountStorage.ClearStorage();
                                Activity.RunOnUiThread(() =>
                                {
                                    if (pd.IsShowing)
                                    {
                                        pd.Dismiss();
                                    }

                                    builder.SetTitle("Wrong Credentials")
                                    .SetMessage("Please check your username and password and try again.")
                                    .SetNeutralButton("Okay", (sender2, args2) => { builder.Dispose(); })
                                    .SetCancelable(false);
                                    AlertDialog alert = builder.Create();
                                    alert.Show();
                                    Debug.WriteLine("We should have shown the dialog now");
                                });
                            }
                            else if (responseStr.Contains("permission-denied"))
                            {
                                Debug.WriteLine("permission issue");
                                AccountStorage.ClearStorage();
                                Activity.RunOnUiThread(() =>
                                {
                                    if (pd.IsShowing)
                                    {
                                        pd.Dismiss();
                                    }

                                    builder.SetTitle("Access Denied")
                                    .SetMessage("You donot have access to this dataset")
                                    .SetNeutralButton("Okay", (sender2, args2) => { builder.Dispose(); })
                                    .SetCancelable(false);
                                    AlertDialog alert = builder.Create();

                                    alert.Show();
                                });
                            }
                            else if (responseStr.Contains("dataset"))
                            {
                                Debug.WriteLine("Username and password was correct");
                                Activity.RunOnUiThread(() =>
                                {
                                    pd.SetMessage("Getting Account Info");
                                    pd.SetCancelable(false);
                                    if (!pd.IsShowing)
                                    {
                                        pd.Show();
                                    }
                                });
                                Task.Run(() =>
                                {
                                    //LOAD METHOD TO GET ACCOUNT INFO


                                    Debug.WriteLine("We are going to store the values");

                                    Debug.WriteLine("We have stored the values");
                                    Debug.WriteLine(AccountStorage.BaseUrl);
                                    Debug.WriteLine(AccountStorage.DataSet);
                                    Debug.WriteLine(AccountStorage.Password);
                                    Debug.WriteLine(AccountStorage.UserId);

                                    // Switch to next screen

                                    //HIDE PROGRESS DIALOG
                                    Activity.RunOnUiThread(() =>
                                    {
                                        if (pd.IsShowing)
                                        {
                                            pd.Dismiss();
                                        }

                                        Toast.MakeText(Activity, "Logged in", ToastLength.Short).Show();
                                        ((MainActivity)Activity).FragmentManager.PopBackStack();
                                        ((MainActivity)Activity).SetDrawerState(true);
                                        ((MainActivity)Activity).SwitchToFragment(
                                            MainActivity.FragmentTypes.Home);
                                    });
                                });
                            }
                        }
                    }
                }
                catch (WebException e)
                {
                    Debug.WriteLine("We have a problem");
                    Activity.RunOnUiThread(() =>
                    {
                        if (pd.IsShowing)
                        {
                            pd.Dismiss();
                        }
                    });
                    using (WebResponse response = e.Response)
                    {
                        HttpWebResponse httpResponse = (HttpWebResponse)response;
                        Console.WriteLine("Error code: {0}", httpResponse.StatusCode);

                        if (httpResponse.StatusCode == HttpStatusCode.Unauthorized)
                        {
                            Debug.WriteLine("Wrong credentials");
                            AccountStorage.ClearStorage();
                            Activity.RunOnUiThread(() =>
                            {
                                try
                                {
                                    builder.SetTitle("Unauthorized")
                                    .SetMessage(
                                        "Please check your username and password and data token and try again.")
                                    .SetNeutralButton("Okay", (sender2, args2) => { builder.Dispose(); })
                                    .SetCancelable(false);

                                    AlertDialog alert = builder.Create();

                                    alert.Show();
                                }
                                catch (Exception e2)
                                {
                                    Debug.WriteLine("We have hit an error while showing the dialog :" + e2.Message);
                                    AccountStorage.ClearStorage();
                                }
                            });
                        }
                    }
                }


                catch (Exception e)
                {
                    // Catching any generic exception
                    Debug.WriteLine("We have hit a generic exception :" + e.Message);
                    AccountStorage.ClearStorage();
                    Activity.RunOnUiThread(() =>
                    {
                        AlertDialog.Builder builder2 = new AlertDialog.Builder(Activity);
                        builder2.SetTitle("Error occured")
                        .SetMessage(e.Message +
                                    ". Please report this error to the developers. We are sorry for the inconvenience.")
                        .SetNeutralButton("Okay", (sender2, args2) => { builder2.Dispose(); })
                        .SetCancelable(false);
                        AlertDialog alert2 = builder2.Create();
                        alert2.Show();
                    });
                }

                return(true);
            });

            //    pd.Dismiss();

            System.Diagnostics.Debug.WriteLine("We are done with the outer task");
            return(0);
        }