function show_calendar(str_target, str_date) {
	var arr_months = ["ЯНВАРЬ", "ФЕВРАЛЬ", "МАРТ", "АПРЕЛЬ", "МАЙ", "ИЮНЬ", "ИЮЛЬ", "АВГУСТ", "СЕНТЯБРЬ", "ОКТЯБРЬ", "НОЯБРЬ", "ДЕКАБРЬ"];
	var week_days = ["Вс", "Пн", "Вт", "Ср", "Чт", "Пт", "Сб"];
	var n_weekstart = 1;

	if (str_date != null && str_date != "") {
		if (str2dt2(str_date) == false) {
		 	alert("Неверный формат даты: "+str_date);
			return;
		}
	}
	var dt_date = (str_date == null || str_date =="" ?  new Date() : str2dt2(str_date));
	var dt_prev_month = new Date(dt_date);
	dt_prev_month.setMonth(dt_date.getMonth()-1);
	if (dt_date.getMonth()%12 != (dt_prev_month.getMonth()+1)%12) {
		dt_prev_month.setMonth(dt_date.getMonth());
		dt_prev_month.setDate(0);
	}
	var dt_prev_year = new Date(dt_date);
	dt_prev_year.setMonth(dt_date.getMonth()-12);

	var dt_next_month = new Date(dt_date);
	dt_next_month.setMonth(dt_date.getMonth()+1);
	if ((dt_date.getMonth() + 1)%12 != dt_next_month.getMonth()%12)
		dt_next_month.setDate(0);

	var dt_next_year = new Date(dt_date);
	dt_next_year.setMonth(dt_date.getMonth()+12);

	var dt_firstday = new Date(dt_date);
	dt_firstday.setDate(1);
	dt_firstday.setDate(1-(7+dt_firstday.getDay()-n_weekstart)%7);
	var dt_lastday = new Date(dt_next_month);
	dt_lastday.setDate(0);

	/*
	var str_buffer = new String (
		"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n"+
		"<html>\n"+
		"<head>\n"+
		"<title>Выбор даты</title>\n"+
		"<meta http-equiv='Content-Type' content='text/html; charset=windows-1251'>\n"+
		"<link rel='stylesheet' type='text/css' href='/common/css/calendar.css'>\n"+
		"</head>\n"+
		"<body>\n"+
		"<table class='calendar'>\n"+
		"<tr class='tr_title'>\n<td class='left'><a href='javascript:window.opener.show_calendar(\""+str_target+"\", \""+dt2dtstr2(dt_prev_year)+"\");'><img src='/common/img/nav_first.gif' alt='Предыдущий год' title='Предыдущий год'></a>&nbsp;"+
		"<a href='javascript:window.opener.show_calendar(\""+str_target+"\", \""+ dt2dtstr2(dt_prev_month)+"\");'>"+
		"<img src='/common/img/nav_prev.gif' alt='Предыдущий месяц' title='Предыдущий месяц'></a></td>\n"+
		"<td class='title'>"+arr_months[dt_date.getMonth()]+" "+dt_date.getFullYear()+"</td>\n"+
		"<td class='right'><a href='javascript:window.opener.show_calendar(\""+str_target+"\", \""+dt2dtstr2(dt_next_month)+"\");'>"+
		"<img src='/common/img/nav_next.gif' alt='Следующий месяц' title='Следующий месяц'></a>&nbsp;<a href='javascript:window.opener.show_calendar(\""+str_target+"\", \""+dt2dtstr2(dt_next_year)+"\");'><img src='/common/img/nav_last.gif' alt='Следующий год' title='Следующий год'></a></td>\n</tr>\n</table>\n"
	);*/
	
		var str_buffer = new String (
		"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n"+
		"<html>\n"+
		"<head>\n"+
		"<title>Выбор даты</title>\n"+
		"<meta http-equiv='Content-Type' content='text/html; charset=windows-1251'>\n"+
		"<link rel='stylesheet' type='text/css' href='/common/css/calendar.css'>\n"+
		"</head>\n"+
		"<body>\n"+
		"<table class='calendar'>\n"+
		"<tr class='tr_title'>\n<td class='left'>&nbsp;&nbsp;<a href='javascript:window.opener.show_calendar(\""+str_target+"\", \""+dt2dtstr2(dt_prev_year)+"\");'><b>&lt;&lt;</b></a>&nbsp;"+
		"<a href='javascript:window.opener.show_calendar(\""+str_target+"\", \""+ dt2dtstr2(dt_prev_month)+"\");'>"+
		"<b>&lt;</b></a></td>\n"+
		"<td class='title'>"+arr_months[dt_date.getMonth()]+" "+dt_date.getFullYear()+"</td>\n"+
		"<td class='right'><a href='javascript:window.opener.show_calendar(\""+str_target+"\", \""+dt2dtstr2(dt_next_month)+"\");'>"+
		"<b>&gt;</b></a>&nbsp;<a href='javascript:window.opener.show_calendar(\""+str_target+"\", \""+dt2dtstr2(dt_next_year)+"\");'><b>&gt;&gt;</b></a>&nbsp;&nbsp;</td>\n</tr>\n</table>\n"
	);

	
	
	
	

	var dt_current_day = new Date(dt_firstday), tr_num = 0;
	str_buffer += "<table class='calendar'>\n<tr class='days'>\n";
	for (var n=0; n<7; n++) {
		if(n == 6) str_buffer += "<td class='rest_day'>";
		else str_buffer += "<td>";
		str_buffer += week_days[(n_weekstart+n)%7]+"</td>\n";
	}
	str_buffer += "</tr>\n";
	while (dt_current_day.getMonth() == dt_date.getMonth() ||
		dt_current_day.getMonth() == dt_firstday.getMonth()) {
		tr_num++;
		str_buffer += "<tr align='center'>\n";
		for (var n_current_wday=0; n_current_wday<7; n_current_wday++) {
				if (dt_current_day.getDate() == dt_date.getDate() && dt_current_day.getMonth() == dt_date.getMonth()) {
					str_buffer += "		<td class='sel'>";
				}
				else if (dt_current_day.getDay() == 0 || dt_current_day.getDay() == 6) {
					str_buffer += "		<td>";
				}
				else {
					str_buffer += "		<td>";
				}
				if (dt_current_day.getMonth() == dt_date.getMonth()) {
					str_buffer += "<a class='month' href='javascript:window.opener."+str_target+
					".value=\""+dt2dtstr2(dt_current_day)+"\"; self.close();'>"+
					"";
				}
				else {
					str_buffer += "<a href='javascript:window.opener."+str_target+
					".value=\""+dt2dtstr2(dt_current_day)+"\"; self.close();'>"+
					"";
				}
				str_buffer += dt_current_day.getDate()+"</a></td>\n";
				dt_current_day.setDate(dt_current_day.getDate()+1);
		}

		str_buffer += "	</tr>\n";
	}

	str_buffer += "<tr><td colspan=7 class='current-date'>Текущая дата: <a href=\"javascript:window.opener."+str_target+".value='"+
	dt2dtstr2(new Date())+"';self.close();\"><b>"+dt2dtstr2(new Date())+"</b></a></td></tr>";

	str_buffer +=
		"</table>\n" +
		"</body>\n" +
		"</html>\n";

	var vWinCal = window.open("", "Calendar", "width=180,height=186,frameborder=0,toolbar=0,location=0,directoties=0,status=0,menubar=0,resizable=0,top=200,left=200");
	vWinCal.opener = self;
	vWinCal.focus();
	var calc_doc = vWinCal.document;
	calc_doc.write(str_buffer);
	calc_doc.close();
}

function str2dt2 (str_date) {
	var re_date = /^(\d+)\.(\d+)\.(\d+)$/;
	if (!re_date.exec(str_date)) {
		return (false);
	} else {
		var arrD = new Array();
		var m, d, Y;
		arrD = str_date.split(".");
		d = parseInt(correct_number(arrD[0]));
		m = parseInt(correct_number(arrD[1])) - 1;
		Y = parseInt(arrD[2]);
		return (new Date(Y, m, d));
		//return (new Date (RegExp.$3, RegExp.$2-1, RegExp.$1));
	}
}

function dt2dtstr2 (dt_date) {
var bday, bmonth;
bday = dt_date.getDate().toString();
bmonth = (dt_date.getMonth()+1).toString();

if (bmonth.length == 1) {
	bmonth = "0" + bmonth;
}
if (bday.length == 1) {
	bday = "0" + bday;
}

	return (new String (
			bday+"."+bmonth+"."+dt_date.getFullYear()));
}

function correct_number(strIn) {
	var res;
	if (strIn.length = 2 && (strIn.substr(0, 1) == '0')) {
		res = strIn.substr(1, 1);
	} else {
		res = strIn;
	}
	return (res);
}
