Please use code tags when posting your source. it preserves the indentation, keeping things readable. you can either type them by hand:
(without the space), or use the little "instant UBB" buttons you seen when making your post. click the button, and paste your code between.
Also, it really helps if you ask a focused question, and limit the code to the part where the problem seems to be. simply posting 261 lines of code and saying "This doesn't work" will probably not get you a very good response.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors