Attribute VB_Name = "Promo"
Option Explicit


'----------------------------------------------------------
'ulozeni proma
'----------------------------------------------------------

Public Sub UlozPromo(ind As Integer)

On Error GoTo Dal2:

    If (Form1.TxtProOdDen(ind).Text <> "" And Form1.TxtProOdMes(ind).Text <> "" And Form1.TxtProOdRok(ind).Text <> "" And Form1.TxtProDoDen(ind).Text <> "" And Form1.TxtProDoMes(ind).Text <> "" And Form1.TxtProDoRok(ind).Text <> "") Then
    Else: GoTo Dal2
    End If
    
    If (PromoI(ind) > 0) Then
    
        If (Form1.TxtProText(ind) <> "") Then
    
            On Error GoTo Dal:
        
            Select Case ind:
            
                Case 0: Kill (CestaSoubory + "/RDS-Promo/RDS-Promo1/" + "*.rds")
                Case 1: Kill (CestaSoubory + "/RDS-Promo/RDS-Promo2/" + "*.rds")
                Case 2: Kill (CestaSoubory + "/RDS-Promo/RDS-Promo3/" + "*.rds")
                Case 3: Kill (CestaSoubory + "/RDS-Promo/RDS-Promo4/" + "*.rds")
                Case 4: Kill (CestaSoubory + "/RDS-Promo/RDS-Promo5/" + "*.rds")
            
            End Select
    
Dal:
        
            PromoOd = CDate(Prevod(Form1.TxtProOdDen(ind).Text) + "/" + Prevod(Form1.TxtProOdMes(ind).Text) + "/" + Form1.TxtProOdRok(ind).Text)
            PromoDo = CDate(Prevod(Form1.TxtProDoDen(ind).Text) + "/" + Prevod(Form1.TxtProDoMes(ind).Text) + "/" + Form1.TxtProDoRok(ind).Text)
            
            If (PromoDo < PromoOd) Then
                GoTo Dal2
            End If
        
            PromoPom = PromoOd
            
            Do While (DateDiff("d", PromoPom, PromoDo) >= 0)
                
                
                If (Form1.ChckProPo(ind).Value = 1) Then
                    If (Weekday(PromoPom, vbMonday) = 1) Then
                        ZapisPromo ind
                    End If
                End If
                
                If (Form1.ChckProUt(ind).Value = 1) Then
                    If (Weekday(PromoPom, vbMonday) = 2) Then
                        ZapisPromo ind
                    End If
                End If
                
                If (Form1.ChckProSt(ind).Value = 1) Then
                    If (Weekday(PromoPom, vbMonday) = 3) Then
                        ZapisPromo ind
                    End If
                End If
                
                If (Form1.ChckProCt(ind).Value = 1) Then
                    If (Weekday(PromoPom, vbMonday) = 4) Then
                        ZapisPromo ind
                    End If
                End If
                
                If (Form1.ChckProPa(ind).Value = 1) Then
                    If (Weekday(PromoPom, vbMonday) = 5) Then
                        ZapisPromo ind
                    End If
                End If
                
                If (Form1.ChckProSo(ind).Value = 1) Then
                    If (Weekday(PromoPom, vbMonday) = 6) Then
                        ZapisPromo ind
                    End If
                End If
                
                If (Form1.ChckProNe(ind).Value = 1) Then
                    If (Weekday(PromoPom, vbMonday) = 7) Then
                        ZapisPromo ind
                    End If
                End If
                
                PromoPom = DateAdd("d", 1, PromoPom)
                
                PromoDatum(ind, 1) = Prevod(Form1.TxtProOdDen(ind).Text) & Prevod(Form1.TxtProOdMes(ind).Text) & Form1.TxtProOdRok(ind).Text
                PromoDatum(ind, 2) = Prevod(Form1.TxtProDoDen(ind).Text) & Prevod(Form1.TxtProDoMes(ind).Text) & Form1.TxtProDoRok(ind).Text
                PromoPozn(ind) = Form1.TxtProPoznamka(ind).Text
                PromoText(ind) = Form1.TxtProText(ind).Text
                ZmenaP(ind) = False
                
            Loop
        
            ZapisPromoDoIni ind
            LogPromo ind, "save"
        
        Else
            
           MsgBox "Zadejte text proma"
        
        End If
    
    Else
    
        MsgBox "Nejsou zadany vysilaci casy"
    
    End If

Exit Sub

Dal2:

End Sub


