ver = 0;

var bName = navigator.appName;
var bVer = parseInt(navigator.appVersion);

var showCalendar = new Array();

if (bName == "Netscape" && bVer >= 5) ver = 2;
	else if (bName == "Microsoft Internet Explorer" && bVer >= 4)  ver = 1;


//---------------------------------------------------------------------------------------------------------
// Создать на странице столько календариков сколько нужно ;-) 

var CalendarCreated = 0;

function crtCalendars(){
// Если календари уже созданы - повторяться не надо.
	if (CalendarCreated > 0) {
		return 0;
	}
	for(i = 0; i < showCalendar.length; i++){
		if(showCalendar[i] != "" || showCalendar[i] != null){
			eval(showCalendar[i]);
		}
	}
// Календарь создан - второй раз создавать не надо.
	CalendarCreated = 1;
	return 0;
}

function leapYear(year) {
//Функция, проверяющая високосный ли год...
	if (year % 4 == 0) {
		if (year % 100 == 0)
			if (year % 400 == 0)
				return true;
			else
				return false;
			return true;
  	};
	return false;
}

function DaysInMonth(month, year) {
	var ar = new Array(11);
	ar[0] = 31; ar[1] = (leapYear(year)) ? 29 : 28;
	ar[2] = 31; ar[3] = 30; ar[4] = 31; ar[5] = 30; ar[6] = 31; ar[7] = 31;
	ar[8] = 30; ar[9] = 31; ar[10] = 30; ar[11] = 31;
	return ar[month];
}

function getMonthName(month) {
	var ar = new Array(11);
	ar[0] = "Январь"; ar[1] = "Февраль"; ar[2] = "Март"; ar[3] = "Апрель";
	ar[4] = "Май"; ar[5] = "Июнь"; ar[6] = "Июль"; ar[7] = "Август";
	ar[8] = "Сентябрь"; ar[9] = "Октябрь"; ar[10] = "Ноябрь"; ar[11] = "Декабрь";
	return ar[month];
}

function getNormMonth(month) {
	month++;
	if (month > 9) {
		return month;
	} else{
		return "0"+month;
	}
}

function getShortWkDayName(wday) {
	var wkDay = new Array(6);
	wkDay[0] = "Пн"; wkDay[1] = "Вт"; wkDay[2] = "Ср"; wkDay[3] = "Чт";
	wkDay[4] = "Пт"; wkDay[5] = "Сб"; wkDay[6] = "Вс";
	return wkDay[wday];
}

function drawCal(sdate, ID, tform, tname) {
	var d = new Date();
	var date_pieces = new Array();
	var year;
	var month;
	if ((sdate == "") || (sdate == null)) {
		year = d.getFullYear();
		month = d.getMonth();

	} else {
		date_pieces = sdate.split(".");
		year = date_pieces[2];
		month = date_pieces[1];
		month--;

	}

	var firstDayInstance, firstDay, lastDay;
	firstDayInstance = new Date(year, month, 1);
	firstDay=1;
	var firstDayWeekDay=(firstDayInstance.getDay()-1 < 0) ? 6 : firstDayInstance.getDay()-1;
	firstDayInstance = null;
	lastDay=DaysInMonth(month, year);

	var text = "";
	text += '<table class="CALall" cellspacing="0" cellpadding="0">\n';
	text += '<colgroup span="5" id="Day" />';
	text += '<colgroup span="2" id="HDay" />\n';
	text += '<tr><th class="CAL_move" align="left"><nobr><a class="Move" onClick="javascript:movePrevM('+ "'" +ID+ "'" +');">&lt;</a>';
	text += '<a class="Move" onClick="javascript:movePrevY('+ "'" +ID+ "'" +');">&laquo;</a></nobr></th>';
	text += '<th class="CALmnth" colspan="5">';
	text += '<input type="hidden" name="cal_month" value="'+month+'">';
	text += '<input type="hidden" name="cal_year" value="'+year+'">';
	text += '<input type="text" class="cal_m" onClick="javascript:Off('+"'"+ ID + "'"+');"  SIZE="14" readonly name="cal_m" value="' + getMonthName(month) + ' ' + year + '"></th>';
	text += '<th class="CAL_move" align="right"><nobr><a class="Move" onClick="javascript:moveNextY('+ "'"+ID+ "'" +');">&raquo;</a>';
	text += '<a class="Move" onClick="javascript:moveNextM('+ "'" +ID+ "'" +');">&gt;</a></nobr></th>';
	text += '</tr>\n';

	text += '<tr class="CALwkd">';
	for (var dayNum = 0; dayNum < 7; ++dayNum) {
		text += '<td ' + ((dayNum>4) ? 'class="CALwkdHL" >' : '>') + getShortWkDayName(dayNum) + '</td>';
	}
	text += '</tr>\n';

	var digit = firstDay;
	var curCell = 0;

	for (var row = 1; row <= 6; row++) {
		text += '<tr>';
		for (var col = 1; col <= 7; col++) {
			text += '<td ';
			text += (col > 5) ? 'class="CALdgtHD" >' : '>';
			text += "<input type=hidden name='h" + ID + '_' + col + '_' + row + "' value='";
			if ((curCell >= firstDayWeekDay) && (digit <= lastDay)) {
				text += digit + '.' + getNormMonth(month) + '.' + year;
			}
			text += "'>"
			text += '<input type=text size=2 readonly name="i';
			text += ID + '_' + col+ '_' +row + '"';
			text += ' onClick="javascript:SetDate(' + "'"+tform+"', '"+tname+"', '"+ID+"', ";
			text += "'h" + ID + '_' + col + '_' + row + "'" + ');"';
			if (curCell < firstDayWeekDay) {
				++curCell;
				text += ' class="cal" value="">';
			} else {
				if  (digit <= lastDay) {
					text += 'class="' + ((digit == d.getDate()  && month == d.getMonth() && year == d.getFullYear()) ? 'cal_now' : 'cal') + '"';
					text += ' value="'+ digit +'">';
					digit++;
				} else {
					text += ' class="cal" value="">';
				}
			}
			text += '</td>';
		}
		text += '</tr>\n';
	};
	text += '</table>\n\n';
	return text;
}

