// Tigra Calendar v4.0.2 (12-01-2009) European (dd-mm-yyyy)
// http://www.softcomplex.com/products/tigra_calendar/
// Public Domain Software... You're welcome.

/*
	MB: Kalenterille käy seuraavat muuttujat (18.3.2009)
	Nimi               Esimerkki    Kuvaus (P=pakollinen)
	formname         : 'contact'  : Lomakkeen nimi (P)
	controlname      : 'birthday' : Päiväyskentän nimi (P)
	yearMode         : 'select'   : Vuosivalinnan tapa, select/scroll
	startYear        : '1960'     : Vuosivalinnan aloitusvuosi (oletuksena 1960)
	endYear          : '2020'     : Vuosivalinnan lopetusvuosi (oletuksena kuluva vuosi)
	timepicker       : 1          : Näytetään ajanvalintatoiminto (oletuksena 0)
	update           : 'end_time' : Automaattisesti päivitettävän toisen päiväyskentän nimi
	update_set_hours : '7'        : Automaattisesti päivitettävän kentän arvon kasvu
	weeknumbers      : '1'        : Näytetään kalenterissa viikkonumerot (oletus 0)
	onclick          : 'msg()'    : Javascript-funktio joka suoritetaan päiväyksen valinnan jälkeen
*/

// default settins
var A_TCALDEF = {
	'months' : ['Tammikuu', 'Helmikuu', 'Maaliskuu', 'Huhtikuu', 'Toukokuu', 'Kesäkuu', 'Heinäkuu', 'Elokuu', 'Syyskuu', 'Lokakuu', 'Marraskuu', 'Joulukuu'],
	'weekdays' : ['Su', 'Ma', 'Ti', 'Ke', 'To', 'Pe', 'La'],
	'week_title'  : 'V',
	'clock_title'  : 'Klo.',
	'yearscroll': true, // show year scroller
	'weekstart': 1, // first day of week: 0-Su or 1-Mo
	'centyear'  : 70, // 2 digit years less than 'centyear' are in 20xx, othewise in 19xx.
	'next_year'  : 'Seuraava vuosi',
	'next_month'  : 'Seuraava kuukausi',
	'prev_year'  : 'Edellinen vuosi',
	'prev_month'  : 'Edellinen kuukausi',
	'set_time'  : 'Päivitä kellonaika',
	'select_date'  : 'Valitse päivämäärä',
	'close_cal'  : 'Sulje kalenteri',
	'imgpath' : 'template/html/default/img/datepicker/' // directory with calendar images
}
var A_TCALeng = {
	'months' : ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
	'weekdays' : ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'],
	'week_title'  : 'W',
	'clock_title'  : 'Time',
	'yearscroll': true,
	'weekstart': 1,
	'centyear'  : 70,
	'next_year'  : 'Next year',
	'next_month'  : 'Next month',
	'prev_year'  : 'Previous year',
	'prev_month'  : 'Previous month',
	'set_time'  : 'Set time',
	'select_date'  : 'Select date',
	'close_cal'  : 'Close calendar',
	'imgpath' : 'template/html/default/img/datepicker/'
}
// date parsing function
function f_tcalParseDate (s_stamp) {

	// Tarkastetaan onko päiväyksessä kellonaikaa
	if (s_stamp.indexOf(' ') > 0){
		s_date = s_stamp.substr(0, s_stamp.indexOf(' '));
	}
	else{
		s_date = s_stamp;
	}

	// Tarkastetaan päiväystä
	var re_date = /^\s*(\d{1,2})\.(\d{1,2})\.(\d{2,4})\s*$/;
	if (!re_date.exec(s_date))
		return alert ("Invalid date: '" + s_date + "'.\nAccepted format is dd.mm.yyyy.")

	var n_day = Number(RegExp.$1),
		n_month = Number(RegExp.$2),
		n_year = Number(RegExp.$3);

	if (n_year < 100)
		n_year += (n_year < this.a_tpl.centyear ? 2000 : 1900);
	if (n_month < 1 || n_month > 12)
		return alert ("Invalid month value: '" + n_month + "'.\nAllowed range is 01-12.");
	var d_numdays = new Date(n_year, n_month, 0);
	if (n_day > d_numdays.getDate())
		return alert("Invalid day of month value: '" + n_day + "'.\nAllowed range for selected month is 01 - " + d_numdays.getDate() + ".");


	return new Date (n_year, n_month - 1, n_day);
}
function f_tcalParseTime (s_stamp) {

	// Tarkastetaan onko päiväyksessä kellonaikaa
	if (s_stamp.indexOf(' ') > 0){
		s_time = s_stamp.substr(s_stamp.indexOf(' ')+1, s_stamp.length);
	}
	else{
		s_time = s_stamp;
	}

	// Tarkastetaan päiväystä
	var re_date = /^\s*(\d{1,2})\:(\d{1,2})\s*$/;
	if (!re_date.exec(s_time))
		return false;

	var n_hour = Number(RegExp.$1),
		n_minute = Number(RegExp.$2);

	if (n_hour < 0 || n_hour > 23)
		return false;
	if (n_minute < 0 || n_minute > 59)
		return false;

	return s_time;
}

