var NS = (document.all) ? false : true; // MSIE vs. Others
var LAYERS = (document.layers) ? true : false; // NN4 vs. Others
var calWinSize = [210,210];
var calMonths = ['Январь','Февраль','Март','Апрель','Май','Июнь','Июль','Август','Сентябрь','Октябрь','Ноябрь','Декабрь'];
var calWeek = ['Пн','Вт','Ср','Чт','Пт','Сб','Вс'];
var calMonthDays = [31,28,31,30,31,30,31,31,30,31,30,31];
var curInput = null;
var calUseTransit = true; // Whether to use transition alpha effects in MSIE
var calOpacity = 100; // Final opacity of the control in MSIE
var calOpacitySteps = 1; // Number of steps when changing opacity in MSIE

if (LAYERS) document.write("<layer id=calMain visibility=hide><br></layer>");
else document.write("<div id=calMain class=cl_out style='position:absolute; display:none;'><br></div>");


//****************************************************************************
function calGetDate(dateBtn, dateField)
{
	calPosX = (LAYERS) ? dateBtn.x : dateBtn.offsetLeft; 
	calPosY = (LAYERS) ? dateBtn.y : dateBtn.offsetTop + dateBtn.offsetHeight;
	
	for(el=dateBtn.offsetParent; el!=null; el=el.offsetParent) {
		calPosX += el.offsetLeft; calPosY += el.offsetTop;
	}
	
	if(!LAYERS)
	{
		bodyWidth = (NS) ? document.body.offsetWidth : document.body.clientWidth-5;
		while((calPosX > 0) && ((calPosX+calWinSize[0]) > bodyWidth)) calPosX--;
	}

	curInput = dateField.id;
	curDate = dateField.value.split("-");
	nowDate = new Date();
	
	curDate[1] = parseInt(curDate[1]);
	curDate[2] = parseInt(curDate[2]);
	
	if(curDate[1]); // WTF? I get a bug without this line
		calFillInside((((curDate[1]>=1)&&(curDate[1]<=12)) ? (curDate[1]-1) : nowDate.getMonth()), ((curDate[2]>=2000) ? curDate[2] : nowDate.getFullYear()));

	if (LAYERS != false) main = document.layers.calMain;
	else if (NS != false) document.getElementById('calMain'); else document.all.calMain;

	if (LAYERS)
	{
		main.left = calPosX; main.top = calPosY;
		main.visibility = "show";
	} else {
		main.style.left = calPosX+'px'; main.style.top = calPosY+'px';
		if(NS||((!NS)&&(!calUseTransit))) main.style.display = 'block'; else calTransitShow(true, 0);
	}
	
	return false;
}

//****************************************************************************
function calTransitShow(isShow, step) {
	obj = document.getElementById('calMain');
	
	opStart = (isShow) ? 0 : calOpacity;
	opEnd = (isShow) ? calOpacity : 0;
	if(!step) {
		obj.style.filter = "alpha(opacity="+opStart+")";
		obj.style.display = "";
		setTimeout("calTransitShow("+isShow+","+(step+1)+")", 1);
	} else if(step <= (calOpacitySteps+1)) {
		opacity = parseInt( (opStart-opEnd) * (step-1) / calOpacitySteps );
		if(opacity < 0) opacity = -opacity;
		if(!isShow) opacity = 100 - opacity;
		obj.style.filter = "alpha(opacity="+opacity+")";
		setTimeout("calTransitShow("+isShow+","+(step+1)+")", 1);
	} else {
		obj.style.display = (isShow) ? "" : "none";
	}
}

//****************************************************************************
function calExit(yea, mon, day)
{
	if (LAYERS)
	{
		with(document.layers.calMain.document)
		{
			open("text/html");
			write("<br>");
			close();
		}
		document.layers.calMain.visibility = "hide";
	}
	else
	{
		obj = (NS) ? document.getElementById('calMain') : document.all.calMain;
		if (NS||((!NS)&&(!calUseTransit))) obj.style.display = "none"; else calTransitShow(false, 0);
		obj.innerHTML = "<br>";
	}
	
	if (curInput && day && (++mon) && yea)
	{
		curInputObj = (NS) ? curInput : document.all[curInput];
		curInputObj.value = ((day < 10)?"0":"")+day + "-" + ((mon < 10)?"0":"")+mon + "-" + yea;
	}
	return false;
}

