var TTKAL = {
	hin: true,
	formname: 'bengine',
	terminMin: '',
	terminMax: '',
	selTermin: null,
	showMonth: null,
	init: function (formName, getTerminMin, getTerminMax) {
		this.terminMin = getTerminMin;
		this.terminMax = getTerminMax;
		this.formname = (formName === '') ? this.formname : formName;
		var bform = document.forms[this.formname],
			str = '';
		try {
			if (this.hin) {
				str = bform.fday.value + "." + bform.fmonth.value;
			} else {
				str = bform.tday.value + "." + bform.tmonth.value;
			}
			this.selTermin = new Date(str.split('.')[2], parseInt(str.split('.')[1], 10) - 1,parseInt(str.split('.')[0], 10), 12, 0, 0);
		} catch(e) {
			this.selTermin = new Date();
			if (this.hin) {
				this.selTermin.setTime(bform.termin.value * 1000);
			} else {
				this.selTermin.setTime(bform.ruecktermin.value * 1000);
			}
		}
		this.showMonth = new Date(this.selTermin.getTime());
		this.showMonth.setDate(1);
		this.showMonth.setHours(0,0,0,0);
	},
	updateSelTermin:function(hin_bool){
		var bform = document.forms[this.formname],
			str = '';
		 try {
			if (hin_bool) {
				str = bform.fday.value + "." + bform.fmonth.value;
			} else {
				str = bform.tday.value + "." + bform.tmonth.value;
			}
			this.selTermin = new Date(str.split('.')[2], parseInt(str.split('.')[1], 10) - 1, parseInt(str.split('.')[0], 10), 12, 0, 0);
		} catch(e) {
			this.selTermin = new Date();
			if (hin_bool) {
				this.selTermin.setTime(bform.termin.value * 1000);
			} else {
				this.selTermin.setTime(bform.ruecktermin.value * 1000);
			}
		}
		this.showMonth = new Date(this.selTermin.getTime());
		this.showMonth.setDate(1);
		this.showMonth.setHours(0,0,0,0);
		this.showKalenderControl();
	},
	showKalenderControl: function () {
		var xhtml = [],
			divTable = document.getElementById('kalTable'),
			divTitel = document.getElementById('kalTitel'),
			strTitel = monthNames[this.showMonth.getMonth()] + " " + this.showMonth.getFullYear(),
			today = new Date(),
			startDate = new Date(this.showMonth.getTime()),
			diff = startDate.getDay(),
			i = 0, j = 0,
			aktCss = '', strLink = '';
		divTitel.innerHTML = strTitel;
		today.setHours(12,0,0,0); 
		if (diff) {
			startDate.addDay(1 - diff);
		} else {
			startDate.addDay(-6);
		}
		startDate.setHours(12);
		xhtml.push('<table cellspacing="0" cellpadding="0">');
		xhtml.push('<tbody>');
		for (i = 0; i < 6; i += 1) {
			xhtml.push('<tr>');
			for (j = 0; j < 7; j += 1) {
				if (
					startDate.getTime() < (this.terminMin * 1000) ||
					startDate.getTime() > (this.terminMax * 1000)
				) {
					if (startDate.getDay() == 0) {
						aktCss = 'klnl niedziela';
					} else if (startDate.getDay() == 6) {
						aktCss = 'klnl sobota';
					} else {
						aktCss = 'klnl';
					}
				} else if (
					startDate.getMonth() != this.showMonth.getMonth() ||
					startDate.getFullYear() != this.showMonth.getFullYear()
				) {
					if (startDate.getDay() == 0) {
						aktCss = 'kalam niedziela';
					} else if (startDate.getDay() == 6) {
						aktCss = 'kalam sobota';
					} else {
						aktCss = 'kalam';
					}
				} else if (startDate.getDay() == 0) {
				  aktCss = 'kalson2 niedziela';
				} else {
					if (startDate.getDay() == 0) {
						aktCss = 'kalwt niedziela';
					} else if (startDate.getDay() == 6) {
						aktCss = 'kalwt sobota';
					} else {
						aktCss = 'kalwt';
					}
				}
				if (startDate.getTime() == this.selTermin.getTime()) {
					aktCss += '" id="today';
				}
				if (aktCss!='klnl') {
					var val = twoDigit(startDate.getDate()) + "." + twoDigit(startDate.getMonth() + 1) + "." + startDate.getFullYear();
					strLink = ' onclick="TTKAL.selectDate(\'' + val + '\');" style="cursor:pointer;"';
				} else {
					strLink = '';
				}
				xhtml.push('<td class="' + aktCss + '"' + strLink + '>' + startDate.getDate() + '</td>' + "\r\n");
				startDate.addDay();
			}
			xhtml.push('</tr>');
		}
		xhtml.push('</tbody>');
		xhtml.push('</table>');
		divTable.innerHTML = xhtml.join('');
	},
	showNextMonth: function() {
		var tmpMonth = new Date(this.showMonth);
		tmpMonth.addMonth();
		if (tmpMonth.getTime() < (this.terminMax * 1000)) {
		  this.showMonth = tmpMonth;
		  this.showKalenderControl();
		} else {
		  alert("Późniejszy termin nie jest niestety możliwy.");
		}
	},
	showPrevMonth: function () {
		var tmpMonth = new Date(this.showMonth);
		tmpMonth.setDate(0);tmpMonth.setDate(1);
		var firstMonth = new Date(this.terminMin * 1000);
		firstMonth.setDate(1);firstMonth.setHours(0,0,0,0);
		if (tmpMonth.getTime() >= firstMonth.getTime()) {
		  this.showMonth = tmpMonth;
		  this.showKalenderControl();
		} else {
		  alert("Wcześniejszy termin nie jest niestety możliwy.");
		}
	},
	selectDate: function(strDate) {
		var strDateSplit = strDate.split('.'),
			i = 0, len = 0,
			bform = document.forms[this.formname],
			timestamp = (Date.UTC(strDateSplit[2], (parseInt(strDateSplit[1], 10) - 1), strDateSplit[0]) / 1000);
		try {
			if (this.hin) {
				bform.termin.value = timestamp;
				len = bform.fday.length;
				for (i = 0; i < len; i += 1) {
					if (parseInt(bform.fday.options[i].value, 10) === parseInt(strDateSplit[0],10)) {
						bform.fday.options[i].selected = true;
					}
				}
				len = bform.fmonth.length;
				for (i = 0; i < len; i += 1) {
					if (bform.fmonth.options[i].value == parseInt(strDateSplit[1],10) + '.' + strDateSplit[2]) {
						bform.fmonth.options[i].selected = true;
						changeDaySelect(bform.fmonth.value, 'fday');
						if (i + 3 < bform.tmonth.length) {
							bform.tmonth.options[i + 3].selected = true;
							changeDaySelect(bform.tmonth.value, 'tday');
						} else if (i < bform.tmonth.length) {
							bform.tmonth.options[bform.tmonth.length - 1].selected = true;
							changeDaySelect(bform.tmonth.value, 'tday');
						}
					}
				}			
			} else {
				bform.ruecktermin.value = timestamp;
				len = bform.tday.length;
				for (i = 0; i < len; i += 1) {
					if (parseInt(bform.tday.options[i].value, 10) === parseInt(strDateSplit[0],10)) {
						bform.tday.options[i].selected = true;
					}
				}
				
				len = bform.tmonth.length;
				for (i = 0; i < len; i += 1) {
					if (bform.tmonth.options[i].value == parseInt(strDateSplit[1],10) + '.' + strDateSplit[2]) {				
						bform.tmonth.options[i].selected = true;		
						changeDaySelect(bform.tmonth.value, 'tday');
						if (i - 3 > 0) {
							bform.fmonth.options[i - 3].selected = true;
							changeDaySelect(bform.fmonth.value, 'fday');
						} else if (i > 0) {
							bform.fmonth.options[0].selected = true;
							changeDaySelect(bform.fmonth.value, 'fday');
						}
					}
				}
			}
			document.getElementById('popup_kal').style.display= "none";
		} catch(e) {}
	}
};
