// ######################### LOGIN  #########################

function CHECK_PASSWORD(user, pwd) {
  var handle = atlbplugin.OpenTransaction();
  if(handle < 0) {
    showMsg(handle, NO_DB, "Das Passwort kann nicht geprüft werden.");
    return("400");
  }
  atlbplugin.SetQueryParam(handle, "USERNAME", user);
  if(pwd != "") atlbplugin.SetQueryParam(handle, "PASSWORD", new String(pwd));
  if(atlbplugin.RunQuery(handle, "login/checkPassword") != 200) {
    showMsg(handle, "", "Das Passwort kann nicht geprüft werden.");
    atlbplugin.CloseTransaction(handle);
    return("400");
  }
  var result = getQueryData(handle);
  atlbplugin.CloseTransaction(handle);
  return(result);
}

function CHANGE_PASSWORD(user, pwd, newpwd) {
  var handle = atlbplugin.OpenTransaction();
  if(handle < 0) {
    showMsg(handle, NO_DB, "Das Passwort kann nicht geändert werden.");
    return("400");
  }
  atlbplugin.SetQueryParam(handle, "USERNAME", user);
  atlbplugin.SetQueryParam(handle, "PASSWORD", new String(pwd));
  atlbplugin.SetQueryParam(handle, "NEW_PASSWORD", new String(newpwd));
  if(atlbplugin.RunQuery(handle, "login/changePassword") != 200) {
    showMsg(handle, "", "Das Passwort kann nicht geändert werden.");
    atlbplugin.CloseTransaction(handle);
    return("400");
  }
  var result = getQueryData(handle);
  atlbplugin.CloseTransaction(handle);
  return(result);
}

function RESET_PASSWORD(user) {
  var handle = atlbplugin.OpenTransaction();
  if(handle < 0) {
    showMsg(handle, NO_DB, "Das Passwort kann nicht geändert werden.");
    return("400");
  }
  atlbplugin.SetQueryParam(handle, "USERNAME", user);
  if(atlbplugin.RunQuery(handle, "login/resetPassword") != 200) {
    showMsg(handle, "", "Das Passwort kann nicht zurückgesetzt werden.");
    atlbplugin.CloseTransaction(handle);
    return("400");
  }
  var result = getQueryData(handle);
  atlbplugin.CloseTransaction(handle);
  return(result);
}

function LISTE_LOGIN_BORG(user) {
  var handle = atlbplugin.OpenTransaction();
  if(handle < 0) {
    showMsg(handle, NO_DB, "Die Organisationsdaten können nicht gelesen werden.");
    return(null);
  }
  atlbplugin.SetQueryParam(handle, "P_BEA", user);
  atlbplugin.SetQueryParam(handle, "order", "ORG_ID");
  if(atlbplugin.RunQuery(handle, "adi/query/e_q_borg_view") != 200) {
    showMsg(handle, "", "Die Organisationsdaten können nicht gelesen werden.");
    atlbplugin.CloseTransaction(handle);
    return(null);
  }
  var result = atlbplugin.GetQueryData(handle);
  atlbplugin.CloseTransaction(handle);
  return(result);
}

function READ_LOGIN_BORG(handle, borgs, index) {
  if(index == "") return;
  var doOpen = (handle == null);
  if(doOpen) {
    handle = atlbplugin.OpenTransaction();
    if(handle < 0) return;
    atlbplugin.SetQueryData(handle, borgs)
  }
  sbiplg_setvar("A_BEA_ID[0]", atlbplugin.GetQueryResultItem(handle, "BEA_ID", index));
  sbiplg_setvar("A_BEA_VNAME[0]", atlbplugin.GetQueryResultItem(handle, "BEA_VNAME", index));
  sbiplg_setvar("A_BEA_NAME[0]", atlbplugin.GetQueryResultItem(handle, "BEA_NAME", index));
  sbiplg_setvar("A_BEA_POSITION[0]", atlbplugin.GetQueryResultItem(handle, "BEA_POSITION", index));
  sbiplg_setvar("A_BEA_GESCHLECHT[0]", atlbplugin.GetQueryResultItem(handle, "BEA_GESCHLECHT", index));
  sbiplg_setvar("A_BEA_STRASSE[0]", atlbplugin.GetQueryResultItem(handle, "BEA_STRASSE", index));
  sbiplg_setvar("A_BEA_PLZ[0]", atlbplugin.GetQueryResultItem(handle, "BEA_PLZ", index));
  sbiplg_setvar("A_BEA_ORT[0]", atlbplugin.GetQueryResultItem(handle, "BEA_ORT", index));
  sbiplg_setvar("A_BEA_LAND[0]", atlbplugin.GetQueryResultItem(handle, "BEA_LAND", index));
  sbiplg_setvar("A_BEA_TEL[0]", atlbplugin.GetQueryResultItem(handle, "BEA_TEL", index));
  sbiplg_setvar("A_BEA_MOBIL[0]", atlbplugin.GetQueryResultItem(handle, "BEA_MOBIL", index));
  sbiplg_setvar("A_BEA_FAX[0]", atlbplugin.GetQueryResultItem(handle, "BEA_FAX", index));
  sbiplg_setvar("A_BEA_ERSTELLT[0]", atlbplugin.GetQueryResultItem(handle, "BEA_ERSTELLT", index));
  sbiplg_setvar("A_BEA_REVIDIERT[0]", atlbplugin.GetQueryResultItem(handle, "BEA_REVIDIERT", index));

  sbiplg_setvar("A_ORG_ID[0]", atlbplugin.GetQueryResultItem(handle, "ORG_ID", index));
  sbiplg_setvar("A_ORG_MANDANT[0]", atlbplugin.GetQueryResultItem(handle, "ORG_MANDANT", index));
  sbiplg_setvar("A_ORG_NAME1[0]", atlbplugin.GetQueryResultItem(handle, "ORG_NAME1", index));
  sbiplg_setvar("A_ORG_NAME2[0]", atlbplugin.GetQueryResultItem(handle, "ORG_NAME2", index));
  sbiplg_setvar("A_ORG_NAME3[0]", atlbplugin.GetQueryResultItem(handle, "ORG_NAME3", index));
  sbiplg_setvar("A_ORG_STRASSE[0]", atlbplugin.GetQueryResultItem(handle, "ORG_STRASSE", index));
  sbiplg_setvar("A_ORG_PLZ[0]", atlbplugin.GetQueryResultItem(handle, "ORG_PLZ", index));
  sbiplg_setvar("A_ORG_ORT[0]", atlbplugin.GetQueryResultItem(handle, "ORG_ORT", index));
  sbiplg_setvar("A_ORG_LAND[0]", atlbplugin.GetQueryResultItem(handle, "ORG_LAND", index));
  sbiplg_setvar("A_ORG_TEL[0]", atlbplugin.GetQueryResultItem(handle, "ORG_TEL", index));
  sbiplg_setvar("A_ORG_FAX[0]", atlbplugin.GetQueryResultItem(handle, "ORG_FAX", index));
  sbiplg_setvar("A_ORG_EMAIL[0]", atlbplugin.GetQueryResultItem(handle, "ORG_EMAIL", index));
  sbiplg_setvar("A_ORG_WEB[0]", atlbplugin.GetQueryResultItem(handle, "ORG_WEB", index));
  sbiplg_setvar("A_ORG_ERSTELLT[0]", atlbplugin.GetQueryResultItem(handle, "ORG_ERSTELLT", index));
  sbiplg_setvar("A_ORG_REVIDIERT[0]", atlbplugin.GetQueryResultItem(handle, "ORG_REVIDIERT", index));

  sbiplg_setvar("A_BORG_ID[0]", atlbplugin.GetQueryResultItem(handle, "BORG_ID", index));
  sbiplg_setvar("A_BORG_ORG_ID[0]", atlbplugin.GetQueryResultItem(handle, "ORG_ID", index));
  sbiplg_setvar("A_BORG_ROLLE[0]", atlbplugin.GetQueryResultItem(handle, "BORG_ROLLE", index));
  sbiplg_setvar("A_BORG_PRODUKTION[0]", atlbplugin.GetQueryResultItem(handle, "BORG_PRODUKTION", index));
  sbiplg_setvar("A_BORG_PRODUKTION2[0]", atlbplugin.GetQueryResultItem(handle, "BORG_PRODUKTION2", index));
  sbiplg_setvar("A_BORG_PRODUKTION3[0]", atlbplugin.GetQueryResultItem(handle, "BORG_PRODUKTION3", index));
  sbiplg_setvar("A_BORG_PRODUKTION4[0]", atlbplugin.GetQueryResultItem(handle, "BORG_PRODUKTION4", index));
  sbiplg_setvar("A_BORG_EMAIL[0]", atlbplugin.GetQueryResultItem(handle, "BORG_EMAIL", index));
  sbiplg_setvar("A_BORG_ERSTELLT[0]", atlbplugin.GetQueryResultItem(handle, "BORG_ERSTELLT", index));
  sbiplg_setvar("A_BORG_REVIDIERT[0]", atlbplugin.GetQueryResultItem(handle, "BORG_REVIDIERT", index));
  if(doOpen) atlbplugin.CloseTransaction(handle);
}

