/*
Script: Calendar
Version: 1.0
Date: 25.02.2004
Author: Alexander Egorov
Email: alexon@htc-cs.com
*/

var _cf=new Object();

_cf.lang=(typeof(_cf_lang)=="string") ? _cf_lang : 'ru';
_cf.target=(typeof(_cf_target)=="string") ? _cf_target : '_self';
_cf.theme=(typeof(_cf_theme)=="string") ? _cf_theme : 'blue';
_cf.cookie_expires=(typeof(_cf_cookie_expires)=="number") ? _cf_cookie_expires : 30;
_cf.monday_first=(typeof(_cf_monday_first)=="number") ? _cf_monday_first : 1;
_cf.mark_weekend=(typeof(_cf_mark_weekend)=="number") ? _cf_mark_weekend : 1;
_cf.show_div=(typeof(_cf_show_div)=="number") ? _cf_show_div : 1;
_cf.show_top_navigate=(typeof(_cf_show_top_navigate)=="number") ? _cf_show_top_navigate : 1;
_cf.show_bottom_navigate=(typeof(_cf_show_bottom_navigate)=="number") ? _cf_show_bottom_navigate : 1;
_cf.show_links=(typeof(_cf_show_links)=="number") ? _cf_show_links : 1;
_cf.show_all_links=(typeof(_cf_show_all_links)=="number") ? _cf_show_all_links : 0;
_cf.week_length=(typeof(_cf_week_length)=="number") ? _cf_week_length : 2;
_cf.week_case=(typeof(_cf_week_case)=="string") ? _cf_week_case : 'normal';
_cf.format_day=(typeof(_cf_format_day)=="string") ? _cf_format_day : 'dd';
_cf.format_month=(typeof(_cf_format_month)=="string") ? _cf_format_month : 'mm';
_cf.format_year=(typeof(_cf_format_year)=="string") ? _cf_format_year : 'yyyy';
_cf.year_start=(typeof(_cf_year_start)=="number") ? _cf_year_start : 2000;
_cf.year_end=(typeof(_cf_year_end)=="number") ? _cf_year_end : 2005;
_cf.return_url=(typeof(_cf_return_url)=="string") ? _cf_return_url : '?year=#year#&month=#month#&day=#day#';

_cf.skin=new Object();
_cf.skin['blue']=new Object();
_cf.skin['blue'].font_size='11px';
_cf.skin['blue'].font_family='verdana';
_cf.skin['blue'].font_color='#333333';
_cf.skin['blue'].font_color_header='#B5DFF7';
_cf.skin['blue'].font_color_selected='#FF0000';
_cf.skin['blue'].link_color='#0065B5';
_cf.skin['blue'].link_color_hover='#CE285A';
_cf.skin['blue'].link_hover='none';
_cf.skin['blue'].bg_header='#0071B5';
_cf.skin['blue'].bg_main='#FFFFFF';
_cf.skin['blue'].bg_today='#C6E3EF';
_cf.skin['blue'].bg_weekend='#E6E6E6';

_cf.skin['red']=new Object();
_cf.skin['red'].font_size='11px';
_cf.skin['red'].font_family='verdana';
_cf.skin['red'].font_color='#333333';
_cf.skin['red'].font_color_header='#FFFFFF';
_cf.skin['red'].font_color_selected='#FF0000';
_cf.skin['red'].link_color='#990000';
_cf.skin['red'].link_color_hover='#990000';
_cf.skin['red'].link_hover='underline';
_cf.skin['red'].bg_header='#990000';
_cf.skin['red'].bg_main='#FFFFFF';
_cf.skin['red'].bg_today='#EEAAAA';
_cf.skin['red'].bg_weekend='#E6E6E6';

_cf.skin['gray']=new Object();
_cf.skin['gray'].font_size='10px';
_cf.skin['gray'].font_family='verdana';
_cf.skin['gray'].font_color='#AAAAAA';
_cf.skin['gray'].font_color_header='#FFFFFF';
_cf.skin['gray'].font_color_selected='#000000';
_cf.skin['gray'].link_color='#666666';
_cf.skin['gray'].link_color_hover='#000000';
_cf.skin['gray'].link_hover='underline';
_cf.skin['gray'].bg_header='#A0A0A0';
_cf.skin['gray'].bg_main='#FFFFFF';
_cf.skin['gray'].bg_today='#E0E0E0';
_cf.skin['gray'].bg_weekend='#E6E6E6';