function f_tcalCheckTime (s_time) {

	if (s_time.indexOf(':') > 0){

		// Tarkastetaan kellonaikaa
		var re_time = /^\s*(\d{1,2})\:(\d{1,2})\s*$/;
		if (!re_time.exec(s_time))
			return alert ("Invalid time: '" + s_time + "'.\nAccepted format is HH:MM.");

		var n_hour = Number(RegExp.$1),
			n_minute = Number(RegExp.$2);

		if (n_hour < 0 || n_hour > 24)
			return alert ("Invalid hour value: '" + n_hour + "'.\nAllowed range is 00-23.");
		if (n_minute < 0 || n_minute > 59)
			return alert ("Invalid minute value: '" + n_minute + "'.\nAllowed range is 00-59.");

		return true;
	}
	else
		return false;
}

function f_tcalCheckStamp (s_stamp) {
	if (s_stamp.indexOf(' ') > 0){

		s_date = s_stamp.substr(0, s_stamp.indexOf(' '));
		s_time = s_stamp.substr(s_stamp.indexOf(' '), s_stamp.length);

		// Tarkastetaan päiväystä
		var re_date = /^\s*(\d{1,2})\.(\d{1,2})\.(\d{2,4})\s*$/;
		if (!re_date.exec(s_date))
			return false;

		var n_day = Number(RegExp.$1),
			n_month = Number(RegExp.$2),
			n_year = Number(RegExp.$3);

		if (n_year < 100)
			n_year += (n_year < this.a_tpl.centyear ? 2000 : 1900);
		if (n_month < 1 || n_month > 12)
			return false;
		var d_numdays = new Date(n_year, n_month, 0);
		if (n_day > d_numdays.getDate())
			return false;


		// Tarkastetaan kellonaikaa
		var re_time = /^\s*(\d{1,2})\:(\d{1,2})\s*$/;
		if (!re_time.exec(s_time))
			return false;

		var n_hour = Number(RegExp.$1),
			n_minute = Number(RegExp.$2);

		if (n_hour < 0 || n_hour > 24)
			return false;
		if (n_minute < 0 || n_minute > 59)
			return false;

		return true;
	}
	else
		return false;
}