// ######################### ORG  #########################

function LISTE_ORG(sql) {
  var handle = atlbplugin.OpenTransaction();
  if(handle < 0) {
    showMsg(handle, NO_DB, "Die Liste der Organisationen kann nicht gezeigt werden.");
    return;
  }
  atlbplugin.SetQueryParam(handle, "order", orgOrder);
  if(sql.length > 0) atlbplugin.SetQueryParam(handle, "sql", sql);
  atlbplugin.CreateFrame(handle, "orglist.htpl", "frMain.frSM2");
  atlbplugin.CloseTransaction(handle);
}

function UPDATE_ORG() {
  var handle = atlbplugin.OpenTransaction();
  if(handle < 0) {
    showMsg(handle, NO_DB, "Die Organisation kann nicht gespeichert werden.");
    return(false);
  }
  atlbplugin.SetQueryParam(handle, "MANDANT", compute("A_ORG_Mandant"));
  atlbplugin.SetQueryParam(handle, "NAME1", compute("A_ORG_Name1"));
  atlbplugin.SetQueryParam(handle, "NAME2", compute("A_ORG_Name2"));
  atlbplugin.SetQueryParam(handle, "NAME3", compute("A_ORG_Name3"));
  atlbplugin.SetQueryParam(handle, "STRASSE", compute("A_ORG_Strasse"));
  atlbplugin.SetQueryParam(handle, "PLZ", compute("A_ORG_PLZ"));
  atlbplugin.SetQueryParam(handle, "ORT", compute("A_ORG_Ort"));
  atlbplugin.SetQueryParam(handle, "LAND", compute("A_ORG_Land"));
  atlbplugin.SetQueryParam(handle, "TEL", compute("A_ORG_Tel"));
  atlbplugin.SetQueryParam(handle, "FAX", compute("A_ORG_Fax"));
  atlbplugin.SetQueryParam(handle, "EMAIL", compute("A_ORG_EMail"));
  atlbplugin.SetQueryParam(handle, "WEB", compute("A_ORG_Web"));
  if(sbiplg.GetConfigString("CONFIG.ORGLOGO", "value", "0") == "1"){
  	atlbplugin.SetQueryParam(handle, "TITLECOLOR", compute("A_ORG_TitleColor"));
  	atlbplugin.SetQueryParam(handle, "TITLEBGCOLOR", compute("A_ORG_TitleBgColor"));
  }
  if(getORG_ID() != "") atlbplugin.SetQueryParam(handle, "ORG_ID", getORG_ID());
  if(atlbplugin.RunQuery(handle, "adi/insert_or_update/e_organisation") != 200) {
    showMsg(handle, "", "Die Organisation kann nicht gespeichert werden.");
    atlbplugin.CloseTransaction(handle);
    return(false);
  }
  setORG_ID(getQueryData(handle));
  atlbplugin.CloseTransaction(handle);
  return(true);
}

function READ_ORG() {
  var orgid = getORG_ID();
  if(orgid == "") return(false);
  var handle = atlbplugin.OpenTransaction();
  if(handle < 0) {
    showMsg(handle, NO_DB, "Die Organisation kann nicht gelesen werden.");
    return(false);
  }
  atlbplugin.SetQueryParam(handle, "ORG_ID", orgid);
  if(atlbplugin.RunQuery(handle, "adi/query/e_organisation") != 200) {
    showMsg(handle, "", "Die Organisation kann nicht gelesen werden.");
    atlbplugin.CloseTransaction(handle);
    return(false);
  }
  sbiplg_setvar("A_ORG_ID", atlbplugin.GetQueryResultItem(handle, "ORG_ID", 0));
  sbiplg_setvar("A_ORG_Mandant", atlbplugin.GetQueryResultItem(handle, "MANDANT", 0));
  sbiplg_setvar("A_ORG_Name1", atlbplugin.GetQueryResultItem(handle, "NAME1", 0));
  sbiplg_setvar("A_ORG_Name2", atlbplugin.GetQueryResultItem(handle, "NAME2", 0));
  sbiplg_setvar("A_ORG_Name3", atlbplugin.GetQueryResultItem(handle, "NAME3", 0));
  sbiplg_setvar("A_ORG_Strasse", atlbplugin.GetQueryResultItem(handle, "STRASSE", 0));
  sbiplg_setvar("A_ORG_PLZ", atlbplugin.GetQueryResultItem(handle, "PLZ", 0));
  sbiplg_setvar("A_ORG_Ort", atlbplugin.GetQueryResultItem(handle, "ORT", 0));
  sbiplg_setvar("A_ORG_Land", atlbplugin.GetQueryResultItem(handle, "LAND", 0));
  sbiplg_setvar("A_ORG_Tel", atlbplugin.GetQueryResultItem(handle, "TEL", 0));
  sbiplg_setvar("A_ORG_Fax", atlbplugin.GetQueryResultItem(handle, "FAX", 0));
  sbiplg_setvar("A_ORG_EMail", atlbplugin.GetQueryResultItem(handle, "EMAIL", 0));
  sbiplg_setvar("A_ORG_Web", atlbplugin.GetQueryResultItem(handle, "WEB", 0)); 
  if(sbiplg.GetConfigString("CONFIG.ORGLOGO", "value", "0") == "1"){
  	sbiplg_setvar("A_ORG_TitleColor", atlbplugin.GetQueryResultItem(handle, "TITLECOLOR", 0)); //atlbplugin.GetQueryResultItem(handle, "TITLECOLOR", 0));
  	sbiplg_setvar("A_ORG_TitleBgColor", atlbplugin.GetQueryResultItem(handle, "TITLEBGCOLOR", 0)); //atlbplugin.GetQueryResultItem(handle, "TITLEBGCOLOR", 0));
  }
  sbiplg_setvar("A_ORG_Erstellt", atlbplugin.GetQueryResultItem(handle, "ERSTELLT", 0));
  sbiplg_setvar("A_ORG_Revidiert", atlbplugin.GetQueryResultItem(handle, "REVIDIERT", 0));
  atlbplugin.CloseTransaction(handle);
  return(true);
}

function DELETE_ORG() {
  var orgid = getORG_ID();
  if(orgid == "") return(false);
  var handle = atlbplugin.OpenTransaction();
  if(handle < 0) {
    showMsg(handle, NO_DB, "Die Organisation kann nicht gelöscht werden.");
    return(false);
  }
  atlbplugin.SetQueryParam(handle, "ORG_ID", orgid);
  if(atlbplugin.RunQuery(handle, "adi/delete/e_organisation") != 200) {
    showMsg(handle, "", "Die Organisation kann nicht gelöscht werden.");
    atlbplugin.CloseTransaction(handle);
    return(false);
  }
  atlbplugin.CloseTransaction(handle);
  return(true);
}

// ######################### BEA  #########################

function LISTE_BEA(sql) {
  var handle = atlbplugin.OpenTransaction();
  if(handle < 0) {
    showMsg(handle, NO_DB, "Die Liste der Bearbeiter kann nicht gezeigt werden.");
    return;
  }
  atlbplugin.SetQueryParam(handle, "order", beaOrder);
  if(sql.length > 0) atlbplugin.SetQueryParam(handle, "sql", sql);
  atlbplugin.CreateFrame(handle, "bealist.htpl", "frMain.frSM2");
  atlbplugin.CloseTransaction(handle);
}

