

var scheduleIdFilter = [];
var scheduleTypeFilter = [];  // track schedule type name
var scheduleStartFilter = [];  // track schedule start time in seconds
var scheduleMonFilter = [];  // track if mon enabled
var scheduleTueFilter = [];  // track if tue enabled
var scheduleWedFilter = [];  // track if wed enabled
var scheduleThuFilter = [];  // track if thu enabled
var scheduleFriFilter = [];  // track if fri enabled
var scheduleSatFilter = [];  // track if sat enabled
var scheduleSunFilter = [];  // track if sun enabled
var scheduleRepeatEnabledFilter = [];  // track if schedule repeats X number of days
var scheduleRepeatDaysFilter = [];  // track if schedule is repeated and how often in days
var scheduleOffOnFilter = [];  // track if schedule is on or off
var scheduleNameFilter = [];  // track schedule custom name
var scheduleEquipmentNameFilter = [];  // track name of equipment for schedule
var scheduleEquipmentIconIdFilter = [];  // track iconID of equipment for schedule
var scheduleEnabledFilter = [];  // track if scheduled is enabled
var scheduleEmailFilter = [];  // track if email alert is enabled
var scheduleAlertFilter = [];  // track if dashboard alert is enabled
var scheduleDoseMLFilter = [];  // track how many milliliters are attached to schedule

var scheduleTimeEdit = [];  // used to track unconverted hours, converted hours, minutes and seconds when editing schedule
var scheduleNew = [];  // temporarily store new schedule as it's being created until ready to save
var totalSch = 0;
var enableButtonPressed = 0;
var viewingDosingPumpScheduleID = 0;  // track array location when viewing dosing schedules on dosing settings page

function printSchedulesTableMenu(typeID) {  // tabbed menu for schedules widget
    printHeaderTitle("SCHEDULES", "schedules3.png");  // name and file

    removeFooter();
    
    document.getElementById('content').innerHTML +=
        "<div class='cleanScreen'>\
            <div class='schedulesListContainer' id='schedulesListContainer'>\
                <div class='schedulesWidgetMenu' id='schedulesWidgetMenu'></div>\
                <div class='schedulesWidgetBoxContainer' id='schedulesWidgetBoxContainer'></div>\
            </div>\
            <div class='widgetButtonBox'>\
                <button class='button _48 add_48' style='margin-top:40px;' type='button' onclick='addSchedule()'></button>\
                <button class='button _wide' style='margin-top:40px;' type='button' onclick='featureNotAvailable()'>Upcoming Schedules</button>\
                <button class='button _48 home_48' style='margin-top:40px;' type='button' onclick='loadLayout()'></button>\
            </div>\
        </div>";


        // <div class='schedulesWidgetMenu boxheader2' id='schedulesWidgetMenu'></div>\

    $('.schedulesWidgetMenu').empty();
        
    document.getElementById('schedulesWidgetMenu').innerHTML += 
        '<div class="schedulesTopNav">\
            <a id="schedulesModesNav" onclick="printSchedulesTable(' + 6 + ')"></a>\
            <a id="schedulesRulesNav" onclick="printSchedulesTable(' + 5 + ')"></a>\
            <a id="schedulesFeederNav" onclick="printSchedulesTable(' + 4 + ')"></a>\
            <a id="schedulesDosingNav" onclick="printSchedulesTable(' + 3 + ')"></a>\
            <a id="schedulesDcNav" onclick="printSchedulesTable(' + 2 + ')"></a>\
            <a id="schedulesAcNav" onclick="printSchedulesTable(' + 1 + ')"></a>\
            <a id="schedulesAllNav" onclick="printSchedulesTable(' + 0 + ')"></a>\
            <div class="scheduleWidgetTitle" id="scheduleWidgetTitle"></div>\
        </div>';
            
    printSchedulesTable(typeID);
}

