// ___calendar__________________________________________________________________________________start
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 calendarRun(id, title, mindate, maxdate, holidays, styles, callBack) {    
    var r;
    calendar_close();
    calendar_window = window.open('about:blank','calendar_window',"location=no, menubar=no, resizable=no, scrollbars=no, status=no, directories=no, toolbar=no, width=186,height=146,left="+Math.floor((screen.width-196)/2)+",top="+Math.floor((screen.height-175)/2));
    openedCalendar = calendar_window; //okno s kalendarem

    calendar_DT = new Date(); //datum kalendare
    
    //projde pole vsech 'holidays' a odstrani tecky na koncich => calendar_holidays
    calendar_holidays = new Array();
    if (!holidays) holidays = new Array();
    for (i=0;i<holidays.length;i++) {
        if (holidays[i].charAt(holidays[i].length-1) == ".") calendar_holidays[i] = holidays[i].substring(0,holidays[i].length-1);
	    else calendar_holidays[i] = holidays[i];
	}

    if (mindate instanceof Date) {
        calendar_mindate = mindate;
    } else {
        calendar_mindate = new Date();
        calendar_mindate.setVal(mindate+' 00:00:00');
    }
    if (maxdate instanceof Date) {
        calendar_maxdate = maxdate;
    } else {
        calendar_maxdate = new Date();
        calendar_maxdate.setVal(maxdate+' 23:59:59');
    }

    //title - bezpecna cestina
    if (!title) calendar_title = encodeURIComponentFull('Kalendář');
    else calendar_title = title;
    calendar_id = id;

    if (callBack) calendar_callback = callBack;
    else calendar_callback = '';

    if (!styles) calendar_styles = "<link href=\"css/calendar.css\" rel=\"stylesheet\" type=\"text/css\"/>\n";
    else calendar_styles = decodeURIComponent(styles);
    
    if ((r = document.getElementById(id)) && (r.value != '')) calendar_DT.setVal(r.value);
    window.onfocus=calendar_close;
    calendar_Runtime();
}

