/* These functions support an interface element in which you can select today, weekend, month, week, etc.. from a dropdown menu and it will autopopulate the date range with that start-end time. */

function DateRange()
{
  // Basic Date variables
  var curdate = new Date();
  this.day_of_month = this.zeroPad(curdate.getDate(), 2);
  this.month = this.zeroPad(curdate.getMonth() + 1, 2);  // 0..11, so you have to add 1
  this.year = curdate.getFullYear();
  // Last day of month
  var dd = new Date(this.year, this.month, 0);
  this.last_day_of_month = this.zeroPad(dd.getDate(), 2);

  // 1st day of week
  dd = new Date() // Today
  dd.setDate( dd.getDate() - (dd.getDay()+7-1)%7 )
  this.monday_date = this.zeroPad(dd.getDate(), 2);
  this.monday_month = this.zeroPad(dd.getMonth() + 1, 2);
  this.monday_year = dd.getFullYear();

  // Nearest Sunday
  dd = new Date() // Today
  dd.setDate( dd.getDate() - (dd.getDay())%7 +7)
  this.sunday_date = this.zeroPad(dd.getDate(), 2);
  this.sunday_month = this.zeroPad(dd.getMonth() + 1, 2);
  this.sunday_year = dd.getFullYear();

  // Nearest Friday
  dd = new Date() // Today
  dd.setDate( dd.getDate() - (dd.getDay())%7 +5)
  this.friday_date = this.zeroPad(dd.getDate(), 2);
  this.friday_month = this.zeroPad(dd.getMonth() + 1, 2);
  this.friday_year = dd.getFullYear();
}

DateRange.prototype.checkWhen = function ( element )
{
  selection = element.options[element.selectedIndex].value

  switch (selection ) {
    case "range":
      document.getElementById('daterange').style.display = 'inline';
      break

    case "week":
      document.getElementById('daterange').style.display = 'none';
      this.setDateThisWeek()
      break
    
    case "Today":
      document.getElementById('daterange').style.display = 'none';
      this.setDateToday();
      break
    
    case "weekend":
      document.getElementById('daterange').style.display = 'none';
      this.setDateThisWeekend();
      break
    
    case "month":
      document.getElementById('daterange').style.display = 'none';
      this.setDateThisMonth();
      break
    
    default:
      document.getElementById('daterange').style.display = 'none';
      break
  }
}

DateRange.prototype.setDateToday = function ()
{
  y = this.month +'/'+ this.day_of_month +'/'+ this.year;
  x = document.getElementById('occurrence_start_dt').value = y;
  x = document.getElementById('occurrence_end_dt').value = y;
}

DateRange.prototype.setDateThisMonth = function ()
{
  y = this.month +'/01/'+ this.year;
  x = document.getElementById('occurrence_start_dt').value = y;

  y = this.month +'/'+this.last_day_of_month+'/'+ this.year;
  x = document.getElementById('occurrence_end_dt').value = y;
}

DateRange.prototype.setDateThisWeek = function ()
{
  y = this.monday_month +'/'+this.monday_date+'/'+ this.monday_year;
  x = document.getElementById('occurrence_start_dt').value = y;

  y = this.sunday_month +'/'+this.sunday_date+'/'+ this.sunday_year;
  x = document.getElementById('occurrence_end_dt').value = y;
}

DateRange.prototype.setDateThisWeekend = function ()
{
  y = this.friday_month +'/'+this.friday_date+'/'+ this.friday_year;
  x = document.getElementById('occurrence_start_dt').value = y;

  y = this.sunday_month +'/'+this.sunday_date+'/'+ this.sunday_year;
  x = document.getElementById('occurrence_end_dt').value = y;
}

DateRange.prototype.zeroPad = function (num, width) {
  num = num.toString();
  while (num.length < width)
  num = "0" + num;
  return num;
}


