

// ===================================================================
// Author: Matt Kruse <matt@mattkruse.com>
// WWW: http://www.mattkruse.com/
//
// NOTICE: You may use this code for any purpose, commercial or
// private, without any further permission from the author. You may
// remove this notice from your final code if you wish, however it is
// appreciated by the author if at least my web site address is kept.
//
// You may *NOT* re-distribute this code in any way except through its
// use. That means, you can include it in your product, or your web
// site, or any other form where the code is actually being used. You
// may not put the plain javascript up on your site for download or
// include it in your javascript libraries for download.
// If you wish to share this code with others, please just point them
// to the URL instead.
// Please DO NOT link directly to my .js files from your site. Copy
// the files to your server and use them there. Thank you.
// ===================================================================

/*
AnchorPosition.js
Author: Matt Kruse
Last modified: 10/11/02

DESCRIPTION: These functions find the position of an <A> tag in a document,
so other elements can be positioned relative to it.

COMPATABILITY: Netscape 4.x,6.x,Mozilla, IE 5.x,6.x on Windows. Some small
positioning errors - usually with Window positioning - occur on the
Macintosh platform.

FUNCTIONS:
getAnchorPosition(anchorname)
  Returns an Object() having .x and .y properties of the pixel coordinates
  of the upper-left corner of the anchor. Position is relative to the PAGE.

getAnchorWindowPosition(anchorname)
  Returns an Object() having .x and .y properties of the pixel coordinates
  of the upper-left corner of the anchor, relative to the WHOLE SCREEN.

NOTES:

1) For popping up separate browser windows, use getAnchorWindowPosition.
   Otherwise, use getAnchorPosition

2) Your anchor tag MUST contain both NAME and ID attributes which are the
   same. For example:
   <A NAME="test" ID="test"> </A>

3) There must be at least a space between <A> </A> for IE5.5 to see the
   anchor tag correctly. Do not do <A></A> with no space.
*/

// getAnchorPosition(anchorname)
//   This function returns an object having .x and .y properties which are the coordinates
//   of the named anchor, relative to the page.
function getAnchorPosition(anchorname) {
	// This function will return an Object with x and y properties
	var useWindow=false;
	var coordinates=new Object();
	var x=0,y=0;
	// Browser capability sniffing
	var use_gebi=false, use_css=false, use_layers=false;
	if (document.getElementById) { use_gebi=true; }
	else if (document.all) { use_css=true; }
	else if (document.layers) { use_layers=true; }

	// Logic to find position
 	if (use_gebi) {
		var o=document.getElementById(anchorname);
		x=AnchorPosition_getPageOffsetLeft(o);
		y=AnchorPosition_getPageOffsetTop(o);
		}
 	else if (use_css) {
		x=AnchorPosition_getPageOffsetLeft(document.all[anchorname]);
		y=AnchorPosition_getPageOffsetTop(document.all[anchorname]);
		}
	else if (use_layers) {
		var found=0;
		for (var i=0; i<document.anchors.length; i++) {
			if (document.anchors[i].name==anchorname) { found=1; break; }
			}
		if (found==0) {
			coordinates.x=0; coordinates.y=0; return coordinates;
			}
		x=document.anchors[i].x;
		y=document.anchors[i].y;
		}
	else {
		coordinates.x=0; coordinates.y=0; return coordinates;
		}
	coordinates.x=x;
	coordinates.y=y;
	return coordinates;
	}

// getAnchorWindowPosition(anchorname)
//   This function returns an object having .x and .y properties which are the coordinates
//   of the named anchor, relative to the window
function getAnchorWindowPosition(anchorname) {
	var coordinates=getAnchorPosition(anchorname);
	var x=0;
	var y=0;
	if (document.getElementById) {
		if (isNaN(window.screenX)) {
			x=coordinates.x-document.body.scrollLeft+window.screenLeft;
			y=coordinates.y-document.body.scrollTop+window.screenTop;
			}
		else {
			x=coordinates.x+window.screenX+(window.outerWidth-window.innerWidth)-window.pageXOffset;
			y=coordinates.y+window.screenY+(window.outerHeight-24-window.innerHeight)-window.pageYOffset;
			}
		}
	else if (document.all) {
		x=coordinates.x-document.body.scrollLeft+window.screenLeft;
		y=coordinates.y-document.body.scrollTop+window.screenTop;
		}
	else if (document.layers) {
		x=coordinates.x+window.screenX+(window.outerWidth-window.innerWidth)-window.pageXOffset;
		y=coordinates.y+window.screenY+(window.outerHeight-24-window.innerHeight)-window.pageYOffset;
		}
	coordinates.x=x;
	coordinates.y=y;
	return coordinates;
	}

