document.write('<div>' +
	'<table id="fc" style="z-index:3000;position:absolute;border-collapse:collapse;background:#1e5ca8;border:1px solid #1e5ca8;display:none;" cellpadding="2">' +
	'<tr>' +
	'	<td colspan="7" id="mns" align="center" style="color: #fff; font:bold 12px Verdana" nowrap></td>' +
	'</tr>' +
	'<tr>' +
	'	<td style="cursor:pointer; color: #fff;" onclick="csuby()"> << </td>' +
	'	<td style="cursor:pointer; color: #fff;" onclick="csubm()"> < </td>' +
	'	<td style="cursor:pointer; text-align: center; color: #fff; " colspan="3" onclick="ctoday()">DNES</td>' +
	'	<td align="right" style="cursor:pointer; color: #fff;" onclick="caddm()"> > </td>' +
	'	<td align="right" style="cursor:pointer; color: #fff;" onclick="caddy()"> >> </td>' +
	'</tr>' +
	'<tr>' +
	'	<td align=center style="border-bottom: 1px solid #1e5ca8; background:#C7E1F3;font:12px Verdana">Po</td>' +
	'	<td align=center style="border-bottom: 1px solid #1e5ca8; background:#C7E1F3;font:12px Verdana">Út</td>' +
	'	<td align=center style="border-bottom: 1px solid #1e5ca8; background:#C7E1F3;font:12px Verdana">St</td>' +
	'	<td align=center style="border-bottom: 1px solid #1e5ca8; background:#C7E1F3;font:12px Verdana">Čt</td>' +
	'	<td align=center style="border-bottom: 1px solid #1e5ca8; background:#C7E1F3;font:12px Verdana">Pá</td>' +
	'	<td align=center style="border-bottom: 1px solid #1e5ca8; background:#C7E1F3;font:12px Verdana">So</td>' +
	'	<td align=center style="border-bottom: 1px solid #1e5ca8; background:#C7E1F3;font:12px Verdana">Ne</td>' +
	'</tr>'
);

for(var kk = 1; kk <= 6; kk++) {
	document.write('<tr>');
	for(var tt = 1; tt <= 7; tt++) {
		num = 7 * (kk - 1) - (-tt);
		document.write('<td id="v' + num + '" style="width:18px;height:18px">&nbsp;</td>');
	}
	document.write('</tr>');
}
document.write('</table>');
document.write('</div>');

$(document).click(checkCalendarClick);

var now = new Date;
var sccm = now.getMonth();
var sccy = now.getFullYear();
var sccd = now.getDate();
var ccm = now.getMonth();
var ccy = now.getFullYear();

var updobj;

function changeObjectDisplay(objectId, newDisplay) {
    document.getElementById(objectId).style.display = newDisplay;
}

function getObj(objID) {
    if (document.getElementById) {
    	return document.getElementById(objID);
    } else if (document.all) {
    	return document.all[objID];
    } else if (document.layers) {
    	return document.layers[objID];
    }
}

function checkCalendarClick(event) {
    var trg = $(event.target);
    if(trg.hasClass('field-calendar') || trg.parents('#fc').length) {
        return;
    }
}

function Left(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	} else if (obj.x) {
		curleft += obj.x;
	}
	return curleft;
}

function Top(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	} else if (obj.y) {
		curtop += obj.y;
	}
	return curtop;
}

function lcs(ielem) {
	ielem = document.getElementById(ielem);
	updobj = ielem;
    $('#fc').css('left', Left(ielem));
    $('#fc').css('top', Top(ielem) + ielem.offsetHeight);
    changeObjectDisplay('fc', 'block');

	curdt=ielem.value;
	curdtarr=curdt.split('.');
	isdt = true;
	for(var k = 0; k < curdtarr.length; k++) {
		if (isNaN(curdtarr[k]))
			isdt = false;
	}
	
	if (isdt&(curdtarr.length == 3)) {
		ccm=curdtarr[1] - 1;
		ccy=curdtarr[2];
		prepcalendar(curdtarr[0], curdtarr[1] - 1, curdtarr[2]);
	}
}

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;
	}
}

