rem filter view,merge,check autorepeat on key,auto cheque numbering,space jumping,open file menu,archive c/f line to text file sensibly,auto process repeats of statement quit and 12 midnight
rem 
rem add down arrow to s3 font
app Scbank
	type $1003 rem s3a
rem s3z 	type 3 
	path "\bank"
	ext "bnk"
	icon "\opd\scbanka.pic" rem s3a
rem s3z 	icon "\opd\scbank.pic" 
enda

proc main:
rem way too much global stuff :-)
rem sorder% last thing on statement
	global total,stotal,sorder%
	global filepos%,scrpos%,typel$(100),stypel$(100)
rem remember where we are moving from
	global oldpos%
rem for display helpfuls
	global lastpos%
	global mark$(6),scrlen%,statlen%
rem found a repeats file yet
	global nfile$(255),stdstat$(10)
rem indexes
	global chqidx%,stmidx%,curidx%
rem filter info
	global onfilt$(3),filt$(20)
rem font info
	global linehi%,lined%,linea%
rem screensizes
	global scwidth%,schight%
	global dispwin%
	global search$(20),statwin%,stmwin%
	global stmstat$(3),mrkstat$(3)
	global ordstat$(10),why$(3),clkstat$(3)
	global mrkwin%,tmpfile$(255),stem$(255)
	global version$(20),filenm$(128)
	global statnx%,statmx%,statny%,statmy%
rem fonts for zooming
	global curfont%,zfontid%(4),zfonts%
rem statuswindow type
	global swintp%
rem are we moving a window
	global moving$(3)
rem statement repeats
	global stmrep$(60)
rem no of elements
	global fcount%
	local infowin%
rem sc needs optimising
	cache 2000,2000 rem s3a
	version$="Version 2.2"
rem load up extra procs needed if compiling on S3a
rem	loadm "\opo\scmore.opo"
rem	version$=version$+"(develop)"
	scwidth%=gwidth : schight%=gheight
rem remove text window
	screen 1,1,1,1
	gsetwin 1,1,2,2
	infowin%=strtscr%:
rem setup font array
	zfonts%=4 rem s3a
	zfontid%(1)=9 rem swiss8 rem s3a
	zfontid%(2)=10 rem swiss11 rem s3a
	zfontid%(3)=11 rem swiss13 rem s3a
	zfontid%(4)=12 rem swiss16 rem s3a
rem s3z 	tmpfile$=ssdfind$:("\opd\scbank.fon") 
rem s3z 	if (exist(tmpfile$)=-1) 
rem s3z 		zfontid%(2)=gloadfont(tmpfile$) 
rem s3z 		zfonts%=2 
rem s3z 	else 
rem s3z 		zfonts%=1 
rem s3z 	endif 
rem s3z 	zfontid%(1)=1 rem swiss8 
	diaminit 1,"ChqBook","Statmnt" rem s3a
rem set up repeat string
	stmrep$="Daily,Weekly,Monthly,Quarterly,6Monthly,Yearly"
	fset:(cmd$(3),cmd$(2))
	startup:
	gclose infowin%
	mainloop:
endp

proc startup: rem things to do when starting up new file
rem start without repeats window
	stmstat$="On"
rem start without mark window
	mrkstat$="On"
rem we dont start off moving transaction
	moving$="Off"
rem start without a display filter
	onfilt$="On"
rem start in chequebook mode
	ordstat$="Statement"
	statwin%=gcreate(2,12,1,schight%-16,1,1) rem s3a
rem s3z 	statwin%=gcreate(2,12,1,schight%-16,1) 
	dispwin%=statwin%
	findval:
rem display titles
	sizewin:
rem current index as chqbk
	curidx%=chqidx%
	guse dispwin%
	bankp:
endp

proc foninfo: rem font size info
	local info%(32)
	gfont zfontid%(curfont%)
	ginfo info%()
	linehi%=info%(3)+1 rem how high is this font
	lined%=info%(4)+1 rem font descent
	linea%=info%(5)+1 rem font ascent
endp

proc sizewin: rem determine size of main window
	local extent%(4),tamm$(10)
	statwininfo(-1,extent%()) rem s3a
	statnx%=0
	statmx%=extent%(1) rem s3a
rem s3z 	statmx%=scwidth% 
	guse statwin%
	foninfo:
	gsetwin statnx%,0,statmx%,schight%
	statny%=linehi%+3
	statmy%=schight%-statny%-1
	statlen%=statmy%/linehi%
	statmy%=statlen%*linehi% rem make sue we don't have any left over
rem	gsetwin statnx%+2,statny%,statmx%-4,statmy%
	if (ordstat$="Statement")
		tamm$="ChequeBook"
	else
		tamm$="Statement"
	endif
	dottl:("Date","Type","Amount",tamm$,"Mark","",79,statmx%-232,statmx%-160,statmx%-63,statmx%-24,statmx%-10,79) rem s3a
rem s3z 	dottl:("Date","Type","Amount",tamm$,"Mrk","",53,statmx%-139,statmx%-95,statmx%-34,statmx%-14,statmx%-9,53) 
	guse dispwin%
	gfont zfontid%(curfont%)
	scrlen%=statlen%
endp

proc stmzwin: rem determine size of repeats window
	guse stmwin%
	foninfo:
	statnx%=5 rem s3a
rem s3z 	statnx%=0 
	statny%=7
	statmx%=scwidth%-11 rem s3a
rem s3z 	statmx%=scwidth% 
	statmy%=schight%-10
	statlen%=(statmy%-linehi%-4)/linehi%
	statmy%=statlen%*linehi% rem make sue we don't have any left over
	gsetwin statnx%,statny%,statmx%,statmy%+linehi%+6
	dottl:("Statement","Description","Amount","Process","Type","Until",83,185,256,329,386,460,460) rem s3a
rem s3z 	dottl:("Statement","Desc","Amount","Process","Type","Until",53,77,112,160,185,232,232) 
	guse dispwin%
	gfont zfontid%(curfont%)
	scrlen%=statlen%
endp

proc mrkzwin: rem determine size of mark totals window
	guse mrkwin%
	foninfo:
	statnx%=5
	statny%=5
	statmx%=170
	statmy%=schight%-5
	statlen%=(statmy%-linehi%-4)/linehi%
	statmy%=statlen%*linehi% rem make sue we don't have any left over
	gsetwin statnx%,statny%,statmx%,statmy%+linehi%+4
	dottl:("Mark","Total","","","","",60,160,160,160,160,160,160) rem s3a
rem s3z 	dottl:("Mark","Total","","","","",60,160,160,160,160,160,160) 
	guse dispwin%
	gfont zfontid%(curfont%)
	scrlen%=statlen%
endp

proc createb:(file$,dire$) rem create account file
	trap create file$,A,date$,desc$,amm$,state$,order$,sorder$,total$,stotal$,type$,stm$
	if err
rem it could be just no directory
		trap mkdir dire$
		if err rem give up
			showerr:(err)
			stop
		else
			trap create file$,A,date$,desc$,amm$,state$,order$,sorder$,total$,stotal$,type$,stm$
			if err rem give up
				showerr:(err)
				stop
			endif
		endif
	endif
endp

proc fset:(opt$,file$) rem parse command line args
	local p%(6),x%(6)
	parse$("",file$,x%())
	stem$=mid$(file$,1,x%(5))
	nfile$=stem$+"stm"
	if opt$="C"
		stdstat$="NoFile"
		createb:(file$,mid$(file$,1,x%(4)-1))
		wrcf:(days(day,month,year),"c/f",0.0,"On,2,2",0,0,"delta,hiw,visa,chq,dd,bc,dep,trf,amex,bp,text","food,petrol,computer,invest,sport,work,clothes,goodies,general,text"," ","x")
		append
	elseif opt$="O"
		if (exist(nfile$)=-1)
			trap open nfile$,B,sdate$,pdate$,desc$,amm$,type$,rep$,always$,until$
			if err
				showerr:(err)
				stop
			endif
			stdstat$="File"
		else
			stdstat$="NoFile"
		endif
		trap open file$,A,date$,desc$,amm$,state$,order$,sorder$,total$,stotal$,type$,stm$
		if err
			showerr:(err)
			stop
		endif
	endif
	filenm$=file$
	setname file$
	use A
	fcount%=count
endp

proc strtscr%: rem start up screen
	local infowin%
	infowin%=gcreate(statnx%+90,statny%+5,250,120,1,1) rem s3a
rem s3z 	infowin%=gcreate(statnx%+45,statny%+1,130,60,1) 
	gxborder 1,1 rem s3a
rem s3z 	gborder 1 
	gat 5,40 rem s3a
rem s3z 	gat 5,17 
	gfont 12 rem s3a
rem s3z 	gfont 2 
	gstyle 8
	gprintb "SCBank",240,3 rem s3a
	gat 5,60 rem s3a
	gfont 11 rem s3a
	gstyle 0 rem s3a
	gprintb version$,240,3 rem s3a
	gat 5,80 rem s3a
	gprintb "¸ Susan Carter 1994",240,3 rem s3a
	gat 5,110 rem s3a
	gprintb "Sue@squish.demon.co.uk",240,3 rem s3a
rem s3z 	gprintb "SCBank",120,3 
rem s3z 	gat 5,30 
rem s3z 	gfont 1 
rem s3z 	gstyle 0 
rem s3z 	gprintb version$,120,3 
rem s3z 	gat 5,44 
rem s3z 	gprintb "¸ Susan Carter 1994",120,3 
rem s3z 	gat 5,55 
rem s3z 	gprintb "Sue@squish.demon.co.uk",120,3 
	return infowin%
endp

proc getnth$:(list$,num%) rem get nth item from list
	local i%,j%,first%,second%
	i%=1:j%=1
	while (j%<>num%)
		while (mid$(list$,i%,1)<>",")
			i%=i%+1
		endwh
		j%=j%+1
		i%=i%+1
	endwh
	first%=i%
	while ((i%<=len(list$))and(mid$(list$,i%,1)<>","))
		i%=i%+1
	endwh
	second%=i%
	return (mid$(list$,first%,(second%-first%)))
