The code is too long for the forum..
I will give you a link to the code.
here is a better link.....sorry