  The cross section for the photoproduction of large-p_T J/psi mesons at HERA
is calculated at next-to-leading order, adopting a perturbative approach to
describe the fragmentation of charm quarks and gluons into J/psi mesons. We
treat the charm quark according to the massless factorization scheme, where it
is assumed to be one of the active flavours inside the proton and the resolved
photon. We present inclusive distributions in transverse momentum and rapidity,
including the contributions due to direct and resolved photons. The importance
of the colour-octet components of the J/psi wave function, which contribute to
the fragmentation process, is emphasized. In addition to prompt J/psi
production, we consider also the production of chi_{cJ} states followed by
radiative decays to J/psi mesons, both in the colour-singlet and colour-octet
channels.
