<%@ LANGUAGE = JavaScript %>
<% /*
    $Revision$
    $Id$

    File:           ins_delete_srtdeel.asp
    Description:    Verwijderen alg-objectsoort

    Parameters:     key
    Context:        Vanuit show-schermen
    Result:         JSON object
    Note:

*/
%>

<%
var JSON_Result = true;
%>

<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../Shared/json2.js" -->
<!-- #include file="./sle.inc" -->

<%
protectRequest.validateToken();
var authparamsSLEBOF = user.checkAutorisation("WEB_SLEBOF");
user.auth_required_or_abort(authparamsSLEBOF.ALGwritelevel < 9 && authparamsSLEBOF.PRSwritelevel < 9);

var ins_srtdeel_key = getFParamInt('key');

var sql = "SELECT sd.ins_srtgroep_key"
        + " FROM ins_srtdeel sd"
        + " WHERE sd.ins_srtdeel_key = " + ins_srtdeel_key;
var oRs = Oracle.Execute(sql);
var sletype = (oRs("ins_srtgroep_key").value == S("sle_ins_srt_groep_key")? "S" : "C");

var result = { success: false, message : L("lcl_sle_delete_failed") };

var alldeleted = true;
var sql = "SELECT d.ins_deel_key"
        + "  FROM ins_deel d"
        + " WHERE d.ins_srtdeel_key = " + ins_srtdeel_key;
oRs = Oracle.Execute(sql, true);
while (!oRs.eof)
{
  var ins_key = oRs("ins_deel_key").value;
  var this_sle = sle.func_enabled_slecil(ins_key);
  if (this_sle.canDelete)
  {
    sql = "DELETE ins_deel"
        + " WHERE ins_deel_key = " + ins_key;
    var err = Oracle.Execute(sql, true);
    if (err.friendlyMsg)
      alldeleted = false;
  }
  else
    alldeleted = false;
  oRs.MoveNext();
}

if (alldeleted)
{
  sql = "BEGIN"
      + "  DELETE ins_srtdeel_srtdeel"
      + "   WHERE ins_srtdeel_key_1 = " + ins_srtdeel_key + ";"
      + "  DELETE ins_srtdeel_srtdeel"
      + "   WHERE ins_srtdeel_key_2 = " + ins_srtdeel_key + ";"
      + "  DELETE ins_srtdeel"
      + "   WHERE ins_srtdeel_key = " + ins_srtdeel_key + ";"
      + "END;"
  var err = Oracle.Execute(sql, true);
  if (err.friendlyMsg)
    var result = { success: false, message : L("lcl_sle_delete_failed") };
  else
    var result = { success: true, message : (sletype == "S"? L("lcl_sle_deleted") : L("lcl_cil_deleted")) };
}

Response.Write(JSON.stringify(result));
%>