// date generating function
function f_tcalGenerDate (d_date) {
	return (
		(d_date.getDate() < 10 ? '0' : '') + d_date.getDate() + "."
		+ (d_date.getMonth() < 9 ? '0' : '') + (d_date.getMonth() + 1) + "."
		+ d_date.getFullYear()
	);
}
/*
function f_tcalGenerTime (d_time) {
	return (d_time.getHours() + ":"
	 + (d_time.getMinutes() < 10 ? '0' : '') + (d_time.getMinutes()));
}
*/
// implementation
function tcal(a_cfg, a_tpl) {

	// apply default template if not specified
	if (!a_tpl)
		a_tpl = A_TCALDEF;

	// register in global collections
	if (!window.A_TCALS)
		window.A_TCALS = [];
	if (!window.A_TCALSIDX)
		window.A_TCALSIDX = [];

	this.s_id = a_cfg.id ? a_cfg.id : A_TCALS.length;
	window.A_TCALS[this.s_id] = this;
	window.A_TCALSIDX[window.A_TCALSIDX.length] = this;

	// assign methods
	this.f_show = f_tcalShow;
	this.f_hide = f_tcalHide;
	this.f_toggle = f_tcalToggle;
	this.f_update = f_tcalUpdate;
	this.f_setYear = f_setYear;
	this.f_relDate = f_tcalRelDate;
	this.f_parseDate = f_tcalParseDate;
	this.f_parseTime = f_tcalParseTime;
	this.f_generDate = f_tcalGenerDate;
	this.f_checkTime = f_tcalCheckTime;
	this.f_checkStamp = f_tcalCheckStamp;
	this.f_timechange = f_timeChange;
	this.f_updateAnotherField = updateAnotherField;
	this.f_onClick = f_onClick;

	// create calendar icon
	this.s_iconId = 'tcalico_' + this.s_id;
	this.e_icon = f_getElement(this.s_iconId);
/* // Tää ei vaan toimi oikein
	if (!this.e_icon) {
		//document.write('<img src="' + a_tpl.imgpath + 'cal.gif" id="' + this.s_iconId + '" onclick="A_TCALS[\'' + this.s_id + '\'].f_toggle()" class="tcalIcon" alt="Open Calendar" />');
		this.e_icon = f_getElement(this.s_iconId);
	}
*/

	if (!a_cfg.startYear){
		a_cfg.startYear = 1960;
	}
	if (!a_cfg.endYear){
		date = new Date;
		a_cfg.endYear = date.getFullYear();
	}

	// save received parameters
	this.a_cfg = a_cfg;
	this.a_tpl = a_tpl;
	this.time = '';
	this.time_prev = '';
	this.date = '';
	this.date_prev = '';
}

function f_tcalShow (d_date) {

	// find input field
	if (!this.a_cfg.controlname)
		throw("TC: control name is not specified");
	if (this.a_cfg.formname) {
		var e_form = document.forms[this.a_cfg.formname];
		if (!e_form)
			throw("TC: form '" + this.a_cfg.formname + "' can not be found");
		this.e_input = e_form.elements[this.a_cfg.controlname];
	}
	else
		this.e_input = f_getElement(this.a_cfg.controlname);

	if (!this.e_input || !this.e_input.tagName || this.e_input.tagName != 'INPUT')
		throw("TC: element '" + this.a_cfg.controlname + "' does not exist in "
			+ (this.a_cfg.formname ? "form '" + this.a_cfg.controlname + "'" : 'this document'));

	// dynamically create HTML elements if needed
	this.e_div = f_getElement('tcal');
	if (!this.e_div) {
		this.e_div = document.createElement("DIV");
		this.e_div.id = 'tcal';
		document.body.appendChild(this.e_div);
	}
	this.e_shade = f_getElement('tcalShade');
	if (!this.e_shade) {
		this.e_shade = document.createElement("DIV");
		this.e_shade.id = 'tcalShade';
		document.body.appendChild(this.e_shade);
	}
	this.e_iframe =  f_getElement('tcalIF')
	if (b_ieFix && !this.e_iframe) {
		this.e_iframe = document.createElement("IFRAME");
		this.e_iframe.style.filter = 'alpha(opacity=0)';
		this.e_iframe.id = 'tcalIF';
		this.e_iframe.src = this.a_tpl.imgpath + 'pixel.gif';
		document.body.appendChild(this.e_iframe);
	}

	// hide all calendars
	f_tcalHideAll();

	// generate HTML and show calendar
	this.e_icon = f_getElement(this.s_iconId);
	if (!this.f_update())
		return;

	this.e_div.style.visibility = 'visible';
	this.e_shade.style.visibility = 'visible';
	if (this.e_iframe)
		this.e_iframe.style.visibility = 'visible';

	// change icon and status
	this.e_icon.src = this.a_tpl.imgpath + 'no_cal.gif';
	this.e_icon.title = this.a_tpl.close_cal;
	this.b_visible = true;
}

