The reason the first alert is undefined is because you re-declared global
as a local variable below it in the function. And in javascript that means from the top of the function it is considered the local variable.
The one below it works because just above the alert you gave it a value.