if (typeof Date.prototype.getWeek == "undefined") {
 Date.prototype.getWeek = function ()
  {
	var th = new Date(this.getFullYear(), this.getMonth(), this.getDate() + 4 - (this.getDay() || 7));
	return Math.floor((th-new Date(th.getFullYear(),0,1))/604800000)+1;	//(7*24*60*60*1000)
};}

function objGet(x,doc)	{var d=(doc)?doc:document; return (d.getElementById?d.getElementById(x):d.all?d.all[x]:d.layers?d.layers[x]:null);}
function selV(x)	{return x[x.selectedIndex].value;}

function obj_calendar(xName,xFrom,xTo,xConfig)
{
	this.title      = "Calendar";
	this.days       = "Sun,Mon,Tue,Wed,Thu,Fri,Sat".split(",");
	this.months     = "January,February,March,April,May,June,July,August,September,October,November,December".split(",");
	this.holidays   = {101:"New year",1224:"Xmas"};
	this.week       = "week";
	this.firstday   = 0;		// 0=Sunday
	this.dateformat = "yyyy-m-d";	// yyyy-m-d ... yyyy/yy/y/mm/m/dd/d
if (xConfig!=null)			// read config
	{
	var q,i,n;
	n = 'days';	 q = (xConfig[n]!=null) ? xConfig[n] : this[n];
	for (i=0;i<7;i++)  {if (q[i]==null) {q = this[n]; break;}}
	this[n]     = q;
	n = 'months';	 q = (xConfig[n]!=null) ? xConfig[n] : this[n];
	for (i=0;i<12;i++) {if (q[i]==null) {q = this[n]; break;}}
	this[n] = q;
	n = 'holidays';	 q = (xConfig[n]!=null) ? xConfig[n] : this[n];	this[n] = q;
	n = 'firstday';	 q = (xConfig[n]!=null) ? xConfig[n] : this[n];
	q = (q>=0 && q<7) ? q : this[n];
	this[n] = q;
	n = 'week';	 q = (xConfig[n]!=null) ? xConfig[n] : this[n];	this[n] = q;
	n = 'title';	 q = (xConfig[n]!=null) ? xConfig[n] : this[n];	this[n] = q;
	n = 'dateformat';q = (xConfig[n]!=null) ? xConfig[n] : this[n];	this[n] = q;
	}
	this.nameCal = xName;
	this.objCal  = xFrom;
	this.objTo   = xTo;
	this.writeDate = calWriteDate;
	this.readDate= calReadDate;
	this.open    = calWrite;
	this.close   = function() {this.objCal.innerHTML = "";}
}

function calReadDate()
{
var y,m,d, f,t,reg, a,b,i,j,n;
f = this.dateformat;
t = this.objTo.value;
reg = new RegExp("[0-9]","");
j = 0;
b = "";
y = ""; m = ""; d = "";
for (i=0;i<t.length;i++)
	{
	n = t.substring(i,i+1);
	if (reg.test(n))
		{
		a = f.substring(j,j+1);
		if (a=="y" || a=="m" || a=="d") {b = a;}
		else {j--;}
		switch (b)
			{
			case "y": y+= n; break;
			case "m": m+= n; break;
			case "d": d+= n; break;
			default: break;
			}
		}
	j++;
	}
if (y.length==2) {y=((new Date()).getFullYear().toString().substring(0,2))+y;}
if (y.length==0) {y=(new Date()).getFullYear().toString();}
if (d.length>0 && d.length<3 && m.length>0 && m.length<3 && y.length==4)
	{
	d = d*1; m = m*1-1; y = y*1;
	if (new Date(y,m,d)) {return new Date(y,m,d);}
	}
return new Date();
}

function zeroX(str,n)
{
if (str.length<n) {for(var i=0;i<n-str.length;i++) {str="0"+str;}}
return str;
}

function calWriteDate(y,m,d)
{
var f,t,a,str,i,y1,m1,d1;
f   = this.dateformat;
y1  = f.replace(/[^y]/g,"");
m1  = f.replace(/[^m]/g,"");
d1  = f.replace(/[^d]/g,"");
str = "";
for (i=0;i<f.length;i++)
	{
	a = f.substring(i,i+1);
	switch (a)
		{
		case "y": if (y1.length==2) {y = ""+y; y = y.substring(y.length-2,y.length);}
			  str+= zeroX(""+y,    y1.length); i+=y1.length-1; break;
		case "m": str+= zeroX(""+(m+1),m1.length); i+=m1.length-1; break;
		case "d": str+= zeroX(""+d,    d1.length); i+=d1.length-1; break;
		default:  str+=a; break;
		}
	}
this.objTo.value = str;
this.close();
}