endp

proc which%:(list$,item$) rem return position in list
	local i%,j%,first%,second%
	first%=1:second%=1
	i%=1:j%=1
	while (i%<len(list$))
		i%=i%+1
		if ((mid$(list$,i%,1)=",")or(i%=len(list$)))
			if (mid$(list$,first%,i%-first%)=item$)
				return (j%)
			else
				j%=j%+1
				i%=i%+1
				first%=i%
			endif
		endif
	endwh
	return (-1)
endp

proc wrinit:(date&,desc$,amm,state$,order%,sorder%,total,stotal,type$,stm$) rem assign record fields for account
	local date$(10),amm$(10),order$(8),sorder$(8)
	local temp%,yr%,mo%,dy%
	secstodate ((date&-days(1,1,1970))*60*60*24),yr%,mo%,dy%,temp%,temp%,temp%,temp%
	date$=right$("0"+num$(dy%,2),2)+"/"+right$("0"+num$(mo%,2),2)+"/"+right$(num$(yr%,4),2)
	A.date$=date$
	A.desc$=desc$
	A.amm$=fix$(amm,2,10)
	A.state$=state$
	A.order$=num$(order%,10)
	A.sorder$=num$(sorder%,10)
	A.total$=fix$(total,2,10)
	A.stotal$=fix$(stotal,2,10)
	A.type$=type$
	A.stm$=stm$
endp

proc wrcf:(date&,desc$,amm,state$,order%,sorder%,typel$,stypel$,type$,stm$) rem assign record fields for c/f line
	local date$(10),amm$(10),order$(8),sorder$(8)
	local temp%,yr%,mo%,dy%
	secstodate ((date&-days(1,1,1970))*60*60*24),yr%,mo%,dy%,temp%,temp%,temp%,temp%
	date$=right$("0"+num$(dy%,2),2)+"/"+right$("0"+num$(mo%,2),2)+"/"+right$(num$(yr%,4),2)
	A.date$=date$
	A.desc$=desc$
	A.amm$=fix$(amm,2,10)
	A.state$=state$
	A.order$=num$(order%,10)
	A.sorder$=num$(sorder%,10)
	A.total$=typel$
	A.stotal$=stypel$
	A.type$=type$
	A.stm$=stm$
endp

proc wrstm:(ds&,dsday%,dp&,dpday%,desc$,amm,type$,rep$,always$,until&) rem assign record fields for repeats
	local date$(13),amm$(10)
	local temp%,yr%,mo%,dy%
	secstodate ((dp&-days(1,1,1970))*60*60*24),yr%,mo%,dy%,temp%,temp%,temp%,temp%
	date$=right$("0"+num$(dy%,2),2)+"/"+right$("0"+num$(mo%,2),2)+"/"+right$(num$(yr%,4),2)
	if (dpday%>=29) rem potential end of month problem
		date$=date$+"*"+num$(dpday%,2)
	endif
	B.pdate$=date$
	secstodate ((ds&-days(1,1,1970))*60*60*24),yr%,mo%,dy%,temp%,temp%,temp%,temp%
	date$=right$("0"+num$(dy%,2),2)+"/"+right$("0"+num$(mo%,2),2)+"/"+right$(num$(yr%,4),2)
	if (dsday%>=29) rem potential end of month problem
		date$=date$+"*"+num$(dsday%,2)
	endif
	B.sdate$=date$
	B.desc$=desc$
	B.amm$=fix$(amm,2,10)
	B.type$=type$
	B.rep$=rep$
	B.always$=always$
	secstodate ((until&-days(1,1,1970))*60*60*24),yr%,mo%,dy%,temp%,temp%,temp%,temp%
	date$=right$("0"+num$(dy%,2),2)+"/"+right$("0"+num$(mo%,2),2)+"/"+right$(num$(yr%,4),2)
	B.until$=date$
endp

proc mainloop: rem main control loop
	local k%,mod%,h$(26),hu$(26),a$(6),a%(6),t$(1),file$(128),pname$(6)
	filepos%=count-1
	lastpos%=filepos%
	if (count<scrlen%)
		scrpos%=count
	else
		scrpos%=scrlen%
	endif
	while 1
		gat 2,(scrpos%-1)*linehi%+linea%+linehi%+3
rem s3z 		if moving$="Off" 
		gprint ""
rem s3z 		else 
rem s3z 			gprint chr$($0a) 
rem s3z 		endif 
		ggrey 1 rem s3a
		if moving$="Off" rem s3a
			gat 2,(scrpos%-1)*linehi%+1+linehi%+3 rem s3a
			gfill scwidth%,linehi%-1,2 rem s3a
		else rem s3a
			gat 2,(scrpos%)*linehi%+linehi%+3 rem s3a
			gtmode 2 rem s3a
			gprint rept$(chr$($0d),255) rem s3a
		endif rem s3a
		ggrey 0 rem s3a
		getevent a%()
		gat 2,(scrpos%-1)*linehi%+linea%+linehi%+3
rem s3z 		if moving$="Off" 
		gprintb " ",gtwidth("")-1
rem s3z 		else 
rem s3z 			gprintb "",gtwidth(chr$($0a))-1 
rem s3z 		endif 
		ggrey 1 rem s3a
		if moving$="Off" rem s3a
			gat 2,(scrpos%-1)*linehi%+1+linehi%+3 rem s3a
			gfill scwidth%,linehi%-1,2 rem s3a
		else rem s3a
			gat 2,(scrpos%)*linehi%+linehi%+3 rem s3a
			gtmode 2 rem s3a
			gprint rept$(chr$($0d),255) rem s3a
		endif rem s3a
		ggrey 0 rem s3a
		if ((a%(1) and $400)<>0) rem not keypress
			if a%(1)=$404 rem system action
				file$=getcmd$
				t$=left$(file$,1)
				file$=mid$(file$,2,128)
				if t$="X"
					bankx:
				elseif t$="C" or t$="O"
rem shut everything down
					if (mrkstat$="Off")
						markj:
					elseif (stmstat$="Off")
						stmt:
					endif
					gclose statwin%
					if (stdstat$<>"NoFile")
						use B
						trap close
						if err
							showerr:(err)
						endif
					endif
					use A
					trap close
					if err
						showerr:(err)
					endif
					freealloc chqidx% rem s3a
					freealloc stmidx% rem s3a
rem s3z 					call($0381,0,chqidx%) 
rem s3z 					call($0381,0,stmidx%) 
					fset:(t$,file$)
					startup:
				endif
			endif
		else rem a keypress sc should optimise this
			k%=a%(1)
			mod%=a%(2) and $00ff
			if (mrkstat$="Off")
				h$="cijxz" REM Hot keys
				hu$="Z" rem upper case hot keys rem s3a
				pname$="mark"
			elseif (stmstat$="Off")
				h$="adeiptwxz" REM Hot keys
				hu$="Z" rem upper case hot keys rem s3a
				pname$="stm"
			elseif (moving$="On")
				h$="aixvz" REM Hot keys
				hu$="Z" rem upper case hot keys rem s3a
				pname$="move"
			elseif (onfilt$="Off")
				h$="cdeiomxz" REM Hot keys
				hu$="CZ" rem upper case hot keys rem s3a
rem s3z 				hu$="u" 
rem should be able to use the standard bank procedures in filter mode
				pname$="bank"
			elseif (stmstat$="On")
				h$="acdefgijklmnpqrstvwxz" REM Hot keys
				hu$="CZ" rem upper case hot keys rem s3a
rem s3z 				hu$="u" 
				pname$="bank"
			endif
			if k%=$122 rem menu key
rem only on main screen
				if ((mod%=4)and(mrkstat$="On")and(stmstat$="On"))
					if (swintp%=0) rem s3a
						swintp%=2 rem s3a
						statuswin on, swintp% rem s3a
					elseif (swintp%=1) rem s3a
						swintp%=0 rem s3a
						statuswin off rem s3a
					else rem s3a
						swintp%=1 rem s3a
						statuswin on, swintp% rem s3a
					endif rem s3a
					sizewin:
					display:(filepos%,scrpos%)
				else
					minit
					if (mrkstat$="Off")
						mcard "Edit","Check",%c
						mcard "View","Info...",%i,"Mark totals "+mrkstat$+"...",%j
						mcard "Special","Exit",-%x,"Zoom in",%z,"Zoom out",%Z rem s3a
rem s3z 						mcard "Special","Exit",%x,"Zoom Font",%z 
					elseif (stmstat$="Off")
						mcard "File","Process repeats",%p
						mcard "Edit","Add...",%a,"Edit...",%e,"Delete",%d
						mcard "View","Spend Type "+why$,%w,"Info...",%i,"Repeats "+stmstat$+"...",%t
						mcard "Special","Exit",-%x,"Zoom in",%z,"Zoom out",%Z rem s3a
rem s3z 						mcard "Special","Exit",%x,"Zoom Font",%z 
					elseif (moving$="On")
						mcard "Edit","Place",%a,"Quit Moving",%v
						mcard "View","Info...",%i
						mcard "Special","Exit",-%x,"Zoom in",%z,"Zoom out",%Z rem s3a
rem s3z 						mcard "Special","Exit",%x,"Zoom Font",%z 
					elseif (onfilt$="Off")
						mcard "Edit","Edit...",%e,"Delete",-%d,"Check",%c,"Check Off",-%C,"Mark...",%m rem s3a
rem s3z 						mcard "Edit","Edit...",%e,"Delete",%d,"Check",%c,"Check Off",%u,"Mark...",%m 
						mcard "Search","Filter "+onfilt$+"...",%o
						mcard "View","Info...",%i
						mcard "Special","Exit",-%x,"Zoom in",%z,"Zoom out",%Z rem s3a
rem s3z 						mcard "Special","Exit",%x,"Zoom Font",%z 
					elseif (stmstat$="On")
						mcard "File","Compress",%s,"Archive...",%k,"Recalc",%l,"Process repeats",%p
						mcard "Edit","Add...",%a,"Edit...",%e,"Delete",-%d,"Check",%c,"Check Off",-%C,"Mark...",%m,"Move...",%v rem s3a
rem s3z 						mcard "Edit","Add...",%a,"Edit...",%e,"Delete",%d,"Check",%c,"Check Off",%u 
rem s3z 						mcard "Edit2","Mark...",%m,"Move...",%v 
						mcard "Search","Find...",%f,"Find again",%g rem Not yet implemented ,"Filter "+onfilt$+"...",%o
						mcard "View","Spend Type "+why$,%w,"Info...",%i,"Repeats "+stmstat$+"...",%t,"Mark totals "+mrkstat$+"...",%j
						mcard "Special","Set preferences...",-%q,"Exit",-%x,"Zoom in",%z,"Zoom out",%Z rem s3a
rem s3z 						mcard "Special","Set preferences...",%q,"Exit",%x,"Zoom Font",%z 
						mcard chr$(4),ordstat$+" view",%r
					endif
					lock on
					k%=menu
					lock off
					if ((k%>=%A)and(k%<=%Z)) rem s3a
						if loc(hu$,chr$(k%))
							a$=pname$+"u"+chr$(k%)
							@(a$):
						endif
					else rem s3a
						if k% and (loc(h$,chr$(k%))<>0)
							a$=pname$+chr$(k%)
							@(a$):
						endif
					endif rem s3a
				endif
			elseif k% and $200 rem hot key
				k%=k%-$200
				if ((mod% and 2)=2) rem s3a
					if loc(hu$,chr$(k%))
						a$=pname$+"u"+chr$(k%)
						@(a$):
					endif
				else rem s3a
					if loc(h$,chr$(k%))
						a$=pname$+chr$(k%)
						@(a$):
					endif
				endif rem s3a
			elseif (k%=13) rem enter does add
				if (loc(h$,chr$(%a))) rem only do it if a valid command for this mode
					@(pname$+"a"):
				endif
			elseif (k%=8) rem delete key
				if (loc(h$,chr$(%d))) rem only do it if a valid command for this mode
					@(pname$+"d"):
				endif
rem diamond key
			elseif ((k%=292)and(mrkstat$="On")and(stmstat$="On")) rem s3a
				bankr: rem s3a
rem help key
			elseif (k%=291)
				trap loadm ssdfind$:("\opo\sys$sch.opo")
				if err
					giprint "Help module not found"
				else
					if (schelp%:(ssdfind$:("\opd\scbank.hlp"))=-1)
						giprint "Help not available"
					endif
					unloadm ssdfind$:("\opo\sys$sch.opo")
					guse dispwin%
				endif
			endif
rem process any cursor key
			currkey:(k%)
		endif
	endwh
rem never get here!
endp

proc ssdfind$:(file$) rem find file on ssd devices
	local lfile$(128),ssd$(4),i%
	ssd$="mabc"
	i%=1
	while (i%<=4)
		lfile$=mid$(ssd$,i%,1)+":"+file$
		if exist(lfile$)
			return lfile$
		endif
		i%=i%+1
	endwh
	return file$ rem just in case
endp

proc currkey:(k%) rem do a cursor movement
	if (k%=262) rem home
		lastpos%=filepos%
		display:(0,1)
	elseif (k%=263) rem end
		lastpos%=filepos%
		display:(fcount%,scrlen%)
	elseif ((k%=257)and(filepos%<count-1)) rem down arrow
rem alert(num$(filepos%,10),num$(count,10))
		posfp:(1)
		if (scrpos%=scrlen%)
			display:(filepos%,scrlen%)
		else
			scrpos%=scrpos%+1
		endif
	elseif ((k%=261)and(filepos%<fcount%-1)) rem page down
		if (scrpos%=scrlen%)
			posfp:(scrlen%-1)
			display:(filepos%,scrlen%)
		else
			posfp:(-scrpos%+scrlen%)
			scrpos%=scrlen%
rem a little check for very small files
			filepos%=min(count-1,filepos%)
			scrpos%=min(scrpos%,fcount%)
		endif
	elseif ((k%=256)and(filepos%>0)) rem up arrow
		posfp:(-1)
		if (scrpos%=1)
			display:(filepos%,1)
		else
			scrpos%=scrpos%-1
		endif
	elseif ((k%=260)and(filepos%>0)) rem page up
		if (scrpos%=1)
			posfp:(-(scrlen%-1))
			display:(filepos%,1)
		else
			posfp:(-scrpos%+1)
			scrpos%=1
		endif
	endif
endp

proc showerr:(val%)
	alert ("Error "+err$(val%))
	busy off
endp

proc bankj: rem mark totals
	local mrknx%,mrkmx%,mrkny%,mrkmy%,mrklen%
	busy "Mark totals..."
	tmpfile$=stem$+"mrk"
	if (exist(tmpfile$)=-1)
		delete tmpfile$
	endif
	trap create tmpfile$,C,mark$,amm$
	if err
		showerr:(err)
		return
	endif
	use A
	fcount%=count
	first
	while not eof
rem only bother with transactions that have marks and not the c/f line
		if ((A.state$<>" ")and(A.order$<>"0"))
			use C
			first
			while not eof
				if (A.state$=C.mark$)
					C.amm$=fix$((val(A.amm$)+val(C.amm$)),2,10)
					update
					break
				endif
				next
			endwh
			if eof
				C.mark$=A.state$
				C.amm$=A.amm$
				append
			endif
			use A
		endif
		next
	endwh
	mrkstat$="Off"
	mrknx%=10 rem s3a
	mrkny%=10 rem s3a
	mrkmx%=scwidth%-310 rem s3a
	mrkmy%=schight%-30 rem s3a
	mrkwin%=gcreate(mrknx%+2,mrkny%+linehi%+3,mrkmx%-4,mrkmy%-linehi%-4,1,1) rem s3a
rem s3z 	mrknx%=10 
rem s3z 	mrkny%=10 
rem s3z 	mrkmx%=200 
rem s3z 	mrkmy%=schight%-30 
rem s3z 	mrkwin%=gcreate(mrknx%+2,mrkny%+linehi%+3,mrkmx%-4,mrkmy%-linehi%-4,1) 
	dispwin%=mrkwin%
	mrkzwin:
	scrpos%=1
	filepos%=0
	lastpos%=filepos%
	use C
	fcount%=count
	busy off
	display:(filepos%,scrpos%)
endp

proc bankz: rem font resize up
	curfont%=curfont%+1
	if (curfont%>zfonts%)
		curfont%=1
	endif
rem if displaying repeats window
	if (stmstat$="Off")
		stmzwin:
	elseif (mrkstat$="Off")
		mrkzwin:
	else
		sizewin:
	endif
	display:(filepos%,scrpos%)
endp

proc bankuz: rem font resize down
	curfont%=curfont%-1
	if (curfont%<1)
		curfont%=zfonts%
	endif
	sizewin:
rem if displaying repeats window
	if (stmstat$="Off")
		stmzwin:
	elseif (mrkstat$="Off")
		mrkzwin:
	endif
	display:(filepos%,scrpos%)
endp

proc bankv: rem move transaction
	position rptr%:(curidx%,filepos%)
	if ((ordstat$="ChequeBook") and (val(A.sorder$)=-1))
		alert("Doesn't make sense to move transactions","not on statement in statement mode")
		return
	endif
	moving$="On"
	oldpos%=filepos%
	lastpos%=filepos%
	display:(filepos%,scrpos%)
endp

proc bankk:
	local delto%,arctotal,d&,date$(20),file$(128)
	local handle%,ret%,mode%,txt$(255),address%
	local c1%,c2%,delim$(1),i%,j%
	if (A.stm$<>"x")
		alert ("Only archive transactions on statement")
		return
	endif
	delim$=","
	file$="\Bank\Archive.bnk"
	dinit "Archive to"
	dfile file$,"",3
	dchoice c1%,"Archive as","Bank,Text"
	dedit delim$,"Delimiter",1
	dchoice c2%,"Archive","Copy to file and delete entries,Copy to file,Delete entries"
	if dialog
		if ((c2%=1)or(c2%=2)) rem Copy to file requested
			if (c1%=1) rem Archive in native bank format
				trap create file$,D,date$,desc$,amm$,state$,order$,sorder$,total$,stotal$,type$,stm$
				if err
					showerr:(err)
					return
				endif
				use A
			elseif (c1%=2) rem Archive in text format using delimiter
				mode%=$0100 or $0020 or $0001
				ret%=ioopen(handle%,file$,mode%)
				if ret%<0
					showerr:(ret%)
					return
				endif
				address%=addr(txt$)
			endif
		endif
		busy "Archiving..."
		position rptr%:(curidx%,filepos%)
		arctotal=0
		delto%=val(A.sorder$)
		date$=A.date$
		i%=0
		while (i%<=delto%)
			position rptr%:(stmidx%,i%)
			arctotal=arctotal+val(A.amm$)
			if ((c2%=1)or(c2%=2)) rem Copy to file requested
				if (c1%=1) rem Archive in native bank format
					D.date$=A.date$
					D.desc$=A.desc$
					D.amm$=A.amm$
					D.state$=A.state$
					D.order$=A.order$
					D.sorder$=A.sorder$
					D.total$=A.total$
					D.stotal$=A.stotal$
					D.type$=A.type$
					D.stm$=A.stm$
					use D
					append
					use A
				elseif (c1%=2) rem Archive in text format using delimiter
					txt$=A.date$+delim$+A.desc$+delim$+A.amm$+delim$+A.total$+delim$+A.stotal$+delim$+A.order$+delim$+A.sorder$+delim$+A.state$+delim$+A.type$+delim$+A.stm$
					ret%=iowrite(handle%,address%+1,len(txt$))
					if ret%<0
						showerr:(ret%)
						return
					endif
				endif
			endif
			i%=i%+1
		endwh
		if ((c2%=1)or(c2%=2)) rem Copy to file requested
			if (c1%=1) rem Archive in native bank format
				use D
				close
				use A
			elseif (c1%=2) rem Archive in text format using delimiter
				ret%=ioclose(handle%)
				if ret%
					showerr:(ret%)
				endif
				busy off
			endif
		endif
		if ((c2%=1)or(c2%=3)) rem delete archived entries