function UPDATE_BEA() {
  var handle = atlbplugin.OpenTransaction();
  if(handle < 0) {
    showMsg(handle, NO_DB, "Der Bearbeiter kann nicht gespeichert werden.");
    return(false);
  }
  //atlbplugin.SetQueryParam(handle, "PASSWORT", compute("A_BEA_Passwort"));
  atlbplugin.SetQueryParam(handle, "NAME", compute("A_BEA_Name"));
  atlbplugin.SetQueryParam(handle, "VNAME", compute("A_BEA_Vname"));
  atlbplugin.SetQueryParam(handle, "POSITION", compute("A_BEA_Position"));
  atlbplugin.SetQueryParam(handle, "GESCHLECHT", compute("A_BEA_Geschlecht"));
  atlbplugin.SetQueryParam(handle, "STRASSE", compute("A_BEA_Strasse"));
  atlbplugin.SetQueryParam(handle, "PLZ", compute("A_BEA_PLZ"));
  atlbplugin.SetQueryParam(handle, "ORT", compute("A_BEA_Ort"));
  atlbplugin.SetQueryParam(handle, "LAND", compute("A_BEA_Land"));
  atlbplugin.SetQueryParam(handle, "TEL", compute("A_BEA_Tel"));
  atlbplugin.SetQueryParam(handle, "FAX", compute("A_BEA_Fax"));
  atlbplugin.SetQueryParam(handle, "MOBIL", compute("A_BEA_Mobil"));
  if(getBEA_ID() != "") atlbplugin.SetQueryParam(handle, "BEA_ID", getBEA_ID());
  if(atlbplugin.RunQuery(handle, "adi/insert_or_update/e_bearbeiter") != 200) {
    showMsg(handle, "", "Der Bearbeiter kann nicht gespeichert werden.");
    atlbplugin.CloseTransaction(handle);
    return(false);
  }
  setBEA_ID(getQueryData(handle));
  atlbplugin.CloseTransaction(handle);
  return(true);
}

function READ_BEA() {
  var beaid = getBEA_ID();
  if(beaid == "") return(false);
  var handle = atlbplugin.OpenTransaction();
  if(handle < 0) {
    showMsg(handle, NO_DB, "Der Bearbeiter kann nicht gelesen werden.");
    return(false);
  }
  atlbplugin.SetQueryParam(handle, "BEA_ID", beaid);
  if(atlbplugin.RunQuery(handle, "adi/query/e_bearbeiter") != 200) {
    showMsg(handle, "", "Der Bearbeiter kann nicht gelesen werden.");
    atlbplugin.CloseTransaction(handle);
    return(false);
  }
  sbiplg_setvar("A_BEA_ID", atlbplugin.GetQueryResultItem(handle, "BEA_ID", 0));
  //sbiplg_setvar("A_BEA_Passwort", atlbplugin.GetQueryResultItem(handle, "PASSWORT", 0));
  sbiplg_setvar("A_BEA_Name", atlbplugin.GetQueryResultItem(handle, "NAME", 0));
  sbiplg_setvar("A_BEA_Vname", atlbplugin.GetQueryResultItem(handle, "VNAME", 0));
  sbiplg_setvar("A_BEA_Position", atlbplugin.GetQueryResultItem(handle, "POSITION", 0));
  sbiplg_setvar("A_BEA_Geschlecht", atlbplugin.GetQueryResultItem(handle, "GESCHLECHT", 0));
  sbiplg_setvar("A_BEA_Strasse", atlbplugin.GetQueryResultItem(handle, "STRASSE", 0));
  sbiplg_setvar("A_BEA_PLZ", atlbplugin.GetQueryResultItem(handle, "PLZ", 0));
  sbiplg_setvar("A_BEA_Ort", atlbplugin.GetQueryResultItem(handle, "ORT", 0));
  sbiplg_setvar("A_BEA_Land", atlbplugin.GetQueryResultItem(handle, "LAND", 0));
  sbiplg_setvar("A_BEA_Tel", atlbplugin.GetQueryResultItem(handle, "TEL", 0));
  sbiplg_setvar("A_BEA_Fax", atlbplugin.GetQueryResultItem(handle, "FAX", 0));
  sbiplg_setvar("A_BEA_Mobil", atlbplugin.GetQueryResultItem(handle, "MOBIL", 0));
  sbiplg_setvar("A_BEA_Erstellt", atlbplugin.GetQueryResultItem(handle, "ERSTELLT", 0));
  sbiplg_setvar("A_BEA_Revidiert", atlbplugin.GetQueryResultItem(handle, "REVIDIERT", 0));
  atlbplugin.CloseTransaction(handle);
  return(true);
}

function DELETE_BEA() {
  var beaid = getBEA_ID();
  if(beaid == "") return(false);
  var handle = atlbplugin.OpenTransaction();
  if(handle < 0) {
    showMsg(handle, NO_DB, "Der Bearbeiter kann nicht gelöscht werden.");
    return(false);
  }
  atlbplugin.SetQueryParam(handle, "BEA_ID", beaid);
  if(atlbplugin.RunQuery(handle, "adi/delete/e_bearbeiter") != 200) {
    showMsg(handle, "", "Der Bearbeiter kann nicht gelöscht werden.");
    atlbplugin.CloseTransaction(handle);
    return(false);
  }
  atlbplugin.CloseTransaction(handle);
  return(true);
}

function LISTE_BEA_BORG() {
  var beaid = getBEA_ID();
  if(beaid == "") return(false);
  var handle = atlbplugin.OpenTransaction();
  if(handle < 0) {
    showMsg(handle, NO_DB, "Die Liste der Organisationen kann nicht gelesen werden.");
    return(false);
  }
  atlbplugin.SetQueryParam(handle, "BEA_ID", beaid);
  atlbplugin.SetQueryParam(handle, "order", "ORG_ID");
  if(atlbplugin.RunQuery(handle, "adi/query/e_borg") != 200) {
    showMsg(handle, "", "Die Liste der Organisationen kann nicht gelesen werden.");
    atlbplugin.CloseTransaction(handle);
    return(false);
  }
  var index = 0;
  while(atlbplugin.GetQueryResultItem(handle, "BORG_ID", index).length > 0) {
    sbiplg_setvar("A_BORG_ID["+index+"]", atlbplugin.GetQueryResultItem(handle, "BORG_ID", index));
    sbiplg_setvar("A_BORG_Org_ID["+index+"]", atlbplugin.GetQueryResultItem(handle, "ORG_ID", index));
    sbiplg_setvar("A_BORG_Rolle["+index+"]", atlbplugin.GetQueryResultItem(handle, "ROLLE", index));
    sbiplg_setvar("A_BORG_Produktion["+index+"]", atlbplugin.GetQueryResultItem(handle, "PRODUKTION", index));
    sbiplg_setvar("A_BORG_Produktion2["+index+"]", atlbplugin.GetQueryResultItem(handle, "PRODUKTION2", index));
    sbiplg_setvar("A_BORG_Produktion3["+index+"]", atlbplugin.GetQueryResultItem(handle, "PRODUKTION3", index));
    sbiplg_setvar("A_BORG_Produktion4["+index+"]", atlbplugin.GetQueryResultItem(handle, "PRODUKTION4", index));
    sbiplg_setvar("A_BORG_Email["+index+"]", atlbplugin.GetQueryResultItem(handle, "EMAIL", index));
    sbiplg_setvar("A_BORG_Erstellt["+index+"]", atlbplugin.GetQueryResultItem(handle, "ERSTELLT", index));
    sbiplg_setvar("A_BORG_Revidiert["+index+"]", atlbplugin.GetQueryResultItem(handle, "REVIDIERT", index));
    index++;
  }
  sbiplg_setvar("A_BORGi", index);
  atlbplugin.CloseTransaction(handle);
  return(true);
}

