<%@ language = "JavaScript" %>
<%
/*
    $Revision$
    $Id$

    File:               get_cnt_info_ajax.asp
    Description:        Ophalen van allerlei info om client-side data te hebben
    Parameters:         req_info:   "afdeling"    --> requires "afd_key"

    Context:
    Note:
    Sample:             appl/mld/get_cnt_info_ajax.asp?req_info=afdeling&afd_key=1
*/ %>

<%
var JSON_Result = true;
%>

<!-- #include file="../Shared/common.inc"-->
<!-- #include file="./cnt.inc" -->

<%
var req_info = getQParam("req_info");
var result = {};
switch (req_info)
{
    case "afdeling":
    {
        var afd_key = getQParamInt("afd_key");
        result = cnt.cnt_get_afdeling(afd_key);
        break;
    }
    case "locked":
    {
        var cnt_key = getFParamInt("module_key", -1);
        var result = {module_key: cnt_key, locked: false};
        var sql = "SELECT c.cnt_contract_locked_user_key"
                + "     , c.cnt_contract_locked_date"
                + "     , (SELECT p.prs_perslid_naam_full"
                        + "  FROM prs_v_perslid_fullnames_all p"
                        + " WHERE p.prs_perslid_key = c.cnt_contract_locked_user_key"
                        + ") cnt_contract_locked_user_name"
                + "  FROM cnt_contract c"
                + " WHERE c.cnt_contract_locked_user_key IS NOT NULL"
                + "   AND c.cnt_contract_locked_user_key <> " + user_key
                + "   AND c.cnt_contract_locked_date IS NOT NULL"
                + "   AND c.cnt_contract_key = " + cnt_key;
        var oRs = Oracle.Execute(sql);
        if (!oRs.eof)
        {
            var expire = new Date;
            expire.setMinutes(expire.getMinutes() - S("mld_locked_expire"));
            result.locked = oRs("cnt_contract_locked_user_key").Value && oRs("cnt_contract_locked_date").Value >= expire;
            result.locked_user = oRs("cnt_contract_locked_user_key").Value;
            result.locked_date = toTimeString(oRs("cnt_contract_locked_date").Value);
            result.locked_name = new Perslid(result.locked_user).naam();
        }
        oRs.Close();
        break;
    }
    case "termijndatum":
    {
        var startdatum = getQParamDate("startdatum", null);
        var termijn_key = getQParamInt("termijn_key", -1);
        var result = {};
        var sql = "SELECT cnt.cnt_getTermijndatum( " + startdatum.toSQL()
                                              + ", " + termijn_key
                                              + ", 1" 
                                              + ") einddatum"
                + "     , cnt_termijn_omschrijving"
                + "  FROM cnt_termijn"
                + " WHERE cnt_termijn_key = " + termijn_key;
        var oRs = Oracle.Execute(sql);
        if (!oRs.EOF) {
            var einddatum = new Date(oRs("einddatum").Value);
            result = { termijn_key: termijn_key
                     , termijn_oms: oRs("cnt_termijn_omschrijving").Value
                     , termijn_datum: einddatum.getTime()
                     };
        }
        oRs.Close();
        break;
    }
}

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