function CCalendar ()
{
}

CCalendar.Enable = function ()
{
    CCalendar.GetCatElements ();
    CCalendar.Filter ();
}

CCalendar.Filter = function ()
{
    var categories = CCalendar.GetCategories ();
    var re         = /^idcatfilter_\d+((?:_\d+)+)$/;
    var container  = window.document.getElementById ("idcalendarsection");
    var elements   = container.getElementsByTagName ("div");
    var nelements  = elements.length;
    for (var i = 0; i < nelements; ++i) {
        var element = elements [i];
        var id      = element.id;
        var m       = id.match (re);
        if (m !== null) {
            catnums = m [1].substring (1).split ("_");
            show = false;
            for (var j = 0; ! show && j < catnums.length; show = categories.catgroups [catnums [j++]]);
            element.style.display = (show ? "block" : "none");
        }
    }
}

CCalendar.FollowLink = function (theatag)
{
    var url    = theatag.href;
    var re     = /^(?:([^:]+):)?(?:\/\/([^\/]+))?(\/[^\/][^?]*)(?:\?([^#]*))?(?:#(.*))?$/
    var m      = url.match (re);
    var schema = m [1];
    var auth   = m [2];
    var path   = m [3];
    var query  = (m [4] ? m [4].split ("&") : undefined);
    var frag   = m [5];
    var pfx    = "user_calendar_pi1";
    var open   = pfx + "[open]";
    var cf     = pfx + "[catfilter";
    var j      = 0;
    var values = CForm (window.document.getElementById ("idcalendarfilter")).CollectValues ("");
    var re2    = /^idmonth(\d+)$/;
    var elems  = window.document.getElementById ("idcalendarsection").getElementsByTagName ("div");
    var nelems = elems.length;
    var omonth = [];
    if (query) {
        for (var i = 0; i < query.length; ++i) {
            var q = query [i].split ("=");
            if (q [0] != open && q [0].substring (0, cf.length) != cf) {
                query [i] = q;
                query [j++] = query [i];
            }
        }
        query.length = j;
        for (var i in values.values)
            if (i != "id" && i != open)
                query.push ([i, values.values [i]]);
        for (var i = 0; i < nelems; ++i) {
            var elem = elems [i];
            var id   = elem.id;
            var m    = id.match (re2);
            if (m !== null && elem.style.display != "none")
                omonth.push (m [1]);
        }
        if (omonth.length > 0)
            query.push ([open, omonth.join (",")]);
        for (var i in query)
            query [i] = query [i].join ("=");
        query = query.join ("&");
    }
    url = "";
    if (schema)
        url += schema + ":";
    if (auth)
        url += "//" + auth;
    if (path)
        url += path;
    if (query)
        url += "?" + query;
    if (frag)
        url += "#" + frag;
    theatag.href = url;
    return (true);
}

CCalendar.GetCategories = function ()
{
    var all  = window.document.getElementById ("catfilter_all").checked && ! window.document.getElementById ("catfilter_some").checked;
    var has  = all;
    var elms = CCalendar.GetCatElements ();
    for (var i in elms) {
        var checked = elms [i].checked;
        has = has || checked;
        elms [i] = all || checked;
    }
    return ({ canfilter: has, catgroups: elms });
}

CCalendar.GetCatElements = function ()
{
    var all  = window.document.getElementById ("catfilter_all").checked && ! window.document.getElementById ("catfilter_some").checked;
    var ret  = [];
    var re   = /^catfilter_(\d+)$/;
    var form = window.document.getElementById ("idcalendarfilter");
    var elms = form.getElementsByTagName ("input");
    var nelm = elms.length;
    for (var i = 0; i < nelm; ++i) {
        var elem = elms [i];
        var id   = elem.id;
        var m    = id.match (re);
        if (m !== null) {
            ret [Number (m [1])] = elem;
            elem.disabled = all;
        }
    }
    return (ret);
}

