I need a little help with the AE/AF lock and the various settings on
the D40. If I want to expose for the backround and use fill flash
for foreground subjects, how do I refocus on the subjects when I move
the frame away from the backround (which I need to lock the exposure
on).
You can try using Slow Sync on the flash. This slows the shutter speed and balances the exposure of the foreground/background. You can set this manually, or use the "Night Portrait" scene mode. It's explained in your manual. This might require a tripod.
In PAS modes, if your AE/AF button is set to default, pressing it will lock both the focus and the exposure. If you set it to "AE Lock Only", it will lock the exposure until you release it, but won't affect focus.
Setting it to "AE Lock Hold" will lock the exposure when you press and release the AE/AF button. When you press and release it again, the exposure is unlocked. You have to be careful with this, as it keeps exposure locked through multiple pictures and you may forget you've left it locked.
Also, what's the point of custom setting 13, AE Lock on/off when this
seems to be covered in custom setting 12 and the various AE/AF combos?
[13] controls whether pressing and holding the shutter-release button (not the AE/AF button) halfway will lock exposure. Normally it does not, but you can enable it.
What you probably want is [12] set to AE Lock Only, and [13] set to Off. Then you point to the background area you want to expose for, press and hold AE/AF, point to the subject you want to focus on and press the shutter-release halfway and hold, then compose the shot and press the shutter-release the rest of the way to take the picture.
The camera should be in spot or center metering to do this. Remember spot metering meters off the selected focus point, and center meters around a larger area at the center focus point.