var calendar;
var arrayCalendars = new Array();
function calendar_Run() {    
    var r;
    calendar_close();
    calendar_window = window.open('about:blank','calendar_window',"location=0,menubar=0,resizable=0,scrollbars=0,status=0,toolbar=0,width=230,height=190,left="+Math.floor((screen.width-196)/2)+",top="+Math.floor((screen.height-175)/2));
    openedCalendar = calendar_window; //okno s kalendarem

    calendar_DT = new Date();
    if(this.value instanceof Date)
        calendar_DT.setTime(this.value.getTime()); //datum kalendare
    calendar_actual = this.value;
    //projde pole vsech 'holidays' a odstrani tecky na koncich => calendar_holidays
    calendar_holidays = new Array();
    for (var i=0;i<this.holidays.length;i++) {
        if (this.holidays[i].charAt(this.holidays[i].length-1) == ".") calendar_holidays[i] = this.holidays[i].substring(0,this.holidays[i].length-1);
	    else calendar_holidays[i] = this.holidays[i];
	}
    if (this.mindate instanceof Date) {
        calendar_mindate = this.mindate;
    } else {
        calendar_mindate = new Date();
        calendar_mindate.setVal(this.mindate+' 00:00:00');
    }
    if (this.maxdate instanceof Date) {
        calendar_maxdate = this.maxdate;
    } else {
        calendar_maxdate = new Date();
        calendar_maxdate.setVal(this.maxdate+' 23:59:59');
    }
    //title - bezpecna cestina
    if (!this.title) calendar_title = 'Calendar';
    else calendar_title = this.title;
    calendar_id = this.id;
    calendar_styles = this.styles;

	window.onfocus=calendar_close;    
    this.drawCalendar();//calendar_Runtime();    
}
function calendar_Runtime() {
    var i,pd,bm,yr,ho, row;
    var mnames = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
    var wnames = new Array('Mo','Tu','We','Th','Fr','Sa','Su');
    var cal_document = calendar_window.document; //calendar dokument
    var Larrow = '&#x00ab;';
    var Rarrow = '&#x00bb;';
    cal_document.open();
    cal_document.write("<html>\n<head>\n");
    if (document.all) cal_document.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset="+document.charset+"\">\n");
    cal_document.write("<title>"+decodeURIComponent(calendar_title)+"</title>");
    cal_document.write("\n<style>\n"+calendar_styles+"\n</style>\n");
    cal_document.write("<scr"+"ipt language=\"javascript\">\n");
    cal_document.write("\nfunction ff(v) { var id='"+calendar_id+"'; window.opener.arrayCalendars[id].finishCalendar(v); }");//calendar_finishCalendar(v,id)
    cal_document.write("\nfunction set_month(v) { window.opener.calendar_DT.setMonth(v); window.opener.arrayCalendars['"+calendar_id+"'].drawCalendar(); }");
    cal_document.write("\nfunction set_year(v) { window.opener.calendar_DT.setFullYear(v); window.opener.arrayCalendars['"+calendar_id+"'].drawCalendar(); }");
    cal_document.write("</scr"+"ipt>\n");
    cal_document.write("</head>\n<body topmargin=\"3\" leftmargin=\"3\" marginheight=\"3\" marginwidth=\"3\">");
    cal_document.write("<div class='calendar'>");
    cal_document.write("<table width=\"180\" cellpadding=\"1\" cellspacing=\"0\" border=\"0\"><tr><td class=\"border\">\n<table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\"><tr><td width=\"20\" class=\"head\">");
    if ((calendar_DT.getFullYear() > calendar_mindate.getFullYear()) || (calendar_DT.getMonth() > calendar_mindate.getMonth()) && (calendar_DT.getFullYear() == calendar_mindate.getFullYear())) cal_document.write("<a href=\"javascript:set_month("+(calendar_DT.getMonth()-1)+");\"><b>"+Larrow+"</b></a>");
    else cal_document.write("<b>"+Larrow+"</b>");
	cal_document.write("</td><td class=\"head\">"+mnames[calendar_DT.getMonth()]+"&nbsp;&nbsp;"+calendar_DT.getFullYear());
	cal_document.write("</td><td class=\"head\" width=\"20\">");
    if ((calendar_DT.getFullYear() < calendar_maxdate.getFullYear()) || ((calendar_DT.getMonth() < calendar_maxdate.getMonth()) && (calendar_DT.getFullYear() == calendar_maxdate.getFullYear()))) cal_document.write("<a href=\"javascript:set_month("+(calendar_DT.getMonth()+1)+");\"><b>"+Rarrow+"</b></a>");
    else cal_document.write("<b>"+Rarrow+"</b>");
    cal_document.write("</td></tr></table><table width=\"100%\" cellpadding=\"2\" cellspacing=\"0\" border=\"0\"><tr>");
    for (i=0;i<6;i++) cal_document.write("<th width=\"20\">"+wnames[i]+"</th>");
    cal_document.write("<th width=\"20\"><span>"+wnames[6]+"</span></th></tr>\n");
    
    calendar_DT.setDate(1);
    pd = calendar_DT.getDay()-1;
    row = 0;
    if (pd == -1) pd = 6; //nedele
	bm = calendar_DT.getMonth();
	yr = calendar_DT.getFullYear();
	if (bm == 0) { // kdyz je leden, nastavi se prosinec predchoziho roku
	    calendar_DT.setMonth(11);
	    calendar_DT.setFullYear(yr-1);
	} else calendar_DT.setMonth(bm-1); //jinak se nastavi predchozi mesic
	for (var i=calendar_DT.getDaysOfMonth()-pd+1;i<=calendar_DT.getDaysOfMonth();i++) {
	    calendar_DT.setDate(i);
 	    if (calendar_DT.getDay() == 0) ho = "class=\"holiday\""; //zvyrazneni nedele
	    else ho = "";
	    cal_document.write("<td>&nbsp</td>");
	}
	
	calendar_DT.setDate(1);
	calendar_DT.setMonth(bm);
	calendar_DT.setFullYear(yr);	
	for(var i=0;i<calendar_DT.getDaysOfMonth();i++) {	   
	    calendar_DT.setDate(i+1);
	    if ((calendar_DT.getDay() == 1) && (calendar_DT.getDate() != 1)) {
	 	    cal_document.write("</tr>\n<tr>");
	 	    row++;
	    } else if ((calendar_DT.getDay() == 1) && (calendar_DT.getDate() == 1)) cal_document.write("<tr>");
	    
	    if((calendar_DT.getDate() == calendar_actual.getDate()) && (calendar_DT.getMonth() == calendar_actual.getMonth()) && (calendar_DT.getFullYear() == calendar_actual.getFullYear()))
	        ho= "class=\"calendaractive\""; //hodnota nastavena v objektu kalendare
	    else if (calendar_DT.getDay() == 0 || calendar_DT.getDay() == 6) ho= "class=\"calendarweekend\"";//"class=\"holiday\"";
	    else ho = "";
 	    for (var j=0;j<calendar_holidays.length;j++){
 	        if ((calendar_holidays[j].substring(0,calendar_holidays[j].indexOf(".")) == calendar_DT.getDate()) && (calendar_holidays[j].substring(calendar_holidays[j].indexOf(".")+1,calendar_holidays[j].length) == (calendar_DT.getMonth()+1)))
 	            ho = "class=\"calendarweekend\"";
 	    }
	    ///////////////* vypis dnu *//////////////////////////////
        cal_document.write("<td "+ho+">");	  
	    if ((calendar_DT.getTime() > calendar_maxdate.getTime()) || (calendar_DT.getTime() < calendar_mindate.getTime()))
	    	cal_document.write(calendar_DT.getDate());
	    else 
	        cal_document.write("<a href=\"javascript:ff('"+calendar_DT.getVal('Y-m-d')+"');\">"+calendar_DT.getDate()+"</a>");
	    cal_document.write("</td>");
	}
	
	calendar_DT.setDate(1);
	calendar_DT.setMonth(bm+1);
	pd = calendar_DT.getDay()-1;
	if (pd == 0) pd = 20;
	if (pd == -1) pd = 6;
	for (var i=1;i<=(7-pd);i++) {
	    cal_document.write("<td>&nbsp</td>");
	    calendar_DT.setDate(i);
	    if (calendar_DT.getDay()==0) ho="class=\"holiday\"";
	    else ho = "";
	}
	
	calendar_DT.setDate(1);
	calendar_DT.setMonth(bm);
	calendar_DT.setFullYear(yr);
    if (row < 5) cal_document.write("</tr><tr><td colspan=\"7\">&nbsp;</td>");
    cal_document.write("</tr></table></td></tr></table></div></body></html>");
    cal_document.close();
    window.calendar_window.focus();
}
function calendar_close() {
    if (openedCalendar && !window.openedCalendar.closed) {
	    window.openedCalendar.close();
	    window.openedCalendar = null;
	}
	window.onfocus=null;
}
function calendar_finishCalendar(datum) {
    var myDT = new Date();
    myDT.setVal(datum,'Y-m-d H:i:s');
    	window.calendar_window.close();
    	windowopenedCalendar = false;
    this.value=myDT;
    this.callBack();        
}
/*function calendar_setDate(obj,time){
	var arr = obj.split(".");
	var d = new Date();
	if(!time)time='00:00:00';
	var arrT = time.split(":");		
		if(arrT){
			d.setSeconds(arrT[2]);
			d.setMinutes(arrT[1]);
			d.setHours(arrT[0]);
		}
		d.setDate(arr[0]);
		d.setMonth(arr[1]-1);
		d.setFullYear(arr[2]);
	return d;
}*/
function calendar_callBack(){	
	eval(this.returnBack);
}
function calendar_setMindate(date){   
    this.mindate.setTime(date.getTime());
    this.mindate.setVal('00:00:00','H:i:s');
}
function calendar_setMaxdate(date){
    this.maxdate.setTime(date.getTime());
    this.maxdate.setVal('23:59:59','H:i:s');
}
function Calendar(id){
	this.id=id;
	this.mindate = new Date();
    this.maxdate = new Date();
    this.value = new Date();
}	
Calendar.prototype.id = null;
Calendar.prototype.returnBack = null; //navratova funkce
Calendar.prototype.mindate = null;
Calendar.prototype.maxdate = null;
Calendar.prototype.title = null;
Calendar.prototype.value = null;
Calendar.prototype.styles = "body {text-align: center;} .calendar { text-align: left; margin: auto; font-family: tahoma;  font-size: 70%;  text-align: center;  width: 210px;    } .calendar a {  color: #E0393D;  text-decoration: underline;  } .calendar a:hover {  color: #000;  text-decoration: none;  } .calendar tr.mes td  {  background: #fff;  } .calendar td.mes  {  text-align: center;  width: 150px;  } .calendar table {  border-collapse: collapse;  border: none;  font-size: 100%;  width: 100%;   } .calendar table th {  background: #A8B6BF;  border: 1px solid #fff;  text-align: center;  color: #fff;  padding: 3px;  /*width: 20px;*/  }  .calendar table td {  background: #F4F4F4;  border: 1px solid #fff;  color: #000;  padding: 3px;  text-align: center;  width: 30px;  } .calendar table td.na {   color: #aaa;  }  .calendar table td.calendarweekend {  background: #e7e7e7;  } .calendar table td.calendaractive {  background: #ffc0c0;  }";
Calendar.prototype.holidays = new Array();

