'===========================================================================
' Subject: Appointment Book                   Date: 06-13-03 (  :  )       
'  Author: David Drake                        Code: LIBERTY                
'  Origin: DDrake@acipco.com                Packet: TEXT.ABC
'===========================================================================
'Simple Text Scheduler
'by David Drake
'Released as Open Source May 2003
'Requires LB4

'This calendar code borrowed from ???

[startup]
    if val(Version$) < 4 then notice "Requires LB4 or higher":end

    Y = val(right$(date$("mm/dd/yyyy"),4))
    Q = val(left$(date$("mm/dd/yyyy"),2))
    day = date$("days")

    mainwin 80 40

    'Open appointment data file
    dim appointment$(10000,4)
    dim appointmentHour$(24)
    dim appointmentHourNote$(24)
    open "appointments.dat" for append as #1
    close #1
    open "appointments.dat" for input as #1
    while not(eof(#1))
        line input #1, temp1 'Date (in days since 1901)
        line input #1, temp2 'Time (hour of day 0 - 23)
        line input #1, temp3$ 'Appointment description (short form)
        line input #1, temp4$ 'Appointment (note)
        appointmentTime(appointmentCount,0) = temp1
        appointmentTime(appointmentCount,1) = temp2
        appointment$(appointmentCount,1) = temp3$ 
        appointment$(appointmentCount,2) = temp4$ 
        appointmentCount = appointmentCount + 1
    wend

    appointmentCount = appointmentCount - 1

    print "======================================================="
    print "!       Welcome to Really Simple Text Schedule        !"
    print "!       Open-Source Simplewear by David Drake         !"
    print "!                                                     !"
    print "!       Contact : daviddrake@foundrysearch.com        !"
    print "!      Get more FREE software and source code at      !"
    print "!               www.foundrysearch.com                 !"
    print "======================================================="
    input "Press ENTER for instructions > ";temp$
    cls
    startupFlag = 1
    helpMode$ = "ALL"
    goto [helpAll]


[showMonth] 'Show a month
    showMonth = 1
    showYear = 0
    showDay = 0
    IF showMonth = 1 then F=0 : goto [oneMonth]

[showYear] 'Show a year
    cls
    showYear = 1
    showMonth = 0
    showDay = 0
    F=1 : fcal=1 : TF=80

[oneMonth]
    cls
    C = 1
    T = 0
    L = 0

[FULL]
    IF F = 1 THEN Q = C

    DIM DN$(50), CL$(50, 50), Z$(50)

    RESTORE [information]

    FOR N = 1 TO 12
        READ I$ 
        Z$(N)=I$ 
    NEXT N

    FOR I = 1 TO 7
        READ I$ 
        DN$(I)=I$ 
    NEXT I

    IF F = 1 THEN MN$ = Z$(C)
    MT = 0

    FOR X = 1 TO Q
        READ MN$, FOM, DM
        MT = MT + 1
    NEXT X

    YER = Y
    YR = Y
    IF YER > 1900 THEN YR = YR - 1900
    NLY = INT(YR / 4)
    LY = 0
    xf=YR-(4*int(YR/4))
    if xf=0 THEN LY = 1
    OS = YR + NLY
    IF LY AND MT < 3 THEN OS = OS - 1
    IF LY AND MT = 2 THEN DM = 29
    OS = OS-(7*int(OS/7))
    tf= FOM+OS
    FOM = tf-(int(tf/7))
    IF FOM = 0 THEN FOM = 7

    FOR I = 1 TO FOM - 1
        CL$(I, 1) = " "
    NEXT I

    day = 1

    FOR I = FOM TO 7
        CL$(I, 1) = STR$(day)
        day = day + 1
    NEXT I

    FOR J = 2 TO 6
        FOR I = 1 TO 7
            CL$(I, J) = STR$(day)
            day = day + 1
            IF day > DM THEN I = 7: J = 6
        NEXT I
    NEXT J

    IF F = 0 GOTO [makeOneMonth]

    'Display a complete year
    t$= MN$+ "  "+ str$(YER)
    print tab(18);t$ 
    print
    c=6+T
    l=2+L

    FOR I = 1 TO 7
        t$=DN$(I)
        print tab(I*6);t$;
        c=4+T
        l=I+L+2
    NEXT I
    print

    FOR J = 1 TO 6
        FOR I = 1 TO 7
            V = I + L + 2
            H = J * 3 + 5 + T
            t$=CL$(I,J)

            'Add <> around dates with appointments
            dateText$ = MN$+"/"+t$+"/"+str$(YER)
            dayCheck = date$(dateText$)

            for a = 1 to appointmentCount
                if appointmentTime(a,0) = dayCheck then
                    t$ = "<"+t$+">"
                    exit for
                end if
            next a


            print tab(I*6);t$;
            c=H
            l=V
        NEXT I
    print
    NEXT J

    C = C + 1
    T = T + 24
    IF T > TF THEN T = 0: L = L + 11
    IF C < 13 GOTO [FULL]

    if TF=60 then return
    c=30
    l=34
    goto [getCommands]

[makeOneMonth] 'Display one month
    t$= MN$+" "+str$(YER)
    print tab(18);t$ 
    print
    c=11
    l=1
    P = 3
    FOR I = 1 TO 7
        t$= DN$(I)
        print tab(I*6);t$;
        c=1
        l=P
        P = P + 3
    NEXT I

    print:print

    FOR J = 1 TO 6
        P = 3
        FOR I = 1 TO 7
            H = J * 7
            t$= CL$(I,J)

            'Add <> around dates with appointments
            dateText$ = MN$+"/"+t$+"/"+str$(YER)
            dayCheck = date$(dateText$)

            for a = 1 to appointmentCount
                if appointmentTime(a,0) = dayCheck then
                    t$ = "<"+t$+">"
                    exit for
                end if
            next a

            print tab(I*6);t$;
            c=H
            l=P
            P = P + 3
        NEXT I
    print:print
    NEXT J
    c=30
    l=34
    goto [getCommands]

[showDay] ' Show one day
    cls
    redim appointmentHour$(24)
    redim appointmentHourNote$(24)
    print "======================================================="
    print "Appointments for ";date$(day)
    print "======================================================="
    noAppointments = 1
    for a = 1 to appointmentCount
        if appointmentTime(a,0) <> 0 then noAppointments = 0
        if appointmentTime(a,0) = day then
            appointmentHour$(appointmentTime(a,1)) = appointment$(a,1)
            appointmentHourNote$(appointmentTime(a,1)) = appointment$(a,2)
        end if
    next a

    if noAppointments = 1 then
        print "You have no appointments!"
    else
        for hour = 0 to 23
            if appointmentHourNote$(hour) <> "" then
                print "N ";
            else
                print "  ";
            end if
            print tab(2);hour;":00";tab(9);appointmentHour$(hour)
        next hour
    end if
    print "======================================================="
    goto [getCommands]

[addAppointment] ' Add an appointment
    print "======================================================="
    print "Add an appointment"
    print "======================================================="
    print "Enter date of appointment (default = ";date$(day);")";
    input temp$ 
    if temp$ <> "" then day = date$(temp$)
    print "Enter hour of appointment (0-23)";
    input temp
    if temp > 0 and temp < 24 then
        hour = temp
    else
        goto [addAppointment]
    end if
    print "Enter appointment description (60 chars max)";
    input description$ 
    if len(description$) > 60 then description$ = left$(description$,60)
    print "Enter notes about appointment";
    input note$ 
    print "======================================================="
    print "Adding the following appointment:"
    print "On ";date$(day);" at ";hour;":00"
    print "Desc: ";description$ 
    print "Note: ";note$ 
    print "======================================================="
    input "Add this appointment (y/n)?";yn$ 
    if lower$(yn$) <> "y" then
        print "Not added"
        goto [getCommands]
    else
        appointmentCount = appointmentCount + 1
        appointmentTime(appointmentCount,0) = day
        appointmentTime(appointmentCount,1) = hour
        appointment$(appointmentCount,1) = description$ 
        appointment$(appointmentCount,2) = note$ 
    end if
    goto [showDay]

[editAppointment] 'Edit an appointment
    print "======================================================="
    foundIt = 0
    for a = 1 to appointmentCount
        if appointmentTime(a,0) = day and appointmentTime(a,1) = HOUR then
            print date$(appointmentTime(a,0));" at ";appointmentTime(a,1);":00"
            print "Desc: ";appointment$(a,1)
            print "Note: ";appointment$(a,2)
            foundIt = 1
            print "======================================================="
        end if
    next a
    if foundIt = 1 then
        input "Are you sure you want to delete this (y/n)";temp$ 
        if lower$(temp$) = "y" then
            for z = a to appointmentCount
                appointmentTime(z,0) = appointmentTime(z+1,0)
                appointmentTime(z,1) = appointmentTime(z+1,1)
                appointment$(z,1) = appointment$(z+1,1)
                appointment$(z,2) = appointment$(z+1,2)
            next z
            appointmentTime(appointmentCount,0) = 0
            appointmentTime(appointmentCount,1) = 0
            appointment$(appointmentCount,1) = ""
            appointment$(appointmentCount,2) = ""
            appointmentCount = appointmentCount - 1
            print "Appointment deleted!"
        else
            print "Not deleted!"
        end if
    else
        print "Appointment not found"
    end if
    goto [getCommands]

[viewAppointment] ' View an appointment
    print "======================================================="
    for a = 1 to appointmentCount
        if appointmentTime(a,0) = day and appointmentTime(a,1) = HOUR then
            print date$(appointmentTime(a,0));" at ";appointmentTime(a,1);":00"
            print "Desc: ";appointment$(a,1)
            print "Note: ";appointment$(a,2)
        end if
    next a
    print "======================================================="
    goto [getCommands]

[delAppointment] ' Delete an appointment
    print "======================================================="
    foundIt = 0
    for a = 1 to appointmentCount
        if appointmentTime(a,0) = day and appointmentTime(a,1) = HOUR then
            print date$(appointmentTime(a,0));" at ";appointmentTime(a,1);":00"
            print "Desc: ";appointment$(a,1)
            print "Note: ";appointment$(a,2)
            foundIt = 1
            print "======================================================="
        end if
    next a
    if foundIt = 1 then
        input "Are you sure you want to delete this (y/n)";temp$ 
        if lower$(temp$) = "y" then
            for z = a to appointmentCount
                appointmentTime(z,0) = appointmentTime(z+1,0)
                appointmentTime(z,1) = appointmentTime(z+1,1)
                appointment$(z,1) = appointment$(z+1,1)
                appointment$(z,2) = appointment$(z+1,2)
            next z
            appointmentTime(appointmentCount,0) = 0
            appointmentTime(appointmentCount,1) = 0
            appointment$(appointmentCount,1) = ""
            appointment$(appointmentCount,2) = ""
            appointmentCount = appointmentCount - 1
            print "Appointment deleted!"
        else
            print "Not deleted!"
        end if
    else
        print "Appointment not found"
    end if
    goto [getCommands]



[getCommands] ' Get commands
    print mode$;
    input " mode > ";com$ 
    if com$ = "" then [getCommands]

    'Convert to one case
    com$ = lower$(com$)

    if com$ = "help all" then
        helpMode$ = "ALL"
        goto [helpAll]
    end if

    if com$ = "help" then [helpAll]

    'Check single-letter shortct commands
    if com$ = "t" then com$ = "today"
    if com$ = "y" then com$ = "year"
    if com$ = "m" then com$ = "month"
    if com$ = "d" then com$ = "day"
    if com$ = "a" then com$ = "add"
    if com$ = "q" then com$ = "quit"
    if com$ = "n" then com$ = "next"
    if com$ = "l" then com$ = "last"
    if word$(com$,1) = "e" then com$ = "edit " + word$(com$,2)
    if word$(com$,1) = "v" then com$ = "view " + word$(com$,2)

    'Show specific year
    if val(left$(com$,4)) = 2003 then
        Y = 2003
        goto [showYear]
    end if

    if val(left$(com$,4)) = 2004 then
        Y = 2004
        goto [showYear]
    end if

    if val(left$(com$,4)) = 2005 then
        Y = 2005
        goto [showYear]
    end if

    'Show secific month
    if com$ = "january" or com$ = "jan" then
            mode$ = "MONTH"
            Q = 1
            goto [showMonth]
    end if
    if com$ = "february" or com$ = "feb" then
            mode$ = "MONTH"
            Q = 2
            goto [showMonth]
    end if
    if com$ = "march" or com$ = "mar" then
            mode$ = "MONTH"
            Q = 3
            goto [showMonth]
    end if
    if com$ = "april" or com$ = "apr" then
            mode$ = "MONTH"
            Q = 4
            goto [showMonth]
    end if
    if com$ = "may" then
            mode$ = "MONTH"
            Q = 5
            goto [showMonth]
    end if
    if com$ = "jun" or com$ = "jun" then
            mode$ = "MONTH"
            Q = 6
            goto [showMonth]
    end if
    if com$ = "july" or com$ = "jul" then
            mode$ = "MONTH"
            Q = 7
            goto [showMonth]
    end if
    if com$ = "august" or com$ = "aug" then
            mode$ = "MONTH"
            Q = 8
            goto [showMonth]
    end if
    if com$ = "september" or com$ = "sep" then
            mode$ = "MONTH"
            Q = 9
            goto [showMonth]
    end if
    if com$ = "october" or com$ = "oct" then
            mode$ = "MONTH"
            Q = 10
            goto [showMonth]
    end if
    if com$ = "november" or com$ = "nov" then
            mode$ = "MONTH"
            Q = 11
            goto [showMonth]
    end if
    if com$ = "december" or com$ = "dec" then
            mode$ = "MONTH"
            Q = 12
            goto [showMonth]
    end if

    'Show specific date
    if date$(com$) >= 37255 and date$(com$) <= 38350 then
        day = date$(com$)
        goto [showDay]
    end if

    'Other commands
    select case com$ 
        case "today"
            day = date$("days")
            mode$ = "DAY"
            goto [showDay]
        case "quit"
            goto [quit]
        case "day"
            mode$ = "DAY"
            goto [showDay]
        case "month"
            mode$ = "MONTH"
            Q = val(left$(date$("mm/dd/yyyy"),2))
            goto [showMonth]
        case "year"
            mode$ = "YEAR"
            goto [showYear]
        case "add"
            goto [addAppointment]
    end select

    'Commands while in DAY mode
    if mode$ = "DAY" then
        if len(com$) < 3 and val(com$) > -1 and val(com$) < 24 then
            HOUR = val(com$)
            goto [viewAppointment]
        end if

        select case word$(com$,1)
            case "next"
                day = day + 1
                goto [showDay]
            case "last"
                day = day - 1
                goto [showDay]
            case "edit"
                HOUR = val(right$(com$,2))
                goto [editAppointment]
            case "view"
                HOUR = val(right$(com$,2))
                goto [viewAppointment]
            case "del"
                HOUR = val(right$(com$,2))
                goto [delAppointment]
        end select
    end if


    'Commands while in MONTH mode
    if mode$ = "MONTH" then
        if val(com$) > 0 and val(com$) < 32 then
            mode$ = "DAY"
            dateText$ = MN$+"/"+com$+"/"+str$(YER)
            day = date$(dateText$)
            goto [showDay]
        end if

        select case com$ 
            case "next"
                Q = Q + 1
                if Q > 12 then Q = 1:Y = Y + 1
                goto [showMonth]
            case "last"
                Q = Q - 1
                if Q < 1 then Q = 12:Y = Y - 1
                goto [showMonth]
        end select
    end if

    'Commands while in YEAR mode
    if mode$ = "YEAR" then
        select case com$ 
            case "next"
                Y = Y + 1
                goto [showYear]
            case "last"
                Y = Y - 1
                goto [showYear]
        end select
    end if

    print "Invalid command!"
    goto [getCommands]

[DAYS] 'Days data
    DATA 31,29,31,30,31,30,31,31,30,31,30,31

[quit] 'Exit program
    close #1

    open "appointments.dat" for output as #1
    for a = 0 to appointmentCount
        print #1, appointmentTime(a,0)
        print #1, appointmentTime(a,1)
        print #1, appointment$(a,1)
        print #1, appointment$(a,2)
    next a
    close #1
    print "Program terminated normally... Thanks for using Simple Text Scheduler!"
    end

[information] 'Month data
    DATA "January","February","March","April","May"
    DATA "June","July","August","September","October","November","December"
    DATA "Mon","Tue","Wed","Thu","Fri","Sat","Sun"
    DATA "January",1,31,"February",4,28,"March",4,31,"April",7,30
    DATA "May",2,31,"June",5,30,"July",7,31,"August",3,31,"September",6,30
    DATA "October",1,31,"November",4,30,"December",6,31
    DATA 0,3,3,6,1,4,6,2,5,0,3,5,"Sunday","Monday","Tuesday"
    DATA "Wednesday","Thursday","Friday","Saturday","Sunday"
    end

[helpAll] 'Display instructions
    if helpMode$ = "ALL" then
        print tab(3);"HELP ALL will display this list [single-letter"
    else
        print tab(3);"HELP will display this list [single-letter"
    end if
    print tab(3);"   shortcuts are shown in parentheses () ]"
    print ""
    print tab(3);"Global commands:"
    print tab(4);"(D)DAY.......";tab(15);"Switch to single-day view"
    print tab(4);"(T)TODAY.....";tab(15);"Show today's appointments"
    print tab(4);"11/27/2003...";tab(15);"Show appointments for date"
    print tab(4);"May 1, 2003..";tab(15);"Show appointments for date"
    print tab(4);"dec 1 2004...";tab(15);"Show appointments for date"
    print tab(4);"1 dec 2003...";tab(15);"Show appointments for date"
    print tab(4);"12.1.2003....";tab(15);"Show appointments for date"
    print tab(4);"JANUARY......";tab(15);"Show calendar for named month"
    print tab(4);"JAN..........";tab(15);"Show calendar for named month"
    print tab(4);"(M)MONTH.....";tab(15);"Switch to month view"
    print tab(4);"(Y)YEAR......";tab(15);"Switch to year view"
    print tab(4);"(A)ADD.......";tab(15);"Add an appointment"
    print tab(4);"(Q)QUIT......";tab(15);"Exit Really Simple Text Schedule"
    print "======================================================="
    if helpMode$ = "ALL" or mode$ = "DAY" then
    print tab(3);"DAY mode commands:"
    print tab(4);"(E)EDIT #....";tab(15);"Edit the appointment at hour #"
    print tab(4);"DEL #........";tab(15);"Delete the appointment at hour #"
    print tab(4);"(V)VIEW #....";tab(15);"View the appointment at hour #"
    print tab(4);"#............";tab(15);"View the appointment at hour #"
    print tab(4);"(N)NEXT......";tab(15);"Show next day"
    print tab(4);"(L)LAST......";tab(15);"Show prior day"
    print "======================================================="
    end if
    if helpMode$ = "ALL" or mode$ = "MONTH" then
    print tab(3);"MONTH mode commands"
    print tab(4);"#............";tab(15);"View the daily appointments at day #"
    print tab(4);"(N)NEXT......";tab(15);"Show next month"
    print tab(4);"(L)LAST......";tab(15);"Show prior month"
    print "======================================================="
    end if
    if helpMode$ = "ALL" or mode$ = "YEAR" then
    print tab(3);"YEAR mode commands"
    print tab(4);"2004.........";tab(15);"Show specified year"
    print tab(4);"(N)NEXT......";tab(15);"Show next year"
    print tab(4);"(L)LAST......";tab(15);"Show prior year"
    print "======================================================="
    end if
    print ""
    input "Press ENTER to continue > ";temp$ 
    helpMode$ = ""
    if startupFlag = 1 then
        cls
        startupFlag = 0
        day = date$("days")
        mode$ = "DAY"
        goto [showDay]
    end if
    goto [getCommands]