function LISTE_BEA_ORG() {
  var handle = atlbplugin.OpenTransaction();
  if(handle < 0) {
    showMsg(handle, NO_DB, "Die Organisation kann nicht gelesen werden.");
    return(false);
  }
  atlbplugin.SetQueryParam(handle, "order", "ORG_ID");
  if(atlbplugin.RunQuery(handle, "adi/query/e_organisation") != 200) {
    showMsg(handle, "", "Die Organisation kann nicht gelesen werden.");
    atlbplugin.CloseTransaction(handle);
    return(false);
  }
  var index = 0;
  while(atlbplugin.GetQueryResultItem(handle, "ORG_ID", index).length > 0) {
    var ind = compute('F_AktuellerIndexMandant("'+atlbplugin.GetQueryResultItem(handle, "MANDANT", index)+'")');
    if(parseInt(ind) >= 0) {
      var ind2 = compute("A_ORGi2["+ind+"]");
      sbiplg_setvar("A_ORG_ID["+ind+";"+ind2+"]", atlbplugin.GetQueryResultItem(handle, "ORG_ID", index));
      sbiplg_setvar("A_ORG_Mandant["+ind+";"+ind2+"]", atlbplugin.GetQueryResultItem(handle, "MANDANT", index));
      sbiplg_setvar("A_ORG_Name1["+ind+";"+ind2+"]", atlbplugin.GetQueryResultItem(handle, "NAME1", index));
      sbiplg_setvar("A_ORG_Name2["+ind+";"+ind2+"]", atlbplugin.GetQueryResultItem(handle, "NAME2", index));
      sbiplg_setvar("A_ORG_Name3["+ind+";"+ind2+"]", atlbplugin.GetQueryResultItem(handle, "NAME3", index));
      sbiplg_setvar("A_ORG_Strasse["+ind+";"+ind2+"]", atlbplugin.GetQueryResultItem(handle, "STRASSE", index));
      sbiplg_setvar("A_ORG_PLZ["+ind+";"+ind2+"]", atlbplugin.GetQueryResultItem(handle, "PLZ", index));
      sbiplg_setvar("A_ORG_Ort["+ind+";"+ind2+"]", atlbplugin.GetQueryResultItem(handle, "ORT", index));
      sbiplg_setvar("A_ORG_Land["+ind+";"+ind2+"]", atlbplugin.GetQueryResultItem(handle, "LAND", index));
      sbiplg_setvar("A_ORG_Tel["+ind+";"+ind2+"]", atlbplugin.GetQueryResultItem(handle, "TEL", index));
      sbiplg_setvar("A_ORG_Fax["+ind+";"+ind2+"]", atlbplugin.GetQueryResultItem(handle, "FAX", index));
      sbiplg_setvar("A_ORG_EMail["+ind+";"+ind2+"]", atlbplugin.GetQueryResultItem(handle, "EMAIL", index));
      sbiplg_setvar("A_ORG_Web["+ind+";"+ind2+"]", atlbplugin.GetQueryResultItem(handle, "WEB", index));
      if(sbiplg.GetConfigString("CONFIG.ORGLOGO", "value", "0") == "1"){
			  sbiplg_setvar("A_ORG_TitleColor["+ind+";"+ind2+"]", atlbplugin.GetQueryResultItem(handle, "TITLECOLOR", index));
			  sbiplg_setvar("A_ORG_TitleBgColor["+ind+";"+ind2+"]", atlbplugin.GetQueryResultItem(handle, "TITLEBGCOLOR", index));
      }
      
      sbiplg_setvar("A_ORG_Erstellt["+ind+";"+ind2+"]", atlbplugin.GetQueryResultItem(handle, "ERSTELLT", index));
      sbiplg_setvar("A_ORG_Revidiert["+ind+";"+ind2+"]", atlbplugin.GetQueryResultItem(handle, "REVIDIERT", index));
      sbiplg_setvar("A_ORGi2["+ind+"]", String(parseInt(ind2)+1));
    }
    index++;
  }
  atlbplugin.CloseTransaction(handle);
  return(true);
}

function UPDATE_BORG(index) {
  var beaid = getBEA_ID();
  if(beaid == "") return(false);
  var handle = atlbplugin.OpenTransaction();
  if(handle < 0) {
    showMsg(handle, NO_DB, "Die Organisation kann nicht gespeichert werden.");
    return(false);
  }
  atlbplugin.SetQueryParam(handle, "BEA_ID", beaid);
  atlbplugin.SetQueryParam(handle, "ORG_ID", compute("A_BORG_Org_ID["+index+"]"));
  atlbplugin.SetQueryParam(handle, "BORG_ID", compute("A_BORG_ID["+index+"]"));
  atlbplugin.SetQueryParam(handle, "ROLLE", compute("A_BORG_Rolle["+index+"]"));
  atlbplugin.SetQueryParam(handle, "PRODUKTION", compute("A_BORG_Produktion["+index+"]"));
  atlbplugin.SetQueryParam(handle, "PRODUKTION2", compute("A_BORG_Produktion2["+index+"]"));
  atlbplugin.SetQueryParam(handle, "PRODUKTION3", compute("A_BORG_Produktion3["+index+"]"));
  atlbplugin.SetQueryParam(handle, "PRODUKTION4", compute("A_BORG_Produktion4["+index+"]"));
  atlbplugin.SetQueryParam(handle, "EMAIL", compute("A_BORG_EMail["+index+"]"));
  if(atlbplugin.RunQuery(handle, "adi/insert_or_update/e_borg") != 200) {
    showMsg(handle, "", "Die Organisation kann nicht gespeichert werden.");
    atlbplugin.CloseTransaction(handle);
    return(false);
  }
  atlbplugin.CloseTransaction(handle);
  return(true);
}

function DELETE_BORG(index) {
  var borgid = compute("A_BORG_ID["+index+"]");
  if(borgid == "") return(false);
  var handle = atlbplugin.OpenTransaction();
  if(handle < 0) {
    showMsg(handle, NO_DB, "Die Organisation kann nicht gelöscht werden.");
    return(false);
  }
  atlbplugin.SetQueryParam(handle, "BORG_ID", borgid);
  if(atlbplugin.RunQuery(handle, "adi/delete/e_borg") != 200) {
    showMsg(handle, "", "Die Organisation kann nicht gelöscht werden.");
    atlbplugin.CloseTransaction(handle);
    return(false);
  }
  atlbplugin.CloseTransaction(handle);
  return(true);
}

// ######################### BORG  #########################

function READ_BORG() {
  var orgid = getBORG_ID();
  if(orgid == "") return(false);
  var handle = atlbplugin.OpenTransaction();
  if(handle < 0) {
    showMsg(handle, NO_DB, "Die Organisation kann nicht gelesen werden.");
    return(false);
  }
  atlbplugin.SetQueryParam(handle, "BORG_ID", orgid);
  if(atlbplugin.RunQuery(handle, "adi/query/e_borg") != 200) {
    showMsg(handle, "", "Die Organisation kann nicht gelesen werden.");
    atlbplugin.CloseTransaction(handle);
    return(false);
  }
  sbiplg_setvar("A_BORG_ID", atlbplugin.GetQueryResultItem(handle, "BORG_ID", 0));
  sbiplg_setvar("A_BORG_Org_ID", atlbplugin.GetQueryResultItem(handle, "ORG_ID", 0));
  sbiplg_setvar("A_BORG_Rolle", atlbplugin.GetQueryResultItem(handle, "ROLLE", 0));
  sbiplg_setvar("A_BORG_Produktion", atlbplugin.GetQueryResultItem(handle, "PRODUKTION", 0));
  sbiplg_setvar("A_BORG_Produktion2", atlbplugin.GetQueryResultItem(handle, "PRODUKTION2", 0));
  sbiplg_setvar("A_BORG_Produktion3", atlbplugin.GetQueryResultItem(handle, "PRODUKTION3", 0));
  sbiplg_setvar("A_BORG_Produktion4", atlbplugin.GetQueryResultItem(handle, "PRODUKTION4", 0));
  sbiplg_setvar("A_BORG_Email", atlbplugin.GetQueryResultItem(handle, "EMAIL", 0));
  sbiplg_setvar("A_BORG_Erstellt", atlbplugin.GetQueryResultItem(handle, "ERSTELLT", 0));
  sbiplg_setvar("A_BORG_Revidiert", atlbplugin.GetQueryResultItem(handle, "REVIDIERT", 0));
  atlbplugin.CloseTransaction(handle);
  return(true);
}

// ######################### BEA ORG BORG  ################

