
/*
*	INPUT-ra naptart huzo class 2008-06-25
*	Berlik Levente, flashpistols kft. http://www.fps.hu
*
*	pl: <input type="text" class="calendar_mysql" name="test_date_3" value="<?php echo $_GET["test_date_3"];?>" />
*	a legvegen meg:
*	<script...>fps_calendar();</script>
*/

var calendar_zindex = 99;
function js_calendar(oid, lang) {

	Number.prototype.NaN0 = function(){ return isNaN(this) ? 0 : this; }

	this.use_input = 1;
	this.use_icon = 1;
	this.input_editable = 1;
	this.icon_url = "image/calendar.gif";
	this.required = 0;
	this.predict = 1;
	this.popup = 1;

	this.target_obj = "";
	this.timer = 0;						// onblur
	this.timer2 = 0;					// onerror
	this.onfocus_called = 0;			// ez csak onfocus esemenykor (alapbol) 1-be all, jelezve az onclick-nek, hogy mar megnyitottuk, az torli es nem csinal semmit

	this.event_onclick = function() {
		if (last_date == "") last_date = new Date();
		if (this.onfocus_called == 1) this.onfocus_called = 0; else this.showhide();
	}

	this.event_onblur = function() {
		this.predict_date();	// utoformazas kilepeskor
		this.check_format();
		this.timer = setTimeout(this.object_name+'.hide();', 250);	// ezt a .show metodus torli, amugy becsuk
	}

	this.event_icon_onblur = function() {
		this.check_format();
		this.timer = setTimeout(this.object_name+'.hide();', 250);
	}

	this.event_onfocus = function() {
		if (this.textual_element) this.textual_element.className = 'calendar_input_'+construct_id;
		if (last_date == "") last_date = new Date();
		this.onfocus_called = 1;
		this.show();
		this.predict_date();	// ez miert is mar?

		// ez a beszaras:
		// ez a predict_date vegen is ott vannak, annak kene ezt intezni, jelenleg ha nem kap esemenyt az nem intezi, amugy igen
		// ha az intezi, IE-k fagynak, ha iderakom (tok ugyan az a hatas), nem fagy...
		// amugy itt focus-nal kellene allitani, de a fagyas a blur-ben meghivottnal volt
		this.set_caret_to('calendar_input_'+construct_id, 11);
	}

	this.event_onkeypress = function(event) { }
	this.event_onkeydown = function(event) { }

	this.event_onkeyup = function(event) {
		this.predict_date(event);
	}

	this.event_ondateselect = function(day, month, year) {
		this.set_date(day, month, year);
	}

	// nyelv specifikus cuccok
	if (typeof lang == "undefined") lang = "hu_HU";
	this.sunday_first = 0;	// TODO: ezt megcsinalni
	var titles = new Array();

	switch (lang) {

		// angol
		case 'en_US':
			var tooltip = {
				next_month: "Next month",
				prev_month: "Previous month",
				next_year: "Next year",
				prev_year: "Previous year",
				icon: "Select date",
				today: "Back to today's date",
				close: "Close"
			}
			var daynames = new Array('M', 'Tu', 'W', 'Th', 'F', 'Sa', 'Su');
		break;

		// nemet
		case 'de_DE':
			var tooltip = {
				next_month: "Nächste Monate",
				prev_month: "Vorherige Monate",
				next_year: "Nächste Jahr",
				prev_year: "Vorherige Jahr",
				icon: "Wählen Date",
				today: "Zurück zu aktuelles Zeit",
				close: "Anschlüss or something like that"
			}
			var daynames = new Array('M', 'Di', 'M', 'Do', 'F', 'Sa', 'So');
		break;

		// magyar
		default:
			var tooltip = {
				next_month: "Következő hónap",
				prev_month: "Előző hónap",
				next_year: "Következő év",
				prev_year: "Előző év",
				icon: "Dátum választása",
				today: "Vissza a mai dátumra",
				close: "Bezárás"
			}
			var daynames = new Array('H', 'K', 'Sze', 'Cs', 'P', 'Szo', 'V');
		break;
	}

	// eof nyelv

	Date.prototype.getWeek = function() {
		var onejan = new Date(this.getFullYear(), 0, 1);
		return Math.ceil((((this - onejan) / 86400000) + onejan.getDay())/7);
	}

	var construct_id;		// ebben tarolodk this.print utan a megadott id/name
	var last_date = "";		// az utoljara kivalasztott datum
	var today_date = new Date();

	this.last_selected_date = 0;

	// ebbe construct elott peldanyositas utan kotelezo megadni a letrehozott nevet :-(
	this.object_name = oid;
	this.ch_count = 0;	// szamolja a lenyomott billenytyuket

	this.hidden_element = 0;
	this.textual_element = 0;
	this.month_select_element = 0;
	this.day_select_element = 0;
	this.browser_element = 0;

	this.date_return_mode = "timestamp";	// mysql: pl: 2007-10-26 | timestamp: unix_timestamp(sec)
	// lehet: 'mysql' formatum vagy 'timestamp'
	this.set_date_return_mode = function(mode) {
		if (typeof mode == "undefined") { alert("Set_date_return_mode: Missing parameter;"); return false; }

		if (mode == "mysql") {
			this.date_return_mode = "mysql";
		} else {
			this.date_return_mode = "timestamp";
		}
	}

	this.set_target_obj = function(obj) { this.target_obj = obj; }
	this.set_required = function(req) { this.required = req; }
	this.set_use_icon = function(icon) { this.use_icon = icon; }
	this.set_use_input = function(i) { this.use_input = i; }
	this.set_input_editable = function(i) { this.input_editable = i; }
	this.set_predict = function(i) { this.predict = i; }
	this.set_popup = function(i) { this.popup = i; }

	// kirakja az inputot alaphelyzetben
	this.construct = function(obj, value) {

		if (typeof obj == "undefined") return false;
		if (document.getElementById(obj)) {
			var id = obj;
			obj = document.getElementById(obj);
			this.target_obj = obj;
		} else {
			this.target_obj = obj;
			var id = this.target_obj.getAttributeNode("name");
			id = id.nodeValue;
			if (!id) return false;
		}
		construct_id = id;

		if (this.object_name == "" || eval('typeof '+this.object_name) == "undefined") { alert('Calendar: Missing objectname: '+this.object_name); return; }

		if (this.target_obj == "") return false;
		if (this.target_obj.value != "") value = this.target_obj.value;

		if (typeof value == "undefined" || value == '0000-00-00') {		// sql ures date...
			/*var d = new Date();
			var day = d.getDate();
			var month = d.getMonth() + 1;
			var year = d.getFullYear();
			value = year+'.'+this.number_format(month)+'.'+this.number_format(day)+'.';
			last_date = d;*/
			value = "";
		} else {
			last_date = new Date();
			var v = value;
			if (!v.match(/\-/) && !v.match(/\./)) {
				last_date.setTime(v*1000);
			} else {
				if (v.match(/\-/)) {
					var parts = v.split("-");
				} else if (v.match(/\./)) {
					var parts = v.split(".");
				} else return false;	// ha nem jo, nem jo
				last_date = new Date(parts[0], (parts[1]-1), parts[2]);
			}
			value = last_date.getFullYear()+'.'+this.number_format(last_date.getMonth()+1)+'.'+this.number_format(last_date.getDate())+'.';
			this.last_selected_date = new Date(last_date.getFullYear(), last_date.getMonth(), last_date.getDate());
		}

		// ide az esemenyek
		var calendar_events =
			'onclick="'+this.object_name+'.event_onclick();" '+
			'onblur="'+this.object_name+'.event_onblur();" '+
			'onfocus="'+this.object_name+'.event_onfocus();" '+
			'onkeypress="'+this.object_name+'.event_onkeypress(event);" '+
			'onkeydown="'+this.object_name+'.event_onkeydown(event);" '+
			'onkeyup="'+this.object_name+'.event_onkeyup(event);" ';

		var icon_events =
			'onclick="'+this.object_name+'.event_onclick();" '+
			'onblur="'+this.object_name+'.event_icon_onblur();"';

		/*
		var calendar_html =
				(this.use_input == 1 ?
					'<input class="calendar_input_'+(this.required == 1 && value == '' ? 'error_' : '')+id+'" id="calendar_input_'+id+'" type="text" value="'+value+'"  name="calendar_input_textual_'+id+'" '+calendar_events+' '+(this.input_editable == 1 ? '' : 'disabled="disabled"')+' maxlength="11" autocomplete="off" />' : '')+

				(this.use_icon == 1 ?
					'<a class="calendar_icon_'+id+' tooltip" href="javascript: void(0);" title="'+tooltip['icon']+'" '+icon_events+'>'+
						'<img src="'+this.icon_url+'" alt="" />'+
					'</a>' : '')+

				'<input type="hidden" name="'+id+'" id="'+id+'" value="'+this.return_date()+'" />'+

				(this.use_input == 1 ? '<br />' : '')+

				(this.popup == 1 ? '<div id="calendar_browser_'+id+'" class="calendar_browser" style="display: none;">'+
					'<div class="month_select" id="calendar_month_select_'+id+'"><!-- x --></div>'+
					'<div class="day_select" id="calendar_day_select_'+id+'"><!-- x --></div>'+
				'</div>' : '');
		*/

		var calendar_html =
				(this.use_input == 1 ?
					'<input class="calendar_input_'+(this.required == 1 && value == '' ? 'error_' : '')+id+'" id="calendar_input_'+id+'" type="text" value="'+value+'"  name="calendar_input_textual_'+id+'" '+calendar_events+' '+(this.input_editable == 1 ? '' : 'disabled="disabled"')+' maxlength="11" autocomplete="off" />' : '')+

				(this.use_icon == 1 ?
					'<a class="calendar_icon_'+id+' tooltip" href="javascript: void(0);" title="'+tooltip['icon']+'" '+icon_events+'>'+
						'<img src="'+this.icon_url+'" alt="" />'+
					'</a>' : '')+

				'<input type="hidden" name="'+id+'" id="'+id+'" value="'+this.return_date()+'" />'+

				(this.use_input == 1 ? '<br />' : '');

		if (this.popup == 1) {
			var cb = document.createElement('div');
			cb.setAttribute('id', 'calendar_browser_'+id);
			cb.className = 'calendar_browser';
			cb.style.display = 'none';
			cb.innerHTML = '<div class="month_select" id="calendar_month_select_'+id+'"><!-- x --></div>'+
					'<div class="day_select" id="calendar_day_select_'+id+'"><!-- x --></div>';
			document.body.appendChild(cb);
		}

		var span = document.createElement('span');
		span.style.zIndex = --calendar_zindex;
		span.innerHTML = calendar_html;
		obj.parentNode.replaceChild(span, obj);

		this.hidden_element = document.getElementById(id);
		if (this.use_input) this.textual_element = document.getElementById('calendar_input_'+id);
		if (this.popup) {
			this.month_select_element = document.getElementById('calendar_month_select_'+id);
			this.day_select_element = document.getElementById('calendar_day_select_'+id);
			this.browser_element = document.getElementById('calendar_browser_'+id);
		}
	}

	// csak vegignezi, hogy minden ok
	this.selftest = function() {
		var e = 0;
		if (typeof construct_id == "undefined") e = 'Calendar is not constucted.';
			else if (!this.textual_element && this.use_input) e = 'Missing input element.';
			else if (!this.browser_element && this.popup) e = 'Missing browser element.';
			else if (!this.month_select_element && this.popup) e = 'Missing month select element.';
			else if (!this.day_select_element && this.popup) e = 'Missing day select element.';
			else if (!this.hidden_element) e = 'Missing hidden timestamp elment.';
			else if (this.object_name == "" || eval('typeof '+this.object_name) == "undefined") e = 'Calendar: Missing objectname: '+this.object_name;
		if (e != 0) { alert('Calendar: Bad initialization\n'+e); return false; }
		return true;
	}

	// a calendar-t magat intezik
	this.showhide = function() {
		if (this.popup == 0) return false;
		if (this.toggleelement('calendar_browser_'+construct_id) == 2) this.show();
	}

	this.hide = function() {
		clearTimeout(this.timer2);
		if (this.popup == 0) return false;
		if (!this.selftest()) return false;
		this.hideelement('calendar_browser_'+construct_id);
	}

	this.show = function() {
		clearTimeout(this.timer);
		if (!this.selftest()) return false;
		if (this.popup == 1) {
			var pos = this.getPosition(this.textual_element);
			var teh = this.textual_element.offsetHeight;

			this.browser_element.style.left = pos.x+'px';
			this.browser_element.style.top = pos.y+teh+'px';

			this.showelement('calendar_browser_'+construct_id);
			this.print_month();
			this.print_days();
		}
	}

	// ev/honap lapozo
	this.print_month = function() {

		if (!this.selftest()) return false;
		var month_select = this.month_select_element;
		month_select.innerHTML = '';

		var year = last_date.getFullYear();
		var month = last_date.getMonth() + 1;

		var IH =
			'<a class="close" title="'+tooltip['close']+'" href="javascript: void(0);" onclick="'+this.object_name+'.hide();"></a>'+
			'<a class="pager" href="javascript: void(0);" title="'+tooltip['prev_year']+'" onclick="'+this.object_name+'.set_prev_year();" '+(year == 1970 ? 'style="visibility: hidden;"' : '')+'>&nbsp;&laquo;&nbsp;</a> '+
			year+
			' <a class="pager" href="javascript: void(0);" title="'+tooltip['next_year']+'" onclick="'+this.object_name+'.set_next_year();" '+(year == 2038 ? 'style="visibility: hidden;"' : '')+'>&nbsp;&raquo;&nbsp;</a>'+

			'&nbsp;&nbsp;'+
			'<a class="pager" href="javascript: void(0);" title="'+tooltip['today']+'" onclick="'+this.object_name+'.set_current_date();">|+|</a>'+
			'&nbsp;&nbsp;'+

			'<a class="pager" href="javascript: void(0);" title="'+tooltip['prev_month']+'" onclick="'+this.object_name+'.set_prev_month();">&nbsp;&laquo;&nbsp;</a> '+
			this.number_format(month)+
			' <a class="pager" href="javascript: void(0);" title="'+tooltip['next_month']+'" onclick="'+this.object_name+'.set_next_month();">&nbsp;&raquo;&nbsp;</a>';

		var placeholder = document.createElement('div');
		placeholder.innerHTML = IH;
		month_select.appendChild(placeholder);

	}

	// elozo honapra leptet
	this.set_prev_month = function() {
		if (!this.selftest()) return false;
		var year = last_date.getFullYear();
		var month = last_date.getMonth();

		if (month == 0) {
			month = 11;
			year--;
		} else {
			month--;
		}

		last_date.setMonth(month);
		last_date.setFullYear(year);
		this.show();

		return true;
	}

	// kovetkezo honapra leptet
	this.set_next_month = function() {
		if (!this.selftest()) return false;
		var year = last_date.getFullYear();
		var month = last_date.getMonth();

		if (month == 11) {
			month = 0;
			year++;
		} else {
			month++;
		}

		last_date.setMonth(month);
		last_date.setFullYear(year);
		this.show();

		return true;
	}

	// elozo evre leptet
	this.set_prev_year = function() {
		if (!this.selftest()) return false;
		var year = last_date.getFullYear();

		if (year == 1970) return false;		// mivel timestamp-ben adjuk vissza, az meg nem lehet korabbi
		year--;
		last_date.setFullYear(year);
		this.show();

		return true;
	}

	// kovetkezo evre leptet
	this.set_next_year = function() {
		if (!this.selftest()) return false;
		var year = last_date.getFullYear();

		if (year == 2038) return false;
		year++;
		last_date.setFullYear(year);
		this.show();

		return true;
	}

	// ez nyomja ki magat a naptar reszt
	this.print_days = function() {

		if (!this.selftest()) return false;
		var day_select = this.day_select_element;

		var year = last_date.getFullYear();

		var month = last_date.getMonth();	// 0 - 11 ig
		var days_in_month = this.get_days_in_month(month);
		var prev_dim = this.get_days_in_month(month-1);

		var day = last_date.getDate();
		var day_of_week = last_date.getDay();	// 0 -hetfo, 6 -vasarnap
		var first_dow = this.get_day_of_week(1);
		var last_dow = this.get_day_of_week(days_in_month);

		var first_week = new Date(year, month, 1).getWeek();
		//var first_week = date_tmp.getWeek();

		var i;
		var IH = "";
		var counter = 1;	// nem nulla! :-)
		var class_name = "";

		IH += '<div class="daynames">&nbsp;</div>';
		for (i = 0; i <= 6; i++) { IH += '<div class="daynames">'+daynames[i]+'</div>'; }
		IH += '<div class="calendar_clear"><!-- x --></div>';

		IH += '<div class="weeks">'+first_week+'</div>';
		first_week++;

		for (i = first_dow; i > 0; i--) {
			if (counter == 6) class_name = "inactive_saturday";
				else if (counter == 7) class_name = "inactive_sunday";
				else class_name = "inactive";

			IH += '<a href="javascript: void(0);" onclick="'+this.object_name+'.event_ondateselect('+(prev_dim - i + 1)+', '+(month-1)+');" class="'+class_name+'">'+(prev_dim - i + 1)+'</a>';
			counter++;
		}

		var title_tmp = "";
		for (i = 1; i <= days_in_month; i++) {
			if (this.last_selected_date != 0 && i == this.last_selected_date.getDate() && month == this.last_selected_date.getMonth() && year == this.last_selected_date.getFullYear()) class_name = "selected";
				else if (i == today_date.getDate() && year == today_date.getFullYear() && month == today_date.getMonth()) class_name = "today";
				else if (counter == 7) class_name = "active_sunday";
				else if (counter == 6) class_name = "active_saturday";
				else class_name = "active";

			title_tmp = this.get_date_title(i);

			IH += '<a href="javascript: void(0);" onclick="'+this.object_name+'.event_ondateselect('+i+', '+month+');" class="'+class_name+'" '+(title_tmp != "" ? 'title="'+title_tmp+'"' : '')+' '+(title_tmp != "" ? 'style="height: 19px; border-bottom: 2px solid #faa;"' : '')+'>'+i+'</a>';

			if (counter == 7) {
				IH += '<div class="calendar_clear"><!-- x --></div>';
				counter = 0;
				if (i != days_in_month) {	// csak akkor irjunk kovetkezo hetet, ha az utolso nap nem vasarnap
					IH += '<div class="weeks">'+first_week+'</div>';
					first_week++;
				}
			}

			counter++;
		}

		if (counter > 1) {	// ha nem vasarnappal ert veget a het
			var counter_tmp = counter;
			for (i = 1; i <= 8-counter_tmp; i++) {
				if (counter == 6) class_name = "inactive_saturday";
					else if (counter == 7) class_name = "inactive_sunday";
					else class_name = "inactive";

				IH += '<a href="javascript: void(0);" onclick="'+this.object_name+'.event_ondateselect('+i+', '+(month+1)+');" class="'+class_name+'">'+i+'</a>';
				counter++;
			}
		}

		IH += '<div class="calendar_clear"><!-- x --></div>'+
			'<div class="last_selected_date">'+
				last_date.toLocaleDateString()+
				'<a class="close" title="'+tooltip['close']+'"></a>'+
			'</div>';

		day_select.innerHTML = '';
		var placeholder = document.createElement('div');
		placeholder.innerHTML = IH;
		day_select.appendChild(placeholder);
	}

	// a this.titles-ben megadott datumtitleket teriti vissza
	this.get_date_title = function(active_day) {
		var title_tmp = "";
		if (typeof this.titles == "undefined") return "";

		var year = last_date.getFullYear();
		var month = last_date.getMonth();

		// konkret datum
		var title_date = this.return_date('mysql', new Date(year, month, active_day));
		if (typeof this.titles[title_date] != "undefined") return this.titles[title_date];

		// minden ev
		var title_date = '*-'+this.number_format(month+1)+'-'+this.number_format(active_day);
		if (typeof this.titles[title_date] != "undefined") return this.titles[title_date];

		// minden honap
		var title_date = year+'-*-'+this.number_format(active_day);
		if (typeof this.titles[title_date] != "undefined") return this.titles[title_date];

		// nap
		var title_date = year+'-'+this.number_format(month+1)+'-*';
		if (typeof this.titles[title_date] != "undefined") return this.titles[title_date];

		return "";
	}

	// beirja a kijelolt datumot, csak a nap megy be, a tobbi a last_date-ben
	this.set_date = function(day, month, year) {
		if (!this.selftest()) return false;

		if (typeof month == "undefined") month = last_date.getMonth();
		if (typeof year == "undefined") year = last_date.getFullYear();
		var input_field = this.textual_element;
		var hidden_field = this.hidden_element;

		last_date = new Date(year, month, day);
		var ts = this.return_date();

		hidden_field.value = ts;
		input_field.value = last_date.getFullYear()+'.'+this.number_format(last_date.getMonth()+1)+'.'+this.number_format(last_date.getDate())+'.';
		this.hide();

		this.last_selected_date = new Date(year, month, day);
	}

	// visszadja a date_return_mode szerint a jelenlegi datumstringet
	this.return_date = function(mode, date) {
		if (last_date == "") return "";
		if (typeof mode == "undefined") mode = this.date_return_mode;
		if (typeof date == "undefined") date = last_date;

		if (mode == "timestamp") {
			var ts = Math.floor(date.getTime()/1000);
		} else {
			var ts = date.getFullYear()+'-'+this.number_format(date.getMonth()+1)+'-'+this.number_format(date.getDate());
		}
		return ts;
	}

	// visszaadja a last_date -et
    this.get_last_date = function() {
	 	return last_date;
	}

	// beallitja a mai datumot
	this.set_current_date = function() {
		//if (last_date == today_date) return false;
		if (!this.selftest()) return false;
		last_date = new Date();
		this.show();
		return true;
	}

	// megmondja hany nap van adott honapban azert van elol a month, hogy el tudjuk hagyni az evet
	this.get_days_in_month = function(month, year) {
		if (typeof year == "undefined") year = last_date.getFullYear();
		return 32 - new Date(year, month, 32).getDate();
	}

	// 0 - vasarnap , 1 -hetfo, 7 - szombat
	this.get_day_of_week = function(day, month, year) {
		if (typeof year == "undefined") year = last_date.getFullYear();
		if (typeof month == "undefined") month = last_date.getMonth();
		var dow = new Date(year, month, day).getDay();
		if (dow == 0) dow = 6; else dow--;
		return dow;
	}

	// gepeleskor megprobalja formazni a datumot
	this.predict_date = function(event) {

		if (!this.textual_element) return false;
		if (!this.selftest()) return false;
		var i = this.textual_element;

		if (typeof event != "undefined") var ch = event.keyCode; else ch = 0;

		if (ch == 40) {
			this.showelement('calendar_browser_'+construct_id);
		}

		if (ch == 38 || ch == 27 || ch == 9) { this.hideelement('calendar_browser_'+construct_id); }	//

		if (this.predict == 1) {
			// kurzor pos
			var caret_pos = this.get_selection_start('calendar_input_'+construct_id);

			// ha nem backspace, jobbra/balra - uj gepeles kozben javit
			if (ch != 8 && ch != 37 && ch != 39 && ch != 46) {
				// ev
				if (i.value.match(/^[0-9]{1}$/) && ch != 50 && ch != 49) {	// 2
					if (ch >= 48 && ch <= 57) {	// 0, !1, !2, 3,4,5..9
						i.value = '20'+i.value;
						caret_pos = 11;
					}
				} else if (i.value.match(/^[0-9]{1}[^0-9]$/)) {
					i.value = '200'+i.value.replace(/[^0-9]/, '.');
					caret_pos = 11;
				} else if (i.value.match(/^[0-9]{2}[^0-9]$/)) {
					i.value = '20'+i.value.replace(/[^0-9]/, '.');
					caret_pos = 11;
				} else if (i.value.match(/^[0-9]{4}$/)) {
					i.value += '.';
					caret_pos = 11;
				}

				// honap
				if (i.value.match(/^[0-9]{4}\.[0-9]{1}$/)) {
					if (ch >= 50 && ch <= 57) {	// 2-9
						// ha 2-es vagy folotte
						i.value = i.value.replace(/^([0-9]{4})\.([0-9]{1})$/, "$1.0$2.");
						caret_pos = 11;
					}
				} else if (i.value.match(/^[0-9]{4}\.[0-9]{1}[^0-9]$/) && ch != 48) {
					i.value = i.value.replace(/^([0-9]{4})\.([0-9]{1})[^0-9]/, "$1.0$2.");
					caret_pos = 11;
				} else if (i.value.match(/^[0-9]{4}\.[0-9]{2}$/)) {
					if (ch >= 48 && ch <= 50) {
						i.value += '.';
						caret_pos = 11;
					} else {
						i.value.replace(/^([0-9]{4}).+/, "$1.");
						caret_pos = 11;
					}
				}

				// ha beirta a napot
				if (i.value.match(/^[0-9]{4}\.[0-9]{2}\.[1-9]{1}[^0-9]$/)) {
					i.value = i.value.replace(/^([0-9]{4})\.([0-9]{2})\.([0-9]{1})[^0-9]$/, "$1.$2.0$3.");
				} else if (i.value.match(/^[0-9]{4}\.[0-9]{2}\.[0-9]{2}$/)) {
					i.value += '.';
				}

			} /// NOTE: caret_pos = 11;	a vegere

			// clean: mindent ami nem szam es pont, es ha tobb pont van egymas utan
			i.value = i.value.replace(/[^0-9\.]/g, ".");
			if (ch != 8 && ch != 46 && ch != 190) i.value = i.value.replace(/\.{2,11}/g, ".");

			// honap, nap utojavitas, ha nem bkpspc/delete/szam/pont
			var ch_limit = 4;
			if (ch == 37 || ch == 39) this.ch_count++;
			if (this.ch_count > ch_limit) this.ch_count = ch_limit;

			if (ch != 8 && ch != 46 && ch != 190 && (ch < 48 || ch > 57) && this.ch_count == ch_limit) {
				i.value = i.value.replace(/\.([1-9]{1})\./g, ".0$1.");
				i.value = i.value.replace(/^([0-9]{1}[^0-9])/, "200$1");
				i.value = i.value.replace(/^([0-9]{2}[^0-9])/, "20$1");
				i.value = i.value.replace(/^([0-9]{3}[^0-9])/, "2$1");
				this.ch_count = 0;
			}
		} // eof predict

		// naptar utan allitgatas
		if (i.value.match(/^([0-9]{4}).*/)
			&& this.browser_element.style.display == 'block'
			&& typeof event != "undefined"
			//&& ch < 33 && ch > 46
		) {
			var year = i.value.replace(/^([0-9]{4}).*/, "$1");

			if (i.value.match(/^[0-9]{4}\.[0-9]{2}.*/)) {
				var month = i.value.replace(/^[0-9]{4}\.([0-9]{2}).*/, "$1") - 1;
			} else {
				var month = last_date.getMonth();
			}

			if (i.value.match(/^[0-9]{4}\.[0-9]{1,2}\.[0-9]{1,2}[^0-9]*$/)) {
				var day = i.value.replace(/^[0-9]{4}\.[0-9]{1,2}\.([0-9]{1,2})[^0-9]*$/, "$1");
			} else {
				var day = last_date.getDate();
			}

			if (year >= 1970 && year <= 2038 && month >=0 && month <= 11 && day >= 1 && day <= 31) {
				var hidden_field = this.hidden_element;
				last_date = new Date(year, month, day);
				var ts = this.return_date();
				hidden_field.value = ts;

				this.print_month();
				this.print_days();
			}

		}

		// kurzor allitas, EZ A BESZARAS, LASD ONFOCUS ESEMENY, FENT
		if (ch != 0 && this.predict == 1) this.set_caret_to('calendar_input_'+construct_id, caret_pos);
	}

	// ellenoriz + beir blur eseten
	this.check_format = function() {
		if (!this.selftest()) return false;
		var i = this.textual_element;

		if (i.value.match(/^[0-9]{4}\.[0-9]{1,2}\.[0-9]{1}$/)) i.value = i.value.replace(/([0-9]{1})$/, "0$1");
		if (i.value.match(/^[0-9]{4}\.[0-9]{1,2}\.[0-9]{2}$/)) i.value += '.';

		if (i.value == "" && this.required == 0) {
			var hidden = this.hidden_element;
			hidden.value = "";
			return true;
		}

	 	if (!i.value.match(/^[0-9]{4}\.[0-9]{1,2}\.[0-9]{1,2}\.$/)) {
			this.timer2 = setTimeout('document.getElementById("calendar_input_'+construct_id+'").className = "calendar_input_error_'+construct_id+'";', 250);

			if (this.required == 0) {
				setTimeout('document.getElementById("calendar_input_'+construct_id+'").className = "calendar_input_'+construct_id+'";', 5000);
			}
			return false;
		} else {
			var year = i.value.replace(/^([0-9]{4})\.[0-9]{1,2}\.[0-9]{1,2}\.$/, "$1");
			var month = i.value.replace(/^[0-9]{4}\.([0-9]{1,2})\.[0-9]{1,2}\.$/, "$1");
			var day = i.value.replace(/^[0-9]{4}\.[0-9]{1,2}\.([0-9]{1,2})\.$/, "$1");

			if (month > 1 && month < 12) var days_in_month = this.get_days_in_month(month-1);

			if (year < 1970 || year > 2038 || month < 1 || month > 12 || day < 1 || day > days_in_month) {
				this.timer2 = setTimeout('document.getElementById("calendar_input_'+construct_id+'").className = "calendar_input_error_'+construct_id+'";', 50);

				if (this.required == 0) {
					setTimeout('document.getElementById("calendar_input_'+construct_id+'").className = "calendar_input_'+construct_id+'";', 5000);
				}

				return false;
			}
		}
		return true;
	}

	// mindenfelek...
	this.number_format = function(n) {	// de jobb nev mint a pina!
		if (n < 10) return '0'+n;
		return n;
	}

	this.showelement = function(id) {
		if (!document.getElementById(id)) return false;
		document.getElementById(id).style.display = 'block';
		return true;
	}

	this.hideelement = function(id) {
		if (!document.getElementById(id)) return false;
		document.getElementById(id).style.display = 'none';
		return true;
	}

	this.toggleelement = function(id) {
		if (!document.getElementById(id)) return false;
		var elem = document.getElementById(id);
		if (elem.style.display == 'block') {
			if (this.hideelement(id)) return 1;
		} else {
			if (this.showelement(id)) return 2;
		}
		return false;
	}

	this.focuselement = function (id) {
		if (!document.getElementById(id)) return false;
		var elem = document.getElementById(id);
		elem.focus();
	}

	this.get_selection_start = function(id) {
		if (!document.getElementById(id)) return false;
		id = document.getElementById(id);
		if (id.createTextRange) {
			var r = document.selection.createRange().duplicate()
			r.moveEnd('character', id.value.length)
			if (r.text == '') return id.value.length
			return id.value.lastIndexOf(r.text)
		} else return id.selectionStart
	}

	this.get_selection_end = function(id) {
		if (!document.getElementById(id)) return false;
		id = document.getElementById(id);
		if (id.createTextRange) {
			var r = document.selection.createRange().duplicate()
			r.moveStart('character', -id.value.length)
			return r.text.length
		} else return id.selectionEnd
	}

	this.set_caret_to = function(obj, pos) {

		// ha van kijelolesunk ne basszuk el
		if (this.get_selection_start(obj) != this.get_selection_end(obj)) return false;

		if (!document.getElementById(obj)) return false;
		obj = document.getElementById(obj);
		if (obj.setSelectionRange) {	// opera miatt ez az if ag kell elore
			obj.setSelectionRange(pos, pos);
		} else if(obj.createTextRange) {
			var range = obj.createTextRange();
			range.move("character", pos);
			range.select();	// FIXME: ITT A KAKI!!! ld. onfocus fent
		}
		obj.focus();
	}


	this.getPosition = function(obj){
		var posX = obj.offsetLeft;
		var posY = obj.offsetTop;
		while (obj.offsetParent) {
			posX = posX + obj.offsetParent.offsetLeft;
			posY = posY + obj.offsetParent.offsetTop;
			if (obj == document.body) break; else obj = obj.offsetParent;
		}
		return {x:posX, y:posY};
	}

}

