var 	monthArray = ['ßíâàðü','Ôåâðàëü','Ìàðò','Àïðåëü','Ìàé','Èþíü','Èþëü','Àâãóñò','Ñåíòÿáðü','Îêòÿáðü','Íîÿáðü','Äåêàáðü'];
var 	dayArray = ['Ïí','Âò','Ñð','×ò','Ïò','Ñá','Âñ'];
var	calendar_format = "dd.mm.yyyy";
var	calendar_popup_id='calendarpopup';

var	calendar_popup=new calendar();
function calendar_date(parentelem,ename,fname)
{
	calendar_popup.popup("calendar_popup",parentelem,fname,ename,"dd.mm.yyyy",0);
}

function calendar()
{
	this.div=null;
}

calendar.prototype.popup = function(varname,elem,formname,inputname,format,yeartype)
{
	if(this.div && this.div.style.display=='block')
	{
		this.div.style.display='none';
		return;
	}

	this.varname=varname;
	this.id=calendar_popup_id;
	this.elem=elem;
	this.formname=formname;
	this.yeartype=yeartype;
	this.inputname=inputname;
	this.input=document.forms[formname].elements[inputname];
	this.format=format;
	this.date=this.parse(this.input.value,this.format);
	this.div=null;
	if(document.getElementById(this.id))
	{
		this.div=document.getElementById(calendar_popup_id);
	}
	else
	{
		this.div=document.createElement('DIV');
		this.div.id=this.id;
		this.div.style.display='none';
		document.body.appendChild(this.div);
	}
	this.div.style.position='absolute';
	this.div.className='calendar';
	this.div.style.left=5;
	this.div.style.top=5;

	this.draw(this.date.getDate(),this.date.getMonth()+1,this.date.getFullYear());

	this.x=offsetx(this.elem);
	this.y=offsety(this.elem)+this.elem.offsetHeight+2;
	this.w=this.div.offsetWidth;
	this.h=this.div.offsetHeight;
	if(this.x>document.body.scrollLeft+window.innerWidth-15-this.w)
		this.x=document.body.scrollLeft+window.innerWidth-this.w-15;
	if(this.y>document.body.scrollTop+window.innerHeight-15-this.h)
		this.y=document.body.scrollTop+window.innerHeight-this.h-15;
	this.div.style.left=(this.x>0?this.x:5)+"px";
	this.div.style.top=(this.y>0?this.y:5)+"px";
}

calendar.prototype.out = function(elem)
{
	if(elem.className=='dayover')
		elem.className='day';
}

calendar.prototype.over = function(elem)
{
	if(elem.className=='day')
		elem.className='dayover';
}

calendar.prototype.cancel = function()
{
	document.getElementById(this.id).style.display='none';
}

calendar.prototype.ok = function()
{
	this.input.value=document.getElementById(this.id+'current').innerHTML;
	if(this.input.onchange)
		this.input.onchange();
	document.getElementById(this.id).style.display='none';
}

calendar.prototype.drawdate = function(d,m,y)
{
	var str=this.format;
	str = str.replace('dd',intformat(d,2));
	str = str.replace('mm',intformat(m,2));
	str = str.replace('yyyy',y);
	return str;
}

calendar.prototype.click = function(d,m,y)
{
	this.date=new Date(y,m-1,d);
	this.draw(d,m,y);
	document.getElementById(this.id+'current').innerHTML=this.drawdate(d,m,y);
}

calendar.prototype.parse = function(value,format)
{
	if(value.length==format.length)
	{
		var monthPos = format.indexOf('mm');
		var currentMonth = value.substr(monthPos,2);
		var yearPos = format.indexOf('yyyy');
		var currentYear = value.substr(yearPos,4);
		var dayPos = format.indexOf('dd');
		var currentDay = value.substr(dayPos,2);
		return new Date(currentYear,currentMonth-1,currentDay);
	}
	else
	{
		return new Date();
	}
}

calendar.prototype.draw = function(day,month,year)
{
	var i,j,inserted,b='',y,jumped,start,weeks;

	var monthdays = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	var drawday = new Date(year,month-1,1);

	month = drawday.getMonth();
	var wday = drawday.getDay()-1;
	if(wday<0) wday=6;
	var days = monthdays[month];
	year = drawday.getFullYear();
	if(month == 1 && !(year%4) && year%100)
		days = 29;

	b+="<table border='0' cellspacing='0' cellpadding=1>";
	b+="<tr><th colspan=7 align=center id="+this.id+"current>"+this.drawdate(this.date.getDate(),this.date.getMonth()+1,this.date.getFullYear())+"</th></tr>";
	b+="<tr>";
	b+="<td colspan=7>";
	b+="<select class=month name=month onchange='"+this.varname+".draw(1,this.options[selectedIndex].value,"+year+");'>";
	for(i=1;i<=12;i++)
		if(i==month+1)
			b+="<option value="+i+" selected>"+ monthArray[i-1] + "</option>";
		else
			b+="<option value="+i+">"+ monthArray[i-1] + "</option>";
	b+="</select>";
	b+="<select class=year name=year onchange='"+this.varname+".draw(1,"+(month+1)+",this.options[selectedIndex].value);'>";

	switch(this.yeartype)
	{
	default:
		var today = new Date();

		for(i=1;i<=10;i++)
		{
			j=today.getFullYear()-10+i;
			if(j==year)
				b+="<option value="+j+" selected>"+ j + "</option>";
			else
				b+="<option value="+j+">"+ j + "</option>";
		}
		break;
	}
	b+="</select>";
	b+="</td>";
	b+="</tr>";
	b+="<tr>";
	for(i=0;i<7;i++)
		b+="<th>"+dayArray[i]+"</th>";
	b+="</tr>";
	jumped = 0;
	inserted = 1;
	start = wday;
	if (start < 0)
		start += 7;
	weeks = parseInt((start + days)/7);
	if ((start + days)%7 != 0)
		weeks++;
	for(i=weeks; i>0; i--)
	{
		b+="<tr height=24 >";
		for (j=7; j>0; j--)
		{
			switch(j)
			{
			default:
				cc='';
				break;
			case 2:
				cc=" id=saturday ";
				break;
			case 1:
				cc=" id=sunday ";
				break;
			}
			if(jumped<start || inserted>days)
			{
				b+="<td "+cc+">&nbsp;";
				jumped++;
			}
			else
			{
				title=" title='"+intformat(inserted,2)+"."+intformat(month,2)+"."+year+"' ";
				event1=" onmouseover='"+this.varname+".over(this);' onmouseout='"+this.varname+".out(this);' onclick='"+this.varname+".click("+inserted+","+(month+1)+","+year+");'";

				if(drawday.getFullYear()==this.date.getFullYear() && drawday.getMonth() == this.date.getMonth() && inserted==this.date.getDate())
					b+="<td class=day "+title+" id=selectedday "+event1+">";
				else
					b+="<td class=day "+title+cc+event1+">";
				b+=inserted;
				inserted++;
			}
			b+="</td>";
		}
		b+="</tr>";
	}
	b+="<tr>";
	b+="<td colspan=7 style='padding:10' nowrap>";
	b+="<input class=submit type=submit value='Cancel' onclick='"+this.varname+".cancel();'>&nbsp;";
	b+="<input class=submit type=submit value='Ok' onclick='"+this.varname+".ok();'>";
	b+="</td>";
	b+="</tr>";
	b+="</table>";
	document.getElementById(this.id).innerHTML=b;
	document.getElementById(this.id).style.display='block';
}

