Attribute VB_Name = "Main"
Option Explicit

'----------------------------------------------------------
'Deklarace promennych
'----------------------------------------------------------

'------------------
'promenne modulu Edit
Public konec As Integer
Public Search As String
Public WherePom As Integer
Public Where As Integer
Public p As Integer
Public CelyText(1442, 2) As String
Public CelyTextZ(1442, 2) As String
Public CasTextu(1442) As String
Public VybranyText(1442, 2) As String
Public PocetVybranych As Integer
Public EditDotaz As Boolean
Public EDen As String
Public EMesic As String
Public ERok As String
Public EDatum As String
Public EDatumPom As Date
Public IDT As Boolean
Public SpustKres As Boolean
Public Pozice As Integer
Public DenNeboNahr As String
Public DenUlozen As Boolean
Public CboCalIndex(2) As Integer
Public ProbihaEditace As Boolean
Public PocetMinut As Integer
Public Kontrola As Boolean

'------------------
'promenne modulu Ini
Public Maska As String
Public Heslo(9) As String
Public Jmeno(9) As String
Public Prava(9) As String
Public HesloSpravce As String
Public Bafr As String
Public Delka As Integer
Public Ovladani(4) As String
Public List As String
Public pok As String

'------------------
'promenne modulu Log
Public IP As String
Public User As String
Public ZmenaLog(1440) As String
Public ZmenaIndx As Integer
Public PomNazev As String

'------------------
'promenne modulu Main
Public Cesta As String
Public CestaSoubory As String
Public a As Integer
Public B As Integer
Public MaskaN As String
Public OdpovedUzivateleNaUlozeni As Integer
Public SouborDenniTextCasModifikace As Date
Public SouborPromoCasModifikace(5) As Date
Public SouborTextNaDnyCasModifikace As Date
Public Z As String
Public StartOk As Boolean
Public TAsec As Integer

'------------------
'promenne modulu Promo
Public PromoOd As Date
Public PromoDo As Date
Public PromoPom As String
Public PromoCas(500, 4) As String
Public PromoMin As Integer
Public PromoHod As Integer
Public PromoI(4) As Integer
Public LC As Integer
Public ZmenaP(4) As Boolean
Public Zalozka As Integer
Public ID As Boolean
Public PromoText(4) As String
Public PromoPozn(4) As String
Public PromoDny As String
Public PromoDatum(4, 2) As String

'------------------
'promenne modulu Setup
Public Zmena As Boolean
Public MaskaZal As String
Public CestaSouboryZal As String

'------------------
'promenne modulu Status
Public Indik As String
Public PomRetez As String
Public Vypocet As Long
Public VysPlan(1450) As String
Public VysPromo(1451) As String
Public FLPosledniZmena As Date
Public FLpom As Integer
Public Kpom As String
Public fso As New FileSystemObject

'------------------
'promenne modulu ZadavaniHesel
Public AktJmeno As String
Public anno As Boolean


'----------------------------------------------------------
'API deklarace funkce ktera umi obsluhovat INI soubory
'----------------------------------------------------------
Declare Function GetPrivateProfileString Lib "kernel32" Alias _
"GetPrivateProfileStringA" (ByVal lpSectionName As String, _
ByVal lpKeyName As Any, _
ByVal lpDefault As String, _
ByVal lpReturnedString As String, _
ByVal nSize As Long, _
ByVal lpFileName As String) As Long

Declare Function GetPrivateProfileInt Lib "kernel32" Alias _
"GetPrivateProfileIntA" (ByVal lpSectionName As String, _
ByVal lpKeyName As String, _
ByVal nDefault As Long, _
ByVal lpFileName As String) As Long

Declare Function WritePrivateProfileString Lib "kernel32" Alias _
"WritePrivateProfileStringA" (ByVal lpSectionName As String, _
ByVal lpKeyName As Any, _
ByVal lpString As Any, _
ByVal lpFileName As String) As Long


'----------------------------------------------------------
'Deklarace funkce ktera zjistuje uzivatelske jmeno pro LOGIVANI
'----------------------------------------------------------
Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long


'----------------------------------------------------------
'spusteni programu
'----------------------------------------------------------

