You are probably looking for the following: http://en.wikipedia.org/wiki/Inverse_kinematics

However, it's not really that easy (Heel-X or some other guy may have done it in the past, I'm not sure).
If I were you I would try to approximate the result without some fancy mathematics - still you could for example draw a triangle where the longest side is the stretched leg and its opposing vertex the knee, then use some sinus/ cosinus theorems and probably the scalar product to calculate the necessary angle.
Simple approach: Create a 2 or more frame animation for each leg where you go from stretched to bent leg. You then memorize the distances from the body to the foot and use the ground distance to get the correct animation value (0..100%).


"Falls das Resultat nicht einfach nur dermassen gut aussieht, sollten Sie nochmal von vorn anfangen..." - Manual

Check out my new game: Pogostuck: Rage With Your Friends