//Method that logs the user into the application
        private void btnLogin_Click(object sender, EventArgs e)
        {
            ShowLoading(true);
            string userEmail = tbEmail.Text;
            string password  = tbPassword.Text;

            //Validates the email address format
            if (!IsValidEmail(userEmail))
            {
                ShowLoading(false);
                MessageBox.Show("Invalid email has been entered. Please make sure your email format is correct e.g [email protected]");
            }
            //Correct email format has been entered
            else
            {
                AsyncCallback <BackendlessUser> callback = new AsyncCallback <BackendlessUser>(
                    user =>
                {
                    OwnerStorage.CurrentlyLoggedIn = user;
                    String whereClause             = "ownerId = '" + user.ObjectId + "'";
                    BackendlessAPI.Persistence.DataQueryBuilder queryBuilder = BackendlessAPI.Persistence.DataQueryBuilder.Create();
                    queryBuilder.SetWhereClause(whereClause);

                    AsyncCallback <IList <Restaurant> > getRestaurantCallback = new AsyncCallback <IList <Restaurant> >(
                        foundRestaurant =>
                    {
                        //Checks if restaurants exist under the logged in user
                        if (foundRestaurant.Count != 0)
                        {
                            List <Restaurant> tempList = (List <Restaurant>)foundRestaurant;
                            //Checks if the user manages multiple restaurants and if true, prompts the user to select a restaurant to sign into
                            if (tempList.Count > 1)
                            {
                                SelectRestaurantForm selectForm = new SelectRestaurantForm(tempList);
                                selectForm.ShowDialog();
                                OwnerStorage.ThisRestaurant = selectForm.selected;
                            }
                            //The user only manages one restaurant - Logs the user into their registered restaurant
                            else
                            {
                                OwnerStorage.ThisRestaurant = tempList[0];
                            }
                            Invoke(new Action(() =>
                            {
                                DialogResult = DialogResult.OK;
                                this.Close();
                            }));
                        }
                        //No restaurants were found under the entered login credentials
                        else
                        {
                            Invoke(new Action(() =>
                            {
                                ShowLoading(false);
                                MessageBox.Show(this, "No Restaurant was located under these login credentials. If " +
                                                "you are a new user who only recently created a Tablefind Profile " +
                                                "then it may be that we are still setting up your profile for a restaurant " +
                                                "on our side. If this issue is not resolved within one week, please contact " +
                                                "a member of our development team to assist with restaurant registeration");
                            }));
                        }
                    },
                        error =>
                    {
                        //Something went wrong. An error message will now be displayed
                        //Runs visual aspects on a new thread because you cannot alter visual aspects on any thread other than the GUI thread
                        Invoke(new Action(() =>
                        {
                            ShowLoading(false);
                            MessageBox.Show(this, "Error: " + error.Message);
                        }));
                    });

                    //Executes the callback
                    Backendless.Data.Of <Restaurant>().Find(queryBuilder, getRestaurantCallback);
                },
                    fault =>
                {
                    //Something went wrong. An error message will now be displayed
                    //Runs visual aspects on a new thread because you cannot alter visual aspects on any thread other than the GUI thread
                    Invoke(new Action(() =>
                    {
                        ShowLoading(false);
                        MessageBox.Show(this, "Error: " + fault.Message);
                    }));
                });

                //Signs the user into the application and keeps the user logged in if the 'Stay Signed In' checkbox is checked
                Backendless.UserService.Login(userEmail, password, callback, cbxStaySignedIn.Checked ? true : false);
            }
        }
        public SignInForm()
        {
            InitializeComponent();
            ShowLoading(true);

            AsyncCallback <Boolean> callback = new AsyncCallback <Boolean>(
                isValidLogin =>
            {
                string loggedInUserObjectId = Backendless.UserService.LoggedInUserObjectId();
                //Checks if the user chose to stay logged in
                if (loggedInUserObjectId != null)
                {
                    AsyncCallback <BackendlessUser> findCallBack = new AsyncCallback <BackendlessUser>(
                        success =>
                    {
                        OwnerStorage.CurrentlyLoggedIn = success;
                        String whereClause             = "ownerId = '" + success.ObjectId + "'";
                        BackendlessAPI.Persistence.DataQueryBuilder queryBuilder = BackendlessAPI.Persistence.DataQueryBuilder.Create();
                        queryBuilder.SetWhereClause(whereClause);

                        AsyncCallback <IList <Restaurant> > getRestaurantCallback = new AsyncCallback <IList <Restaurant> >(
                            foundRestaurant =>
                        {
                            //Checks if restaurants exist under the logged in user
                            if (foundRestaurant.Count != 0)
                            {
                                List <Restaurant> tempList = (List <Restaurant>)foundRestaurant;
                                //Checks if the user manages multiple restaurants and if true, prompts the user to select a restaurant to sign into
                                if (tempList.Count > 1)
                                {
                                    SelectRestaurantForm selectForm = new SelectRestaurantForm(tempList);
                                    selectForm.ShowDialog();
                                    OwnerStorage.ThisRestaurant = selectForm.selected;
                                }
                                //The user only manages one restaurant - Logs the user into their registered restaurant
                                else
                                {
                                    OwnerStorage.ThisRestaurant = tempList[0];
                                }
                                Invoke(new Action(() =>
                                {
                                    DialogResult = DialogResult.OK;
                                    this.Close();
                                }));
                            }
                            //No restaurants were found under the entered login credentials
                            else
                            {
                                Invoke(new Action(() =>
                                {
                                    ShowLoading(false);
                                    MessageBox.Show(this, "No Restaurant was located under these login credentials. If " +
                                                    "you are a new user who only recently created a Tablefind Profile " +
                                                    "then it may be that we are still setting up your profile for a restaurant " +
                                                    "on our side. If this issue is not resolved within one week, please contact us.");
                                }));
                            }
                        },
                            error =>
                        {
                            //Something went wrong. An error message will now be displayed
                            //Runs visual aspects on a new thread because you cannot alter visual aspects on any thread other than the GUI thread
                            Invoke(new Action(() =>
                            {
                                ShowLoading(false);
                                MessageBox.Show(this, "Error: " + error.Message);
                            }));
                        });

                        Backendless.Data.Of <Restaurant>().Find(queryBuilder, getRestaurantCallback);
                    },
                        fault =>
                    {
                        //Something went wrong. An error message will now be displayed
                        //Runs visual aspects on a new thread because you cannot alter visual aspects on any thread other than the GUI thread
                        AsyncCallback <Object> logoutCallback = new AsyncCallback <Object>(
                            user =>
                        {
                        },
                            logoutfault =>
                        {
                        });
                        Backendless.UserService.Logout(logoutCallback);

                        Invoke(new Action(() =>
                        {
                            ShowLoading(false);
                        }));
                    });
                    //Retrieves the user object
                    Backendless.Data.Of <BackendlessUser>().FindById(loggedInUserObjectId, findCallBack);
                }
                //User chose to not stay signed in
                else
                {
                    ShowLoading(false);
                }
            },
                fault =>
            {
                //Something went wrong. An error message will now be displayed
                //Runs visual aspects on a new thread because you cannot alter visual aspects on any thread other than the GUI thread
                Invoke(new Action(() =>
                {
                    ShowLoading(false);
                }));
            });

            //Executes the callback
            Backendless.UserService.IsValidLogin(callback);

            //Displays textbox field hints
            SendMessage(tbEmail.Handle, 0x1501, 1, "  Please enter E-mail");
            SendMessage(tbPassword.Handle, 0x1501, 1, "  Please enter password");
            SendMessage(tbEmailAddress.Handle, 0x1501, 1, "  Enter E-mail address");
            SendMessage(tbPass.Handle, 0x1501, 1, "  Enter password");
            SendMessage(tbConfirmPass.Handle, 0x1501, 1, "  Confirm password");
            SendMessage(tbFirstName.Handle, 0x1501, 1, "  First name");
            SendMessage(tbLastName.Handle, 0x1501, 1, "  Last name");
            SendMessage(tbContactNumber.Handle, 0x1501, 1, "  Mobile Number");
        }