<% /*
     $Revision$
     $Id$

    File:           scaffolding_common.inc

    Description:    Common voor gewone GUI en mobiel
    Parameters:
    Context:
    */

var scf =
    {
        getMonday: function (d)
        {
          d = new Date(d);
          var day = d.getDay(),
              diff = d.getDate() - day + (day == 0 ? -6:1); // adjust when day is sunday
          return new Date(d.setDate(diff));
        },
        defaultDate: function (code)
        {
            if (!code)
                return null;
            var dt = null;
            var pn = code.substr(1,1); // p is dag terug, n is dag vooruit
            var pnnum = (pn=='p'?-1:pn=='n'?1:0);
            if (pn != 0)
                pnnum *= (parseInt(code.substr(2), 10) || 1);
            switch (code.substr(0,1).toLowerCase())
            {
                case "n":
                    dt = new Date();
                    break;
                case "d":
                    dt = new Date();
                    dt.setHours(0,0,0,0);
                    dt.setDate(dt.getDate() + pnnum);
                    break;
                case "w":
                    dt = scf.getMonday(new Date());
                    dt.setHours(0,0,0,0);
                    dt.setDate(dt.getDate() + pnnum * 7);
                    break;
                case "m":
                    dt = new Date();
                    dt.setDate(1);
                    dt.setHours(0,0,0,0);
                    dt.setMonth(dt.getMonth() + pnnum);
                    break;
                case "q":
                    dt = new Date();
                    dt.setDate(1);
                    dt.setHours(0,0,0,0);
                    dt.setMonth(3 * Math.floor(dt.getMonth() / 3));
                    dt.setMonth(dt.getMonth() + pnnum * 3);
                    break;
                case "y":
                    dt = new Date();
                    dt.setDate(1);
                    dt.setMonth(0);
                    dt.setHours(0,0,0,0);
                    dt.setYear(dt.getYear() + pnnum);
                    break;
            }

            // Nu correctie naar einde periode voor hoofdletter
            switch (code.substr(0,1))
            {
                case "N":
                    break;
                case "D":
                    dt.setDate(1 + dt.getDate());
                    break;
                case "W":
                    dt.setDate(7 + dt.getDate());
                    break;
                case "M":
                    dt.setMonth(1 + dt.getMonth());
                    break;
                case "Q":
                    dt.setMonth(3 + dt.getMonth());
                    break;
                case "Y":
                    dt.setYear(1 + dt.getYear());
                    break;
            }
            // Onderstaande pas *na* voorgaande om problemen
            // met maanden van 30 dagen te voorkomen
            if (code.substr(0,1) != code.substr(0,1).toLowerCase())
                dt.setHours(0,0,-1,0); // Einde dag ervoor

            return dt;
        },
        parseDefault: function (def, field)
        {
            var result = { fixed: false, vmin: null, vmax: null };
            if (!def || def.substr(0,1) != ":")
            {
                if (field.typ == "date" || field.typ == "datetime" || field.typ == "tmie")
                    return result; // fixed datumvelden ondersteunen we (nog) niet.
                result.vmin = def;
                return result;
            }
            def = def.substr(1);
            if (def.substr(0,1) == "!") // ':!' bevriest waarde
            {
                result.fixed = true;
                def = def.substr(1);
            }
            result.vmin = def;
            if (def.substr(0,2) == "M:") // 'M' multiselect
            {
                def = def.substr(2);
                result.vmin = def.split(";");
            }
            if (field.filter == "range")
            {
                result.vmin = def.split('-')[0];
                result.vmax = def.split('-')[1];
            }
            switch (field.typ)
            {
                case "key":
                    switch (result.vmin.toLowerCase())
                    {
                        case "self":
                            result.vmin = user_key;
                            break;
                        case "location":
                            result.vmin = user.alg_locatie_key({withcurrent:true});
                            break;
                        case "building":
                            result.vmin = user.alg_gebouw_key({withcurrent:true});
                            break;
                    }
                    break;
                case "date":
                case "datetime":
                    result.vmin = scf.defaultDate(result.vmin);
                    result.vmax = scf.defaultDate(result.vmax);
                    break;
            }
            return result;
        }
    }

function scf_transit2url(scf_params)
{
    var transit = "";
    if ("transit" in scf_params)
    {
        var tdata = scf_params.transit;
        for (trans in tdata)
            transit = "&" + trans + "=" + safe.url(tdata[trans]);
    }
    return transit;
}
%>