That's way too much code for us to wade through, and we can't do your homework for you. I suggest breaking your problem down into an SSCCE and focusing on one thing at a time.