See this thread and solution by RAKESH MANE
You need to extend this information further to achieve your rest of the requirements. Obviously this is a developer's job. It should be possible.
Also go through this thread and the reply by Paul Meehan
Jogeswara Rao K