;************************************************************************
;*									*
;*		     Module F of ZDT -- Edit Routines			*
;*									*
;************************************************************************
;
;
edit:	ld	a,(xflag)
	or	a
	jr	z,new		; no, go to new
	xor	a		; edit existing data record
	ld	(newflg),a
	call	decfptr
	jr	edita
;
new:				; adding a new record
	call	iniflds		; initialize fields
	ld	a,true		; set "new" flag
	ld	(newflg),a
;
edita:	call	clrmnu
	db	1,'^X/TAB/RET=nxt fld ^E=prv fld ^S/^D=char l/r'
	db	' ^Y=era cursor rt ESC=exit',2,0
;
edfields:
	ld	hl,pospanel	; point to cursor position panel
	ld	de,fieldpanel	; point to field address panel
	ld	b,(hl)		; number of fields
	inc	hl
;
efloop:	ld	a,(hl)		; save row position
	ld	(lpos),a
	call	@goxy		; position cursor
	push	bc		; save count
	ld	b,(hl)		; get field length
	dec	hl		; save column position
	ld	a,(hl)
	ld	(cpos),a
	inc	hl		; point to next field
	inc	hl
	ex	de,hl		; hl=field address
	push	hl
	call	lhlhl		; get field pointer in hl
	push	de
	call	edloop		; edit field
	pop	de
	pop	hl
	inc	hl		; point to next field address
	inc	hl
	ex	de,hl		; hl=next field cursor position
	pop	bc
	cp	esc		; quit edloop?
	jr	z,term		; yes, go to end
	cp	ctrlw		; ^W same as esc
	jr	z,term
	cp	ctrlq		; ^Q to quit without saving edit
	jr	z,term
	cp	ctrle		; if ^e, go to previous field
	jr	z,prevf
	cp	'Z'-40h
	jr	z,qskip
	djnz	efloop		; else do next field
	jr	term		; done
;
qskip:	ld	hl,mempos
	ld	de,memfld
	ld	b,6
	jr	efloop
;
prevf:	or	a		; clear carry
	push	bc		; save count
	ld	bc,6		; back up to previous field cursor pos
	sbc	hl,bc
	ex	de,hl
	ld	bc,4		; back up to previous field
	sbc	hl,bc
	ex	de,hl
	pop	bc		; restore count
	inc	b		; back up one field
	ld	a,(pospanel)	; get number of fields
	cp	b		; test for first field
	jr	nc,efloop	; loop or fall thru if past first field
;
term:	push	af
	call	delins		; delete insert msg
	pop	af
	cp	ctrlq		; exit without saving?
	jr	nz,term1	; not ^Q, so skip
;
	call	menu1
	jp	curren0
;
term1:	call	showmsg
	db	1,'TAB=restart/[SAVE]',2,0
	call	cin
	push	af
	call	clrmsg
	pop	af
	cp	tab
	jp	z,edita
	call	menu1
	ld	a,(newflg)	; editing an existing record?
	or	a
	jr	z,wrecs		; yes, we're done here
;
	ld	hl,(recs)	; get number of index records
	ld	(xfptr),hl	; save it for updinx and to
	ld	(fptr),hl	; keep track of appended records
	ld	hl,(n)
	ld	(count),hl	; save number of index records
	call	wrtflds
	call	updinx		; add new key to index table
	call	inxsrt		; rebuild order table and sort index
	ld	hl,(recs)	; increment record count
	inc	hl
	inc	hl
	inc	hl
	inc	hl
	ld	(recs),hl
;
; since the index may have been resorted and we only know the
; current fptr, we need to find the corresponding recptr to properly
; redisplay the current record at its new location in the index
;
getrp:	call	decfptr		; back up one
getrp0:	ld	hl,(first)	; start of index
	ld	de,3		; record number offset
;
recplp:	add	hl,de		; hl=recptr
	push	hl		; save recptr
	call	lhlhl		; hl=fptr for index record
	ld	de,(fptr)	; de=fptr
	call	comphd		; does this record have the right fptr?
	pop	hl		; restore recptr
	ld	de,5		; offset to next
	jr	nz,recplp	; not this index record, check next
	ld	(recptr),hl	; save record pointer
	jp	curren0		; recalc date and display
;
;
wrecs:	call	wrtflds
	ld	hl,(recptr)	; get record pointer
	ld	de,3
	sbc	hl,de		; back up to index pointer
	ex	de,hl		; pointer in de for movkey
	call	movkey		; update index key
	ld	de,ssb
	call	sort		; sort index
;
	jr	getrp		; exit from edit, so get record pointer

wrtflds:ld	hl,fields
	call	rwrite		; write new record
	ld	hl,fields2
	call	rwrite
	ld	hl,fields3
	call	rwrite
	ld	hl,fields4
	jp	rwrite
	