' iBAddress.ibas
{CREATORID "LDIA"}
{VERSION "2.3"}
{RESOURCEFILE + "iBAddress.rsrc"}
{SECUREFILES OFF}

CONST M$="Last name"
CONST N$="First name"
CONST O$="Birthday"
CONST Y$="iBAddress v2.3¶	November 4, 2005¶"
CONST Z$="	by Laurent Duveau¶	www.aldweb.com"

BEGIN
GOSUB _InitDisplay
GOSUB _InitPgm
WHILE D<>-1
   D=DOEVENTS
   IF D=10 GOSUB _RDate : D=0
   IF D=15 LET S=SELECTEDCHOICE : GOSUB _RSort
   IF D=16 GOSUB _RReverse
   IF D=18 GOSUB _RFirst
   IF D=19 GOSUB _RPrevious
   IF D=20 GOSUB _RNext
   IF D=21 GOSUB _RLast
   IF D=22 GOSUB _RDelete
   IF D=23 GOSUB _RNew
   IF D=24 LET D=MESSAGEBOX(Y$+Z$,1) : D=0
   IF D=25 KEYBOARD 0
   IF D=26 KEYBOARD 1
   IF D=27 KEYBOARD 2
WEND
GOSUB _W1Record
SAVEPREF #1,L
SAVEPREF #2,K
SAVEPREF #3,I
SAVEPREF #4,S
SAVEPREF #5,T
END

