Theory of cavity-enhanced spontaneous parametric downconversion