function f_tcalHide (n_date) {

	if (n_date){
		this.date = this.f_generDate(new Date(n_date));
		this.e_input.value = this.date;
	}
	if (this.a_cfg.timepicker){
		if (this.f_checkTime(this.time) && this.f_checkStamp(this.e_input.value+' '+this.time))
			this.e_input.value += ' '+ this.time;
		else if (this.prev)
			this.e_input.value += ' '+ this.prev;
	}

	// no action if not visible
	if (!this.b_visible)
		return;

	// hide elements
	if (this.e_iframe)
		this.e_iframe.style.visibility = 'hidden';
	if (this.e_shade)
		this.e_shade.style.visibility = 'hidden';
	this.e_div.style.visibility = 'hidden';

	// change icon and status
	this.e_icon = f_getElement(this.s_iconId);
	this.e_icon.src = this.a_tpl.imgpath + 'cal.gif';
	this.e_icon.title = this.a_tpl.select_date;
	this.b_visible = false;

	// Päivitetään muiden kenttien arvoja
	this.f_updateAnotherField();

	// Suoritetaan onClick-eventti
	if (this.time != this.time_prev || this.date != this.date_prev)
		this.f_onClick();
}

function f_tcalToggle () {
	return this.b_visible ? this.f_hide() : this.f_show();
}