Public Sub Start()

    Dim i As Integer
    Dim j As Integer
    Dim k As Integer
    Dim l As Integer
    
    l = 0
        
    For j = 0 To 23
        For k = 0 To 59
            CasTextu(l) = Prevod(j) & ":" & Prevod(k)
            l = l + 1
        Next k
    Next j
                
    Cesta = App.Path
    Zmena = False
    
    For i = 0 To 4
        ZmenaP(i) = False
    Next i
    
    CteniCfg
    CestaSouboryZal = CestaSoubory
    Zalozka = 0
    EDatumPom = Date
    EDatum = EDatumPom
    Form1.LblEditDen.Caption = Format(EDatum, "dddd  dd . mm . yyyy")
    ID = False
    IDT = False
    Z = ""
    SpustKres = False
    StartOk = False
    EditDotaz = True
    DenNeboNahr = "D"
    DenUlozen = False
    ProbihaEditace = False
    Kontrola = True
    
    Form1.CmdEditOznacit.Enabled = False
    Form1.CmdEditOdznacit.Enabled = False
    Form1.CmdEditSmazatOznacene.Enabled = False
    Form1.CmdEditAdd.Enabled = False
    Form1.CmdEditVlozit.Enabled = False
    Form1.CmdEditDelOzn.Enabled = False
    Form1.CmdEditDel.Enabled = False
    Form1.CmdEditUndo.Enabled = False
    Form1.CmdEditSave.Enabled = False
    Form1.CmdEditNahr.Enabled = False
    Form1.FraEditText4.Locked = True
    Form1.FraEditText4.ForeColor = &H80000008
    Form1.FraEditText4.Font.Bold = False
    
    Form1.LblStatusErr.Visible = False
    Form1.LblStCas.Caption = 60 - Second(Time) & "s"
    Form1.LblStDat(0).Caption = Time
    Form1.LblStDat(1).Caption = Date
    Form1.ProgressBar1.Value = Second(Time)
    Form1.FraStatus.Visible = True
    Form1.FraEdit.Visible = False
    Form1.FraPromo.Visible = False
    Form1.FraSetup.Visible = False
    Form1.FraEditCal.Visible = False
    Form1.FraPromoObsazeno.Visible = False
    Form1.FraForm2.Visible = False
    Form1.FraPromoUloz.Visible = False
    Form1.FraUlozZmeny.Visible = False
    Form1.FraTextObsazeno.Visible = False
    Form1.FraHeslo.Visible = True
    Form1.FraERRORAdr.Visible = False
    Form1.FraEditUloz.Visible = False
    Form1.FraPromoSmaz.Visible = False
    Form1.FraCboCas.ListIndex = 1
    Form1.SSTabNabidka.Tab = 0
    Form1.Timer2.Enabled = True
    Form1.SSTabNabidka.Width = Form1.FraStatus.Width + 250
    Form1.Width = Form1.SSTabNabidka.Width + 320
    
    If (Dir(Cesta & "\RDSEdit.cfg") = "") Then
        Open Cesta & "\RDSEdit.cfg" For Output As #10
            Print #10, "[Config]"
            Print #10, "Cestasoubory=C:\"
        Close #10
    End If
    
    If (KontrolaAdresaru = True) Then
        StartOk = True
        Pokracovani
    Else
        StartOk = False
        Form1.FraERRORAdr.Visible = True
    End If
    
    Pokracovani

End Sub


'----------------------------------------------------------
'pokracovani programu po uspesne kontrole adresarove struktury
'----------------------------------------------------------

Public Sub Pokracovani()

    If (StartOk = True) Then
        
        CteniIni
        MaskaZal = Maska
        CteniPromozIni
        Form1.Timer1.Enabled = True
        Form1.Timer3.Enabled = True
        Vypocet = Hour(Time) * 60 + Minute(Time)
        PraveVysilame
        PraveVysilameZobr
        DenniProgram Date
        VysilaciPlanZobr
        KontrolaFlash
        PocetVybranych = 0
        KontrolaVysilani

        If (Second(Time) >= 50) Then
            PraveVysilame
            Vypocet = Hour(Time) * 60 + Minute(Time) + 1
            DenniProgram Date
        End If
    
    End If

End Sub


'----------------------------------------------------------
'prevod masky
'----------------------------------------------------------

Public Function GenerujNazevSouboru(ByVal mDatum As Date)

    MaskaN = Maska

    a = InStr(MaskaN, "%d")
    If a <> 0 Then
        Mid(MaskaN, a, 2) = Prevod(Day(mDatum))
    End If

    a = InStr(MaskaN, "%m")
    If a <> 0 Then
        Mid(MaskaN, a, 2) = Prevod(Month(mDatum))
    End If

    a = InStr(MaskaN, "%yyy")
    If a <> 0 Then
        Mid(MaskaN, a, 4) = Year(mDatum)
    ElseIf (InStr(MaskaN, "%y") <> 0) Then
        Mid(MaskaN, a, 2) = Right(Year(mDatum), 2)
    End If

    GenerujNazevSouboru = MaskaN