function SelectByValue (obj, search_value) {
	var i;

	if (search_value != "") {
		if (obj.options != undefined) {
			for (i = 0; i < obj.length; i++) {
				if (obj.options[i].value == search_value) {
						obj.selectedIndex = i;
						return;
				}
			}
			obj.selectedIndex = 0;
		} else {
			obj.value = search_value;
		}
	}
	return;
}

function SetCal(year, month, ID) {
	var d = new Date();
	var firstDayInstance, firstDay, lastDay;
	firstDayInstance = new Date(year, month, 1);
	firstDay=1;
	var firstDayWeekDay=(firstDayInstance.getDay()-1 < 0) ? 6 : firstDayInstance.getDay()-1;
	firstDayInstance = null;
	lastDay=DaysInMonth(month, year);

	var digit = firstDay;
	var curCell = 0;
	eval("document.forms.cal"+ID+".cal_year.value='"+year+"'");
	eval("document.forms.cal"+ID+".cal_month.value='"+month+"'");
	eval ("document.forms.cal"+ID+".cal_m.value='" + getMonthName(month) + ' ' + year + "'");
	for (var row = 1; row <= 6; row++) {
		for (var col = 1; col <= 7; col++) {
			if ((curCell >= firstDayWeekDay) && (digit <= lastDay)) {
				eval ("document.forms.cal"+ID+".h"+ID+"_"+col+"_"+row+".value='"+ digit + '.' + getNormMonth(month) + '.' + year +"'");
				eval ("document.forms.cal"+ID+".i"+ID+"_"+col+"_"+row+".value='"+ digit +"'");

				eval ("document.forms.cal"+ID+".i"+ID+"_"+col+"_"+row+".className='"+ ((digit == d.getDate()  && month == d.getMonth() && year == d.getFullYear()) ? 'cal_now' : 'cal') +"'");
			} else {
				eval ("document.forms.cal"+ID+".h"+ID+"_"+col+"_"+row+".value=''");
				eval ("document.forms.cal"+ID+".i"+ID+"_"+col+"_"+row+".value=''");
				eval ("document.forms.cal"+ID+".i"+ID+"_"+col+"_"+row+".className='cal'");
			}
			if (curCell < firstDayWeekDay) {
				++curCell;
			} else {
				if  (digit <= lastDay) {
					digit++;
				}
			}
		}
	}
}

function movePrevY (ID) {
	var year = eval("document.forms.cal"+ID+".cal_year.value");
	year--;
	var month = eval("document.forms.cal"+ID+".cal_month.value");
	SetCal (year, month, ID);
}

function movePrevM (ID) {
	var year = eval("document.forms.cal"+ID+".cal_year.value");
	var month = eval("document.forms.cal"+ID+".cal_month.value");
	month--;
	if (month == -1) {
		month = 11;
		year--;
	}
	SetCal (year, month, ID);
}
function moveNextY (ID) {
	var year = document.forms['cal'+ID].cal_year.value;
	year++;
	var month = document.forms['cal'+ID].cal_month.value;
	SetCal (year, month, ID);
}

function moveNextM (ID) {
	var year = document.forms['cal'+ID].cal_year.value;
	var month = document.forms['cal'+ID].cal_month.value;
	month++;
	if (month == 12) {
		month = 0;
		year++;
	}
	SetCal (year, month, ID);
}

function SetDate(tform, tname, fid, id) {
	if (eval("document.forms.cal"+fid+"."+id+".value") ) {
		eval("document.forms."+tform+"."+tname+".value = document.forms.cal"+fid+"."+id+".value");
		new_sdate = eval("document.forms.cal"+fid+"."+id+".value");
		eval("document.forms."+tform+"."+tname+".focus()");
	}
	Off(fid);
}

