$(function() {
   // end-date  date picker logic
    var enddate = $(".date-picker-end").datepicker({minDate:3, showOn: 'both', buttonImage: '/img/calendar_on_w.gif', buttonImageOnly: true, dateFormat: 'dd/mm/yy',
      onClose: function() {
        // get the start date
        var sdate = new Date(startdate.datepicker('getDate'));
        // get the newly set end ate
        var edate = new Date(enddate.datepicker('getDate'));
        // if date of the end is before start date, set start to 1 day before
        if (edate < sdate) {
          // minus 1 day from date of end-date
          edate.setDate(edate.getDate() - parseInt(1));
          // set the start date to this new date
          startdate.datepicker('setDate', edate);
        };      
      }
    });   

    // start-date   date picker logic
    var startdate = $(".date-picker-start").datepicker({minDate:2, showOn: 'both', buttonImage: '/img/calendar_on_w.gif', buttonImageOnly: true, dateFormat: 'dd/mm/yy',
      onClose: function() {
        // get date select from date picker
        var date = new Date(startdate.datepicker('getDate'));
        // get the no. of days from the days drop down
        var days_text = $('#no_of_days option:selected').text();
        var days = days_text.substring(0,1);
        
        // get the date from end date (if already selected)
        var edate = new Date(enddate.datepicker('getDate'));

        // if date range selected is less than 'days', set end date
        edate.setDate(edate.getDate() - parseInt(days));
        if (date > edate) {
          // add 'days' to this date
          date.setDate(date.getDate() + parseInt(days));
          // set the end date to this new date
          enddate.datepicker('setDate', date);
        };        
        // show the end-date picker
        start_date_set = true;
        // basket summary logic
      }
    }); 

    // automatically updated the enddate
    $('#no_of_days').change(function() {
      enddate.datepicker('setDate', 1 + parseInt(this.value.substr(0, 1)));
    });
    
    // startdate.setDate('1');
    startdate.datepicker('setDate', 1);
    enddate.datepicker('setDate', 2)
    $('#pickup_branch').change(function() {
      // adjust radio select box times dependant on branch opening times (assume weekday)
      // opening time for branch x
      $.ajax({
          type: "POST",
          url: "/getClosingTime.php",
          data: "branch_id_home=" + $('#pickup_branch option:selected').val() + "&type=home-pickup",
          success: function(msg){
            $("div#pickup_home").html(msg);            
          }
      });
      // closing time for branch
      $.ajax({
          type: "POST",
          url: "/getClosingTime.php",
          data: "branch_id_home=" + $('#pickup_branch option:selected').val() + "&type=home-dropoff",
          success: function(msg){
            $("div#dropoff_home").html(msg);            
          }
      });

      $("#startdate").slideDown();
      $("#enddate").slideDown();
      $("p#step1").hide();
      branch_set = true;
    });
    
    $.ajax({
        type: "POST",
        url: "/getClosingTime.php",
        data: "branch_id_home=" + $('#pickup_branch option:selected').val() + "&type=home-pickup",
        success: function(msg){
          $("div#pickup_home").html(msg);            
        }
    });
    // closing time for branch
    $.ajax({
        type: "POST",
        url: "/getClosingTime.php",
        data: "branch_id_home=" + $('#pickup_branch option:selected').val() + "&type=home-dropoff",
        success: function(msg){
          $("div#dropoff_home").html(msg);            
        }
    });
    
});