Texturing terrain at runtime

Is it possible to write a script (well I know it is probably possible) to put textures on a terrain at runtime, with different textures at different heights on the terrain and if the terrain is raised and lowered then the texture will change depending on the height?

What you are trying to do is very similar to the Terrain Toolkit, which tends to fill my needs. You might want to look at that, and how it works. The code is commented if I remember properly.