﻿Event.observe(window, 'load', function() {
    month = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
    
    $$('input.cal').each( function(n) {
        $('calendar').value = n.id;
       
        var ref = $(n.id).next().value.split('-');
        var m = parseInt(ref[0], 10);
        var d = parseInt(ref[1], 10);
        var y = parseInt(ref[2], 10);
        
        chooseDate(m, d, y);
            
        Event.observe(n, 'click', function() {
            $('calendar').value = n.id;
            
            var today = new Date();
            var ref = $(n.id).next().value.split('-');
            var m = parseInt(ref[0], 10);
            var y = parseInt(ref[2], 10);
            
            $('curMM').value = (m == 0) ? today.getMonth() : m - 1;
            $('curYYYY').value = (y == 0) ? today.getFullYear() : y;
            
            populateTable(); 
            showCalendar(n);
            return false;
        });
    });
});

function chooseDate(m, d, y)
{
    $('calendar').hide();
    if(isNaN(m) || isNaN(d) || isNaN(y))
    {
        var today = new Date();
        m = today.getMonth() + 1;
        d = today.getDate();
        y = today.getFullYear();
    }
    while(m.toString().length < 2) { m = '0' + m.toString(); }
    while(d.toString().length < 2) { d = '0' + d.toString(); }
    while(y.toString().length < 4) { y = '0' + y.toString(); }
    $($('calendar').value).previous().innerHTML = m + '-' + d + '-' + y;
    $($('calendar').value).next().value = m + '-' + d + '-' + y;
}

function updateCalendar(m, y)
{
    var theMonth = $('curMM').value;
    var theYear = $('curYYYY').value;
    
    if (m == 0 && y == 0)
    {
        var today = new Date();
        theMonth = today.getMonth();
        theYear = today.getFullYear();
    }
    
    if (theMonth + m < 0)
    {
        $('curMM').value = 11;
        theYear--;
    }
    else if (theMonth + m > 11)
    {
        $('curMM').value = 0;
        theYear++;
    }
    else
    {
        $('curMM').value = theMonth + m;
    }
    
    $('curYYYY').value = theYear + y;
    populateTable();
}

function populateTable()
{
    var theMonth = $('curMM').value;
    var theYear = $('curYYYY').value;
    var firstDay = getFirstDay(theYear, theMonth);
    var howMany = getMonthLen(theYear, theMonth);
    var today = new Date();
    
    $('thead').innerHTML = month[theMonth] + " " + theYear;
    $('curMM').innerHTML = month[theMonth];
    $('curYYYY').innerHTML = theYear;
    $$('#tbody tr').invoke('remove');
    
    var d = 1;
    var done = false;
    while(!done)
    {
        nr = $('tbody').insertRow($('tbody').rows.length);
        for (var i = 0; i < 7; i++)
        {
            nc = nr.insertCell(nr.cells.length);
            if ($('tbody').rows.length == 1 && i < firstDay) 
            {
                nc.innerHTML = '&nbsp;';
                continue;
            }
            if (d == howMany) done = true;
            if (d <= howMany)
            {
                if (today.getFullYear() == theYear && today.getMonth() == theMonth && today.getDate() == d) nc.id = "today";
                nc.innerHTML = '<a href="#" onclick="chooseDate(' + (theMonth + 1) + ',' + d + ',' + theYear + '); return false;">' + d + '</a>';
                d++;
            }
            else
            {
                nc.innerHTML = '&nbsp;';
            }
        }
    }
}

function getFirstDay(theYear, theMonth){
    var firstDate = new Date(theYear, theMonth, 1);
    return firstDate.getDay();
}

function getMonthLen(theYear, theMonth) {
    var nextMonth = new Date(theYear, theMonth + 1, 1);
    nextMonth.setHours(nextMonth.getHours() - 3);
    return nextMonth.getDate();
}

function showCalendar(e)
{
    var cal_hide = null;
    Position.clone(e, 'calendar', { setWidth: false, setHeight: false, offsetTop: -10, offsetLeft: 30 });
    
    //$$('select').invoke('hide');
    
    $('calendar').show();
    $('calendar').scrollTo();
    clearTimeout(cal_hide);
    cal_hide = window.setTimeout(function() { $('calendar').hide(); $$('select').invoke('show'); }, 5000);
    
    Event.observe('calendar', 'mouseover', function() {
	    $('calendar').show();
	    clearTimeout(cal_hide);
	    
	    Event.observe('calendar', 'mouseout', function() {
            clearTimeout(cal_hide);
            cal_hide = window.setTimeout(function() { $('calendar').hide(); $$('select').invoke('show'); }, 500);
        });
    });
}