function printSchedulesTableData(typeID, pinID) {  // print schedule table
    
    scheduleIdFilter = [];
    scheduleTypeFilter = [];  // track schedule type name
    scheduleStartFilter = [];  // track schedule start time in seconds
    scheduleMonFilter = [];  // track if mon enabled
    scheduleTueFilter = [];  // track if tue enabled
    scheduleWedFilter = [];  // track if wed enabled
    scheduleThuFilter = [];  // track if thu enabled
    scheduleFriFilter = [];  // track if fri enabled
    scheduleSatFilter = [];  // track if sat enabled
    scheduleSunFilter = [];  // track if sun enabled
    scheduleRepeatEnabledFilter = [];  // track if schedule repeats X number of days
    scheduleRepeatDaysFilter = [];  // track if schedule is repeated and how often in days
    scheduleOffOnFilter = [];  // track if schedule is on or off
    scheduleNameFilter = [];  // track schedule custom name
    scheduleEquipmentNameFilter = [];  // track name of equipment for schedule
    scheduleEquipmentIconIdFilter = [];  // track iconID of equipment for schedule
    scheduleEnabledFilter = [];  // track if scheduled is enabled
    scheduleEmailFilter = [];  // track if email alert is enabled
    scheduleAlertFilter = [];  // track if dashboard alert is enabled
    scheduleDoseMLFilter = [];  // track how many milliliters are attached to schedule

    totalSch = 0;
    for (var i=0; i < scheduleID.length; i++)  // go through all schedules
    {
        if ((scheduleTypeID[i] == typeID) || (typeID == 0)) {  // find schedules that match filter
            // console.log("pinID             = " + pinID);
            // console.log("schedulePinID[i]  = " + schedulePinID[i]);
            if ((pinID == 0) || (schedulePinID[i] == pinID)) {
                scheduleIdFilter[totalSch] = scheduleID[i];
                scheduleTypeFilter[totalSch] = scheduleType[i];  // track schedule type name
                scheduleStartFilter[totalSch] = scheduleStart[i];  // track schedule start time in seconds
                scheduleMonFilter[totalSch] = scheduleMon[i];  // track if mon enabled
                scheduleTueFilter[totalSch] = scheduleTue[i];  // track if tue enabled
                scheduleWedFilter[totalSch] = scheduleWed[i];  // track if wed enabled
                scheduleThuFilter[totalSch] = scheduleThu[i];  // track if thu enabled
                scheduleFriFilter[totalSch] = scheduleFri[i];  // track if fri enabled
                scheduleSatFilter[totalSch] = scheduleSat[i];  // track if sat enabled
                scheduleSunFilter[totalSch] = scheduleSun[i];  // track if sun enabled
                scheduleRepeatDaysFilter[totalSch] = scheduleRepeatDays[i];  // track if schedule is repeated and how often in days
                if (scheduleRepeatEnabled[i] == 0) {scheduleRepeatEnabledFilter[totalSch] = 0;}
                else {scheduleRepeatEnabledFilter[totalSch] = 1;}
                scheduleOffOnFilter[totalSch] = scheduleOffOn[i];  // track if schedule is on or off
                scheduleNameFilter[totalSch] = scheduleName[i];  // track schedule custom name
                scheduleEquipmentNameFilter[totalSch] = scheduleEquipmentName[i];  // track name of equipment for schedule
                scheduleEquipmentIconIdFilter[totalSch] = scheduleEquipmentIconID[i];  // track iconID of equipment for schedule
                scheduleEnabledFilter[totalSch] = scheduleEnabled[i];  // track if scheduled is enabled
                scheduleEmailFilter[totalSch] = scheduleEmail[i];  // track if scheduled is enabled
                scheduleAlertFilter[totalSch] = scheduleAlert[i];  // track if scheduled is enabled
                scheduleDoseMLFilter[totalSch] = scheduleDoseML[i];  // track how many milliliters are attached to schedule
                totalSch++;
            }
        }
    }

    document.getElementById('typeSort').style = 'color:#FFFFFF';
    document.getElementById('equipSort').style = 'color:#FFFFFF';
    document.getElementById('descrSort').style = 'color:#FFFFFF';
    document.getElementById('startSort').style = 'color:#FFFFFF';
    document.getElementById('monS').style = 'color:#FFFFFF';
    document.getElementById('tueS').style = 'color:#FFFFFF';
    document.getElementById('wedS').style = 'color:#FFFFFF';
    document.getElementById('thuS').style = 'color:#FFFFFF';
    document.getElementById('friS').style = 'color:#FFFFFF';
    document.getElementById('satS').style = 'color:#FFFFFF';
    document.getElementById('sunS').style = 'color:#FFFFFF';

    // console.log("scheduleSortID = " + scheduleSortID);
    // console.log("scheduleSortOrder = " + scheduleSortOrder);
    if (scheduleSortID == 1) {
        document.getElementById('typeSortIcon').style = 'color:#32f3ff';
        document.getElementById('typeSort').style = 'color:#32f3ff';
        if (scheduleSortOrder == 0) {document.getElementById('typeSortIcon').className = 'sortIcon bi bi-sort-alpha-down-alt';}
        else {document.getElementById('typeSortIcon').className = 'sortIcon bi bi-sort-alpha-down';}
    }
    else if (scheduleSortID == 2) {
        document.getElementById('descrSortIcon').style = 'color:#32f3ff';
        document.getElementById('descrSort').style = 'color:#32f3ff';
        if (scheduleSortOrder == 0) {document.getElementById('descrSortIcon').className = 'sortIcon bi bi-sort-alpha-down-alt';}
        else {document.getElementById('descrSortIcon').className = 'sortIcon bi bi-sort-alpha-down';}
    }
    else if (scheduleSortID == 3) {
        document.getElementById('startSortIcon').style = 'color:#32f3ff';
        document.getElementById('startSort').style = 'color:#32f3ff';
        if (scheduleSortOrder == 0) {document.getElementById('startSortIcon').className = 'sortIcon bi bi-sort-numeric-down-alt';}
        else {document.getElementById('startSortIcon').className = 'sortIcon bi bi-sort-numeric-down';}
    }
    else if (scheduleSortID == 4) {
        document.getElementById('offOnSortIcon').style = 'color:#32f3ff';
        if (scheduleSortOrder == 0) {document.getElementById('offOnSortIcon').className = 'sortIcon bi bi-sort-alpha-down-alt';}
        else {document.getElementById('offOnSortIcon').className = 'sortIcon bi bi-sort-alpha-down';}
    }
    else if (scheduleSortID == 5) {document.getElementById('monIcon').style = 'color:#32f3ff'; document.getElementById('monS').style = 'color:#32f3ff';}
    else if (scheduleSortID == 6) {document.getElementById('tueIcon').style = 'color:#32f3ff'; document.getElementById('tueS').style = 'color:#32f3ff';}
    else if (scheduleSortID == 7) {document.getElementById('wedIcon').style = 'color:#32f3ff'; document.getElementById('wedS').style = 'color:#32f3ff';}
    else if (scheduleSortID == 8) {document.getElementById('thuIcon').style = 'color:#32f3ff'; document.getElementById('thuS').style = 'color:#32f3ff';}
    else if (scheduleSortID == 9) {document.getElementById('friIcon').style = 'color:#32f3ff'; document.getElementById('friS').style = 'color:#32f3ff';}
    else if (scheduleSortID == 10) {document.getElementById('satIcon').style = 'color:#32f3ff'; document.getElementById('satS').style = 'color:#32f3ff';}
    else if (scheduleSortID == 11) {document.getElementById('sunIcon').style = 'color:#32f3ff'; document.getElementById('sunS').style = 'color:#32f3ff';}
    else if (scheduleSortID == 12) {
        document.getElementById('enableSortIcon').style = 'color:#32f3ff';
        if (scheduleSortOrder == 0) {document.getElementById('enableSortIcon').className = 'sortIcon bi bi-sort-alpha-down-alt';}
        else {document.getElementById('enableSortIcon').className = 'sortIcon bi bi-sort-alpha-down';}
    }
    else if (scheduleSortID == 13) {
        document.getElementById('emailSortIcon').style = 'color:#32f3ff';
        if (scheduleSortOrder == 0) {document.getElementById('emailSortIcon').className = 'sortIcon bi bi-sort-alpha-down-alt';}
        else {document.getElementById('emailSortIcon').className = 'sortIcon bi bi-sort-alpha-down';}
    }
    else if (scheduleSortID == 14) {
        document.getElementById('alertSortIcon').style = 'color:#32f3ff';
        if (scheduleSortOrder == 0) {document.getElementById('alertSortIcon').className = 'sortIcon bi bi-sort-alpha-down-alt';}
        else {document.getElementById('alertSortIcon').className = 'sortIcon bi bi-sort-alpha-down';}
    }
    else if (scheduleSortID == 15) {
        document.getElementById('equipSortIcon').style = 'color:#32f3ff';
        document.getElementById('equipSort').style = 'color:#32f3ff';
        if (scheduleSortOrder == 0) {document.getElementById('equipSortIcon').className = 'sortIcon bi bi-sort-alpha-down-alt';}
        else {document.getElementById('equipSortIcon').className = 'sortIcon bi bi-sort-alpha-down';}
    }
    else if (scheduleSortID == 16) {
        document.getElementById('mlSortIcon').style = 'color:#32f3ff';
        document.getElementById('mlSort').style = 'color:#32f3ff; padding-top:4px;';
        if (scheduleSortOrder == 0) {document.getElementById('mlSortIcon').className = 'sortIcon bi bi-sort-numeric-down-alt';}
        else {document.getElementById('mlSortIcon').className = 'sortIcon bi bi-sort-numeric-down';}
    }
    
    totalPages = Math.ceil(totalSch / scheduleTableLimit);  // round up to integer
    // console.log("totalPages = " + totalPages);
    if (totalPages == 0) {totalPages = 1;}

    if (schedulePageNumber >= totalPages) schedulePageNumber = 0;  // if we come to end of list go to page 1
    else if (schedulePageNumber < 0) schedulePageNumber = (totalPages - 1);  // go to last page when backing up
    // console.log("schedulePageNumber = " + schedulePageNumber);

    var s = schedulePageNumber * scheduleTableLimit;  // calculate start array location so correct page can print
    var p = 0;  // end of array location when printing schedule list
    
    // calculate end array location so correct page can print
    if (schedulePageNumber < totalPages-1) p = (schedulePageNumber * scheduleTableLimit) + scheduleTableLimit;  // if we are in middle of list
    else p = totalSch;  // if we are at end of list get last array location

    for (var i=s; i < p; i++)  // draw each schedule to list
    {
        // console.log("scheduleIdFilter[i] = " + scheduleIdFilter[i]);
        var convertedStart = secondsToTime(scheduleStartFilter[i]);
        var start = tConvert(convertedStart);  // convert schedule start time to 12hr format if necessary
        
        const table = document.getElementById("schedulesList");

        let row = table.insertRow();
        row.id = "schedulesList_" + scheduleIdFilter[i];
        row.className = "scheduleTR";

        let expand = row.insertCell(0);
        expand.innerHTML = 
            '<div class="" id="scheduleExpandIcon_' + scheduleIdFilter[i] + '" onclick="expandSchedule(' + scheduleIdFilter[i] + ')">\
                <div class="schedulesListIcon bi bi-arrow-90deg-down" title="View Schedule Details" style="font-size:8px; margin-left:8px;"></div>\
            </div>';

        let type = row.insertCell(1);
        type.id = "schedType_" + scheduleIdFilter[i];
        type.className = "typeSort";
        type.innerHTML = scheduleTypeFilter[i];
        type.setAttribute("onclick","expandSchedule(" + scheduleIdFilter[i] + ")");

        let equip = row.insertCell(2);
        equip.id = "schedEquip_" + scheduleIdFilter[i];
        equip.className = "equipSort";
        equip.innerHTML = scheduleEquipmentNameFilter[i];
        equip.setAttribute("onclick","expandSchedule(" + scheduleIdFilter[i] + ")");

        let descr = row.insertCell(3);
        descr.id = "schedDescr_" + scheduleIdFilter[i];
        descr.className = "descrSort";
        descr.innerHTML = scheduleNameFilter[i];
        descr.setAttribute("onclick","expandSchedule(" + scheduleIdFilter[i] + ")");

        let time = row.insertCell(4);
        time.id = "schedTime_" + scheduleIdFilter[i];
        time.innerHTML = start;
        time.setAttribute("onclick","expandSchedule(" + scheduleIdFilter[i] + ")");
        
        let mL = row.insertCell(5);
        mL.className = "mlSort";
        mL.id = "doseML_" + scheduleIdFilter[i];
        if (scheduleDoseMLFilter[i] > 0) {mL.innerHTML = scheduleDoseMLFilter[i] + " mL";}
        mL.setAttribute("onclick","expandSchedule(" + scheduleIdFilter[i] + ")");

        let action = row.insertCell(6);
        action.className = "onOffSort";
        action.innerHTML = '<div id="scheduleOffOn_' + scheduleIdFilter[i] + '" class="schedulesListIcon bi bi-lightbulb-fill" data-toggle="tooltip" data-placement="bottom" title="" style="font-size:13px; color:rgb(200, 200, 200);"></div>';
        action.setAttribute("onclick","expandSchedule(" + scheduleIdFilter[i] + ")");

        let email = row.insertCell(7);
        email.className = "emailSort";
        email.innerHTML = '<div id="scheduleEmails_' + scheduleIdFilter[i] + '" class="schedulesListIcon bi bi-envelope-fill" data-toggle="tooltip" data-placement="bottom" title="" style="font-size:12px"></div>';
        email.setAttribute("onclick","expandSchedule(" + scheduleIdFilter[i] + ")");

        let alert = row.insertCell(8);
        alert.className = "alertSort";
        alert.innerHTML = '<div id="scheduleAlerts_' + scheduleIdFilter[i] + '" class="schedulesListIcon bi bi-exclamation-triangle-fill" data-toggle="tooltip" data-placement="bottom" title="" style="font-size:12px"></div>';
        alert.setAttribute("onclick","expandSchedule(" + scheduleIdFilter[i] + ")");

        if (scheduleRepeatDaysFilter[i] == 0) {  // if repeat X number of days isn't used we can create a column for each day of the week
            let m = row.insertCell(9);
            m.className = "monS";
            m.innerHTML = '<div class="schedulesListIcon" id="scheduleMon_' + scheduleIdFilter[i] + '" data-toggle="tooltip" data-placement="bottom" title="" style="font-size:15px">&nbsp</div>';
            m.setAttribute("onclick","expandSchedule(" + scheduleIdFilter[i] + ")");
            // m.colSpan = "6";

            let t = row.insertCell(10);
            t.className = "tueS";
            t.innerHTML = '<div class="schedulesListIcon" id="scheduleTue_' + scheduleIdFilter[i] + '" data-toggle="tooltip" data-placement="bottom" title="" style="font-size:15px">&nbsp</div>';
            t.setAttribute("onclick","expandSchedule(" + scheduleIdFilter[i] + ")");

            let w = row.insertCell(11);
            w.className = "wedS";
            w.innerHTML = '<div class="schedulesListIcon" id="scheduleWed_' + scheduleIdFilter[i] + '" data-toggle="tooltip" data-placement="bottom" title="" style="font-size:15px">&nbsp</div>';
            w.setAttribute("onclick","expandSchedule(" + scheduleIdFilter[i] + ")");

            let T = row.insertCell(12);
            T.className = "thuS";
            T.innerHTML = '<div class="schedulesListIcon" id="scheduleThu_' + scheduleIdFilter[i] + '" data-toggle="tooltip" data-placement="bottom" title="" style="font-size:15px">&nbsp</div>';
            T.setAttribute("onclick","expandSchedule(" + scheduleIdFilter[i] + ")");

            let f = row.insertCell(13);
            f.className = "friS";
            f.innerHTML = '<div class="schedulesListIcon" id="scheduleFri_' + scheduleIdFilter[i] + '" data-toggle="tooltip" data-placement="bottom" title="" style="font-size:15px">&nbsp</div>';
            f.setAttribute("onclick","expandSchedule(" + scheduleIdFilter[i] + ")");

            let S = row.insertCell(14);
            S.className = "satS";
            S.innerHTML = '<div class="schedulesListIcon" id="scheduleSat_' + scheduleIdFilter[i] + '" data-toggle="tooltip" data-placement="bottom" title="" style="font-size:15px">&nbsp</div>';
            S.setAttribute("onclick","expandSchedule(" + scheduleIdFilter[i] + ")");

            let s = row.insertCell(15);
            s.className = "sunS";
            s.innerHTML = '<div class="schedulesListIcon" id="scheduleSun_' + scheduleIdFilter[i] + '" data-toggle="tooltip" data-placement="bottom" title="" style="font-size:15px">&nbsp</div>';
            s.setAttribute("onclick","expandSchedule(" + scheduleIdFilter[i] + ")");

            let en = row.insertCell(16);
            en.innerHTML = '<div class="" id="scheduleEnable_' + scheduleIdFilter[i] + '" title="Enable/Disable Schedule"></div>';
    
            let edit = row.insertCell(17);
            edit.innerHTML = '<div onclick="editSchedule(' + i + ')" class="schedulesListIcon bi bi-pencil-fill" title="Edit Schedule" style="font-size:13px"></div>';
    
            let del = row.insertCell(18);
            del.innerHTML = '<div onclick="confirmDeleteSchedule(' + i + ')" class="schedulesListIcon bi bi-x-circle-fill" title="Delete Schedule" style="font-size:13px"></div>';
        
            row = table.insertRow();  // 2nd row for schedule details
            row.id = "schedulesContent_" + scheduleIdFilter[i];
            row.className = "schedulesContent";
    
            expand = row.insertCell(0);
            expand.colSpan = "19";  // span all columns for schedule details
        }
        else {  // if repeat X number of days is being used we only need one column 
            let m = row.insertCell(9);
            m.className = "monS";
            m.innerHTML = 
                '<div id="scheduleDays_' + scheduleIdFilter[i] + '" class="schedulesListTable1"></div>';
            m.setAttribute("onclick","expandSchedule(" + scheduleIdFilter[i] + ")");
            m.colSpan = "7";  // span days of week column in other rows

            let en = row.insertCell(10);
            en.innerHTML = '<div class="" id="scheduleEnable_' + scheduleIdFilter[i] + '" title="Enable/Disable Schedule"></div>';
    
            let edit = row.insertCell(11);
            edit.innerHTML = '<div onclick="editSchedule(' + i + ')" class="schedulesListIcon bi bi-pencil-fill" title="Edit Schedule" style="font-size:13px"></div>';
    
            let del = row.insertCell(12);
            del.innerHTML = '<div onclick="confirmDeleteSchedule(' + i + ')" class="schedulesListIcon bi bi-x-circle-fill" title="Delete Schedule" style="font-size:13px"></div>';
        
            row = table.insertRow();  // 2nd row for schedule details
            row.id = "schedulesContent_" + scheduleIdFilter[i];
            row.className = "schedulesContent";
    
            expand = row.insertCell(0);
            expand.colSpan = "13";  // span all columns for schedule details
        }
    }

    for (var i=s; i < p; i++)  // go through each schedule in table and set day checkmarks and other icons
    {
        if (scheduleEnabledFilter[i] == 1) {  // check if schedule is enabled
            if (scheduleRepeatDaysFilter[i] > 0) {  // if schedule repeats X number of days
                // document.getElementById('scheduleDays_' + scheduleIdFilter[i]).style.color = 'rgb(0, 0, 0)';  // 
                document.getElementById('scheduleDays_' + scheduleIdFilter[i]).innerHTML = 'Repeat every ' + scheduleRepeatDaysFilter[i] + ' days';
                document.getElementById('scheduleDays_' + scheduleIdFilter[i]).style = 'text-align:left; padding-left:12px; color:rgb(0, 0, 0);';
            }
            else {  // change check color to black
                document.getElementById('scheduleMon_' + scheduleIdFilter[i]).style.color = 'rgb(0, 0, 0)';  // 
                document.getElementById('scheduleTue_' + scheduleIdFilter[i]).style.color = 'rgb(0, 0, 0)';  // 
                document.getElementById('scheduleWed_' + scheduleIdFilter[i]).style.color = 'rgb(0, 0, 0)';  // 
                document.getElementById('scheduleThu_' + scheduleIdFilter[i]).style.color = 'rgb(0, 0, 0)';  // 
                document.getElementById('scheduleFri_' + scheduleIdFilter[i]).style.color = 'rgb(0, 0, 0)';  // 
                document.getElementById('scheduleSat_' + scheduleIdFilter[i]).style.color = 'rgb(0, 0, 0)';  // 
                document.getElementById('scheduleSun_' + scheduleIdFilter[i]).style.color = 'rgb(0, 0, 0)';  // 
            }

            if (scheduleEmailFilter[i] == 1) {  // if schedule is disabled during maintenance
                document.getElementById('scheduleEmails_' + scheduleIdFilter[i]).style = 'color:#00b610;';
                document.getElementById('scheduleEmails_' + scheduleIdFilter[i]).className = 'schedulesListIcon bi bi-envelope-fill';
                document.getElementById('scheduleEmails_' + scheduleIdFilter[i]).title = "Email will be sent when schedule runs";
            }
            else {  // if schedule is enabled during maintenance
                document.getElementById('scheduleEmails_' + scheduleIdFilter[i]).style = 'color:rgb(200, 200, 200);';
                document.getElementById('scheduleEmails_' + scheduleIdFilter[i]).className = 'schedulesListIcon bi bi-envelope-fill';
                document.getElementById('scheduleEmails_' + scheduleIdFilter[i]).title = "No email will be sent";
            }

            if (scheduleAlertFilter[i] == 1) {  // if schedule is disabled during maintenance
                document.getElementById('scheduleAlerts_' + scheduleIdFilter[i]).style = 'color:#00b610;';
                document.getElementById('scheduleAlerts_' + scheduleIdFilter[i]).className = 'schedulesListIcon bi bi-exclamation-triangle-fill';
                document.getElementById('scheduleAlerts_' + scheduleIdFilter[i]).title = "Dashboard alert will go off when schedule runs";
            }
            else {  // if schedule is enabled during maintenance
                document.getElementById('scheduleAlerts_' + scheduleIdFilter[i]).style = 'color:rgb(200, 200, 200);';
                document.getElementById('scheduleAlerts_' + scheduleIdFilter[i]).className = 'schedulesListIcon bi bi-exclamation-triangle-fill';
                document.getElementById('scheduleAlerts_' + scheduleIdFilter[i]).title = "No dashboard alert will appear";
            }

            if (scheduleOffOnFilter[i] == 0) {  // if schedule turns something off
                document.getElementById('scheduleOffOn_' + scheduleIdFilter[i]).style = 'color:#FF0000';
                document.getElementById('scheduleOffOn_' + scheduleIdFilter[i]).className = 'schedulesListIcon bi bi-lightbulb-fill';
                document.getElementById('scheduleOffOn_' + scheduleIdFilter[i]).title = "Turn " + scheduleTypeFilter[i] + " Off";
                // <div class="schedulesListIcon bi bi-lightbulb-off-fill" data-toggle="tooltip" data-placement="bottom" title="" style="font-size:13px"></div>\
            }
            else {  // if schedule turns something on
                document.getElementById('scheduleOffOn_' + scheduleIdFilter[i]).style = 'color:#00b610';
                document.getElementById('scheduleOffOn_' + scheduleIdFilter[i]).className = 'schedulesListIcon bi bi-lightbulb-fill';
                document.getElementById('scheduleOffOn_' + scheduleIdFilter[i]).title = "Turn " + scheduleTypeFilter[i] + " On";
            }

            document.getElementById('scheduleEnable_' + scheduleIdFilter[i]).innerHTML =  // set toggle switch to enabled schedule
                '<div id="scheduleEnableButton_' + scheduleIdFilter[i] + '" onclick="enableSchedule(' + i + ')" class="schedulesListIcon bi bi-toggle-on" style="font-size:20px; color:#00b610;"></div>';
        }
        else {  // check if schedule is disabled

            document.getElementById('schedType_' + scheduleIdFilter[i]).style = 'color:rgb(200, 200, 200)';
            document.getElementById('schedEquip_' + scheduleIdFilter[i]).style = 'color:rgb(200, 200, 200)';
            document.getElementById('schedDescr_' + scheduleIdFilter[i]).style = 'color:rgb(200, 200, 200)';
            document.getElementById('schedTime_' + scheduleIdFilter[i]).style = 'color:rgb(200, 200, 200)';
            document.getElementById('doseML_' + scheduleIdFilter[i]).style = 'color:rgb(200, 200, 200)';
            document.getElementById('scheduleOffOn_' + scheduleIdFilter[i]).style = 'color:rgb(230, 230, 230)';
            document.getElementById('scheduleEmails_' + scheduleIdFilter[i]).style = 'color:rgb(230, 230, 230)';
            document.getElementById('scheduleAlerts_' + scheduleIdFilter[i]).style = 'color:rgb(230, 230, 230)';

            if (scheduleRepeatDaysFilter[i] > 0) {  // if schedule repeats X number of days
                // document.getElementById('scheduleDays_' + scheduleIdFilter[i]).style = 'color:rgb(200, 200, 200)';
                document.getElementById('scheduleDays_' + scheduleIdFilter[i]).innerHTML = 'Repeat every ' + scheduleRepeatDaysFilter[i] + ' days';
                document.getElementById('scheduleDays_' + scheduleIdFilter[i]).style = 'text-align:left; padding-left:12px; color:rgb(200, 200, 200);';
            }
            else {  // change check color to black
                // change days check mark color to grey
                document.getElementById('scheduleMon_' + scheduleIdFilter[i]).style.color = 'rgb(200, 200, 200)';  // 
                document.getElementById('scheduleTue_' + scheduleIdFilter[i]).style.color = 'rgb(200, 200, 200)';  // 
                document.getElementById('scheduleWed_' + scheduleIdFilter[i]).style.color = 'rgb(200, 200, 200)';  // 
                document.getElementById('scheduleThu_' + scheduleIdFilter[i]).style.color = 'rgb(200, 200, 200)';  // 
                document.getElementById('scheduleFri_' + scheduleIdFilter[i]).style.color = 'rgb(200, 200, 200)';  // 
                document.getElementById('scheduleSat_' + scheduleIdFilter[i]).style.color = 'rgb(200, 200, 200)';  // 
                document.getElementById('scheduleSun_' + scheduleIdFilter[i]).style.color = 'rgb(200, 200, 200)';  // 
            }

            document.getElementById('scheduleEnable_' + scheduleIdFilter[i]).innerHTML =  // set toggle switch to disable schedule
                '<div id="scheduleEnableButton_' + scheduleIdFilter[i] + '" onclick="enableSchedule(' + i + ')" class="schedulesListIcon bi bi-toggle-off" style="font-size:20px; color:rgb(50, 50, 50);"></div>';
        }

        if (scheduleRepeatDaysFilter[i] == 0) {  // if schedule doesn't repeat X number of days
            document.getElementById('scheduleMon_' + scheduleIdFilter[i]).title = 'Scheduled';  // 
            document.getElementById('scheduleTue_' + scheduleIdFilter[i]).title = 'Scheduled';  // 
            document.getElementById('scheduleWed_' + scheduleIdFilter[i]).title = 'Scheduled';  // 
            document.getElementById('scheduleThu_' + scheduleIdFilter[i]).title = 'Scheduled';  // 
            document.getElementById('scheduleFri_' + scheduleIdFilter[i]).title = 'Scheduled';  // 
            document.getElementById('scheduleSat_' + scheduleIdFilter[i]).title = 'Scheduled';  // 
            document.getElementById('scheduleSun_' + scheduleIdFilter[i]).title = 'Scheduled';  // 

            // add check mark if day is enabled otherwise add a space so div retains position
            if (scheduleMonFilter[i] == 1) {document.getElementById('scheduleMon_' + scheduleIdFilter[i]).className = 'schedulesListIcon bi bi-check2';}  // 
            else {document.getElementById('scheduleMon_' + scheduleIdFilter[i]).innerHTML = '&nbsp';}  // 

            if (scheduleTueFilter[i] == 1) {document.getElementById('scheduleTue_' + scheduleIdFilter[i]).className = 'schedulesListIcon bi bi-check2';}  // 
            else {document.getElementById('scheduleTue_' + scheduleIdFilter[i]).innerHTML = '&nbsp';}  // 

            if (scheduleWedFilter[i] == 1) {document.getElementById('scheduleWed_' + scheduleIdFilter[i]).className = 'schedulesListIcon bi bi-check2';}  // 
            else {document.getElementById('scheduleWed_' + scheduleIdFilter[i]).innerHTML = '&nbsp';}  // 

            if (scheduleThuFilter[i] == 1) {document.getElementById('scheduleThu_' + scheduleIdFilter[i]).className = 'schedulesListIcon bi bi-check2';}  // 
            else {document.getElementById('scheduleThu_' + scheduleIdFilter[i]).innerHTML = '&nbsp';}  // 

            if (scheduleFriFilter[i] == 1) {document.getElementById('scheduleFri_' + scheduleIdFilter[i]).className = 'schedulesListIcon bi bi-check2';}  // 
            else {document.getElementById('scheduleFri_' + scheduleIdFilter[i]).innerHTML = '&nbsp';}  // 

            if (scheduleSatFilter[i] == 1) {document.getElementById('scheduleSat_' + scheduleIdFilter[i]).className = 'schedulesListIcon bi bi-check2';}  // 
            else {document.getElementById('scheduleSat_' + scheduleIdFilter[i]).innerHTML = '&nbsp';}  // 

            if (scheduleSunFilter[i] == 1) {document.getElementById('scheduleSun_' + scheduleIdFilter[i]).className = 'schedulesListIcon bi bi-check2';}  // 
            else {document.getElementById('scheduleSun_' + scheduleIdFilter[i]).innerHTML = '&nbsp';}  // 
        }
    }
}

