Forms API and Ajax

So, I’m working directly with the forms API, and Ajax to dynamically create a form. The form should work like this:

On form load, only one form element is visible ($form[‘date’]), which contains a programmatically populated list of test dates based on a certain content type. (This field is an entity reference field)

When a test date is selected, it runs the ‘reschedule_time_picker’ ajax callback, which takes the value of the $form[‘date’] (a node ID), and loads it. It then rebuilds the form, and adds $form[‘time’] to the form, which is a select list with values populated based off of four fields inside of the node that was loaded.

When a time in the select list is selected, it again runs ‘reschedule_time_picker’, which takes the values of $form[‘date’] and $form[‘time’], to see if somebody is currently in that slot (based on the field_eipa_test_x_details), array. If it is, it rebuilds the form, and exposes $form[‘markup2’], which displays the registrant information for the time slot and date picked.

This part of the system works, however there are circumstances where it stops working. I don’t think the values of date and time are being passed through correctly. A scenario that breaks it as like below:

1.) User selects ‘October 31, 2016’ from the list.
2.) user selects ’10:00pm’ from the list.
3.) This slot is taken, so $form[‘markup2’] displays the info
4.) User selects ‘8:00pm’ from the list.
5.) The slot is open, so $form[‘markup2’] doesn’t display info
6.) user selects ‘- Select -‘ for time.
7.) #5 happens again.
8.) User selects ‘- Select -‘ from $form[‘date’]
9.) The $form[‘time’] field goes away.
10.) User selects ‘October 31, 2016’ from the list again.
11.) The $form[‘time’] field shows again
12.) user selects ’10:00pm’ from the list, this time $form[‘markup2’] doesn’t evaluate, even though it should.

there are other scenarios like this that happen as well, but similar to this one.

Could somebody help me work through this issue so I can get it resolved? The pastebin link below is the link to the source code that I am using.

http://pastebin.com/mtmAYAL2

Drupal version: 


Source: https://www.drupal.org/taxonomy/term/4/feed