Medium effects on the molecular nonlinear optical properties: Theory and applications in the polarizable continuum model