(function(){ "use strict"; /* Pure logic below — no DOM access, so it can be exercised by node tests. All dynamic values are rendered with textContent (never innerHTML), which keeps user input escaped. */ var GALLONS_PER_SQFT_INCH = 0.623; function ppDripNumOrNull(raw){ if (raw === null || raw === undefined) return null; var t = String(raw).trim(); if (t === "") return null; var v = Number(t); if (!isFinite(v)) return null; return v; } function ppDripRuntime(emitters, flowGph, areaSqft, inches, soil){ emitters = Number(emitters); flowGph = Number(flowGph); areaSqft = Number(areaSqft); inches = Number(inches); if (!isFinite(emitters) || !(emitters >= 1) || Math.floor(emitters) !== emitters) return null; if (!isFinite(flowGph) || !(flowGph > 0)) return null; if (!isFinite(areaSqft) || !(areaSqft > 0)) return null; if (!isFinite(inches) || !(inches > 0)) return null; var gallons = areaSqft * inches * GALLONS_PER_SQFT_INCH; var systemGph = emitters * flowGph; var totalMinutes = Math.round((gallons / systemGph) * 60); var sessionNote; if (soil === "sandy"){ sessionNote = "Sandy soil drains fast, so split this into 2\u20133 shorter sessions spread across the week instead of one long soak."; } else if (soil === "clay"){ sessionNote = "Clay holds water well \u2014 one deep session is the way to go. If you see water pooling or running off, split it into two shorter runs a few hours apart."; } else { sessionNote = "For loam, one or two sessions a week works well."; } return { gallons: gallons, systemGph: systemGph, totalMinutes: totalMinutes, sessionNote: sessionNote }; } function ppDripFormatRuntime(totalMinutes){ if (!(totalMinutes >= 1)) return "less than a minute"; var h = Math.floor(totalMinutes / 60); var m = totalMinutes % 60; var parts = []; if (h > 0) parts.push(h + (h === 1 ? " hour" : " hours")); if (m > 0) parts.push(m + (m === 1 ? " minute" : " minutes")); return "about " + parts.join(" "); } function ppDripFormatGallons(g){ if (g < 10) return String(Math.round(g * 10) / 10); return String(Math.round(g)); } function ppDripTrack(){ if (typeof window === "undefined") return; if (typeof window.ppTrack === "function"){ window.ppTrack("pp_tool_used", {tool: "drip-irrigation-calculator"}); } } function ppDripEl(tag, text, cls){ var n = document.createElement(tag); if (cls) n.className = cls; if (text === undefined) text = null; if (text !== null) n.textContent = text; return n; } function ppDripShowError(out, msg){ out.textContent = ""; var box = ppDripEl("div", null, "pp-error"); box.setAttribute("role", "alert"); box.appendChild(ppDripEl("p", msg)); out.appendChild(box); } if (typeof document !== "undefined"){ var form = document.getElementById("pp-drip-form"); var out = document.getElementById("pp-drip-output"); var emittersInput = document.getElementById("pp-drip-emitters"); var flowSelect = document.getElementById("pp-drip-flow"); var areaInput = document.getElementById("pp-drip-area"); var soilSelect = document.getElementById("pp-drip-soil"); var inchesInput = document.getElementById("pp-drip-inches"); var inchesTouched = false; inchesInput.addEventListener("input", function(){ inchesTouched = true; }); soilSelect.addEventListener("change", function(){ if (!inchesTouched){ var s = soilSelect.value; if (s === "sandy") inchesInput.value = "1.5"; else if (s === "clay") inchesInput.value = "0.75"; else inchesInput.value = "1"; } }); form.addEventListener("submit", function(ev){ ev.preventDefault(); var emitters = ppDripNumOrNull(emittersInput.value); if (emitters === null || Math.floor(emitters) !== emitters || emitters < 1){ ppDripShowError(out, "How many emitters does your system have? Enter a whole number above zero \u2014 count the little drippers on the bed."); return; } var area = ppDripNumOrNull(areaInput.value); if (area === null || area <= 0){ ppDripShowError(out, "What is your bed area in square feet? Length times width works \u2014 a 4 by 8 bed is 32."); return; } var inches = ppDripNumOrNull(inchesInput.value); if (inches === null || inches <= 0){ ppDripShowError(out, "How many inches of water per week are you aiming for? Try 1 as a starting point \u2014 1.5 for sandy soil, 0.75 for clay."); return; } var flow = Number(flowSelect.value); var soil = soilSelect.value; var r = ppDripRuntime(emitters, flow, area, inches, soil); if (r === null){ ppDripShowError(out, "Hmm, something in those numbers does not add up. Double-check each field and try again."); return; } out.textContent = ""; var box = ppDripEl("div", null, "pp-result"); box.appendChild(ppDripEl("h3", "Your weekly runtime")); box.appendChild(ppDripEl("p", "Run " + ppDripFormatRuntime(r.totalMinutes) + " per week.", "pp-action")); var detail = "That is about " + ppDripFormatGallons(r.gallons) + " gallons a week through " + emitters + " emitter" + (emitters === 1 ? "" : "s") + " at " + flow + " GPH each."; box.appendChild(ppDripEl("p", detail)); box.appendChild(ppDripEl("p", r.sessionNote)); if (inches > 3){ box.appendChild(ppDripEl("p", "Heads up: that is a lot of water \u2014 most gardens need somewhere between 0.5 and 2 inches a week. Double-check your target.")); } if (r.totalMinutes > 10080){ box.appendChild(ppDripEl("p", "That is longer than a week has hours \u2014 double-check your numbers, or your system may be undersized for this bed.")); } if (r.totalMinutes < 1){ box.appendChild(ppDripEl("p", "That is a very short run \u2014 your system puts out a lot of water for a small bed. Double-check your emitter count and flow rate.")); } out.appendChild(box); ppDripTrack(); }); } if (typeof module !== "undefined"){ if (module.exports){ module.exports.ppDripRuntime = ppDripRuntime; module.exports.ppDripFormatRuntime = ppDripFormatRuntime; module.exports.ppDripFormatGallons = ppDripFormatGallons; module.exports.ppDripNumOrNull = ppDripNumOrNull; } } })();