function printSchedulesTable(typeID) {  // print schedule table
    $('.schedulesWidgetBoxContainer').empty();
    schedulesSaveTabSelected(typeID);

    var divExists = document.getElementById("schedulesAllNav");  // check if div ID is drawn
    if(divExists){
        document.getElementById('schedulesAllNav').className = "";
        document.getElementById('schedulesAcNav').className = "";
        document.getElementById('schedulesDcNav').className = "";
        document.getElementById('schedulesDosingNav').className = "";
        document.getElementById('schedulesFeederNav').className = "";
        document.getElementById('schedulesRulesNav').className = "";
        document.getElementById('schedulesModesNav').className = "";

        if (typeID == 0) {document.getElementById('schedulesAllNav').className = "active";
            document.getElementById('scheduleWidgetTitle').innerHTML = "Viewing All Schedules";}
        else if (typeID == 1) {document.getElementById('schedulesAcNav').className = "active";
            document.getElementById('scheduleWidgetTitle').innerHTML = "Viewing AC Schedules";}
        else if (typeID == 2) {document.getElementById('schedulesDcNav').className = "active";
            document.getElementById('scheduleWidgetTitle').innerHTML = "Viewing DC Schedules";}
        else if (typeID == 3) {document.getElementById('schedulesDosingNav').className = "active";
            document.getElementById('scheduleWidgetTitle').innerHTML = "Viewing Dosing Schedules";}
        else if (typeID == 4) {document.getElementById('schedulesFeederNav').className = "active";
            document.getElementById('scheduleWidgetTitle').innerHTML = "Viewing Feeder Schedules";}
        else if (typeID == 5) {document.getElementById('schedulesRulesNav').className = "active";
            document.getElementById('scheduleWidgetTitle').innerHTML = "Viewing Custom Rule Schedules";}
        else if (typeID == 6) { document.getElementById('schedulesModesNav').className = "active";
            document.getElementById('scheduleWidgetTitle').innerHTML = "Viewing Custom Mode Schedules";}
    }

    schedulePrevExpanded = null;
    
    
    var divExists = document.getElementById("schedulesWidgetBoxContainer");  // check if div ID is drawn
    if(divExists){
        document.getElementById('schedulesWidgetBoxContainer').innerHTML +=  // draw schedule table header
        `<div class="schedulesList" id="schedulesListBox">
            <table class="logTable">
                <thead class="logTableHeader" id="logTableHeader">
                    <tr>
                        <th></th>
                        <th class="typeSort" id="typeSort" onclick="schedulesSort(1)"><div id="typeSortIcon" class="sortIcon bi bi-arrow-down-up"></div>Type</th>
                        <th id="equipSort" onclick="schedulesSort(15)"><div id="equipSortIcon" class="sortIcon bi bi-arrow-down-up"></div>Equipment</th>
                        <th class="descrSort" id="descrSort" onclick="schedulesSort(2)"><div id="descrSortIcon" class="sortIcon bi bi-arrow-down-up"></div>Description</th>
                        <th id="startSort" onclick="schedulesSort(3)"><div id="startSortIcon" class="sortIcon bi-sort-numeric-down"></div>Runs At</th>
                        <th class="mlSort" id="mlSort" onclick="schedulesSort(16)"><div id="mlSortIcon" class="sortIcon bi-sort-numeric-down"></div>mL</th>
                        <th onclick="schedulesSort(4)"><div id="offOnSortIcon" class="sortIcon bi bi-arrow-down-up"></div></th>
                        <th class="emailSort" onclick="schedulesSort(13)"><div id="emailSortIcon" class="sortIcon bi bi-arrow-down-up"></div></th>
                        <th class="alertSort" onclick="schedulesSort(14)"><div id="alertSortIcon" class="sortIcon bi bi-arrow-down-up"></div></th>
                        <th class="monS" id="monS" onclick="schedulesSort(5)"><div id="monIcon" class="sortIcon"></div>M</th>
                        <th class="tueS" id="tueS" onclick="schedulesSort(6)"><div id="tueIcon" class="sortIcon"></div>T</th>
                        <th class="wedS" id="wedS" onclick="schedulesSort(7)"><div id="wedIcon" class="sortIcon"></div>W</th>
                        <th class="thuS" id="thuS" onclick="schedulesSort(8)"><div id="thuIcon" class="sortIcon"></div>T</th>
                        <th class="friS" id="friS" onclick="schedulesSort(9)"><div id="friIcon" class="sortIcon"></div>F</th>
                        <th class="satS" id="satS" onclick="schedulesSort(10)"><div id="satIcon" class="sortIcon"></div>S</th>
                        <th class="sunS" id="sunS" onclick="schedulesSort(11)"><div id="sunIcon" class="sortIcon"></div>S</th>
                        <th onclick="schedulesSort(12)"><div id="enableSortIcon" class="sortIcon bi bi-arrow-down-up"></div></th>
                        <th><div id="timeSortIcon" class="sortIcon"></div></th>
                        <th><div id="timeSortIcon" class="sortIcon"></div></th>
                    </tr>
                </thead>
                <div class="logUpdating" id="logUpdating">
                    <div class="updateRoller"><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div></div>
                </div>
                <tbody class="logList" id="schedulesList"></tbody>
            </table>
        </div>`;

        document.getElementById('logTableHeader').style = 'background-color:#0226f0';
        printSchedulesTableData(typeID,0);
    // }

    
    // if (divExists) {
        document.getElementById('schedulesWidgetBoxContainer').innerHTML += 
            '<div class="paginationBox">\
                <span class="pagination2 bi bi-caret-left-fill" onclick="schedulePreviousPage(' + typeID + ')"></span>\
                <span class="pagination">Page ' + (schedulePageNumber + 1) + ' of ' + totalPages + '</span>\
                <span class="pagination2 bi bi-caret-right-fill" onclick="scheduleNextPage(' + typeID + ')"></span>\
            </div>';
    }
}

function schedulesSaveTabSelected(id) {  // save which schedule tab is being viewed
    scheduleTabSelected = id;  // track which schedule tab is selected

    $.ajax({  // save to database
        method: "POST",
        url: "/php/schedulesSaveTabSelected.php",
        dataType: "text/plain",
        data: {"v": id }
    });
}

function addSchedule()  // add schedules button
{
    // console.log('addSchedule'); 
    removeFooter();

    document.getElementById('content').innerHTML +=
        "<div class='cleanScreen' id='cleanScreen'>\
        </div>";

    document.getElementById('cleanScreen').innerHTML +=  // draw schedule table header
    '<div class="settingsTitle">What is this schedule for?</div>\
    <div class="ruleOptionContainer">\
        <div class="icon96pxBox">\
            <button onclick="addSchedule2(1)" style="background-image: url(\'images/icons/AC2.png\')" class="icon96px"></button>\
            AC Outlet\
        </div>\
        <div class="icon96pxBox">\
            <button onclick="addSchedule2(2)" style="background-image: url(\'images/icons/DC2.png\')" class="icon96px"></button>\
            DC Port\
        </div>\
        <div class="icon96pxBox">\
            <button onclick="addSchedule2(3)" style="background-image: url(\'images/icons/dosing.png\')" class="icon96px"></button>\
            Dosing Pump\
        </div>\
        <!-- <div class="icon96pxBox">\
            <button onclick="addSchedule2(4)" style="background-image: url(\'images/icons/feed.png\')" class="icon96px"></button>\
            Feeder\
        </div>\
        <div class="icon96pxBox">\
            <button onclick="addSchedule2(5)" style="background-image: url(\'images/icons/rules.png\')" class="icon96px"></button>\
            Custom Rule\
        </div>\
        <div class="icon96pxBox">\
            <button onclick="addSchedule2(6)" style="background-image: url(\'images/icons/modes.png\')" class="icon96px"></button>\
            Custom Mode\
        </div> -->\
    </div>\
    <div class="widgetButtonBox">\
        <button class="button _48 back_48" type="button" onclick="printSchedulesTableMenu(' + scheduleTabSelected + ')"></button>\
    </div>';
}

function addSchedule2(type)  // add schedules button
{
    // console.log('addSchedule2 = ' + type); 
    // $('.schedulesWidgetBoxContainer').empty();

    removeFooter();

    scheduleNew[0] = type;  // track type of new schedule so we can save later

    var typeName = "";
    switch (type)
    {
        case 1: typeName = "an AC Outlet"; break;
        case 2: typeName = "a DC Port"; break;
        case 3: typeName = "a Dosing Pump"; break;
        case 4: typeName = "a Feeder"; break;
        case 5: typeName = "a Custom Rule"; break;
        case 6: typeName = "a Custom Mode"; break;
    }

    document.getElementById('content').innerHTML +=  // draw schedule table header
        '<div class="cleanScreen" id="cleanScreen">\
        <div class="settingsTitle">Select ' + typeName + ' for this schedule!</div>\
        <div class="ruleOptionContainer" id="schOutletBox"></div>\
        <div class="widgetButtonBox">\
            <button class="button _48 back_48" type="button" onclick="printSchedulesTableMenu(' + scheduleTabSelected + ')"></button>\
        </div>\
        </div>';

    $('#schOutletBox').empty();  // clear ac outlet widget
    if (type == 1) {  // print AC outlet icons
        // console.log('printAcOutlets--- Total = ' + outletPinID.length);
        // console.log(outletGpioPinNumber);
        // console.log(outletPinID);
        for (var i = 0; i < outletPinID.length; i++) {
            if (outletAssigned[i] == 1)
            {
                // create divs for each outlet
                document.getElementById('schOutletBox').innerHTML += `<div onclick="addSchedule3(` + i + `)" class="outletColumn" id='schOutletPic_` + outletPinID[i] + `'></div>`;
                    
                // print icon to screen
                document.getElementById('schOutletPic_' + outletPinID[i]).innerHTML = `<img class='outletListBackgroundGrey' src='images/Outlets/OutLD` + outletIconID[i] + `M.png'></br>` + outletName[i] + `</div>`;
            }
        }
    }
    else if (type == 2) {  // print DC port icons
        for (var i = 0; i < dcPinID.length; i++) {
            if (dcAssigned[i] == 1)
            {
                // create divs for each DC port
                document.getElementById('schOutletBox').innerHTML += `<div onclick="addSchedule3(`+ i + `)" class="outletColumn" id='schDcPic_` + dcPinID[i] + `'></div>`;

                // print icon to screen
                document.getElementById('schDcPic_' + dcPinID[i]).innerHTML = `<img class='outletListBackgroundGrey' src='images/Outlets/OutLD` + dcIconID[i] + `M.png'></br>` + dcName[i] + `</div>`;
            }
        }
    }
    else if (type == 3) {  // print DC port icons
        for (var i = 0; i < dosePinID.length; i++) {
            // if (dcAssigned[i] == 1)
            // {
                // create divs for each DC port
                document.getElementById('schOutletBox').innerHTML += `<div onclick="addSchedule3(`+ i + `)" class="outletColumn" id='schDcPic_` + dosePinID[i] + `'></div>`;

                // print icon to screen
                document.getElementById('schDcPic_' + dosePinID[i]).innerHTML = `<img class='outletListBackgroundGrey' src='images/Outlets/OutLD24M.png'></br>` + doseName[i] + `</div>`;
            // }
        }
    }
}

function validateNewSchedule() {
    for (var i = 0; i < scheduleNew.length; i++) {
        console.log('scheduleNew[' + i + ']        = ' + scheduleNew[i]);
    }
    var divExists = document.getElementById("schDescription");  // check if div ID is drawn
    if(divExists){scheduleNew[14] = document.getElementById('schDescription').value;}

    scheduleNew[3] = document.getElementById('repeatDays').value;
    if (scheduleNew[3] == "") {scheduleNew[3] = 0;}
    if (scheduleNew[3] < 0) {scheduleNew[3] = 0; document.getElementById('repeatDays').innerHTML = 0;}
    if (scheduleNew[0] == 3) {scheduleNew[16] = document.getElementById('schML').value;}

    if (((scheduleNew[14] == null) || (scheduleNew[14] == "")) && (divExists)) {  // check if name is blank.
        document.getElementById('fieldError1').style.display = "inline-block";  // show error
        document.getElementById('fieldError1').innerHTML = "You need to enter a discription for the schedule!";
        document.getElementById('schDescription').style.border = "2px solid rgb(255, 0, 0)";
        document.getElementById('schDescription').focus();
        return 0;
    }
    else if ((scheduleNew[0] == 3) && (scheduleNew[16] <= 0.0)) {  // check if mL is empty
        document.getElementById('fieldError3').style.display = "inline-block";  // show error
        document.getElementById('fieldError3').innerHTML = "You need to enter number of milliliters to be dosed!";
        document.getElementById('schML').style.border = "2px solid rgb(255, 0, 0)";
        document.getElementById('schML').focus();
        if(divExists) {
            document.getElementById('schDescription').style.border = "";
            document.getElementById('fieldError1').style.display = "none";  // show error
        }
        return 0;
    }
    else if ((scheduleNew[4] == 0) && (scheduleNew[5] == 0) && (scheduleNew[6] == 0) && (scheduleNew[7] == 0) && (scheduleNew[8] == 0) && (scheduleNew[9] == 0) && (scheduleNew[10] == 0) && (scheduleNew[3] == 0)) {  // check if no days were selected.  
        document.getElementById('fieldError4').style.display = "inline-block";  // show error
        document.getElementById('fieldError4').innerHTML = 'You need to select a day for schedule to run or set "Repeat Every X Days"!';
        document.getElementById('fieldError2').style.display = "inline-block";  // show error
        document.getElementById('fieldError2').innerHTML = "You need to enter number of days!";
        document.getElementById('repeatContainer').style.border = "2px solid rgb(255, 0, 0)";
        document.getElementById('schDayContainter').style.border = "2px solid rgb(255, 0, 0)";
        document.getElementById('schDayContainter').focus();
        if(divExists) {
            document.getElementById('schDescription').style.border = "";
            document.getElementById('schDescription').style.border = "";
            document.getElementById('fieldError1').style.display = "none";  // show error
        }
        if (scheduleNew[0] == 3) {
            document.getElementById('fieldError3').style.display = "none";  // show error
            document.getElementById('schML').style.border = "";
        }
        return 0;
    }
    else {
        document.getElementById('fieldError2').style.display = "none";  // show error
        document.getElementById('fieldError4').style.display = "none";  // show error
        document.getElementById('schDayContainter').style.border = "";
        document.getElementById('repeatContainer').style.border = "";
        if(divExists) {
            document.getElementById('schDescription').style.border = "";
            document.getElementById('fieldError1').style.display = "none";  // show error
        }
        if (scheduleNew[0] == 3) {
            document.getElementById('fieldError3').style.display = "none";  // show error
            document.getElementById('schML').style.border = "";
        }
        if (scheduleNew[2] == 0) {return 1;}  // check if repeat days is disabled, no more checks required if so 
        // return 1;
    }

    // if (scheduleNew[2] == 1) {  // check if repeat days is enabled 
    //     if ((scheduleNew[3] == null) || (scheduleNew[3] == "")) {  // check if number of days is blank.    
    //         document.getElementById('fieldError2').style.display = "inline-block";  // show error
    //         document.getElementById('fieldError2').innerHTML = "You need to enter number of days!";
    //         document.getElementById('repeatDays').style.border = "2px solid rgb(255, 0, 0)";
    //         document.getElementById('repeatDays').focus();
    //         return 0;
    //     }
    //     else {
    //         document.getElementById('fieldError2').style.display = "none";  // show error
    //         document.getElementById('repeatDays').style.border = "";
    //         return 1;
    //     }
    // }
}

