function dateChanged(calendar) {
  // Beware that this function is called even if the end-user only
  // changed the month/year.  In order to determine if a date was
  // clicked you can use the dateClicked property of the calendar:  
  if (calendar.dateClicked) {
    // verify input
    if (typeof cal_id == 'undefined') { alert ('cal_id must be defined'); return; }
    if (cal_id === undefined) { alert ('cal_id must be defined'); return; }
    if (isNaN(cal_id)) { alert ('cal_id must be numeric'); return; }
    // OK, a date was clicked, redirect
    var y = calendar.date.getFullYear();
    var m = calendar.date.getMonth() + 1;     // integer, 0..11
    var d = calendar.date.getDate();          // integer, 1..31
    var lzm = '';
    var lzd = '';
    if (m < 10) { lzm = '0' };
    if (d < 10) { lzd = '0' };
    // Check that the date is not already reserved
    var ymd =  y + lzm + m + lzd + d;
    //alert ("checking to see if " + ymd + ' is reserved');
    //alert (aReserved[ymd]);
    if (aReserved[ymd] == 1) {
      alert ("The date you selected is Sold Out");
      
    //clear the date in the selected box
    var cal_label=document.getElementById('calendarlabel');
    if (cal_label.innerHTML=='Please Select Your Tour Date'){
      document.getElementById('arrival_date_c').value='';
    }
      return;
    }
    // Check that the date is not old
    var today = new Date();
    var yest = new Date(today.getFullYear(), today.getMonth(), today.getDate(), 0,0,0,0);
    if (calendar.date < yest) {
      alert ("You cannot select a date in the past");
      return;
    }
	
    // Check that the date is not with in 24 hours
    var tomorrow=new Date();
    tomorrow.setDate(tomorrow.getDate()+1);
    if (calendar.date < tomorrow) {
      alert ("You cannot book within 24 hours of today");
      return;
    }
    
    //place the selected date in the textbox
    var cal_label=document.getElementById('calendarlabel');
    if (cal_label.innerHTML=='Please Select Your Tour Date'){
    //  document.getElementById('arrival_date_c').value=lzm+m+'/'+lzd+d+'/'+y;//calendar.date;
      document.getElementById('arrival_date_c').value=y+'-'+lzm+m+'-'+lzd+d;// FonalityCRM calendar.date;	
    }
        
  }
};


// Customize the onSelect function when an item is selected in the pop-up
// calendar
function onSelect(cal) {
  var p = cal.params;
  var update = (cal.dateClicked || p.electric);

  var y = calendar.date.getFullYear();
  var m = calendar.date.getMonth() + 1;     // integer, 0..11
  var d = calendar.date.getDate();      // integer, 1..31
  var lzm = '';
  var lzd = '';
  if (m < 10) { lzm = '0' };
  if (d < 10) { lzd = '0' };

  // Check that the date is not already reserved
  var ymd =  y + lzm + m + lzd + d;
  //alert ("checking to see if " + ymd + ' is reserved');
  //alert (aReserved[ymd]);
  if (aReserved[ymd] == 1) {
    alert ("The date you selected is reserved");
    return;
  }
  // Check that the date is not old
  var today = new Date();
  var yest = new Date(today.getFullYear(), today.getMonth(), today.getDate(), 0,0,0,0);
  //myDate.setDate(myDate.getDate()+7);
  if (calendar.date < yest) {
    alert ("You cannot select a date in the past");
    return;
  }
  // Check that the date is not with in 24 hours
  var tomorrow=new Date();
  tomorrow.setDate(tomorrow.getDate()+1);
  if (calendar.date < tomorrow) {
    alert ("You cannot book within 24 hours of today");
    return;
  }

  if (update && p.inputField) {
    p.inputField.value = cal.date.print(p.ifFormat);
    if (typeof p.inputField.onchange == "function")
      p.inputField.onchange();
  }
  if (update && p.displayArea)
    p.displayArea.innerHTML = cal.date.print(p.daFormat);
  if (update && typeof p.onUpdate == "function")
    p.onUpdate(cal);
  if (update && p.flat) {
    if (typeof p.flatCallback == "function")
      p.flatCallback(cal);
  }
  if (update && p.singleClick && cal.dateClicked)
    cal.callCloseHandler();
};

// this is the actual date status handler.  Note that it receives the
// date object as well as separate values of year, month and date, for
// your confort. See section 5.3.8 for details.
function dateStatusHandler(date, y, m, d) {
  if (dateIsReserved(date, y, m+1, d)) return 'reserved';
  else return false;
}

// Returns true if a date is reserved
// Arguments:
//    date - a Date object
//    y - full year
//    m - integer of current month (1..12)
//    d - day of month (1..31)
function dateIsReserved(date, y, m, d) {
  var lzm = '';
  var lzd = '';
  if (m < 10) { lzm = '0' };
  if (d < 10) { lzd = '0' };
  var fulldate = y + lzm + m + lzd + d;
  //var fulldate = date.getFullYear().toString() + (date.getMonth() + 1) + date.getDate().toString();
  //if (date.getDate() == today.getDate()) { alert("hello world\n" + date.getDate() + "\n" + today.getDate() + "\n" + fulldate); }
  //if (date.getDate() == today.getDate()) { alert("hello world\n" + fulldate ); }

  // Disable all previous dates
  var today = new Date();
  
  // This is the functionality that allows us to add days to the Calendar avaialabilty function
  // Ex: This will show availability two days from today.
  today.setDate(today.getDate()+2);
  
  if (date < today) {
    //var fulltoday = today.getFullYear().toString() + (today.getMonth() + 1) + today.getDate().toString();
    var y = today.getFullYear();
    var m = today.getMonth() + 1;     // integer, 0..11
    var d = today.getDate();      // integer, 1..31
    var lzm = '';
    var lzd = '';
    if (m < 10) { lzm = '0' };
    if (d < 10) { lzd = '0' };
    var fulltoday = y + lzm + m + lzd + d;
    //alert("fulldate = " + fulldate + "\nfulltoday = " + fulltoday);
    if (m < 10) { lzm = '0' };
    if (d < 10) { lzd = '0' };
    if (fulldate != fulltoday) return true;
  }
  // Disable reserved dates
  for (key in aReserved) {
    if (key == fulldate) {
      //alert ("Found a reserved date - " + key.toString() + "\nFulldate = " + fulldate);
      return true;
    }
  }
}

// DEPRECATED!
// If this handler returns true then the "date" given as
// parameter will be disabled.  In this example we enable
// only days within a range of 10 days from the current
// date.
// You can use the functions date.getFullYear() -- returns the year
// as 4 digit number, date.getMonth() -- returns the month as 0..11,
// and date.getDate() -- returns the date of the month as 1..31, to
// make heavy calculations here.  However, beware that this function
// should be very fast, as it is called for each day in a month when
// the calendar is (re)constructed.
function isDisabled(date) {
  var today = new Date();
  var disabled;

  // does not append 0 to 1-digit months or days
  var fulldate = date.getFullYear().toString() + (date.getMonth() + 1) + date.getDate().toString();
  //if (date.getDate() == today.getDate()) { alert("hello world\n" + date.getDate() + "\n" + today.getDate() + "\n" + fulldate); }

  // Disable all previous dates
  disabled = date < today;
  // Disable reserved dates
  for (key in aReserved) {
    if (key == fulldate) {
      //alert ("Found a reserved date - " + key.toString());
      disabled = true;
    }
  }
  return disabled;
}
