Topological changes of the photospheric magnetic field inside active regions: a prelude to flares