$(function() { var mkRow = function() { var row = $(""); row.append($("")); var yn = mkYNCheckbox("RepetitionEnabled", false); var cell = $(""); cell.append(yn.hidden); cell.append(yn.checkbox); row.append(cell); cell = $(""); cell.append($(" ")); var unitsel = $("")); attachFocusHandler($("input, select", row)); return row; }; var mkYNCheckbox = function(name, b) { var hidden = $(""); hidden.prop("value", b ? "yes" : "no"); hidden.prop("name", name); var checkbox = $(""); checkbox.prop("checked", b); checkbox.change(function() { hidden.prop("value", checkbox.prop("checked") ? "yes" : "no"); }); return {"hidden": hidden, "checkbox": checkbox}; }; $("select.enabler").each(function(i) { var self = $(this); var yn = mkYNCheckbox(self.prop("name"), self.val() == "yes"); self.before(yn.hidden); self.before(yn.checkbox); self.remove(); }); var maxSchedules = $("table.schedules tbody tr").length; var checkInsRow = function() { if($("table.schedules tbody tr").length >= maxSchedules) { return; } $("table.schedules tbody").append(mkRow()); }; var attachFocusHandler = function(q) { q.focus(function() { var myrow = $(this).parent().parent(); $("table.schedules tbody tr").not(myrow).each(function(i) { checkRemoveRow($(this)); }); checkInsRow(); }); }; var checkRemoveRow = function(row) { if($("input[name=\"Start\"]", row).val() == "") { row.remove(); } }; var updateSchedulesTab = function() { $("table.schedules tbody tr").each(function(i) { checkRemoveRow($(this)); }); checkInsRow(); }; attachFocusHandler($("table.schedules input, table.schedules select")); updateSchedulesTab(); });