function READ_BEAORGBORG() {
  var borgid = getBORG_ID();
  if(borgid == "") return(false);
  var handle = atlbplugin.OpenTransaction();
  if(handle < 0) {
    showMsg(handle, NO_DB, "Die Organisationsdaten können nicht gelesen werden.");
    return(null);
  }
  atlbplugin.SetQueryParam(handle, "P_BORG", borgid);
  if(atlbplugin.RunQuery(handle, "adi/query/e_q_borg_view") != 200) {
    showMsg(handle, "", "Die Organisationsdaten können nicht gelesen werden.");
    atlbplugin.CloseTransaction(handle);
    return(null);
  }
  setBORG_ID(borgid);
  sbiplg_setvar("A_BEA_ID", atlbplugin.GetQueryResultItem(handle, "BEA_ID", 0));
  sbiplg_setvar("A_BEA_VNAME", atlbplugin.GetQueryResultItem(handle, "BEA_VNAME", 0));
  sbiplg_setvar("A_BEA_NAME", atlbplugin.GetQueryResultItem(handle, "BEA_NAME", 0));
  sbiplg_setvar("A_BEA_POSITION", atlbplugin.GetQueryResultItem(handle, "BEA_POSITION", 0));
  sbiplg_setvar("A_BEA_GESCHLECHT", atlbplugin.GetQueryResultItem(handle, "BEA_GESCHLECHT", 0));
  sbiplg_setvar("A_BEA_STRASSE", atlbplugin.GetQueryResultItem(handle, "BEA_STRASSE", 0));
  sbiplg_setvar("A_BEA_PLZ", atlbplugin.GetQueryResultItem(handle, "BEA_PLZ", 0));
  sbiplg_setvar("A_BEA_ORT", atlbplugin.GetQueryResultItem(handle, "BEA_ORT", 0));
  sbiplg_setvar("A_BEA_LAND", atlbplugin.GetQueryResultItem(handle, "BEA_LAND", 0));
  sbiplg_setvar("A_BEA_TEL", atlbplugin.GetQueryResultItem(handle, "BEA_TEL", 0));
  sbiplg_setvar("A_BEA_MOBIL", atlbplugin.GetQueryResultItem(handle, "BEA_MOBIL", 0));
  sbiplg_setvar("A_BEA_FAX", atlbplugin.GetQueryResultItem(handle, "BEA_FAX", 0));
  sbiplg_setvar("A_ORG_ID", atlbplugin.GetQueryResultItem(handle, "ORG_ID", 0));
  sbiplg_setvar("A_ORG_Mandant", atlbplugin.GetQueryResultItem(handle, "ORG_MANDANT", 0));
  sbiplg_setvar("A_ORG_NAME1", atlbplugin.GetQueryResultItem(handle, "ORG_NAME1", 0));
  sbiplg_setvar("A_ORG_NAME2", atlbplugin.GetQueryResultItem(handle, "ORG_NAME2", 0));
  sbiplg_setvar("A_ORG_NAME3", atlbplugin.GetQueryResultItem(handle, "ORG_NAME3", 0));
  sbiplg_setvar("A_ORG_STRASSE", atlbplugin.GetQueryResultItem(handle, "ORG_STRASSE", 0));
  sbiplg_setvar("A_ORG_PLZ", atlbplugin.GetQueryResultItem(handle, "ORG_PLZ", 0));
  sbiplg_setvar("A_ORG_ORT", atlbplugin.GetQueryResultItem(handle, "ORG_ORT", 0));
  sbiplg_setvar("A_ORG_LAND", atlbplugin.GetQueryResultItem(handle, "ORG_LAND", 0));
  sbiplg_setvar("A_ORG_TEL", atlbplugin.GetQueryResultItem(handle, "ORG_TEL", 0));
  sbiplg_setvar("A_ORG_FAX", atlbplugin.GetQueryResultItem(handle, "ORG_FAX", 0));
  sbiplg_setvar("A_ORG_EMAIL", atlbplugin.GetQueryResultItem(handle, "ORG_EMAIL", 0));
  sbiplg_setvar("A_ORG_WEB", atlbplugin.GetQueryResultItem(handle, "ORG_WEB", 0));
  sbiplg_setvar("A_BORG_ID", atlbplugin.GetQueryResultItem(handle, "BORG_ID", 0));
  sbiplg_setvar("A_BORG_Org_ID", atlbplugin.GetQueryResultItem(handle, "ORG_ID", 0));
  sbiplg_setvar("A_BORG_ROLLE", atlbplugin.GetQueryResultItem(handle, "BORG_ROLLE", 0));
  sbiplg_setvar("A_BORG_PRODUKTION", atlbplugin.GetQueryResultItem(handle, "BORG_PRODUKTION", 0));
  sbiplg_setvar("A_BORG_PRODUKTION2", atlbplugin.GetQueryResultItem(handle, "BORG_PRODUKTION2", 0));
  sbiplg_setvar("A_BORG_PRODUKTION3", atlbplugin.GetQueryResultItem(handle, "BORG_PRODUKTION3", 0));
  sbiplg_setvar("A_BORG_PRODUKTION4", atlbplugin.GetQueryResultItem(handle, "BORG_PRODUKTION4", 0));
  sbiplg_setvar("A_BORG_EMAIL", atlbplugin.GetQueryResultItem(handle, "BORG_EMAIL", 0));
  atlbplugin.CloseTransaction(handle);
  return(true);
}

// ######################### PER  #########################

function LISTE_ADMIN_PER(sql) {
  var handle = atlbplugin.OpenTransaction();
  if(handle < 0) {
    showMsg(handle, NO_DB, "Die Liste der Partner kann nicht gezeigt werden.");
    return(false);
  }
  atlbplugin.SetQueryParam(handle, "order", perOrder);
  if(sql.length > 0) atlbplugin.SetQueryParam(handle, "sql", sql);
  atlbplugin.CreateFrame(handle, "perlist.htpl", "frMain.frSM2");
  atlbplugin.CloseTransaction(handle);
}

function LISTE_PER(sql) {
  var eigeneBorg = compute("A_BASE_EigeneBorg");
  var phonetisch = compute("A_BASE_Phonetisch");
  var pers = new Array();
  initBlob();
  sbiplg_setvar("A_BASE_EigeneBorg", eigeneBorg);
  sbiplg_setvar("A_BASE_Phonetisch", phonetisch);

  if(getBORG_ID() == "" || getBEA_ID() == "" || getORG_ID() == "") return(false);
  var borgid = getBORG_ID();
  if(allBorg()) {
    var s = "";
    var viewLevel = compute("A_BASE_ViewLevel");
    if(viewLevel == "allBEAallOrgs")
      s = "%";
    else if(viewLevel == "myBEAallOrgs")
      s = "%_" + getBEA_ID();
    else if(viewLevel == "allBEAmyOrg") 
      s = getORG_ID() + "_%";
    
    if(s.length > 0) {
      borgid = "";
      if(sql.length > 0) sql = sql + " AND ";
      sql = sql + "BORG_ID LIKE '" + s + "'";
    }
  }
  
  var handle = atlbplugin.OpenTransaction();
  if(handle < 0) {
    showMsg(handle, NO_DB, "Die Liste der Partner kann nicht gezeigt werden.");
    return(false);
  }
  if(borgid != "") atlbplugin.SetQueryParam(handle, "P_BORG", borgid);
 
  
  atlbplugin.SetQueryParam(handle, "@ORDER", perOrder);
  atlbplugin.SetQueryParam(handle, "ADI_MAXCOUNT", maxCount);
  if(sql.length > 0) atlbplugin.SetQueryParam(handle, "P_SQL", sql);
  
  if(atlbplugin.RunQuery(handle, "adi/query/e_q_per_view") != 200) {
    showMsg(handle, "", "Die Partnerdaten können nicht gelesen werden.");
    atlbplugin.CloseTransaction(handle);
    return("400");
  }
  var i = 0;
  while(atlbplugin.GetQueryResultItem(handle, "PER_ID", i).length > 0) {
    pers[i] = new Array();
    pers[i][0] = atlbplugin.GetQueryResultItem(handle, "PER_ID", i);
    pers[i][1] = atlbplugin.GetQueryResultItem(handle, "PER_TYPE", i); 
    pers[i][2] = atlbplugin.GetQueryResultItem(handle, "PER_NAME", i);
    pers[i][3] = atlbplugin.GetQueryResultItem(handle, "PER_VNAME", i);
    pers[i][4] = atlbplugin.GetQueryResultItem(handle, "PER_PLZ", i);
    pers[i][5] = atlbplugin.GetQueryResultItem(handle, "PER_ORT", i);
    pers[i][6] = atlbplugin.GetQueryResultItem(handle, "PER_REVIDIERT", i);   
    i++; 
  }
  atlbplugin.CloseTransaction(handle);
  
  var table = frMain.frSM2.document.all.item("tableSelection");
  var tbody = frMain.frSM2.document.all.item("tableSelectionBody");
  var clonedTrTitle = tbody.firstChild.cloneNode(true);
   
  while(table.rows.length > 0)
    table.deleteRow(0);
    
  tbody.appendChild(clonedTrTitle);
    
  if(pers.length==0){ 
    showMessage(translate(this, "Keine Eintr&auml;ge gefunden, bitte Suchkriterien &uuml;berpr&uuml;fen!"));
    return;
  }
  var numOfResults = pers.length;
  if(maxCount > 0 && maxCount < numOfResults) numOfResults = maxCount;
  
  for (i=0;i<numOfResults;i++) {
    var newTR = frMain.frSM2.document.createElement("tr");
    var row = tbody.appendChild(newTR);
    if(pers[i]!= null){
      row.setAttribute("id",pers[i][0]);
      for (var j=0;j<pers[i].length-1;j++){
        var cell = row.insertCell(j);
        var cls="";
        if(j==0){
          if(pers[i][j+1] == "p")
            cls="tab_btn_p";
          else if(pers[i][j+1] == "f")
            cls="tab_btn_f";
          else if(pers[i][j+1] == "s")
            cls="tab_btn_s";
        }else{ 
          cls="tab_data";
          cell.innerHTML = pers[i][j+1];
        }
      cell.setAttribute("className",cls);
      }
    }
  }
  frMain.frSM2.reInitTable(false);
}

