modified datepicker script for more readability and removed global variables