function calWrite(date)
{
var a,d,d0,d2,m,m0,m1,m2,x,y,y2,w,w0, cal,i,j,days1,s1,s2,days2,dateT,h,c1,c2;
a  = this.nameCal;
if (date==null)	{date = this.readDate();}
dateT = new Date();
d2 = dateT.getDate();
m2 = dateT.getMonth();
y2 = dateT.getFullYear();
d  = date.getDate();
m  = date.getMonth();
y  = date.getFullYear();
x  = ((y2-y)==0 && (m2-m)==0) ? 1 : 0;	//is today?
m0 = new Date(y,m,  1);
m1 = new Date(y,m+1,1);
w0 = m0.getDay();
w0 = ((w0-this.firstday)<0) ? 6 : w0-this.firstday;
days1 = Math.round((m1.getTime()-m0.getTime())/86400000);	//1000*60*60*24

 s1 = "\n<select onchange=\""+a+".open(new Date(selV(this.form."+a+"year),selV(this),1))\" name=\""+a+"month\">";
for(i=0;i<12;i++)
{s1+= "\n<option value=\""+i+"\""+((i==m)?" selected=\"selected\"":"")+">"+this.months[i]+"<\/option>";}
 s1+= "\n<\/select>";
 s2 = "\n<select onchange=\""+a+".open(new Date(selV(this),selV(this.form."+a+"month),1))\" name=\""+a+"year\">";
for(i=y-2;i<y+3;i++)
{s2+= "\n<option value=\""+i+"\""+((i==y)?" selected=\"selected\"":"")+">"+i+"<\/option>";}
 s2+= "\n<\/select>";

days2 = "\n<tr>";
days2+= "\n<th>"+this.week+"<\/th>";
for (i=0;i<7;i++)	//nazvy dnu
	{
	j = this.days[(i+this.firstday)%7];
	j = (i==5 || i==6) ? "<i>"+j+"<\/i>" : j;
	days2+= "\n<th>"+j+"<\/th>";
	}
days2+= "<\/tr>";

cal = "<fieldset class=\"cal\">";
cal+= "\n<legend><span onclick=\""+a+".close()\">[x]<\/span> "+this.title+"<\/legend>";
cal+= "\n"+s1+s2;
cal+= "\n<table>";
cal+= "\n<thead>"+days2+"\n<\/thead>";
cal+= "\n<tbody align=\"right\">";

w  = w0;
j  = (days1+w0)%7;
c1 = ""; if (w>0)        {for (i=0;i<w;i++) {c1+= "\n<td>&nbsp;<\/td>";}} //prazdne pred
c2 = ""; if (j>0 && j<7) {for (i=j;i<7;i++) {c2+= "\n<td>&nbsp;<\/td>";}} //prazdne za
for (i=1;i<=days1;i++)
	{
	w%= 7;
	if (w==0 || i==1) {cal+= "<tr>\n<th>"+(new Date(y,m,i)).getWeek()+".<\/th>"+c1; c1="";}
	j = (x && i==d2) ? "<u>"+i+"<\/u>" : i;
	j = (i==d) ? "<b>"+j+"<\/b>" : j;
	h = (m+1)+(i<=9?"0":"")+i;
	h = (this.holidays[h]!=null) ? " title=\""+this.holidays[h]+"\"" : "";
	j = (w==5 || w==6 || h!="") ? "<i"+h+">"+j+"<\/i>" : j;
	cal+= "\n<td onclick=\""+a+".writeDate("+y+","+m+","+i+")\">"+j+"<\/td>";//Y-m-d
	if (i==days1) {cal+= c2;}
	if (w==7) {cal+= "<\/tr>";}
	w++;
	}
cal+= "\n<\/tbody>";
cal+= "\n<\/table>";
cal+= "\n<\/fieldset>";

this.objCal.innerHTML = cal;
}
