var calendars = []; // all calendars 
var calwindows = [];
var mounth_str = ['январь','февраль','март','апрель','май','июнь','июль','август','сентябрь','октябрь','ноябрь','декабрь'];

function calendar_create(atarget, nStartstr, nEndstr) {
	this.ie = document.all ? 1 : 0;
	this.ns4 = document.layers ? 1 : 0;
	this.dom = document.getElementById ? 1 : 0;
	var d = document;
	if(nStartstr != undefined){
		nStart = new Date(nStartstr);
		nEnd = new Date(nEndstr);
		
	}
    if (!atarget) return;
	var obj = this.dom ? d.getElementById(atarget) : this.ie ? d.all[atarget] : d.layers[atarget];

	this.target=obj;
    this.id = calendars.length;
    
    if (this.target.value <= "") 
    	var str_date = new Date().getFullYear() + '-' + (new Date().getMonth()+1) + '-' + new Date().getDate();  
    else var str_date = this.target.value;
   	this.target.value = str_date;
  	this.popup=calendar_popup;	
	calendars[this.id]=this;	

}

function close_calendar(calId) {

	calwindows[calId].close();
	
}

function writeday(doc,anid,y,m,d,dow) {
	mm=1*m; if (mm<10) mm="0"+mm;
	dd=1*d; if (dd<10) dd="0"+dd;
	nthisdate = new Date(y,1*mm-1,1*dd);
	if(this.nStart<new Date()){
		if(nthisdate>=this.nStart){
			if(nthisdate<=this.nEnd){
				//doc.write('\r\n<A href="#" onclick="javascript:opener.calendars['+anid+'].target.value=\''+y+'-'+mm+'-'+dd+'\'; window.close(); return false;"><b> '+d+' </b></a>');
				doc.write('\r\n<A href="#" onclick="javascript:opener.location=\''+strLocationBase+'&strDate='+y+'-'+mm+'-'+dd+'\';window.close(); return false;"><b> '+d+' </b></a>');
				return;
			}
		}
	}
	
	doc.write('\r\n<b> '+d+' </b>');
	
}

function forwardback(doc,anid,y,m,txt) {
	ms=1*m;
	if (ms>12) { ms=1; y++; }
	if (ms<1) { ms=12; y--; }
	if (ms<10) ms="0"+ms;
	doc.write('\r\n<td bgcolor=silver><A href="#" onclick="javascript:opener.calendars['+anid+'].target.value=\''+y+'-'+ms+'-01\'; opener.calendars['+anid+'].popup(); return false;">'+txt+'</a></td>');
}

function calendar_popup(fromx) {
	var obj_calwindow =0;

	if (this.target.value <= "") var str_date = new Date().getFullYear() + '-' + (new Date().getMonth()+1) + '-' + new Date().getDate();
	else var str_date = this.target.value;
	
	var w = (screen.width)/2;
	var h = (screen.height)/2;
	
	var posTop =  h-85;
	var posLeft = w-110;

    var arr_date = str_date.split('-');
	if (arr_date.length != 3) { arr_date = str_date.split('.'); };	
	if (arr_date.length != 3) { alert("Неверный формат даты: '" + str_date + "'.\nДопустимый формат ДД.ММ.ГГ или ДД-ММ-ГГГГ"); return; };
	obj_calwindow= window.open('','Календарь','width=220,height=170 status=no,location=no,resizable=no,top='+posTop+',left='+posLeft+',dependent=yes,alwaysRaised=yes');
	
	if (fromx) calwindows[fromx] = obj_calwindow;
	
	dd=1*arr_date[0];
	dd=1*arr_date[2];
	mm=1*arr_date[1];
	yy=1*arr_date[0];
	if (yy<2000) yy=yy+2000;
	if (mm>12) mm=12;
	var tempdate = new Date(yy,mm-1,1);
	var curdate = new Date();
	wdstart=tempdate.getDay(); // ## dnya nedeli v 1 den mesyatsa	
	if (wdstart==0) wdstart=7;
	doc=obj_calwindow.document;
	doc.clear(); // make it empty !!!
	doc.write('<HTML><HEAD><META http-equiv=Content-Type content="text/html; charset=windows-1251"><TITLE>Календарь</TITLE><STYLE>');
	doc.write('TD {COLOR:#000; FONT:11px tahoma,arial; text-align: center; border:1px solid #888;}');	
	doc.write('A:link {	COLOR: #06c; TEXT-DECORATION: none}');
	doc.write('A:visited {COLOR: #039; TEXT-DECORATION: none}');
	doc.write('A:hover {COLOR: #d57919; TEXT-DECORATION: underline}');	
	doc.write('</STYLE></HEAD><BODY marginwidth=0 marginheight=0>');
	doc.write('\r\n<table width=100% cellspacing=1 cellpadding=1><tr>');
	forwardback(doc,this.id,1*yy,1*mm-1,"««");
	doc.write('<td colspan=5>'+mounth_str[mm-1]+' '+yy+'</td>');
	forwardback(doc,this.id,1*yy,1*mm+1,"»»");
	doc.write('</td><tr bgcolor=lightblue><td>пн<td>вт<td>ср<td>чтв<td>птн<td>сб<td>вск</tr>');
	day=1;
	week=1;
	started=0;
	
	for (week=1;week<7;week++) {
		doc.write('<tr>');
		for (weekday=0;weekday<=6;weekday++) {
			wd=weekday+1;
			sdow='';
			if (weekday>=5) sdow=' bgcolor=#FFD0FF';
			if ((mm-1)==curdate.getMonth()) {
				if (day==curdate.getDate()) sdow=' bgcolor=#FFFF00';
			}
			doc.write('<td'+sdow+'>\r\n');
			if (wd==wdstart) { 
					if (started==0)	started=1; 
				}; 
			if (started==1) {
				writeday(doc,this.id,yy,mm,day,wd);
				//doc.write(day);
				day++;
			} // if started
			else {
				doc.write('&nbsp;');
			}; // empty section
			if (day>27) {
				tempdate = new Date(yy,mm-1,day);
				//tempdate.setDate(day);
				if (tempdate.getDate()!=day) started=2;
				if (tempdate.getMonth()!=(mm-1)) started=2; 
			} // day > 27			
		} // for - weekday
		if (started==2) break; // not need empty week-line
	} // week
	doc.write('</table><br>');
	doc.write('</body></html>');
	doc.close();
    obj_calwindow.opener = window;
    obj_calwindow.focus();
    
}