function f_tcalUpdate (d_date) {

	var d_client = new Date();
	d_client.setHours(0);
	d_client.setMinutes(0);
	d_client.setSeconds(0);
	d_client.setMilliseconds(0);

	var d_today = this.a_cfg.today ? this.f_parseDate(this.a_cfg.today) : d_client;

	var d_selected = this.e_input.value == ''
		? (this.a_cfg.selected ? this.f_parseDate(this.a_cfg.selected) : d_today)
		: this.f_parseDate(this.e_input.value);


	// figure out date to display
	if (!d_date)
		// selected by default
		d_date = d_selected;
	else if (typeof(d_date) == 'number')
		// get from number
		d_date = new Date(d_date);
	else if (typeof(d_date) == 'string')
		// parse from string
		this.f_parseDate(d_date);

	if (!d_date) return false;

	this.date = this.f_generDate(d_date);
	this.date_prev = this.date;

	// first date to display
	var d_firstday = new Date(d_date);
	d_firstday.setDate(1);
	d_firstday.setDate(1 - (7 + d_firstday.getDay() - this.a_tpl.weekstart) % 7);

	// Piirretään vuoden valintadropdown
	if (this.a_cfg.yearMode == 'select' && this.a_cfg.startYear < this.a_cfg.endYear){
		var a_class, s_html = '<table class="ctrl"><tbody><tr>'
			+ '<td' + this.f_relDate(d_date, -1) + ' title="' + this.a_tpl.prev_month + '"><img src="' + this.a_tpl.imgpath + 'prev_mon.gif" /></td><th>'
			+ this.a_tpl.months[d_date.getMonth()] + ' ';

		s_html = s_html + '<select id="selYear" onchange="A_TCALS[\'' + this.s_id + '\'].f_setYear(\''+d_date+'\',this.value)">';

		for (i=this.a_cfg.startYear; i<=this.a_cfg.endYear; i++){
			s_html = s_html + '<option'
			+ (i === d_date.getFullYear() ? ' selected ' :' ')
			+'value="'+i+'">'+i+'</option>';
		}

		s_html = s_html	+ '</select>'
			+ '</th><td' + this.f_relDate(d_date, 1) + ' title="' + this.a_tpl.next_month + '"><img src="' + this.a_tpl.imgpath + 'next_mon.gif" /></td>'
			+ '</tr></tbody></table><table><tbody><tr class="wd">';
	}
	// Vuosia scrollataan next/prev-tyyliin
	else{
		var a_class, s_html = '<table class="ctrl"><tbody><tr>'
			+ (this.a_tpl.yearscroll ? '<td' + this.f_relDate(d_date, -1, 'y') + ' title="' + this.a_tpl.prev_year + '"><img src="' + this.a_tpl.imgpath + 'prev_year.gif" /></td>' : '')
			+ '<td' + this.f_relDate(d_date, -1) + ' title="' + this.a_tpl.prev_month + '"><img src="' + this.a_tpl.imgpath + 'prev_mon.gif" /></td><th>'
			+ this.a_tpl.months[d_date.getMonth()] + ' ' + d_date.getFullYear()
			+ '</th><td' + this.f_relDate(d_date, 1) + ' title="' + this.a_tpl.next_month + '"><img src="' + this.a_tpl.imgpath + 'next_mon.gif" /></td>'
			+ (this.a_tpl.yearscroll ? '<td' + this.f_relDate(d_date, 1, 'y') + ' title="' + this.a_tpl.next_year + '"><img src="' + this.a_tpl.imgpath + 'next_year.gif" /></td></td>' : '')
			+ '</tr></tbody></table><table><tbody><tr class="wd">';
	}

	// Viikon numeron head
	if (this.a_cfg.weeknumbers){
		document.getElementById('tcal').style.width = '178px';
		s_html +='<th>' + this.a_tpl.week_title + '</th>';
	}

	// print weekdays titles
	for (var i = 0; i < 7; i++)
		s_html += '<th>' + this.a_tpl.weekdays[(this.a_tpl.weekstart + i) % 7] + '</th>';
	s_html += '</tr>' ;

	// print calendar table
	var d_current = new Date(d_firstday);

	// Tarkastetaan päiväys. Otetaan 'nyt' jos inputista ei aikaa saada.
	if (!this.f_checkTime(this.time)){
		this.time = this.f_parseTime(this.e_input.value);

		if (!this.time){
			var t_current = new Date();
			this.time = (t_current.getHours()+':'
				+((t_current.getMinutes() < 10 ? '0' : '') + t_current.getMinutes()));
		}

		this.time_prev = this.time;
	}


	while (d_current.getMonth() == d_date.getMonth() ||
		d_current.getMonth() == d_firstday.getMonth()) {

		// print row heder
		s_html +='<tr>';

		// Viikonnumerot
		if (this.a_cfg.weeknumbers){
			s_html +='<td class="tweek">'+ (d_current.getWeek()) +'</td>';
		}

		for (var n_wday = 0; n_wday < 7; n_wday++) {

			a_class = [];
			// other month
			if (d_current.getMonth() != d_date.getMonth())
				a_class[a_class.length] = 'othermonth';
			// weekend
			if (d_current.getDay() == 0 || d_current.getDay() == 6)
				a_class[a_class.length] = 'weekend';
			// today
			if (d_current.valueOf() == d_today.valueOf())
				a_class[a_class.length] = 'today';
			// selected
			if (d_current.valueOf() == d_selected.valueOf())
				a_class[a_class.length] = 'selected';

			s_html += '<td onclick="A_TCALS[\'' + this.s_id + '\'].f_hide(' + d_current.valueOf() + ')"' + (a_class.length ? ' class="' + a_class.join(' ') + '">' : '>') + d_current.getDate() + '</td>'
			d_current.setDate(d_current.getDate() + 1);
		}
		// print row footer
		s_html +='</tr>';
	}

	// MB:aika
	if (this.a_cfg.timepicker){
		s_html +='<tr>';
		s_html +='<td class="time_left" colspan="2">' + this.a_tpl.clock_title + '</td>';
		s_html +='<td class="time" colspan="3"><input onblur="A_TCALS[\'' + this.s_id + '\'].f_timechange(this.value);" type="text" value="'+this.time+'" class="timefield"></td>';
		s_html +='<td class="time_right" colspan="2"><img class="button_refreshtime" title="' + this.a_tpl.set_time + '" src="' + this.a_tpl.imgpath + 'time.gif" onclick="A_TCALS[\'' + this.s_id + '\'].f_hide(' + d_date.valueOf() + ')"></td>';
		s_html +='</tr>';
	}

	s_html +='</tbody></table>';

	// update HTML, positions and sizes
	this.e_div.innerHTML = s_html;

	var n_width  = this.e_div.offsetWidth;
	var n_height = this.e_div.offsetHeight;
	var n_top  = f_getPosition (this.e_icon, 'Top') + this.e_icon.offsetHeight;
	var n_left = f_getPosition (this.e_icon, 'Left') - n_width + this.e_icon.offsetWidth;
	if (n_left < 0) n_left = 0;

	this.e_div.style.left = n_left + 'px';
	this.e_div.style.top  = n_top + 'px';

	this.e_shade.style.width = (n_width + 8) + 'px';
	this.e_shade.style.left = (n_left - 1) + 'px';
	this.e_shade.style.top = (n_top - 1) + 'px';
	this.e_shade.innerHTML = b_ieFix
		? '<table><tbody><tr><td rowspan="2" colspan="2" width="6"><img src="' + this.a_tpl.imgpath + 'pixel.gif"></td><td width="7" height="7" style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + this.a_tpl.imgpath + 'shade_tr.png\', sizingMethod=\'scale\');"><img src="' + this.a_tpl.imgpath + 'pixel.gif"></td></tr><tr><td height="' + (n_height - 7) + '" style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + this.a_tpl.imgpath + 'shade_mr.png\', sizingMethod=\'scale\');"><img src="' + this.a_tpl.imgpath + 'pixel.gif"></td></tr><tr><td width="7" style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + this.a_tpl.imgpath + 'shade_bl.png\', sizingMethod=\'scale\');"><img src="' + this.a_tpl.imgpath + 'pixel.gif"></td><td style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + this.a_tpl.imgpath + 'shade_bm.png\', sizingMethod=\'scale\');" height="7" align="left"><img src="' + this.a_tpl.imgpath + 'pixel.gif"></td><td style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + this.a_tpl.imgpath + 'shade_br.png\', sizingMethod=\'scale\');"><img src="' + this.a_tpl.imgpath + 'pixel.gif"></td></tr><tbody></table>'
		: '<table><tbody><tr><td rowspan="2" width="6"><img src="' + this.a_tpl.imgpath + 'pixel.gif"></td><td rowspan="2"><img src="' + this.a_tpl.imgpath + 'pixel.gif"></td><td width="7" height="7"><img src="' + this.a_tpl.imgpath + 'shade_tr.png"></td></tr><tr><td background="' + this.a_tpl.imgpath + 'shade_mr.png" height="' + (n_height - 7) + '"><img src="' + this.a_tpl.imgpath + 'pixel.gif"></td></tr><tr><td><img src="' + this.a_tpl.imgpath + 'shade_bl.png"></td><td background="' + this.a_tpl.imgpath + 'shade_bm.png" height="7" align="left"><img src="' + this.a_tpl.imgpath + 'pixel.gif"></td><td><img src="' + this.a_tpl.imgpath + 'shade_br.png"></td></tr><tbody></table>';

	if (this.e_iframe) {
		this.e_iframe.style.left = n_left + 'px';
		this.e_iframe.style.top  = n_top + 'px';
		this.e_iframe.style.width = (n_width + 6) + 'px';
		this.e_iframe.style.height = (n_height + 6) +'px';
	}
	return true;
}

