

function Calendar(oDate, oTrgt, WDaysP, aMonthsP, onDataClick) 
{
    var  oContainer = oTrgt;
    
    var oCurrentDate = new Date();
    var oRequestDate = oDate; //(!oDate) ? new Date() : oDate;
    var oPrevMonthDate = new Date((!oRequestDate) ? new Date() : oRequestDate);
        oPrevMonthDate.setDate(1); 
        oPrevMonthDate.setMonth(((!oRequestDate) ? new Date() : oRequestDate).getMonth()-1);
    var prevMonthDaysCount = Calendar.prototype.getLastDay(oPrevMonthDate);
    var emptyDays = Calendar.prototype.getEmptyWDays((!oRequestDate) ? new Date() : oRequestDate);
    var prevMonthDayStart = (prevMonthDaysCount-emptyDays) + 1;
    var currentMonthDaysCount = Calendar.prototype.getLastDay((!oRequestDate) ? new Date() : oRequestDate);

    Calendar.prototype.currentDate = oRequestDate;
    
    Calendar.prototype.onDataClick = onDataClick;
    
    
    if(!WDaysP)
        var aWeekDays = new Array("П", "В", "С", "Ч", "П", "С", "В");
    else
        var aWeekDays = WDaysP;
        
    if(!aMonthsP)
        var aMonths = new Array("Январь", "Февраль", "Март", "Апрель", "Май", "Июнь", "Июль", "Август", "Сентябрь", "Октябрь", "Ноябрь", "Декабрь");
    else
        var aMonths = aMonthsP;
    
    var oMainTable = document.createElement('TABLE');
        oMainTable.id = 'oCalendarTable';
        oMainTable.cellPadding = 0;
        oMainTable.cellSpacing = 1;
        oMainTable.className = 'Calendar_MainTable';
    var oMainRow = oMainTable.insertRow(oMainTable.rows.length);

    var oMainCell = oMainRow.insertCell(oMainRow.cells.length);
        oMainCell.style.width = '100px';
        oMainCell.style.textAlign = 'center';
        oMainCell.style.font = '11px Tahoma, Helvetica, sans-serif';
        
        var oMonthYearTable = document.createElement('TABLE');
            oMonthYearTable.id = 'oCalendarMonthYearTable';
            oMonthYearTable.cellPadding = 0;
            oMonthYearTable.cellSpacing = 1;
            oMonthYearTable.border = 0;
            oMonthYearTable.width="100%";
            
        oMainCell.appendChild(oMonthYearTable);
            
        var oMonthYearTableRow = oMonthYearTable.insertRow(oMonthYearTable.rows.length);
        
        var oMonthCell = oMonthYearTableRow.insertCell(oMonthYearTableRow.cells.length);
        oMonthCell.width="60%"
        
        var oSelectMonth = document.createElement("SELECT");
        oSelectMonth.className = "oCalendar_month_selector"
        oSelectMonth.style.width="100%";
        oMonthCell.appendChild(oSelectMonth);
        for(var i=0; i<aMonths.length;i++)
        {
            var oMonth = document.createElement("OPTION");
            oSelectMonth.appendChild(oMonth);
            oMonth.value = i;
            oMonth.name = "M" + i;
            oMonth.innerHTML = aMonths[i];
            if( ((!oRequestDate)?new Date():oRequestDate).getMonth() == i )
                oMonth.selected = true;
        }
        oSelectMonth.selectedIndex = ( (!oRequestDate)?new Date():oRequestDate ).getMonth();
        
        oSelectMonth.onchange = function() {
            var oCurrentDate = new Date((!Calendar.prototype.currentDate)?new Date():Calendar.prototype.currentDate);
            var oNewDate = new Date(oCurrentDate);
            vall = this.value;
            if(vall < 0)
                vall = oCurrentDate.getMonth();
            
            oNewDate.setMonth(vall);
            Calendar(oNewDate,oTrgt,null,null,Calendar.prototype.onDataClick);  
        }
        
        var oYearCell = oMonthYearTableRow.insertCell(oMonthYearTableRow.cells.length);
        oYearCell.width="40%"
        
        var oSelectYear = document.createElement("SELECT");
        oSelectYear.className = "oCalendar_year_selector"
        oSelectYear.style.width="100%";
        oYearCell.appendChild(oSelectYear);
        for(var i=1977; i<2099;i++)
        {
            var oYear = document.createElement("OPTION");
            oSelectYear.appendChild(oYear);
            oYear.value = i;
            oYear.name = "Y" + i;
            oYear.innerHTML = i;
            if( ((!oRequestDate)?new Date():oRequestDate ).getFullYear() == i)
                oYear.selected = true;
        }
        
        oSelectYear.selectedIndex = ( (!oRequestDate)?new Date():oRequestDate ).getFullYear() - 1977;
        
        oSelectYear.onchange = function() {
            var oCurrentDate = new Date((!Calendar.prototype.currentDate)?new Date():Calendar.prototype.currentDate);
            var oNewDate = new Date(oCurrentDate);
            vall = this.value
            if(vall == 0)
                vall = oCurrentDate.getFullYear();
            oNewDate.setFullYear(vall);
            Calendar(oNewDate,oTrgt,null,null,Calendar.prototype.onDataClick);  
        }
        
    var oMainRow = oMainTable.insertRow(oMainTable.rows.length);
    var oMainCell = oMainRow.insertCell(oMainRow.cells.length);
        oMainCell.colSpan = 3;
                    
    var oNewTable = document.createElement('TABLE');
    oNewTable.cellPadding = 0;
    oNewTable.cellSpacing = 0;
    oNewTable.className = 'Calendar_Table';
    
    myNewRow = oNewTable.insertRow(oNewTable.rows.length);
    for (i=0; i<7; i++)
    {
        myNewCell = myNewRow.insertCell(myNewRow.cells.length);
        myNewCell.className = 'Calendar_days_wdays';
        myNewCell.innerHTML = aWeekDays[i];
    }

    myNewRow = oNewTable.insertRow(oNewTable.rows.length);
    for (var i=0; i<emptyDays; i++) 
    {
        myNewCell = myNewRow.insertCell(myNewRow.cells.length);
        myNewCell.className = 'Calendar_days_out';
        myNewCell.innerHTML = prevMonthDayStart;
        prevMonthDayStart++;
        myNewCell.onmousedown = function() 
        {
            var oCurrentDate = (!Calendar.prototype.currentDate)?new Date():new Date(Calendar.prototype.currentDate);
            var oNewDate = new Date(oCurrentDate);
            oNewDate.setMonth(oCurrentDate.getMonth()-1);
            oNewDate.setDate(parseInt(this.innerHTML));
            Calendar(oNewDate,oTrgt,null,null,Calendar.prototype.onDataClick); 
        }
    }
    
    // Anoaaeyai aie oaeouaai ianyoa
    for (var i=1; i<=currentMonthDaysCount; i++)
    {
        if (!(myNewRow.cells.length % 7)) 
        {
            myNewRow = oNewTable.insertRow(oNewTable.rows.length);
        }
        
        myNewCell = myNewRow.insertCell(myNewRow.cells.length);
        
        var oCurrentDate1 = (!Calendar.prototype.currentDate)?new Date():new Date(Calendar.prototype.currentDate);
        var oNewDate1 = new Date(oCurrentDate1);
        oNewDate1.setDate(parseInt(i));
        var iCurDate = oHilitedDays.indexOf(oNewDate1.toDateString());
        
        if(!oRequestDate)
            myNewCell.className = 'Calendar_days_all';
        else
            myNewCell.className = (i == ((!oRequestDate)?new Date():oRequestDate).getDate()) ? 'Calendar_days_currday' : 'Calendar_days_all';
        
        if (i == oCurrentDate.getDate() && ((!oRequestDate)?new Date():oRequestDate).getMonth() == oCurrentDate.getMonth() && ((!oRequestDate)?new Date():oRequestDate).getFullYear() == oCurrentDate.getFullYear())  {
            myNewCell.style.border = '1px solid red';
        }
        
        myNewCell.innerHTML = i;
        myNewCell.onmousedown = function() 
        {
            var oCurrentDate = (!Calendar.prototype.currentDate)?new Date():new Date(Calendar.prototype.currentDate);
            var oNewDate = new Date(oCurrentDate);
            oNewDate.setDate(parseInt(this.innerHTML));
            if(Calendar.prototype.onDataClick)
                Calendar.prototype.onDataClick(oNewDate);
            Calendar(oNewDate,oTrgt,null,null,Calendar.prototype.onDataClick);
        }

        if(iCurDate >= 0) {
            myNewCell.className += ' Calendar_days_hilited';
        }
        else {
            myNewCell.className += ' Calendar_days_nothilited';
            myNewCell.onmousedown = null;
        }
        
    }

    // Anoaaeyai aie neaao?uaai ianyoa
    var nextMonthDaysCount = (7 - myNewRow.childNodes.length);
    for (var i=1; i<=nextMonthDaysCount; i++)
    {
        myNewCell = myNewRow.insertCell(myNewRow.cells.length);
        myNewCell.className = 'Calendar_days_out';
        myNewCell.innerHTML = i;
        myNewCell.onmousedown = function() 
        {
            var oCurrentDate = (!Calendar.prototype.currentDate)?new Date():new Date(Calendar.prototype.currentDate);
            var oNewDate = new Date(oCurrentDate);
            oNewDate.setMonth(oCurrentDate.getMonth()+1);
            oNewDate.setDate(parseInt(this.innerHTML));
            Calendar(oNewDate,oTrgt,null,null,Calendar.prototype.onDataClick);
        }
    }
    
    
    oMainCell.appendChild(oNewTable);
    

    // Anoaaeyai eaeaiaa?u a eiioaeia?
    if (oContainer.childNodes.length > 0) {
        oContainer.removeChild(oContainer.childNodes[0]);
    }
    
    oContainer.appendChild(oMainTable);
    
}


Calendar.prototype.getLastDay = function(oDate) 
{
    var myDate = new Date(oDate.getYear(), oDate.getMonth()+1, -1);
    return myDate.getDate() + 1;
}

Calendar.prototype.getEmptyWDays = function (oDate) 
{
    var myDate = new Date(oDate)
    myDate.setDate(1);
    return (myDate.getDay()) ? myDate.getDay()-1  : 6;
}