function GetX (id) {
	var x;
	
	if (id.offsetLeft != undefined) {
		x = id.offsetLeft;
	} else {
		x = 0;
	}
	if (id.tagName.toUpperCase() == "FORM") {
		x = 0;
	}
	if (id.tagName.toUpperCase() == "SPAN") {
		x = 0;
	}

	if (
		(
			((navigator.appName == "Netscape") && id.parentNode)
			||
			((navigator.appName != "Netscape") && id.parentElement)
		) && (id.tagName.toUpperCase() != "HTML")
	) {
		if (navigator.appName == "Netscape") {
			if (id.tagName.toUpperCase() == "TR") {
				x = 0;
			}
			if (id.tagName.toUpperCase() == "TBODY") {
				x = 0;
			}
			x += GetX (id.parentNode);
		} else {
			x += GetX (id.parentElement);
		}
	}
// alert(id.tagName + 'X =>' + id.offsetLeft);

	return x;
}

function GetY (id) {
	var y;

	if (id.offsetTop != undefined) {
		y = id.offsetTop;
//		alert (id.tagName.toUpperCase() + " " + id.offsetTop);
	} else {
		y = 0;
	}
	//alert(id.tagName + ' Y =>' + id.offsetTop);
// Some fixes for table elements
	if (id.tagName.toUpperCase() == "FORM") {
		y = 0;
	}
	if (id.tagName.toUpperCase() == "TD") {
		y = 0;
	}
	if (id.tagName.toUpperCase() == "SPAN") {
		y = 0;
	}
	if (id.tagName.toUpperCase() == "CENTER") {
		y = 0;
	}
	if (
		(
		((navigator.appName == "Netscape") && id.parentNode)
			||
		((navigator.appName != "Netscape") && id.parentElement)
		) && (id.tagName.toUpperCase() != "HTML")
	) {
		if (navigator.appName == "Netscape") {
			y += GetY (id.parentNode);
		} else {
			y += GetY (id.parentElement);
		}
	}
	return y;
}

function CreateLayer(text, id, img) {
	var str2 = "<form name='cal" + id + "'>";
	var str = "";

	if (document.getElementById) {
		str += "<div id='l_cal" + id + "' style=\"position:absolute;top:" + (GetY(img) + 20);
		str += ";left:" + (GetX(img) - 40) + ";visibility:hidden;z-index:-3;display:none;filter:alpha(opacity=100);\"";
		str += ">" + str2;
		str += text + "</form></div>";
	}
	document.write(str);
}

function AppendLayer(text, id, img) {
	var str = "<form name='cal" + id + "'>";

	if (document.getElementById) {
		if (document.getElementById('l_cal'+id) != null) {
			return;
		}

		var oDiv = document.createElement("DIV");
		oDiv.id = 'l_cal'+id;
		oDiv.style.position = 'absolute';
		oDiv.style.top = GetY(img) + 20;
		oDiv.style.left = GetX(img) - 40;
		oDiv.style.visibility = 'hidden';
		oDiv.style.zIndex = -3;
		oDiv.style.display = 'none';
		oDiv.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=100)';
		oDiv.innerHTML = str + text + '</form>';
		document.body.appendChild(oDiv);
	}
}

function Turn(id, img){
	if (document.getElementById) {
		if (img != null && document.getElementById("l_cal" + id) != null) {
			document.getElementById("l_cal" + id).style.left = GetX(img) - 40;
			document.getElementById("l_cal" + id).style.top = GetY(img) + 20;
		}
		if (document.getElementById("l_cal" + id).style.visibility == 'visible') {
			Off(id);
		} else {
			On(id);
		}
	}
}

function On(id){
	if (document.getElementById) {
		document.getElementById("l_cal" + id).style.visibility = 'visible';
		document.getElementById("l_cal" + id).style.display = 'block';
		document.getElementById("l_cal" + id).style.zIndex = 3;
	}
}

function Off(id){
	if (document.getElementById) {
		document.getElementById("l_cal" + id).style.visibility = 'hidden';
		document.getElementById("l_cal" + id).style.display = 'none';
		document.getElementById("l_cal" + id).style.zIndex = -3;
	}
}

var now = new Date();
var yr  = now.getFullYear ();
var mth = now.getMonth();
var day = now.getDate();

function FixCalCng(obj) {
	obj.className = (obj.className == 'cal' ? 'calHD' : 'cal');

	if (document.getElementById) {
		document.getElementById(obj.kf).value = ((obj.className == 'cal') ? 0 : 1);
	}
//	alert(document.getElementById(obj.kf).value);
}

function resetCals(obj) {
	if ((obj != null) && (obj.form != null) && (obj.form.cal_count != null)) {
		var count = obj.form.cal_count.value;
		for (var i = 1; i < count; i++) {
			for (var j = 1; j <= DaysInMonth((document.getElementById('cal_'+i+'_month')).value - 1,document.getElementById('cal_'+i+'_year').value); j++) {
				if (document.getElementById('h'+i+'_'+j).value != document.getElementById('h'+i+'_'+j+'_old').value) {
					FixCalCng(document.getElementById('t'+i+'_'+j));
				}
			}
		}
	}
}