function f_timeChange(value){
	if (this.f_checkTime(value))
		this.time = value;
}

function f_getPosition (e_elemRef, s_coord) {
	var n_pos = 0, n_offset,
		e_elem = e_elemRef;

	while (e_elem) {
		n_offset = e_elem["offset" + s_coord];
		n_pos += n_offset;
		e_elem = e_elem.offsetParent;
	}
	// margin correction in some browsers
	if (b_ieMac)
		n_pos += parseInt(document.body[s_coord.toLowerCase() + 'Margin']);
	else if (b_safari)
		n_pos -= n_offset;

	e_elem = e_elemRef;
	while (e_elem != document.body) {
		n_offset = e_elem["scroll" + s_coord];
		if (n_offset && e_elem.style.overflow == 'scroll')
			n_pos -= n_offset;
		e_elem = e_elem.parentNode;
	}
	return n_pos;
}

function f_tcalRelDate (d_date, d_diff, s_units) {
	var s_units = (s_units == 'y' ? 'FullYear' : 'Month');
	var d_result = new Date(d_date);
	d_result['set' + s_units](d_date['get' + s_units]() + d_diff);
	if (d_result.getDate() != d_date.getDate())
		d_result.setDate(0);
	return ' onclick="A_TCALS[\'' + this.s_id + '\'].f_update(' + d_result.valueOf() + ')"';
}

