! dmc apr 2004 -- all xja routines use bmnmx instead of bmidp -- code
!  now in terms of min and max b on flux surfaces, not midplane values
!======================================================================
!  xjafnd-- find next intercept
!
subroutine xjafnd(istart,ifound,islope,zxi,zvpll,ze,zb,zr,zvrot)
  !
  !  numerically find orbit intercept function roots; mediate btw.
  !   xjaset and xjacon subroutine
  !
  !  arguments -- input
  !   istart  = 0 -- reinitialize
  !           = j -- search from this point
  !   islope  = 1 positive slope = -1 negative slope intercept sought
  !
  !   zmass,zekin,zepot,zmub,zpsito as in xjacon **
  !
  !           --- output
  !   ifound=midplane index if intercept found, 0 otherwise
  !
  !   if found:
  !   zxi   intercept location
  !   zvpll vpll est. at intercept
  !   ze    ptcl energy at intercept
  !   zb    field strength at intercept
  !   zr    major radius at intercept
  !
  !------------------------------------------
  !
  use nbi_com
  !
  use xjasub_mod
  !
  !============
  implicit none
  integer :: ifound,islope,istart
  !============
  real(fp) :: zxi,zvpll,ze,zb,zr,zvrot
  !============
  integer :: ifndl
  real(fp) :: zxil(2),zvplll(2),z_el(2),zbl(2),zrl(2),zvrotl(2)
  save ifndl,zxil,zvplll,z_el,zbl,zrl,zvrotl
  !
  !------------------------------------------
  !  execrable code
  !
  if(istart.eq.0) then
     !  initialize
     ifndl=0
     istart=i2nzpx
  end if
  !
  if(ifndl.eq.0) &
       call xjacon(istart,islope, &
       ifndl,zxil,zvplll,z_el,zbl,zrl,zvrotl)
  !
  if(ifndl.gt.0) then
     ifound=istart
     zxi=zxil(ifndl)
     zvpll=zvplll(ifndl)
     ze=z_el(ifndl)
     zb=zbl(ifndl)
     zr=zrl(ifndl)
     zvrot=zvrotl(ifndl)
     ifndl=ifndl-1
  else
     ifound=0
     zxi=zxil(1)
     zvpll=zvplll(1)
     ze=z_el(1)
     zb=zbl(1)
     zr=zrl(1)
     zvrot=zvrotl(1)
  end if
  !
  return
end subroutine xjafnd
