/*
 * ZWPDateWidget
 *
 * Javascript for date widgets.
 *
 * Copyright (C) 2007 Jason den Dulk. All rights reserved.
 *
 * GNU LGPL
 *
 * This code is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This code is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * The GNU LGPL can be seen at http://www.gnu.org/licenses/lgpl.html
 *
 * The GNU LGPL can also be obtained from the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */


zwp_date_dim = [ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];
zwp_date_month_names = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ];

function zwp_date_days_in_month(month, year)
{
    if (!zwp_date_is_leap_year(year) && month == 2)
        return 28;
    else
        return zwp_date_dim[month-1];
}

function zwp_date_is_leap_year(year)
{
    var ret;

    if (year/100 == Math.round(year/100))
        ret = (year/400 == Math.round(year/400));
    else
        ret = (year/4 == Math.round(year/4));
    return ret;
}

function zwp_date_set_days(id)
{
    var select = xGetElementById(id+"-zwp-day");

    if (select)
    {
        var days;

        var month = xGetElementById(id+"-zwp-month");
        month = month.value;
        
        var year = xGetElementById(id+"-zwp-year");
        if (year)
            days = zwp_date_days_in_month(month, year.value);
        else
            days = zwp_date_dim[month];
    
        var size = select.options.length;

        if (select.selectedIndex >= days)
            select.selectedIndex = days-1;

        for (var i=size-1; i>=days; --i)
            select.remove(i);

        for (i=size+1; i<=days; ++i)
        {
            var e = document.createElement('option');
            e.text = i;
            select.add(e, null);
        }
    }
}

function zwp_quick_today(id)
{
    return;
    
    var date = new Date()

    var d = xGetElementById(id+"-zwp-year");
    d.selectedIndex = date.getFullYear()-1970;
    d = xGetElementById(id+"-zwp-month");
    d.selectedIndex = date.getMonth();
    zwp_date_set_days(id);
    d = xGetElementById(id+"-zwp-day");
    d.selectedIndex = date.getDate()-1;
}

function zwp_rebuild_months(id)
{
    var month_wgt = xGetElementById(id+"-zwp-month");
    var month = month_wgt.value;
    while (month_wgt.options.length)
         month_wgt.remove(0);
    for (var i=0; i<12; ++i)
    {
        var e = document.createElement('option');
        e.text = zwp_date_month_names[i];
        e.value = i+1;
        if (document.all)
            month_wgt.add(e);
        else
            month_wgt.add(e, null);
        if (month == i+1) month_wgt.selectedIndex = i;
    }
}

/*
 * Removes all dates prior to the "today" date.
 */

function zwp_remove_previous_days(id, today)
{
    var index;
    
    var year_wgt = xGetElementById(id+"-zwp-year");
    if (year_wgt)
    {
        var index = year_wgt.selectedIndex;
        
        while (year_wgt.options[0].text < today.getFullYear())
        {
             year_wgt.remove(0);
             --index;
        }
        if (index < 0) index = 0;
        year_wgt.selectedIndex = index;
        if (year_wgt.options[year_wgt.selectedIndex].text > today.getFullYear()) return;
    }
    
    var mth_wgt = xGetElementById(id+"-zwp-month");
    if (mth_wgt)
    {
        index = mth_wgt.selectedIndex;
        
    
        // Remember that in JS month is 0 based (Jan = 0). In ZWP month is 1 based,
        // (Jan = 1).
        
        while (mth_wgt.options[0].value <= today.getMonth())
        {
             mth_wgt.remove(0);
             --index;
        }
        
        if (index < 0) index = 0;
        mth_wgt.selectedIndex = index;
    
        if (mth_wgt.value > today.getMonth() + 1) return;
    }
    
    var day_wgt = xGetElementById(id+"-zwp-day");
    if (!day_wgt) return;
    index = day_wgt.selectedIndex;
    
    while (day_wgt.options[0].text < today.getDate())
    {
         day_wgt.remove(0);
         --index;
    }
    
    if (index < 0) index = 0;
    day_wgt.selectedIndex = index;
}

function zwp_date_set_days_from_dow(id, dow)
{
    var select = xGetElementById(id+"-zwp-day");

    if (select)
    {
        var day;
        var today = new Date();
    
        var month = xGetElementById(id+"-zwp-month");
        if (month) month = month.selectedIndex;
        else month = today.getMonth();
        var year = xGetElementById(id+"-zwp-year");
        if (year) year = year.options[year.selectedIndex].text;
        else year = today.getFullYear();
    
        var date = new Date(year, month, 1);
    
        var _dow = date.getDay();
        day = dow - _dow + 1;
        if (day <= 0) day = day + 7;

        var dim = zwp_date_days_in_month(month+1, year);
        
        //var current = select.value;
        var current = select.options[select.selectedIndex].text;
        
        var size = select.options.length;
        while (select.options.length)
             select.remove(0);

        while (day <= dim)
        {
            var e = document.createElement('option');
            e.text = day;
            if (document.all)
                select.add(e);
            else
                select.add(e, null);
            if (current <= day)
            {
                select.selectedIndex = select.options.length-1;
                current = 32;
            }
            day += 7;
        }
    }
}