'----------------------------------------------------------
'zapis proma do souboru
'----------------------------------------------------------

Public Sub ZapisPromo(ind As Integer)

    Dim i As Long
    Dim j As Integer
    Dim k As Long
    Dim HH As Integer
    Dim MM As Integer
    Dim PromoAll(1440, 4) As String
    
    For i = 1 To 1439
        PromoAll(i, ind) = ""
    Next i
    
    Select Case ind:
    
        Case 0: Open CestaSoubory + "/RDS-Promo/RDS-Promo1/" + GenerujNazevSouboru(PromoPom) + "Promo1.rds" For Output As #4
        Case 1: Open CestaSoubory + "/RDS-Promo/RDS-Promo2/" + GenerujNazevSouboru(PromoPom) + "Promo2.rds" For Output As #4
        Case 2: Open CestaSoubory + "/RDS-Promo/RDS-Promo3/" + GenerujNazevSouboru(PromoPom) + "Promo3.rds" For Output As #4
        Case 3: Open CestaSoubory + "/RDS-Promo/RDS-Promo4/" + GenerujNazevSouboru(PromoPom) + "Promo4.rds" For Output As #4
        Case 4: Open CestaSoubory + "/RDS-Promo/RDS-Promo5/" + GenerujNazevSouboru(PromoPom) + "Promo5.rds" For Output As #4
    
    End Select
    
        For j = 1 To PromoI(ind)
                
                HH = CInt(Left(PromoCas(j, ind), 2))
                MM = CInt(Right(PromoCas(j, ind), 2))
    
            For i = 1 To 1439
                If (i = (HH * 60 + MM)) Then
                    PromoAll(i, ind) = Form1.TxtProText(ind).Text
                End If
            Next i
        
        Next j
            
    For k = 1 To 1440
        Print #4, PromoAll(k, ind)
    Next k
    
    Close #4
    
    FreeFile (4)

End Sub


'----------------------------------------------------------
'pridani casu
'----------------------------------------------------------

Public Sub PromoPridatCas(ind As Integer)

On Error GoTo Pokr

    PromoHod = CInt(Form1.TxtProPCHod(ind).Text)
    PromoMin = CInt(Form1.TxtProPCMin(ind).Text)
    
    If (PromoHod < 25) And (PromoMin < 60) Then
        PromoI(ind) = PromoI(ind) + 1
        PromoCas(PromoI(ind), (ind)) = Prevod(PromoHod) + ":" + Prevod(PromoMin)
        Preskladani ind
    Else:  MsgBox "Zadali jste nesprávný časový údaj"

    End If

    ZmenaP(ind) = True

Exit Sub

Pokr:

    MsgBox "Zadali jste nesprávný časový údaj"

End Sub


'----------------------------------------------------------
'smazani vybraneho casu casu
'----------------------------------------------------------

Public Sub PromoUbratCas(ind As Integer)

    Dim LI As Integer
    Dim konec As Boolean
    Dim j As Integer
    
    konec = False

    Do While (j < Form1.LstPromoCasy(ind).ListCount And konec = False)
        
        If (Form1.LstPromoCasy(ind).Selected(j) = True) Then
            konec = True
            LI = Form1.LstPromoCasy(ind).ListIndex
        End If
        
        j = j + 1
        
    Loop
    
    LC = Form1.LstPromoCasy(ind).ListCount
    
    If (LI >= 0) Then
    
        Form1.LstPromoCasy(ind).Clear
        Do While (LI < LC - 1)
            PromoCas(LI + 1, (ind)) = PromoCas(LI + 2, (ind))
            LI = LI + 1
        Loop
        
        PromoI(ind) = LC - 1
        
        Preskladani ind
        
        ZmenaP(ind) = True
    
    End If

End Sub


'----------------------------------------------------------
'smazani vsech casu
'----------------------------------------------------------

Public Sub SmazVsechnyCasy(ind As Integer)

Dim i As Integer

LC = 0

For i = 1 To PromoI(ind)
    PromoCas(i, (ind)) = ""
Next i

PromoI(ind) = 0
Form1.LstPromoCasy(ind).Clear

End Sub


'----------------------------------------------------------
'serazovani podle abecedy
'----------------------------------------------------------