function validateEditSchedule(i) {
    scheduleNameFilter[i] = document.getElementById('schDescription').value;
    scheduleRepeatDaysFilter[i] = document.getElementById('repeatDays').value;
    if (scheduleRepeatDaysFilter[i] == "") {scheduleRepeatDaysFilter[i] = 0;}
    if (scheduleRepeatDaysFilter[i] < 0) {scheduleRepeatDaysFilter[i] = 0; document.getElementById('repeatDays').innerHTML = 0;}
    if (scheduleTypeFilter[i] === "Dosing") {scheduleDoseMLFilter[i] = document.getElementById('schML').value;}

    if ((scheduleNameFilter[i] == null) || (scheduleNameFilter[i] == "")) {  // check if name is blank.
        document.getElementById('fieldError1').style.display = "inline-block";  // show error
        document.getElementById('fieldError1').innerHTML = "You need to enter a discription for the schedule!";
        document.getElementById('schDescription').style.border = "2px solid rgb(255, 0, 0)";
        document.getElementById('schDescription').focus();
        return 0;
    }
    else if ((scheduleTypeFilter[i] === "Dosing") && (scheduleDoseMLFilter[i] <= 0.0)) {  // check if mL is empty
        document.getElementById('fieldError3').style.display = "inline-block";  // show error
        document.getElementById('fieldError3').innerHTML = "You need to enter number of milliliters to be dosed!";
        document.getElementById('schML').style.border = "2px solid rgb(255, 0, 0)";
        document.getElementById('schML').focus();
        document.getElementById('fieldError1').style.display = "none";  // show error
        document.getElementById('schDescription').style.border = "";
        return 0;
    }
    else if ((scheduleMonFilter[i] == 0) && (scheduleTueFilter[i] == 0) && (scheduleWedFilter[i] == 0) && (scheduleThuFilter[i] == 0) && (scheduleFriFilter[i] == 0) && (scheduleSatFilter[i] == 0) && (scheduleSunFilter[i] == 0) && (scheduleRepeatDaysFilter[i] == 0)) {  // check if no days were selected.  
        document.getElementById('fieldError4').style.display = "inline-block";  // show error
        document.getElementById('fieldError4').innerHTML = 'You need to select a day for schedule to run or set "Repeat Every X Days"!';
        document.getElementById('schDescription').style.border = "";
        document.getElementById('repeatContainer').style.border = "2px solid rgb(255, 0, 0)";
        document.getElementById('schDayContainter').style.border = "2px solid rgb(255, 0, 0)";
        document.getElementById('schDayContainter').focus();
        document.getElementById('fieldError1').style.display = "none";  // show error
        document.getElementById('schDescription').style.border = "";
        if (scheduleTypeFilter[i] === "Dosing") {
            document.getElementById('fieldError3').style.display = "none";  // show error
            document.getElementById('schML').style.border = "";
        }
        return 0;
    }
    else {
        document.getElementById('fieldError1').style.display = "none";  // show error
        document.getElementById('fieldError2').style.display = "none";  // show error
        document.getElementById('fieldError4').style.display = "none";  // show error
        document.getElementById('schDescription').style.border = "";
        document.getElementById('schDayContainter').style.border = "";
        document.getElementById('repeatContainer').style.border = "";
        if (scheduleTypeFilter[i] === "Dosing") {
            document.getElementById('fieldError3').style.display = "none";  // show error
            document.getElementById('schML').style.border = "";
        }
        if (scheduleRepeatEnabledFilter[i] == 0) {return 1;}  // check if repeat days is disabled, no more checks required if so 
        // return 1;
    }

    if (scheduleRepeatEnabledFilter[i] == 1) {  // check if repeat days is enabled 
        if ((scheduleRepeatDaysFilter[i] == null) || (scheduleRepeatDaysFilter[i] == "")) {  // check if number of days is blank.    
            document.getElementById('fieldError2').style.display = "inline-block";  // show error
            document.getElementById('fieldError2').innerHTML = "You need to enter number of days!";
            document.getElementById('repeatDays').style.border = "2px solid rgb(255, 0, 0)";
            document.getElementById('repeatDays').focus();
            return 0;
        }
        else {
            document.getElementById('fieldError2').style.display = "none";  // show error
            document.getElementById('repeatDays').style.border = "";
            return 1;
        }
    }
}

function addSchedule3(i) {  // 
    // console.log('addSchedule3   = ' + i); 
    // var convertedStart = secondsToTime(scheduleStartFilter[i]);
    // console.log('scheduleNameFilter[i]  = ' + scheduleNameFilter[i]); 
    // console.log('scheduleStartFilter[i] = ' + scheduleStartFilter[i]); 
    
    // defaults for new schedule
    // scheduleNew[0] = 0;  // schedule type - already set before we get here
    scheduleNew[1] = 1;  // off or on
    scheduleNew[2] = 0;  // repeat enabled - only used on UI, don't need to send to backend
    scheduleNew[3] = 0;  // repeat number of days
    scheduleNew[4] = 0;  // Monday
    scheduleNew[5] = 0;  // Tuesday
    scheduleNew[6] = 0;  // Wednesday
    scheduleNew[7] = 0;  // Thursday
    scheduleNew[8] = 0;  // Friday
    scheduleNew[9] = 0;  // Saturday
    scheduleNew[10] = 0;  // Sunday
    scheduleNew[11] = 0;  // email
    scheduleNew[12] = 0;  // dashboard alert
    scheduleNew[13] = 0;  // converted schedule time
    scheduleNew[14] = "";  // custom name
    scheduleNew[15] = 0;  // pinID
    scheduleNew[16] = 0;  // milliliters for dosing schedule

    removeFooter();
    // $('.schedulesWidgetBoxContainer').empty();
    
    document.getElementById('content').innerHTML +=
        "<div class='cleanScreen' id='cleanScreen'>\
        </div>";

    var typeName = "";
    var tempIconID = 0;
    var tempName = "";
    switch (scheduleNew[0])  // get pinID and convert new schedule typeID to text
    {
        case 1: typeName = "an AC Outlet"; scheduleNew[15] = outletPinID[i]; tempIconID = outletIconID[i]; tempName = outletName[i]; break;
        case 2: typeName = "a DC Port"; scheduleNew[15] = dcPinID[i]; tempIconID = dcIconID[i]; tempName = dcName[i]; break;
        case 3: typeName = "a Dosing Pump"; scheduleNew[15] = dosePinID[i]; tempIconID = 24; tempName = doseName[i]; break;
        case 4: typeName = "a Feeder"; scheduleNew[15] = 0; break;
        case 5: typeName = "a Custom Rule"; scheduleNew[15] = 0; break;
        case 6: typeName = "a Custom Mode"; scheduleNew[15] = 0; break;
    }

    // console.log('outletPinID[i]  = ' + outletPinID[i]); 
    scheduleGetHMS(0);  // 12am as default time

    document.getElementById('cleanScreen').innerHTML +=  // draw screen
        '<div class="schDaysContainer">\
            <div class="schTypeName">New Schedule is for ' + typeName + '</div>\
            <div class="schDaysContainer">\
                <img class="menuIcon" id="schIcon" src="images/Outlets/OutLD' + tempIconID + 'L.png" onclick="schNewOffOn()"></div></br>\
                <div class="schTypeName" style="margin:3px 0px 15px 0px; font-weight:bold; font-size:14px;">' + tempName + '</div>\
            </div>\
            <div class="schDaysContainer" id="schInfoBox">\
                <div class="schFieldName">Schedule Description</div>\
                <input class="schField" type="text" id="schDescription" autocomplete="off">\
                <div class="fieldError" id="fieldError1"></div>\
            </div>\
        </div>\
        <div class="ruleOptionBox" style="padding-bottom:25px; padding-top:10px;">\
            <div class="parameterBox">\
                <div class="parameterInfoBox">\
                    <div class="parameterTitle">Hour</div>\
                    <div class="parameterValue" id="schHour">' + scheduleTimeEdit[1] + '</div>\
                </div>\
                <div class="parameterButtonBox">\
                    <button id="schButtonUp0" onmousedown="scheduleTimeUp(0)" class="upDownButton bi bi-arrow-up-short"></button></br>\
                    <button id="schButtonDown0" onmousedown="scheduleTimeDown(0)" class="upDownButton bi bi-arrow-down-short"></button>\
                </div>\
            </div>\
            <div class="parameterBox">\
                <div class="parameterInfoBox">\
                    <div class="parameterTitle">Minute</div>\
                    <div class="parameterValue" id="schMinute">' + scheduleTimeEdit[2] + '</div>\
                </div>\
                <div class="parameterButtonBox">\
                    <button id="schButtonUp1" onmousedown="scheduleTimeUp(1)" class="upDownButton bi bi-arrow-up-short"></button></br>\
                    <button id="schButtonDown1" onmousedown="scheduleTimeDown(1)" class="upDownButton bi bi-arrow-down-short"></button>\
                </div>\
            </div>\
            <div class="parameterBox">\
                <div class="parameterInfoBox">\
                    <div class="parameterTitle">Second</div>\
                    <div class="parameterValue" id="schSecond">' + scheduleTimeEdit[3] + '</div>\
                </div>\
                <div class="parameterButtonBox">\
                    <button id="schButtonUp2" onmousedown="scheduleTimeUp(2)" class="upDownButton bi bi-arrow-up-short"></button></br>\
                    <button id="schButtonDown2" onmousedown="scheduleTimeDown(2)" class="upDownButton bi bi-arrow-down-short"></button>\
                </div>\
            </div>\
        </div>\
        <div class="schDaysContainer">\
            <div class="schOffOnEdit bi bi-toggle2-on" id="schOffOnEdit" onclick="schNewOffOn()"></div>\
            <div class="repeatContainer" id="repeatContainer">\
                <input type="checkbox" id="repeatCheck" onclick="repeatNewSchedule()">\
                <label for="repeatCheck" style="margin-right:5px;">Repeat every </label>\
                <input class="schField" type="number" id="repeatDays" value="' + scheduleNew[3] + '" onchange="validateNewSchedule()" onfocusout="validateNewSchedule()" autocomplete="off">\
                <label for="repeatCheck" style="margin-left:5px;">days </label>\
                <div class="fieldError" id="fieldError2"></div>\
            </div>\
        </div>\
        <div class="schDaysContainer" id="schDayContainter">\
            <div class="schDays">\
                <label for="schDay1">Monday </label>\
                <input type="checkbox" id="schDay1" onclick="schNewChangeDay(0)">\
            </div>\
            <div class="schDays">\
                <label for="schDay2">Tuesday </label>\
                <input type="checkbox" id="schDay2" onclick="schNewChangeDay(1)">\
            </div>\
            <div class="schDays">\
                <label for="schDay3">Wednesday</label>\
                <input type="checkbox" id="schDay3" onclick="schNewChangeDay(2)">\
            </div>\
            <div class="schDays">\
                <label for="schDay4">Thursday</label>\
                <input type="checkbox" id="schDay4" onclick="schNewChangeDay(3)">\
            </div>\
            <div class="schDays">\
                <label for="schDay5">Friday</label>\
                <input type="checkbox" id="schDay5" onclick="schNewChangeDay(4)">\
            </div>\
            <div class="schDays">\
                <label for="schDay6">Saturday</label>\
                <input type="checkbox" id="schDay6" onclick="schNewChangeDay(5)">\
            </div>\
            <div class="schDays">\
                <label for="schDay7">Sunday</label>\
                <input type="checkbox" id="schDay7" onclick="schNewChangeDay(6)">\
            </div>\
            <div class="fieldError" id="fieldError4"></div>\
        </div>\
        <div class="schDaysContainer">\
            <div class="scheduleListAlerts" id="email" onclick="schNewEmail()"></div>\
            <div class="scheduleListAlerts" id="alert" onclick="schNewAlert()"></div>\
        </div>\
        <br>';

    // console.log('prevDisplayScreen            = ' + prevDisplayScreen); 
    if (prevDisplayScreen == 10) {  // if viewing dosing settings return 
        document.getElementById("cleanScreen").innerHTML +=  // draw screen
        '<div class="widgetButtonBox">\
            <button class="button _wide" type="button" onclick="schSaveNew(' + i + ')">Save</button>\
            <button class="button _48 back_48" type="button" onclick="dosingSettings()"></button>\
        </div>';
    }
    else {  // return back to schedules screen
        document.getElementById('cleanScreen').innerHTML +=  // draw screen
        '<div class="widgetButtonBox">\
            <button class="button _wide" type="button" onclick="schSaveNew(' + i + ')">Save</button>\
            <button class="button _48 back_48" type="button" onclick="printSchedulesTableMenu(' + scheduleTabSelected + ')"></button>\
        </div>';
    }

    if (scheduleNew[0] == 3) {  // schedule is for a dosing pump so add mL field
        document.getElementById('schInfoBox').innerHTML +=  // draw screen
            `<div class="schFieldName">Milliliters to Dose</div>
            <input class="schField" type="number" id="schML" autocomplete="off">
            <div class="fieldError" id="fieldError3"></div>`;
    }

    if (scheduleNew[1] == 0) {
        document.getElementById('schOffOnEdit').className = "schOffOnEdit bi bi-toggle2-off";
        document.getElementById('schOffOnEdit').innerHTML = "&nbsp Off Schedule";
        document.getElementById('schOffOnEdit').style = "color:#FF0000;";
        document.getElementById('schIcon').style.backgroundColor = "#FF0000";
    }
    else {
        document.getElementById('schOffOnEdit').className = "schOffOnEdit bi bi-toggle2-on";
        document.getElementById('schOffOnEdit').innerHTML = "&nbsp On Schedule";
        document.getElementById('schOffOnEdit').style = "color:#00b610;";
        document.getElementById('schIcon').style.backgroundColor = "#00b610";
    }

    if (scheduleNew[2] == 1) {
        document.getElementById('repeatCheck').checked = true;
        document.getElementById('repeatDays').disabled = false;
        document.getElementById('repeatDays').style = "background-color:#FFFFFF; width:70px;";
        document.getElementById('schDayContainter').style.display = "none";  // hide days
    }
    else {
        document.getElementById('repeatCheck').checked = false;
        document.getElementById('repeatDays').disabled = true;
        document.getElementById('repeatDays').value = '';
        document.getElementById('repeatDays').style = "background-color:#dfdfdf; width:70px;";
        document.getElementById('schDayContainter').style.display = "flex";  // show days
    }

    if (scheduleNew[4] == 1) {
        document.getElementById('schDay1').checked = true;
    }
    if (scheduleNew[5] == 1) {
        document.getElementById('schDay2').checked = true;
    }
    if (scheduleNew[6] == 1) {
        document.getElementById('schDay3').checked = true;
    }
    if (scheduleNew[7] == 1) {
        document.getElementById('schDay4').checked = true;
    }
    if (scheduleNew[8] == 1) {
        document.getElementById('schDay5').checked = true;
    }
    if (scheduleNew[9] == 1) {
        document.getElementById('schDay6').checked = true;
    }
    if (scheduleNew[10] == 1) {
        document.getElementById('schDay7').checked = true;
    }

    if (scheduleNew[11] == 1) {  // add email enabled icon
        document.getElementById('email').innerHTML =
            '<i class="bi bi-envelope-fill">&nbsp; Email Enabled</i>';
        document.getElementById('email').style.color = "#00b610";
    }
    else {
        document.getElementById('email').innerHTML =
            '<i class="bi bi-envelope-fill">&nbsp; Email Disabled</i>';
        document.getElementById('email').style.color = "#727272";
    }

    if (scheduleNew[12] == 1) {  // add dashboard alert enabled icon
        document.getElementById('alert').innerHTML =
            '<i class="bi bi-exclamation-triangle-fill">&nbsp; Dashboard Alert Enabled</i>';
        document.getElementById('alert').style.color = "#00b610";
    }
    else {
        document.getElementById('alert').innerHTML =
            '<i class="bi bi-exclamation-triangle-fill">&nbsp; Dashboard Alert Disabled</i>';
        document.getElementById('alert').style.color = "#727272";
    }

    document.getElementById('repeatDays').style = "background-color:#FFFFFF; width:70px;";
    
    // track mouse up/down when changing schedule time
    var timer;  // used to track setInverval so it can be cleared
    $('#schButtonUp0').mousedown(function(){
        timer = setInterval(function() { scheduleTimeUp(0); }, 250);  // run continually while mouse down
    }).mouseup(function() {
        clearInterval(timer);
    }).mouseout(function() {
        clearInterval(timer);
    });
    $('#schButtonUp1').mousedown(function(){
        timer = setInterval(function() { scheduleTimeUp(1); }, 170);  // run continually while mouse down
    }).mouseup(function() {
        clearInterval(timer);
    }).mouseout(function() {
        clearInterval(timer);
    });
    $('#schButtonUp2').mousedown(function(){
        timer = setInterval(function() { scheduleTimeUp(2); }, 150);  // run continually while mouse down
    }).mouseup(function() {
        clearInterval(timer);
    }).mouseout(function() {
        clearInterval(timer);
    });
    $('#schButtonDown0').mousedown(function(){
        timer = setInterval(function() { scheduleTimeDown(0); }, 250);  // run continually while mouse down
    }).mouseup(function() {
        clearInterval(timer);
    }).mouseout(function() {
        clearInterval(timer);
    });
    $('#schButtonDown1').mousedown(function(){
        timer = setInterval(function() { scheduleTimeDown(1); }, 170);  // run continually while mouse down
    }).mouseup(function() {
        clearInterval(timer);
    }).mouseout(function() {
        clearInterval(timer);
    });
    $('#schButtonDown2').mousedown(function(){
        timer = setInterval(function() { scheduleTimeDown(2); }, 150);  // run continually while mouse down
    }).mouseup(function() {
        clearInterval(timer);
    }).mouseout(function() {
        clearInterval(timer);
    });
}