_InitDisplay:
   J=FILEEXISTS("iBAddrDB")
   IF J=1 THEN
      L=LOADPREF(#1)
      IF L>0 THEN
         K=LOADPREF(#2)
         I=LOADPREF(#3)
         S=LOADPREF(#4)
         T=LOADPREF(#5)
      ELSE S=1
      ENDIF
   ELSE S=1
   ENDIF
   LABEL #1,M$,5,18
   TEXTFIELD #2,"",0,55,18,100,15
   LABEL #3,N$,5,33
   TEXTFIELD #4,"",0,55,33,100,15
   LABEL #5,"Phone",5,48
   TEXTFIELD #6,"",0,55,48,100,15
   LABEL #7,"E-mail",5,63
   TEXTFIELD #8,"",0,55,63,100,15
   LABEL #9,O$,5,78
   TEXTSELECTOR #10,"",55,78,54,11
   LABEL #12,"Other",5,93
   TEXTFIELD #13,"",1,55,93,100,30
   LABEL #14,"Sort by:",5,122
   L$=M$+"¶"+N$+"¶"+O$
   J=12+S : J$=A$(J)
   POPUPCHOICE #15,J$,L$,3,40,120,60,15
   CHECKBOX #16,"Reverse",T,105,120,50,15
   LABEL #17,"1/1",42,142
   Q$=GETOSVER$
   IF Q$<"3.5" THEN
      BUTTON #18,"<<",5,140,15,15
      BUTTON #19,"<",21,140,15,15
      BUTTON #20,">",75,140,15,15
      BUTTON #21,">>",91,140,15,15
      BUTTON #22,"X",112,140,15,15
      BUTTON #23,"*",131,140,15,15
   ELSE
      IMAGEBUTTON #18,11,5,140,15,15
      IMAGEBUTTON #19,9,21,140,15,15
      IMAGEBUTTON #20,10,75,140,15,15
      IMAGEBUTTON #21,12,91,140,15,15
      IMAGEBUTTON #22,28,112,140,15,15
      IMAGEBUTTON #23,18,131,140,15,15
   ENDIF
   SETFONT 1
   BUTTON #24,"i",149,1,10,10
   SETFONT 0
   BUTTON #25,"abc",79,1,20,10
   BUTTON #26,"123",102,1,20,10
   BUTTON #27,"Int'l",125,1,20,10
RETURN

_ShowRecord:
   UPDATEFIELD #2,A$
   UPDATEFIELD #4,B$
   UPDATEFIELD #6,C$
   UPDATEFIELD #8,D$
   UPDATETEXT #10,E$
   UPDATEFIELD #13,F$
   J$=STR$(I,0)+"/"+STR$(K,0)
   UPDATELABEL #17,J$
   SETFOCUS #2
   GRAFFITISHIFT 1,150,150
RETURN

_RDate:
   IF E$="00/00/0000" THEN
      J$=DATESELECT$("")
   ELSE
      J$=DATESELECT$(E$)
   ENDIF
   IF J$<>"" THEN
      E$=J$
      UPDATETEXT #10,E$
   ENDIF
RETURN

_R1Record:
   OPEN "iBAddrDB" FOR RANDOM AS #1
   J=I-1*7+1
   SEEK #1,J
   INPUT #1,N
   INPUT #1,A$ : INPUT #1,B$
   INPUT #1,C$ : INPUT #1,D$
   INPUT #1,E$ : INPUT #1,F$
   CLOSE #1
   GOSUB _ShowRecord
RETURN

_W1Now:
   OPEN "iBAddrDB" FOR RANDOM AS #1
   J=I-1*7+1
   SEEK #1,J
   PRINT #1,N
   PRINT #1,A$ : PRINT #1,B$
   PRINT #1,C$ : PRINT #1,D$
   PRINT #1,E$ : PRINT #1,F$
   CLOSE #1
RETURN

_W1Record:
   A$=FIELD$(#2) : B$=FIELD$(#4)
   C$=FIELD$(#6) : D$=FIELD$(#8)
   F$=FIELD$(#13)
   GOSUB _W1Now
RETURN

_D1Record:
   OPEN "iBAddrDB" FOR INPUT AS #1
   OPEN "iBAddrDBt" FOR OUTPUT AS #2
   J=I-1*7
   FOR M=1 TO J
      INPUT #1,J$ : PRINT #2,J$
   NEXT
   FOR M=1 TO 7 : INPUT #1,J$ : NEXT
   J=J+8 : P=K*7
   FOR M=J TO P
      INPUT #1,J$ : PRINT #2,J$
   NEXT
   CLOSE #2
   CLOSE #1
   KILL "iBAddrDB"
   RENAME "iBAddrDBt","iBAddrDB"
   IF I=K LET I=I-1
   K=K-1
RETURN

_RFirst:
   GOSUB _W1Record
   I=1
   GOSUB _R1Record 
RETURN

_RPrevious:
   GOSUB _W1Record
   I=I-1 : I=MAX(I,1)
   GOSUB _R1Record 
RETURN

_RNext:
   GOSUB _W1Record
   I=I+1 : I=MIN(I,K)
   GOSUB _R1Record 
RETURN

_RLast:
   GOSUB _W1Record
   I=K
   GOSUB _R1Record 
RETURN

_RNew:
   IF K>0 GOSUB _W1Record
   L=L+1 : N=L
   K=K+1 : I=K
   A$="" : B$="" : C$=""
   D$="" : E$="00/00/0000" : F$=""
   GOSUB _W1Now : GOSUB _R1Record
RETURN

_RDelete:
  J=MESSAGEBOX("Really delete this record ?",3)
   IF J=1 THEN
      GOSUB _D1Record
      IF K=0 THEN GOSUB _RNew
      ELSE GOSUB _R1Record
      ENDIF
   ENDIF
RETURN

_W1Swap:
   V=X-J+1
   W=Y-J+1
   SEEK #1,V
   INPUT #1,M
   INPUT #1,A$ : INPUT #1,B$
   INPUT #1,C$ : INPUT #1,D$
   INPUT #1,E$ : INPUT #1,F$
   SEEK #1,W
   INPUT #1,O
   INPUT #1,G$ : INPUT #1,H$
   INPUT #1,I$ : INPUT #1,J$
   INPUT #1,K$ : INPUT #1,L$
   SEEK #1,V
   PRINT #1,O
   PRINT #1,G$ : PRINT #1,H$
   PRINT #1,I$ : PRINT #1,J$
   PRINT #1,K$ : PRINT #1,L$
   SEEK #1,W
   PRINT #1,M
   PRINT #1,A$ : PRINT #1,B$
   PRINT #1,C$ : PRINT #1,D$
   PRINT #1,E$ : PRINT #1,F$
   IF N=M LET I=W-1\7+1
   IF N=O LET I=V-1\7+1
   P$=Q$
RETURN

_RSort:
   GOSUB _W1Record
   J=S+1 : IF S=3 LET J=J+2
   OPEN "iBAddrDB" FOR RANDOM AS #1
   P=K*7
   FOR X=J TO P STEP 7
      SEEK #1,X
      INPUT #1,P$
      IF S=3 LET P$=RIGHT$(P$,4)+MID$(P$,4,2)+LEFT$(P$,2)
      Z=X+7
      FOR Y=Z TO P STEP 7
         SEEK #1,Y
         INPUT #1,Q$
         IF S=3 LET Q$=RIGHT$(Q$,4)+MID$(Q$,4,2)+LEFT$(Q$,2)
         IF T=0 THEN
            IF P$>Q$ GOSUB _W1Swap
         ELSE
            IF P$<Q$ GOSUB _W1Swap
         ENDIF
      NEXT
   NEXT
   CLOSE #1
   GOSUB _R1Record
RETURN

_InitPgm:
   IF L>0 THEN GOSUB _R1Record
   ELSE GOSUB _RNew
   ENDIF
RETURN

_RReverse:
   IF T=0 THEN T=1
   ELSE T=0
   ENDIF
   GOSUB _RSort
RETURN
