function VPA_calendar(name,id)
{
    this.name=name;
    this.id=id;
    this.months=Array('Январь','Февраль','Март','Апрель','Май','Июнь','Июль','Август','Сентябрь','Октябрь','Ноябрь','Декабрь');
    this.days=Array('Пн','Вт','Ср','Чт','Пт','Сб','Вс');
    this.date=new Date();
    this.day=this.date.getDate();
    this.day_week=this.date.getDay();
    this.month_cur=this.month=this.date.getMonth();
    this.year_cur=this.year=this.date.getYear()<1000 ? 1900+this.date.getYear() : this.date.getYear();
    this.month_next=(this.month+1<12) ? this.month+1 : 0;
    this.year_next=(this.month+1<12) ? this.year : this.year+1;
    this.ret_attribute;
    this.calendar=document.getElementById(this.id);
    this.cal_table_left=document.getElementById('cal_left');
    this.cal_table_right=document.getElementById('cal_right');
    this.calendar.style.display='none';
    this.shown=0;
    
    
    this.show=function(ret_attribute,start_month,start_year)
    {
        this.calendar.style.display='block';
        var cal_head_left=this.cal_table_left.getElementsByTagName('thead')[0]; 
        cc=cal_head_left.rows[0].cells[0];
        start_month=(start_month==-1) ? this.month : start_month;
        start_year=(start_year==-1) ? this.year : start_year;
        this.month=start_month;
        this.year=start_year;
        this.month_next=(this.month+1<12) ? this.month+1 : 0;
        this.year_next=(this.month+1<12) ? this.year : this.year+1;
        this.year=(this.month<12) ? this.year : this.year+1;
        this.month=(this.month<12) ? this.month : 0;
        
        if (this.shown==0)
        {
            this.set_months(this.month,this.year,this.cal_table_left);
            this.set_months(this.month_next,this.year_next,this.cal_table_right);
        }
        this.set_days_for_month(this.month,this.year,this.cal_table_left);
        this.set_days_for_month(this.month_next,this.year_next,this.cal_table_right);
        this.ret_attribute=ret_attribute;
        this.shown+=1;
    }
    
    this.next=function()
    {
        this.year=(this.month+1<12) ? this.year : this.year+1;
        this.month=(this.month+1<12) ? this.month+1 : 0;
        this.shown=0;
        this.show(this.ret_attribute,this.month,this.year);
    }

    this.prev=function()
    {
        this.shown=0;
        this.year=(this.month-1>0) ? this.year : this.year-1;
        this.month=(this.month-1>0) ? this.month-1 : 11;
        this.show(this.ret_attribute,this.month,this.year);
    }


    this.hide=function()
    {
        this.calendar.style.display='none';
    }
    
    this.set_months=function(start_month,start_year,cal_tb)
    {
        var cal_head=cal_tb.getElementsByTagName('thead')[0]; 
        cc=cal_head.rows[0];
        var vpa_sel=cc.getElementsByTagName('span')[0];
        vpa_sel.innerHTML=this.months[start_month]+' '+start_year;
    }
    
    this.set_days_for_month=function(month,year,cal_tb)
    {
        //var cal_tb=this.cal_table;
        var cal_body=cal_tb.getElementsByTagName('tbody')[0];
        var vpa_date=new Date(year,month,1,12,0); 
        var offset_date=vpa_date.getDay();
        var offset_date=(offset_date)?offset_date-1:6;
        for (var i=27;i<=32;i++) if (month!=new Date(year,month,i,12,0).getMonth()) break;
        var days_on_month=i-1;
        var tds=cal_body.getElementsByTagName('td'); 
        for (var i=0;i<tds.length;i++) 
        {
            tds[i].innerHTML='';
            if (i<offset_date || i+1-offset_date>days_on_month) 
            {
                tds[i].appendChild(document.createTextNode(' '));
            }
            else
            {
                var offset_day=i+1-offset_date;
                var day_node=document.createTextNode(i+1-offset_date);
                if (this.month_cur==month && this.year_cur==year && offset_day<this.day)  
                {
                    tds[i].appendChild(day_node);
                    tds[i].className='noactive';
                    tds[i].style.cssText='font-weight:normal; cursor:standart;';
                    tds[i].onclick=null;
                }
                else
                {
                    tds[i].appendChild(day_node);
                    tds[i].style.cssText='font-weight:bold; cursor:pointer;';
                    tds[i].setAttribute('name',this.name); 
                    tds[i].setAttribute('class',offset_day); 
                    tds[i].onclick=function()
                    {
                        if (typeof(window.vpa_calendar_day_click)=="function")
                        {
                            var obj=window[this.getAttribute('name')];
                            var day=this.getAttribute('class');
                            vpa_calendar_day_click(day,(month+1),year,obj.ret_attribute);
                            obj.hide();
                        }
                    }
                }
            }
        }
    }
}

VPA_calendar.prototype.toString=function() { return 'Object VPA_calendar\nDeveloped by Andrey Pahomov\nAndrey.Pahomov@gmail.com';};