_cf.month=new Array();
_cf.month['ru']=new Array("","ßíâàðü","Ôåâðàëü","Ìàðò","Àïðåëü","Ìàé","Èþíü","Èþëü","Àâãóñò","Ñåíòÿáðü","Îêòÿáðü","Íîÿáðü","Äåêàáðü");
_cf.month['en']=new Array("","January","February","March","April","May","June","July","August","September","October","November","December");
_cf.days=new Array();
if (_cf.monday_first==0) {
	_cf.days['ru']=new Array("Âñê","Ïíä","Âòð","Ñðä","×òâ","Ïòí","Ñáò");
	_cf.days['en']=new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
} else {
	_cf.days['ru']=new Array("Ïíä","Âòð","Ñðä","×òâ","Ïòí","Ñáò","Âñê");
	_cf.days['en']=new Array("Mon","Tue","Wed","Thu","Fri","Sat","Sun");
}
_cf.today=new Array();
_cf.today['ru']='ñåãîäíÿ';
_cf.today['en']='today';
_cf.today['de']='heute';
_cf.today['it']='today';
_cf.today['no']='today';
_cf.today['fr']='today';
_cf.today['sp']='today';

var _skin=_cf.skin[_cf.theme];

document.write("<style>");
document.write("a:link.calendarlink {font-family: "+_skin.font_family+"; font-size: "+_skin.font_size+"; color: "+_skin.link_color+"; text-decoration: none;}");
document.write("a:visited.calendarlink {font-family: "+_skin.font_family+"; font-size: "+_skin.font_size+"; color: "+_skin.link_color+"; text-decoration: none;}");
document.write("a:hover.calendarlink {font-family: "+_skin.font_family+"; font-size: "+_skin.font_size+"; color: "+_skin.link_color_hover+"; text-decoration: "+_skin.link_hover+";}");
document.write("a:active.calendarlink {font-family: "+_skin.font_family+"; font-size: "+_skin.font_size+"; color: "+_skin.link_color+"; text-decoration: none;}");
document.write(".calendar {font-family: "+_skin.font_family+"; font-size: "+_skin.font_size+"; color: "+_skin.font_color+";}");
document.write(".calendarheader {font-family: "+_skin.font_family+"; font-size: "+_skin.font_size+"; color: "+_skin.font_color_header+";}");
document.write("</style>");

var _nowDate=new Date();
var _url_today=_cf.return_url.replace("#year#",_f_format_date('year',_nowDate.getFullYear()));
_url_today=_url_today.replace("#month#",_f_format_date('month',_nowDate.getMonth()+1));
_url_today=_url_today.replace("#day#",_f_format_date('day',_nowDate.getDate()));

