		// adding PHP like date format function. Added as Date.prototype 
	Date.prototype.format = function(format)
	{
		var returnStr = '';
		var replace = Date.replaceChars;

		for (var i = 0; i < format.length; i++)
		{
			var curChar = format.charAt(i);

			if (replace[curChar])
				returnStr += replace[curChar].call(this);
			else	returnStr += curChar;
		}

		return returnStr;
	}

	Date.replaceChars =
	{
		shortMonths: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
		longMonths: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
		shortDays: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
		longDays: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
	
		// Day
		d: function() { return (this.getDate() < 10 ? '0' : '') + this.getDate(); },
		D: function() { return Date.replace.shortDays[this.getDay()]; },
		j: function() { return this.getDate(); },
		l: function() { return Date.replace.longDays[this.getDay()]; },
		N: function() { return this.getDay() + 1; },
		S: function() { return (this.getDate() % 10 == 1 && this.getDate() != 11 ? 'st' : (this.getDate() % 10 == 2 && this.getDate() != 12 ? 'nd' : (this.getDate() % 10 == 13 && this.getDate() != 1 ? 'rd' : 'th'))); },
		w: function() { return this.getDay(); },
		z: function() { return "Not Yet Supported"; },
	
		// Week
		W: function() { return "Not Yet Supported"; },
	
		// Month
		F: function() { return Date.replace.longMonths[this.getMonth()]; },
		m: function() { return (this.getMonth() < 11 ? '0' : '') + (this.getMonth() + 1); },
		M: function() { return Date.replace.shortMonths[this.getMonth()]; },
		n: function() { return this.getMonth() + 1; },
		t: function() { return "Not Yet Supported"; },
	 
		// Year
		L: function() { return "Not Yet Supported"; },
		o: function() { return "Not Supported"; },
		Y: function() { return this.getFullYear(); },
		y: function() { return ('' + this.getFullYear()).substr(2); },
	 
		// Time
		a: function() { return this.getHours() < 12 ? 'am' : 'pm'; },
		A: function() { return this.getHours() < 12 ? 'AM' : 'PM'; },
		B: function() { return "Not Yet Supported"; },
		g: function() { return this.getHours() == 0 ? 12 : (this.getHours() > 12 ? this.getHours() - 12 : this.getHours()); },
		G: function() { return this.getHours(); },
		h: function() { return (this.getHours() < 10 || (12 < this.getHours() < 22) ? '0' : '') + (this.getHours() < 10 ? this.getHours() + 1 : this.getHours() - 12); },
		H: function() { return (this.getHours() < 10 ? '0' : '') + this.getHours(); },
		i: function() { return (this.getMinutes() < 10 ? '0' : '') + this.getMinutes(); },
		s: function() { return (this.getSeconds() < 10 ? '0' : '') + this.getSeconds(); },
	
		// Timezone
		e: function() { return "Not Yet Supported"; },
		I: function() { return "Not Supported"; },
		O: function() { return (this.getTimezoneOffset() < 0 ? '-' : '+') + (this.getTimezoneOffset() / 60 < 10 ? '0' : '') + (this.getTimezoneOffset() / 60) + '00'; },
		T: function() { return "Not Yet Supported"; },
		Z: function() { return this.getTimezoneOffset() * 60; },
	
		// Full Date/Time
		c: function() { return "Not Yet Supported"; },
		r: function() { return this.toString(); },
		U: function() { return this.getTime() / 1000; }
	}