Calendar.prototype.runCalendar = calendar_Run;
Calendar.prototype.drawCalendar = calendar_Runtime;
Calendar.prototype.finishCalendar = calendar_finishCalendar;
//Calendar.prototype.setDate = calendar_setDate; //pro nastaveni min a max date
Calendar.prototype.callBack = calendar_callBack; //po kliknuti na dane datum
Calendar.prototype.setMindate = calendar_setMindate;
Calendar.prototype.setMaxdate = calendar_setMaxdate;
//////////////////////////////////////////////////////////////////////////////////////////
/*************************** metody rozsirujici objekt Date *****************************/
//////////////////////////////////////////////////////////////////////////////////////////

function classDate_parse(textDT, format) { //datum, format
    var pole, i, x, y;
    var cas = new Array(this.getFullYear(), this.getMonth(), this.getDate(), this.getHours(), this.getMinutes(), this.getSeconds());
    if (!format) format = 'Y-m-d H:i:s';
    var p = new RegExp('[^a-zA-Z]+', 'g');
    format = format.replace(p, '');
    p = new RegExp('[^0-9a-zA-Z]+');
    pole = textDT.split(p);
    for (i=0;((i<format.length) && (i<pole.length));i++) {
        x = format.substr(i,1);
        if (!isNaN(pole[i])) { //ciselne hodnoty
            if (x == 'd') cas[2] = pole[i];
            else if (x == 'j') cas[2] = pole[i];
            else if (x == 'm') cas[1] = pole[i]-1;
            else if (x == 'n') cas[1] = pole[i]-1;
            else if (x == 'Y') cas[0] = pole[i];
            else if (x == 'H') cas[3] = pole[i];
            else if (x == 'G') cas[3] = pole[i];
            else if (x == 'i') cas[4] = pole[i];
            else if (x == 's') cas[5] = pole[i];
            else if (x == 'g') cas[3] = pole[i];
            else if (x == 'h') cas[3] = pole[i];
            else if (x == 'w') {}
            else return(false);
        } else {               //textove hodnoty
            if (x == 'F') {
                for (y=0;y<12;y++) if (this.mnames[y] == pole[i]) cas[1] = y;
            }
            else if (x == 'M') {
                for (y=0;y<12;y++) if (this.msnames[y] == pole[i]) cas[1] = y;
            }
            
            else if (x == 'a') {
                if ((pole[i] == 'pm') && (cas[3] < 12)) cas[3] += 12;
                else if ((pole[i] == 'am') && (cas[3] >= 12)) cas[3] -= 12;
            }
            else if (x == 'A') {
                if ((pole[i] == 'PM') && (cas[3] < 12)) cas[3] += 12;
                else if ((pole[i] == 'AM') && (cas[3] >= 12)) cas[3] -= 12;
            }
            else return(false);
        }
    }
    var xx = this.checkDate(cas);
    if (xx == false) return(false);
    this.setTime(xx);
    return(true);
}
function classDate_checkDate(pole) {
    if (!pole || !(pole instanceof Array)) return(false);
    if ((pole[5] < 0) || (pole[5] > 59)) return(false);
    if ((pole[4] < 0) || (pole[4] > 59)) return(false);
    if ((pole[3] < 0) || (pole[3] > 23)) return(false);
    var xx = new Date(pole[0], pole[1], pole[2], pole[3], pole[4], pole[5]);
    if (xx.getFullYear() != pole[0]) return(false);
    if (xx.getMonth() != pole[1]) return(false);
    if (xx.getDate() != pole[2]) return(false);
    return(xx.getTime());
}
function classDate_isLeapYear() { // zjistuje prestupnost aktualniho roku
	if (0 == this.getFullYear()%4 && ((this.getFullYear()%100 != 0) || (this.getFullYear()%400 == 0))) return(true);
	return(false);
}
function classDate_getDaysOfMonths() { // zjistuje pocet dnu v kazdem mesici aktualniho roku - vraci pole
	var daysOfMonth = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	if (this.isLeap()) daysOfMonth[1] = 29;
	return(daysOfMonth);
}
function classDate_getDaysOfMonth() { // zjistuje pocet dnu v aktualni mesici aktualniho roku - integer
	var daysOfMonths = this.getDaysOfMonths();
	return(daysOfMonths[this.getMonth()]);
}
function classDate_addDay(num) { // pricte x dni
	if (!num) num = 1; 
	var newDay = this.getTime() + (num * 24 * 60 * 60 * 1000);
	this.setTime(newDay);
}
function classDate_subDay(num) { // odecte x dni
	if (!num) num = 1;
	var newDay = this.getTime() - (num * 24 * 60 * 60 * 1000);
	this.setTime(newDay);
}