document.write("<table width=100% border=0 cellpadding=0 cellspacing=2>");
if (_cf.show_top_navigate==1) {
	document.write("<tr><td>");
	document.write("<select id=\"_month_\" name=\"_month_\" style=\"font-size: "+_skin.font_size+";\" onchange=_f_change_month(this.options.selectedIndex)>");
	for (i=1;i<_cf.month[_cf.lang].length;i++) {
		if (_nowDate.getMonth()+1!=i) {
			document.write("<option value="+i+">"+_cf.month[_cf.lang][i]);
		} else {
			document.write("<option value="+i+" selected>"+_cf.month[_cf.lang][i]);
		}
	}
	document.write("</select>");
	document.write("</td><td align=right>");
	document.write ("<select id=\"_year_\" name=\"_year_\" style=\"font-size: "+_skin.font_size+";\" onchange=_f_change_year(this.options[this.options.selectedIndex])>");
	for (i=_cf.year_start;i<=_cf.year_end;i++) {
		if (_nowDate.getFullYear() != i) {
			document.write("<option value="+i+">"+i+"</option>");
		} else {
			document.write("<option value="+i+" selected>"+i+"</option>");
		}
	}
	document.write("</select></td></tr>");
}
document.write("<tr><td colspan=2>");
document.write("<table width=100% border=0 bgcolor="+_skin.bg_main+" cellspacing=0 cellpadding=1><tr bgcolor="+_skin.bg_header+">");
for (i=0;i<7;i++) {
	document.write("<td class=calendarheader align=center><font color="+_skin.font_color_header+">");
	if (_cf.week_case!='normal') {
		if (_cf.week_case=='upper') {
			document.write(_cf.days[_cf.lang][i].substring(0,_cf.week_length).toUpperCase());
		} else {
			document.write(_cf.days[_cf.lang][i].substring(0,_cf.week_length).toLowerCase());
		}
	} else {
		document.write(_cf.days[_cf.lang][i].substring(0,_cf.week_length));
	}
	document.write("</td>");
}
document.write("</tr>");
for (j=0;j<6;j++) {
	document.write("<tr>");
	for (i=0;i<7;i++) {
		document.write("<td "+((_cf.show_div==1) ? "style=\"border-bottom: 1px solid #DDDDDD;\"" : "")+" align=center><div class=calendar id=d"+i+"r"+j+"></div></td>");
	}
	document.write("</tr>");
}
document.write("</table>");
if (_cf.show_bottom_navigate==1) {
	document.write("<table width=100% cellpadding=0 cellspacing=0>");
	document.write("<tr><td width=100% align=center>");
	document.write("<a class=calendarlink href=\"#\" onclick=\"_f_chg_year(-1); return false;\">««</a>&nbsp;&nbsp; <a class=calendarlink href=\"#\" onclick=\"_f_chg_month(-1); return false;\">«</a>&nbsp;&nbsp; <a class=calendarlink href=\""+_url_today+"\" onclick=\"_f_chg_today();\">"+_cf.today[_cf.lang]+"</a>&nbsp;&nbsp; <a class=calendarlink href=\"#\" onclick=\"_f_chg_month(1); return false;\">»</a>&nbsp;&nbsp; <a class=calendarlink href=\"#\" onclick=\"_f_chg_year(1); return false;\">»»</a>");
	document.write("</tr></td>");
	document.write("</table>");
}
document.write("</td></tr></table>");
var _showDate=new Date();

function _f_donothing() {

}

function _f_format_date(_t,_var) {
	switch (_t) {
		case 'year': {
			return _var.toString().substr(4-_cf.format_year.length);
			break;
		}
		case 'month': {
			return (_cf.format_month.length==2 && _var<10) ? '0'+_var : _var;
			break;
		}
		case 'day': {
			return (_cf.format_day.length==2 && _var<10) ? '0'+_var : _var;
			break;
		}
	}
}

function _f_chg_today() {
	_nowDate=new Date();
	_f_setCookie('dd',_nowDate.getDate(),"","");
	_f_setCookie('mm',_nowDate.getMonth()+1,"","");
	_f_setCookie('yy',_nowDate.getFullYear(),"","");
	_f_setCalendar();
}

function _f_chg_month(_inc) {
	var _mx=document.getElementById('_month_');
	var _mt=_mx.options.selectedIndex+_inc;
	if (_mt<0) {
		if (_f_chg_year(-1)) {
			_f_change_month(11);
			_mx.options.selectedIndex=11;
			return true;
		} else {
			return false;
		}
	}
	if (_mt>11) {
		if (_f_chg_year(1)) {
			_f_change_month(0);
			_mx.options.selectedIndex=0;
			return true;
		} else {
			return false;
		}
	}
	_f_change_month(_mt);
	_mx.options.selectedIndex=_mt;
}

function _f_chg_year(_inc) {
	var _yx=document.getElementById('_year_');
	var _yt=_yx.options.selectedIndex+_inc;
	if (_yt<0) {return false;}
	if (_yt>=_yx.options.length) {return false;}
	_f_change_year(_yx.options[_yt]);
	_yx.options.selectedIndex=_yt;
	return true;
}