function calendar_Runtime() {
    var i,pd,bm,yr,ho, row;
    var mnames = new Array('Leden','Únor','Březen','Duben','Květen','Červen','Červenec','Srpen','Září','Říjen','Listopad','Prosinec');
    var wnames = new Array('Po','Út','St','Čt','Pá','So','Ne');
    var cal_document = calendar_window.document; //calendar dokument
    var Larrow = '&#x00ab;';
    var Rarrow = '&#x00bb;';
    //var Larrow = '&lt;&lt;';
    //var Rarrow = '&gt;&gt;';
    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)+"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</title>");
    cal_document.write("\n"+calendar_styles+"\n");
    cal_document.write("<scr"+"ipt language=\"javascript\">\n");
    cal_document.write("\nfunction ff(v) { var id='"+calendar_id+"'; var cb='"+calendar_callback+"'; window.opener.calendar_finishDate(v,id,cb); }");
    cal_document.write("\nfunction set_month(v) { window.opener.calendar_DT.setMonth(v); window.opener.calendar_Runtime(); }");
    cal_document.write("\nfunction set_year(v) { window.opener.calendar_DT.setFullYear(v); window.opener.calendar_Runtime(); }");
    cal_document.write("</scr"+"ipt>\n");
    cal_document.write("</head>\n<body topmargin=\"3\" leftmargin=\"3\" marginheight=\"3\" marginwidth=\"3\">");
    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() != 0) && (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><form><td class=\"head\"><select name=\"m\" onchange=\"set_month(this[this.selectedIndex].value);\">");
	for (i=0;i<12;i++) {
	    cal_document.write("<option value=\""+i+"\"");
	    if (i == (calendar_DT.getMonth())) { cal_document.write(" selected=\"selected\""); }
	    cal_document.write(">"+mnames[i]+"</option>\n");
	}
	cal_document.write("</select><select name=\"r\" onchange=\"set_year(this[this.selectedIndex].value);\">");
	if ((calendar_DT.getFullYear() < calendar_mindate.getFullYear()) || (calendar_DT.getFullYear() > calendar_maxdate.getFullYear())) cal_document.write("<option value=\""+calendar_DT.getFullYear()+"\" selected=\"selected\">"+calendar_DT.getFullYear()+"</option>\n");
	for (i=calendar_mindate.getFullYear();i<(calendar_maxdate.getFullYear()+1);i++) {
	    cal_document.write("<option value=\""+i+"\"");
	    if (i == calendar_DT.getFullYear()) cal_document.write(" selected=\"selected\"");
	    cal_document.write(">"+i+"</option>\n");
	}
	cal_document.write("</select></td></form><td class=\"head\" width=\"20\">");
    if ((calendar_DT.getFullYear() < calendar_maxdate.getFullYear()) || ((calendar_DT.getMonth() != 11) && (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 (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(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.getDay() == 0) ho= "class=\"holiday\"";
	    else ho = "";
 	    for (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=\"holiday\"";
	    if ((calendar_DT.getTime() > calendar_maxdate.getTime()) || (calendar_DT.getTime() < calendar_mindate.getTime())) cal_document.write("<td "+ho+"><span class=\"disable\">"+calendar_DT.getDate()+"</span></td>");
	    else if (ho == "") cal_document.write("<td><a href=\"javascript:ff('"+calendar_DT.getVal('Y-m-d')+"');\">"+calendar_DT.getDate()+"</a></td>");
	    else cal_document.write("<td><a href=\"javascript:ff('"+calendar_DT.getVal('Y-m-d')+"');\"><span class=\"holiday\">"+calendar_DT.getDate()+"</span></a></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 (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></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;
}
//vola se na konci kalendare, po vybrani datumu / callBack = zaencodovany JS prikaz
function calendar_finishDate(datum, id, callBack) {
    var el1, el2, ele;
    var myDT = new Date();
    if (ele = document.getElementById(id)) {
        if (ele.value != '') myDT.setVal(ele.value); //vcetne casu
        myDT.setVal(datum, 'Y-m-d'); //jen datum (cas se zachova)
        ele.value = myDT.getVal('Y-m-d H:i:s');
    }
    window.calendar_window.close();
    openedCalendar = false;
    if ((el1 = document.getElementById(id+'_format')) && (el2 = document.getElementById(id+'_show'))) {
        el2.value = myDT.getVal(el1.value);
    }
    if (callBack) {
        callBack = decodeURIComponent(callBack);
        eval(callBack);
    }
}
function calendar_checkUsrDate(id) {
    var el1, el2, ele;
    var old = '';
    var myDT = new Date();
    if ((ele = document.getElementById(id)) && (el1 = document.getElementById(id+'_format'))) {
        if (ele.value != '') {
            myDT.setVal(ele.value); //vcetne casu
            old = myDT.getVal(el1.value);
        }
        if (el2 = document.getElementById(id+'_show')) {
            if (el2.value == '') { //uzivatel to smazal
                ele.value = ''; //vycisti se VALUE objektu
            } else if (!myDT.setVal(el2.value, el1.value)) { //parsne se uzivatelem zadane data
                alert('Chybné datum'+' '+el2.value+' - '+'použijte formát'+': '+myDT.formatPresentation(el1.value));
                el2.value = old;
            } else {
                el2.value = myDT.getVal(el1.value); //zpatky se zobrazi v koreknim formatu
                ele.value = myDT.getVal('Y-m-d H:i:s'); //ulozi se VALUE objektu
            }
        }
    }
}
function dummy(){}

var openedCalendar = null; // aktualne otevrene kalendarove okno
Date.prototype.mnames = new Array('Leden','Únor','Březen','Duben','Květen','Červen','Červenec','Srpen','Září','Říjen','Listopad','Prosinec');
Date.prototype.msnames = new Array('Led','Úno','Bře','Dub','Kvě','Čvn','Čec','Srp','Zář','Říj','Lis','Pro');
Date.prototype.wnames = new Array('Neděle','Pondělí','Úterý','Středa','Čtvrtek','Pátek','Sobota','Neděle');
Date.prototype.wsnames = new Array('Ned','Pon','Úte','Stř','Čtv','Pát','Sob','Ned');
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;
/*
function calendar_init(id) {
    var r, el1, el2;
    var myDT = new Date();
    if ((el1 = document.getElementById(id+'_format')) && (el2 = document.getElementById(id+'_show'))) {
        if ((r = document.getElementById(id)) && (r.value != '')) {
            myDT.setVal(r.value);
            el2.value = myDT.getVal(el1.value);
        }
        el2.title = myDT.formatPresentation(el1.value);
    }
}
*/
// ___calendar__________________________________________________________________________________end