rem go to the c/f line and write new value
			i%=0
			j%=count
			first
			while (i%<j%)
				if (val(A.sorder$)=0) rem the c/f line
					d&=strtod&:(date$,"/")
					wrcf:(d&,"c/f",arctotal,A.state$,0,0,typel$,stypel$," ","x")
					update
				elseif ((val(A.sorder$)<=delto%)and(A.stm$="x")) rem to be archived
					erase
				else
					update
				endif
				i%=i%+1
				first
			endwh
			fcount%=count
			freealloc chqidx% rem s3a
			freealloc stmidx% rem s3a
			chqidx%=alloc(count*2) rem s3a
			stmidx%=alloc(count*2) rem s3a
rem s3z 			call($0381,0,chqidx%) 
rem s3z 			call($0381,0,stmidx%) 
rem s3z 			chqidx%=call($0081,0,count*2) 
rem s3z 			stmidx%=call($0081,0,count*2) 
			if ((chqidx%=0)or(stmidx%=0))
				alert ("Not enough memory to allocate index","Account file preserved")
				stop
			endif
			altcurp:
			calc:
			display:(0,1)
		endif
	endif
	fcount%=count
endp

proc bankn: rem sc to be deleted; debugging aid
	local file$(128),i%
	local handle%,ret%,mode%,txt$(255),address%
	file$="\Bank\dump.idx"
	mode%=$0100 or $0020 or $0001
	ret%=ioopen(handle%,file$,mode%)
	if ret%<0
		showerr:(ret%)
		return
	endif
	address%=addr(txt$)
	busy "Dumping..."
	i%=0
	while (i%<count)
		txt$=num$(i%,4)+","+num$(rptr%:(chqidx%,i%),6)+","+num$(rptr%:(stmidx%,i%),6)
		ret%=iowrite(handle%,address%+1,len(txt$))
		if ret%<0
			showerr:(ret%)
			return
		endif
		i%=i%+1
	endwh
	ret%=ioclose(handle%)
	if ret%
		showerr:(ret%)
	endif
	busy off
endp

proc bankp: rem process repeats
	local dp&,ds&,du&,today&,amm,date$(20)
	local dsday%,dpday%
	local yr%,mo%,dy%,temp%
	local i%,changed%,deleted%,many%
	if (stdstat$<>"NoFile")
		busy "Repeats ..."
		use B
		fcount%=count
		many%=0
		today&=days(day,month,year)
		do
			changed%=0
			i%=1
			while (i%<=count)
				first
				deleted%=0
				dp&=strtod&:(B.pdate$,"/")
				if (dp&<=today&)
					ds&=strtod&:(B.sdate$,"/")
					du&=strtod&:(B.until$,"/")
					amm=val(B.amm$)
					total=total+amm
					use A
					fcount%=count
					wrinit:(ds&,B.desc$,amm," ",count,-1,total,0.0,B.type$," ")
					append
					addptr:
					many%=many%+1
					if (B.rep$="Monthly")
						secstodate ((ds&-days(1,1,1970))*60*60*24),yr%,mo%,dy%,temp%,temp%,temp%,temp%
						if mo%=12
							mo%=1
							yr%=yr%+1
						else
							mo%=mo%+1
						endif
						if (mid$(B.sdate$,9,1)="*") rem end of month condition
							dy%=val(right$(B.sdate$,2))
						endif
						dsday%=dy%
						ds&=((scdtos&:(yr%,mo%,dy%))/24/60/60)+days(1,1,1970)
						secstodate ((dp&-days(1,1,1970))*60*60*24),yr%,mo%,dy%,temp%,temp%,temp%,temp%
						if mo%=12
							mo%=1
							yr%=yr%+1
						else
							mo%=mo%+1
						endif
						if (mid$(B.pdate$,9,1)="*") rem end of month condition
							dy%=val(right$(B.pdate$,2))
						endif
						dpday%=dy%
						dp&=((scdtos&:(yr%,mo%,dy%))/24/60/60)+days(1,1,1970)
					elseif (B.rep$="Quarterly")
						secstodate ((ds&-days(1,1,1970))*60*60*24),yr%,mo%,dy%,temp%,temp%,temp%,temp%
						mo%=mo%+3
						if (mo%>12)
							mo%=mo%-12
							yr%=yr%+1
						endif
						if (mid$(B.sdate$,9,1)="*") rem end of month condition
							dy%=val(right$(B.sdate$,2))
						endif
						dsday%=dy%
						ds&=((scdtos&:(yr%,mo%,dy%))/24/60/60)+days(1,1,1970)
						secstodate ((dp&-days(1,1,1970))*60*60*24),yr%,mo%,dy%,temp%,temp%,temp%,temp%
						mo%=mo%+3
						if (mo%>12)
							mo%=mo%-12
							yr%=yr%+1
						endif
						if (mid$(B.pdate$,9,1)="*") rem end of month condition
							dy%=val(right$(B.pdate$,2))
						endif
						dpday%=dy%
						dp&=((scdtos&:(yr%,mo%,dy%))/24/60/60)+days(1,1,1970)
					elseif (B.rep$="6Monthly")
						secstodate ((ds&-days(1,1,1970))*60*60*24),yr%,mo%,dy%,temp%,temp%,temp%,temp%
						mo%=mo%+6
						if (mo%>12)
							mo%=mo%-12
							yr%=yr%+1
						endif
						if (mid$(B.sdate$,9,1)="*") rem end of month condition
							dy%=val(right$(B.sdate$,2))
						endif
						dsday%=dy%
						ds&=((scdtos&:(yr%,mo%,dy%))/24/60/60)+days(1,1,1970)
						secstodate ((dp&-days(1,1,1970))*60*60*24),yr%,mo%,dy%,temp%,temp%,temp%,temp%
						mo%=mo%+6
						if (mo%>12)
							mo%=mo%-12
							yr%=yr%+1
						endif
						if (mid$(B.pdate$,9,1)="*") rem end of month condition
							dy%=val(right$(B.pdate$,2))
						endif
						dpday%=dy%
						dp&=((scdtos&:(yr%,mo%,dy%))/24/60/60)+days(1,1,1970)
					elseif (B.rep$="Yearly")
						secstodate ((ds&-days(1,1,1970))*60*60*24),yr%,mo%,dy%,temp%,temp%,temp%,temp%
						yr%=yr%+1
						dsday%=dy%
						ds&=((scdtos&:(yr%,mo%,dy%))/24/60/60)+days(1,1,1970)
						secstodate ((dp&-days(1,1,1970))*60*60*24),yr%,mo%,dy%,temp%,temp%,temp%,temp%
						yr%=yr%+1
						dpday%=dy%
						dp&=((scdtos&:(yr%,mo%,dy%))/24/60/60)+days(1,1,1970)
					elseif (B.rep$="Daily")
						ds&=ds&+1
						dsday%=0
						dp&=dp&+1
						dpday%=0
					elseif (B.rep$="Weekly")
						ds&=ds&+7
						dsday%=0
						dp&=dp&+7
						dpday%=0
					endif
					use B
					fcount%=count
					if ((B.always$="No")and(du&<dp&)) rem times up
						erase
						deleted%=1
					else
						wrstm:(ds&,dsday%,dp&,dpday%,B.desc$,amm,B.type$,B.rep$,B.always$,du&)
					endif
					changed%=1
				endif
				if (deleted%=0)
					update
				endif
				i%=i%+1
			endwh
		until (changed%=0)
		busy off
		if (many%<>0) rem things have been added
			giprint num$(many%,10)+" transactions added"
		endif
		if (stmstat$="On")
			use A
			fcount%=count
		else
			use B
			fcount%=count
		endif
	endif
	display:(count-1,scrlen%)
endp

proc bankt:
	local stmnx%,stmmx%,stmny%,stmmy%,stmlen%
	if (stdstat$="NoFile")
		if (sure%:("Create Repeats File?"))
			trap create nfile$,B,sdate$,pdate$,desc$,amm$,type$,rep$,always$,until$
			if err
				showerr:(err)
				return
			endif
			stdstat$="File"
		else
			return
		endif
	endif
	stmnx%=5
	stmny%=10
	stmmx%=scwidth%-10
	stmmy%=schight%-10
	stmwin%=gcreate(stmnx%+2,stmny%+linehi%+3,stmmx%-4,stmmy%-linehi%-4,1,1) rem s3a
rem s3z 	stmwin%=gcreate(stmnx%+2,stmny%+linehi%+3,stmmx%-4,stmmy%-linehi%-4,1) 
	dispwin%=stmwin%
	stmzwin:
	scrpos%=1
	filepos%=0
	lastpos%=filepos%
	stmstat$="Off"
	use B
	fcount%=count
	display:(filepos%,scrpos%)
endp

proc banki:
	local k%,infowin%
	infowin%=strtscr%:
	k%=get
	gclose infowin%
	guse dispwin%
endp

proc bankd:
	local i%,oldchq%,oldstm%,thispos%,x%,extra%,pval%
	position rptr%:(curidx%,filepos%)
	if (sure%:("Delete entry?"))
		if A.order$<>"0"
			busy "Deleting entry ..."
			if (A.stm$="x")
				stotal=stotal-val(A.amm$)
				sorder%=sorder%-1
			endif
			total=total-val(A.amm$)
			oldchq%=val(A.order$)
			oldstm%=val(A.sorder$)
			erase
			fcount%=count
			freealloc chqidx% rem s3a
			freealloc stmidx% rem s3a
			chqidx%=alloc(count*2) rem s3a
			stmidx%=alloc(count*2) rem s3a