function UPDATE_PER_AS_BLOB(perdata) {
  var perid = getPER_ID();
  var handle = atlbplugin.OpenTransaction();
  if(handle < 0) {
    showMsg(handle, NO_DB, "Die Partnerdaten können nicht gespeichert werden.");
    return(false);
  }
  atlbplugin.SetQueryParam(handle, "BORG_ID", compute("A_PER_BORG_ID"));
  atlbplugin.SetQueryParam(handle, "TYPE", compute("A_PER_Type[0]"));
  atlbplugin.SetQueryParam(handle, "EXTERN_ID", compute("A_PER_Extern_ID[0]"));
  atlbplugin.SetQueryParam(handle, "ANREDE", compute("A_PER_Anrede[0]"));
  atlbplugin.SetQueryParam(handle, "TITEL", compute("A_PER_Titel[0]"));
  atlbplugin.SetQueryParam(handle, "VNAME", compute("A_PER_VName[0]"));
  atlbplugin.SetQueryParam(handle, "NAME", compute("A_PER_Name[0]"));
  atlbplugin.SetQueryParam(handle, "GESCHLECHT", compute("A_PER_Geschlecht[0]"));
  atlbplugin.SetQueryParam(handle, "GEBDAT", compute("A_PER_Gebdat[0]"));
  atlbplugin.SetQueryParam(handle, "NATIONALITAET", compute("A_PER_Nationalitaet[0]"));
  atlbplugin.SetQueryParam(handle, "ZIVILSTAND", compute("A_PER_Zivilstand[0]"));
  atlbplugin.SetQueryParam(handle, "STATUS", compute("A_PER_Status[0]"));
  atlbplugin.SetQueryParam(handle, "STRASSE", compute("A_PER_Strasse[0]"));
  atlbplugin.SetQueryParam(handle, "POSTFACH", compute("A_PER_Postfach[0]"));
  atlbplugin.SetQueryParam(handle, "PLZ", compute("A_PER_PLZ[0]"));
  atlbplugin.SetQueryParam(handle, "ORT", compute("A_PER_Ort[0]"));
  atlbplugin.SetQueryParam(handle, "LAND", compute("A_PER_Land[0]"));
  atlbplugin.SetQueryParam(handle, "TEL", compute("A_PER_Tel[0]"));
  atlbplugin.SetQueryParam(handle, "MOBIL", compute("A_PER_Mobil[0]"));
  atlbplugin.SetQueryParam(handle, "FAX", compute("A_PER_Fax[0]"));
  atlbplugin.SetQueryParam(handle, "EMAIL", compute("A_PER_EMail[0]"));
  atlbplugin.SetQueryParam(handle, "PRIVAT", compute("A_PER_Privat[0]"));
  atlbplugin.SetQueryParam(handle, "KONTAKT", compute("A_PER_Kontakt[0]"));
  if(perid != "") atlbplugin.SetQueryParam(handle, "PER_ID", perid);
  if(perdata != "") atlbplugin.SetQueryParam(handle, "DATA", perdata);
  if(atlbplugin.RunQuery(handle, "adi/insert_or_update/e_person") != 200) {
    showMsg(handle, "", "Die Partnerdaten können nicht gespeichert werden.");
    atlbplugin.CloseTransaction(handle);
    return(false);
  }
  setPER_ID(getQueryData(handle));
  atlbplugin.CloseTransaction(handle);
  return(true);
}

function UPDATE_PER() {
  var perid = getPER_ID();
  if (!isAllowed((perid != "" ? "kv_save" : "kv_new"))) return false;
  var handle = atlbplugin.OpenTransaction();
  if(handle < 0) {
    showMsg(handle, NO_DB, "Die Partnerdaten können nicht gespeichert werden.");
    return(false);
  }
  if(compute("A_PER_BORG_ID") == "") sbiplg_setvar("A_PER_BORG_ID", getBORG_ID());
  atlbplugin.SetQueryParam(handle, "BORG_ID", compute("A_PER_BORG_ID"));
  atlbplugin.SetQueryParam(handle, "TYPE", compute("A_PER_Type[0]"));
  atlbplugin.SetQueryParam(handle, "EXTERN_ID", compute("A_PER_Extern_ID[0]"));
  atlbplugin.SetQueryParam(handle, "ANREDE", compute("A_PER_Anrede[0]"));
  atlbplugin.SetQueryParam(handle, "TITEL", compute("A_PER_Titel[0]"));
  atlbplugin.SetQueryParam(handle, "VNAME", compute("A_PER_VName[0]"));
  atlbplugin.SetQueryParam(handle, "NAME", compute("A_PER_Name[0]"));
  atlbplugin.SetQueryParam(handle, "GESCHLECHT", compute("A_PER_Geschlecht[0]"));
  atlbplugin.SetQueryParam(handle, "GEBDAT", compute("A_PER_Gebdat[0]"));
  atlbplugin.SetQueryParam(handle, "NATIONALITAET", compute("A_PER_Nationalitaet[0]"));
  atlbplugin.SetQueryParam(handle, "ZIVILSTAND", compute("A_PER_Zivilstand[0]"));
  atlbplugin.SetQueryParam(handle, "STATUS", compute("A_PER_Status[0]"));
  atlbplugin.SetQueryParam(handle, "STRASSE", compute("A_PER_Strasse[0]"));
  atlbplugin.SetQueryParam(handle, "POSTFACH", compute("A_PER_Postfach[0]"));
  atlbplugin.SetQueryParam(handle, "PLZ", compute("A_PER_PLZ[0]"));
  atlbplugin.SetQueryParam(handle, "ORT", compute("A_PER_Ort[0]"));
  atlbplugin.SetQueryParam(handle, "LAND", compute("A_PER_Land[0]"));
  atlbplugin.SetQueryParam(handle, "TEL", compute("A_PER_Tel[0]"));
  atlbplugin.SetQueryParam(handle, "MOBIL", compute("A_PER_Mobil[0]"));
  atlbplugin.SetQueryParam(handle, "FAX", compute("A_PER_Fax[0]"));
  atlbplugin.SetQueryParam(handle, "EMAIL", compute("A_PER_EMail[0]"));
  atlbplugin.SetQueryParam(handle, "PRIVAT", compute("A_PER_Privat[0]"));
  atlbplugin.SetQueryParam(handle, "KONTAKT", compute("A_PER_Kontakt[0]"));
  if(perid != "") atlbplugin.SetQueryParam(handle, "PER_ID", perid);
  sbiplg.storexmlsession();
  atlbplugin.SetQueryParam(handle, "DATA", sbiplg.base64data);
  if(atlbplugin.RunQuery(handle, "adi/insert_or_update/e_person") != 200) {
    showMsg(handle, "", "Die Partnerdaten können nicht gespeichert werden.");
    atlbplugin.CloseTransaction(handle);
    return(false);
  }
  setPER_ID(getQueryData(handle));
  atlbplugin.CloseTransaction(handle);
  if(perBlob != "") saveBlob();
  return(true);
}

