
var startupWizardRunning = 0;
var startupRunning = 0;
var stopSendingData = 0;
var sendingDataSpeed = 1000;
var currentMillisTime = 0;
var currentTime;

var windowWidth = 0;
var windowHeight = 0;
var health = 0;

var dashboardDataID = 0;
var menuScrollMarginLeft = 0;

var autoUpdate = 0;
var updateAvailable = 0;
var updateReady = 0;
var appVersion = 0;
var latestVersion = 0;
var supportID = 0;
var controllerVersion = 0;

var updatingUI = 0;
var validationFail = 0;

var displayScreen = 0;  // screen being viewed
var prevDisplayScreen = 0;  // previous screen being viewed
// 0 = Dashboard
// 1 = Controller Port Settings
// 2 = Manual AC Outlets 
// 3 = Manual DC Ports
// 4 = Calibrate pH
// 5 = Calibrate pH Type
// 6 = Settings
// 7 = Graphs
// 8 = System Settings
// 9 = Logs
// 10 = Dosing Pump settings
// 11 = Version Log
// 12 = Custom rule review


var userID = [];
var username = [];
var userFirstRun = [];
var tipTextEnabled = 1;

var selectedBox = '';  // tracks with widget is being used
var rect = new Array(10);  // stores the size of widgets

var portLabel = [];  // port label


// Drivers
var driverID = [];  // track driverID for each port
var portsPinID = [];  // track all pinID for each port
var roboTankPortName = [];  // portName from pinsAvailable table
var customPortName = [];  // name from pinsAvailable table
var pinTypeID = [];  // pinTypeID from pinsAvailable table
var pinTypeName = [];  // pinTypeID from pinsType table
var gpioPin = [];  // gpio from pinsAvailable table
var pinAssigned = [];  // assigned from pinsAvailable table
var driverName = [];  // driverName from drivers table
var pinIconID = [];  // iconID from pinsAvailable table
var portLogic = [];  // track if logic should be inverted
var totalDrivers = 0;  // track how many drivers are found
var totalDriverPins = [];  // track how many pins each driver has

// Schedules
var scheduleID = [];
var scheduleTypeID = [];  // track what the schedule is for
var schedulePinID = [];  // track pinID from pinsAvailable table
var scheduleType = [];  // track schedule type name
var scheduleStart = [];  // track schedule start time in seconds
var scheduleMon = [];  // track if mon enabled
var scheduleTue = [];  // track if tue enabled
var scheduleWed = [];  // track if wed enabled
var scheduleThu = [];  // track if thu enabled
var scheduleFri = [];  // track if fri enabled
var scheduleSat = [];  // track if sat enabled
var scheduleSun = [];  // track if sun enabled
var scheduleRepeatEnabled = [];  // track if schedule repeats X number of days
var scheduleRepeatDays = [];  // track if schedule is repeated and how often in days
var scheduleOffOn = [];  // track if schedule is on or off
var scheduleName = [];  // track schedule custom name
var scheduleEquipmentName = [];  // track name of equipment for schedule
var scheduleEquipmentIconID = [];  // track iconID of equipment for schedule
var scheduleEnabled = [];  // track if scheduled is enabled
var scheduleEmail = [];  // track if email alert is enabled
var scheduleAlert = [];  // track if dashboard alert is enabled
var scheduleDoseML = [];  // track how many milliliters are attached to schedule
var scheduleTabSelected = 0;  // track what schedule tab is selected
var scheduleSortID = 0;
var scheduleSortType = "";  // clear sort type
var scheduleSortOrder = 0;
var scheduleTableLimit = 10;
var schedulePageNumber = 0;
var scheduleTotal = 0;
var schedulePrevExpanded = null;
var totalPages = 1;

// Sensors
var sensorName = [];  // sensor name
var sensorTypeID = [];  // sensor pinTypeID
var sensorTypeName = [];  // sensor type name
var sensorAssigned = [];  // track if outlet is assigned/enabled 
var sensorIconID = [];  // sensor icon
var sensorGpioPinNumber = [];  // sensor pigpio library pin number
var sensorPinID = [];  // sensor pin ID from pinsAvailable table
var sensorInvert = [];  // track if sensor logic is inverted
var sensorStatus = [];  // used to store state of sensors
var previousSensorStatus = [];  // used to store previous state of sensors
var sensorUpdateIcon = [];  // track if sensor status changed

