move js flight ajax call into a function