function READ_PER_AS_BLOB() {
  var perid = getPER_ID();
  if(perid == "") return("");
  var handle = atlbplugin.OpenTransaction();
  if(handle < 0) {
    showMsg(handle, NO_DB, "Die Partnerdaten können nicht gelesen werden.");
    return("");
  }
  atlbplugin.SetQueryParam(handle, "PER_ID", perid);
  if(atlbplugin.RunQuery(handle, "adi/query/e_person") != 200) {
    showMsg(handle, "", "Die Partnerdaten können nicht gelesen werden.");
    atlbplugin.CloseTransaction(handle);
    return("");
  }

  sbiplg_setvar("A_PER_BORG_ID[0]", atlbplugin.GetQueryResultItem(handle, "BORG_ID", 0));
  sbiplg_setvar("A_PER_Type[0]", atlbplugin.GetQueryResultItem(handle, "TYPE", 0));
  sbiplg_setvar("A_PER_Extern_ID[0]", atlbplugin.GetQueryResultItem(handle, "EXTERN_ID", 0));
  sbiplg_setvar("A_PER_Anrede[0]", atlbplugin.GetQueryResultItem(handle, "ANREDE", 0));
  sbiplg_setvar("A_PER_Titel[0]", atlbplugin.GetQueryResultItem(handle, "TITEL", 0));
  sbiplg_setvar("A_PER_VName[0]", atlbplugin.GetQueryResultItem(handle, "VNAME", 0));
  sbiplg_setvar("A_PER_Name[0]", atlbplugin.GetQueryResultItem(handle, "NAME", 0));
  sbiplg_setvar("A_PER_Geschlecht[0]", atlbplugin.GetQueryResultItem(handle, "GESCHLECHT", 0));
  sbiplg_setvar("A_PER_Gebdat[0]", atlbplugin.GetQueryResultItem(handle, "GEBDAT", 0));
  sbiplg_setvar("A_PER_Nationalitaet[0]", atlbplugin.GetQueryResultItem(handle, "NATIONALITAET", 0));
  sbiplg_setvar("A_PER_Zivilstand[0]", atlbplugin.GetQueryResultItem(handle, "ZIVILSTAND", 0));
  sbiplg_setvar("A_PER_Status[0]", atlbplugin.GetQueryResultItem(handle, "STATUS", 0));
  sbiplg_setvar("A_PER_Strasse[0]", atlbplugin.GetQueryResultItem(handle, "STRASSE", 0));
  sbiplg_setvar("A_PER_Postfach[0]", atlbplugin.GetQueryResultItem(handle, "POSTFACH", 0));
  sbiplg_setvar("A_PER_PLZ[0]", atlbplugin.GetQueryResultItem(handle, "PLZ", 0));
  sbiplg_setvar("A_PER_Ort[0]", atlbplugin.GetQueryResultItem(handle, "ORT", 0));
  sbiplg_setvar("A_PER_Land[0]", atlbplugin.GetQueryResultItem(handle, "LAND", 0));
  sbiplg_setvar("A_PER_Tel[0]", atlbplugin.GetQueryResultItem(handle, "TEL", 0));
  sbiplg_setvar("A_PER_Mobil[0]", atlbplugin.GetQueryResultItem(handle, "MOBIL", 0));
  sbiplg_setvar("A_PER_Fax[0]", atlbplugin.GetQueryResultItem(handle, "FAX", 0));
  sbiplg_setvar("A_PER_EMail[0]", atlbplugin.GetQueryResultItem(handle, "EMAIL", 0));
  sbiplg_setvar("A_PER_Privat[0]", atlbplugin.GetQueryResultItem(handle, "PRIVAT", 0));
  sbiplg_setvar("A_PER_Kontakt[0]", atlbplugin.GetQueryResultItem(handle, "KONTAKT", 0));
  sbiplg_setvar("A_PER_Erstellt[0]", atlbplugin.GetQueryResultItem(handle, "ERSTELLT", 0));
  sbiplg_setvar("A_PER_Revidiert[0]", atlbplugin.GetQueryResultItem(handle, "REVIDIERT", 0));
  var perdata = atlbplugin.GetQueryResultItem(handle, "DATA", 0);
  atlbplugin.CloseTransaction(handle);

  return(perdata);
}

function READ_PER(readVorCount) {
  var perid = getPER_ID();
  if(perid == "") return(false);
  var handle = atlbplugin.OpenTransaction();
  if(handle < 0) {
    showMsg(handle, NO_DB, "Die Partnerdaten können nicht gelesen werden.");
    return(false);
  }
  atlbplugin.SetQueryParam(handle, "PER_ID", perid);
  var rc = atlbplugin.RunQuery(handle, "adi/query/e_person");
  if(rc == 500) {
    var msg = atlbplugin.GetQueryData(handle);
    showMsg(-1, "", msg);
    atlbplugin.CloseTransaction(handle);
    closeWindow();
    return(false);
  }
  if(rc != 200) {
    showMsg(handle, "", "Die Partnerdaten können nicht gelesen werden.");
    atlbplugin.CloseTransaction(handle);
    return(false);
  }

  initBlob(); // wird alles gelöscht und session mit NUR loginBlob befüllt (BEA,ORG,BORG)
  var rolle = getRolle();
  // falls blob leer ist, wird eins mit DB-Felder erstellt und gespeichert.
  if((atlbplugin.GetQueryResultItem(handle, "DATA", 0)).length == 0) {
    sbiplg_debugPrintLine("JS: db.js READ_PER(): No DATA (BLOB) found in database");
    setVarsEmptyPer(handle);
    UPDATE_PER();
    atlbplugin.RunQuery(handle, "adi/query/e_person");
    sbiplg_debugPrintLine("JS: db.js READ_PER(): DATA(BLOB) updated to database");

  } else {
    setModelData(atlbplugin.GetQueryResultItem(handle, "DATA", 0));
  }
  setModelDataEx(atlbplugin.GetQueryResultItem(handle, "DATA2", 0));
  // zum sicherheit gesetzt obwohl gleich ist (Feld "DATA"). Attribut A_PER_BORG_ID sagt auch wohin Partner anhängen muss (UPDATE_PER)
  sbiplg_setvar("A_PER_BORG_ID", atlbplugin.GetQueryResultItem(handle, "BORG_ID", 0));

  if (config_refreshbeadata()) {
    setModelData(loginBlob);
  } else {
    setRolle(rolle);
  }

  sbiplg_setvar("A_PER_Erstellt[0]", atlbplugin.GetQueryResultItem(handle, "ERSTELLT", 0));
  sbiplg_setvar("A_PER_Revidiert[0]", atlbplugin.GetQueryResultItem(handle, "REVIDIERT", 0));
  setPER_ID(perid);
  atlbplugin.CloseTransaction(handle);

  if(readVorCount) READ_VOR_COUNT();
  if(perBlob != "") saveBlob();
  //sbiplg_writeXmlData("c:/temp/byRead.xml");
  return(true);
}

function DELETE_PER() {
  var perid = getPER_ID();
  if(perid == "") return(false);
  var handle = atlbplugin.OpenTransaction();
  if(handle < 0) {
    showMsg(handle, NO_DB, "Die Partnerdaten können nicht gelöscht werden.");
    atlbplugin.CloseTransaction(handle);
    return(false);
  }
  atlbplugin.SetQueryParam(handle, "PER_ID", perid);
  if(atlbplugin.RunQuery(handle, "adi/delete/e_person") != 200) {
    atlbplugin.CloseTransaction(handle);
    showMsg(handle, "", "Die Partnerdaten können nicht gelöscht werden.");
    return(false);
  }
  atlbplugin.CloseTransaction(handle);
  return(true);
}