rem s3z 			call($0381,0,chqidx%) 
rem s3z 			call($0381,0,stmidx%) 
rem s3z 			chqidx%=call($0081,0,count*2) 
rem s3z 			stmidx%=call($0081,0,count*2) 
			if ((chqidx%=0)or(stmidx%=0))
				alert ("Not enough memory to allocate index","Account file preserved")
				stop
			endif
			i%=0
			extra%=1
			while (i%<count)
				first
				x%=val(A.order$)
				if (x%>=oldchq%)
					x%=x%-1
				endif
				wptr:(chqidx%,x%,i%+1)
				A.order$=fix$(x%,0,10)
				x%=val(A.sorder$)
				pval%=x%
				if (x%=-1) rem not on statement
					pval%=sorder%+extra%
					extra%=extra%+1
				else
					if ((x%>=oldstm%)and(oldstm%<>-1))
						x%=x%-1
						pval%=x%
					endif
				endif
				wptr:(stmidx%,pval%,i%+1)
				A.sorder$=fix$(x%,0,10)
				update
				i%=i%+1
			endwh
			altcurp:
			filepos%=filepos%-1
			lastpos%=filepos%
			position rptr%:(curidx%,filepos%)
			busy off
			display:(filepos%,scrpos%)
		else
			alert("Can't delete the c/f line!")
		endif
	endif
endp

proc sure%:(disp$) rem a yes/no tester
	local k%
	dinit disp$
	dbuttons "No",%N,"Yes",%Y
	k%=dialog
	if (k%=%y)
		return 1
	endif
	return 0
endp

proc bankf:
	local found%,line%
	dinit "Search string"
	dedit search$,""
	lock on
	if dialog
		found%=ptrfind%:(filepos%,search$)
		if (found%<>-1)
			line%=int(scrlen%/2)
			position rptr%:(curidx%,found%)
			display:(found%,line%)
		else
			giprint "Not found"
		endif
	endif
	lock off
endp

proc bankg:
	local found%,line%
	found%=ptrfind%:(filepos%,search$)
	if (found%<>-1)
		line%=int(scrlen%/2)
		position rptr%:(curidx%,found%)
		display:(found%,line%)
	else
		giprint "Not found"
	endif
endp

proc bankw:
	if (why$="On")
		why$="Off"
	else
		why$="On"
	endif
	display:(filepos%,scrpos%)
endp

proc bankm:
	position rptr%:(curidx%,filepos%)
	dinit "Marker"
	dedit mark$,"Mark",6
	lock on
	if dialog
		if (A.order$<>"0") rem not the c/f line
			A.state$=mark$
			update
			upidx:(filepos%)
			display:(filepos%,scrpos%)
		else
			alert("Not the c/f line")
		endif
	endif
	lock off
endp

proc bankl:
	calc:
	display:(count-1,scrlen%)
endp

proc bankx:
	stop
endp

proc bankr:
	dottl:("Date","Type","Amount",ordstat$,"Mark","",79,statmx%-232,statmx%-160,statmx%-63,statmx%-24,statmx%-10,79) rem s3a
rem s3z 	dottl:("Date","Type","Amount",ordstat$,"Mrk","",53,statmx%-139,statmx%-95,statmx%-34,statmx%-14,statmx%-9,53) 
	guse dispwin%
	if (ordstat$="Statement")
		ordstat$="ChequeBook"
		diampos 2 rem s3a
		curidx%=stmidx%
		display:(sorder%,2)
	else
		ordstat$="Statement"
		diampos 1 rem s3a
		curidx%=chqidx%
		display:(count-1,scrlen%)
	endif
endp

proc bankq:
	local c1%,c2%,c3%,switch$(10),quest$(5),x%,font$(40),swinch$(40)
	switch$="Off,On"
	font$="Small,Medium,Large,XLarge" rem s3a
rem s3z 	font$="Large,Small" 
	swinch$="Off,Small,Large"
	c1%=which%:("On,Off",why$)
	c2%=curfont%
	c3%=swintp%+1
	dinit "Set preferences"
	dchoice c1%,"Spend type",switch$
	dchoice c2%,"Font Size",font$
	dchoice c3%,"Status Window",swinch$ rem s3a
	dedit typel$,"Transaction type",20
	dedit stypel$,"Spend type",0
	lock on
	if dialog
		position rptr%:(curidx%,0)
		A.total$=typel$
		A.stotal$=stypel$
		A.state$=getnth$:(switch$,c1%)+","+num$(c2%,2)+","+num$(c3%-1,2)
		update
		upidx:(0)
rem display spend types
		quest$=getnth$:(switch$,c1%)
		if (quest$="On")
			why$="Off"
		else
			why$="On"
		endif
rem set size off status window
		swintp%=c3%-1 rem s3a
		if (swintp%=0) rem s3a
			statuswin off rem s3a
		else rem s3a
			statuswin on, swintp% rem s3a
		endif rem s3a
rem do font operation
		curfont%=c2%
		sizewin:
		display:(filepos%,scrpos%)
	endif
	lock off
endp

rem scmore.opl begins here
proc banks:
rem close and open file to compress it
	busy "Compressing ..."
	use A
	close
	open filenm$,A,date$,desc$,amm$,state$,order$,sorder$,total$,stotal$,type$,stm$
	busy off
endp

proc bankc: rem check onto statement
	local x%,oldstat%
	position rptr%:(curidx%,filepos%)
	if A.stm$<>"x"
		checkon:
		display:(sorder%,2)
	else
		alert("Already on statement!")
	endif
endp

rem s3z proc bankuu: 
rem s3z 	bankuc: 
rem s3z endp 

proc bankuc: rem check off statement
	local i%,lorder%,lamm
	position rptr%:(curidx%,filepos%)
	if (A.stm$="x")
		if (sure%:("Remove from statement?"))
			lorder%=val(A.sorder$)
			lamm=val(A.amm$)
			sorder%=sorder%-1
			stotal=stotal-val(A.amm$)
			i%=1
			while (i%<=count)
				first
				if (val(A.sorder$)>lorder%)
					A.sorder$=num$((val(A.sorder$)-1),10)
				elseif (val(A.sorder$)=lorder%)
					A.sorder$="-1"
					A.stm$=" "
					A.stotal$="0.00"
				endif
				update
				i%=i%+1
			endwh
			buildidx:
			display:(filepos%,scrpos%)
		endif
	else
		alert("Not on statement!")
	endif
endp

proc checkon: rem check a transaction onto a statement
	sorder%=sorder%+1
	stotal=stotal+val(A.amm$)
	A.sorder$=num$(sorder%,10)
	A.stotal$=fix$(stotal,2,10)
	A.stm$="x"
	update
rem might it be quicker to be clever here?
	buildidx:
endp

proc upidx:(uppos%)
	local i%,oldpos%
	busy "Updating Index..."
	oldpos%=rptr%:(curidx%,uppos%)
	i%=0
	while (i%<count)
		if (rptr%:(chqidx%,i%)=oldpos%)
			wptr:(chqidx%,i%,count)
		elseif (rptr%:(chqidx%,i%)>oldpos%)
			wptr:(chqidx%,i%,(rptr%:(chqidx%,i%))-1)
		endif
		if (rptr%:(stmidx%,i%)=oldpos%)
			wptr:(stmidx%,i%,count)
		elseif (rptr%:(stmidx%,i%)>oldpos%)
			wptr:(stmidx%,i%,(rptr%:(stmidx%,i%))-1)
		endif
		i%=i%+1
	endwh
	busy off
endp

proc banke:
	local d&,c%,ttype$(60),d%,stype$(60),amm,state$(10),order&,sorder&,desc$(60),type$(60),lorder%,lsorder%,ltotal,lstotal,stm$(1)
	position rptr%:(curidx%,filepos%)
	d&=strtod&:(A.date$,"/")
	desc$=A.desc$
	type$=A.type$
	c%=which%:(typel$,A.desc$)
	d%=which%:(stypel$,A.type$)
	amm=val(A.amm$)
	state$=A.state$
	stm$=A.stm$
	order&=val(A.order$)
	sorder&=val(A.sorder$)
	if (order&>0) rem ordinary transaction
		dinit "Transaction details"
		ddate d&,"Date",0,days(31,12,2100)
		dchoice c%,"Trans type",typel$
		dfloat amm,"Amount",-1E13,1E13
		dchoice d%,"Spend type",stypel$
rem		dedit state$,"Mark"
rem		dedit stm$,"Statement"
rem		dlong order&,"Order",-10,32767
rem		dlong sorder&,"Statement Order",-10,32767
		lock on
		if dialog
			ttype$=getnth$:(typel$,c%)
			stype$=getnth$:(stypel$,d%)
			if ttype$="text"
				dinit "Enter free text description"
				dedit desc$,"Trans Type"
				if dialog
					ttype$=desc$
				endif
			endif
			if stype$="text"
				dinit "Enter free text description"
				dedit type$,"Spend Type"
				if dialog
					stype$=type$
				endif
			endif
			if (val(A.amm$)<>amm)
				total=total+amm-val(A.amm$)
				if stm$="x"
					stotal=stotal+amm-val(A.amm$)
				endif
				if (val(A.order$)<>count-1)
					giprint "Do a recalc to be safe :-)"
				endif
			endif
			lorder%=order&
			lsorder%=sorder&
			wrinit:(d&,ttype$,amm,state$,lorder%,lsorder%,total,0.0,stype$,stm$)
			update
			upidx:(filepos%)
			display:(filepos%,scrpos%)
		endif
		lock off
	else rem edit the c/f line
		ltotal=val(A.amm$)
		lstotal=val(A.amm$)
		dinit "Carried forward details"
		ddate d&,"Date",0,days(31,12,2100)
		dfloat ltotal,"c/f",-1E13,1E13
		lock on
		if dialog
			total=total+ltotal-val(A.amm$)
			stotal=stotal+lstotal-val(A.amm$)
			if (count<>1)
				giprint "Do a recalc to be safe :-)"
			endif
			wrcf:(d&,"c/f",ltotal,A.state$,0,0,typel$,stypel$," ","x")
			update
			upidx:(filepos%)
			lastpos%=filepos%
			display:(filepos%,scrpos%)
		endif
		lock off
	endif
endp