// Functions for IE to get position of an object
function AnchorPosition_getPageOffsetLeft (el) {
	var ol=el.offsetLeft;
	while ((el=el.offsetParent) != null) { ol += el.offsetLeft; }
	return ol;
	}
function AnchorPosition_getWindowOffsetLeft (el) {
	return AnchorPosition_getPageOffsetLeft(el)-document.body.scrollLeft;
	}
function AnchorPosition_getPageOffsetTop (el) {
	var ot=el.offsetTop;
	while((el=el.offsetParent) != null) { ot += el.offsetTop; }
	return ot;
	}
function AnchorPosition_getWindowOffsetTop (el) {
	return AnchorPosition_getPageOffsetTop(el)-document.body.scrollTop;
	}

	
	
	
	
	
	
	
	
	
// ===================================================================
// Author: Radek Parchanski <diwi@seznam.cz>
// 20.01.2008
//
// NOTICE: You may use this code for any purpose, commercial or
// private, without any further permission from the author. 
// ===================================================================

// jsDate 1.0

// HELP ---
// Days
// d  	Day of the month, 2 digits with leading zeros  	01 to 31
// D  	A textual representation of a day, three letters  	Mon through Sun
// j  	Day of the month without leading zeros  	1 to 31
// l   	A full textual representation of the day of the week  	Sunday through Saturday
// N  	ISO-8601 numeric representation of the day of the week (added in PHP 5.1.0)  	1 (for Monday) through 7 (for Sunday)
// S  	English ordinal suffix for the day of the month, 2 characters  	 st, nd, rd or th. Works well with j
// w  	Numeric representation of the day of the week  	0 (for Sunday) through 6 (for Saturday)
// z  	The day of the year (starting from 0)  	0 through 365

// Weeks
// W  	ISO-8601 week number of year, weeks starting on Monday (added in PHP 4.1.0)  	Example: 42 (the 42nd week in the year)

// Months
// F  	A full textual representation of a month, such as January or March  	January through December
// m  	Numeric representation of a month, with leading zeros  	01 through 12
// M  	A short textual representation of a month, three letters  	Jan through Dec
// n  	Numeric representation of a month, without leading zeros  	1 through 12
// t  	Number of days in the given month  	28 through 31

/// Years
// L  	Whether it's a leap year  	1 if it is a leap year, 0 otherwise.
// Y  	A full numeric representation of a year, 4 digits  	Examples: 1999 or 2003
// y  	A two digit representation of a year  	Examples: 99 or 03


// date must be array [year, month, day] (month start from 0 to 11)
function jsDate(format, date) {
	var str = '';
	format = format.split("");

	for (var i=0; i<format.length; i++) {
		if (format[i].match(/[a-z]/gi)) str += jsDateConvertChar(format[i], date);
		else str += format[i];
	}
	return str;
}

// return array [year, month, day] or false
function jsParseDate (format, date) {
	var dateArr = new Array();
	format = format.split("");

	for (var i=0; i<format.length; i++) { // step char by char
		if (format[i].match(/[a-z]/gi)) date = jsCreateDate(format[i], date, dateArr);
		else date = date.substr(1); // all non alphanumeric characters are removed
	}

	return (dateArr[0] && dateArr[1] >= 0 && dateArr[2]) ? dateArr : false;
}

function jsCreateDate (dateChar, date, dateArr) {
	var num = '' + date.match(/^[0-9]+/); // find numbers from begining of string, this is later removed from date
	var str = '' + date.match(/^[a-z]+/gi); // find letters from begining of string, this is later removed from date

	if (num != 'null' && (dateChar == 'd' || dateChar == 'j')) {
		date = date.substr(num.length);
		dateArr[2] = parseInt(num);
	}
	else if (num != 'null' && (dateChar == 'm' || dateChar == 'n')) {
		date = date.substr(num.length);
		dateArr[1] = parseInt(num) - 1;
	}
	else if (str != 'null' && (dateChar == 'F' || dateChar == 'M')) {
		for (var i=0; i<monthNames.length; i++)	{
			if (str == monthNames[i] || str == monthNames[i].substring(0, 3)) {
				date = date.substr(str.length);
				dateArr[1] = parseInt(i);
				break;
			}
		}
	}
	else if (num != 'null' && (dateChar == 'y' || dateChar == 'Y')) {
		date = date.substr(num.length);
		dateArr[0] = parseInt(num.length == 2 ? '20' + num : num);
	}
	else { // other chars which doesnt contain day or month or year we just strip
		if (num != 'null') date = date.substr(num.length);
		else if (str != 'null') date = date.substr(str.length);
	}

	return date;
}

