And it's interesting that you'd post some code written by randoms some 13 years ago without even disguising the fact. I'm getting the impression that you have little to no idea what you're doing. When you have a minimal understanding of how to do something, do you imagine it's easier to start from scratch and learn how to do it correctly as you go, OR start with someone else's broken attempt and fix it to do what you need?
I won't help you fix someone else's code copied indiscriminately from the Internet to suit your purpose.