Optimal duration and planning of switching treatments taking drug toxicity into account