Public Sub Preskladani(ind As Integer)

    Dim i As Integer
    Dim j As Integer
    Dim Ppom As String
        
        Form1.LstPromoCasy(ind).Clear
        
    For i = 1 To PromoI(ind)
        For j = 1 To PromoI(ind) - 1
        
            If (PromoCas(j, (ind)) > PromoCas(j + 1, (ind))) Then
                Ppom = PromoCas(j, (ind))
                PromoCas(j, (ind)) = PromoCas(j + 1, (ind))
                PromoCas(j + 1, (ind)) = Ppom
                
            End If
        Next j
    Next i
    
    For i = 1 To PromoI(ind)
        Form1.LstPromoCasy(ind).AddItem PromoCas(i, (ind)), i - 1
    Next i
    
    Form1.LblPromoPocet(ind).Caption = "Počet časů: " & Form1.LstPromoCasy(ind).ListCount
    
End Sub


'----------------------------------------------------------
'generovani nahodneho casu
'----------------------------------------------------------

Public Sub PromoPridatNah(ind As Integer)

    Dim i As Integer
    Dim j As Integer
    Dim B As Boolean
    Dim Oh As Integer
    Dim Dh As Integer
    Dim Om As Integer
    Dim Dm As Integer
    Dim PPromo As String
    Dim Pmax As String
    Dim Pmin As String
    Dim PP As String
    
    Form1.LstPromoCasy(ind).Clear
    
    For i = 1 To PromoI(ind)
        PromoCas(PromoI(ind), (ind)) = ""
    Next i
    
    PromoI(ind) = 0
    i = 1
    B = True

On Error GoTo Pokr

    Oh = CInt(Form1.TxtProOdHod(ind).Text)
    Om = CInt(Form1.TxtProOdMin(ind).Text)
    Dh = CInt(Form1.TxtProDoHod(ind).Text)
    Dm = CInt(Form1.TxtProDoMin(ind).Text)
    
    Pmin = Oh * 60 + Om
    Pmax = Dh * 60 + Dm
    
    If (Form1.TxtProPocOpak(ind).Text < 101) Then
    
        If (Oh < 24) And (Om < 60) And (Dh < 24) And (Dm < 60) And (Pmax >= (Pmin + CInt(Form1.TxtProPocOpak(ind).Text))) Then
        
            Do While (i <= CInt(Form1.TxtProPocOpak(ind).Text))
                
                Randomize   ' Initialize random-number generator.
                
                PP = Int((Pmax - Pmin + 1) * Rnd + Pmin)
                
                PromoHod = PP \ 60
                PromoMin = PP Mod 60
                PPromo = Prevod(PromoHod) + ":" + Prevod(PromoMin)
        
                B = True
                
                If PromoI(ind) > 1 Then
                    For j = 1 To PromoI(ind)
                
                        If (PromoCas(j, (ind)) = PPromo) Then
                            B = False
                        End If
                    
                    Next j
                End If
                     
                    If (B = True) Then
                        PromoI(ind) = PromoI(ind) + 1
                        PromoCas(PromoI(ind), (ind)) = PPromo
                        Preskladani ind
                        i = i + 1
                    End If
             
            Loop
        
            ZmenaP(ind) = True
    
        Else:
            MsgBox "Zadali jste nesprávný časový údaj"
        End If

    Else:
        MsgBox "Maximální počet opakování je 100"
    End If

Exit Sub

Pokr:

    If (Form1.TxtProPocOpak(ind).Text <> "") Then
        MsgBox "Zadali jste nesprávný časový údaj"
    Else
        MsgBox "Není zadán počet opakování"
    End If
    
End Sub


'----------------------------------------------------------
'odstraneni proma
'----------------------------------------------------------

Public Sub SmazPromo(ind As Integer)

  
    
    Form1.FraPromoSmaz.Visible = True
    Form1.LblPromoIndxS.Caption = ind + 1
    OdpovedUzivateleNaUlozeni = 0

    Do While OdpovedUzivateleNaUlozeni = 0
        DoEvents
        If (OdpovedUzivateleNaUlozeni = 2) Then
            Smazat (ind)
        End If
    Loop
    
    Form1.FraPromoSmaz.Visible = False
    ZmenaP(ind) = False
    LogPromo ind, "del"
End Sub


'----------------------------------------------------------
'odstraneni proma pokracovani
'----------------------------------------------------------

Public Sub Smazat(ind As Integer)

  Dim i As Integer

On Error GoTo Dal1:
    Kill (CestaSoubory & "/RDS-Promo/RDS-Promo" & CStr(ind + 1) & "/" & "*.rds")

