// Edit these variables to match the maintenance window

// RESOURCES
// https://stackoverflow.com/questions/27416750/how-to-check-if-the-current-time-is-in-between-two-given-times

// ============= ACTIVATE THE NOTICE HERE ==============
var NoticeIsActive = false;      // Set this variable to "true" if you want the notice to be active.
// ===================================================

var MaintYear = "2020";                     // Enter the year in which the maintenance begins (Ex.: 2020)
var MainWeekday = "Tuesday";             // Enter the day of the week maintenance is to begin (Ex.: Friday)
var MaintMonth = "April";                // Enter the Month in which the maintenance begins (Ex.: April)
var MaintMonthNum = "4";                // Enter the Month number in which the maintenance begins (Ex.: 4)
var MaintDate = "27";                        // Enter the day of the month (Ex.: 17)
var EndMaintYear = "2020";              // Enter the year in which the maintenance ends (Ex.: 2020)
var EndMaintWeekday = "Wednesday"  // Enter the day in which the maintenance ends (Ex.: Saturday)
var EndMaintMonth = "April";         // Enter the Month in which the maintenance begins (Ex.: April)
var EndMaintMonthNum = "4";         // Enter the Month in which the maintenance begins (Ex.: April)
var EndMaintDate = "29";                 // Enter the date in which the maintenance begins (Ex.: 5)

var startTime = '10:00 PM';              // Enter the time at which the maintenance begins (Ex.: 10:00 PM)
var endTime = '2:00 AM';                  // Enter the time at which the maintenance ends (Ex.: 2:00 AM)

var TruncatedEndTime = endTime.charAt(0);

var  MaintTimeIsPast = false;

var DateMatches = false;
var EverythingMatches = false;
var TimesMatch = false;
var date=new Date();
var year=date.getFullYear();
var month=date.getMonth() + 1;
var day=date.getDate();
var time=date.getHours();
var minutes=date.getMinutes();

var Int_EndMaintYear = parseInt(EndMaintYear);
var Int_EndMaintMonth = parseInt(EndMaintMonthNum);
var Int_EndMaintDate = parseInt(EndMaintDate);
var Int_EndMaintTime = parseInt(endTime);

if (Int_EndMaintYear <= year &&
      Int_EndMaintMonth <= month &&
      Int_EndMaintDate <= day &&
      Int_EndMaintTime <= time){
    MaintTimeIsPast = true;
}

if (NoticeIsActive && MaintTimeIsPast == false) {
    var curr_time = getval();

    if (NoticeIsActive && MaintTimeIsPast == false){
        // Create the notification
        $('<ServerMaintenanceNotice>' + 
        '<div class="ServerMaintenanceContent">SCHEDULED MAINTENANCE: <br/>On ' +
        MainWeekday + ', '  + MaintMonth + " " + MaintDate + " starting at " + startTime + ' CST' +
        ' the documentation servers will undergo scheduled maintenance and will be unavailable ' +
        'during this time.<br/>' + 
        'Regular service is scheduled to resume at ' +
        endTime + ' CST, ' + EndMaintWeekday + ', ' + EndMaintMonth + ' ' + EndMaintDate + '.<br/>' +
        'NOTE: This does not impact Wolters Kluwer applications. Only documentation is affected.</div></BrowserVersionNotice>').insertBefore('header');
        $('.ServerMaintenanceNotice').css({
            'display' : 'flex',
            'position' : 'absolute',
            'top' : '0px',
            'width' : '100%',
            'background-color' : 'darkgray'
        });
        $('.ServerMaintenanceContent').css({
            'margin' : 'auto',
            'background-color': '#28a745',
            'color' : 'white',
            'padding' : '10px',
            'text-align' : 'center'
        });
    }
}







function get24Hr(time){
      var hours = Number(time.match(/^(\d+)/)[1]);
      var AMPM = time.match(/\s(.*)$/)[1];
      if(AMPM == "PM" && hours<12) hours = hours+12;
      if(AMPM == "AM" && hours==12) hours = hours-12;

      var minutes = Number(time.match(/:(\d+)/)[1]);
      hours = hours*100+minutes;
      console.log(time +" - "+hours);
      return hours;
 }

function getval() {
     var currentTime = new Date()
     var hours = currentTime.getHours()
     var minutes = currentTime.getMinutes()

     if (minutes < 10) minutes = "0" + minutes;

     var suffix = "AM";
     if (hours >= 12) {
        suffix = "PM";
        hours = hours - 12;
     }
     if (hours == 0) {
        hours = 12;
     }
     var current_time = hours + ":" + minutes + " " + suffix;

     return current_time;

}