proc banko: rem filter on/off sc not yet implemented
	local found%
	if (onfilt$="On")
		dinit "Filter string"
		dedit filt$,""
		lock on
		if dialog
			onfilt$="Off"
			position rptr%:(curidx%,0)
			found%=ptrfind%:(0,filt$)
			if (found%<>-1)
				filepos%=found%
				lastpos%=filepos%
				fcount%=0
rem find out how many in total
				do
					found%=ptrfind%:(found%,filt$)
					fcount%=fcount%+1
				until (found%=-1)
			else
				giprint filt$+" not found"
			endif
		endif
rem alert (num$(filepos%,10)+num$(found%,10)+num$(fcount%,10))
		lock off
	else
		onfilt$="On"
		fcount%=count
		filepos%=0
		lastpos%=filepos%
	endif
rem alert (num$(filepos%,10))
	display:(filepos%,1)
endp

proc ptrfind%:(from%,fstr$)
	local i%,txt$(255)
	i%=from%+1
	do
		position rptr%:(curidx%,i%)
		txt$=A.date$+":"+A.desc$+":"+A.amm$+":"+A.total$+":"+A.stotal$+":"+A.order$+":"+A.sorder$+":"+A.state$+":"+A.type$+":"+A.stm$
		if ((loc(txt$,fstr$))<>0)
			return i%
		endif
		i%=i%+1
	until (i%>=count)
	return -1
endp

proc banka:
	local d&,c%,ttype$(20),d%,stype$(20),amm,state$(10),order&,sorder&
	d&=days(day,month,year)
	dinit "Transaction details"
	ddate d&,"Date",0,days(31,12,2100)
	dchoice c%,"Trans type",typel$
	dfloat amm,"Amount",-1E13,1E13
	dchoice d%,"Spend type",stypel$
	lock on
	if dialog
		ttype$=getnth$:(typel$,c%)
		stype$=getnth$:(stypel$,d%)
		if ttype$="text"
			dinit "Enter free text transaction type"
			dedit ttype$,"Trans Type"
			dialog
		endif
		if stype$="text"
			dinit "Enter free text spend type"
			dedit stype$,"Spend Type"
			dialog
		endif
		total=total+amm
		wrinit:(d&,ttype$,amm," ",count,-1,total,0.0,stype$," ")
		append
		addptr:
		fcount%=count
		display:(count-1,scrlen%)
	endif
	lock off
endp

proc addptr:
	chqidx%=realloc(chqidx%,count*2) rem s3a
	stmidx%=realloc(stmidx%,count*2) rem s3a
rem s3z 	call($0381,0,chqidx%) 
rem s3z 	call($0381,0,stmidx%) 
rem s3z 	chqidx%=call($0081,0,(count+1)*2) 
rem s3z 	stmidx%=call($0081,0,(count+1)*2) 
	if ((chqidx%=0)or(stmidx%=0))
		alert ("Not enough memory to allocate index","Account file preserved")
		stop
	endif
rem addresses might have changed, so reassign curidx%
	altcurp:
rem s3z 	buildidx: 
	wptr:(chqidx%,count-1,count) rem s3a
	wptr:(stmidx%,count-1,count) rem s3a
endp

proc altcurp: rem the addresses might have changed
	if (ordstat$="ChequeBook")
		curidx%=stmidx%
	else
		curidx%=chqidx%
	endif
endp

proc wptr:(index%,value%,count%) rem write value in address
	pokew uadd(index%,value%*2),count% rem s3a
rem s3z 	pokew index%+value%*2,count% 
endp

proc rptr%:(index%,value%) rem read value from address
	return (peekw(uadd(index%,value%*2))) rem s3a
rem s3z 	return (peekw(index%+value%*2)) 
endp

proc display:(from%,posit%) rem display current screen of entries
	local i%,j%,k%,pos%,lposit%,ppos%,qpos%,opos%,rpos%,disp$(128)
rem alert(num$(from%,10),num$(posit%,10))
	lposit%=posit%
rem if position beyond start of file, should be unnecessary
	if (from%<0)
		filepos%=0
rem if position beyond end of file, should be unnecessary
	else
		filepos%=min(from%,count-1)
	endif
rem if the gap between screenpos and end of screen is greater than
rem gap from file position and end of file then fill up resultant gap on screen
	if ((scrlen%-lposit%)>(fcount%-filepos%-1))and(onfilt$="On")
rem if there is enough left over to move downto fill whole screen
		if (scrlen%<fcount%)
			lposit%=scrlen%-(fcount%-filepos%-1)
		else
rem move down the spare stuff
			lposit%=filepos%+1
		endif
	endif
rem simple check for small files
	if ((lposit%>fcount%)and(onfilt$="On"))
		lposit%=fcount%
	endif
rem stop positioning beyond end of screen, should be unnecessary
	if (lposit%>scrlen%)
		lposit%=scrlen%
	endif
rem alert(num$(lposit%,10),"filepos "+num$(filepos%,10))
rem i counts position on screen j counts how many printed
	i%=0:j%=1
	gupdate off
rem top of display is current - start
	pos%=filepos%-lposit%+1
	if (pos%<0)
		pos%=0
		lposit%=filepos%+1
	endif
rem alert(num$(oldpos%,10),"filepos "+num$(filepos%,10))
rem only use indexes for main display
	if ((stmstat$="On")and(mrkstat$="On"))
		position rptr%:(curidx%,pos%)
	else
		position pos%+1
	endif
	if ((scrpos%=1)and(lposit%=1)and(filepos%=lastpos%-1))
		gscroll 0,linehi%,2,linehi%+3,gwidth-10,statmy%-linehi%
		j%=scrlen%
	elseif ((scrpos%=scrlen%)and(lposit%=scrlen%)and(filepos%=lastpos%+1))
		gscroll 0,-linehi%,2,linehi%+3+linehi%,gwidth-10,statmy%-linehi%
		j%=scrlen%
		while (i%<scrlen%-1)
			i%=i%+1
		endwh
	else
	endif
	gfont zfontid%(curfont%) :gstyle 0
	if (pos%<>0)
		gat (gwidth-8),linea%+linehi%+3 :gprint ""
	else
		gat (gwidth-8),linea%+linehi%+3 :gprintb " ",6
	endif
	k%=i%
	while ((j%<=scrlen%)and(pos%+k%<fcount%))
		if ((stmstat$="On")and(mrkstat$="On"))
			position rptr%:(curidx%,(pos%+k%))
		else
			position pos%+k%+1
		endif
		if (mrkstat$="Off")
			gat 6,(i%*linehi%+linea%+linehi%+3) :gprintb C.mark$,55,3
			gat 79,(i%*linehi%+linea%+linehi%+3) :gprintb C.amm$,80,1
		elseif (stmstat$="Off")
			if (why$="Off")
				disp$=B.desc$+"("+B.type$+")"
			else
				disp$=B.desc$
			endif
			gat 8,(i%*linehi%+linea%+linehi%+3) :gprintb left$(B.sdate$,8),80,3 rem s3a
			gat 90,(i%*linehi%+linea%+linehi%+3) :gprintb disp$,120,2 rem s3a
			gat 202+(40-gtwidth(B.amm$)),(i%*linehi%+linea%+linehi%+3) :gprintb B.amm$,gtwidth(B.amm$)+10,1 rem s3a
			gat 254,(i%*linehi%+linea%+linehi%+3) :gprintb left$(B.pdate$,8),78,3 rem s3a
			gat 334,(i%*linehi%+linea%+linehi%+3) :gprintb B.rep$,130,2 rem s3a
rem s3z 			gat 5,(i%*linehi%+linea%+linehi%+3) :gprintb left$(B.sdate$,8),50,3 
rem s3z 			gat 54,(i%*linehi%+linea%+linehi%+3) :gprintb disp$,60,2 
rem s3z 			gat 111-gtwidth(B.amm$),(i%*linehi%+linea%+linehi%+3) :gprintb B.amm$,gtwidth(B.amm$)+1,1 
rem s3z 			gat 112,(i%*linehi%+linea%+linehi%+3) :gprintb left$(B.pdate$,8),50,3 
rem s3z 			gat 162,(i%*linehi%+linea%+linehi%+3) :gprintb B.rep$,70,2 
			if (B.always$="No")
				gat 388,(i%*linehi%+linea%+linehi%+3) :gprintb B.until$,75,3 rem s3a
rem s3z 				gat 184,(i%*linehi%+linea%+linehi%+3) :gprintb B.until$,50,3 
			endif
		else
			if (why$="Off")
				disp$=A.desc$+"("+A.type$+")"
			else
				disp$=A.desc$
			endif
			if ((onfilt$="On") or (loc(disp$,filt$)<>0))
				gat 4,(i%*linehi%+linea%+linehi%+3) :gprintb A.date$,80,3 rem s3a
				gat 82,(i%*linehi%+linea%+linehi%+3) :gprintb disp$,statmx%-90,2 rem s3a
				opos%=statmx%-201+(40-gtwidth(A.amm$)) rem s3a
				ppos%=statmx%-124 rem s3a
				qpos%=statmx%-61 rem s3a
				rpos%=statmx%-21 rem s3a
rem s3z 				gat 5,(i%*linehi%+linea%+linehi%+3) :gprintb A.date$,50,3 
rem s3z 				gat 55,(i%*linehi%+linea%+linehi%+3) :gprintb disp$,100,2 
rem s3z 				opos%=statmx%-95-gtwidth(A.amm$) 
rem s3z 				ppos%=statmx%-94 
rem s3z 				qpos%=statmx%-33 
rem s3z 				rpos%=statmx%-13 
				gat opos%,(i%*linehi%+linea%+linehi%+3) :gprintb A.amm$,gtwidth(A.amm$)+1,1
				if (A.order$="0") rem the c/f line
					disp$=A.amm$
				elseif (ordstat$="Statement")
					disp$=A.total$
				else
					disp$=A.stotal$
				endif
				gat ppos%,(i%*linehi%+linea%+linehi%+3) :gprintb disp$,61,1
				if (A.order$<>"0") rem the c/f line