End Function


'----------------------------------------------------------
'uprava nazvu na dvojmistny 9 -> 09 ...
'----------------------------------------------------------

Public Function Prevod(ByVal CastDat As String) As String
    
On Error GoTo chyba
    
    If (CastDat < 10) Then
        Prevod = "0" + CStr(CastDat)
        Prevod = Right(Prevod, 2)
    Else: Prevod = CastDat
    End If
        
Exit Function

chyba:

    Prevod = ""
    
End Function


'----------------------------------------------------------
'vyber cesty
'----------------------------------------------------------

Public Sub UkazCesta()

    Form1.FraForm2.Visible = True

    On Error GoTo chyba

        Form1.DirCesta.Path = CestaSoubory
        Form1.DrvDisk.Drive = Form1.DirCesta.Path
        Form1.TxtForm2Cesta.Text = CestaSoubory
        
    Exit Sub
    
chyba:
    
    Form1.DirCesta.Path = "C:"
    Form1.DrvDisk.Drive = "C:"
    Form1.TxtForm2Cesta.Text = "C:"
    
    Resume Next

End Sub


'----------------------------------------------------------
'kontrola spravnosti vysilani
'----------------------------------------------------------

Public Sub KontrolaVysilani()

    Dim fl As File
    Dim PZ As String
    
    PZ = Date & " " & DateAdd("n", -1, Time)
    
    If (Dir(CestaSoubory & "/PraveVysilame.rds") <> "") Then
    
        Set fl = fso.GetFile(CestaSoubory & "/PraveVysilame.rds")
    
        If (PZ > fl.DateLastModified) Then
            Form1.LblStatusKdy.BackColor = &H8080FF
            Form1.LblStatusErr.Visible = True
            Form1.LblStatusErr.Caption = "Chyba v řídícím softwaru!!!!"
            FLpom = 4
        End If
        
    End If

End Sub


'----------------------------------------------------------
'pri startu se tady kontroluje jestli je uplna pozadovana
'adresarova struktura
'----------------------------------------------------------

Public Function KontrolaAdresaru() As Boolean

    On Error GoTo chyba

    If (Dir(CestaSoubory & "\RDS-Promo\RDS-Promo1", vbDirectory) = "") Then GoTo chyba
    If (Dir(CestaSoubory & "\RDS-Promo\RDS-Promo2", vbDirectory) = "") Then GoTo chyba
    If (Dir(CestaSoubory & "\RDS-Promo\RDS-Promo3", vbDirectory) = "") Then GoTo chyba
    If (Dir(CestaSoubory & "\RDS-Promo\RDS-Promo4", vbDirectory) = "") Then GoTo chyba
    If (Dir(CestaSoubory & "\RDS-Promo\RDS-Promo5", vbDirectory) = "") Then GoTo chyba
    If (Dir(CestaSoubory & "\RDS-Logy", vbDirectory) = "") Then GoTo chyba
    If (Dir(CestaSoubory & "\RDS-Texty", vbDirectory) = "") Then GoTo chyba
    If (Dir(CestaSoubory & "\RDS-Logy\Odvysilano\TA", vbDirectory) = "") Then GoTo chyba
    If (Dir(CestaSoubory & "\RDS-Logy\Odvysilano\Texty", vbDirectory) = "") Then GoTo chyba
    If (Dir(CestaSoubory & "\RDS-Logy\Odvysilano\Promo", vbDirectory) = "") Then GoTo chyba
    If (Dir(CestaSoubory & "\RDS-Logy\Upravovano\Texty", vbDirectory) = "") Then GoTo chyba
    If (Dir(CestaSoubory & "\RDS-Logy\Upravovano\Promo", vbDirectory) = "") Then GoTo chyba
    If (Dir(CestaSoubory & "\RDS-Logy\SystemLog", vbDirectory) = "") Then GoTo chyba
    If (Dir(CestaSoubory & "\RDSEdit.Ini") = "") Then GoTo chyba
        
    KontrolaAdresaru = True
    
    Exit Function
        
chyba:
    
    KontrolaAdresaru = False
    
End Function