function _f_set_cal(_showDate) {
	var _begin_day=new Date(_showDate.getFullYear(),_showDate.getMonth());
	var _begin_day_date=_begin_day.getDay();
	var _end_day=new Date(_showDate.getFullYear(),_showDate.getMonth()+1);
	var _count_day=(_end_day-_begin_day)/1000/60/60/24;
	if (_begin_day.getMonth()==2) {
		_count_day=31;
	} else {
		if (_begin_day.getMonth()==9) {_count_day=31;}
	}
	_f_init_table(_begin_day_date,_count_day);
}

_f_set_cal(_showDate);

function _f_init_table(_begin,_count) {
	if (_cf.monday_first==1) {if (_begin!=0) {_begin--;}}
	_f_init();
	j=0;
	if (_begin!=0) {i=_begin;} else {i=0;}
	for (c=1;c<_count+1;c++) {
		var _cell_id="d"+i+"r"+j;
		_cell_id=document.getElementById(_cell_id);
		if ((_nowDate.getDate()==c) && (_showDate.getMonth()==_nowDate.getMonth()) && (_showDate.getFullYear()==_nowDate.getFullYear())) {
			_cell_id.style.backgroundColor=_skin.bg_today;
		}
		var _dd=c;
		var _mm=_showDate.getMonth()+1;
		var _yy=_showDate.getFullYear();
		_url=_cf.return_url.replace("#year#",_f_format_date('year',_showDate.getFullYear()));
		_url=_url.replace("#month#",_f_format_date('month',_showDate.getMonth()+1));
		_url=_url.replace("#day#",_f_format_date('day',c));
		if (_cf.show_all_links==1) {
			_cell_id.innerHTML="<a class=calendarlink target=\""+_cf.target+"\" href=\""+_url+"\" onclick=\"_f_setdate("+_dd+","+_mm+","+_yy+");\">"+c+"</a>";
		} else if (_cf.show_links==1) {
			if ((_showDate.getMonth()<=_nowDate.getMonth()) && (_showDate.getFullYear()<=_nowDate.getFullYear()) || (_showDate.getFullYear()<_nowDate.getFullYear())) {
				if ((_showDate.getMonth()==_nowDate.getMonth()) && (_showDate.getFullYear()==_nowDate.getFullYear())) {
					if (_nowDate.getDate()<c) {
						_cell_id.innerHTML=c;
					} else {
						_cell_id.innerHTML="<a class=calendarlink target=\""+_cf.target+"\" href=\""+_url+"\" onclick=\"_f_setdate("+_dd+","+_mm+","+_yy+");\">"+c+"</a>";
					}
				} else {
					_cell_id.innerHTML="<a class=calendarlink target=\""+_cf.target+"\" href=\""+_url+"\" onclick=\"_f_setdate("+_dd+","+_mm+","+_yy+");\">"+c+"</a>";
				}
			} else {
				_cell_id.innerHTML=c;
			}
		} else {
			_cell_id.innerHTML=c;
		}
		if (c==_f_getCookie('dd') && _showDate.getMonth()==(_f_getCookie('mm')-1) && _showDate.getFullYear()==_f_getCookie('yy')) {
			_cell_id.innerHTML="<font color="+_skin.font_color_selected+">"+c+"</font>";
		}
		i++;
		if (i==7) {i=0;j++;}
	}
	_f_after_init();
}

function _f_init() {
	for(j=0;j<6;j++) {
		for(i=0;i<7;i++) {
			var _cell_id="d"+i+"r"+j;
			_cell_id=document.getElementById(_cell_id);
			_cell_id.innerHTML="-";
			_cell_id.style.backgroundColor="";
			_cell_id.style.color=_skin.font_color;
		}
	}
}