function f_setYear (d_date, s_year) {
	if (d_date && s_year){
		var d_result = new Date(d_date);

		d_result.setYear(s_year);

		this.f_update(d_result.valueOf());
	}
}

function f_tcalHideAll () {
	if (window.A_TCALSIDX){
		for (var i = 0; i < window.A_TCALSIDX.length; i++)
			window.A_TCALSIDX[i].f_hide();
	}
}

function updateAnotherField(){
	var targetField = this.a_cfg.update;
	var addition    = this.a_cfg.update_set_hours;
	var aField      = document.getElementsByName(targetField);

	if (aField.length == 1 && addition && this.time != this.time_prev){
		var targetField = aField[0];

		s_stamp = targetField.value;

		if (s_stamp.indexOf(' ') > 0){

			s_date = this.f_generDate(this.f_parseDate(this.e_input.value));

			// Tarkastetaan kellonaikaa
			var re_time = /^\s*(\d{1,2})\:(\d{1,2})\s*$/;
			if (!re_time.exec(this.time))
				return alert ("Invalid time: '" + this.time + "'.\nAccepted format is HH:MM.");

			// Jaetaan kellonaika osiin
			var n_hour = Number(RegExp.$1),
				n_minute = Number(RegExp.$2);

			// Tarkastetaan päiväys
			var re_date = /^\s*(\d{1,2})\.(\d{1,2})\.(\d{2,4})\s*$/;
			if (!re_date.exec(s_date))
				return alert ("Invalid date: '" + s_date + "'.\nAccepted format is dd.mm.yyyy.")

			var n_day = Number(RegExp.$1),
				n_month = Number(RegExp.$2),
				n_year = Number(RegExp.$3);

			n_hour = parseInt(n_hour) + parseInt(addition);

			// Jos ajan lisäys pyöräyttää kellon ympäri
			if (n_hour > 23){
				n_hour = n_hour - 24;
				s_date = new Date (n_year, n_month - 1, n_day + 1);
				s_date = this.f_generDate(s_date);
			}

			// Asetetaan uusi aika
			targetField.value = s_date+' '+n_hour+':'+((n_minute < 10 ? '0' : '')+n_minute);
		}
	}
}

function f_onClick(){
	if (this.a_cfg.onclick){
		eval(this.a_cfg.onclick);
	}
}
Date.prototype.getWeek = function() {
	var onejan = new Date(this.getFullYear(),0,1);
	return Math.ceil((((this - onejan) / 86400000) + onejan.getDay())/7);
}

f_getElement = document.all ?
	function (s_id) { return document.all[s_id] } :
	function (s_id) { return document.getElementById(s_id) };

if (document.addEventListener)
	window.addEventListener('scroll', f_tcalHideAll, false);
if (window.attachEvent)
	window.attachEvent('onscroll', f_tcalHideAll);

// global variables
var s_userAgent = navigator.userAgent.toLowerCase(),
	re_webkit = /WebKit\/(\d+)/i;
var b_mac = s_userAgent.indexOf('mac') != -1,
	b_ie5 = s_userAgent.indexOf('msie 5') != -1,
	b_ie6 = s_userAgent.indexOf('msie 6') != -1 && s_userAgent.indexOf('opera') == -1;
var b_ieFix = b_ie5 || b_ie6,
	b_ieMac  = b_mac && b_ie5,
	b_safari = b_mac && re_webkit.exec(s_userAgent) && Number(RegExp.$1) < 500;
