Nondifferentiable functionals and applications to elliptic problems with discontinuous nonlinearities