/* Òðåáóåò ôàéëû date.js è popup.js è div.js */
function CalendarPopup(d){
var c=new DivPopup(d);
c.dayHeaders=new Array("Ïí","Âò","Ñð","×ò","Ïò","Ñá","Âñ");
c.curDate=null;
c.refreshCalendar=CP_refreshCalendar;
c.getCalendar=CP_getCalendar;
c.select=CP_select;
return c;
}

function CP_refreshCalendar(){
var c=this;
if(arguments.length>1)c.getCalendar(arguments[0],arguments[1]);else c.getCalendar();
}

function CP_select(l,scd,ecd,fmt,fn,sd,dx,dy,pref){
if(pref.length>0)this.pref=pref;
this.returnFunction=fn;
this.dateFormat=fmt;
this.curDate=sd==null?new Date():new Date(getDateFromFormat(sd,fmt));
this.criticalStartDate=scd==null?null:getDateFromFormat(scd,fmt);
this.criticalEndDate=ecd==null?null:getDateFromFormat(ecd,fmt);
this.getCalendar();
this.show(l,dx,dy);
}

function CP_setClick(obj,id,date){
var e=document.getElementById(id);
if(e!=null){
e.onclick=date==null?null:function(){obj.returnFunction(date);obj.hide();};
e.style.cursor=date==null?'default':'pointer';}
}

function CP_setMonthClick(obj,id,m,y){
var e=document.getElementById(id);
if(e!=null){e.onclick=function(){obj.refreshCalendar(m,y)}}
}

function CP_setText(id,t){var e=document.getElementById(id);if(e!=null)e.innerHTML=t;}
function CP_setHref(id,h){var e=document.getElementById(id);if(e!=null)e.href=h;}

function CP_setClass(id,c){
var e=document.getElementById(id);
if(e!=null){
e.className=c;
e.onmouseover=function(){e.className=c+"_hover";};
e.onmouseout=function(){e.className=c};}
}

function CP_getCalendar(){
var n=new Date();
var m=arguments.length>0?arguments[0]:this.curDate.getMonth()+1;
var y=(arguments.length>1&&arguments[1]>0&&arguments[1]-0==arguments[1])?arguments[1]:this.curDate.getFullYear();
var dim=new Array(0,31,28,31,30,31,30,31,31,30,31,30,31);
if(((y%4==0)&&(y%100!=0))||(y%400==0))dim[2]=29;
var w=new Date(y,m-1,1).getDay();if(w==0)w=7;
var nm=m+1;var nmy=y;
if(nm>12){nm=1;nmy++;}
var lm=m-1;var lmy=y;
if(lm<1){lm=12;lmy--;}
CP_setMonthClick(this,this.pref+"l_pred_month",lm,lmy);
CP_setMonthClick(this,this.pref+"l_next_month",nm,nmy);
CP_setText(this.pref+"cur_month",MONTH_NAMES[m-1]+' '+y);
for(var i=1;i<=42;i++){
var num=null;
if(i>=w&&i<dim[m]+w)num=i-w+1;
CP_setText(this.pref+i,num!=null?num:'&nbsp;');
var c=new Date(y,m-1,num);
var e=(c>=this.criticalStartDate||this.criticalStartDate==null)&&(c<=this.criticalEndDate||this.criticalEndDate==null);
if(num!=null){
if(m==this.curDate.getMonth()+1&&num==this.curDate.getDate()&&y==this.curDate.getFullYear())
CP_setClass(this.pref+i,this.pref+'number_selected');
else if(m==n.getMonth()+1&&num==n.getDate()&&y==n.getFullYear())
CP_setClass(this.pref+i,this.pref+'now');
else if(e)CP_setClass(this.pref+i,this.pref+'number');
else CP_setClass(this.pref+i,this.pref+'disabled_number')
if(e)CP_setClick(this,this.pref+i,formatDate(c,this.dateFormat));
else CP_setClick(this,this.pref+i,null);
}else{CP_setClass(this.pref+i,this.pref+'disabled_number');CP_setClick(this,this.pref+i,null);}
}
}