function classDate_format(format) {
    var i, x, y;
    var out = new String();
	if (!format) format = 'Y-m-d H:i:s';
	
	for (i=0;i<format.length;i++) {
        x = format.substr(i,1);
        if (x == 'a') {
            if (this.getHours() > 11) out += 'pm';
	        else out += 'am';
	    } 
	    else if (x == 'A') {
            if (this.getHours() > 11) out += 'PM';
	        else out += 'AM';
	    } 
	    else if (x == 'c') { }
	    else if (x == 'd') { 
	        if (this.getDate() < 10) out += '0';
	        out += this.getDate();
	    } 
	    else if (x == 'D') out += this.wsnames[this.getDay()];
	    else if (x == 'F') out += this.mnames[this.getMonth()];
        else if (x == 'g') { 
	        if (this.getHours() > 12) out += (this.getHours() - 12);
	        else if (this.getHours() == 0) out += '12';
	        else out += this.getHours();
	    } 
        else if (x == 'G') out += this.getHours();
        else if (x == 'h') { 
	        if (this.getHours() > 21) out += (this.getHours() - 12);
	        else if (this.getHours() > 12) out += '0' + (this.getHours() - 12);
	        else if (this.getHours() == 0) out += '12';
	        else if (this.getHours() < 10) out += '0' + this.getHours();
	        else out += this.getHours();
	    }
	    else if (x == 'H') { 
	        if (this.getHours() < 10) out += '0';
	        out += this.getHours();
	    } 
        else if (x == 'i') { 
	        if (this.getMinutes() < 10) out += '0';
	        out += this.getMinutes();
	    }
        else if (x == 'I') { }
        else if (x == 'j') out += this.getDate();
        else if (x == 'l') out += this.wnames[this.getDay()];
        else if (x == 'L') {
            if (this.isLeap()) out += '1';
            else out += '0';
        }
        else if (x == 'm') { 
	        if (this.getMonth() < 9) out += '0';
	        out += (this.getMonth() + 1);
	    }
	    else if (x == 'M') out += this.msnames[this.getMonth()];
        else if (x == 'n') out += (this.getMonth() + 1);
        else if (x == 'O') { }
        else if (x == 'r') out += this.getVal('D, d M Y H:i:s O');
        else if (x == 's') { 
	        if (this.getSeconds() < 10) out += '0';
	        out += this.getSeconds();
	    }
	    else if (x == 't') out += this.daysOfMonth();
	    else if (x == 'T') { }
        else if (x == 'w') out += this.getDay();
        else if (x == 'W') { }
        else if (x == 'Y') out += this.getFullYear();
        else if (x == 'y') {
            y = new String('aa00' + this.getFullYear());
            out += y.substr(y.length - 2, 2);
        }
        else if (x == 'z') { }
        else if (x == 'Z') { }
        else out += x;
    }
    return(out);
}
function classDate_formatPresentation(format) {
    var i, x;
    var out = new String();
	if (!format) format = 'Y-m-d H:i:s';
	
	for (i=0;i<format.length;i++) {
        x = format.substr(i,1);
        if (x == 'a') out += 'am/pm';
	    else if (x == 'A') out += 'AM/PM';
	    else if (x == 'd') out += 'DD';
        else if (x == 'j') out += 'DD';
        else if (x == 'm') out += 'MM';
        else if (x == 'n') out += 'MM';
        else if (x == 'Y') out += 'YYYY';
        else if (x == 'H') out += 'hh';
        else if (x == 'G') out += 'hh';
        else if (x == 'i') out += 'mm';
        else if (x == 's') out += 'ss';
        else if (x == 'g') out += 'hh';
        else if (x == 'h') out += 'hh';
        else if (x == 'F') out += 'MMMM';
        else if (x == 'M') out += 'MMM';
        else out += x;
    }
    return(out);
}
function dummy(){}

var openedCalendar = null; // aktualne otevrene kalendarove okno
Date.prototype.mnames = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
Date.prototype.msnames = new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
Date.prototype.wnames = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday');
Date.prototype.wsnames = new Array('Sun','Mon','Tue','Wed','Thu','Fri','Sat','Sun');
Date.prototype.checkDate = classDate_checkDate;
Date.prototype.setVal = classDate_parse;
Date.prototype.isLeap = classDate_isLeapYear;
Date.prototype.getDaysOfMonths = classDate_getDaysOfMonths;
Date.prototype.getDaysOfMonth = classDate_getDaysOfMonth;
Date.prototype.getVal = classDate_format;
Date.prototype.formatPresentation = classDate_formatPresentation;
Date.prototype.addDay = classDate_addDay;
Date.prototype.subDay = classDate_subDay;
