var monthes = Array( 
    'Январь', 'Февраль', 'Март', 'Апрель', 'Май', 'Июнь',
    'Июль', 'Август', 'Сентябрь', 'Октябрь', 'Ноябрь', 'Декабрь' 
    );

var calendars = new Array();
var calCnt = 0;

function CalendarDate( calId, calDate, inl, empty )
{
    this.year = calDate.getFullYear();
    this.month = calDate.getMonth();
    this.day = calDate.getDate();
    this.src = new Date( calDate.getFullYear(), calDate.getMonth(), calDate.getDate() );
    this.prevValue = new Date( calDate.getFullYear(), calDate.getMonth(), calDate.getDate() );
    this.id = calId;
    this.inline = inl;
    this.calShown = false;
    this.dtWidth = 'auto';
    this.empty = (empty == 1) ? true : false;

    this.getDateStr = function ()
	{
	if( this.empty )
	    return '';
	else
	    return this.src.getDate() + '.' + (this.src.getMonth()+1) + '.' + this.src.getFullYear();
	}
	
    this.prepareCalendar = function() 
        {
        var obj = findObject( calId );
        if( !obj )
            return;
        var ds = '';
	//ds += '<input type=hidden id=i_'+this.id +' name=i_'+this.id+' value="';
	//ds += this.src.getDate() + ', ' + monthes[this.src.getMonth()] + ', ' + this.src.getFullYear();	
	//ds += '">';
        ds += '<div id=' + this.id + '_dt style="color: #808080; text-align: center;">';
        ds += '<table width=100% cellpadding=0 cellspacing=0 border=0><tr><td width=100% align=left>';
        ds += '<span style="cursor: pointer; cursor: hand;" onclick="return showCalendar(\''+this.id+'\');">';
	if( !this.empty )
	    ds += this.src.getDate() + ', ' + monthes[this.src.getMonth()] + ', ' + this.src.getFullYear();
	ds += '</span>';
        ds += '</td>';
        if( this.inline )
            ds += '<td valign=center><a href=/ onclick="return showCalendar(\''+this.id+'\');"><img src=img/dropdown.gif width=12 height=12 border=0 alt="Выбрать дату из календарика"></a></td>';
        ds += '<td valign=center><a href=/ onclick="return revertDate(\''+this.id+'\');"><img src=img/revert.gif width=12 height=12 border=0 alt="Сбросить дату"></a></td></tr></table>';
        ds += '</div>';

        if( this.inline )
            {
            visStyle = this.calShown ? 'width: ' +this.dtWidth+';' : 'top: 0px; left: 0px; visibility: hidden;';
            ds += '<div id=' + this.id+'_bdy style="margin-left: 0px; position: absolute; ' + visStyle + ' background-color: #d7cec4; border: 1px solid #898989;">';
            }

        ds += '<table class=cal cellpadding=0 cellspacing=0 border=0>';
        ds += '<tr><td colspan=7>';

        ds += '<table width=100% style="width: 220px;" cellpadding=0 cellspacing=0 border=0>';
        ds += '<tr><td><a href=/ onclick="return decreaseYear(\''+this.id+'\');"><img src=img/prev_year.gif width=12 height=12 border=0 alt="<<" title=""></a></td>';
        ds += '<td><a href=/ onclick="return decreaseMonth(\''+this.id+'\');"><img src=img/prev_month.gif width=12 height=12 border=0 alt="<" title=""></a></td>';
        ds += '<td class=noc width=100% style="text-align: center;">' + monthes[this.month] + ' ' + this.year +'</td>';
        ds += '<td><a href=/ onclick="return increaseMonth(\''+this.id+'\');"><img src=img/next_month.gif width=12 height=12 border=0 alt=">" title=""></a></td>';
        ds += '<td><a href=/ onclick="return increaseYear(\''+this.id+'\');"><img src=img/next_year.gif width=12 height=12 border=0 alt=">>" title=""></a></td>';
        ds += '</table>';
        ds += '</td></tr>';

        ds += '<tr><td class=noc width=15%>Вс</td><td class=noc width=14%>Пн</td><td class=noc width=14%>Вт</td><td class=noc width=14%>Ср</td><td class=noc width=14%>Чт</td><td class=noc width=14%>Пт</td><td class=noc width=15%>Сб</td></tr>';

//        sd = (new Date( this.year, this.month, 1 ) ).getDay(); 
        currDay = 1;

        var fdobj = new Date( this.year, this.month, 1 );
        sd = fdobj.getDay(); currDay = 1; fd = 31;
        do{
            if( fdobj.getMonth() != this.month )
                {
                fdobj.setFullYear( this.year, this.month, 1 );
                fd--;
                }
            fdobj.setFullYear( this.year, this.month, fd );
            } while( this.month != fdobj.getMonth() );
        started = false; finished = false;
        
        for( i=0; i<6; i++ )
            {
            ds += '<tr>';
            for( j=0; j<7; j++ )
                {
                dayNum = '&nbsp;'; clAddOn = ' class=noc'; clickAddOn = '';
                if( j==sd && !started )
                    started = true;
                if( started && !finished )
                    {
                    clAddOn = '';
                    if( this.month == this.src.getMonth() &&
                        this.year == this.src.getFullYear() && 
                        this.src.getDate() == currDay )
                        clAddOn = ' class=s';
                    else
                        clickAddOn = 'onclick="return setSingleDate(\'' + 
                            this.id + '\',\'' + i + '' + j + '\');"';
                    dayNum = currDay;
                    currDay++;
                    }
                if( currDay > fd )
                    finished = true;
                ds += '<td ' + clickAddOn + ' id=' + 
                    this.id + '_' + i + '' + j + clAddOn +'>' + 
                    dayNum + '</td>';
                }
            ds += '</tr>';
            }

        ds += '</table>';
        if( this.inline )
            ds += '</div>';
        obj.innerHTML = ds;
        }

    this.getSmallerDate = function () 
        {
        return (new Date( this.src.getFullYear(), this.src.getMonth(),
            this.src.getDate(), 0, 0, 0, 0 )).valueOf()/1000;
        }

    this.getGreaterDate = function ()
        {
        return (new Date( this.src.getFullYear(), this.src.getMonth(),
            this.src.getDate()+1, 0, 0, 0, 0 )).valueOf()/1000;
        }

    this.updateDate = function()
        {
        var tstd = new Date( this.year, this.month, this.day )
        this.year = tstd.getFullYear();
        this.month = tstd.getMonth();
        }

    this.increaseMonth = function ()
        {
        this.month++;
        this.updateDate();
        this.prepareCalendar();
        }

    this.decreaseMonth = function ()
        {
        this.month--;
        this.updateDate();
        this.prepareCalendar();
        }

    this.increaseYear = function ()
        {
        this.year++;
        this.updateDate();
        this.prepareCalendar();
        }

    this.decreaseYear = function ()
        {
        this.year--;
        this.updateDate();
        this.prepareCalendar();
        }

    this.setSingleDate = function ( csrc )
        {
        var cobj = findObject( this.id + '_' + csrc );
        if( !cobj )
            return false;
        
        var day = isNaN( parseInt( cobj.innerHTML ) ) ? 1 : parseInt( cobj.innerHTML );
        this.src.setFullYear( this.year, this.month, day );
        this.year = this.src.getFullYear();
        this.month = this.src.getMonth();
        this.day = this.src.getDate();
        this.calShown = false;
	this.empty = false;
        this.prepareCalendar();
        }

    this.revertDate = function ()
        {
        this.src.setFullYear( this.prevValue.getFullYear(),
            this.prevValue.getMonth(), this.prevValue.getDate() );
        this.year = this.src.getFullYear();
        this.month = this.src.getMonth();
        this.day = this.src.getDate();
        this.prepareCalendar();
        }

    this.showCalendar = function ()
        {
        if( this.calShown )
            hideElem( this.id + '_bdy' );
        else
            {
            var calBdyObj = findObject( this.id+'_bdy' );
            if( calBdyObj )
                {
                var dtObj = findObject( this.id+'_dt' );
                if( !dtObj )
                    return false;
                var y = anchorPosY( dtObj );
                var x = anchorPosX( dtObj );
                calBdyObj.style.top = y+dtObj.offsetHeight+'px';
                calBdyObj.style.left = x+'px';
                calBdyObj.style.width = dtObj.offsetWidth+'px';
                this.dtWidth = dtObj.offsetWidth+'px';
                showElem( this.id+'_bdy' );
                }
            }
        this.calShown = !this.calShown;
        }
}

