I posted one code snippet here
http://www.dreamincode.net/code/snippet712.htm