Organized code into separate functions

For easier testing purposes. can refer to here:
http://stackoverflow.com/questions/2269307/using-jquery-ajax-to-call-a-php-function
for help.