function calendar(wheretoplace, date)
{
  //If no parameter is passed use the current date.
  if(date == null )
    date = dtSelectedDate;
  
  iMonth = date.getMonth();
  iYear = date.getFullYear();


this_month = new Date(iYear, iMonth, 1);
next_month = new Date(iYear, iMonth + 1, 1);
prev_month = new Date(iYear, iMonth - 1, 1);


iDaysInMonth = Math.round((next_month.getTime() - this_month.getTime()) / (1000 * 60 * 60 * 24));
iDaysInLastMonth = Math.round(( this_month.getTime() - prev_month.getTime() ) / (1000 * 60 * 60 * 24));

  months = new Array('Sausis','Vasaris','Kovas','Balandis','Gegužė','Birželis','Liepa','Rugpjūtis','Rugsėjis','Spalis','Lapkritis','Gruodis');

  //Find out when this month starts and ends.         
  iFirstDayWeekday = this_month.getDay();
  if (iFirstDayWeekday == 0) 
    iFirstDayWeekday = 7;

if (this_month.getDay() == 1)
{
    iDay = 1;
    iLastDay = iDaysInMonth;
    blNotThisMonth = false;
}
else
{
    iLastDay = iDaysInLastMonth;
    iDay = iDaysInLastMonth-iFirstDayWeekday+2;
    blNotThisMonth = true;
}
if (date.getMonth() == dtCurrentDate.getMonth() && date.getFullYear() == dtCurrentDate.getFullYear())
    blCurMonth = true;
else
   blCurMonth = false;
iCurDay = dtCurrentDate.getDate();
if (date.getMonth() == dtSelectedDate.getMonth() && date.getFullYear() == dtSelectedDate.getFullYear())
    blSelMonth = true;
else
   blSelMonth = false;
iSelDay = dtSelectedDate.getDate();

scalend =   '<table cellpadding="0" cellspacing="1" class="cal_1">'
                +'<caption class="cal_2">'
                +'<div><a href="#" onclick="calendar(\''+wheretoplace+'\', new Date('+iYear+','+(iMonth-1)+',1)); return false">&laquo;</a></div>'
                +'<div><a href="#" onclick="calendar(\''+wheretoplace+'\', new Date('+iYear+','+(iMonth+1)+',1)); return false">&raquo;</a></div>'
                +iYear+' '+months[iMonth]+'</caption>'
                +'<colgroup span="5" class="cal_4_1"/><colgroup span="2" class="cal_4_2"/>'
                +'<thead><tr class="cal_3"><td>Pr</td><td>A</td><td>T</td><td>K</td><td>P</td><td>Š</td><td>S</td></tr></thead>'
                +'<tbody class="cal_5">';
for (iW = 0; iW <6; iW++)
{
    scalend += '<tr>';
    for (iD=0; iD<7; iD++)
    {
        sClass = '';
        blLink = true;
        if (blNotThisMonth)
        {
           sClass = 'cal_6';
           blLink = false;
        }
        else
        { 
            if (blCurMonth && iCurDay ==  iDay)
                sClass += ' cal_7';
            if (blSelMonth && iSelDay ==  iDay)
                sClass += ' cal_8';
        }
        scalend +='<td';
        if (sClass.length > 1 )
        {
            scalend +=' class=\''+sClass+'\'';
        }
        scalend += '>';
        if (blLink) scalend += '<a href="http://www.kaveikti.lt/index.php?page=calendar_list&amp;sDate='+iYear+'-'+(iMonth+1)+'-'+iDay+'" target="_blank">';
        scalend += ''+iDay+'';
        if (blLink) scalend += '</a>';
        scalend += '</td>';

        if (iDay == iLastDay)
        {
            iDay = 1;
            iLastDay = iDaysInMonth;
            blNotThisMonth = !blNotThisMonth;
        }
        else
        {
            iDay++;
        }
    }
    scalend += '</tr>';
}
scalend += '</tbody></table>';

document.getElementById(wheretoplace).innerHTML = scalend;

}