function confirmDeleteSchedule(i) {  // 
    // console.log('confirmDeleteSchedule = ' + i);
    prevDisplayScreen = displayScreen;
    $('.ds18b20Popup-body').empty();
    $('#popupBackground').show();  // grey out background
    ds18b20Popup.style.display = "block";
    document.querySelector("body").style.overflow = 'hidden';

    $('.ds18b20PopupHeader').empty();
    $(".ds18b20PopupHeader").append("Delete Schedule");

    $(".ds18b20Popup-body").append(
        '<div style="text-align: center;"><p>&nbsp;</p>\
            <p>Are you sure you want to delete schedule "' + scheduleNameFilter[i] + '"?</p>\
            <div class="widgetButtonBox">\
                <button class="button _wide" type="button" onclick="deleteSchedule(`' + i + '`)">Yes</button>\
                <button class="button _wide" type="button" onclick="closeDs18b20Popup()">No</button>\
            </div>\
        </div>');
}

function deleteSchedule(i) {  // delete schedule from backend
    // console.log('deleteSchedule ID = ' + scheduleIdFilter[i]);
    var command = "70, " + scheduleIdFilter[i];
    sendToBackend(command);  // tell backend to delete schedule
    closeDs18b20Popup();  // close popup
    $('#popupBackground').hide();  // hide greyed out background
}

function scheduleGetHMS(value) {  // convert seconds to hours, minutes and seconds so we can edit schedule time easy
    // console.log('scheduleGetHMS---------------- '); 
    // console.log('convertHMS        = ' + value); 
    const sec = parseInt(value, 10); // convert value to number
    scheduleTimeEdit[0] = Math.floor(sec / 3600); // get hours
    scheduleTimeEdit[2] = Math.floor((sec - (scheduleTimeEdit[0] * 3600)) / 60); // get minutes
    scheduleTimeEdit[3] = sec - (scheduleTimeEdit[0] * 3600) - (scheduleTimeEdit[2] * 60); //  get seconds
    
    scheduleConvertHour();

    if (scheduleTimeEdit[2] < 10) {scheduleTimeEdit[2] = "0"+ scheduleTimeEdit[2];}  // add 0 in front of minutes if needed
    if (scheduleTimeEdit[3] < 10) {scheduleTimeEdit[3] = "0" + scheduleTimeEdit[3];}  // add 0 in front of seconds if needed

    // console.log(scheduleTimeEdit[0] + " - " + scheduleTimeEdit[1] + ":" + scheduleTimeEdit[2] + ":" + scheduleTimeEdit[3]); 
}

function scheduleConvertHour() {
    // console.log('scheduleConvertHour---------------- '); 
    // console.log('scheduleTimeEdit[0]  = ' + scheduleTimeEdit[0]); 
    if ((timeFormat == 1) && (scheduleTimeEdit[0] == 0)) scheduleTimeEdit[1] = 12 + "am";  // convert 24hr to 12hr 
    else if ((timeFormat == 1) && (scheduleTimeEdit[0] == 12)) {scheduleTimeEdit[1] = scheduleTimeEdit[0] + "pm";}  // convert 24hr to 12hr
    else if ((timeFormat == 1) && (scheduleTimeEdit[0] > 12)) {scheduleTimeEdit[1] = (scheduleTimeEdit[0] - 12) + "pm";}  // convert 24hr to 12hr
    else if (timeFormat == 1) {scheduleTimeEdit[1] = scheduleTimeEdit[0] + "am";}  // no need to convert
    else if (timeFormat == 0) {scheduleTimeEdit[1] = scheduleTimeEdit[0];}  // no need to convert

    if ((timeFormat == 0) && (scheduleTimeEdit[0] < 10)) {scheduleTimeEdit[1] = "0"+scheduleTimeEdit[0];}  // add 0 in front of hours if needed
}

function scheduleConvertEditTime(i) {  // convert time format to seconds
    // console.log('scheduleConvertEditTime---------------- '); 
    // console.log('scheduleConvertEditTime   = ' + i); 
    // console.log(scheduleTimeEdit[0] + ":" + scheduleTimeEdit[2] + ":" + scheduleTimeEdit[3]); 
    // console.log('scheduleStartFilter[i] = ' + scheduleStartFilter[i]); 
    var time = scheduleTimeEdit[0] + ":" + scheduleTimeEdit[2] + ":" + scheduleTimeEdit[3]; 
    scheduleStartFilter[i] = secondsFromTime(time);
    // console.log('scheduleStartFilter[i] = ' + scheduleStartFilter[i]); 
}

function scheduleConvertNewTime() {  // convert time format to seconds
    // console.log('scheduleConvertNewTime---------------- '); 
    // console.log(scheduleTimeEdit[0] + ":" + scheduleTimeEdit[2] + ":" + scheduleTimeEdit[3]); 
    // console.log('scheduleNew[13] = ' + scheduleNew[13]); 
    var time = scheduleTimeEdit[0] + ":" + scheduleTimeEdit[2] + ":" + scheduleTimeEdit[3]; 
    scheduleNew[13] = secondsFromTime(time);
    // console.log('scheduleNew[13] = ' + scheduleNew[13]); 
}

function scheduleTimeUp(type) {
    // console.log('scheduleTimeUp     = ' + type);
    if (type == 0) {  // hours
        scheduleTimeEdit[0]++;
        if (scheduleTimeEdit[0] > 23) {scheduleTimeEdit[0] = 0;}
        scheduleConvertHour();
        document.getElementById('schHour').innerHTML = scheduleTimeEdit[1]; // draw screen        
    }
    else if (type == 1) {  // minutes
        scheduleTimeEdit[2]++;
        if (scheduleTimeEdit[2] > 59) {scheduleTimeEdit[2] = 0;}
        if (scheduleTimeEdit[2] < 10) {scheduleTimeEdit[2] = "0"+ scheduleTimeEdit[2];}  // add 0 in front of minutes if needed
        document.getElementById('schMinute').innerHTML = scheduleTimeEdit[2]; // draw screen  
    }
    else if (type == 2) {  // seconds
        scheduleTimeEdit[3]++;
        if (scheduleTimeEdit[3] > 59) {scheduleTimeEdit[3] = 0;}
        if (scheduleTimeEdit[3] < 10) {scheduleTimeEdit[3] = "0"+ scheduleTimeEdit[3];}  // add 0 in front of minutes if needed
        document.getElementById('schSecond').innerHTML = scheduleTimeEdit[3]; // draw screen  
    }
}

function scheduleTimeDown(type) {
    // console.log('scheduleTimeDown   = ' + type); 
    if (type == 0) {  // hours
        scheduleTimeEdit[0]--;
        if (scheduleTimeEdit[0] < 0) {scheduleTimeEdit[0] = 23;}
        scheduleConvertHour();
        document.getElementById('schHour').innerHTML = scheduleTimeEdit[1]; // draw screen        
    }
    else if (type == 1) {  // minutes
        scheduleTimeEdit[2]--;
        if (scheduleTimeEdit[2] < 0) {scheduleTimeEdit[2] = 59;}
        if (scheduleTimeEdit[2] < 10) {scheduleTimeEdit[2] = "0"+ scheduleTimeEdit[2];}  // add 0 in front of minutes if needed
        document.getElementById('schMinute').innerHTML = scheduleTimeEdit[2]; // draw screen  
    }
    else if (type == 2) {  // seconds
        scheduleTimeEdit[3]--;
        if (scheduleTimeEdit[3] < 0) {scheduleTimeEdit[3] = 59;}
        if (scheduleTimeEdit[3] < 10) {scheduleTimeEdit[3] = "0"+ scheduleTimeEdit[3];}  // add 0 in front of minutes if needed
        document.getElementById('schSecond').innerHTML = scheduleTimeEdit[3]; // draw screen  
    }
}

function editSchedule(i) {  // 
    // console.log('editSchedule array   = ' + i); 
    // var convertedStart = secondsToTime(scheduleStartFilter[i]);
    // console.log('scheduleNameFilter[i]  = ' + scheduleNameFilter[i]); 
    // console.log('scheduleStartFilter[i] = ' + scheduleStartFilter[i]); 
    console.log("scheduleIdFilter[i] = " + scheduleIdFilter[i]);
    console.log("scheduleTypeFilter[i] = " + scheduleTypeFilter[i]);
    console.log("scheduleOffOnFilter[i] = " + scheduleOffOnFilter[i]);
    console.log("scheduleStartFilter[i] = " + scheduleStartFilter[i]);
    console.log("scheduleMonFilter[i] = " + scheduleMonFilter[i]);
    console.log("scheduleTueFilter[i] = " + scheduleTueFilter[i]);
    console.log("scheduleWedFilter[i] = " + scheduleWedFilter[i]);
    console.log("scheduleThuFilter[i] = " + scheduleThuFilter[i]);
    console.log("scheduleFriFilter[i] = " + scheduleFriFilter[i]);
    console.log("scheduleSatFilter[i] = " + scheduleSatFilter[i]);
    console.log("scheduleSunFilter[i] = " + scheduleSunFilter[i]);
    console.log("scheduleRepeatEnabledFilter[i] = " + scheduleRepeatEnabledFilter[i]);
    console.log("scheduleRepeatDaysFilter[i] = " + scheduleRepeatDaysFilter[i]);
    console.log("scheduleNameFilter[i] = " + scheduleNameFilter[i]);
    console.log("scheduleEquipmentNameFilter[i] = " + scheduleEquipmentNameFilter[i]);
    console.log("scheduleEquipmentIconIdFilter[i] = " + scheduleEquipmentIconIdFilter[i]);
    console.log("scheduleEnabledFilter[i] = " + scheduleEnabledFilter[i]);
    console.log("scheduleDoseMLFilter [i] = " + scheduleDoseMLFilter[i]);
    prevDisplayScreen = displayScreen;
    removeFooter();
    // $('.schedulesWidgetBoxContainer').empty();
    
    document.getElementById('content').innerHTML +=
        "<div class='cleanScreen' id='cleanScreen'>\
        </div>";
        scheduleDoseMLFilter

    scheduleGetHMS(scheduleStartFilter[i]);
            // <div class="schDaysContainer">\
            //     <div class="schFieldName">Start Time</div>\
            //     <input class="schField" type="time" id="schStart" step="1" value="' + convertedStart + '">\
            // </div>\
    document.getElementById('cleanScreen').innerHTML +=  // draw screen
        '<div class="schDaysContainer">\
                <div class="schTypeName">Edit Schedule for ' + scheduleTypeFilter[i] + '</div>\
            <div class="schDaysContainer">\
                <img class="menuIcon" id="schIcon" src="images/Outlets/OutLD' + scheduleEquipmentIconIdFilter[i] + 'L.png" onclick="schChangeOffOn(' + i + ')"></div></br>\
                <div class="schTypeName" style="margin:3px 0px 15px 0px; font-weight:bold; font-size:14px;">' + scheduleEquipmentNameFilter[i] + '</div>\
            </div>\
            <div class="schDaysContainer" id="schInfoBox">\
                <div class="schFieldName">Schedule Description</div>\
                <input class="schField" type="text" id="schDescription" value="' + scheduleNameFilter[i] + '" autocomplete="off">\
                <div class="fieldError" id="fieldError1"></div>\
            </div>\
        </div>\
        <div class="ruleOptionBox" style="padding-bottom:25px; padding-top:10px;">\
            <div class="parameterBox">\
                <div class="parameterInfoBox">\
                    <div class="parameterTitle">Hour</div>\
                    <div class="parameterValue" id="schHour">' + scheduleTimeEdit[1] + '</div>\
                </div>\
                <div class="parameterButtonBox">\
                    <button id="schButtonUp0" onmousedown="scheduleTimeUp(0)" class="upDownButton bi bi-arrow-up-short"></button></br>\
                    <button id="schButtonDown0" onmousedown="scheduleTimeDown(0)" class="upDownButton bi bi-arrow-down-short"></button>\
                </div>\
            </div>\
            <div class="parameterBox">\
                <div class="parameterInfoBox">\
                    <div class="parameterTitle">Minute</div>\
                    <div class="parameterValue" id="schMinute">' + scheduleTimeEdit[2] + '</div>\
                </div>\
                <div class="parameterButtonBox">\
                    <button id="schButtonUp1" onmousedown="scheduleTimeUp(1)" class="upDownButton bi bi-arrow-up-short"></button></br>\
                    <button id="schButtonDown1" onmousedown="scheduleTimeDown(1)" class="upDownButton bi bi-arrow-down-short"></button>\
                </div>\
            </div>\
            <div class="parameterBox">\
                <div class="parameterInfoBox">\
                    <div class="parameterTitle">Second</div>\
                    <div class="parameterValue" id="schSecond">' + scheduleTimeEdit[3] + '</div>\
                </div>\
                <div class="parameterButtonBox">\
                    <button id="schButtonUp2" onmousedown="scheduleTimeUp(2)" class="upDownButton bi bi-arrow-up-short"></button></br>\
                    <button id="schButtonDown2" onmousedown="scheduleTimeDown(2)" class="upDownButton bi bi-arrow-down-short"></button>\
                </div>\
            </div>\
        </div>\
        <div class="schDaysContainer">\
            <div class="schOffOnEdit bi bi-toggle2-on" id="schOffOnEdit" onclick="schChangeOffOn(' + i + ')"></div>\
            <div class="repeatContainer" id="repeatContainer">\
                <input type="checkbox" id="repeatCheck" onclick="repeatSchedule(' + i + ')">\
                <label for="repeatCheck" style="margin-right:5px;">Repeat every </label>\
                <input class="schField" type="number" id="repeatDays" value="' + scheduleRepeatDaysFilter[i] + '" onchange="validateEditSchedule(' + i + ')" onfocusout="validateEditSchedule(' + i + ')" autocomplete="off">\
                <label for="repeatCheck" style="margin-left:5px;">days </label>\
                <div class="fieldError" id="fieldError2"></div>\
            </div>\
        </div>\
        <div class="schDaysContainer" id="schDayContainter">\
            <div class="schDays">\
                <label for="schDay1">Monday </label>\
                <input type="checkbox" id="schDay1" onclick="schChangeDay(0,' + i + ')">\
            </div>\
            <div class="schDays">\
                <label for="schDay2">Tuesday </label>\
                <input type="checkbox" id="schDay2" onclick="schChangeDay(1,' + i + ')">\
            </div>\
            <div class="schDays">\
                <label for="schDay3">Wednesday</label>\
                <input type="checkbox" id="schDay3" onclick="schChangeDay(2,' + i + ')">\
            </div>\
            <div class="schDays">\
                <label for="schDay4">Thursday</label>\
                <input type="checkbox" id="schDay4" onclick="schChangeDay(3,' + i + ')">\
            </div>\
            <div class="schDays">\
                <label for="schDay5">Friday</label>\
                <input type="checkbox" id="schDay5" onclick="schChangeDay(4,' + i + ')">\
            </div>\
            <div class="schDays">\
                <label for="schDay6">Saturday</label>\
                <input type="checkbox" id="schDay6" onclick="schChangeDay(5,' + i + ')">\
            </div>\
            <div class="schDays">\
                <label for="schDay7">Sunday</label>\
                <input type="checkbox" id="schDay7" onclick="schChangeDay(6,' + i + ')">\
            </div>\
            <div class="fieldError" id="fieldError4"></div>\
        </div>\
        <div class="schDaysContainer">\
            <div class="scheduleListAlerts" id="email_' + i + '" onclick="schChangeEmail(' + i + ')"></div>\
            <div class="scheduleListAlerts" id="alert_' + i + '" onclick="schChangeAlert(' + i + ')"></div>\
        </div>\
        <br>';
        
    // console.log('prevDisplayScreen            = ' + prevDisplayScreen); 
    if (prevDisplayScreen == 10) {  // if viewing dosing settings return 
        document.getElementById('cleanScreen').innerHTML +=  // draw screen
        '<div class="widgetButtonBox">\
            <button class="button _wide" type="button" onclick="schSaveChanges(' + i + ')">Save</button>\
            <button class="button _48 back_48" type="button" onclick="dosingSettings()"></button>\
        </div>';
    }
    else {  // return back to schedules screen
        document.getElementById('cleanScreen').innerHTML +=  // draw screen
        '<div class="widgetButtonBox">\
            <button class="button _wide" type="button" onclick="schSaveChanges(' + i + ')">Save</button>\
            <button class="button _48 back_48" type="button" onclick="printSchedulesTableMenu(' + scheduleTabSelected + ')"></button>\
        </div>';
    }

    if (scheduleTypeFilter[i] == "Dosing") {  // schedule is for a dosing pump so add mL field
        document.getElementById('schInfoBox').innerHTML +=  // draw screen
            '<div class="schFieldName">Milliliters to Dose</div>\
            <input class="schField" type="number" id="schML" value="' + scheduleDoseMLFilter[i] + '" autocomplete="off">\
            <div class="fieldError" id="fieldError3"></div>';
    }
    
    if (scheduleOffOnFilter[i] == 0) {
        document.getElementById('schOffOnEdit').className = "schOffOnEdit bi bi-toggle2-off";
        document.getElementById('schOffOnEdit').innerHTML = "&nbsp Off Schedule";
        document.getElementById('schOffOnEdit').style = "color:#FF0000;";
        document.getElementById('schIcon').style.backgroundColor = "#FF0000";    
    }
    else {
        document.getElementById('schOffOnEdit').className = "schOffOnEdit bi bi-toggle2-on";
        document.getElementById('schOffOnEdit').innerHTML = "&nbsp On Schedule";
        document.getElementById('schOffOnEdit').style = "color:#00b610;";
        document.getElementById('schIcon').style.backgroundColor = "#00b610";
    }

    if (scheduleEmailFilter[i] == 1) {  // add email enabled icon
        document.getElementById('email_' + i).innerHTML =
            '<i class="bi bi-envelope-fill">&nbsp; Email Enabled</i>';
        document.getElementById('email_' + i).style.color = "#00b610";
    }
    else {
        document.getElementById('email_' + i).innerHTML =
            '<i class="bi bi-envelope-fill">&nbsp; Email Disabled</i>';
        document.getElementById('email_' + i).style.color = "#727272";
    }

    if (scheduleAlertFilter[i] == 1) {  // add dashboard alert enabled icon
        document.getElementById('alert_' + i).innerHTML =
            '<i class="bi bi-exclamation-triangle-fill">&nbsp; Dashboard Alert Enabled</i>';
        document.getElementById('alert_' + i).style.color = "#00b610";
    }
    else {
        document.getElementById('alert_' + i).innerHTML =
            '<i class="bi bi-exclamation-triangle-fill">&nbsp; Dashboard Alert Disabled</i>';
        document.getElementById('alert_' + i).style.color = "#727272";
    }

    if (scheduleRepeatEnabledFilter[i] == 1) {
        document.getElementById('repeatCheck').checked = true;
        document.getElementById('repeatDays').disabled = false;
        document.getElementById('repeatDays').style = "background-color:#FFFFFF; width:70px;";
        document.getElementById('schDayContainter').style.display = "none";  // hide days
    }
    else {
        document.getElementById('repeatCheck').checked = false;
        document.getElementById('repeatDays').disabled = true;
        document.getElementById('repeatDays').value = '';
        document.getElementById('repeatDays').style = "background-color:#dfdfdf; width:70px;";
        document.getElementById('schDayContainter').style.display = "flex";  // show days
    }

    if (scheduleMonFilter[i] == 1) {
        document.getElementById('schDay1').checked = true;
    }
    if (scheduleTueFilter[i] == 1) {
        document.getElementById('schDay2').checked = true;
    }
    if (scheduleWedFilter[i] == 1) {
        document.getElementById('schDay3').checked = true;
    }
    if (scheduleThuFilter[i] == 1) {
        document.getElementById('schDay4').checked = true;
    }
    if (scheduleFriFilter[i] == 1) {
        document.getElementById('schDay5').checked = true;
    }
    if (scheduleSatFilter[i] == 1) {
        document.getElementById('schDay6').checked = true;
    }
    if (scheduleSunFilter[i] == 1) {
        document.getElementById('schDay7').checked = true;
    }

    // track mouse up/down when changing schedule time
    var timer;  // used to track setInverval so it can be cleared
    $('#schButtonUp0').mousedown(function(){
        timer = setInterval(function() { scheduleTimeUp(0); }, 250);  // run continually while mouse down
    }).mouseup(function() {
        clearInterval(timer);
    }).mouseout(function() {
        clearInterval(timer);
    });
    $('#schButtonUp1').mousedown(function(){
        timer = setInterval(function() { scheduleTimeUp(1); }, 170);  // run continually while mouse down
    }).mouseup(function() {
        clearInterval(timer);
    }).mouseout(function() {
        clearInterval(timer);
    });
    $('#schButtonUp2').mousedown(function(){
        timer = setInterval(function() { scheduleTimeUp(2); }, 150);  // run continually while mouse down
    }).mouseup(function() {
        clearInterval(timer);
    }).mouseout(function() {
        clearInterval(timer);
    });
    $('#schButtonDown0').mousedown(function(){
        timer = setInterval(function() { scheduleTimeDown(0); }, 250);  // run continually while mouse down
    }).mouseup(function() {
        clearInterval(timer);
    }).mouseout(function() {
        clearInterval(timer);
    });
    $('#schButtonDown1').mousedown(function(){
        timer = setInterval(function() { scheduleTimeDown(1); }, 170);  // run continually while mouse down
    }).mouseup(function() {
        clearInterval(timer);
    }).mouseout(function() {
        clearInterval(timer);
    });
    $('#schButtonDown2').mousedown(function(){
        timer = setInterval(function() { scheduleTimeDown(2); }, 150);  // run continually while mouse down
    }).mouseup(function() {
        clearInterval(timer);
    }).mouseout(function() {
        clearInterval(timer);
    });
}

