<%@ language = "JavaScript" %>
<% /*
    $Revision$
    $Id$
    File:               pacta.asp
    Description:        A deeplink call from PACTA is handled to insert or delete a reservation
    Parameters:
        action          Action: insert or delete a reservation
        yy,mm,dd,hh,mi  Preset begindate and begintime for new reservation.
        thh,tmi         Optional, preset enddate and endtime for new reservation.
        gast_login      Optional, host of new reservation
        bezoeker        Optional, name of visiter of new reservation
        postcode        Optional, postal code of visiter of new reservation
        plaats          Optional, recidance of visiter of new reservation
        bsn             Bsn code of visiter of new reservation
    Context:            Client specific file to handle a deeplink call from PACTA.
    Note:               Only room reservation: res_type = 'R'
*/ %>
<!--#include file="../../appl/Shared/common.inc" -->
<!--#include file="../../appl/Shared/iface.inc" -->

<%
  var lcl_pacta_deleted = "Verwijderd door PACTA";

  var act = getQParam("action", "");

  // start tijd
  var date = getQParamInt("dd");
  var month = getQParamInt("mm");
  var year = getQParamInt("yy");
  var hour = getQParamInt("hh");
  var minute = getQParamInt("mi");
  var res_van = new Date(year, month-1, date, hour, minute);

  // optionele eindtijd
  var tth = getQParamInt("thh", hour);
  var tmi = getQParamInt("tmi", hour);
  var res_tot = new Date(year, month-1, date, tth, tmi);

  // optionele host
  var gast_login = getQParam("gast_login", "");
  var host_key = -1;
  if (gast_login!="") {
    sql = "SELECT p.prs_perslid_key"
        + "  FROM prs_perslid p"
        + " WHERE p.prs_perslid_oslogin = " + safe.quoted_sql(gast_login);
    oRs = Oracle.Execute(sql);
    if (!oRs.eof) {
      host_key = oRs(0).value;
    }
  }

  var flex_defaults = "postcode="+getQParam("postcode")
                    + "&plaats="+getQParam("plaats")
                    + "&bsn="+getQParam("bsn");

  var bsn = getQParam("bsn", ""); // voor verwijderen

  // find activity_key of the 'PACTA' activity
  var activity = -1;
  sql = "SELECT ra.res_activiteit_key"
      + "  FROM res_activiteit ra"
      + " WHERE UPPER(ra.res_activiteit_omschrijving) = UPPER('PACTA')"
      + "   AND ra.res_activiteit_verwijder IS NULL";
  oRs = Oracle.Execute(sql);
  activity = oRs(0).value;
%>

<html>

<head>
<body leftmargin=0 topmargin=0 onLoad="Loaded();">

<script language="javascript">

  // Action after deletion, deletion is successful
  function Delete_done(bFull)
  {
    // Deletion is done and successful
    // Send 200 back
    document.write("200");
  }

  function Loaded()
  {
<%
    if (act == "insert") {
      var s = "../../appl/res/res_reservering.asp?urole=fo&restype=R"
      s += "&res_van=" + res_van.getTime();
      s += "&res_tot=" + res_tot.getTime();
      s += "&activiteit_key=" + activity;
      s += "&host_key=" + host_key;
      s += "&bez_naam=" + getQParam("bezoeker", "")
%>
      window.location.href = "<%=s%>&flex_defaults="+escape("<%=flex_defaults%>");
<%
    }
    else if (act == "delete") {
      // Find the reservation which should be deleted
      var ora_date = make_sql_date(year, month, date, hour, minute);
      var res_key = -1;
      sql = "SELECT rr.res_reservering_key"
          + " FROM res_rsv_ruimte rr"
          + ", res_kenmerkwaarde rkw"
          + ", res_activiteit ra"
          + ", res_kenmerk rk"
          + " WHERE rr.res_rsv_ruimte_key = rkw.res_rsv_ruimte_key"
          + " AND rr.res_activiteit_key = ra.res_activiteit_key"
          + " AND rkw.res_kenmerk_key = rk.res_kenmerk_key"
          + " AND ra.res_activiteit_key = rk.res_activiteit_key"
          + " AND rr.res_rsv_ruimte_verwijder IS NULL"
          + " AND ra.res_activiteit_key = " + activity
          // JGL: AND UPPER(res_kenmerk_omschrijving) = 'BSN'
          + " AND rr.res_rsv_ruimte_van = " + res_van.toSQL()
          + " AND rkw.res_kenmerkreservering_waarde = " + safe.quoted_sql(bsn);
      oRs = Oracle.Execute(sql);
      if (!oRs.eof) {
        // Reservation found, delete it
        res_key = oRs(0).value;
%>
        var s = "../../Res/delete.asp?front=1&res_key=<%=res_key%>&opmerk=<%=lcl_pacta_deleted%>";
        window.document.getElementById("hidFrame").src = s;
<%
      }
      else {
        // Can't find reservation, deletion is not successful
        // Send 500 back
%>
        document.write("500");
<%
      }
    }
%>
  }
</script>



<iframe src="../../appl/Shared/empty.html" frameborder="0" id="hidFrame" style="display:none"></iframe>
</body>
</head>
