Firstly, I have formatted your code for you, which you need to do yourself. As you can see, it is eminently more readable. If you've ever used a text editor then you know how to edit the text it contains.
Secondly, you need to be far more specific about the issue. Dumping all your code and saying "it doesn't work" is just too vague. You should have a good - if not perfect - understanding of what each and every line of code is supposed to do so, when you debug it, you can check whether what actually happens matches that expectation. If it doesn't, you can report EXACTLY where and how. You should then post ONLY the relevant code.