rem s3z 					gat qpos%,(i%*linehi%+linea%+linehi%+3) :gprintb A.state$,18,3 
					gat qpos%,(i%*linehi%+linea%+linehi%+3) :gprintb A.state$,36,3 rem s3a
				endif
rem s3z 				gat rpos%,(i%*linehi%+linea%+linehi%+3) :gprintb A.stm$,5,3 
				gat rpos%,(i%*linehi%+linea%+linehi%+3) :gprintb A.stm$,10,3 rem s3a
			else
				j%=j%-1
				i%=i%-1
			endif
		endif
		i%=i%+1
		j%=j%+1
		k%=k%+1
	endwh
	if (j%<=scrlen%)
		gat 4,(i%*linehi%+linea%+linehi%+3) :gprintb " ",statmx%-10,3 rem s3a
rem s3z 		gat 5,(i%*linehi%+linea%+linehi%+3) :gprintb " ",statmx%-10,3 
	endif
	if (((pos%+k%)<>fcount%)and(j%=scrlen%+1))
		gat (gwidth-8),gheight-lined% :gprint chr$($0d)
	else
		gat (gwidth-8),gheight-lined% :gprintb " ",6
	endif
	if (lposit%>(i%))
		scrpos%=i%
	else
		scrpos%=lposit%
	endif
	if scrpos%<1
		scrpos%=1
	endif
	gupdate on
	if ((stmstat$="On")and(mrkstat$="On"))
		position rptr%:(curidx%,filepos%)
	else
		position filepos%+1
	endif
	lastpos%=filepos%
endp

proc posfp:(inc%)
rem move file pos by inc% taking into account any filter
	local i%,sign%,b%,disp$(128)
	lastpos%=filepos%
	if ((mrkstat$="On")and(stmstat$="On")and(onfilt$="Off"))
rem only do for Ordinary view with filter on
		position rptr%:(curidx%,filepos%)
		sign%=inc%/abs(inc%)
		i%=0
		b%=0
		while ((abs(i%)<abs(inc%))and(filepos%+b%>=0)and(filepos%+b%<=count-1))
			position rptr%:(curidx%,(filepos%+b%+sign%))
			if (why$="Off")
				disp$=A.desc$+"("+A.type$+")"
			else
				disp$=A.desc$
			endif
			if ((loc(disp$,filt$)<>0))
				i%=i%+1
			endif
			b%=b%+sign%
rem alert(num$(i%,5)+" "+num$(b%,5)+" "+num$(sign%,5)+" "+num$(filepos%,5)+" "+num$(inc%,5))
		endwh
		filepos%=filepos%+b%
	else
		filepos%=filepos%+inc%
	endif
endp

proc buildidx:
	local x%,extra%
	busy "Rebuilding Indexes ..."
	first
	extra%=1
	while not eof
		x%=val(A.order$)
		wptr:(chqidx%,x%,pos)
		x%=val(A.sorder$)
		if (x%=-1) rem not on statement
			wptr:(stmidx%,sorder%+extra%,pos)
rem alert(num$(sorder%,10),num$(extra%,10))
			extra%=extra%+1
		else
			wptr:(stmidx%,x%,pos)
		endif
		next
	endwh
	busy off
endp

proc stmw:
	bankw:
endp

proc stmz:
	bankz:
endp

proc stmuz:
	bankuz:
endp

proc stmp:
	bankp:
endp

proc stmt:
	gclose stmwin%
	dispwin%=statwin%
	stmstat$="On"
	guse dispwin%
	use A
	fcount%=count
	scrpos%=scrlen%
	filepos%=count-1
rem need this to redo grey background. A quicker way?
	sizewin:
	display:(filepos%,scrpos%)
endp

proc stmx:
	bankx:
endp

proc stmd:
	position filepos%+1
	if (sure%:("Delete entry?"))
		erase
	endif
	fcount%=count
	display:(filepos%,scrpos%)
endp

proc stmi:
	banki:
endp

proc stme:
	local ds&,dp&,amm,desc$(20),type$(20),c1%,c2%,until&
	local dsday%,dpday%,temp%
	if (count=0) rem nothing here to edit
		return
	endif
	position filepos%+1
	dp&=strtod&:(B.pdate$,"/")
	ds&=strtod&:(B.sdate$,"/")
	desc$=B.desc$
	type$=B.type$
	amm=val(B.amm$)
	c1%=which%:(stmrep$,B.rep$)
	c2%=which%:("Yes,No",B.always$)
	until&=strtod&:(B.until$,"/")
	dinit "Repeats details"
	ddate ds&,"Statement Date",0,days(31,12,2100)
	ddate dp&,"Processing Date",0,days(31,12,2100)
	dedit desc$,"Order type"
	dfloat amm,"Amount",-1E13,1E13
	dedit type$,"Spend type"
rem s3z 	lock on 
rem s3z 	if dialog 
rem s3z 		dinit "Repeats details (cont)" 
	dchoice c1%,"Repeat",stmrep$
	dchoice c2%,"Forever","Yes,No"
	ddate until&,"Repeat until",0,days(31,12,2100)
	lock on rem s3a
	if dialog
		if ((until&<dp&)and(c2%=2))
			alert("Error: until date is before processing date")
		else
			secstodate ((ds&-days(1,1,1970))*60*60*24),temp%,temp%,dsday%,temp%,temp%,temp%,temp%
			secstodate ((dp&-days(1,1,1970))*60*60*24),temp%,temp%,dpday%,temp%,temp%,temp%,temp%
			wrstm:(ds&,dsday%,dp&,dpday%,desc$,amm,type$,getnth$:(stmrep$,c1%),getnth$:("Yes,No",c2%),until&)
			update
			display:(count,scrlen%)
		endif
	endif
rem s3z 	endif 
	lock off
endp

proc stma:
	local ds&,dp&,ttype$(20),amm,type$(20),c1%,c2%,until&
	local dsday%,dpday%,temp%
	ds&=days(day,month,year)
	dp&=days(day,month,year)
	until&=days(day,month,year)
	dinit "Repeats details"
	ddate ds&,"Statement Date",0,days(31,12,2100)
	ddate dp&,"Processing Date",0,days(31,12,2100)
	dedit ttype$,"Order type"
	dfloat amm,"Amount",-1E13,1E13
	dedit type$,"Spend type"
rem s3z 	lock on 
rem s3z 	if dialog 
rem s3z 		dinit "Repeats details (cont)" 
	c1%=3
	dchoice c1%,"Repeat",stmrep$
	dchoice c2%,"Forever","Yes,No"
	ddate until&,"Repeat until",0,days(31,12,2100)
	lock on rem s3a
	if dialog
		if ((until&<dp&)and(c2%=2))
			alert("Error: until date is before processing date")
		else
			secstodate ((ds&-days(1,1,1970))*60*60*24),temp%,temp%,dsday%,temp%,temp%,temp%,temp%
			secstodate ((dp&-days(1,1,1970))*60*60*24),temp%,temp%,dpday%,temp%,temp%,temp%,temp%
			wrstm:(ds&,dsday%,dp&,dpday%,ttype$,amm,type$,getnth$:(stmrep$,c1%),getnth$:("Yes,No",c2%),until&)
			append
			fcount%=count
			display:(count,scrlen%)
		endif
	endif
rem s3z 	endif 
	lock off
endp

proc markc:
	local state$(5),i%,oldpos%
	position filepos%+1
	state$=C.mark$
	if (state$="x")
		alert("Already on statement!")
	else
		use A
rem run through in chqbook order
		i%=0
		while (i%<count)
			position rptr%:(chqidx%,i%)
			if (A.state$=state$)
				checkon:
			endif
			i%=i%+1
		endwh
		use C
		markj:
	endif
endp

proc markx:
close
	delete tmpfile$
	bankx:
endp

proc markj:
	gclose mrkwin%
	scrlen%=statlen%
	dispwin%=statwin%
	mrkstat$="On"
	guse dispwin%
	close
	delete tmpfile$
	use A
	fcount%=count
	scrpos%=scrlen%
	filepos%=count-1
	sizewin:
	display:(filepos%,scrpos%)
endp

proc marki:
	banki:
endp

proc markz:
	bankz:
endp

proc markuz:
	bankuz:
endp

proc movev:
	moving$="Off"
	lastpos%=filepos%
	display:(oldpos%,scrpos%)
endp

proc movez:
	bankz:
endp

proc moveuz:
	bankuz:
endp

proc movex:
	bankx:
endp

proc movea:
	local i%
	position rptr%:(curidx%,filepos%)
	if ((ordstat$="ChequeBook")and(val(A.sorder$)=-1))
		alert ("Need to place this somewhere on the statement")
		return
	endif
	busy "Moving..."
	i%=1
	while (i%<=count)
		first
		if (ordstat$="Statement")
			if ((val(A.order$)>oldpos%)and(val(A.order$)<=filepos%))
				A.order$=num$(val(A.order$)-1,10)
			elseif ((val(A.order$)<oldpos%)and(val(A.order$)>filepos%))
				A.order$=num$(val(A.order$)+1,10)
			elseif ((val(A.order$)=oldpos%)and(oldpos%>filepos%))
				A.order$=num$(filepos%+1,10)
			elseif ((val(A.order$)=oldpos%)and(oldpos%<filepos%))
				A.order$=num$(filepos%,10)
			endif
		endif
		if (ordstat$="ChequeBook")
			if ((val(A.sorder$)>oldpos%)and(val(A.sorder$)<=filepos%))
				A.sorder$=num$(val(A.sorder$)-1,10)
			elseif ((val(A.sorder$)<oldpos%)and(val(A.sorder$)>filepos%))
				A.sorder$=num$(val(A.sorder$)+1,10)
			elseif ((val(A.sorder$)=oldpos%)and(oldpos%>filepos%))
				A.sorder$=num$(filepos%+1,10)
			elseif ((val(A.sorder$)=oldpos%)and(oldpos%<filepos%))
				A.sorder$=num$(filepos%,10)
			endif
		endif
		update
		i%=i%+1
	endwh
	moving$="Off"
	busy off
	buildidx:
	lastpos%=filepos%
	display:(filepos%,scrpos%)