function _f_after_init() {
	for(j=0;j<6;j++) {
		for(i=0;i<7;i++) {
			var _cell_id="d"+i+"r"+j;
			_cell_id=document.getElementById(_cell_id);
			if (_cell_id.innerHTML=="-") {
				_cell_id.style.color=_skin.bg_main;
			}
			if (_cf.mark_weekend==1) {
				if (_cf.monday_first==0) {
					if (i==0 || i==6) {
						if (_cell_id.style.backgroundColor!=_skin.bg_today.toLowerCase()) {
							_cell_id.style.backgroundColor=_skin.bg_weekend;
						}
						if (_cell_id.innerHTML=="-") {_cell_id.style.color=_skin.bg_weekend;}
					}
				} else {
					if (i==5 || i==6) {
						if (_cell_id.style.backgroundColor!=_skin.bg_today.toLowerCase()) {
							_cell_id.style.backgroundColor=_skin.bg_weekend;
						}
						if (_cell_id.innerHTML=="-") {_cell_id.style.color=_skin.bg_weekend;}
					}
				}
			}
		}
	}
}

function _f_change_month(_sel_month) {
	_showDate=new Date(_showDate.getFullYear(),_sel_month,1);
	_f_set_cal(_showDate);
}

function _f_change_year(_sel_year) {
	_sel_year=_sel_year.value;
	_showDate=new Date(_sel_year,_showDate.getMonth(),1);
	_f_set_cal(_showDate);
}

function _f_setCookie(_cookieName, _cookieValue, _cookiePath, _cookieExpires) {
	var _now_Date=new Date();
	_cookieValue=escape(_cookieValue);
 	if (_cookieExpires=="") {
		_now_Date.setMinutes(_now_Date.getMinutes()+_cf.cookie_expires);
		_cookieExpires=_now_Date.toGMTString();
	}
	if (_cookiePath!="") {
		_cookiePath=";Path="+_cookiePath;
	}
	document.cookie=_cookieName+"="+_cookieValue+"; expires="+_cookieExpires+_cookiePath;
}

function _f_getCookie(_sName) {
	var _aCookie=document.cookie.split("; ");
	for (var i=0;i<_aCookie.length;i++) {
		var _aCrumb=_aCookie[i].split("=");
			if (_sName==_aCrumb[0]) {
				return unescape(_aCrumb[1]);
			}
	}
	return null;
}

function _f_setdate(_dd,_mm,_yy) {
	_f_setCookie("dd",_dd,"","");	
	_f_setCookie("mm",_mm,"","");	
	_f_setCookie("yy",_yy,"","");	
	_f_change_month(_mm-1);
	_f_set_day();
	return true;
}

function _f_setCalendar() {
	if (_f_getCookie("dd")=="") {_f_setCookie("dd",_nowDate.getDate(),"","")}
	if (_f_getCookie("mm")=="") {_f_setCookie("mm",_nowDate.getMonth(),"","")}
	if (_f_getCookie("yy")=="") {_f_setCookie("yy",_nowDate.getFullYear(),"","")}
	_f_set_year();
	_f_set_month();
	_f_set_day();
	return true;
}

function _f_set_year() {
	var _yy=_f_getCookie('yy');
	if (_yy && _yy!="") {
		var _yx=document.getElementById('_year_');
		for (k=0;k<_yx.options.length;k++) {
			if (_yx.options[k].value==_yy) {
				_f_change_year(_yx.options[k]);
				_yx.options.selectedIndex=k;
			}
		}
	}
	return true;
}
	
function _f_set_month() {
	var _mm=_f_getCookie('mm');
	if (_mm>=1 && _mm<=12) {
		var _mx=document.getElementById('_month_');
		var _mv=(_mm==1) ? 0 : _mm-1;
		_f_change_month(_mv);
		_mx.options.selectedIndex=_mv;
	}
	return true;
}
	
function _f_set_day() {
	var _dd=_f_getCookie('dd');
	j=0;
	i=1;
	for (c=1;c<42+1;c++) {
		_cell_id="d"+i+"r"+j;
		_cell_id=document.getElementById(_cell_id);
		if (_cell_id) {
			if (_cell_id.innerText==_dd) {_cell_id.innerHTML="<font color="+_skin.font_color_selected+">"+_cell_id.innerText+"</font>";}
		}
		i++;
		if (i==7) {i=0;j++;}
	}
	return true;
}

_f_setCalendar();