"use strict";
var CaseData = [];
var DrugInfo = [];
var IvAdmin = [];
var UserIV = [];
var afib6 = [];
var asyst1 = [];
var sinus1 = [];
var sinus2 = [];
var sinus3 = [];
var sinus4 = [];
var sinus5 = [];
var sinus6 = [];
var st1 = [];
var st2 = [];
var st3 = [];
var vfib2 = [];
var vtach3 = [];
var hump = [];
var nl = [];
var slant = [];
var IvFluidStr = ["None", "Lactated Ringers", "Normal saline", "Albumin", "Packed red blood cells"];
var DSF = [0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 6.0, 6.5, 7.0, 8.0, 9.0, 10, 11, 12];
var HAL = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 2.0, 3.0, 4.0, 5.0];
var ISF = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 2.0, 2.5, 3.0, 4.0, 5.0];
var SEV = [0.0, 0.2, 0.4, 0.6, 0.8, 1.0, 1.2, 1.4, 1.6, 1.8, 2.0, 2.2, 2.4, 2.6, 2.8, 3.0, 4.0, 5.0, 6.0, 8.0];

// based on canvas height of 510px in index.php --------------
var SweepWidth = 470;
var EcgIILimit1 = 8;
var EcgIIBaseline = 36;
var EcgIILimit2 = 51;
var EcgV5Limit1 = 59;
var EcgV5Baseline = 87;
var EcgV5Limit2 = 120;
var P1Limit1 = 128;
var P1Baseline = 190; 
var P1Limit2 = 214; 
var SpO2Limit1 = 234; 
var SpO2Baseline = 280; 
var SpO2Limit2 = 310; 
var CO2Limit1 = 324;     
var CO2Baseline = 402;    
var CO2Limit2 = 416;     
var APLimit1 = 434; 
var APBaseline = 492; 
var APLimit2 = 508; 
// ------------------------------------------------

var aboutAnesSim7;
var APY1;
var APY2;
var AwLaryTubeSuccess;
var CaseDefault;
var CaseLogStr1;
var CaseLogStr2;
var CaseTimeInSec;
var CO2Y1;
var CO2Y2;
var CoCurrentPoint;   
var CoHt;
var CprHt;
var CoSweep;
var CurrentCase;
var CurrentCaseIndex;
var CurrentHemoWave;
var CurrentRespWave;
var CurrentState;
var CurrentStateIndex;
var DefibedEnergy;
var EcgX1;
var EcgX2;
var EcgY1;
var EcgY2;
var EcgIIY1;
var EcgIIY2;
var EcgV5Y1;
var EcgV5Y2;
var GradeStr;
var HemoContext;
var HemoX0 = 3;
var HemoCprCount;
var HemoCurrentPoint;
var HemoSweepPosition;
var HemoX1;
var HemoX2;
var HemoZapCount;
var LastOneSecUpdate;
var LastTenSecUpdate;
var LastHemoUpdate;
var LastRespUpdate;
var LastTenSecUpdate;
var MyContentHeight; // used in scripts.js 
var MyTimeout;
var NextHemoWave;
var NextRespWave;
var NbpS;
var NbpD;
var NbpM;
var NbpTimer = 179;
var P1Y1;
var P1Y2;
var PddAddedBicarb;
var PddDeltaHr;
var PddDeltaBp;
var PddHr;
var PddSpRate;
var PddAbpS;
var PddAbpD;
var PddAbpM;
var PddCo;
var PddCvp;
var PddDoa;
var PddMic;
var PddNmb;
var PddPapS;
var PddPapD;
var PddPapM;
var PddPapW;
var PddTfluid;
var PddTlr;
var PddTns;
var PddTalb;
var PddTprbc;
var PddTuo;
var PddTebl;
var PrpAlvAgt;
var PrpAlvDes;
var PrpAlvHal;
var PrpAlvIso;
var PrpAlvN2O;
var PrpAlvO2;
var PrpAlvRr;
var PrpAlvSev;
var PrpAlvTv;
var PrpArtBicarb;
var PrpArtPco2;
var PrpArtPh;
var PrpArtPo2;
var PrpEtCO2;
var PrpFeAgt;
var PrpFeDes;
var PrpFeHal;
var PrpFeIso;
var PrpFeN2O;
var PrpFeO2;
var PrpFeSev;
var PrpFiAgt;
var PrpFiDes;
var PrpFiHal;
var PrpFiIso;
var PrpFiN2O;
var PrpFiO2;
var PrpFiSev;
var PrpMac;
var PrpMonPeep;
var PrpMonPip;
var PrpMonRr;
var PrpMonTv;
var PrpMv;
var PrpPotassium;
var PrpRespWaveStr;
var PrpSpO2;
var PrpTotalFlow;
var PrpTubeStatusStr;
var RespCurrentPoint;
var RespSweepPosition;
var RespX1;
var RespX2;
var Score;
var SpO2Y1;
var SpO2Y2;
var StateDefault;
var StateStartTime;
var SweepStartTime = 0;  // added to debug slow sweep 
var SurgeonStartTime;
var SurgeonStopTime;
var TimeoutInterval = 10;
var TransHr;            
var TransAbpS;
var TransAbpD;
var TransAbpM;
var UpdateOneSecCount;
var UpdateFiveSecCount;
var UserAdjustedHeadFlag;
var UserAirFlow;
var UserAirwayStr;
var UserAspiratedCvpFlag;
var UserBagRr;
var UserBagTv;
var UserBreathModeStr;
var UserCalledForHelpFlag;
var UserCaseRunning;
var UserCheckedCircuitFlag;
var UserCheckedTubeFlag;
var UserConnectedFlag;
var UserConnectedThisCaseFlag;
var UserDefibedEnergy;
var UserDepthTubeCurrent;
var UserDoingCricoidFlag;
var UserDoingCprFlag;
var UserDoingIcedIvFlag;
var UserDoingIcePackFlag;
var UserDoingIcedLavageFlag;
var UserDoingWarmBlanketFlag;
var UserDoingWarmIvFlag;
var UserDrugIndex;
var UserFloodedFieldFlag;
var UserGasColorStr = "US"; // "US", "ISO"
var UserImageStr = "photo"; // "photo" or "styled"
var UserInsertedArtLineFlag;
var UserInsertedChestTubeFlag;
var UserInsertedCvpFlag;
var UserInsertedFoleyFlag;
var UserInsertedNasalFlag;
var UserInsertedOralFlag;
var UserInsertedPaCathFlag;
var UserMakeIncisionFlag;
var UserMgmtEnabledFlag;
var UserMonitorDisplayStr;
var UserN2OFlow;
var UserNbpInterval = 3;
var UserNbpModeStr;
var UserO2Flow;
var UserOverdosedFlag;
var UserPeep;
var UserPositionStr;
var UserShowingCenter = "start"; 
//    "start", "resume", "preop", "caselog", "newcase", or
//    "patient", "monitors", "airway", "breathing", "drugs", or
//    "fluids", "labs", "surgeon", "resuscitate" 
var UserShowingWhatNowFlag;
var UserSoundFlag = false; 
var UserSuctionedFlag;
var UserSurgeonModeStr;
var UserSyncedFlag;
var UserTriedFoFlag;
var UserTriedLaryTubeFlag;
var UserTriedLmaFlag;
var UserVaporizerAmount;
var UserVaporizerStr = "Sevoflurane";
var UserVapStr = "SEV";  // "DEF", "HAL", "ISF", "SEV"
var UserVentRr;
var UserVentTv;
var ZapHt;