function setSingleDate( calId, csrc )
{
    if( co = getCalendarObj( calId ) )
        co.setSingleDate( csrc );
    return false;    
}

function showCalendar( calId )
{
    if( co = getCalendarObj( calId ) )
        co.showCalendar();
    return false;        
}

function revertDate( calId )
{
    if( co = getCalendarObj( calId ) )
        co.revertDate();
    return false;    
}

function getCalendarObj( calId )
{
    for( i=0; i<calCnt; i++ )
        if( calendars[ i ].id == calId )
            return calendars[i];
    return null;
}

function increaseMonth( calId )
{
    if( co = getCalendarObj( calId ) )
        co.increaseMonth();
    return false;
}

function decreaseMonth( calId )
{
    if( co = getCalendarObj( calId ) )
        co.decreaseMonth();
    return false;
}

function decreaseYear( calId )
{
    if( co = getCalendarObj( calId ) )
        co.decreaseYear();
    return false;
}

function increaseYear( calId )
{
    if( co = getCalendarObj( calId ) )
        co.increaseYear();
    return false;
}

function initCalendar( calId, calDate, inline, empty )
{
    calendars[ calCnt ] = new CalendarDate( calId, calDate, inline, empty );
    calendars[ calCnt ].prepareCalendar();
    calCnt++;
}