To be honest, I'm not sure what you are trying to do. Firstly, using alerts to debug is a poor approach. It interrupts the execution flow and so does not give you an accurate representation of how the code is running. Avoid them.
Rather, learn how to use the tools in the browser. Open the JavaScript console (how, depends upon your browser), and use console.log() for tracing in place of alerts.
Also, please post complete code examples (the link you mention doesn't appear in your code so it's a mystery), be sure to
UseCodeTags, and be sucre that your code is properly indented and formatted.