var fps_calendars = new Array();
var fps_calendar_count = 0;
function fps_calendar() {

	if (typeof site_lang == "undefined") var site_lang = "hu_HU";
	var inputs = document.getElementsByTagName("input");

  	for (var i = 0; i < inputs.length; i++) {
		var obj = inputs[i];
		var myclass = obj.getAttributeNode("class");

		if (myclass) {
			var splitted = myclass.nodeValue.split(" ");
			for (var j = 0; j < splitted.length; j++) {
				var c = splitted[j];
				var b = c.split("_");

				if (b[0] == "calendar" && b[1] != "input") {

					//alap, TODO: cleanup, randa
					var required = 0;
					var mode = "timestamp";
					var use_icon = 1;
					var use_input = 1;
					var input_editable = 1;
					var predict = 1;
					var popup = 1;

					for (var k = 1; k < b.length; k++) {
						if (b[k] == "required") required = 1;
						if (b[k] == "mysql") mode = "mysql";
						if (b[k] == "noicon") use_icon = 0;
						if (b[k] == "noinput") use_input = 0;
						if (b[k] == "noedit") input_editable = 0;
						if (b[k] == "nopredict") predict = 0;
						if (b[k] == "nopopup") popup = 0;
					}

					fps_calendars[fps_calendar_count] = new js_calendar('fps_calendars['+fps_calendar_count+']', site_lang);
					fps_calendars[fps_calendar_count].set_date_return_mode(mode);
					fps_calendars[fps_calendar_count].set_required(required);
					fps_calendars[fps_calendar_count].set_use_icon(use_icon);
					fps_calendars[fps_calendar_count].set_use_input(use_input);
					fps_calendars[fps_calendar_count].set_input_editable(input_editable);
					fps_calendars[fps_calendar_count].set_predict(predict);
					fps_calendars[fps_calendar_count].set_popup(popup);
					fps_calendars[fps_calendar_count].construct(obj);
					fps_calendar_count++;
					break;
				}
			}
		} else continue;
	}
	fps_calendar_count = 0;
}
