/***********************************************************************************************
* Password.cs
*
* Developed by     : SafeNet, Inc
*
* Copyright (C) 2009 SafeNet, Inc. All rights reserved.
*
* Purpose          : Password input form. Source file for Key Passowrds form.

***********************************************************************************************/
//Include name space
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace Sproeval
{
   /// <summary>
   ///  Password input form. SOurce file for Key Passowrds form.
   ///  Here we get key password from user,checks for their valid value and then store in global value 
   ///  For furture refrence if valid.
   /// </summary>
   public class Password : System.Windows.Forms.Form
   {
      // Different resource control declaration here
      private System.Windows.Forms.Label label1;
      private System.Windows.Forms.Label label2;
      private System.Windows.Forms.Label label3;
      private System.Windows.Forms.Button OKBtn;
      private System.Windows.Forms.Button CANCELBtn;
      private System.Windows.Forms.TextBox WP;
      private System.Windows.Forms.TextBox OW1;
      private System.Windows.Forms.TextBox OW2;
      private System.ComponentModel.Container components = null;

      public Password()
      {
         //
         // Required for Windows Form Designer support
         //
         // call to initialize diffrent resources in Form.
         InitializeComponent();
      }

      /// <summary>
      // Distroy Form resource at time of closing the form.
      /// </summary>
      protected override void Dispose(bool disposing)
      {
         if (disposing)
         {
            if (components != null)
            {
               components.Dispose();
            }
         }
         base.Dispose(disposing);
      }

      #region Windows Form Designer generated code
      /// <summary>
      /// Required method for Designer support - do not modify
      /// the contents of this method with the code editor.
      /// </summary>
      private void InitializeComponent()
      {
         System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Password));
         this.OKBtn = new System.Windows.Forms.Button();
         this.WP = new System.Windows.Forms.TextBox();
         this.OW2 = new System.Windows.Forms.TextBox();
         this.OW1 = new System.Windows.Forms.TextBox();
         this.CANCELBtn = new System.Windows.Forms.Button();
         this.label1 = new System.Windows.Forms.Label();
         this.label2 = new System.Windows.Forms.Label();
         this.label3 = new System.Windows.Forms.Label();
         this.SuspendLayout();
         // 
         // OKBtn
         // 
         this.OKBtn.Location = new System.Drawing.Point(64, 128);
         this.OKBtn.Name = "OKBtn";
         this.OKBtn.Size = new System.Drawing.Size(75, 25);
         this.OKBtn.TabIndex = 3;
         this.OKBtn.Text = "OK";
         this.OKBtn.Click += new System.EventHandler(this.OKBtn_Click);
         // 
         // WP
         // 
         this.WP.Location = new System.Drawing.Point(160, 24);
         this.WP.MaxLength = 4;
         this.WP.Name = "WP";
         this.WP.Size = new System.Drawing.Size(64, 20);
         this.WP.TabIndex = 0;
         this.WP.Text = "";
         // 
         // OW2
         // 
         this.OW2.Location = new System.Drawing.Point(160, 94);
         this.OW2.MaxLength = 4;
         this.OW2.Name = "OW2";
         this.OW2.Size = new System.Drawing.Size(64, 20);
         this.OW2.TabIndex = 2;
         this.OW2.Text = "";
         // 
         // OW1
         // 
         this.OW1.Location = new System.Drawing.Point(160, 60);
         this.OW1.MaxLength = 4;
         this.OW1.Name = "OW1";
         this.OW1.Size = new System.Drawing.Size(64, 20);
         this.OW1.TabIndex = 1;
         this.OW1.Text = "";
         // 
         // CANCELBtn
         // 
         this.CANCELBtn.Location = new System.Drawing.Point(149, 128);
         this.CANCELBtn.Name = "CANCELBtn";
         this.CANCELBtn.Size = new System.Drawing.Size(75, 25);
         this.CANCELBtn.TabIndex = 4;
         this.CANCELBtn.Text = "Cancel";
         this.CANCELBtn.Click += new System.EventHandler(this.CANCELBtn_Click);
         // 
         // label1
         // 
         this.label1.Location = new System.Drawing.Point(16, 30);
         this.label1.Name = "label1";
         this.label1.Size = new System.Drawing.Size(100, 15);
         this.label1.TabIndex = 0;
         this.label1.Text = "Write Password";
         // 
         // label2
         // 
         this.label2.Location = new System.Drawing.Point(16, 64);
         this.label2.Name = "label2";
         this.label2.Size = new System.Drawing.Size(120, 15);
         this.label2.TabIndex = 1;
         this.label2.Text = "Overwrite Password 1";
         // 
         // label3
         // 
         this.label3.Location = new System.Drawing.Point(16, 98);
         this.label3.Name = "label3";
         this.label3.Size = new System.Drawing.Size(120, 15);
         this.label3.TabIndex = 2;
         this.label3.Text = "Overwrite Password 2";
         // 
         // Password
         // 
         //this.AutoScale = false;
         this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
         this.ClientSize = new System.Drawing.Size(234, 167);
         this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.OW2,
																		  this.OW1,
																		  this.WP,
																		  this.CANCELBtn,
																		  this.OKBtn,
																		  this.label3,
																		  this.label2,
																		  this.label1});
         this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
         this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
         this.MaximizeBox = false;
         this.MinimizeBox = false;
         this.Name = "Password";
         this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
         this.Text = "Passwords";
         this.Load += new System.EventHandler(this.Password_Load);
         this.ResumeLayout(false);

      }
      #endregion

      /***************************************************************************************
        * Procedure : OKBtn_Click
        *
        * Purpose   : Called on OK button click
        *
        * Descrip   : Gets user inputs for Key password.validate values and if valid then 
        *             store in global variable
        ***************************************************************************************/
      private void OKBtn_Click(object sender, System.EventArgs e)
      {
         string sWritePwrd = "";
         string sOverWritePwrd1 = "";
         string sOverWritePwrd2 = "";
         int i_IsValid = -1, j_IsValid = -1, k_IsValid = -1;

         // Get WritePassword value entered by user as string
         sWritePwrd = WP.Text;

         // Get OverWritePassword1 value entered by user as string
         sOverWritePwrd1 = OW1.Text;

         // Get OverWritePassword2 value entered by user as string
         sOverWritePwrd2 = OW2.Text;

         //Perform validation for writepassword string value and convert into integer.
         i_IsValid = MainForm.isValidData(sWritePwrd.Trim(), MainForm.VALIDDATA_LENGTH, ref MainForm.WritePW);
         if (i_IsValid == 1)// If WritePassword is blank
         {
            MessageBox.Show("Specify Write Password to proceed.", "Passwords", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return;
         }
         if (i_IsValid == 2)// If WritePassword string is of invalid length
         {
            MessageBox.Show("Write Passwords must be in hex with length not exceeding 4 digits.", "Passwords", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return;
         }
         if (i_IsValid == 3)// If WritePassword string is not valid HexString
         {
            MessageBox.Show("Write Passwords must be in hex with length not exceeding 4 digits.", "Passwords", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return;
         }

         // Overwrite password 1 or OverwritePassword2 is not blank
         if ((sOverWritePwrd1.Length != 0) || (sOverWritePwrd2.Length != 0)) //if any value blank 
         {
            //Perform validation for Overwritepassword1 string value and convert into integer.
            j_IsValid = MainForm.isValidData(sOverWritePwrd1.Trim(), MainForm.VALIDDATA_LENGTH, ref MainForm.OverWritePW1);

            //Perform validation for Overwritepassword2 string value and convert into integer.
            k_IsValid = MainForm.isValidData(sOverWritePwrd2.Trim(), MainForm.VALIDDATA_LENGTH, ref MainForm.OverWritePW2);
            if ((j_IsValid == 1) || (k_IsValid == 1)) //if any value blank  
            {
               MessageBox.Show("Specify OverWrite Passwords to proceed.", "Passwords", MessageBoxButtons.OK, MessageBoxIcon.Error);
               return;
            }
            if ((j_IsValid == 2) || (k_IsValid == 2)) //if any value is of invalid length
            {
               MessageBox.Show("Overwrite Passwords must be in hex with length not exceeding 4 digits.", "Passwords", MessageBoxButtons.OK, MessageBoxIcon.Error);
               return;
            }
            if ((j_IsValid == 3) || (k_IsValid == 3)) //if any value is of invalid hexstring
            {
               MessageBox.Show("Overwrite Passwords must be in hex with length not exceeding 4 digits.", "Passwords", MessageBoxButtons.OK, MessageBoxIcon.Error);
               return;
            }
            MainForm.Key_OWritePW = true;
            Close(); //  close form if all 3 key passwords are valid
            return;
         }
         MainForm.Key_WritePW = true;
         Close(); // close form if write password is valid
         return;
      }

      /***************************************************************************************
      * Procedure : CANECLBtn_Click
      *
      * Purpose   : Called on Cancel button click
      *
      * Descrip   : Close form 
      ***************************************************************************************/
      private void CANCELBtn_Click(object sender, System.EventArgs e)
      {
         Close();
      }

      /***************************************************************************************
      * Procedure : Password_Load
      *
      * Purpose   : Called at time of form load
      *
      * Descrip   : assign previously enterd value for key password in form's text box
      ***************************************************************************************/
      private void Password_Load(object sender, System.EventArgs e)
      {
         WP.Text = Convert.ToString(MainForm.WritePW, 16); // Write password
         OW1.Text = Convert.ToString(MainForm.OverWritePW1, 16); // OverWrite password1
         OW2.Text = Convert.ToString(MainForm.OverWritePW2, 16);// OverWrite password2

      }
   }
}