endp

proc movei:
	banki:
endp

proc findval:
	local order%,x%,extra%
	busy "Creating Indexes ..."
	order%=-1:sorder%=-1
	total=0:stotal=0
	extra%=1
rem set up indexes
	chqidx%=alloc(count*2) rem s3a
	stmidx%=alloc(count*2) rem s3a
rem s3z 	chqidx%=call($0081,0,count*2) 
rem s3z 	stmidx%=call($0081,0,count*2) 
	if ((chqidx%=0)or(stmidx%=0))
		alert ("Not enough memory to allocate index","Account file preserved")
		stop
	endif
	rem unfortunately we have to find sorder% first, so scan for it
	first
	while not eof
		if ((sorder%<(val(A.sorder$)))and(A.stm$="x"))
rem remember where last thing on statement is
			sorder%=(val(A.sorder$))
			if (sorder%=0) rem the c/f line
				stotal=val(A.amm$)
			else
				stotal=val(A.stotal$)
			endif
		endif
		next
	endwh
	first
	while not eof
		x%=val(A.order$)
		if (x%>=count)
			alert("Found index value too big","Doing a recalc")
			calc:
rem startagain after recalc
			first
			continue
		endif
		wptr:(chqidx%,x%,pos)
		x%=val(A.sorder$)
		if (x%>=count)
			alert("Found index value too big","Doing a recalc")
			calc:
rem startagain after recalc
			first
			continue
		endif
		if (x%=-1) rem not on statement
			wptr:(stmidx%,sorder%+extra%,pos)
			extra%=extra%+1
		else
			wptr:(stmidx%,x%,pos)
		endif
		if (val(A.order$)=count-1)
			if (A.order$="0") rem the c/f line
				total=val(A.amm$)
			else
				total=val(A.total$)
			endif
		endif
		next
	endwh
	getpref:
	busy off
endp

proc getpref: rem get preferences from c/f line
	local quest$(20)
rem find options from c/f line
	position rptr%:(chqidx%,0)
	typel$=A.total$
	stypel$=A.stotal$
rem display spend types
	quest$=getnth$:(A.state$,1)
	if (quest$="On")
		why$="Off"
	else
		why$="On"
	endif
rem set size off status window
	quest$=getnth$:(A.state$,3)
	swintp%=val(quest$) rem s3a
	if (swintp%=0) rem s3a
		statuswin off rem s3a
	else rem s3a
		statuswin on, swintp% rem s3a
	endif rem s3a
rem set font
	quest$=getnth$:(A.state$,2)
	curfont%=val(quest$)
	if (curfont%>zfonts%)
		curfont%=zfonts%
	endif
endp

proc reorder:(afield$)
	local last%,e$(40),e1$(40),f$(40),e%,lpos%,n$(128),c%,win%,altf$(128)
	last :last%=pos
	if (afield$="Statement")
		altf$="Chequebook"
	else
		altf$="Statement"
	endif
	win%=createg%:("Reorder "+altf$+" ...")
	if count>0 rem not really necessary
		while last%<>0
			dispg:(win%,(100-(last%*100/count)))
			position last% :e%=pos
			if (afield$="Statement")
				e$=A.order$
				do
					if val(A.order$)<=val(e$)
						e$=A.order$ :e%=pos
					endif
					lpos%=pos :back
				until pos=1 and lpos%=1
			endif
			if (afield$="Chequebook")
				e$=A.sorder$
				e1$=A.sorder$
				if (e1$="-1")
					e1$="32000"
				endif
				do
rem nasty nasty hack, just need to think about it
					f$=A.sorder$
					if (f$="-1")
						f$="32000"
					endif
					if ((val(f$)<val(e1$)))
						e$=A.sorder$
						e1$=f$
						e%=pos
					endif
					lpos%=pos :back
				until pos=1 and lpos%=1
			endif
			position e%
			update :last%=last%-1
		endwh
	endif
	delg:(win%)
rem compress file
	banks:
endp

proc calc:
	local count%,lorder%
	count%=1
	stotal=0
	reorder:("Chequebook")
	busy "Totaling ..."
	first
	rem first one should be the carried forward
	if (A.desc$="c/f")
		stotal=val(A.amm$)
		A.sorder$="0"
		update
		first
		count%=count%+1
	else
		alert("Can't find c/f line")
	endif
	lorder%=1
	while (count%<=count)
		if (A.stm$="x")
			stotal=stotal+val(A.amm$)
			A.stotal$=fix$(stotal,2,10)
			A.sorder$=num$(lorder%,10)
			lorder%=lorder%+1
		else
			A.stotal$="0.00"
			A.sorder$="-1"
		endif
		count%=count%+1
		update
		first
	endwh
	count%=1
	total=0
	busy off
	reorder:("Statement")
	busy "Totalling ..."
	first
	rem first one should be the carried forward
	if (A.desc$="c/f")
		total=val(A.amm$)
		A.order$="0"
		update
		first
		count%=count%+1
	else
		alert("Can't find c/f line")
	endif
	lorder%=1
	while (count%<=count)
		total=total+val(A.amm$)
		A.total$=fix$(total,2,10)
		A.order$=num$(lorder%,10)
		lorder%=lorder%+1
		count%=count%+1
		update
		first
	endwh
	busy off
rem compress file
	banks:
rem unfortunately sorder% may have changed, so scan for it
	sorder%=-1
	first
	while not eof
		if ((sorder%<(val(A.sorder$)))and(A.stm$="x"))
rem remember where last thing on statement is
			sorder%=(val(A.sorder$))
			if (sorder%=0) rem the c/f line
				stotal=val(A.amm$)
			else
				stotal=val(A.stotal$)
			endif
		endif
		next
	endwh
	buildidx:
endp

proc createg%:(text$) rem create a guage
	local win%,id%
	local midx%,midy%
	midx%=(scwidth%-0)/2
	midy%=(schight%-0)/2
	id%=gidentity
	win%=gcreate(midx%-100,midy%-20,200,40,1)
	gborder 1
	gat 8,15
	gprintb text$,180,3
	gat 0,18
	glineby 200,0
	gat 10,21
	gborder $400,180,14
	guse id%
	return win%
endp

proc dispg:(win%,val%) rem display gauge
	local i%,id%
	id%=gidentity
	guse win%
	gupdate off
rem sc this seems to achieve nothing
	i%=val%+(val%-((val%/2)*2))
	gat 11,22
	gfill 178,12,1
	gfill val%*1.78,12,0
	gupdate on
	guse id%
endp

proc delg:(win%) rem delete window
	local id%
	id%=gidentity
	guse win%
	gclose win%
	guse id%
endp

proc strtod&:(date$,sep$)
	local dy%,mo%,yr%,sep1%,sep2%
	if (mid$(date$,3,1)=sep$)
		sep1%=3
		sep2%=6
	else
		sep1%=2
		sep2%=5
	endif
	if (mid$(date$,sep2%,1)<>sep$)
		sep2%=sep2%-1
	endif
rem alert(left$(date$,sep1%-1))
rem alert(mid$(date$,sep1%+1,sep2%-sep1%-1))
rem alert(mid$(date$,sep2%+1,2))
	dy%=val(left$(date$,sep1%-1))
	mo%=val(mid$(date$,sep1%+1,sep2%-sep1%-1))
	yr%=val(mid$(date$,sep2%+1,2))
	return ((scdtos&:(1900+yr%,mo%,dy%))/24/60/60)+days(1,1,1970)
endp

proc scdtos&:(yr%,mo%,dy%) rem datetosecs function that handles days like 31/2/94
	local ldy%,done%,rvalue&
	ldy%=dy%
	done%=0
	onerr decday
	do
		rvalue&=datetosecs (yr%,mo%,ldy%,1,1,1)
		done%=1
	decday::
		if (done%<>1)
			ldy%=ldy%-1
		else
			return rvalue&
		endif
	until (done%=1)
endp

proc dottl:(m$,n$,p$,q$,r$,s$,mpos%,npos%,ppos%,qpos%,rpos%,spos%,tpos%)
	local tsize%,i%
	ggrey 2 rem s3a
	gcls
	ggrey 0 rem s3a
	gborder 0
	tsize%=linehi%+2
	gat 2,tsize%-lined% :gprintb m$,mpos%-2,3
	gat mpos%+1,tsize%-lined% :gprintb n$,npos%-mpos%,3
	gat npos%+1,tsize%-lined% :gprintb p$,ppos%-npos%,3
	gat ppos%+1,tsize%-lined% :gprintb q$,qpos%-ppos%,3
	gat qpos%+1,tsize%-lined% :gprintb r$,rpos%-qpos%,3
	gat rpos%+1,tsize%-lined% :gprintb s$,spos%-rpos%,3
	gat 0,tsize% :glineby gwidth,0
	gat mpos%,tsize% :glineby 0,-tsize%
	gat npos%,tsize% :glineby 0,-tsize%
	gat ppos%,tsize% :glineby 0,-tsize%
	gat qpos%,tsize% :glineby 0,-tsize%
	gat rpos%,tsize% :glineby 0,-tsize%
	gat spos%,tsize% :glineby 0,-tsize%
	gat tpos%,tsize% :glineby 0,-tsize%
	ggrey 1 rem s3a
	gat mpos%,0 :glineby 0,gheight rem s3a
	gat npos%,0 :glineby 0,gheight rem s3a
	gat ppos%,0 :glineby 0,gheight rem s3a
	gat qpos%,0 :glineby 0,gheight rem s3a
	gat rpos%,0 :glineby 0,gheight rem s3a
	gat spos%,0 :glineby 0,gheight rem s3a
	gat tpos%,0 :glineby 0,gheight rem s3a
	i%=0 rem s3a
	do rem s3a
		gat 0,linehi%*i%+linehi%+3 :glineby gwidth,0 rem s3a
		i%=i%+1 rem s3a
	until i%>statlen% rem s3a
rem	guse dispwin%
	ggrey 0 rem s3a
endp