function confirmSchedule() {  // runs when backend sends command acknowledging the schedule was added or edited
    // console.log('confirmSchedule --------------- ');
    getSchedules();    
    closeDs18b20Popup();
}

function schSaveChanges(i) {
    // console.log('schSaveChanges = ' + i);
    scheduleConvertEditTime(i);  // convert H:M:S to timestamp

    scheduleRepeatDaysFilter[i] = document.getElementById('repeatDays').value;
    scheduleNameFilter[i] = document.getElementById('schDescription').value;
    if (scheduleRepeatDaysFilter[i] == "") {scheduleRepeatDaysFilter[i] = 0;}
    if (scheduleTypeFilter[i] === "Dosing") {scheduleDoseMLFilter[i] = document.getElementById('schML').value;}
    else {scheduleDoseMLFilter[i] = 0;}

    if (scheduleRepeatEnabledFilter[i] == 1) {
        scheduleMonFilter[i] = 0;
        scheduleTueFilter[i] = 0;
        scheduleWedFilter[i] = 0;
        scheduleThuFilter[i] = 0;
        scheduleFriFilter[i] = 0;
        scheduleSatFilter[i] = 0;
        scheduleSunFilter[i] = 0;
    }
    console.log("scheduleRepeatDaysFilter[i] = " + scheduleRepeatDaysFilter[i]);
    if (scheduleRepeatDaysFilter[i] == 1) {
        scheduleMonFilter[i] = 1;
        scheduleTueFilter[i] = 1;
        scheduleWedFilter[i] = 1;
        scheduleThuFilter[i] = 1;
        scheduleFriFilter[i] = 1;
        scheduleSatFilter[i] = 1;
        scheduleSunFilter[i] = 1;
        scheduleRepeatDaysFilter[i] = 0;
    }
    // console.log("scheduleNameFilter[i] = " + scheduleNameFilter[i]);

    // var pass = validateNewSchedule();
    // if (pass == 0) {return;}

    // if (scheduleNew[3] == 1) {  // repeat every one day was selected so we will convert to monday to sunday
    //     for (var i = 4; i < 11; i++) {
    //         scheduleNew[i] = 1;
    //     }
    //     scheduleNew[3] = 0;
    // }
    var pass = validateEditSchedule(i);
    if (pass == 0) {return;}
    

    var command = "71," + scheduleIdFilter[i] + "," + scheduleOffOnFilter[i] + "," + scheduleNameFilter[i] + "," + scheduleStartFilter[i] + "," + scheduleMonFilter[i] + "," + scheduleTueFilter[i] + "," + scheduleWedFilter[i] + "," + scheduleThuFilter[i] + "," + scheduleFriFilter[i] + "," + scheduleSatFilter[i] + "," + scheduleSunFilter[i] + "," + scheduleRepeatDaysFilter[i] + "," + scheduleEnabledFilter[i] + "," + scheduleEmailFilter[i] + "," + scheduleAlertFilter[i] + "," + scheduleDoseMLFilter[i];
    sendToBackend(command);
    // console.log('schSaveChanges------ ' + command);

    console.log("scheduleIdFilter[i] = " + scheduleIdFilter[i]);
    console.log("scheduleTypeFilter[i] = " + scheduleTypeFilter[i]);
    console.log("scheduleOffOnFilter[i] = " + scheduleOffOnFilter[i]);
    console.log("scheduleStartFilter[i] = " + scheduleStartFilter[i]);
    console.log("scheduleMonFilter[i] = " + scheduleMonFilter[i]);
    console.log("scheduleTueFilter[i] = " + scheduleTueFilter[i]);
    console.log("scheduleWedFilter[i] = " + scheduleWedFilter[i]);
    console.log("scheduleThuFilter[i] = " + scheduleThuFilter[i]);
    console.log("scheduleFriFilter[i] = " + scheduleFriFilter[i]);
    console.log("scheduleSatFilter[i] = " + scheduleSatFilter[i]);
    console.log("scheduleSunFilter[i] = " + scheduleSunFilter[i]);
    console.log("scheduleRepeatEnabledFilter[i] = " + scheduleRepeatEnabledFilter[i]);
    console.log("scheduleRepeatDaysFilter[i] = " + scheduleRepeatDaysFilter[i]);
    console.log("scheduleNameFilter[i] = " + scheduleNameFilter[i]);
    console.log("scheduleEquipmentNameFilter[i] = " + scheduleEquipmentNameFilter[i]);
    console.log("scheduleEquipmentIconIdFilter[i] = " + scheduleEquipmentIconIdFilter[i]);
    console.log("scheduleEnabledFilter[i] = " + scheduleEnabledFilter[i]);
    console.log("scheduleDoseMLFilter [i] = " + scheduleDoseMLFilter[i]);
}

function schChangeDay(day, i) {  // edit the days for existing schedule
    // console.log('schChangeDay = ' + day + ' ' + i);

    switch (day) {
        case 0:
            if (scheduleMonFilter[i] == 1) {
                document.getElementById('schDay1').checked = false;
                scheduleMonFilter[i] = 0;
            }
            else {
                document.getElementById('schDay1').checked = true;
                scheduleMonFilter[i] = 1;
            }
        break;
        case 1:
            if (scheduleTueFilter[i] == 1) {
                document.getElementById('schDay2').checked = false;
                scheduleTueFilter[i] = 0;
            }
            else {
                document.getElementById('schDay2').checked = true;
                scheduleTueFilter[i] = 1;
            }
        break;
        case 2:
            if (scheduleWedFilter[i] == 1) {
                document.getElementById('schDay3').checked = false;
                scheduleWedFilter[i] = 0;
            }
            else {
                document.getElementById('schDay3').checked = true;
                scheduleWedFilter[i] = 1;
            }
        break;
        case 3:
            if (scheduleThuFilter[i] == 1) {
                document.getElementById('schDay4').checked = false;
                scheduleThuFilter[i] = 0;
            }
            else {
                document.getElementById('schDay4').checked = true;
                scheduleThuFilter[i] = 1;
            }
        break;
        case 4:
            if (scheduleFriFilter[i] == 1) {
                document.getElementById('schDay5').checked = false;
                scheduleFriFilter[i] = 0;
            }
            else {
                document.getElementById('schDay5').checked = true;
                scheduleFriFilter[i] = 1;
            }
        break;
        case 5:
            if (scheduleSatFilter[i] == 1) {
                document.getElementById('schDay6').checked = false;
                scheduleSatFilter[i] = 0;
            }
            else {
                document.getElementById('schDay6').checked = true;
                scheduleSatFilter[i] = 1;
            }
        break;
        case 6:
            if (scheduleSunFilter[i] == 1) {
                document.getElementById('schDay7').checked = false;
                scheduleSunFilter[i] = 0;
            }
            else {
                document.getElementById('schDay7').checked = true;
                scheduleSunFilter[i] = 1;
            }
        break;
    }
    validateEditSchedule(i);
}

function schChangeEmail(i) {
    // console.log('schChangeEmail = ' + i);
    if (scheduleEmailFilter[i] == 0) {  // add email enabled icon
        document.getElementById('email_' + i).innerHTML =
            '<i class="bi bi-envelope-fill">&nbsp; Email Enabled</i>';
        document.getElementById('email_' + i).style.color = "#00b610";
        scheduleEmailFilter[i] = 1;
    }
    else {
        document.getElementById('email_' + i).innerHTML =
            '<i class="bi bi-envelope-fill">&nbsp; Email Disabled</i>';
        document.getElementById('email_' + i).style.color = "#727272";
        scheduleEmailFilter[i] = 0;
    }
}

function schChangeAlert(i) {
    // console.log('schChangeAlert = ' + i);
    if (scheduleAlertFilter[i] == 0) {  // add dashboard alert enabled icon
        document.getElementById('alert_' + i).innerHTML =
            '<i class="bi bi-exclamation-triangle-fill">&nbsp; Dashboard Alert Enabled</i>';
        document.getElementById('alert_' + i).style.color = "#00b610";
        scheduleAlertFilter[i] = 1;
    }
    else {
        document.getElementById('alert_' + i).innerHTML =
            '<i class="bi bi-exclamation-triangle-fill">&nbsp; Dashboard Alert Disabled</i>';
        document.getElementById('alert_' + i).style.color = "#727272";
        scheduleAlertFilter[i] = 0;
    }
}

function schChangeOffOn(i) {  // edit schedule on/off
    console.log('schChangeOffOn = ' + i);
    if (scheduleTypeFilter[i] != "Dosing")  // don't update if we are creating dosing schedule
    {
        if (scheduleOffOnFilter[i] == 1) {
            document.getElementById('schOffOnEdit').className = "schOffOnEdit bi bi-toggle2-off";
            document.getElementById('schOffOnEdit').innerHTML = "&nbsp Off Schedule";
            document.getElementById('schOffOnEdit').style = "color:#FF0000;";
            document.getElementById('schIcon').style.backgroundColor = "#FF0000";
            scheduleOffOnFilter[i] = 0;
        }
        else {
            document.getElementById('schOffOnEdit').className = "schOffOnEdit bi bi-toggle2-on";
            document.getElementById('schOffOnEdit').innerHTML = "&nbsp On Schedule";
            document.getElementById('schOffOnEdit').style = "color:#00b610;";
            document.getElementById('schIcon').style.backgroundColor = "#00b610";
            scheduleOffOnFilter[i] = 1;
        }
    }
    console.log("scheduleTypeFilter[i] = " + scheduleTypeFilter[i]);
}

function repeatSchedule(i) {  // edit schedule repeat feature
    // console.log('repeatSchedule array = ' + i); 
    // console.log('repeatSchedule------ ' + scheduleIdFilter[i] + ' - ' + scheduleRepeatDaysFilter[i]);

    if (scheduleRepeatEnabledFilter[i] == 0) {  // repeat days not being used
        document.getElementById('repeatCheck').checked = true;
        document.getElementById('repeatDays').disabled = false;
        document.getElementById('repeatDays').style = "background-color:#FFFFFF; width:70px;";
        document.getElementById('repeatDays').focus();
        document.getElementById('repeatDays').select();
        document.getElementById('schDayContainter').style.display = "none";  // hide days
        scheduleRepeatEnabledFilter[i] = 1;
    }
    else {
        document.getElementById('repeatCheck').checked = false;
        document.getElementById('repeatDays').disabled = true;
        document.getElementById('repeatDays').value = '';
        document.getElementById('repeatDays').style = "background-color:#dfdfdf; width:70px;";
        document.getElementById('schDayContainter').style.display = "flex";  // show days
        scheduleRepeatEnabledFilter[i] = 0;
    }
}