document.observe("dom:loaded", function() {
	console.log("UserImageStr: " + UserImageStr);
	console.log("UserSoundFlag: " + UserSoundFlag);
	console.log("UserGasColorStr: " + UserGasColorStr);
//Use function SetGasColors to set gas colors
    try {
 //       $("divErrorScreen").hide();
		document.getElementById("divErrorScreen").hidden = true;
        $("divFullScreen").show();
		InitializeHemoWaveforms();
        HemoContext = $("canvasHemo").getContext("2d");
        HemoContext.lineWidth = 2;
        SetupButtonListeners();
        CreateDropListFlow($("divN2O"));
        $("seldivN2O").observe("change", SetN2OFlow);
        $("seldivN2O").observe("change", RefreshBobbin);
        CreateDropListFlow($("divAir"));
        $("seldivAir").observe("change", SetAirFlow);
        $("seldivAir").observe("change", RefreshBobbin);
        CreateDropListFlow($("divO2"));
        $("seldivO2").observe("change", SetO2Flow);
        $("seldivO2").observe("change", RefreshBobbin);
		
		SetGasColors();

		CreateDropListVapAmount($("divVaporizer"));
        $("selVapAmount").observe("change", SetVapAmount);
        $("selVaporizer").observe("change", ChangeVaporizer);
        $("btnGo").hide();
        $("btnGo").addClassName("clsButtonEnabled").removeClassName("clsButtonDisabled");  
        ShowNewCaseScreen();
        
    } catch (error) {
        console.log("Error during initialization:", error);
    }
});

/* --------------------------------------------------------
function SwitchGasColors() {
	if (UserGasColorStr == "ISO") {
		UserGasColorStr = "US"
	} else {
		UserGasColorStr = "ISO"
	}
	SetGasColors();
	console.log("colors set to " + UserGasColorStr);
}
------------------------------------------------------------- */

function SetGasColors() {
    const gases = ["N2O", "Air", "O2"];
    const schemes = ["ISO", "US", "EIGA", "NFPA", "MEGA"];

    // Cache DOM elements for each gas div
    const elements = gases.map(gas => document.getElementById(`div${gas}`));

    // Remove all scheme classes from each element to avoid conflicts
    elements.forEach(el => {
        schemes.forEach(scheme => {
            gases.forEach(gas => {
                el.classList.remove(`${scheme}-${gas}`);
            });
        });
    });

    // Use US as default scheme if UserGasColorStr is undefined or invalid
    const scheme = (UserGasColorStr && schemes.includes(UserGasColorStr)) ? UserGasColorStr : "US";

    // Add the appropriate scheme class for each gas
    gases.forEach((gas, i) => {
        elements[i].classList.add(`${scheme}-${gas}`);
    });
}


// CreateScroller is called
function CreateScroller(wrapper) {
    $(wrapper).addClassName("desktopScroll");
}


/* --- the following functions are not used anymore --------------------------
document.addEventListener('touchmove', function(e) {
	console.log("touchmove called");
	e.preventDefault();
}, { passive: false });

function RefreshScroller() {
	console.log("RefreshScroller called");
    if (MyScroll) MyScroll.refresh();
}

function onBackPressed() {
	console.log("onBackPressed called");
    onFinish();
    if (typeof Android !== 'undefined' && Android && Android.onFinish) {
        Android.onFinish();
    }
}

function onFinish() {
	console.log("onFinish called");
    clearTimeout(MyTimeout);
}
------------------------------------------------------------------------------ */