Theory of electrostatic fields in the ionosphere at polar and middle geomagnetic latitudes