Use the xf:load action in combination with an xforms-value-changed event to call a "dirty" function containing setBunload(true).

Likewise, use the load action within the submission to call a "clean" function containing "setBunload(false)".

function dirty() {
setBunload(true);
}
function clean() {
setBunload(false);
}
This one goes in your "submission" element:
<xf:loadresource="javascript:clean()"ev:event="xforms-submit-done"/>
and this
<xf:loadresource="javascript:dirty()"ev:event="xforms-value-changed"/>
goes somewhere in the form - place it at the body-level to capture all change events, or nest it within a more appropriate container.