Imports System.Web.Configuration
Imports Stock_Control.sqlDB

Partial Public Class index
  Inherits System.Web.UI.Page
	Public blnDebug As Boolean = CBool(WebConfigurationManager.AppSettings("blnDebug"))
	Public Shared blnRunOnce As Boolean = False

	Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

		FormsAuthentication.SignOut()
		WebConfigurationManager.AppSettings("UDOMAIN") = ""

		'If blnDebug Then
		'	lblMessage.Text = "Debug Mode!"
		'End If

    Me.txtUser.Focus()
    'lblMessage.Text &= My.Settings.SQLConnectionRelease & "<BR>"
    'Dim db As New sqlDB
    'lblMessage.Text &= db.conn.ConnectionString & "<BR>"

  End Sub

	Protected Sub btnLogin_click(ByVal sender As Object, ByVal e As EventArgs) Handles btnLogin.Click
		Dim ds As DataSet
		Dim db As New sqlDB
		Dim s As String

    s = Request.LogonUserIdentity.Name.ToUpper()
    lblMessage.Text = s & "<br>"

		Try

      ds = db.doQueryDS(db.sql_get_Users(s, "StockControl"))
			If ds.Tables(0).Rows.Count > 0 Then
				WebConfigurationManager.AppSettings("UNAME") = ds.Tables(0).Rows(0)!strUserName
				WebConfigurationManager.AppSettings("UDOMAIN") = ds.Tables(0).Rows(0)!strDomainLogin
				WebConfigurationManager.AppSettings("UID") = ds.Tables(0).Rows(0)!UserID
        WebConfigurationManager.AppSettings("UGROUP") = ds.Tables(0).Rows(0)!strGroupSC
				WebConfigurationManager.AppSettings("UGROUPID") = ds.Tables(0).Rows(0)!refUserGroupID


        System.Threading.Thread.Sleep(13)
        db.doQueryDS("INSERT INTO mstUsageLog(mstUserID, dtStamp, strSystem) VALUES (" & ds.Tables(0).Rows(0)!UserID & ", '" & Date.Now.ToString("yyyyMMddHHmmss") & "', 'StockControl')")

        Response.Redirect("ProductPeriod.aspx", False)
			Else
        lblMessage.Text &= "<i>Insufficient Access.</i><BR>Login is controlled through the Active Directory, <BR>Please contact _ if your require access."
				FormsAuthentication.SignOut()
				WebConfigurationManager.AppSettings("UDOMAIN") = ""
			End If
		Catch ex As Exception
      lblMessage.Text &= "<i>A error has occured, please contact the system administrator.</i><BR>" & ex.Message & ex.StackTrace & "<BR><BR>" & sqlDB.conn.ConnectionString

			FormsAuthentication.SignOut()
			WebConfigurationManager.AppSettings("UDOMAIN") = ""
		Finally

		End Try
	End Sub

End Class