function jsDateConvertChar(dateChar, date) { // funkci projde i znak, ktery nepodporuje a vrati jej bez zmeny

	Date.prototype.getDOY = function() {
		var onejan = new Date(this.getFullYear(),0,1);
		return Math.ceil((this - onejan) / 86400000);
	}

	Date.prototype.getWeek = function() {
		var onejan = new Date(this.getFullYear(),0,1);
		return Math.ceil((((this - onejan) / 86400000) + onejan.getDay())/7);
	}

	var tmpDate = new Date();
	tmpDate.setFullYear(date[0],date[1],date[2]);

	var monthLength = new Array(1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1); // konce mesice 31 nebo 30
	var leapYear = ( !(date[0]%4) && date[0]%100 || !(date[0]%400) ); // prestupny rok je pokud je delitelny 4, ale neni delitelny 100, ale pokud je delitelny 400 je prestupny take

	var normal_year = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	var leap_year = new Array(31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

	// Days
	if (dateChar == 'd') dateChar = (date[2] < 10) ? '0' + date[2] : date[2];
	else if (dateChar == 'j') dateChar = date[2];
	else if (dateChar == 'l') dateChar = dayNames[tmpDate.getDay()];
	else if (dateChar == 'D') dateChar = dayNames[tmpDate.getDay()].substring(0, 3);
	else if (dateChar == 'w') dateChar = tmpDate.getDay();
	else if (dateChar == 'N') dateChar = (tmpDate.getDay() == 0) ? 7 : tmpDate.getDay();
	else if (dateChar == 'z') dateChar = tmpDate.getDOY();
	else if (dateChar == 'S') {
		if (date[2] == 1 || date[2] == 21 || date[2] == 31) dateChar = 'st';
		else if (date[2] == 2 || date[2] == 22) dateChar = 'nd';
		else if (date[2] == 3 || date[2] == 33) dateChar = 'rd';
		else dateChar = 'th';
	}

	// Weeks
	else if (dateChar == 'W') dateChar = tmpDate.getWeek();

	// Months
	else if (dateChar =='m') dateChar = (date[1]+1 < 10) ? '0' + (date[1]+1) : date[1]+1;
	else if (dateChar == 'n') dateChar = date[1]+1;
	else if (dateChar == 'F') dateChar = monthNames[date[1]];
	else if (dateChar == 'M') dateChar = monthNames[date[1]].substring(0, 3);
	else if (dateChar == 't') {
		if (date[1] == 1) dateChar = leapYear ? 29 : 28;
		else dateChar = (monthLength[date[1]]) ? 31 : 30;
	}

	// Years
	else if (dateChar == 'Y') dateChar = date[0];
	else if (dateChar == 'y') dateChar = String(date[0]).substring(2);
	else if (dateChar == 'L') dateChar = leapYear ? 1 : 0;

	return dateChar;
}	
	
	
	
	
	
	
	


// ===================================================================
// Author: Radek Parchanski <diwi@seznam.cz>
// Inspired by http://calendar.swazz.org/
//
// NOTICE: You may use this code for any purpose, commercial or
// private, without any further permission from the author. 
// ===================================================================

// Calendar 1.0
// Require: AnchorPosition.js from Matt Kruse
// Require: jsDate.js Addon

// calendar configuration
		var dateFormat 	= 'Y-m-d';	// refer to php date function, supported all Date formats, exept time, timezone and Full date/time
		var disablePast = false;	// disable date selection in the past
		var startDay	= 'Mon';	// Mon = Monday; Sun = Sunday
		var language	= 'pl';		// cs = Cesky; en = English

		if( language == 'en' ) {
			var monthNames = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
			var dayNames = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
		}
		else if( language == 'pl' ) {
			var monthNames = new Array('Styczeń', 'Luty', 'Marzec', 'Kwiecień', 'Maj', 'Czerwiec', 'Lipiec', 'Sierpień', 'Wrzesień', 'Październik', 'Listopad', 'Grudzień');
			var dayNames = new Array('Niedziela', 'Poniedziałek', 'Wtorek', 'Środa', 'Czwartek', 'Piątek', 'Sobota');
		}
		else if( language == 'cs' ) {
			var monthNames = new Array('Leden', 'Únor', 'Březen', 'Duben', 'Květen', 'Červen', 'Črvenec', 'Srpen', 'Září', 'Říjen', 'Listopad', 'Prosinec');
			var dayNames = new Array('Neděle', 'Pondělí', 'Úterý', 'Středa', 'Čtvrtek', 'Pátek', 'Sobota');
		}
		else if( language == 'sk' ) {
			var monthNames = new Array('Január', 'Február', 'Marec', 'Apríl', 'Máj', 'Jún', 'Júl', 'August', 'September', 'Október', 'November', 'December');
			var dayNames = new Array('Nedeľa', 'Pondelok', 'Utorok', 'Streda', 'štvrtok', 'Piatok', 'Sobota');
		}



// Calendar script
var calendar=new Object();

var calvalarr = new Array(42);

	// Start draw empty calendar table
	document.write(
		'<table id="calendar" style="display: none;" >' +
		'<thead><tr>' +
		//	'<th onclick="previous_year()">&lsaquo;</th>' +
			'<th></th>' +
			'<th onclick="previous_month()">&laquo;</th>' +
			'<th colspan="3" id="top_date"></th>' +
			'<th onclick="next_month()">&raquo;</th>' +
		//'<th	 onclick="next_year()">&rsaquo;</th>' +
		'<th></th>' +
		'</tr></thead>' +
			'<tr>');
	if (startDay == 'Sun') {
		for (var kk=0; kk<=6; kk++) {
			document.write('<th>' + dayNames[kk].substring(0, 2) + '</th>');
		}
	}
	else if (startDay == 'Mon')	{
		for (var kk=1; kk<=7; kk++) {
			document.write('<th>' + dayNames[(kk == 7 ? 0 : kk)].substring(0, 2) + '</th>');
		}
	}
	document.write('</tr>');

	for (var kk=1; kk<=6; kk++) {
		document.write('<tr>');
		for(var tt=1; tt<=7; tt++)
		{
			var num = 7 * (kk-1) - (-tt);
			document.write('<td id="v' + num + '">&nbsp;</td>');
		}
		document.write('</tr>');
	}
	// End draw calendar table
	document.write('</table>');

function showCalendar(input_id) {


	calendar.date = actual_date(); // actual date
	calendar.display = actual_date() // internal date of displayed calendar
	calendar.input = new Array(3); // date from input filed

	calendar.inputObject = getObj(input_id); // objekt input
	calendar.tableObject = getObj('calendar'); // objekt calendar table
	var Position = getAnchorPosition(input_id); // zjisti pozici inputu

	// umisti tabulku kalendare na souradnice pod input
	calendar.tableObject.style.left = Position.x + 1 + "px";
	calendar.tableObject.style.top = Position.y + 1 + calendar.inputObject.offsetHeight + "px";

	//calendar.tableObject.style.top += calendar.inputObject.offsetHeight + "px";
	calendar.tableObject.style.display = '';

	// First check date is valid
	calendar.input = jsParseDate (dateFormat, calendar.inputObject.value);
	if (calendar.input) calendar.display = copyObj(calendar.input); // set display date from input
	else calendar.display = copyObj(calendar.date); // datum inputu neni platne, vykreslime aktualni kalendar

	fillCalendar(calendar.display[1], calendar.display[0]);

	 // vyrusi prvni kliknuti na odkaz, aby se nezavrel kalendar
	document.all ? document.attachEvent('onclick', checkClick) : document.addEventListener('click', checkClick, false);
}

function fillCalendar(month, year) {
	var days_in_past = false; // calendar display past days overlined
	var is_today = false; // calendar display actual day with extra background
	var is_selected = false; // select day from input into calendar

	var last_day = parseInt(jsDate('t', calendar.display)); // last day in month
	// set first day in month and get his day in week
	var day_in_week = new Date();
	day_in_week.setFullYear(year,month,1); // Set first day in month
	day_in_week = day_in_week.getDay(); // 0 = sunday, 1 = monday etc..

	// posun dne v tydnu pokud ma tyden zacinat pondelim
	if (startDay == 'Mon') {
		day_in_week--;
		if (day_in_week < 0) day_in_week = 6;
	}

	getObj('top_date').innerHTML = jsDate('M Y', calendar.display).toUpperCase();

	var tmp_day = 0;
	for (var position=1; position<=42; position++)
	{
		var cell = getObj('v' + parseInt(position));

		tmp_day = position - day_in_week; // we must calculate day from difference between cell position and day in week
		if (position >= day_in_week +1 && position <= day_in_week + last_day)
		{
			days_in_past = disablePast && (tmp_day < calendar.date[2]) && (month == calendar.date[1]) && (year == calendar.date[0]);
			// set variable true or false
			is_today = (tmp_day == calendar.date[2] && month == calendar.date[1] && year == calendar.date[0]);
			is_selected = (calendar.input && tmp_day == calendar.input[2] && month == calendar.input[1] && year == calendar.input[0]);

			if (days_in_past) // den v minulosti
				cell.className = 'old';
			else if (is_selected)
				cell.className = 'selected';
			else if (is_today)
				cell.className = 'today';
			else // normalni den
				cell.className = '';

			cell.onclick = (days_in_past) ? null : cs_click; // when onclick action detected on cell witch day, copy them to input
			cell.innerHTML = tmp_day; // display day in cell

			// postupne naplni calvararr vsemi datumy v mesici podle formatu napr. array(,,1.1.2008,2.1.2008,3.1.2008 atd..)
			calvalarr[position] = jsDate(dateFormat, [year,month,tmp_day]);
		}
		else
		{
			cell.onclick = null; // we must clear onlick action assigned to element by previous month
			cell.innerHTML = '&nbsp'; // clear day in cell, doesnt matter if exists or not
			cell.className = 'none';
		}
	}
}

function copyObj (obj) {
    var objCopy = {};
    for (var i in obj) {
        objCopy[i] = obj[i];
    };
    return objCopy;
};

function getObj(objID)
{
	var obj;
	if (document.getElementById) obj = document.getElementById(objID);
	else if (document.all) obj = document.all[objID];
	else if (document.layers) obj = document.layers[objID];
	return obj;
}

function checkClick(e) {
	var evt = e ? e : event;
	evt = evt.target ? evt.target : evt.srcElement;

	while (evt.parentNode) {
		if (evt == calendar.tableObject) return;
		evt = evt.parentNode;
	}
	calendar.tableObject.style.display = 'none';
}

function evtTgt(e) {
	var el;
	if (e.target) el = e.target;
	else if (e.srcElement) el = e.srcElement;
	if (el.nodeType==3) el = el.parentNode; // defeat Safari bug
	return el;
}

function EvtObj(e) {
	if (!e) e = window.event;
	return e;
}

function cs_click(e) {
	//alert(evtTgt(EvtObj(e)).id);
	calendar.inputObject.value = calvalarr[ evtTgt(EvtObj(e)).id.substring(1, evtTgt(EvtObj(e)).id.length) ];
	calendar.tableObject.style.display = 'none';
}

function actual_date() // return array(year,month,day)
{
	var date = new Date();
	return [date.getFullYear(),date.getMonth(),date.getDate()];
}

function previous_year() {
	// var endday = ( !(ccy%4) && ccy%100 || !(ccy%400) ) ? leap_year : normal_year;

	calendar.display[0]--;
	fillCalendar(calendar.display[1],calendar.display[0]);
}

function next_year() {
	// var endday = ( !(ccy%4) && ccy%100 || !(ccy%400) ) ? leap_year : normal_year;
	calendar.display[0]++;
	fillCalendar(calendar.display[1],calendar.display[0]);
}

function previous_month() {
	// var endday = ( !(ccy%4) && ccy%100 || !(ccy%400) ) ? leap_year : normal_year;
	calendar.display[1]-=1;
	if (calendar.display[1]<0) {
		calendar.display[1]=11;
		calendar.display[0]--;
	}
	cdayf();
	fillCalendar(calendar.display[1],calendar.display[0]);
}

function next_month() {
	// var endday = ( !(ccy%4) && ccy%100 || !(ccy%400) ) ? leap_year : normal_year;

	calendar.display[1]+=1;
	if (calendar.display[1]>=12) {
		calendar.display[1]=0;
		calendar.display[0]++;
	}
	cdayf();
	fillCalendar(calendar.display[1],calendar.display[0]);
}

function cdayf() {
	if (!disablePast || (calendar.display[0] > calendar.date[0] || (calendar.display[0] == calendar.date[0] && calendar.display[1] >= calendar.date[1]))) return;
	else {
		calendar.display[0] = calendar.date[0];
		calendar.display[1] = calendar.date[1];
		calendar.display[2] = calendar.date[2];
	}
}
