Morse theory and a scalar field equation on compact surfaces