// <Dyna Load Name selects>

// init request object for AJAX request
function initAjax() {
  try {request = new XMLHttpRequest();}
  catch (trymicrosoft) {
    try {request = new ActiveXObject("Msxml2.XMLHTTP");}
    catch (othermicrosoft) {
      try {request = new ActiveXObject("Microsoft.XMLHTTP");}
      catch (failed) {
        alert('Warning: you need a browser with AJAX support');
        request = false;
      }
    }
  }
}

/* set VN + ZN Cbo
Params: vn/zn = letzter gesuchter Vor-/Zuname; msgs => cbo */
function setNameCbos(oldV, oldZ, msgSel, msgWait){
  cboV = document.forms[0].elements['VNLeft'];
  cboZ = document.forms[0].elements['ZNLeft'];
  getNames(
    cboV.options[cboV.selectedIndex].text,
    cboZ.options[cboZ.selectedIndex].text,
    oldV, oldZ, msgSel, msgWait);
}

function isChar(lft) { return ('-' != lft && '' != lft); }

/* AJAX query to get all Names beginning with lft
Params:
- lftV/Z: Anfangsbuchstabe Vor-/Zuname; '-' => do nothing
- oldV/Z: letztgesuchter Vor-/Zuname
- msgSel, msgWait => Msg in Cbo
*/
function getNames(lftV, lftZ, oldV, oldZ, msgSel, msgWait) {
  cboV = document.forms[0].elements['VORNAME'];
  cboZ = document.forms[0].elements['ZUNAME'];

  if ('-' != lftV) {
    cboV.length = 0;
    var s = ('' == lftV) ? msgSel : msgWait;
    cboV.options[0] = new Option (s, '', false, false);
  }
  if ('-' != lftZ) {
    cboZ.length = 0;
    var s = ('' == lftZ) ? msgSel : msgWait;
    cboZ.options[0] = new Option (s, '', false, false);
  }

  if (isChar(lftV) || isChar(lftZ))
  {
    var url = "inc/getNames.php?lftV="+lftV+"&lftZ="+lftZ;
    request.open("GET", url, true);
    request.onreadystatechange = function() {
      if (request.readyState == 4) {
        if (request.status == 200) {
          var aRespo = request.responseText.split('|');
          if (isChar(lftV)) {
            var names = eval("(" + aRespo[0] + ")");
            fillNames(names, 'VORNAME', oldV, msgSel);
          }
          if (isChar(lftZ)) {
            var names = eval("(" + aRespo[1] + ")");
            fillNames(names, 'ZUNAME', oldZ, msgSel);
          }
        } else alert("status is " + request.status);

      }
    }
    request.send(null);
  }
}

/* Fill Combo with Names
Params:
- names = array with values to fill in
- dest = name of Combo
- old = value to select
- msgSel => Cbo-Text if empty
*/
function fillNames(names, dest, old, msgSel) {
  cbo = document.forms[0].elements[dest];
  cbo.length = 0;
  v = (old != '');
  for(i=0; i<names.length; i++) {
    cbo.options[cbo.length] = new Option(names[i], names[i], false, (v && names[i] == old));
  }
  if (0 == cbo.length) cbo.options[0] = new Option (msgSel, '', false, false);
}

// </Dyna Load Name selects>

// <Show/Hide Elements (Suchmasken) >

function checkMode() {
  InstArtChanged();
  ZeitArtChanged();
}

function InstArtChanged() {
  c = document.forms[0].INST_ART;
  i = c.options[c.selectedIndex].value;
  document.getElementById('dInstAll')  .style.display = (i==0) ? 'block' : 'none';
  document.getElementById('dInstOrd')  .style.display = (i==1) ? 'block' : 'none';
  document.getElementById('dInstDioez').style.display = (i==2) ? 'block' : 'none';
  document.getElementById('dInstAnder').style.display = (i==3) ? 'block' : 'none';
}

function ZeitArtChanged() {
  i = document.forms[0].ZeitArt.selectedIndex;
  document.getElementById('dZeitBelegt').style.display = (i==0) ? 'block' : 'none';
  document.getElementById('dZeitVonBis').style.display = (i==1) ? 'block' : 'none';
}

// </Show/Hide Elements (Suchmasken) >

// convert em >> px
function em2px() {
  return document.getElementById("emSize").offsetLeft;
}