Dal1:
    Form1.CmdProPriCas(ind).Enabled = True
    Form1.CmdProSmazCas(ind).Enabled = True
    Form1.ChckProPo(ind).Value = 0
    Form1.ChckProUt(ind).Value = 0
    Form1.ChckProSt(ind).Value = 0
    Form1.ChckProCt(ind).Value = 0
    Form1.ChckProPa(ind).Value = 0
    Form1.ChckProSo(ind).Value = 0
    Form1.ChckProNe(ind).Value = 0
    Form1.TxtProText(ind).Text = ""
    Form1.TxtProOdDen(ind).Text = ""
    Form1.TxtProDoDen(ind).Text = ""
    Form1.TxtProOdMes(ind).Text = ""
    Form1.TxtProDoMes(ind).Text = ""
    Form1.TxtProOdRok(ind).Text = ""
    Form1.TxtProDoRok(ind).Text = ""
    Form1.TxtProPCHod(ind).Text = ""
    Form1.TxtProPCMin(ind).Text = ""
    Form1.TxtProOdHod(ind).Text = ""
    Form1.TxtProDoHod(ind).Text = ""
    Form1.TxtProOdMin(ind).Text = ""
    Form1.TxtProDoMin(ind).Text = ""
    Form1.TxtProPocOpak(ind).Text = ""
    Form1.TxtProPoznamka(ind).Text = ""
    Form1.LstPromoCasy(ind).Clear

    For i = 1 To PromoI(ind)
        PromoCas(i, (ind)) = ""
    Next i
    
    PromoI(ind) = 0
    
    ZapisPromoDoIni ind

End Sub


'----------------------------------------------------------
'kontrola obsazeni proma
'----------------------------------------------------------

Public Sub KontrolaProma(ind As Integer)

    Dim p As String
     
    If Dir(CestaSoubory & "\RDS-Promo\Promo" & CStr(ind + 1) & ".rds") <> "" Then
    
        Open CestaSoubory & "\RDS-Promo\Promo" & CStr(ind + 1) & ".rds" For Input As #8
        
        Input #8, p
        
            If (p <> AktJmeno) Then
            
                Form1.FraPromoObsazeno.Visible = True
                Form1.LblObsazeno = p
                Form1.LblPopis = Form1.TxtProPoznamka(ind).Text
                ID = False
                
            Else:
            
                ID = True
            
            End If
    
        Close #8
    
    Else: Open CestaSoubory & "\RDS-Promo\Promo" & CStr(ind + 1) & ".rds" For Output As #8
            
            Print #8, AktJmeno
    
            Close #8
             
          ID = True
          
    End If

End Sub


'----------------------------------------------------------
'zruseni zamceni proma
'----------------------------------------------------------

Public Sub ZrusZamceniproma(ind As Integer)

    If (ID = True) Then
    
        Kill (CestaSoubory & "\RDS-Promo\Promo" & CStr(ind + 1) & ".rds")
        ID = False
    
    End If

End Sub


'----------------------------------------------------------
'prebrani proma
'----------------------------------------------------------

Public Sub PrevzitPromo(ind As Integer)

    Kill (CestaSoubory & "\RDS-Promo\Promo" & CStr(ind + 1) & ".rds")

    Open CestaSoubory & "\RDS-Promo\Promo" & CStr(ind + 1) & ".rds" For Output As #8
        
        Print #8, AktJmeno

        Close #8
         
    ID = True
    Form1.FraPromoObsazeno.Visible = False
    
End Sub


'----------------------------------------------------------
'vybrani dne v promu
'----------------------------------------------------------

Public Sub PromoDenClik(Index As Integer, ind As Integer)

    Dim i As Integer
    
    Select Case ind:
        Case 1: i = Form1.ChckProPo(Index).Value
        Case 2: i = Form1.ChckProUt(Index).Value
        Case 3: i = Form1.ChckProSt(Index).Value
        Case 4: i = Form1.ChckProCt(Index).Value
        Case 5: i = Form1.ChckProPa(Index).Value
        Case 6: i = Form1.ChckProSo(Index).Value
        Case 7: i = Form1.ChckProNe(Index).Value
    End Select
    
        If (i = 1) Then
            If (Mid(Ovladani(Index), ind, 1) <> "A") Then
                ZmenaP(Index) = True
            End If
        ElseIf (i = 0) Then
            If (Mid(Ovladani(Index), ind, 1) <> "N") Then
                ZmenaP(Index) = True
            End If
        End If

End Sub