function c_calendar(object_name)
{
	this.day = 0;
	this.month = 0;
	this.year = 0;
	this.hour = 0;
	this.minute = 0;
	this.second = 0;
	this.timestamp = 0;

	this.script_name = "calendar_show.php";
	this.ajax_target = "calendar_dialog_inner";
	this.calendar_show_time = "minute";	// 	values: all,none,hour,minute

	this.object_name = object_name;
	this.max_timestamp = 0;
	this.min_timestamp = 0;
	this.initial_timestamp = 0;

	this.display_time_format = "j.n.Y";
	this.weekends_selectable = true;
	this.close_method = "button";	// "button", "auto" or "both"
	this.done_function = null;

	this.initialize = initialize;
	this.setMinimumTimestamp = setMinimumTimestamp;
	this.setMaximumTimestamp = setMaximumTimestamp;
	this.setInitialTimestamp = setInitialTimestamp;
	this.setDateTimeFromTimestamp = setDateTimeFromTimestamp;
	this.setDateTime = setDateTime;
	this.setWeekendsAvailability = setWeekendsAvailability;
	this.setDoneFunction = setDoneFunction;
	this.setDisplayTimeFormat = setDisplayTimeFormat;
	this.getFormattedReturnData = getFormattedReturnData;
	this.getFormattedDate = getFormattedDate;
	this.setCloseMethod = setCloseMethod;
	this.getUnixTimestamp = getUnixTimestamp;
	this.getHumanTime = getHumanTime;
	this.show = show;
	this.getIframeURL2 = getIframeURL2;
	this.calendarLoaded = calendarLoaded;
	this.setCalendarShowTime = setCalendarShowTime;
	this.setAjaxTarget = setAjaxTarget;
	this.setPHPScript = setPHPScript;
	this.createPopupDialog = createPopupDialog;
	this.hide = hide;
	this.clearCalendar = clearCalendar;
	this.convertDateToTimestamp = convertDateToTimestamp;

	function initialize()
	{
		this.script_name = "calendar_show.php";

		this.max_timestamp = 0;
		this.min_timestamp = 0;
		this.initial_timestamp = 0;

		this.display_time_format = "h:m:s";
		this.close_method = "button";	// "button", "auto" or "both"
		this.done_function = null;
	}

	function setMinimumTimestamp(timestamp)
	{
		this.min_timestamp = timestamp;
	}

	function setMaximumTimestamp(timestamp)
	{
		this.max_timestamp = timestamp;
	}

	function setInitialTimestamp(timestamp)
	{
		this.initial_timestamp = timestamp;
	}

	function setWeekendsAvailability(enabled)
	{
		this.weekends_selectable = enabled; 	// true or false
	}

	function setDoneFunction(done_function)
	{
		this.done_function = done_function;
	}

	function setDisplayTimeFormat(display_time_format)
	{
		this.display_time_format = display_time_format;
	}

	function setCloseMethod(close_method)
	{
		this.close_method = close_method;
	}

	function setAjaxTarget(ajax_target)
	{
		this.ajax_target = ajax_target;
	}

	function setPHPScript(script_name)
	{
		this.script_name = script_name;
	}

	function getFormattedReturnData(day,month,year,hour,minute,second)
	{
		//this.display_time_format;
//alert(year + " " + month + " " + day + " " + hour + " " + minute + " " + second)
		var myDate = new Date(year-1,month-1,day,hour,minute,second);

		return myDate.format('j.n.Y');
	}

	function getUnixTimestamp(day,month,year,hour,minute,second)
	{
		if ( arguments.length == 0)
		{
			day = this.day;
			month = this.month;
			year = this.year;
			hour = this.hour;
			minute = this.minute;
			second = this.second;
		}

		hour = hour == undefined ? 0 : hour;
		minute = minute == undefined ? 0 : minute;
		second = second == undefined ? 0 : second;

		var tmp = new Date(Date.UTC(year, (month-1), day, hour, minute, second));

		if ( arguments.length == 0 && tmp.getTime() > 0)
			this.timestamp = tmp.getTime()/1000;

		if ( tmp.getTime() > 0)
			return tmp.getTime()/1000;
		else	return 0;
	}

	function getHumanTime(timestamp)
	{
		var tmp_date = new Date(timestamp*1000);
		var date_string = tmp_date.toGMTString();
		var arr_date_string = date_string.split(" ");
		var arr_return = new Array();

		var arrMon = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
		var tmp_month = arr_date_string[2];

		for (var i=0;i<arrMon.length;i++)
		{
			if ( arr_date_string[2] == arrMon[i])
			{
				arr_return["month"] = i + 1;
				break;
			}
		}

		arr_return["day"] = arr_date_string[1];
		arr_return["year"] = arr_date_string[3];
		arr_return["hour"] = arr_date_string[4].substr(0,2);
		arr_return["minute"] = arr_date_string[4].substr(3,2);
		arr_return["second"] = arr_date_string[4].substr(6,2);

		return	arr_return;
	}

	function setDateTimeFromTimestamp(timestamp)
	{
		var date = getHumanTime(timestamp);

		this.timestamp = timestamp;

		this.day = date["day"];
		this.month = date["month"];
		this.year = date["year"];
		this.hour = date["hour"];
		this.minute = date["minute"];
		this.second = date["second"];
	}

	function clearCalendar()
	{
		this.day = 0;
		this.month = 0;
		this.year = 0;
		this.hour = 0;
		this.minute = 0;
		this.second = 0;
		this.timestamp = 0;
	}

		/* note - it's under construction. Now it ignores the date_format */
	function convertDateToTimestamp(date_to_convert, date_format)
	{
		var day, month, year;

		if ( date_to_convert == "")
			return "";

		day = date_to_convert.replace(/(\d+).\d+.\d+/,"$1");
		month = date_to_convert.replace(/\d+.(\d+).\d+/,"$1");
		year = date_to_convert.replace(/\d+.\d+.(\d+)/,"$1");

		return this.getUnixTimestamp(day,month,year);
	}

	function getIframeURL2()
	{
		var arr_parameters = new Array();

		arr_parameters[ arr_parameters.length ] = "calendar_object=" + this.object_name;
		arr_parameters[ arr_parameters.length ] = "close_method=" + this.close_method;

		if ( this.calendar_show_time != "")
			arr_parameters[ arr_parameters.length ] = "calendar_show_time=" + this.calendar_show_time;
		if ( this.max_timestamp != 0)
			arr_parameters[ arr_parameters.length ] = "max_timestamp=" + this.max_timestamp;
		if ( this.min_timestamp != 0)
			arr_parameters[ arr_parameters.length ] = "min_timestamp=" + this.min_timestamp;
		if ( this.timestamp != 0)
			arr_parameters[ arr_parameters.length ] = "initial_timestamp=" + this.timestamp;
		if ( this.weekends_selectable == false)
			arr_parameters[ arr_parameters.length ] = "disable_weekends=1";
		else
		{
			if ( this.initial_timestamp != 0)
				arr_parameters[ arr_parameters.length ] = "initial_timestamp=" + this.initial_timestamp;
		}

		if ( arr_parameters.length == 0)
			return this.script_name;

		return this.script_name + "?" + arr_parameters.join("&");
	}

	function show(caption)
	{
		jah( this.getIframeURL2(), this.ajax_target, 'auto', Function("eval('" + this.object_name + '.calendarLoaded()' + "');") );	// g_calendar

		if ( arguments.length == 0)
		{
			if ( this.calendar_show_time == "minute" || this.calendar_show_time == "all" || this.calendar_show_time == "hour")
				caption = "Select date and time";
			else	caption = "Select date";
		}

			// display dialog box
		document.getElementById("calendar_dialog").style.display = 'block';

		if ( window.show_select_elements)
			show_select_elements('hidden');

		var tmp_padding = document.getElementById("calendar_dialog_inner").style.paddingLeft;

		document.getElementById("calendar_dialog_inner").style.padding = "0px";
		document.getElementById("calendar_dialog_box").style.width = document.getElementById("calendar_dialog_inner").scrollWidth + 30 + "px";
		document.getElementById("calendar_dialog_caption").innerHTML = caption;
		document.getElementById("calendar_dialog_inner").style.padding = "15px";
		document.getElementById("calendar_dialog_header").style.width = document.getElementById("calendar_dialog_box").style.width;

		document.getElementById("calendar_dialog_close_button").onclick = Function("eval('" + this.object_name + '.hide()' + "');");
		document.getElementById("calendar_dialog_box").focus();
		document.getElementById("calendar_dialog_box").scrollIntoView();
	}

	function hide()
	{
		document.getElementById("calendar_dialog").style.display = 'none';

		if ( window.show_select_elements)
			show_select_elements('visible');
	}

	function calendarLoaded(calendar_object)
	{
		//alert(this.object_name);
		//document.getElementById("calendar_iframe").src = this.getIframeURL2();	// g_calendar
	}

	function setDateTime(day,month,year,hour,minute,second)
	{
		hour = hour == undefined ? 0 : hour;
		minute = minute == undefined ? 0 : minute;
		second = second == undefined ? 0 : second;

		this.day = day;
		this.month = month;
		this.year = year;
		this.hour = hour;
		this.minute = minute;
		this.second = second;

		this.timestamp = this.getUnixTimestamp();
	}

	function getFormattedDate(display_time_format)
	{
		if ( this.year == 0 || this.year == "")
			return "";

		var myDate = new Date(this.year,this.month-1,this.day,this.hour,this.minute,this.second);

	 	return myDate.format( arguments.length != 0 ? display_time_format : this.display_time_format);
	}

	function setCalendarShowTime(calendar_show_time)
	{
		this.calendar_show_time = calendar_show_time;
	}

	function createPopupDialog()
	{
		var html;

		html = '<div id="calendar_dialog">' + "\n";
		html += '	<img src="images/waiting_back.gif" alt="" style="display:none" />' + "\n";
		html += '	<div id="calendar_dialog_box">' + "\n";
		html += '		<div id="calendar_dialog_header">' + "\n";
		html += '			<div id="calendar_dialog_close_button"></div>' + "\n";
		html += '			<span id="calendar_dialog_caption"></span>' + "\n";
		html += '		</div>' + "\n";
		html += '		<div id="calendar_dialog_inner"></div>' + "\n";
		html += '	</div>' + "\n";
		html += '</div>' + "\n";

		document.writeln(html);
	}
}