var cDate=new Date();
var cYear=cDate.getFullYear();
var cMonth=cDate.getMonth();
var cDay=cDate.getDate(); 

function setCalendar(dt) {
	cYear=dt.getFullYear();
	cMonth=dt.getMonth();
	cDay=dt.getDate();
	firstDay = dt.getDay();
	for(i = 0; i < 42; i++)
		eval("document.myform.B"+i+".value='-'");
	if ((firstDay -2)<-1)
	firstDay +=7;
	dayspermonth = getDaysPerMonth(cMonth);
	for(i = 1; i < dayspermonth+1; i++){
		i1=(i<10)? "0"+i : i;
		eval("document.myform.B"+(i+firstDay -2)+".value='"+i1+"'");
	}
	document.myform.Mesice.selectedIndex=cMonth;
	document.myform.Rok.value=cYear;
}

function getDaysPerMonth(m){
	daysArray=new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	days=daysArray[m];
	if (m==1){
		if((cYear% 4) == 0) {
			if(((cYear% 100) == 0) && (cYear% 400) != 0) 
				days = 28;
			else
				days = 29;
		}
	}
	return days;
}

function setNMonth(m){
	cMonth=m;
	setCalendar(new Date(cYear,cMonth,1));
} 

function setNYear(){
	temp=parseInt(document.myform.Rok.value);
	if (isNaN(temp)){
		alert("Rok musí být číslo");
	return;
	}
	cYear=temp;
	setCalendar(new Date(cYear,cMonth,1));
} 

function ShowDate(temp){
	if (temp=="-")
	return;
	cDay=temp;
	strA=cYear + "-" + (cMonth+1) + "-" + cDay;
	
    if (window.opener.dateType == 'datetime') {
		datum = new Date()
		with (datum){
			h = getHours();
			m = getMinutes();
			s = getSeconds();
		}
        strA += ' ' + formatNum2(h, 'hour') + ':' + formatNum2(m, 'minute') + ':' + formatNum2(s, 'second');
    }
	
	window.opener.dateField.value = strA;
    window.close();
}

/**
 * Formats number to two digits.
 *
 * @param   int number to format.
 * @param   string type of number
 */
function formatNum2(i, valtype) {
    f = (i < 10 ? '0' : '') + i;
    if (valtype && valtype != '') {
        switch(valtype) {
            case 'month':
                f = (f > 12 ? 12 : f);
                break;

            case 'day':
                f = (f > 31 ? 31 : f);
                break;

            case 'hour':
                f = (f > 24 ? 24 : f);
                break;

            default:
            case 'second':
            case 'minute':
                f = (f > 59 ? 59 : f);
                break;
        }
    }

    return f;
}

/**
 * Formats number to two digits.
 *
 * @param   int number to format.
 * @param   int default value
 * @param   string type of number
 */
function formatNum2d(i, default_v, valtype) {
    i = parseInt(i, 10);
    if (isNaN(i)) return default_v;
    return formatNum2(i, valtype)
}

/**
 * Formats number to four digits.
 *
 * @param   int number to format.
 */
function formatNum4(i) {
    i = parseInt(i, 10)
    return (i < 1000 ? i < 100 ? i < 10 ? '000' : '00' : '0' : '') + i;
}

function returnDate(d) {
    txt = d;
    if (window.opener.dateType != 'date') {
        // need to get time
        h = parseInt(document.getElementById('hour').value,10);
        m = parseInt(document.getElementById('minute').value,10);
        s = parseInt(document.getElementById('second').value,10);
        if (window.opener.dateType == 'datetime') {
            txt += ' ' + formatNum2(h, 'hour') + ':' + formatNum2(m, 'minute') + ':' + formatNum2(s, 'second');
        } else {
            // timestamp
            txt += formatNum2(h, 'hour') + formatNum2(m, 'minute') + formatNum2(s, 'second');
        }
    }

    window.opener.dateField.value = txt;
    window.close();
}

/**
 * Opens calendar window.
 *
 * @param   string      calendar.php parameters
 * @param   string      form name
 * @param   string      field name
 * @param   string      edit type - date/timestamp
 */
function openCalendar(params, form, field, type) {
    window.open("inc/calendar.php?" + params, "calendar", "width=188,height=175,status=yes");
    dateField = eval("document." + form + "." + field);
    dateType = type;
}