var mn = new Array('Leden','Únor','Březen','Duben','Květen','Červen','Červenec','Srpen','Září','Říjen','Listopad','Prosinec');
var mnn = new Array('31','28','31','30','31','30','31','31','30','31','30','31');
var mnl = new Array('31','29','31','30','31','30','31','31','30','31','30','31');
var calvalarr = new Array(42);

function f_cps(obj) {
	obj.style.background = '#F1F8FC';
	obj.style.font = '10px Verdana';
	obj.style.color = '#000';
	obj.style.textAlign = 'center';
	obj.style.textDecoration = 'none';
	obj.style.border = '0px';
	obj.style.cursor = 'pointer';
}

function f_hds(obj) {
	obj.style.background = '#fff';
	obj.style.font = 'bold 10px Verdana';
	obj.style.color = 'red';
	obj.style.textAlign = 'center';
	obj.style.border = '1px solid #6487AE';
	obj.style.cursor = 'pointer';
}

function calendarClick(evt) {
    trg = $(evt.target);
    $(updobj).val(calvalarr[trg.attr('id').substring(1, trg.attr('id').length)]);
    changeObjectDisplay('fc', 'none');
    $(updobj).trigger('change');
}

function prepcalendar(hd,cm,cy) {
	now = new Date();
	sd = now.getDate();
	td = new Date();
	td.setDate(1);
	td.setFullYear(cy);
	td.setMonth(cm);
	cd = td.getDay() - 1;
	getObj('mns').innerHTML = mn[cm]+ ' ' + cy;
	marr = ((cy%4) == 0) ? mnl : mnn;
	
	if (hd != '') {
		sccd = hd;
	} 
	
	if (cd < 0) cd = cd + 7;
	
	for(var d = 1; d <= 42; d++) {
		f_cps(getObj('v'+parseInt(d)));
		if ((d >= (cd + 1)) && (d <= cd - (-marr[cm]))) {
			selDay = ((d - cd == sccd) && (cm == sccm) && (cy == sccy));

			htd = ((hd != '') && (d - cd == hd));
			
			if (htd || selDay) {
				f_hds(getObj('v' + parseInt(d)));
			} else {
				f_cps(getObj('v' + parseInt(d)));
			}
            var cell = $('#v' + parseInt(d));
			if (selDay) {
				getObj('v' + parseInt(d)).onmouseover = null;
				getObj('v' + parseInt(d)).onmouseout = null;
			} else {
                cell.mouseover(function(evt){$(evt.target).css('background', '#c7e1f3')});
                cell.mouseout(function(evt){$(evt.target).css('background', '#F1F8FC')});
			}

            cell.click(calendarClick);
			getObj('v' + parseInt(d)).innerHTML = d - cd;
			calvalarr[d] = '' + (d - cd) + '.' + (cm - (-1)) + '.' + cy;
		} else {
			getObj('v' + d).innerHTML = '&nbsp;';
			getObj('v' + parseInt(d)).onmouseover = null;
			getObj('v' + parseInt(d)).onmouseout = null;
			getObj('v' + parseInt(d)).style.cursor = 'default';
		}
	}
}

prepcalendar('', ccm, ccy);

function caddm() {
	marr =((ccy%4) == 0) ? mnl : mnn;
	ccm += 1;
	if (ccm >= 12) {
		ccm = 0;
		ccy++;
	}
	prepcalendar('', ccm, ccy);
}

function caddy() {
	ccy++;
	prepcalendar('', ccm, ccy);
}

function csubm() {
	marr = ((ccy%4) == 0) ? mnl : mnn;
	ccm -= 1;
	if (ccm < 0) {
		ccm = 11;
		ccy--;
	}
	prepcalendar('', ccm, ccy);
}

function csuby() {
	ccy -= 1;
	prepcalendar('', ccm, ccy);
}

function ctoday() {
    now = new Date();
    prepcalendar(now.getDate(), now.getMonth(), now.getFullYear());
    updobj.value = now.getDate() + '.' + (now.getMonth() + 1) + '.' + now.getFullYear();
}
