/// <summary>
 /// Lock the screen and request authentication from the user
 /// </summary>
 /// <param name="reason"></param>
 /// <param name="feature"></param>
 public ReAuthForm(string reason, string feature)
 {
     Reason = reason;
     Feature = feature;
     canceled = false;
     InitializeComponent();
     auth = false;
     SendToOwner = false;
     hwnd = this.Handle;
     _thread2 = new Thread(threadFocus);
     _thread2.IsBackground = true;
     _thread2.Start();
     //_thread = new Thread(hookthread);
     //_thread.IsBackground = true;
     //_thread.Start();
     SetTaskManager(false);
     DisableKeyCombos();
     this.Bounds = Screen.PrimaryScreen.Bounds;
     WindowState = FormWindowState.Maximized;
     panel1.Location = new Point(this.Width / 2 - panel1.Width / 2, this.Height / 2 - panel1.Height / 2);
     SetForegroundWindow(hwnd);
     s = UserConfigurations.MessagingAddress;
     PhoneBox.Text = "A verification code has been sent to your mobile phone and registered email account.";
     otp = new oneTimePassword();
     if(reason.Equals("EndOpenTrust"))
     {
         CancelButton.Enabled = false;
         CancelButton.Visible = false;
         submitButton.Size = new System.Drawing.Size(252, 35);
         label1.Text = "To continue constructing your profile\nActive Authentication requires secondary authentication.";
         Messager.SendEmail(UserConfigurations.EmailAddress, Messager.emailSubjectLine, Messager.endOpenTrustEmail + otp.otp);
         Messager.SendTextMessageTo(s, Messager.endOpenTrustText + otp.otp);
     }
     else if(reason.Equals("Failed"))
     {
         CancelButton.Enabled = false;
         CancelButton.Visible = false;
         submitButton.Size = new System.Drawing.Size(252, 35);
         Messager.SendTextMessageTo(s, Messager.standardTextMessage + otp.otp);
         Messager.SendEmail(UserConfigurations.EmailAddress, Messager.emailSubjectLine, Messager.standardEmailMessage + otp.otp);
     }
     else if(reason.Equals("ProtectedFeature"))
     {
         label1.Text = "This feature requires secondary authentication.";
         Messager.SendTextMessageTo(s, Messager.protectedFeatureTextpt1 + feature + Messager.protectedFeatureTextpt2 + otp.otp);
         Messager.SendEmail(UserConfigurations.EmailAddress, Messager.emailSubjectLine, Messager.protectedFeatureEmailpt1 + feature + Messager.protectedFeatureEmailpt2 + otp.otp);
     }
     else if(reason.Equals("NewKeyboard"))
     {
         label1.Text = "New Keyboard Detected.  As a security precaution secondary authentication is required to continue using this machine.";
         CancelButton.Enabled = false;
         CancelButton.Visible = false;
         submitButton.Size = new System.Drawing.Size(252, 35);
         Messager.SendTextMessageTo(s, Messager.newKeyboardText + otp.otp);
         Messager.SendEmail(UserConfigurations.EmailAddress, Messager.emailSubjectLine, Messager.newKeyboardEmail + otp.otp);
     }
     else // Should never get here
     {
         Messager.SendTextMessageTo(s, "" + otp.otp);
         Messager.SendEmail(UserConfigurations.EmailAddress, Messager.emailSubjectLine, "" + otp.otp);
     }
     #if !DEBUG
     HideAllOthers();
     #endif
     if(AADesktopClient.internetAvailable == false)
     {
         internetNotification.Visible = true;
     }
     NetworkChange.NetworkAvailabilityChanged += new NetworkAvailabilityChangedEventHandler(NetworkAvailabilityChange);
 }
 /// <summary>
 /// Send the one time password
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void reSendButton_Click(object sender, EventArgs e)
 {
     if (otp.IsExpired())
         otp = new oneTimePassword();
     if (SendToOwner)
     {
         if (Reason.Equals("NewUser"))
         {
             Messager.SendEmail(UserConfigurations.OwnerEmail, Messager.emailSubjectLine, Messager.newUserEmail + otp.otp);
             Messager.SendTextMessageTo(s, Messager.newUserText + otp.otp);
         }
         else if (Reason.Equals("OwnerFeature"))
         {
             Messager.SendEmail(UserConfigurations.OwnerEmail, Messager.emailSubjectLine, Messager.ownerFeatureEmailpt1 + Feature + Messager.ownerFeatureEmailpt2 + otp.otp);
             Messager.SendTextMessageTo(s, Messager.ownerFeatureTextpt1 + Feature + Messager.ownerFeatureTextpt2 + otp.otp);
         }
         else if (Reason.Equals("Uninstall"))
         {
             Messager.SendEmail(UserConfigurations.OwnerEmail, Messager.emailSubjectLine, Messager.uninstallEmail + otp.otp);
             Messager.SendTextMessageTo(s, Messager.uninstallText + otp.otp);
         }
         else
         {
             Messager.SendTextMessageTo(s, otp.otp);
             Messager.SendEmail(UserConfigurations.OwnerEmail, "Active Authentication Code", "Your new code is: " + otp.otp);
         }
     }
     else
     {
         if (Reason.Equals("EndOpenTrust"))
         {
             Messager.SendEmail(UserConfigurations.EmailAddress, Messager.emailSubjectLine, Messager.endOpenTrustEmail + otp.otp);
             Messager.SendTextMessageTo(s, Messager.endOpenTrustText + otp.otp);
         }
         else if (Reason.Equals("Failed"))
         {
             Messager.SendTextMessageTo(s, Messager.standardTextMessage + otp.otp);
             Messager.SendEmail(UserConfigurations.EmailAddress, Messager.emailSubjectLine, Messager.standardEmailMessage + otp.otp);
         }
         else if (Reason.Equals("ProtectedFeature"))
         {
             Messager.SendTextMessageTo(s, Messager.protectedFeatureTextpt1 + Feature + Messager.protectedFeatureTextpt2 + otp.otp);
             Messager.SendEmail(UserConfigurations.EmailAddress, Messager.emailSubjectLine, Messager.protectedFeatureEmailpt1 + Feature + Messager.protectedFeatureEmailpt2 + otp.otp);
         }
         else if (Reason.Equals("NewKeyboard"))
         {
             Messager.SendTextMessageTo(s, Messager.newKeyboardText + otp.otp);
             Messager.SendEmail(UserConfigurations.EmailAddress, Messager.emailSubjectLine, Messager.newKeyboardEmail + otp.otp);
         }
         else
         {
             Messager.SendTextMessageTo(s, "" + otp.otp);
             Messager.SendEmail(UserConfigurations.EmailAddress, Messager.emailSubjectLine, "" + otp.otp);
         }
     }
 }
        /// <summary>
        /// Check if the password recieved matches the one that was sent
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void submitButton_Click(object sender, EventArgs e)
        {
            submitButton.Enabled = false;
            progressBar1.Value = 0;
            #if DEBUG
            if (codeBox.Text == "")
            {
                auth = true;
                backgroundWorkerFocus.Dispose();
                #if !DEBUG
                ShowAllOthers();
                #endif
                EnableKeyCombos();
                this.Close();
            }
            #endif
            if (!otp.IsExpired())
            {
                for (int i = 1; i < 100; i++)
                {
                    progressBar1.Value = i;
                    progressBar1.Value = i-1;
                    Thread.Sleep(15);
                }
                if (otp.PasswordMatch(codeBox.Text))
                {

                    auth = true;
                    running = false;
                    _thread2.Join();
                    #if !DEBUG
                    ShowAllOthers();
                    #endif
                    if (!Uninstalling)
                        SetTaskManager(true);
                    EnableKeyCombos();
                    this.Close();

                }
            }
            else
            {
                otp = new oneTimePassword();
                if (SendToOwner)
                {
                    Messager.SendTextMessageTo(UserConfigurations.OwnerMessagingAddress, otp.otp);
                    Messager.SendEmail(UserConfigurations.OwnerEmail, "Your computer has been locked", "If this was you, please use this code to authenticate yourself with the code below \n" + otp.otp);
                }
                else
                {
                    Messager.SendTextMessageTo(UserConfigurations.MessagingAddress, otp.otp);
                    Messager.SendEmail(UserConfigurations.EmailAddress, "Your account has been locked", "If this was you, please use this code to authenticate yourself with the code below \n" + otp.otp);
                }
                MessageBox.Show("Code Expired, Re-sending...");
            }
            progressBar1.Value = 0;
            submitButton.Enabled = true;
        }
 /// <summary>
 /// Lock the screen and request authentication from the owner
 /// </summary>
 /// <param name="ownerAuth"></param>
 /// <param name="reason"></param>
 /// <param name="feature"></param>
 public ReAuthForm(bool ownerAuth, string reason, string feature)
 {
     Reason = reason;
     Feature = feature;
     canceled = false;
     InitializeComponent();
     auth = false;
     hwnd = this.Handle;
     _thread2 = new Thread(threadFocus);
     _thread2.IsBackground = true;
     _thread2.Start();
     DisableKeyCombos();
     this.Bounds = Screen.PrimaryScreen.Bounds;
     WindowState = FormWindowState.Maximized;
     panel1.Location = new Point(this.Width / 2 - panel1.Width / 2, this.Height / 2 - panel1.Height / 2);
     SetForegroundWindow(hwnd);
     String s = UserConfigurations.OwnerMessagingAddress;
     PhoneBox.Text = "A verification code has been sent to your mobile phone and registered email account.";
     otp = new oneTimePassword();
     if(reason.Equals("NewUser"))
     {
         SetTaskManager(false);
         PhoneBox.Visible = false;
         label1.Size = new System.Drawing.Size(280, 125);
         label1.Font = new Font(label1.Font.Name, 9);
         label1.Text = "To continue using this account on this machine you must register with Active Authentication. "
             + "To procede with registration please contact the owner of this machine and request the verification code. "
             + "Once you have the code please enter it below and press submit.";
         CancelButton.Enabled = false;
         CancelButton.Visible = false;
         submitButton.Size = new System.Drawing.Size(252, 35);
         Messager.SendEmail(UserConfigurations.OwnerEmail, Messager.emailSubjectLine, Messager.newUserEmail + otp.otp);
         Messager.SendTextMessageTo(s, Messager.newUserText + otp.otp);
     }
     else if (reason.Equals("OwnerFeature"))
     {
         SetTaskManager(false);
         label1.Text = "This feature requires secondary authentication.";
         Messager.SendEmail(UserConfigurations.OwnerEmail, Messager.emailSubjectLine, Messager.ownerFeatureEmailpt1 + feature + Messager.ownerFeatureEmailpt2 + otp.otp);
         Messager.SendTextMessageTo(s, Messager.ownerFeatureTextpt1 + feature + Messager.ownerFeatureTextpt2 + otp.otp);
     }
     else if(reason.Equals("Uninstall"))
     {
         running = false;
         label1.Text = "This feature requires secondary authentication.";
         Uninstalling = true;
         Messager.SendEmail(UserConfigurations.OwnerEmail, Messager.emailSubjectLine, Messager.uninstallEmail + otp.otp);
         Messager.SendTextMessageTo(s, Messager.uninstallText + otp.otp);
     }
     else // Should never get here
     {
         SetTaskManager(false);
         Messager.SendTextMessageTo(s, otp.otp);
         Messager.SendEmail(UserConfigurations.OwnerEmail, "Active Authentication Code", "Your new code is: " + otp.otp);
     }
     SendToOwner = true;
 }