// AC outlets
var outletName = [];  // outlet name
var outletAssigned = [];  // track if outlet is assigned/enabled
var outletIconID = [];  // outlet icon
var outletGpioPinNumber = [];  // outlet pigpio library pin number
var outletPinID = [];  // outlet pin ID from pinsAvailable table
var outletUpdateIcon = [];  // used for AC icons
var outletSortID = 0;
var outletStatus = [];  // used to store state of outlets
var previousOutletStatus = [];  // used to store previous state of all Pi pins
var outletInvert = [];  // track if outlet logic is inverted
var outletManual = [];  // track if outlet is in manual mode

// DC Ports
var dcName = [];  // dc port name
var dcAssigned = [];  // track if dc port is assigned or enabled
var dcIconID = [];  // dc icon
var dcGpioPinNumber = [];  // dc pigpio library pin number
var dcPinID = [];  // pin ID from pinsAvailable table
var dcUpdateIcon = [];  // used for AC icons
var dcSortID = 0;
var dcStatus = [];  // used to store state of all Pi pins
var previousDcStatus = [];  // used to store previous state of all Pi pins
var dcInvert = [];  // track if DC logic is inverted
var dcManual = [];  // track if outlet is in manual mode

// Custom Rules
var ruleID = [];  // track ruleID
var rulesName = [];  // track rule name
var rulesTypeID = [];  // track ruleTypeID, schedule, sensor or manual
var rulesType = [];  // track rule type, schedule, sensor or manual
var rulesEmail = [];  // track if email is enabled
var rulesAlert = [];  // track if dashboard alert is enabled
var rulesEnabled = [];  // track if rule is enabled
var rulesSensorRuleID = [];  // track ruleID
var rulesSensorAddress = [];  // track sensorAddress for rule sensors
var rulesSensorPinID = [];  // track pinID for rule sensors
var rulesSensorPinTypeID = [];  // track what kind of sensor it is
var rulesSensorName = [];  // track custom name of sensor
var rulesSensorDataType = [];  // track what data from sensor is used in rule
var rulesSensorValue = [];  // track sensor trigger value
var rulesSensorLessGreater = [];  // track if less than or greater than
var rulesSensorStability = [];  // track how long the sensor should remain in trigger zone before triggering, in seconds
var rulesSensorLogic = [];  // track if sensor works with another, None, AND, OR
var rulesEquipRuleID = [];  // track ruleID
var rulesEquipPinID = [];  // track pinID for rule equipment
var rulesEquipPinTypeID = [];  // track pinTypeID for rule equipment
var rulesEquipIconID = [];  // track iconID for rule equipment
var rulesEquipName = [];  // track custom name of equipment
var rulesEquipOffOn = [];  // track if it turns equipment on/off
var rulesEquipSwitchDelay = [];  // track how long until device is switched on or off
var rulesEquipNextDoseDelay = [];  // track how long to delay dosing pump from running again from any system
var rulesEquipDoseML = [];  // track how much to dose for each custom rule

// Light Ports
var lightName = [];  // light name
var lightAssigned = [];  // track if light is assigned or enabled
var lightIconID = [];  // light icon
var lightPinID = [];  // pinID from pinsAvailable table

var lightModeID = [];
var lightModeStart = [];
var lightModePinID = [];
var lightModeStartLevel = [];
var lightModeEnabled = [];



var widgetVisible = [];  // widgets visible
var widgetWidth = [];  // widget width
var widgetHeight = [];  // widget height
var widgetOrder;  // widget order on screen
// var widgetEnabled = [];

var buttonID = [];  // menu buttonID
var buttonLabels = [];  // menu button labels

// system settings
var configPortView = 0;  // track which controller baord is used
var db9_1 = 0;  // track what device is assigned to DB9-1 on controller
var db9_2 = 0;  // track what device is assigned to DB9-2 on controller
var tempCorF = 1;  // track if temp is celsius or fahrenheit
var timeFormat = 0;  // 0 = 24hr, 1 = 12hr time format
var smtpServer = "";  // SMTP server
var smtpPort = "";  // SMTP port
var smtpUser = "";  // SMTP user
var smtpPass = "";  // SMTP password
var smtpProvider = "";  // SMTP provider
var emailEnabled = 0;  // track if email alerts are enabled
var adminEmail = [];
var email1 = [];
var email2 = [];
var email2Enabled = [];
var email3 = [];
var email3Enabled = [];


