	SUBROUTINE DIAL_AJ
C
C	This routine dials the phone number for the Anderson Jacobsen modem
C
C 	April 15, 1985, Lou Kates
C
	INCLUDE 'COM.INC/NOLIST'

	CHARACTER*(*) NFG_MSG, BAD_MSG
	PARAMETER TMO = 10		! Read timeout count.

	INTEGER*4 AUTO_RETRY, STATUS

	PARAMETER (NFG_MSG = SS//
	1 '*** The modem is not responding, aborting... ***'//BELL//SS)
	PARAMETER (BAD_MSG = SS//
	1 '*** Bad response from the modem, aborting... ***'//BELL//SS)

	IF (PHONE_SIZE .EQ. 0) RETURN	! No phone number to dial.
C
C	Setup the terminator table to terminate the read on the
C	AJ prompt character (asterisk *).
C
	CALL SET_TERMINATOR(TPTR,TTBL,%REF('*'))

	AUTO_RETRY = 0			! Initialize retry counter.
C
C	Now queue up a read to the remote.
C
100	IF (.NOT. POST_READ(RBUFFER,TMO,TPTR)) RETURN
C
C	To put the AJ in autodial (interactive) mode, we must
C	send Control e followed by Return and wait for the read.
C
	CALL WRITE_AJ(%REF(ENQ),1)
	CALL WRITE_AJ(%REF(CR),1)	! Send enq cr
	IF (.NOT. WAIT_FOR_READ(RBUFFER,RDESC,.TRUE.)) THEN
		IF (CONTROLC_TYPED) RETURN ! Return if aborted.
		AUTO_RETRY = AUTO_RETRY + 1 ! Bump the retry count.
		IF (AUTO_RETRY .EQ. AUTODIAL_LIMIT) THEN
			CALL WRITE_USER(NFG_MSG)
			RETURN
		ELSE
			GO TO 100	! Try it again ...
		ENDIF
	ENDIF
	AUTO_RETRY = 0			! Reinitialize the retry counter.
C
C	Now we'll do a keyboard dial command.
C
C	*D
C	NUMBER?
C
	CALL SET_TERMINATOR(TPTR,TTBL,%REF(CR))! Terminate read on cr
	IF (.NOT. POST_READ(RBUFFER,TMO,TPTR)) RETURN ! Read the prompt echo.
	CALL WRITE_AJ(%REF('D'),1)
	CALL WRITE_AJ(%REF(CR),1)
	IF (.NOT. WAIT_FOR_READ(RBUFFER,RDESC,.TRUE.)) THEN
		CALL WRITE_USER(BAD_MSG)	! Tell user its no good.
		RETURN
	ENDIF
C
C	Now send the phone number.
C
	CALL SET_TERMINATOR(TPTR,TTBL,%REF(':'))! Terminate read on :
	IF (.NOT. POST_READ(RBUFFER,TMO,TPTR)) RETURN ! Read "DIALING:"
	CALL WRITE_AJ(%REF( PHONE_NUMBER ), PHONE_SIZE ) ! Send the number 
	CALL WRITE_AJ(%REF(CR),1)
	CALL WRITE_AJ(%REF(CR),1)
	CALL WRITE_AJ(%REF(CR),1)
300	IF (.NOT. WAIT_FOR_READ(RBUFFER,RDESC,.TRUE.)) THEN
310		CALL WRITE_AJ(CR,1)		! Any character to abort.
		CALL WRITE_USER(BAD_MSG)	! Tell user its no good.
		RETURN
	ENDIF
C
C	Now set the read terminator to CR.  The AJ will display
C	one of the following messages after dialing:
C
C	*** DIALING: phone_number ON LINE
C				  FAILED CALL
C
	CALL SET_TERMINATOR(TPTR,TTBL,%REF(CR))
	IF (.NOT. POST_READ(RBUFFER,60,TPTR)) RETURN ! Get the dial response.
	IF (.NOT. WAIT_FOR_READ(RBUFFER,RDESC,.TRUE.)) GO TO 310
C
C	If the response from the modem was no answer, try again.
C	If we need to redial, the response from the RIXON is:
C
C	*R
C	DIALING: phone_number <responses listed above>
C
	IF (FIND_SUBSTRING('FAILED CALL',RDESC) .GT. 0) THEN
		AUTO_RETRY = AUTO_RETRY + 1	! Bump the retry counter.
		IF (AUTO_RETRY .EQ. AUTODIAL_LIMIT) RETURN
		CALL WRITE_AJ(%REF('R'),1)	! redial
		CALL SET_TERMINATOR(TPTR,TTBL,%REF(CR))
		IF (.NOT. POST_READ(RBUFFER,TMO,TPTR)) RETURN
		GO TO 300
	ENDIF
C
C	If the modem is really online, set the flags to say so.
C
	IF (FIND_SUBSTRING('ON LINE',RDESC) .GT. 0) THEN
		MODEM_ONLINE = .TRUE.		! Show modem is online.
		REMOTE = .TRUE.			! Make VMS look online.
		IF (.NOT. POST_READ(RBUFFER,0,TPTR)) RETURN ! Flush buffer.
		CALL WAIT_FOR_READ(RBUFFER,RDESC,.TRUE.)
	ENDIF
	RETURN
	END

	SUBROUTINE WRITE_AJ(BUFF,BYTES)
C
C	This routine is used to write characters to the AJ modem.  Each
C	character is written with .40 second delays between them so the
C	AJ doesn't get overrun.
C
	INTEGER*4 BYTES, I
	LOGICAL*1 BUFF(1)

	DO 100 I = 1,BYTES
	CALL WAITABIT('00.40')		! Wait for a short time.
	CALL WRITE_BYTE(BUFF(I),1)	! Write the next character.
100	CONTINUE
	RETURN
	END
