I have this simple javascript function:
Try:
You had a function that was setting popup to 1 but was never checking its value again. This way it gets checked and should work properly.