var sum = 0;
var options = new Array(
  new Array('ndomod_process_process_data', 1),
  new Array('ndomod_process_timed_event_data', 2),
  new Array('ndomod_process_log_data', 4),
  new Array('ndomod_process_system_command_data', 8),
  new Array('ndomod_process_event_handler_data', 16),
  new Array('ndomod_process_notification_data', 32),
  new Array('ndomod_process_service_check_data', 64),
  new Array('ndomod_process_host_check_data', 128),
  new Array('ndomod_process_comment_data', 256),
  new Array('ndomod_process_downtime_data', 512),
  new Array('ndomod_process_flapping_data', 1024),
  new Array('ndomod_process_program_status_data', 2048),
  new Array('ndomod_process_host_status_data', 4096),
  new Array('ndomod_process_service_status_data', 8192),
  new Array('ndomod_process_adaptive_program_data', 16384),
  new Array('ndomod_process_adaptive_host_data', 32768),
  new Array('ndomod_process_adaptive_service_data', 65536),
  new Array('ndomod_process_external_command_data', 131072),
  new Array('ndomod_process_object_config_data', 262144),
  new Array('ndomod_process_main_config_data', 524288),
  new Array('ndomod_process_aggregated_status_data', 1048576),
  new Array('ndomod_process_retention_data', 2097152),
  new Array('ndomod_process_acknowledgement_data', 4194304),
  new Array('ndomod_process_statechange_data', 8388608),
  new Array('ndomod_process_contact_status_data', 16777216),
  new Array('ndomod_process_adaptive_contact_data', 33554432),
  new Array('ndomod_process_everything', 67108863)
);

function updateSum() {
  var sum = 0;
  for (var i = 0; i < options.length; i++) {
    if (document.getElementById(options[i][0]).checked) {
      sum = sum | parseInt(options[i][1]);
    }
  }
  document.getElementById('processing_options').value = 'data_processing_options=' + sum;  
} 

function updateCheckBoxes(event) {
  var code = 0; 
  if (!event) {
    event = window.event;
  }
  if (event.which) {
    code = event.which;
  } else if (event.keyCode) {
    code = event.keyCode;
  }
  if ((code >= 48 && code <= 58) || (code == 8) || (code == 46)) {
    if (sum = document.getElementById('processing_options').value.match(/^\s*data_processing_options\s*=\s*(\d+)\s*$/)) {
      sum = RegExp.$1;
      updateSum2();
    }
  }
}

function updateSum2() {
  for (var i = 0; i < options.length; i++) {
    if (options[i][1] & sum) {
      document.getElementById(options[i][0]).checked = true;
    } else {
      document.getElementById(options[i][0]).checked = false;
    }
  }
  if (sum == 67108863) { 
    document.getElementById('ndomod_process_everything').checked = true;
  } else {
    document.getElementById('ndomod_process_everything').checked = false;
  }
}

function drawTable() { 
  function stringCompareInArray(a, b) {
    return (a[0] == b[0]) ? 0 : (a[0] > b[0]) ? 1 : -1
  }

  var po = document.getElementById('options');
  var table = document.createElement('table');
  table.style.tableLayout = 'auto';
  table.style.width = "0%";
  var tbody = document.createElement('tbody');
  options.sort(stringCompareInArray);
  for (var i = 0; i < options.length; i++) {
    var tr = document.createElement('tr');
    var td1 = document.createElement('td');
    var td2 = document.createElement('td');
    //var td3 = document.createElement('td');
    var inp2 = document.createElement('input');
    td1.style.color = '#33ff00';
    td1.style.whiteSpace = 'nowrap';
    //td1.style.width = '10em';
    td1.style.backgroundColor = 'black';
    td1.style.paddingLeft = '8px';
    td1.style.paddingRight = '16px';
    td1.style.paddingTop = '0px';
    td1.style.paddingBottom = '0px';
    td1.style.height = '1em';
    td1.style.borderLeft = '0px';
    td1.style.borderTop = '0px';
    td1.style.borderBottom = '0px';
    td1.style.borderRight = '0px';

    td2.style.textAlign = 'right';
    td2.style.color = '#33ff00';
    td2.style.backgroundColor = 'black';
    td2.style.paddingRight = '8px';
    td2.style.paddingTop = '0px';
    td2.style.paddingBottom = '0px';
    td2.style.height = '1em';
    td2.style.borderLeft = '0px';
    td2.style.borderTop = '0px';
    td2.style.borderBottom = '0px';
    td2.style.borderRight = '0px';

    td1.style.border = "0px";
    td2.style.border = "0px";
    inp2.id = options[i][0];
    inp2.type = 'checkbox';
    inp2.style.width = '13px';
    inp2.style.height = '13px';
    inp2.style.margin = '0px';
    inp2.style.paddingTop = '0px';
    inp2.style.paddingBottom = '0px';

    inp2.style.border = '1px solid orange';
    if (options[i][0] == 'ndomod_process_everything') {
      inp2.style.border = '1px solid red';
    }
    inp2.onclick = updateSum;
    td1.appendChild(document.createTextNode(options[i][0]));
    td2.appendChild(inp2);
    tr.appendChild(td1);
    tr.appendChild(td2);
    tbody.appendChild(tr);
  }
  var result = document.createElement('input');
  var tr = document.createElement('tr');
  var td = document.createElement('td');
  result.readonly = true;
  result.style.width = '100%';
  result.id = 'processing_options';
  result.value = 'data_processing_options=0';
  result.onkeyup = updateCheckBoxes;
  td.colSpan = 2;
  td.appendChild(result);
  result.style.border = '1px solid orange';
  td.style.color = '#33ff00';
  td.style.backgroundColor = 'black';
  td.style.paddingLeft = '8px';
  td.style.paddingRight = '8px';
  td.style.borderLeft = '0px';
  td.style.borderTop = '0px';
  td.style.borderBottom = '0px';
  tr.style.width = '100%';
  tr.appendChild(td);
  tbody.appendChild(tr);
  table.appendChild(tbody);
  table.style.borderCollapse = 'collapse';
  tbody.firstChild.childNodes[0].style.paddingTop = '8px';
  tbody.firstChild.childNodes[1].style.paddingTop = '8px';
  tbody.lastChild.childNodes[0].style.paddingBottom = '8px';
  po.appendChild(table);
}

