Where to put a function code that changes visibility of multiple objects based on the value of multiple objects
I have a function that I was assisted by AI to write which changes the visibility of a bunch of objects based on the value of a bunch of dropdowns and checkboxes. It also includes listener events so that when these dropdowns and checkboxes are changed the visibility of the items affected by them is updated as well. My problem is that I do not know JavaScript very well and the folly of AI is that it's not always good at answering what I'm asking, but neither is my searching of Google so it's probably just me trying to do JavaScript things without a solid understanding of JavaScript that's causing the issue.
Where does the function go?
Does it go into the master pages Initialize? Does it go under every dropdown and check boxes Change? The code is over 500 lines, and that seems like it would bloat the document quite a lot and be very redundant.
What about the listener events? The AI tells me to put the listener events into the master pages' Initialize as well as every objects' Change, which seems like quite a lot, and doesn't feel right, but I don't know enough to be able to find the answers myself.
Here's a small portion to give you an example of the code:
xfa.resolveNode("ACQTypedrop").addEventListener("change", handleFieldChange);
xfa.resolveNode("KTrevdrop").addEventListener("change", handleFieldChange);
xfa.resolveNode("KTdollarfill").addEventListener("change", handleFieldChange);
function handleFieldChange(event) {
var acqType = xfa.resolveNode("ACQTypedrop").rawValue;
var ktRev = xfa.resolveNode("KTrevdrop").rawValue;
var ktDollar = xfa.resolveNode("KTdollarfill").rawValue;
if ((acqType == "1" || acqType == "2" || acqType == "3") && ktRev == "1") {
xfa.resolveNode("S1Q1").presence = "visible";
xfa.resolveNode("S1Q3").presence = "visible";
} else {
xfa.resolveNode("S1Q1").presence = "hidden";
xfa.resolveNode("S1Q3").presence = "hidden";
}
if ((acqType == "1" || acqType == "2" || acqType == "3") && ktRev == "1" && ktDollarFill >= 250000) {
xfa.resolveNode("S2Q1").presence = "visible";
} else {
xfa.resolveNode("S2Q1").presence = "hidden";
}
}