function schSaveNew() {
    // console.log('schSaveNew -------------');
    scheduleConvertNewTime();  // convert H:M:S to timestamp

    scheduleNew[3] = document.getElementById('repeatDays').value;
    scheduleNew[14] = document.getElementById('schDescription').value;
    if (scheduleNew[0] == 3) {scheduleNew[16] = document.getElementById('schML').value;}

    // if (scheduleNew[3] == "") {scheduleNew[3] = 0;}

    // console.log('scheduleNew[3]        = ' + scheduleNew[3]);

    var pass = validateNewSchedule();
    if (pass == 0) {return;}

    // console.log('scheduleNew[3]        = ' + scheduleNew[3]);
    // scheduleNew[0] = 0;  // schedule type
    // scheduleNew[1] = 0;  // off or on
    // scheduleNew[2] = 0;  // repeat enabled - only used on UI, don't need to send to backend
    // scheduleNew[3] = 0;  // repeat number of days
    // scheduleNew[4] = 0;  // Monday
    // scheduleNew[5] = 0;  // Tuesday
    // scheduleNew[6] = 0;  // Wednesday
    // scheduleNew[7] = 0;  // Thursday
    // scheduleNew[8] = 0;  // Friday
    // scheduleNew[9] = 0;  // Saturday
    // scheduleNew[10] = 0;  // Sunday
    // scheduleNew[11] = 0;  // email
    // scheduleNew[12] = 0;  // dashboard alert
    // scheduleNew[13] = 0;  // converted schedule time
    // scheduleNew[14] = "";  // custom name
    // scheduleNew[15] = 0;  // pinID
    // scheduleNew[16] = 0;  // milliliters for dosing schedule


    if (scheduleNew[3] == 1) {  // repeat every one day was selected so we will convert to monday to sunday
        for (var i = 4; i < 11; i++) {
            scheduleNew[i] = 1;
        }
        scheduleNew[3] = 0;
    }

    for (var i = 0; i < scheduleNew.length; i++) {
        console.log('scheduleNew[' + i + ']        = ' + scheduleNew[i]);
    }

    var command = "72," + scheduleNew[0] + "," + scheduleNew[1] + "," + scheduleNew[14] + "," + scheduleNew[13] + "," + scheduleNew[4] + "," + scheduleNew[5] + "," + scheduleNew[6] + "," + scheduleNew[7] + "," + scheduleNew[8] + "," + scheduleNew[9] + "," + scheduleNew[10] + "," + scheduleNew[3] + "," + scheduleNew[15] + "," + scheduleNew[11] + "," + scheduleNew[12] + "," + scheduleNew[16];
    sendToBackend(command);
}

function schNewChangeDay(day) {  // edit the days for new schedule
    // console.log('schNewChangeDay = ' + day);

    switch (day) {
        case 0:  // Monday
            if (scheduleNew[4] == 1) {
                document.getElementById('schDay1').checked = false;
                scheduleNew[4] = 0;
            }
            else {
                document.getElementById('schDay1').checked = true;
                scheduleNew[4] = 1;
            }
        break;
        case 1:
            if (scheduleNew[5] == 1) {
                document.getElementById('schDay2').checked = false;
                scheduleNew[5] = 0;
            }
            else {
                document.getElementById('schDay2').checked = true;
                scheduleNew[5] = 1;
            }
        break;
        case 2:
            if (scheduleNew[6] == 1) {
                document.getElementById('schDay3').checked = false;
                scheduleNew[6] = 0;
            }
            else {
                document.getElementById('schDay3').checked = true;
                scheduleNew[6] = 1;
            }
        break;
        case 3:
            if (scheduleNew[7] == 1) {
                document.getElementById('schDay4').checked = false;
                scheduleNew[7] = 0;
            }
            else {
                document.getElementById('schDay4').checked = true;
                scheduleNew[7] = 1;
            }
        break;
        case 4:
            if (scheduleNew[8] == 1) {
                document.getElementById('schDay5').checked = false;
                scheduleNew[8] = 0;
            }
            else {
                document.getElementById('schDay5').checked = true;
                scheduleNew[8] = 1;
            }
        break;
        case 5:
            if (scheduleNew[9] == 1) {
                document.getElementById('schDay6').checked = false;
                scheduleNew[9] = 0;
            }
            else {
                document.getElementById('schDay6').checked = true;
                scheduleNew[9] = 1;
            }
        break;
        case 6:
            if (scheduleNew[10] == 1) {
                document.getElementById('schDay7').checked = false;
                scheduleNew[10] = 0;
            }
            else {
                document.getElementById('schDay7').checked = true;
                scheduleNew[10] = 1;
            }
        break;
    }
    validateNewSchedule();
}

function schNewOffOn() {  // change on/off for new schedule being created
    // console.log('schNewOffOn ---------------- ');
    if (scheduleNew[0] != 3) {  // don't update if we are creating dosing schedule
        if (scheduleNew[1] == 1) {
            document.getElementById('schOffOnEdit').className = "schOffOnEdit bi bi-toggle2-off";
            document.getElementById('schOffOnEdit').innerHTML = "&nbsp Off Schedule";
            document.getElementById('schOffOnEdit').style = "color:#FF0000;";
            document.getElementById('schIcon').style.backgroundColor = "#FF0000";
            scheduleNew[1] = 0;
        }
        else {
            document.getElementById('schOffOnEdit').className = "schOffOnEdit bi bi-toggle2-on";
            document.getElementById('schOffOnEdit').innerHTML = "&nbsp On Schedule";
            document.getElementById('schOffOnEdit').style = "color:#00b610;";
            document.getElementById('schIcon').style.backgroundColor = "#00b610";
            scheduleNew[1] = 1;
        }
    }
    // console.log("scheduleNew[i] = " + scheduleNew[1]);
}

function schNewEmail() {
    // console.log('schNewEmail = ');
    if (scheduleNew[11] == 0) {  // add email enabled icon
        document.getElementById('email').innerHTML =
            '<i class="bi bi-envelope-fill">&nbsp; Email Enabled</i>';
        document.getElementById('email').style.color = "#00b610";
        scheduleNew[11] = 1;
    }
    else {
        document.getElementById('email').innerHTML =
            '<i class="bi bi-envelope-fill">&nbsp; Email Disabled</i>';
        document.getElementById('email').style.color = "#727272";
        scheduleNew[11] = 0;
    }
}

function schNewAlert() {
    // console.log('schNewAlert = ');
    if (scheduleNew[12] == 0) {  // add dashboard alert enabled icon
        document.getElementById('alert').innerHTML =
            '<i class="bi bi-exclamation-triangle-fill">&nbsp; Dashboard Alert Enabled</i>';
        document.getElementById('alert').style.color = "#00b610";
        scheduleNew[12] = 1;
    }
    else {
        document.getElementById('alert').innerHTML =
            '<i class="bi bi-exclamation-triangle-fill">&nbsp; Dashboard Alert Disabled</i>';
        document.getElementById('alert').style.color = "#727272";
        scheduleNew[12] = 0;
    }
}

function repeatNewSchedule() {  // new schedule repeat feature
    // console.log('repeatNewSchedule ---------------'); 

    if (scheduleNew[2] == 0) {  // repeat days not being used
        document.getElementById('repeatCheck').checked = true;
        document.getElementById('repeatDays').disabled = false;
        document.getElementById('repeatDays').style = "background-color:#FFFFFF; width:70px;";
        document.getElementById('repeatDays').focus();
        document.getElementById('repeatDays').select();
        document.getElementById('schDayContainter').style.display = "none";  // hide days
        scheduleNew[2] = 1;
    }
    else {
        document.getElementById('repeatCheck').checked = false;
        document.getElementById('repeatDays').disabled = true;
        document.getElementById('repeatDays').value = '';
        document.getElementById('repeatDays').style = "background-color:#dfdfdf; width:70px;";
        document.getElementById('schDayContainter').style.display = "flex";  // show days
        scheduleNew[2] = 0;
        scheduleNew[3] = 0;
        for (var i = 4; i < 11; i++) {  // set each day back to off
            scheduleNew[i] = 1;  // enable each day
            schNewChangeDay(i-4);  // now change new day so we update variables and set each day disabled
        }
    }
    
    // console.log('repeatNewSchedule------ ' + scheduleNew[2]);
}

function schedulesSort(sortID) {  // sort outlets
    // console.log('schedulesSort = ' + sortID);    

    // get field name in table for sorting
    if (sortID == 1) {scheduleSortType = "type"}
    else if (sortID == 2) {scheduleSortType = "schName"}
    else if (sortID == 3) {scheduleSortType = "start"}
    else if (sortID == 4) {scheduleSortType = "offOn"}
    else if (sortID == 5) {scheduleSortType = "mon"}
    else if (sortID == 6) {scheduleSortType = "tue"}
    else if (sortID == 7) {scheduleSortType = "wed"}
    else if (sortID == 8) {scheduleSortType = "thu"}
    else if (sortID == 9) {scheduleSortType = "fri"}
    else if (sortID == 10) {scheduleSortType = "sat"}
    else if (sortID == 11) {scheduleSortType = "sun"}
    else if (sortID == 12) {scheduleSortType = "enabled"}
    else if (sortID == 13) {scheduleSortType = "schEmail"}
    else if (sortID == 14) {scheduleSortType = "schAlert"}
    else if (sortID == 15) {scheduleSortType = "name"}  // equipment name
    else if (sortID == 16) {scheduleSortType = "mL"}
    // console.log(scheduleSortType);

    scheduleSortID = sortID;  // track current sort type
    if (scheduleSortOrder == 0) {scheduleSortOrder = 1;}
    else {scheduleSortOrder = 0;}

    $.ajax({  // save sort type to database
        method: "POST",
        url: "/php/schedulesSortSave.php",
        dataType: "text/plain",
        data: {
            "v": sortID,
            "o": scheduleSortOrder,
        }
    });

    $.ajax({  // update all outlet variables so we have newly sorted data to work with
        method: "GET",
        url: "/php/schedulesSort.php",
        cache: false,
        dataType: "json",
        // dataType: "text/plain",
        data: {
            "s": scheduleSortType,
            "a": scheduleSortOrder
        },
        success: function(dataWindow) {
            
            scheduleID = [];
            scheduleTypeID = [];  // track what the schedule is for
            schedulePinID = [];  // track pinID from pinsAvailable table
            scheduleStart = [];  // track schedule start time in seconds
            scheduleMon = [];  // track if mon enabled
            scheduleTue = [];  // track if tue enabled
            scheduleWed = [];  // track if wed enabled
            scheduleThu = [];  // track if thu enabled
            scheduleFri = [];  // track if fri enabled
            scheduleSat = [];  // track if sat enabled
            scheduleSun = [];  // track if sun enabled
            scheduleRepeatEnabled = [];  // track if schedule repeats X number of days
            scheduleRepeatDays = [];  // track if schedule is repeated and how often in days
            scheduleOffOn = [];  // track if schedule is on or off
            scheduleName = [];  // track schedule custom name
            scheduleEquipmentName = [];  // track name of equipment for schedule
            scheduleEquipmentIconID = [];  // track iconID of equipment for schedule
            scheduleEnabled = [];  // track if scheduled is enabled
            scheduleEmail = [];  // track if email alert is enabled
            scheduleAlert = [];  // track if dashboard alert is enabled
            scheduleType = [];  // track schedule type name
            scheduleDoseML = [];  // track how many milliliters are attached to schedule
            
            for (var i = 0; i < dataWindow.length; i++){
                // place list of outlets in individual variables
                scheduleID.push(dataWindow[i].scheduleID);
                scheduleTypeID.push(dataWindow[i].scheduleTypeID);
                schedulePinID.push(dataWindow[i].pinID);
                scheduleOffOn.push(dataWindow[i].offOn);
                scheduleName.push(dataWindow[i].schName);
                scheduleStart.push(dataWindow[i].start);
                scheduleMon.push(dataWindow[i].mon);
                scheduleTue.push(dataWindow[i].tue);
                scheduleWed.push(dataWindow[i].wed);
                scheduleThu.push(dataWindow[i].thu);
                scheduleFri.push(dataWindow[i].fri);
                scheduleSat.push(dataWindow[i].sat);
                scheduleSun.push(dataWindow[i].sun);
                scheduleRepeatDays.push(dataWindow[i].repeatDays);
                scheduleEquipmentName.push(dataWindow[i].name);
                scheduleEquipmentIconID.push(dataWindow[i].iconID);
                scheduleEnabled.push(dataWindow[i].enabled);
                scheduleEmail.push(dataWindow[i].schEmail);
                scheduleAlert.push(dataWindow[i].schAlert);
                scheduleType.push(dataWindow[i].type);
                scheduleDoseML.push(dataWindow[i].mL);
                if (scheduleRepeatDays[i] == 0) {scheduleRepeatEnabled[i] = 0;}
                else {scheduleRepeatEnabled[i] = 1;}
            }
            
            // console.log('displayScreen = ' + displayScreen);
            if (displayScreen == 10) {dosingSettingsDetails(viewingDosingPumpScheduleID);}
            else {printSchedulesTable(scheduleTabSelected);}
        },
        error: function(dataWindow) {
            console.log(dataWindow);
            console.log('error - schedulesSort');
        }
    });    
}

function scheduleNextPage(typeID) {
    schedulePageNumber++;
    if (displayScreen == 10) {dosingSettingsDetails(viewingDosingPumpScheduleID);}
    else if (displayScreen == 11) {versionLog();}
    else {printSchedulesTable(typeID);}
    // printSchedulesTable(typeID);
}

function schedulePreviousPage(typeID) {
    schedulePageNumber--;
    if (displayScreen == 10) {dosingSettingsDetails(viewingDosingPumpScheduleID);}
    else if (displayScreen == 11) {versionLog();}
    else {printSchedulesTable(typeID);}
    // printSchedulesTable(typeID);
}

function expandSchedule(schID) {  // expand schedule 
    // show or hide schedule details
    // console.log('expandSchedule------');
    // console.log('schID                = ' + schID);
    // console.log('schedulePrevExpanded = ' + schedulePrevExpanded);
    var f = 0;  // flag to block collapse - if schedulePrevExpanded = schID OR if schedulePrevExpanded = null
    if (schedulePrevExpanded == null) {schedulePrevExpanded = schID; f = 1;}  // populate schedulePrevExpanded with current if null and flag so we don't collapse

    if (document.getElementById('schedulesContent_' + schID).style.display != "contents") {  // expand current selection if it isn't open
        f = 1;  // set flag so we don't collapse current data in last step
        document.getElementById('schedulesList_' + schID).style.backgroundColor = "#00b610";  // change row background to green
        // document.getElementById('schedulesList_' + schID).style.border = "solid 1px rgb(165, 165, 165)";  // change row background to green
        document.getElementById('schedulesList_' + schID).style.color = "rgb(255, 255, 255)";  // change text color to white
        document.getElementById('schedulesContent_' + schID).style.display = "contents";  // expand content
        document.getElementById('schedulesContent_' + schID).style.marginBottom = "15px";  // add top margin
        document.getElementById('scheduleExpandIcon_' + schID).innerHTML =  // change collapse icon
            '<div class="schedulesListIcon bi bi-arrow-90deg-up" style="margin-left:8px; font-size:11px; color:#FFFFFF"></div>'
        printScheduleDetails(schID);
        if (schedulePrevExpanded == schID) {f = 2;}  // if schedulePrevExpanded = schID set flag so we don't collapse in last step, this means the same row is being toggled twice or more
    }

    if ((f != 2) && (document.getElementById('schedulesContent_' + schedulePrevExpanded).style.display == "contents")) {  // collapse previous content if open
        document.getElementById('schedulesList_' + schedulePrevExpanded).style.backgroundColor = "rgb(255, 255, 255)";  // change row background to white
        document.getElementById('schedulesList_' + schedulePrevExpanded).style.color = "rgb(0, 0, 0)";  // change text color to black
        document.getElementById('schedulesContent_' + schedulePrevExpanded).style.marginBottom = "0px";  // remove bottom margin
        document.getElementById('schedulesContent_' + schedulePrevExpanded).style.display = "none";  // collapse content
        document.getElementById('scheduleExpandIcon_' + schedulePrevExpanded).innerHTML =  // change collapse icon
            '<div class="schedulesListIcon bi bi-arrow-90deg-down" style="margin-left:8px; font-size:11px"></div>'
    }
    
    if ((schedulePrevExpanded == schID) && (f == 0)) {  // collapse current selection if another row is toggled while open
        document.getElementById('schedulesList_' + schID).style.backgroundColor = "rgb(255, 255, 255)";  // change row background to white
        document.getElementById('schedulesList_' + schID).style.color = "rgb(0, 0, 0)";  // change text color to black
        document.getElementById('schedulesContent_' + schID).style.marginBottom = "0px";  // remove bottom margin
        document.getElementById('schedulesContent_' + schID).style.display = "none";  // collapse content
        document.getElementById('scheduleExpandIcon_' + schID).innerHTML =  // change collapse icon
            '<div class="schedulesListIcon bi bi-arrow-90deg-down" style="margin-left:8px; font-size:11px"></div>'
    }
    schedulePrevExpanded = schID;  // track last row expanded
    // console.log('');
}