//****************************************************************************
function calFillInside(mon, yea) {
	date1 = new Date(2, mon, yea);
	wek1 = date1.getDay() + (((mon > 1) && (!(yea % 4))) ? 1 : 0);
	monthdays = calMonthDays[mon] + (((mon == 1) && (!(yea % 4))) ? 1 : 0);
	
	//alert(calMonthDays[mon]);
	
	main = (LAYERS) ? document.layers.calMain : (NS) ? document.getElementById('calMain') : document.all.calMain;
	if(LAYERS) {
		main.width = calWinSize[0]; 
		main.height = calWinSize[1] + (((monthdays + wek1) > 35) ? 16 : 0);
	} else {
		main.style.width = calWinSize[0]; 
		main.style.height = calWinSize[1] + (((monthdays + wek1) > 35) ? 16 : 0);
	}

	out="";
	if(LAYERS) {
		out += "<table class='clndr' border=1 width=210px cellspacing=5 cellpadding=5><tr><th>";
			//out += "<div class=cl_dayns4>";
			out += "<a class=cl_dayns4 href='javascript:' onclick='return calFillInside("+((mon>0)?mon-1:11)+","+yea+");'>&lt;&lt;&nbsp;</a>";
			out += calMonths[mon];
			out += "<a class=cl_dayns4 href='javascript:' onclick='return calFillInside("+((mon<11)?mon+1:0)+","+yea+");'>&nbsp;&gt;&gt;</a>";
			//out += "</div>";
		out += "</th><th>";
			out += "<div class=cl_dayns4>";
			out += "<a class=cl_dayns4 href='javascript:' onclick='return calFillInside("+mon+","+(yea-1)+");'>&lt;&lt;&nbsp;</a>";
			out += yea;
			out += "<a class=cl_dayns4 href='javascript:' onclick='return calFillInside("+mon+","+(yea+1)+");'>&nbsp;&gt;&gt;</a>";
			out += "</div>";
		out += "</th><th>";
			out += "<a class=cl_dayns4 href='javascript:' onclick='return calExit(0,0,0);'>&nbsp;X&nbsp;</a>";
		out += "</th></tr></table>";
	} else {
		out += "<table class='clndr' border=0 width=210px cellspacing=0 cellpadding=0><tr><th width=10% class=cl_out>";
			out += "<select name=calMon class=cl_sel onchange='calFillInside(this.value,"+yea+")'>";
			for(i=0; i<12; i++) out += "<option value="+i+((i==mon)?" selected":"")+">"+calMonths[i]+"</option>";
			out += "</select>";
		out += "</th><th width=10% class=cl_out>";
			out += "<select name=calYea class=cl_sel onchange='calFillInside("+mon+",this.value)'>";
			for(i=2007; i<=2010; i++) out += "<option value="+i+((i==yea)?" selected":"")+">"+i+"</option>";
			out += "</select>";
		out += "</th><th class=cl_out>";
			out += "<a "+((NS)?"href='javascript:' ":"")+"style='cursor:hand; font:bold xx-small Verdana; color:black; width:100%; text-decoration:none;' onclick='return calExit(0,0,0);' title='Закрыть'>&nbsp;&nbsp;X&nbsp;&nbsp;&nbsp;</a><br>";
		out += "</th></tr></table>";
	}

	out += "<table class='clndr' width=210px border=0 cellspacing=1 cellpadding=0 cols=7><tr>";
		for(i=0; i<7; i++)
			out += ((LAYERS) ? "<th class=cl_wek>"+calWeek[i]+"</th>" : "<th class=cl_out><div class=cl_wek style='font-size: 10px;'>"+calWeek[i]+"</div></th>");
		out += "</tr>";

		if(wek1 > 0)
		{
			out += "<tr>";
			for(daycnt=0; daycnt<wek1; daycnt++)
				out += "<th></th>"; 
				
		}
		else
			daycnt = 0;
			
		for(i=0; i<31; i++)
		{
			if(i >= monthdays) break;
			
			if(!(daycnt % 7))
			{
				if(daycnt > 0) out += "</tr>";
				out += "<tr>";
			}
			out += "<th class=cl_in onmouseover='this.className=\"cl_out\";' onmouseout='this.className=\"cl_in\";'>";
			out += "<a style='font-size: 10px;' class=cl_day"+((LAYERS)?"ns4 ":" ")+((NS|LAYERS)?"href='javascript:' ":"")+"onclick='return calExit("+(i+1)+","+mon+","+yea+");'>"+(i+1)+"</a></th>";
			daycnt++;
		}
	out += "</tr></table>";

	if(LAYERS) {
		with(document.layers.calMain.document) {
			open("text/html"); write(out); close();
		}
	} else {
		obj = (NS) ? document.getElementById('calMain') : document.all.calMain;
		obj.innerHTML = out;
	}
	return false;
}

function debug(msg)
{
	var debug = document.getElementById('txtdebug')
	var d = new Date();
	var tempTimestamp =
	(d.getHours() < 10 ? '0' + d.getHours() : d.getHours())
	+ ':' +
	(d.getMinutes() < 10 ? '0' + d.getMinutes() : d.getMinutes())
	+ ':' +
	(d.getSeconds() < 10 ? '0' + d.getSeconds() : d.getSeconds())
	debug.value = tempTimestamp + ':' + msg + '\n' + debug.value ;
}

function selectOption()
{
	var x=document.getElementById("s1");
	var selObj = document.getElementById('s2');
	selObj.selectedIndex = x.selectedIndex + 1;
}
