// a function to call when the user clicks a date so that the date is added to the right side of the screen

function selectTheDate(argObj) {
formattedDate = datePickerController.printFormattedDate(argObj.date, "d-sl-m-sl-Y", false); //grab the selected date from the datepicker

var dateId, timeId, 
dateId = 'event-date-' + formattedDate.replace(/\//gi, '-'); // use the selected date to create a unique, valid id for the row we're going to add
timeId = 'time-' + formattedDate.replace(/\//gi, '-');
freqId = 'freq-' + formattedDate.replace(/\//gi, '-');

// first, check if the date we're going to add has already been added...
if ($('tr#' + dateId).length == 0) {
    // if it hasn't, write the following into the DOM
    $('#step2Error').html('');
    $('#step2Error').removeClass('step2NoDateError');
    $('.dates').append('<tr id="' + dateId + '"><td><p>' + formattedDate + '</p></td><td><select style="width:auto;" class="text" name="" id="' + timeId + '"><option value="Not chosen">Choose start time...</option><option>07:00</option><option>07:15</option><option>07:30</option><option>07:45</option><option>08:00</option><option>08:15</option><option>08:30</option><option>08:45</option><option>09:00</option><option>09:15</option><option>09:30</option><option>09:45</option><option>10:00</option><option>10:15</option><option>10:30</option><option>10:45</option><option>11:00</option><option>11:15</option><option>11:30</option><option>11:45</option><option>12:00</option><option>12:15</option><option>12:30</option><option>12:45</option><option>13:00</option><option>13:15</option><option>13:30</option><option>13:45</option><option>14:00</option><option>14:15</option><option>14:30</option><option>14:45</option><option>15:00</option><option>15:15</option><option>15:30</option><option>15:45</option><option>16:00</option><option>16:15</option><option>16:30</option><option>16:45</option><option>17:00</option><option>17:15</option><option>17:30</option><option>17:45</option><option>18:00</option><option>18:15</option><option>18:30</option><option>18:45</option><option>19:00</option><option>19:15</option><option>19:30</option><option>19:45</option><option>20:00</option><option>20:15</option><option>20:30</option><option>20:45</option><option>21:00</option><option>21:15</option><option>21:30</option><option>21:45</option><option>22:00</option><option>22:15</option><option>22:30</option><option>22:45</option><option>23:00</option><option>23:15</option><option>23:30</option><option>23:45</option><option>00:00</option><option>00:15</option><option>00:30</option><option>00:45</option><option>01:00</option><option>01:15</option><option>01:30</option><option>01:45</option><option>02:00</option><option>02:15</option><option>02:30</option><option>02:45</option><option>03:00</option><option>03:15</option><option>03:30</option><option>03:45</option><option>04:00</option><option>04:15</option><option>04:30</option><option>04:45</option><option>05:00</option><option>05:15</option><option>05:30</option><option>05:45</option><option>06:00</option><option>06:15</option><option>06:30</option><option>06:45</option></select></td><td><select style="width:auto;" class="text" name="" id="' + freqId + '"><option value="">Please Select...</option><option>One off</option><option>Weekly Visits – 25% discount</option><option>Fortnightly Visits – 20% discount</option><option>Monthly Visits – 15% discount</option><option>Quarterly Visits – 5% discount</option></select></td><td><a class="remove-date" href="#">Delete</a></td></tr>');
		// or tell the user it has already been added...
		} else { alert('This date has already been selected'); }


};

// set up the datepicker
function initCal() {
var dayToday = (new Date()).format("yyyymmdd"); // grab today's date and format it using the dateformat function (date.format.js file)
		
        var opts = {                            
                formElements:{"dp-s1":"d-sl-m-sl-Y"}, // set the id of the element to attach the datepicker to, set the date format we're going to use              
                staticPos:true,
                fillGrid:true,
                constrainSelection:false,
                rangeLow:dayToday, // set a minimum date of today (the date we grabbed earlier - is formatted yyyymmdd)
                finalOpacity:100,
				callbackFunctions:{"dateset":[selectTheDate]} // this callback uses the function we wrote above, adding the date to the right side of the screen
                };           
        datePickerController.createDatePicker(opts); // create the datepicker
}

// a sprinkling of jQuery to set up a click event listener on the delete buttons
$('.remove-date').live('click', function(){ // when the button is clicked,
var removeId = $(this).parent().parent().attr('id'); // find out which row it applies to...
$('tr#' + removeId).remove(); // and remove it from the DOM
return false;
});