function printScheduleDetails(schID)  // print schedule details
{
    // console.log('printScheduleDetails------');
    // console.log(schID);
    var arrayID = 0;
    for (var i=0; i < scheduleIdFilter.length; i++)  // close schedule details being viewed for new for selection
    {
        if (scheduleIdFilter[i] == schID) {arrayID = i;}
    }

    var convertedStart = secondsToTime(scheduleStartFilter[arrayID]);
    var start = tConvert(convertedStart);  // convert schedule start time to 12hr format if necessary

    var offon = "";
    if (scheduleOffOnFilter[arrayID] == 1) {offon = "On";}
    else {offon = "Off";}

    var days = [];
    var c = 0;
    if (scheduleMonFilter[arrayID] == 1) {days[c] = "Monday"; c++;}
    if (scheduleTueFilter[arrayID] == 1) {days[c] = "Tuesday"; c++;}
    if (scheduleWedFilter[arrayID] == 1) {days[c] = "Wednesday"; c++;}
    if (scheduleThuFilter[arrayID] == 1) {days[c] = "Thursday"; c++;}
    if (scheduleFriFilter[arrayID] == 1) {days[c] = "Friday"; c++;}
    if (scheduleSatFilter[arrayID] == 1) {days[c] = "Saturday"; c++;}
    if (scheduleSunFilter[arrayID] == 1) {days[c] = "Sunday"; c++;}

    document.getElementById("schedulesContent_" + schID).innerHTML =  // 
        '<td colspan="19">\
            <div class="schedulesDetailsContainter" id="schedulesDetailsContainter_' + schID + '">\
                <div class="scheduleDetailsIcon expand" id="scheduleDetailsIcon_' + schID + '"></div>\
                <div class="scheduleDetails" id="scheduleDetails_' + schID + '">\
                </div>\
            </div>\
        </td>';

    // console.log('scheduleOffOnFilter[arrayID]  = ' + scheduleOffOnFilter[arrayID]); 

    if (scheduleOffOnFilter[arrayID] == 1) {  // if schedule is ON make icon green
        if (scheduleTypeFilter[arrayID] == "Custom Rule") {
            document.getElementById("scheduleDetailsIcon_" + schID).innerHTML = '<img class="menuIcon" id="schIcon' + schID + '" src="images/icons/rules3.png"></div>';
        }
        else if (scheduleTypeFilter[arrayID] == "Custom Mode") {
            document.getElementById("scheduleDetailsIcon_" + schID).innerHTML = '<img class="menuIcon" id="schIcon' + schID + '" src="images/icons/modes2.png"></div>';
        }
        else {
            document.getElementById("scheduleDetailsIcon_" + schID).innerHTML = '<img class="menuIcon" id="schIcon' + schID + '" src="images/Outlets/OutLD' + scheduleEquipmentIconIdFilter[arrayID] + 'L.png"></div>';
        }
        document.getElementById('schIcon' + schID).style.backgroundColor = "#00b610";
    }
    else {  // if schedule is OFF make icon green
        if (scheduleTypeFilter[arrayID] == "Custom Rule") {
            document.getElementById("scheduleDetailsIcon_" + schID).innerHTML = '<img class="menuIcon" id="schIcon' + schID + '" src="images/icons/rules3.png"></div>';
            document.getElementById('schIcon' + schID).style.backgroundColor = "#00b610";
        }
        else if (scheduleTypeFilter[arrayID] == "Custom Mode") {
            document.getElementById("scheduleDetailsIcon_" + schID).innerHTML = '<img class="menuIcon" id="schIcon' + schID + '" src="images/icons/modes2.png"></div>';
            document.getElementById('schIcon' + schID).style.backgroundColor = "#00b610";
        }
        else {
            document.getElementById("scheduleDetailsIcon_" + schID).innerHTML = '<img class="menuIcon" id="schIcon' + schID + '" src="images/Outlets/OutLD' + scheduleEquipmentIconIdFilter[arrayID] + 'L.png"></div>';
            document.getElementById('schIcon' + schID).style.backgroundColor = "#FF0000";
        }
    }

    document.getElementById("scheduleDetails_" + schID).innerHTML = 
        scheduleEquipmentNameFilter[arrayID] + ' will turn <span style="font-weight:bold";>' + offon + '</span> at ' + start + ' on ';
    
    for (var i=0; i < c; i++)  // close schedule details being viewed for new for selection
    {
        if (i == c-1) {document.getElementById("scheduleDetails_" + schID).innerHTML += " and ";}
        document.getElementById("scheduleDetails_" + schID).innerHTML += days[i] + "'s";
        if (i < c-2) {document.getElementById("scheduleDetails_" + schID).innerHTML += ", ";}
    }

    document.getElementById('schedulesDetailsContainter_' + schID).style.padding = "15px";  // add top margin
    // document.getElementById('schedulesDetailsContainter_' + schID).style.paddingBottom = "15px";  // add top margin
    document.getElementById('schedulesDetailsContainter_' + schID).style.borderBottom = "solid 1px #d3d3d3";  // add top margin
    document.getElementById('schedulesDetailsContainter_' + schID).style.backgroundColor = "#d3d3d3";  // add top margin
    document.getElementById("scheduleDetails_" + schID).innerHTML += "<p><b>Next Schedule:</b></p>";
}

function enableSchedule(arrayLoc) {  // save enable/disable option to database and update icon
    if (enableButtonPressed == 0) {  // add 1000ms delay after button is pressed
        if (scheduleEnabledFilter[arrayLoc] == 0) {  // enable
            // scheduleEnabled[arrayLoc] = 1;
            scheduleEnabledFilter[arrayLoc] = 1;
            
            for (var i=0; i < scheduleID.length; i++)  // close schedule details being viewed for new for selection
            {
                if (scheduleID[i] == scheduleIdFilter[arrayLoc]) {scheduleEnabled[i] = 1; break;}
            }
            document.getElementById('scheduleEnable_' + scheduleIdFilter[arrayLoc]).innerHTML = 
                '<div id="scheduleEnableButton_' + scheduleIdFilter[arrayLoc] + '" onclick="enableSchedule(' + arrayLoc + ')" class="schedulesListIcon bi bi-toggle-on" style="font-size:20px; color:#00b610;"></div>'
                
            document.getElementById('schedType_' + scheduleIdFilter[arrayLoc]).style = 'color:rgb(0, 0, 0)';
            document.getElementById('schedEquip_' + scheduleIdFilter[arrayLoc]).style = 'color:rgb(0, 0, 0)';
            document.getElementById('schedDescr_' + scheduleIdFilter[arrayLoc]).style = 'color:rgb(0, 0, 0)';
            document.getElementById('schedTime_' + scheduleIdFilter[arrayLoc]).style = 'color:rgb(0, 0, 0)';
            document.getElementById('doseML_' + scheduleIdFilter[arrayLoc]).style = 'color:rgb(0, 0, 0)';

            if (scheduleRepeatDaysFilter[arrayLoc] > 0) {  // if schedule repeats X number of days
                document.getElementById('scheduleDays_' + scheduleIdFilter[arrayLoc]).innerHTML = 'Repeat every ' + scheduleRepeatDaysFilter[arrayLoc] + ' days';
                document.getElementById('scheduleDays_' + scheduleIdFilter[arrayLoc]).style = 'text-align:left; padding-left:12px; color:rgb(0, 0, 0);';
            }

            if (scheduleOffOnFilter[arrayLoc] == 0) {  // if schedule turns something off
                document.getElementById('scheduleOffOn_' + scheduleIdFilter[arrayLoc]).style = 'color:rgb(255, 0, 0, 0.75)';
            }
            else {
                document.getElementById('scheduleOffOn_' + scheduleIdFilter[arrayLoc]).style = 'color:#00b610';
            }

            if (scheduleEmailFilter[arrayLoc] == 1) {  // if email is disabled during maintenance
                document.getElementById('scheduleEmails_' + scheduleIdFilter[arrayLoc]).style = 'color:#00b610';
            }
            else {  // if email is enabled during maintenance
                document.getElementById('scheduleEmails_' + scheduleIdFilter[arrayLoc]).style = 'color:rgb(200, 200, 200)';
            }

            if (scheduleAlertFilter[arrayLoc] == 1) {  // if dashboard alert is disabled during maintenance
                document.getElementById('scheduleAlerts_' + scheduleIdFilter[arrayLoc]).style = 'color:#00b610';
            }
            else {  // if dashboard alert is enabled during maintenance
                document.getElementById('scheduleAlerts_' + scheduleIdFilter[arrayLoc]).style = 'color:rgb(200, 200, 200)';
            }
            
            if (scheduleMonFilter[arrayLoc] == 1) {
                document.getElementById('scheduleMon_' + scheduleIdFilter[arrayLoc]).style.color = 'rgb(0, 0, 0)';  // 
                document.getElementById('scheduleMon_' + scheduleIdFilter[arrayLoc]).title = 'Scheduled';  // 
            }
            if (scheduleTueFilter[arrayLoc] == 1) {
                document.getElementById('scheduleTue_' + scheduleIdFilter[arrayLoc]).style.color = 'rgb(0, 0, 0)';  // 
                document.getElementById('scheduleTue_' + scheduleIdFilter[arrayLoc]).title = 'Scheduled';  // 
            }
            if (scheduleWedFilter[arrayLoc] == 1) {
                document.getElementById('scheduleWed_' + scheduleIdFilter[arrayLoc]).style.color = 'rgb(0, 0, 0)';  // 
                document.getElementById('scheduleWed_' + scheduleIdFilter[arrayLoc]).title = 'Scheduled';  // 
            }
            if (scheduleThuFilter[arrayLoc] == 1) {
                document.getElementById('scheduleThu_' + scheduleIdFilter[arrayLoc]).style.color = 'rgb(0, 0, 0)';  // 
                document.getElementById('scheduleThu_' + scheduleIdFilter[arrayLoc]).title = 'Scheduled';  // 
            }
            if (scheduleFriFilter[arrayLoc] == 1) {
                document.getElementById('scheduleFri_' + scheduleIdFilter[arrayLoc]).style.color = 'rgb(0, 0, 0)';  // 
                document.getElementById('scheduleFri_' + scheduleIdFilter[arrayLoc]).title = 'Scheduled';  // 
            }
            if (scheduleSatFilter[arrayLoc] == 1) {
                document.getElementById('scheduleSat_' + scheduleIdFilter[arrayLoc]).style.color = 'rgb(0, 0, 0)';  // 
                document.getElementById('scheduleSat_' + scheduleIdFilter[arrayLoc]).title = 'Scheduled';  // 
            }
            if (scheduleSunFilter[arrayLoc] == 1) {
                document.getElementById('scheduleSun_' + scheduleIdFilter[arrayLoc]).style.color = 'rgb(0, 0, 0)';  // 
                document.getElementById('scheduleSun_' + scheduleIdFilter[arrayLoc]).title = 'Scheduled';  // 
            }
        }
        else {  // disable
            // scheduleEnabled[arrayLoc] = 0;
            scheduleEnabledFilter[arrayLoc] = 0;
            console.log('scheduleID.length          = ' + scheduleID.length);
            console.log('scheduleIdFilter[arrayLoc] = ' + scheduleIdFilter[arrayLoc]);
            for (var i=0; i < scheduleID.length; i++)  // close schedule details being viewed for new for selection
            {
                console.log('scheduleID[i]              = ' + scheduleID[i]);
                if (scheduleID[i] == scheduleIdFilter[arrayLoc]) {scheduleEnabled[i] = 0; break;}
            }
            document.getElementById('scheduleEnable_' + scheduleIdFilter[arrayLoc]).innerHTML = 
                '<div id="scheduleEnableButton_' + scheduleIdFilter[arrayLoc] + '" onclick="enableSchedule(' + arrayLoc + ')" class="schedulesListIcon bi bi-toggle-off" style="font-size:20px; color:rgb(50, 50, 50);"></div>'
                
            document.getElementById('scheduleOffOn_' + scheduleIdFilter[arrayLoc]).style = 'color:rgb(230, 230, 230)';
            document.getElementById('scheduleEmails_' + scheduleIdFilter[arrayLoc]).style = 'color:rgb(230, 230, 230)';
            document.getElementById('scheduleAlerts_' + scheduleIdFilter[arrayLoc]).style = 'color:rgb(230, 230, 230)';

            document.getElementById('schedType_' + scheduleIdFilter[arrayLoc]).style = 'color:rgb(200, 200, 200)';
            document.getElementById('schedEquip_' + scheduleIdFilter[arrayLoc]).style = 'color:rgb(200, 200, 200)';
            document.getElementById('schedDescr_' + scheduleIdFilter[arrayLoc]).style = 'color:rgb(200, 200, 200)';
            document.getElementById('schedTime_' + scheduleIdFilter[arrayLoc]).style = 'color:rgb(200, 200, 200)';
            document.getElementById('doseML_' + scheduleIdFilter[arrayLoc]).style = 'color:rgb(200, 200, 200)';
            
            if (scheduleRepeatDaysFilter[arrayLoc] > 0) {  // if schedule repeats X number of days
                document.getElementById('scheduleDays_' + scheduleIdFilter[arrayLoc]).innerHTML = 'Repeat every ' + scheduleRepeatDaysFilter[arrayLoc] + ' days';
                document.getElementById('scheduleDays_' + scheduleIdFilter[arrayLoc]).style = 'text-align:left; padding-left:12px; color:rgb(200, 200, 200);';
            }

            if (scheduleMonFilter[arrayLoc] == 1) {
                document.getElementById('scheduleMon_' + scheduleIdFilter[arrayLoc]).style.color = 'rgb(200, 200, 200)';  // 
                document.getElementById('scheduleMon_' + scheduleIdFilter[arrayLoc]).title = 'Disabled';  // 
            }
            if (scheduleTueFilter[arrayLoc] == 1) {
                document.getElementById('scheduleTue_' + scheduleIdFilter[arrayLoc]).style.color = 'rgb(200, 200, 200)';  // 
                document.getElementById('scheduleTue_' + scheduleIdFilter[arrayLoc]).title = 'Disabled';  // 
            }
            if (scheduleWedFilter[arrayLoc] == 1) {
                document.getElementById('scheduleWed_' + scheduleIdFilter[arrayLoc]).style.color = 'rgb(200, 200, 200)';  // 
                document.getElementById('scheduleWed_' + scheduleIdFilter[arrayLoc]).title = 'Disabled';  // 
            }
            if (scheduleThuFilter[arrayLoc] == 1) {
                document.getElementById('scheduleThu_' + scheduleIdFilter[arrayLoc]).style.color = 'rgb(200, 200, 200)';  // 
                document.getElementById('scheduleThu_' + scheduleIdFilter[arrayLoc]).title = 'Disabled';  // 
            }
            if (scheduleFriFilter[arrayLoc] == 1) {
                document.getElementById('scheduleFri_' + scheduleIdFilter[arrayLoc]).style.color = 'rgb(200, 200, 200)';  // 
                document.getElementById('scheduleFri_' + scheduleIdFilter[arrayLoc]).title = 'Disabled';  // 
            }
            if (scheduleSatFilter[arrayLoc] == 1) {
                document.getElementById('scheduleSat_' + scheduleIdFilter[arrayLoc]).style.color = 'rgb(200, 200, 200)';  // 
                document.getElementById('scheduleSat_' + scheduleIdFilter[arrayLoc]).title = 'Disabled';  // 
            }
            if (scheduleSunFilter[arrayLoc] == 1) {
                document.getElementById('scheduleSun_' + scheduleIdFilter[arrayLoc]).style.color = 'rgb(200, 200, 200)';  // 
                document.getElementById('scheduleSun_' + scheduleIdFilter[arrayLoc]).title = 'Disabled';  // 
            }
        }

        var command = "73," + scheduleIdFilter[arrayLoc] + "," + scheduleEnabledFilter[arrayLoc];
        sendToBackend(command);
        // console.log('enableSchedule------ ' + command);
        enableButtonPressed = 1;
        setTimeout(function(){enableButtonPressed = 0;},100);  // button can't be pressed again for 1000 millis
    }
    if (displayScreen == 10) {  // remove Type column from schedule table if viewing on dosing settings page
        document.getElementById('typeSort').style = 'display:none';
        document.getElementById('schedEquip_' + scheduleIdFilter[arrayLoc]).style = 'display:none';
        var elements = document.getElementsByClassName('typeSort'); // get all temp elements
        for(var i = 0; i < elements.length; i++){
            elements[i].style.display = "none";
        }
    }
}
