Ion exchange and memory effects in polyaniline