var redrawTempWidget;  // track if sensor count changes so we can redraw temp widget
var previousDs18b20Found;  // number of ds18b20 sensors connected
var ds18b20Found;  // number of ds18b20 sensors connected
var ds18b20Saved;  // track how many sensors are saved in database
// var ds18b20Plugged;  // track if sensor is plugged in
var ds18b20AddrSaved = [];  // ds18b20 addresses saved
var ds18b20AddrFound = [];  // ds18b20 addresses saved
// var ds18b20AddrFoundPrev = [];  // ds18b20 addresses saved
var ds18b20NamePlugged = [];  // ds18b20 custom name - only sensors plugged in
var ds18b20Name = [];  // ds18b20 custom name - all sensors in database
var ds18b20Temp = [];  // ds18b20 temps
var ds18b20Humidity = [];  // dht22 humidity
var ds18b20Min = [];  // ds18b20 temps minimums
var ds18b20Max = [];  // ds18b20 temps maximums
var ds18b20GraphVisible = [];
var ds18b20GraphRange = [];  // track time frame being viewed in graph
var ds18b20GraphAverage = [];  // track rolling average of graph

var widgetContainters = [];  // holds divs for widgets
var leftNavContainters = [];  // holds divs for left navigation


var redrawProbesWidget;  // track if probe count changes so we can redraw probe widget
var previousPhFound;  // number of probe sensors connected
var phSeen;  // number of probe sensors connected
var phSaved;  // track how many sensors are saved in database
var phPlugged = [];  // track if sensor is plugged in
var phAddrSaved = [];  // probe addresses saved
var phAddrPlugged = [];  // probe addresses saved
var phNamePlugged = [];  // probe custom name - only sensors plugged in
var phName = [];  // probe custom name - all sensors in database
var phValue = [];
var phMinValue = [];
var phMaxValue = [];
var phGraphVisible = [];
var phGraphRange = [];  // track time frame being viewed in graph
var phGraphAverage = [];  // track rolling average of graph
var phCalibrateRunning = [];  // track if calibration is running for a probe
var phBuffer1 = 7.0;
var phBuffer2 = 10.0;
var prevPhCalibrationTickCount = 0;  // track how many readings have taken place during stability phase
var phCalibrationTickCount = 0;  // track how many readings have taken place during stability phase
var calibrationTypeRunning;  // track which calibration type is being preformed
var pHcalibratingAddress = 0;  // track what pH address is being calibrated
var phCalibratingStep = 0;  // track what step calibration is on when doing multi point

var doseID = [];  // track dosing pump ID
var dosePinID = [];  // track dosing pump pinID from pinsAvailable table
var doseName = [];  // track dosing pump custom name
var doseAmount = [];  // stores dose amount in ml
var doseReservoirSize = [];  // stores reservoir size in ml
var doseReservoirRemain = [];  // stores remaining space in reservoir in ml
var doseReservoirColor = [];  // track custom reservoir color
var dosePumpRate = [];  // stores pump rate in ms/ml
var doseStepRunning = [];  // tracks which step the system is in during the dosing process
var doseRunTime = [];  // stores how long pump should run in millis
var doseRuleTimeout = [];  // tracks if dosing pump shouldn't run based on custom rule timer
var doseCalibrationAmount = [];  // tracks dose amount used for calibrating vessel
var doseCalibrateRunning = [];  // tracks is calibration is running
var dosePercentageRemaining = [];
var doseInterval = [];  // track the minimum period between doses in minutes
var doseStatus = [];  // track status of dosing pump to keep UI updated
var doseDisableRes = [];  // track if reservoir is disabled
var dosePumpSpeed = [];  // track speed of dosing pump
var doseResAlert = [];  // track if dashboard alert is enabled
var doseResEmail = [];  // track if email alert is enabled
var doseResAlertML = [];  // track level to be alert on low reservoir
var doseStirrer = [];  // stores if stirrer is enabled
var doseStirrerID = [];  // track what stirrer is connected to dosing pump
var stirrerID = [];  // track stirrer ID
var stirrerPinID = [];  // track pinID of stirrer port
var stirrerName = [];  // track stirrer custom name
var stirrerRuntime = [];  // track how long stirrer should run in seconds
var settlingRuntime = [];  // track how long reservoir should settle after stirring in seconds
var stirrerSpeed = [];  // track speed of stirrer

var graphVisible = [];  // track if graph is marked visible
var graphRange = [];  // track time range being viewed in graph
var graphAverage = [];  // track rolling average of graph
var dyGprah = [];  // used to store instance of each graph for dyGraphs
var graphDays = [];  // track how many days are being viewed on dashboard graph
var graphHours = [];  // track how many hours are being viewed on dashboard graph

var logID = [];
var logType = [];
var logPort = [];
var logSource = [];
var logAction = [];
var logTime = [];
var logNote = [];
var logNumberDays = 1;
var logSortOrder = 1;
var logSortCol = 4;

// version log
var versionID = [];  // version ID
var version = [];  // version number
var versionDate = [];  // version date release
