Do you shoot fast moving sports or wildlife?
If not, I think you're better off keeping the 500D and buying lenses.
The 40D is faster, more solid and will handle better, but unless you bash your camera around or shoot fast stuff where you need the fastest possible operation and shooting speed, the 500D will do everything you need and it's smaller and lighter.