// ######################### VOR  #########################

function LISTE_VOR() {
  var perid = getPER_ID();
  if(perid == "") return(false);
  var handle = atlbplugin.OpenTransaction();
  if(handle < 0) {
    showMsg(handle, NO_DB, "Die Liste der Vorgänge kann nicht gezeigt werden.");
    return(false);
  }
  atlbplugin.SetQueryParam(handle, "PER_ID", perid);
  atlbplugin.SetQueryParam(handle, "order", vorOrder);
  atlbplugin.CreateFrame(handle, "vvlist.htpl", "frMain.frSM2");
  atlbplugin.CloseTransaction(handle);
}

function UPDATE_VOR() {
  var handle = atlbplugin.OpenTransaction();
  if(handle < 0) {
    showMsg(handle, NO_DB, "Der Vorgang kann nicht gespeichert werden.");
    return(false);
  }
  atlbplugin.SetQueryParam(handle, "BSTEIN_ID", compute("A_VOR_Bstein_ID"));
  atlbplugin.SetQueryParam(handle, "VERSION", compute("A_VOR_Version"));
  atlbplugin.SetQueryParam(handle, "PER_ID", getPER_ID());
  atlbplugin.SetQueryParam(handle, "VORGANG", VORGANG);
  atlbplugin.SetQueryParam(handle, "KOMMENTAR", delCR(compute("A_VOR_Kommentar")));
  var mode = "insert_or_update";
  if(getVOR_ID() != "") {
    atlbplugin.SetQueryParam(handle, "VOR_ID", getVOR_ID());
    mode = "update";
  }
  if(atlbplugin.RunQuery(handle, "adi/" + mode + "/e_vorgang") != 200) {
    showMsg(handle, "", "Der Vorgang kann nicht gespeichert werden.");
    atlbplugin.CloseTransaction(handle);
    return(false);
  }
  
  setVOR_ID(getQueryData(handle));
  atlbplugin.CloseTransaction(handle);
  return(true);
}

function READ_VOR() {
  var vorid = getVOR_ID();
  if(vorid == "") return(false);
  var handle = atlbplugin.OpenTransaction();
  if(handle < 0) {
    showMsg(handle, NO_DB, "Der Vorgang kann nicht gelesen werden.");
    return(false);
  }
  atlbplugin.SetQueryParam(handle, "VOR_ID", vorid);
  var rc = atlbplugin.RunQuery(handle, "adi/query/e_vorgang");
  if(rc == 500) {
    var msg = atlbplugin.GetQueryData(handle);
    showMsg(-1, "", msg);
    atlbplugin.CloseTransaction(handle);
    closeWindow();
    return(false);
  }
  if(rc != 200) {
    showMsg(handle, "", "Der Vorgang kann nicht gelesen werden.");
    atlbplugin.CloseTransaction(handle);
    return(false);
  }
  VORGANG = atlbplugin.GetQueryResultItem(handle, "VORGANG", 0);
  sbiplg_setvar("A_VOR_ID[0]", atlbplugin.GetQueryResultItem(handle, "VOR_ID", 0));
  sbiplg_setvar("A_VOR_Bstein_ID[0]", atlbplugin.GetQueryResultItem(handle, "BSTEIN_ID", 0));
  sbiplg_setvar("A_VOR_Version[0]", atlbplugin.GetQueryResultItem(handle, "VERSION", 0));
  sbiplg_setvar("A_VOR_Kommentar[0]", addCR(atlbplugin.GetQueryResultItem(handle, "KOMMENTAR", 0)));
  sbiplg_setvar("A_VOR_Erstellt[0]", atlbplugin.GetQueryResultItem(handle, "ERSTELLT", 0));
  sbiplg_setvar("A_VOR_Revidiert[0]", atlbplugin.GetQueryResultItem(handle, "REVIDIERT", 0));
  atlbplugin.CloseTransaction(handle);
  return(true);
}

function DELETE_VOR() {
  var vorid = getVOR_ID();
  if(vorid == "") return(false);
  var handle = atlbplugin.OpenTransaction();
  if(handle < 0) {
    showMsg(handle, NO_DB, "Der Vorgang kann nicht gelöscht werden.");
    return(false);
  }
  atlbplugin.SetQueryParam(handle, "VOR_ID", vorid);
  if(atlbplugin.RunQuery(handle, "adi/delete/e_vorgang") != 200) {
    showMsg(handle, "", "Der Vorgang kann nicht gelöscht werden.");
    atlbplugin.CloseTransaction(handle);
    return(false);
  }
  atlbplugin.CloseTransaction(handle);
  return(true);
}

function READ_VOR_COUNT() {
  var perid = getPER_ID();
  if(perid == "") return(false);
  var handle = atlbplugin.OpenTransaction();
  if(handle < 0) {
    showMsg(handle, NO_DB, "Die Anzahl der Vorgänge kann nicht ermittelt werden.");
    return(false);
  }
  atlbplugin.SetQueryParam(handle, "PER_ID", perid);
  if(atlbplugin.RunQuery(handle, "adi/count/e_vorgang") != 200) {
    showMsg(handle, "", "Die Anzahl der Vorgänge kann nicht ermittelt werden.");
    atlbplugin.CloseTransaction(handle);
    return(false);
  }
  sbiplg_setvar("A_VORi", getQueryData(handle));
  atlbplugin.CloseTransaction(handle);
  return(true);
}


// ######################### XVORS  #########################

function UPDATE_XVOR() {
  var vorId = getVOR_ID();
  for(var i = 0 ; i < XVORS.length ; i++){
    var handle = atlbplugin.OpenTransaction();
    if(handle < 0) {
      showMsg(handle, NO_DB, "Der XVorgang kann nicht gespeichert werden.");
      return(false);
    }

    atlbplugin.SetQueryParam(handle, "VOR_ID", vorId);
    atlbplugin.SetQueryParam(handle, "XBSTEIN_ID", XVORS[i]["XBSTEIN_ID"]);
    atlbplugin.SetQueryParam(handle, "XVORGANG", XVORS[i]["XVORGANG"]);
    
    var mode = "insert_or_update";
    if(atlbplugin.RunQuery(handle, "adi/" + mode + "/e_xvorgang") != 200) {
      showMsg(handle, "", "Der XVorgang kann nicht gespeichert werden.");
      atlbplugin.CloseTransaction(handle);
      return(false);
    }
    setVOR_ID(getQueryData(handle));
    atlbplugin.CloseTransaction(handle);
  }
  return(true);
}

function READ_XVORS() {
  var i = 0;
  var vorid = getVOR_ID();
  if(vorid == "") return(false);
  var handle = atlbplugin.OpenTransaction();
  if(handle < 0) {
    showMsg(handle, NO_DB, "Der Vorgang kann nicht gelesen werden.");
    return(false);
  }
  atlbplugin.SetQueryParam(handle, "VOR_ID", vorid);
  var rc = atlbplugin.RunQuery(handle, "adi/query/e_xvorgang");
  if(rc == 500) {
    var msg = atlbplugin.GetQueryData(handle);
    showMsg(-1, "", msg);
    atlbplugin.CloseTransaction(handle);
    closeWindow();
    return(false);
  }
  if(rc != 200) {
    showMsg(handle, "", "Der Vorgang kann nicht gelesen werden.");
    atlbplugin.CloseTransaction(handle);
    return(false);
  }
  while(atlbplugin.GetQueryResultItem(handle, "XVOR_ID", i).length > 0) {
    XVORS[i] = new Array();
    XVORS[i]["XVOR_ID"] = atlbplugin.GetQueryResultItem(handle, "XVOR_ID", i); 
    XVORS[i]["VOR_ID"] = vorid; 
    XVORS[i]["XBSTEIN_ID"] = atlbplugin.GetQueryResultItem(handle, "XBSTEIN_ID", i); 
    XVORS[i]["XVORGANG"] = atlbplugin.GetQueryResultItem(handle, "XVORGANG", i);
    i++; 
  }
  atlbplugin.CloseTransaction(handle);
  return(true);
}
