Imports System.IO
Imports Microsoft.Office.Interop
Imports System.Drawing.Drawing2D
Imports VERAG_PROG_ALLGEMEIN


Public Class frmMain

    Dim dgvKunden As DataGridView
    Dim kundenSQL As New kundenSQL

    ' INFO:
    Private InfoDauer As Integer = 3
    Private WithEvents timShow As New Timer With {.Interval = 10}
    Private WithEvents timHide As New Timer With {.Interval = 10}
    ' Dim acd As List(Of cAutoCompleteData) = kundenSQL.getKundenShort()

    Dim WithEvents UpdateUpdater As New Timer
    Dim WithEvents UpdateSession As New Timer

    Dim WithEvents TimerUPDATE As New Timer ' prüft periodisch, ob ein Update verfügbar

    Dim FUNC As New cProgramFunctions

    Public BERECHTIGUNEN As List(Of cBerechtigungen) = cBerechtignungenFunctions.loadBerechtigungen(VERAG_PROG_ALLGEMEIN.cAllgemein.USRID)
    Private Declare Function GetActiveWindow Lib "user32" Alias "GetActiveWindow" () As IntPtr


    Private Sub UpdateSession_Tick(sender As Object, e As EventArgs) Handles UpdateSession.Tick
        VERAG_PROG_ALLGEMEIN.cAllgemein.SESSION.UPDATE_TIME()
    End Sub


    Public Sub New()
        Try
            ' initialize added component on form1, which is menu strip;
            'note: if you have added any second component alongside menu
            'strip, such as context menu strip just add additional code 
            'line under InitializeComponent(), and it will work;
            InitializeComponent()
            MenuStripSDL.Renderer = New MyRenderer()
        Catch ex As Exception

        End Try
    End Sub


    Private Sub TimerUPDATE_Tick(sender As Object, e As EventArgs) Handles TimerUPDATE.Tick
        Dim SDL As New cOptionenDAL
        If CInt(SDL.getAktuelleVersion().Replace(".", "")) > CInt(Application.ProductVersion.Replace(".", "")) Then
            Label9.Visible = True
        End If
    End Sub


    Private Sub frmMain_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
        If e.KeyCode = Keys.Escape Then
            KdSearchBox1.Text = ""
            KdSearchBox1.Focus()
        End If
        '  If e.KeyValue = Keys.ControlKey And e.KeyValue = Keys.Add Then
        If e.KeyCode = Keys.ControlKey And e.KeyCode = Keys.Add Then
            '  MsgBox("+")
            Button7.PerformClick()
        End If '
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.MaximumSize = Screen.PrimaryScreen.WorkingArea.Size
        Me.WindowState = FormWindowState.Maximized

        Timer_REFRESH_Tick(Timer_REFRESH, New EventArgs)

        UpdateSession.Interval = 60000
        UpdateSession.Enabled = True

        Panel3.Left = Me.Width / 2 - (Panel3.Width / 2)
        If VERAG_PROG_ALLGEMEIN.cAllgemein.TESTSYSTEM Then lblTestsystem.Visible = True
        Control.CheckForIllegalCrossThreadCalls = False
        If VERAG_PROG_ALLGEMEIN.cBerechtignunen.CHECK_BERECHTIGUNG("MDM_Hauptmenü", Me) = 0 Then
            mnueMDM.Enabled = True
        Else
            mnueMDM.Enabled = False
        End If
        If VERAG_PROG_ALLGEMEIN.cBerechtignunen.CHECK_BERECHTIGUNG("ZOLL_Hauptmenü", Me) = 0 Then
            mnueZoll.Enabled = True
        Else
            mnueZoll.Enabled = False
        End If

        If VERAG_PROG_ALLGEMEIN.cBerechtignunen.CHECK_BERECHTIGUNG_bool("MESSENGER", "AVISO") Or VERAG_PROG_ALLGEMEIN.cBerechtignunen.CHECK_BERECHTIGUNG_bool("MESSENGER", "SDL") Then
            picAVISOMessenger.Visible = True
        End If


        lblVersion.Text = "V " & Application.ProductVersion
        lblMs.Text = ""
        Dim usrCntl As New usrCntlWelcome
        pnlMain.Controls.Add(usrCntl)
        usrCntl.Dock = System.Windows.Forms.DockStyle.Fill
        initLoad()

        ' btnUpdate.FlatAppearance.BorderSize = 0
        pnlInfo.Height = 0
        'MsgBox(frmLogin.USRBER)
        If False Then 'frmLogin.USRBER > 0 Then

            mnueKassenbuch.Visible = False
            mnueAdministration.Visible = False
            mnueKunden.Enabled = False
            ' btnDaten.Enabled = False
            mnueMDM.Enabled = False
            mnueZoll.Enabled = False
            mnueBuchhaltung.Enabled = False
        End If

        If VERAG_PROG_ALLGEMEIN.cBerechtignunen.CHECK_BERECHTIGUNG_bool("ADMINFUNCTIONS", Me) Then
            mnueAdministration.Visible = True
        End If


        If VERAG_PROG_ALLGEMEIN.cBerechtignunen.CHECK_BERECHTIGUNG_bool("PARKPLATZ_Parkzeit", Me) Then
            ParkzeitWaberersEtcToolStripMenuItem.Visible = True
        End If



        If Not VERAG_PROG_ALLGEMEIN.cBerechtignunen.CHECK_BERECHTIGUNG_bool("MDM_Leistungen_bearbeiten", Me) Then
            Button7.Visible = False
        End If


        KdSearchBox1._ValueKdAndName = True
        KdSearchBox1.dgvpos = "RIGHT"
        KdSearchBox1.initKdBox(Me)
        'initKdSearch()
        btnMitarbeiterKonto.Text = VERAG_PROG_ALLGEMEIN.cAllgemein.USRNAME
        setProfileImage()



        If VERAG_PROG_ALLGEMEIN.cAllgemein.MITARBEITER.mit_timasId IsNot Nothing Then
            Dim timas As New cTimasAPI
            Dim infoMessage As String = ""
            Dim timeEntryExisting As Boolean = False
            timas.getTimeEntries(cAllgemein.MITARBEITER.mit_timasId, Nothing, timeEntryExisting)
            If Not timeEntryExisting Then
                Dim a As MsgBoxResult = MsgBox("Für " & VERAG_PROG_ALLGEMEIN.cAllgemein.USRKURZNAME & " wurde heute noch keine Zeit erfasst!" & vbNewLine & Now.ToString() & " als Startzeit erfassen?", vbYesNo)
                If a = vbYes Then
                    Dim info
                    timas.setTimeEntry(cAllgemein.MITARBEITER, Now, info, "in")
                    MsgBox(info)
                End If
            End If

        End If

        AddHandler KdSearchBox1.PropertyChanged, Sub()
                                                     Try
                                                         If KdSearchBox1.KdNr > 0 Then
                                                             Dim pf As New cProgramFunctions
                                                             cProgramFunctions.openKundenblatt(KdSearchBox1.KdNr, Me, True)
                                                         End If
                                                     Catch ex As Exception
                                                         MsgBox(ex.Message & vbNewLine & ex.StackTrace)
                                                     End Try
                                                 End Sub
    End Sub




    Private Sub Button1_Click(sender As Object, e As EventArgs)
        Label1.Visible = False
    End Sub

    Private Sub initLoad()
        Label1.Visible = True
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs)
        initLoad()
    End Sub

    Private Sub Button8_Click(sender As Object, e As EventArgs)
        Me.Close()
    End Sub

    Private Sub btnKunden_Click(sender As Object, e As EventArgs)
        clrPnlMain()
        Dim usrCntl As New usrCntlKunden
        usrCntl.Dock = System.Windows.Forms.DockStyle.Fill
        pnlMain.Controls.Add(usrCntl)

        '   setInfo("err", "Kundendaten sind noch nicht freigegeben!", 3)

    End Sub

    Private Sub clrPnlMain()

        lblMs.Text = ""
        While pnlMain.HasChildren
            pnlMain.Controls.RemoveAt(0)
        End While
    End Sub




    Private Sub Panel1_Paint(sender As Object, e As PaintEventArgs) Handles pnlLine.Paint
        With pnlLine

            .BorderStyle = BorderStyle.None
            Dim p As New Pen(Color.FromArgb(0, 54, 128), 10)
            e.Graphics.DrawLine(p, .Left, .Bottom - 5, .Width, .Bottom)
        End With
        pnlLine.BackColor = Color.FromArgb(0, 54, 128)
    End Sub


    Private Sub btnStammdaten_2Click(sender As Object, e As EventArgs)
        If VERAG_PROG_ALLGEMEIN.cBerechtignunen.CHECK_BERECHTIGUNG("BH_Hauptmenü", Me) = 0 Then
            clrPnlMain()
            Dim usrCntl As New usrCntlBH
            usrCntl.Dock = System.Windows.Forms.DockStyle.Fill
            pnlMain.Controls.Add(usrCntl)
        Else
            setInfo("err", "Daten sind noch nicht freigegeben!", 3)
        End If
    End Sub
    Sub playInfoSound()
        Try 'My.Computer.Audio.Play(My.Resources.chimes, AudioPlayMode.WaitToComplete)
        Catch ex As Exception : End Try
    End Sub
    Sub playErrorSound()
        Try 'My.Computer.Audio.Play(My.Resources.chimes, AudioPlayMode.WaitToComplete)
        Catch ex As Exception : End Try
    End Sub

    Private Sub btnBerichte_2Click(sender As Object, e As EventArgs)
        If VERAG_PROG_ALLGEMEIN.cBerechtignunen.CHECK_BERECHTIGUNG("Berichte_Hauptmenü", Me) = 0 Then
            clrPnlMain()
            Dim usrCntl As New usrCntlSeriendruck
            usrCntl.Dock = System.Windows.Forms.DockStyle.Fill
            pnlMain.Controls.Add(usrCntl)
        Else
            setInfo("err", "Berichte sind noch nicht freigegeben!", 3)
        End If
        'setInfo("info", "Das ist nur eine Information", 3)
    End Sub





    Public Sub setInfo(art As String, text As String, dauer As Integer)

        pnlInfo.Height = 0
        If art.ToLower = "info" Then
            playInfoSound()
            InfoDauer = dauer
            txtInfotext.Text = text
            txtInfotext.Left = (pnlInfo.Width / 2) - (txtInfotext.Width / 2)
            timShow.Enabled = True
            pnlInfo.BackColor = Color.FromArgb(255, 255, 128)
        End If
        If art.ToLower = "err" Then
            playErrorSound()
            InfoDauer = dauer
            txtInfotext.Text = text
            txtInfotext.Left = (pnlInfo.Width / 2) - (txtInfotext.Width / 2)
            timShow.Enabled = True
            pnlInfo.BackColor = Color.FromArgb(255, 128, 128)
        End If
    End Sub

    Private Sub tim_Tick(sender As Object, e As EventArgs) Handles timShow.Tick
        If pnlInfo.Height >= 32 Then
            timShow.Enabled = False
            Wait(InfoDauer * 1000)
            timHide.Enabled = True
        Else
            pnlInfo.Height = pnlInfo.Height + 1
        End If
    End Sub
    Private Sub timHide_Tick(sender As Object, e As EventArgs) Handles timHide.Tick
        If pnlInfo.Height <= 0 Then
            timHide.Enabled = False
        Else
            pnlInfo.Height = pnlInfo.Height - 1
        End If
    End Sub
    Sub Wait(ByVal milliseconds As Integer)
        For x = 1 To milliseconds
            System.Threading.Thread.Sleep(1)
            Application.DoEvents()
        Next
    End Sub


    Public Sub setLoadingTime(s As String)
        lblMs.Text = s
    End Sub

    Private Sub btnKarten_Click(sender As Object, e As EventArgs)


        'ContextMenuStrip

        'setInfo("err", "Berichte sind noch nicht verfügbar!", 3)
        '  End If
    End Sub

    Private Sub frmMain_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
        KdSearchBox1.Focus()
        UpdateUpdater.Interval = 5000
        UpdateUpdater.Enabled = True
        TimerUPDATE.Interval = 900000
        TimerUPDATE.Enabled = True
    End Sub


    Private Sub btnSchnittstelle_Click(sender As Object, e As EventArgs)
        If VERAG_PROG_ALLGEMEIN.cAllgemein.USRKURZNAME.ToLower = "admin" Then
            frmSecureLoginKassenbuch.Show()
        End If
    End Sub

    Private Sub UpdateUpdater_Tick(sender As Object, e As EventArgs) Handles UpdateUpdater.Tick
        UpdateUpdater.Enabled = False
        Try
            Dim aktVersion = "4.0" ' Aktuelle Updater Version
            Dim F = "\\share01\Programme\SDL_install\"
            Dim doUpdaterUpdate = False
            If File.Exists(AppDomain.CurrentDomain.BaseDirectory & "\SDLUPDATER_Version.txt") Then 'Updater muss auch existieren, sonst wird er auch beim Developer Rechner eingefügt...
                Dim fs As New FileStream(AppDomain.CurrentDomain.BaseDirectory & "\SDLUPDATER_Version.txt", FileMode.Open, FileAccess.Read)
                Dim strmReader As New StreamReader(fs)
                Dim Version = strmReader.ReadLine
                If IsNumeric(Version.Replace(".", "")) Then
                    If Version.Replace(".", "") < aktVersion.Replace(".", "") Then
                        doUpdaterUpdate = True
                    End If
                End If
                fs.Close()

                strmReader.Close()
            Else
                doUpdaterUpdate = True
            End If
            If Not doUpdaterUpdate Then Exit Sub

            If Directory.Exists(F) And File.Exists(AppDomain.CurrentDomain.BaseDirectory & "\SDLUPDATER.exe") Then 'Updater muss auch existieren, sonst wird er auch beim Developer Rechner eingefügt...
                For Each file As String In Directory.GetFiles(F) ' Ermittelt alle Dateien des Ordners 
                    System.IO.File.Copy(file, AppDomain.CurrentDomain.BaseDirectory & cut_file(file), True) ' Kopiert die Dateien Next 
                Next
                For Each file As String In Directory.GetDirectories(F) ' Ermittelt alle Unterordner des Ordners 
                    My.Computer.FileSystem.CopyDirectory(file, AppDomain.CurrentDomain.BaseDirectory & cut_file(file), True)
                Next
            End If

        Catch ex As Exception
            'nix....
        End Try
    End Sub
    Private Function cut_file(ByVal file As String) As String ' Funktion zum Entfernen der Backslashs / Ordner While file.Contains("\") file = file.Remove(0, 1) End While Return file End Function 
        While file.Contains("\")
            file = file.Remove(0, 1)
        End While
        Return file
    End Function

    Private Sub btnAdministration_Click(sender As Object, e As EventArgs)
        If VERAG_PROG_ALLGEMEIN.cBerechtignunen.CHECK_BERECHTIGUNG("ADMINFUNCTIONS", Me) = 0 Then
            clrPnlMain()
            Dim usrCntl As New usrCntlAdministration
            usrCntl.Dock = System.Windows.Forms.DockStyle.Fill
            pnlMain.Controls.Add(usrCntl)
        Else
            setInfo("err", "Administration ist noch nicht verfügbar!", 3)
        End If
    End Sub

    Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub

    Private Sub btnUpdate_Click(sender As Object, e As EventArgs)
        frmLogin.UpdateSDL()
    End Sub









    Private Sub KartenverwaltungToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles KartenverwaltungToolStripMenuItem.Click
        If VERAG_PROG_ALLGEMEIN.cBerechtignunen.CHECK_BERECHTIGUNG("MDM_Kartenverwaltung", Me) = 0 Then '39
            clrPnlMain()
            Dim usrCntl As New usrCntlKartenDaten
            usrCntl.Dock = System.Windows.Forms.DockStyle.Fill
            pnlMain.Controls.Add(usrCntl)
        Else
            setInfo("err", "Daten sind noch nicht freigegeben!", 3)
        End If
    End Sub

    Private Sub VERAGCardParkplatzToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles VERAGCardParkplatzToolStripMenuItem.Click
        If VERAG_PROG_ALLGEMEIN.cBerechtignunen.CHECK_BERECHTIGUNG("MDM_VERAG_Card", Me) = 0 Then
            clrPnlMain()
            Dim usrCntl As New usrCntlVERAGCard
            usrCntl.Dock = System.Windows.Forms.DockStyle.Fill
            pnlMain.Controls.Add(usrCntl)
        Else
            setInfo("err", "Daten sind noch nicht freigegeben!", 3)
        End If
    End Sub

    Private Sub LKWListeToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles LKWListeToolStripMenuItem.Click
        If VERAG_PROG_ALLGEMEIN.cBerechtignunen.CHECK_BERECHTIGUNG("MDM_Hauptmenü", Me) = 0 Then
            clrPnlMain()
            Dim usrCntl As New usrCntlLkwListe
            usrCntl.Dock = System.Windows.Forms.DockStyle.Fill
            pnlMain.Controls.Add(usrCntl)
        Else
            setInfo("err", "Daten sind noch nicht freigegeben!", 3)
        End If
    End Sub

    Private Sub AuswertungenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles AuswertungenToolStripMenuItem.Click
        If VERAG_PROG_ALLGEMEIN.cBerechtignunen.CHECK_BERECHTIGUNG("MDM_Auswertungen", Me) = 0 Then
            clrPnlMain()
            Dim usrCntl As New usrCntlMDMAuswertungen
            usrCntl.Dock = System.Windows.Forms.DockStyle.Fill
            pnlMain.Controls.Add(usrCntl)
        Else
            setInfo("err", "Daten sind noch nicht freigegeben!", 3)
        End If
    End Sub

    Private WithEvents T As New Timers.Timer With {.Interval = 500, .Enabled = True, .AutoReset = True}
    Private FormCount As Integer = 1
    Private Sub SomeForm_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
        ' If e.KeyData = Keys.Escape Then
        'Dim TheForm = DirectCast(DirectCast(sender, Control).TopLevelControl, Form)
        ' If Not TheForm Is Nothing Then
        'TheForm.Close()
        ' End If
        'End If
        If e.KeyData = Keys.PrintScreen Then
            Dim TheForm = DirectCast(DirectCast(sender, Control).TopLevelControl, Form)
            If Not TheForm Is Nothing Then
                Dim c As New cProgramFunctions
                c.screenshot()
            End If
        End If
    End Sub
    Private Sub HookControls(ByVal f As Control)
        For Each Control As Control In f.Controls
            AddHandler Control.KeyUp, AddressOf SomeForm_KeyUp
            If Control.Controls.Count <> 0 Then
                HookControls(Control)
            End If
        Next
    End Sub
    Private Sub T_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles T.Elapsed
        If FormCount = 0 Then FormCount = 1
        Dim FormOpened As Integer = My.Application.OpenForms.Count
        If FormCount <> FormOpened Then
            If FormOpened > FormCount Then
                For x As Integer = 1 To (FormOpened - FormCount)
                    Dim F As Form = My.Application.OpenForms.Item(FormOpened - x)
                    AddHandler F.KeyDown, AddressOf SomeForm_KeyUp
                    HookControls(F)
                    FormCount = My.Application.OpenForms.Count
                Next
            Else
                FormCount = My.Application.OpenForms.Count
            End If
        End If
    End Sub

    Private Sub btnZoll_Click(sender As Object, e As EventArgs)
        If VERAG_PROG_ALLGEMEIN.cBerechtignunen.CHECK_BERECHTIGUNG("ZOLL_Hauptmenü", Me) = 0 Then
            clrPnlMain()
            Dim usrCntl As New usrCntlZollHauptmenue
            usrCntl.BERECHTIGUNEN = BERECHTIGUNEN
            usrCntl.Dock = System.Windows.Forms.DockStyle.Fill
            pnlMain.Controls.Add(usrCntl)
        Else
            setInfo("err", "Daten sind noch nicht freigegeben!", 3)
        End If
    End Sub


    Public Sub ToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles ToolStripMenuItem1.Click
        If VERAG_PROG_ALLGEMEIN.cBerechtignunen.CHECK_BERECHTIGUNG("ZOLL_Kundenzolltarif", Me) = 0 Then
            clrPnlMain()
            Dim usrCntl As New usrCntlZollArtikel
            usrCntl.Dock = System.Windows.Forms.DockStyle.Fill
            pnlMain.Controls.Add(usrCntl)
        End If
    End Sub

    Private Sub AuswertungenToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles AuswertungenToolStripMenuItem1.Click
        If VERAG_PROG_ALLGEMEIN.cBerechtignunen.CHECK_BERECHTIGUNG("ZOLL_Auswertungen", Me) = 0 Then
            clrPnlMain()
            Dim usrCntl As New usrCntlZOLLAuswertungen
            usrCntl.Dock = System.Windows.Forms.DockStyle.Fill
            pnlMain.Controls.Add(usrCntl)
        Else
            setInfo("err", "Daten sind noch nicht freigegeben!", 3)
        End If
    End Sub

    Private Sub Button1_Click_1(sender As Object, e As EventArgs)
        Dim SC As New cServerClient
        SC.begin("192.168.0.95", "FSSAtlasStart") '95 =atlas

        AddHandler SC.FunctionFinished, Sub(se As Object, ev As FunctionFinishedArgs)
                                            If ev.Result = FunctionFinishedArgs.EventResult.OK Then
                                                MsgBox("OK" & vbNewLine & ev.Message)
                                            Else
                                                MsgBox(ev.Message)
                                            End If
                                        End Sub

    End Sub

    Private Sub Button1_Click_2(sender As Object, e As EventArgs) Handles Button1.Click
        frmInfo.Show()
    End Sub


    Private Sub btnTest_Click(sender As Object, e As EventArgs) Handles btnTest.Click
        'Form1.Show()
        ' Dim l As New VERAG_PROG_ALLGEMEIN.cSDL_LKW.GET_LIST_WEB(401462)
        'Dim sdl As New VERAG_PROG_ALLGEMEIN.cSDL

        'Dim list = VERAG_PROG_ALLGEMEIN.cSDL.GET_LIST_WEB(401940)
        'For Each l In list
        '    l.KfzKennzeichen
        'Next

        'MsgBox(list.Count)
        'Dim list2 = VERAG_PROG_ALLGEMEIN.cSDL_LKW.GET_LIST_WEB(401940)
        'MsgBox(list2.Count)
        'Dim list3 = VERAG_PROG_ALLGEMEIN.cSDL.GET_LIST_WEB_LKW(401940, list2(0).KfzKennzeichen)
        'MsgBox(list3.Count)
        'MsgBox(list3(0).KartenNr)
    End Sub


    Private Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub Button4_Click_1(sender As Object, e As EventArgs) Handles Button4.Click
        Me.WindowState = FormWindowState.Minimized
    End Sub

    Private Sub UpdateToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles UpdateToolStripMenuItem.Click
        frmLogin.UpdateSDL()
    End Sub

    Private Sub MenuStripSDL_ItemClicked(sender As Object, e As ToolStripItemClickedEventArgs) Handles MenuStripSDL.ItemClicked

    End Sub

    Private Sub mnueKunden_Click(sender As Object, e As EventArgs) Handles mnueKunden.Click
        If VERAG_PROG_ALLGEMEIN.cBerechtignunen.CHECK_BERECHTIGUNG("MDM_Hauptmenü", Me) = 0 Then
            clrPnlMain()
            Dim usrCntl As New usrCntlKunden
            usrCntl.Dock = System.Windows.Forms.DockStyle.Fill
            pnlMain.Controls.Add(usrCntl)
        Else
            setInfo("err", "Kundendaten sind noch nicht freigegeben!", 3)
        End If
    End Sub

    Private Sub mnueBerichte_Click(sender As Object, e As EventArgs) Handles mnueBerichte.Click
        If VERAG_PROG_ALLGEMEIN.cBerechtignunen.CHECK_BERECHTIGUNG("MDM_Hauptmenü", Me) = 0 Then
            clrPnlMain()
            Dim usrCntl As New usrCntlSeriendruck
            usrCntl.Dock = System.Windows.Forms.DockStyle.Fill
            pnlMain.Controls.Add(usrCntl)
        Else
            setInfo("err", "Berichte sind noch nicht freigegeben!", 3)
        End If
    End Sub

    Private Sub mnueBuchhaltung_Click(sender As Object, e As EventArgs) Handles mnueBuchhaltung.Click
        If VERAG_PROG_ALLGEMEIN.cBerechtignunen.CHECK_BERECHTIGUNG("BH_Hauptmenü", Me) = 0 Then
            clrPnlMain()
            Dim usrCntl As New usrCntlBH
            usrCntl.Dock = System.Windows.Forms.DockStyle.Fill
            pnlMain.Controls.Add(usrCntl)
        Else
            setInfo("err", "Daten sind noch nicht freigegeben!", 3)
        End If
    End Sub

    Private Sub mnueKassenbuch_Click(sender As Object, e As EventArgs) Handles mnueKassenbuch.Click
        '   MsgBox(VERAG_PROG_ALLGEMEIN.cBerechtignunen.CHECK_BERECHTIGUNG("KASSE", "SDL"))
        If VERAG_PROG_ALLGEMEIN.cBerechtignunen.CHECK_BERECHTIGUNG_bool("KASSE", "SDL") Or
         VERAG_PROG_ALLGEMEIN.cBerechtignunen.CHECK_BERECHTIGUNG_bool("KASSE_BH_Login", "SDL") Or
         VERAG_PROG_ALLGEMEIN.cBerechtignunen.CHECK_BERECHTIGUNG_bool("KASSE_Saldo_Anzeige", "SDL") Then
            Dim frmSecureLoginKassenbuch As New SDL.frmSecureLoginKassenbuch()
            frmSecureLoginKassenbuch.openKB = True
            frmSecureLoginKassenbuch.ShowDialog(Me)
        End If
    End Sub

    Private Sub AdministrationToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles mnueAdministration.Click
        If VERAG_PROG_ALLGEMEIN.cBerechtignunen.CHECK_BERECHTIGUNG("ADMINFUNCTIONS", Me) = 0 Then
            clrPnlMain()
            Dim usrCntl As New usrCntlAdministration
            usrCntl.Dock = System.Windows.Forms.DockStyle.Fill
            pnlMain.Controls.Add(usrCntl)
        Else
            setInfo("err", "Administration ist noch nicht verfügbar!", 3)
        End If
    End Sub


    Private Sub KartenverwaltungToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles KartenverwaltungToolStripMenuItem1.Click
        If VERAG_PROG_ALLGEMEIN.cBerechtignunen.CHECK_BERECHTIGUNG("MDM_Kartenverwaltung", Me) = 0 Then
            clrPnlMain()
            Dim usrCntl As New usrCntlKartenDaten
            usrCntl.Dock = System.Windows.Forms.DockStyle.Fill
            pnlMain.Controls.Add(usrCntl)
        Else
            setInfo("err", "Daten sind noch nicht freigegeben!", 3)
        End If
    End Sub

    Private Sub VERAGCardParkplatzToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles VERAGCardParkplatzToolStripMenuItem1.Click
        If VERAG_PROG_ALLGEMEIN.cBerechtignunen.CHECK_BERECHTIGUNG("MDM_VERAG_Card", Me) = 0 Then
            clrPnlMain()
            Dim usrCntl As New usrCntlVERAGCard
            usrCntl.Dock = System.Windows.Forms.DockStyle.Fill
            pnlMain.Controls.Add(usrCntl)
        Else
            setInfo("err", "Daten sind noch nicht freigegeben!", 3)
        End If
    End Sub

    Private Sub LKWListeToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles LKWListeToolStripMenuItem1.Click
        If VERAG_PROG_ALLGEMEIN.cBerechtignunen.CHECK_BERECHTIGUNG("MDM_Hauptmenü", Me) = 0 Then
            clrPnlMain()
            Dim usrCntl As New usrCntlLkwListe
            usrCntl.Dock = System.Windows.Forms.DockStyle.Fill
            pnlMain.Controls.Add(usrCntl)
        Else
            setInfo("err", "Daten sind noch nicht freigegeben!", 3)
        End If
    End Sub

    Private Sub AuswertungenToolStripMenuItem2_Click(sender As Object, e As EventArgs) Handles AuswertungenToolStripMenuItem2.Click
        If VERAG_PROG_ALLGEMEIN.cBerechtignunen.CHECK_BERECHTIGUNG("MDM_Auswertungen", Me) = 0 Then
            clrPnlMain()
            Dim usrCntl As New usrCntlMDMAuswertungen
            usrCntl.Dock = System.Windows.Forms.DockStyle.Fill
            pnlMain.Controls.Add(usrCntl)
        Else
            setInfo("err", "Daten sind noch nicht freigegeben!", 3)
        End If
    End Sub

    Private Sub ArtikelTarifierenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ArtikelTarifierenToolStripMenuItem.Click
        If VERAG_PROG_ALLGEMEIN.cBerechtignunen.CHECK_BERECHTIGUNG("ZOLL_Kundenzolltarif", Me) = 0 Then
            clrPnlMain()
            Dim usrCntl As New usrCntlZollArtikel
            usrCntl.Dock = System.Windows.Forms.DockStyle.Fill
            pnlMain.Controls.Add(usrCntl)
        End If
    End Sub

    Private Sub AuswertungToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles AuswertungToolStripMenuItem.Click
        If VERAG_PROG_ALLGEMEIN.cBerechtignunen.CHECK_BERECHTIGUNG("ZOLL_Auswertungen", Me) = 0 Then
            clrPnlMain()
            Dim usrCntl As New usrCntlZOLLAuswertungen
            usrCntl.Dock = System.Windows.Forms.DockStyle.Fill
            pnlMain.Controls.Add(usrCntl)
        Else
            setInfo("err", "Daten sind noch nicht freigegeben!", 3)
        End If
    End Sub

    ' Private Sub mnueMDM_Click(sender As Object, e As EventArgs) Handles mnueMDM.MouseHover, mnueZoll.MouseHover
    '    sender.ShowDropDown()
    'End Sub

    Private Sub TESTToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles TESTToolStripMenuItem.Click
        If VERAG_PROG_ALLGEMEIN.cAllgemein.USRKURZNAME = "ADMIN" Then
            frmQRCode.Show()
        End If
    End Sub


    Private Sub KdSearchBox1_GotFocus(sender As Object, e As EventArgs) Handles KdSearchBox1.GotFocus
        pnlJumpKunde.BackColor = SystemColors.MenuHighlight
        Label1.ForeColor = Color.White
        Label3.ForeColor = Color.White
    End Sub

    Private Sub KdSearchBox1_LostFocus(sender As Object, e As EventArgs) Handles KdSearchBox1.LostFocus
        pnlJumpKunde.BackColor = Color.FromArgb(224, 224, 224)
        Label1.ForeColor = Color.Black
        Label3.ForeColor = Color.Black
    End Sub

    Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
        cProgramFunctions.newFrmData(Me.FindForm, New Integer = -1, New Integer = -1, New String(""), New Integer = -1, True)
    End Sub

    Private Sub FlatButton1_Click(sender As Object, e As EventArgs) Handles FlatButton1.Click
        If Not My.Computer.FileSystem.DirectoryExists(Environment.GetFolderPath(Environment.SpecialFolder.Personal) & "\VERAG\tmp\") Then
            My.Computer.FileSystem.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.Personal) & "\VERAG\tmp\")
        End If

        Dim strFileName As String = Environment.GetFolderPath(Environment.SpecialFolder.Personal) & "\VERAG\tmp\tmp.doc"


        Dim pfad = AppDomain.CurrentDomain.BaseDirectory & "Resources\Retour\VIATOLL.doc"


        Dim oWord As New Microsoft.Office.Interop.Word.Application
        Dim odoc As Microsoft.Office.Interop.Word.Document
        Try
            oWord.Visible = True 'Word sehen zum Test?
            odoc = oWord.Documents.Open(pfad)
            odoc.SaveAs(strFileName)

            'doc.Protect(WdProtectionType.wdNoProtection)
            odoc.Activate()

            'Dim oDoc As Microsoft.Office.Interop.Word.Document = oWord.Documents.Open(pfad)
            Dim oTable As Word.Table
            '  Dim oPara1 As Word.Paragraph, oPara2 As Word.Paragraph
            '   Dim oPara3 As Word.Paragraph, oPara4 As Word.Paragraph
            '  Dim oRng As Word.Range
            '  Dim oShape As Word.InlineShape

            'Word starten und Dokumentvorlage öffnen.
            'oWord = CreateObject("Word.Application")
            '  oWord.Visible = True
            odoc.FormFields("Anzahl").Range.Text = "2"
            odoc.FormFields("KdName").Range.Text = "VERAG Spedition AG"
            odoc.FormFields("KdStrasse").Range.Text = "Suben Nr. 100"
            odoc.FormFields("KdOrt").Range.Text = "A 4975 Suben"
            odoc.FormFields("KdNrVERAG").Range.Text = "401053"
            odoc.FormFields("KdNrMST").Range.Text = "123456"
            odoc.FormFields("Sachbearbeiter").Range.Text = "Andreas Luxbauer"

            '  oDoc = oWord.Documents.Add

            If odoc.Range.Bookmarks.Exists("TabelleKarten2") Then
                If odoc.Bookmarks("TabelleKarten2").Range.Tables.Count > 0 Then
                    oTable = odoc.Bookmarks("TabelleKarten2").Range.Tables(1)
                    For i As Integer = 2 To 5
                        oTable.Rows(i).Cells(1).Range.Text = "ST008OR"
                        oTable.Rows(i).Cells(2).Range.Text = "7083801193800691"
                        oTable.Rows(i).Cells(3).Range.Text = ""
                        If i < 5 Then oTable.Rows.Add()
                    Next i
                End If
            Else
                MessageBox.Show("Textmarke nicht vorhanden!")
            End If
            ' WordApp.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone
            odoc.Save()

        Catch ex As Exception
            MessageBox.Show(ex.ToString)
            ' Log.SaveLogFile(ex.ToString)
            'Finally
            ' word.Application.Quit()
        End Try
    End Sub

    Private Sub SpeditionsbuchToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SpeditionsbuchToolStripMenuItem.Click
        Dim frmSpeditionsbuchSuche As New frmSpeditionsbuchSuche
        frmSpeditionsbuchSuche.Show(Me)
    End Sub

    Private Sub SDLAbholaufträgeToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SDLAbholaufträgeToolStripMenuItem.Click
        If VERAG_PROG_ALLGEMEIN.cBerechtignunen.CHECK_BERECHTIGUNG("MDM_Kartenverwaltung", Me) = 0 Then
            clrPnlMain()
            Dim usrCntl As New usrCntlSDLAbholung
            usrCntl.Dock = System.Windows.Forms.DockStyle.Fill
            pnlMain.Controls.Add(usrCntl)
        Else
            setInfo("err", "Daten sind noch nicht freigegeben!", 3)
        End If
    End Sub

    Private Sub Button2_Click_1(sender As Object, e As EventArgs) Handles Button2.Click
        Dim f As New frmSDLAbholungEintrag
        If f.ShowDialog(Me) = DialogResult.OK Then
            SDLAbholaufträgeToolStripMenuItem.PerformClick()
        End If
    End Sub

    Private Sub NeueSMSSendenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles NeueSMSSendenToolStripMenuItem.Click
        Dim f As New SDL.frmSendSMS("", "")
        f.ShowDialog()
    End Sub

    Private Sub SMSVerlaufToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SMSVerlaufToolStripMenuItem.Click
        Dim f As New SDL.frmSMS
        f.Show(Me)
    End Sub

    Private Sub SchnittstellenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SchnittstellenToolStripMenuItem.Click

    End Sub

    Private Sub NeuesPasswortToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles NeuesPasswortToolStripMenuItem.Click
        Dim f As New frmChangePWD(VERAG_PROG_ALLGEMEIN.cAllgemein.MITARBEITER)
        If f.ShowDialog() = Windows.Forms.DialogResult.OK Then
            MsgBox("Passwort geändert!", vbInformation)
        End If
    End Sub

    Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
        Dim DS As New VERAG_PROG_ALLGEMEIN.cDATENSERVER("DOKUMENTE", "VORLAGEN", "SONSTIGE", "", "", "Telefonliste")
        'MsgBox(DS.da_id)
        DS.OPEN_SINGLE()
    End Sub

    Private Sub ParkzeitWaberersEtcToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ParkzeitWaberersEtcToolStripMenuItem.Click
        Dim f As New Parkzeit.frmMain
        f.Show()
    End Sub

    Dim Timer_REFRESH_firstTime = True
    Private Sub Timer_REFRESH_Tick(sender As Object, e As EventArgs) Handles Timer_REFRESH.Tick
        If Timer_REFRESH_firstTime Then
            Timer_REFRESH_firstTime = False
            Exit Sub
        End If
        Try
            If VERAG_PROG_ALLGEMEIN.cAllgemein.MITARBEITER.mit_ChatBenutzer And picAVISOMessenger.Visible Then
                Dim newMsg = VERAG_PROG_ALLGEMEIN.cMessenger.GET_NewMSG_COUNT()
                ' MsgBox(newMsg)
                Label20.Text = newMsg
                Label20.Visible = (newMsg > 0)
                If newMsg Then
                    ' Me.Icon = My.Resources.avisoNewMsg

                    Dim isshown = False
                    For Each openForm In Application.OpenForms()
                        If TypeOf (openForm) Is frmNotify Then
                            isshown = True
                        End If
                    Next

                    Dim isInFront = False
                    For Each openForm In Application.OpenForms()
                        If TypeOf (openForm) Is VERAG_PROG_ALLGEMEIN.frmMessenger Then
                            '  If DirectCast(openForm, VERAG_PROG_ALLGEMEIN.frmMessenger).Focused Then
                            If GetActiveWindow = DirectCast(openForm, VERAG_PROG_ALLGEMEIN.frmMessenger).Handle Then
                                isInFront = True
                            End If
                        End If
                    Next
                    If Not isshown And Not isInFront Then newNotify(frmNotify.cNotify_ART.NEUER_CHAT, 0, "Neue Chat-Nachricht!", -1)
                Else
                    '  Me.Icon = My.Resources.Aviso
                End If
                If Label20.Visible Then Label20.BringToFront()
            End If

        Catch ex As Exception

        End Try
    End Sub

    Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles picAVISOMessenger.Click

        For Each openForm In Application.OpenForms()
            If TypeOf (openForm) Is VERAG_PROG_ALLGEMEIN.frmMessenger Then
                CType(openForm, VERAG_PROG_ALLGEMEIN.frmMessenger).WindowState = FormWindowState.Normal
                CType(openForm, VERAG_PROG_ALLGEMEIN.frmMessenger).BringToFront()
                Exit Sub
            End If
        Next

        Dim f As New VERAG_PROG_ALLGEMEIN.frmMessenger
        f.Location = Cursor.Position
        f.Show(Me)
    End Sub


    Sub newNotify(art As frmNotify.cNotify_ART, title As Integer, text As String, id As Integer)
        '   Exit Sub 'NICHT AKTIV
        Dim formTmp = Me.ActiveForm
        Dim frmNotify As New frmNotify(art, title, text, id)
        frmNotify.TimerInterval = 1200000 ' 20min


        Dim tmpY As Integer = Me.Height
        If tmpY <= 0 Then tmpY = My.Computer.Screen.WorkingArea.Height

        For Each ft As Form In Application.OpenForms
            ' MsgBox(ft.GetType.ToString)
            If ft.GetType.ToString = "frmNotify" Then
                If ft.Location.Y < tmpY Then tmpY = ft.Location.Y
            End If
        Next
        frmNotify.Y = tmpY - frmNotify.Height - 10
        frmNotify.Show()

        If formTmp IsNot Nothing Then formTmp.Focus()
    End Sub

    Dim secondmonitorSelected = False
    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Dim secondaryMonitor = Screen.AllScreens.FirstOrDefault(Function(x) Not x.Primary)
        If secondmonitorSelected Then
            secondaryMonitor = Screen.AllScreens.FirstOrDefault(Function(x) x.Primary) 'Hauptbildschirm
        End If
        If secondaryMonitor IsNot Nothing Then
            Me.WindowState = FormWindowState.Normal
            Dim newLocation = secondaryMonitor.Bounds.Location
            newLocation.Offset(0, 0) ' adjust as needed
            Me.Location = newLocation
            Me.Refresh()
            Me.WindowState = FormWindowState.Maximized
            ' Also see Me.Size and Me.Bounds
        End If
        secondmonitorSelected = Not secondmonitorSelected
    End Sub

    Private Sub USTVAnträgeToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles USTVAnträgeToolStripMenuItem.Click
        If VERAG_PROG_ALLGEMEIN.cBerechtignunen.CHECK_BERECHTIGUNG("MDM_Hauptmenü", Me) = 0 Then
            clrPnlMain()
            Dim usrCntl As New usrCntlUSTV
            usrCntl.Dock = System.Windows.Forms.DockStyle.Fill
            pnlMain.Controls.Add(usrCntl)
        Else
            setInfo("err", "Daten sind nicht freigegeben!", 3)
        End If
    End Sub

    Private Sub BesuchsberichteToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles BesuchsberichteToolStripMenuItem1.Click
        If VERAG_PROG_ALLGEMEIN.cBerechtignunen.CHECK_BERECHTIGUNG("MDM_Besuchsberichte", Me) = 0 Then
            clrPnlMain()
            Dim usrCntl As New usrCntlBesuchsberichte
            usrCntl.Dock = System.Windows.Forms.DockStyle.Fill
            pnlMain.Controls.Add(usrCntl)
        Else
            setInfo("err", "Keine Berechtigung!", 3)
        End If
    End Sub

    Private Sub MenuStrip1_ItemClicked(sender As Object, e As ToolStripItemClickedEventArgs) Handles MenuStrip1.ItemClicked

    End Sub

    Private Sub mnueEssensbestellung_Click(sender As Object, e As EventArgs) Handles mnueEssensbestellung.Click
        Dim f As New frmEssensbestellungen()
        f.Show()
    End Sub

    Private Sub btnMitarbeiterKonto_Click(sender As Object, e As EventArgs) Handles btnMitarbeiterKonto.Click
        Me.Cursor = Cursors.WaitCursor
        Dim f As New VERAG_PROG_ALLGEMEIN.frmMitarbeitersuche(VERAG_PROG_ALLGEMEIN.cAllgemein.USRID)
        f.Show()
        Me.Cursor = Cursors.Default
    End Sub

    Public Sub setProfileImage()
        Try
            If VERAG_PROG_ALLGEMEIN.cAllgemein.MITARBEITER.mit_foto_DocId IsNot Nothing Then

                Dim originalImage = Image.FromFile(cDATENSERVER.GET_PDFPath_BY_DocID(VERAG_PROG_ALLGEMEIN.cAllgemein.MITARBEITER.mit_foto_DocId)) 'My.Resources.Koerfez61

                Dim picSize = 35

                Dim space_X As Integer = 0
                Dim space_Y As Integer = 0
                Dim picWidth As Integer = 0
                Dim picHheight As Integer = 0

                Dim factor As Double = 1
                If originalImage.Height < originalImage.Width Then
                    factor = picSize / originalImage.Height
                    picWidth = originalImage.Width * factor
                    picHheight = originalImage.Height * factor


                Else
                    factor = picSize / originalImage.Width
                    picWidth = originalImage.Width * factor
                    picHheight = originalImage.Height * factor

                End If


                Dim croppedImage As New Bitmap(picWidth, picHheight)

                Using g = Graphics.FromImage(croppedImage)
                    Dim path As New GraphicsPath


                    path.AddEllipse(0, 0, picSize, picSize)

                    Dim reg As New Region(path)


                    g.Clip = reg

                    g.DrawImage(originalImage, 0, 0, picWidth, picHheight)
                End Using


                btnMitarbeiterKonto.Image = croppedImage

                Exit Sub
            End If
        Catch ex As Exception
            MsgBox(ex.Message & ex.StackTrace)

        End Try
        'btnMitarbeiterKonto.Image = My.Resources.mitarbeiter_s

    End Sub

    Private Sub RechnungsdatenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles RechnungsdatenToolStripMenuItem.Click
        If VERAG_PROG_ALLGEMEIN.cBerechtignunen.CHECK_BERECHTIGUNG("MDM_Fakturierung", Me) = 0 Then
            'clrPnlMain()
            'Dim usrCntl As New SDL.frmRechnungenSuche
            'usrCntl.Dock = System.Windows.Forms.DockStyle.Fill
            'pnlMain.Controls.Add(usrCntl)
            Dim f As New SDL.frmRechnungenSuche
            f.Show(Me)
        Else
            setInfo("err", "Daten sind noch nicht freigegeben!", 3)
        End If
    End Sub
End Class

Public Class MyRenderer
    Inherits ToolStripProfessionalRenderer

    Protected Overloads Overrides Sub OnRenderMenuItemBackground(ByVal e As ToolStripItemRenderEventArgs)
        If e.Item.GetCurrentParent.Name = "MenuStripSDL" Then
            Dim rc As New Rectangle(Point.Empty, e.Item.Size)
            Dim c As Color = IIf(e.Item.Selected, Color.LightSteelBlue, Color.FromArgb(0, 54, 128))
            e.Item.ForeColor = IIf(e.Item.Selected, Color.Black, Color.White)
            'e.Graphics.DrawRectangle(New Pen(Brushes.White), rc)
            Using brush As New SolidBrush(c)
                e.Graphics.FillRectangle(brush, rc)
            End Using
        Else
            Dim rc As New Rectangle(Point.Empty, e.Item.Size)
            Dim c As Color = IIf(e.Item.Selected, Color.LightSteelBlue, Color.White)

            Using brush As New SolidBrush(c)
                e.Graphics.FillRectangle(brush, rc)
            End Using
        End If

    End Sub

End Class