Variational method for three-dimensional toroidal equilibria