// Variablen deklarieren
var x=0;var y=0; var h=0;

function oeffne(nr,hoehe,weite){
// UMRECHNUNGEN:

// für NN-Fenster muss ein größeres Window aufgebaut werden, da der kein CSS2 kann:
if (document.layers) {y=(Number(weite)+15);x=(Number(weite)+15);h=(Number(hoehe)+55)}

// für IE übernehmen wir einfach die Höhe (+50px für Button) und Breite
else {x=hoehe;h=(Number(hoehe)+50);y=weite;}

// nun das seperate Fenster öffnen:
mywin=window.open("","Detailansicht","height="+h+",width="+y+",top=10,left=10,screenX=10,screenY=10");
  mywin.document.open('text/html', 'replace');
  mywin.document.write('<HTML><HEAD><TITLE>Detailansicht<\/TITLE><LINK rel="stylesheet" href="/css/style.css.php" type="text/css"><\/HEAD>');
  mywin.document.write('<BODY leftmargin=0 topmargin=0>');
  mywin.document.write('<IMG SRC=');
  mywin.document.write(nr);
  mywin.document.write(' WIDTH=');
  mywin.document.write(weite);
  mywin.document.write(' HEIGHT=');
  mywin.document.write(hoehe);
  mywin.document.write(' BORDER=0 VSPACE=0 HSPACE=0 ALT="Lade Bild. Einen Moment bitte!" TITLE=""><BR>');
  mywin.document.write('<p align="center"><a href="#" onClick="self.close\(\);"><span class="hinterlegt">[ Fenster schließen ]<\/span><\/a><\/p>');
  mywin.document.write('<\/BODY><\/HTML>');
  mywin.document.close();
  mywin.focus();
}

onfocus=myCloser;
function myCloser()
{
    if(self.mywin && !self.mywin.closed)
    {
        self.mywin.close();  self.mywin = null;   
    }
}

/**
 * Ermitelt das eingebene Datum.
 *
 * @param   string      field value
 * @param   string      form name
 * @param   string      art name (from oder bis - Datum)
 */
function getCalendarParams(form, field, art) {
    //Datumswert ermitteln
    var date = eval("document." + form + "." + field + ".value");
    if (date == '') {
        date = '';
        var par = '';
    } else {
        // Datum wurde eingegeben - Tag, Monat, Jahr ermitteln
        var tag = date.substring( 0, 2);
        var monat = date.substring( 3, 5);
        var jahr = date.substring( 6, 11);
        var par = '?d=' + tag + '&m=' + monat + '&y=' + jahr + '&art=' + art;
    }
    alert(par);
    return par;
}
/**
 * Opens calendar window.
 *
 * @param   string      calendar.php parameters
 * @param   string      form name
 * @param   string      field name
 * @param   string      edit type - date/timestamp
 */
function openCalendar(form, field, type) {
    params = getCalendarParams(form, field, 'bis');
    window.open("/test2.php" + params, "calendar", "width=400,height=200,status=yes");
    dateField = eval("document." + form + "." + field);
    dateType = type;
}

function initCalendar() {
    if (!year && !month && !day) {
        /* Called for first time */
        if (window.opener.dateField.value) {
            value = window.opener.dateField.value;
            if (window.opener.dateType == 'datetime' || window.opener.dateType == 'date') {
                if (window.opener.dateType == 'datetime') {
                    parts   = value.split(' ');
                    value   = parts[0];

                    if (parts[1]) {
                        time    = parts[1].split(':');
                        hour    = parseInt(time[0],10);
                        minute  = parseInt(time[1],10);
                        second  = parseInt(time[2],10);
                    }
                }
                date        = value.split("-");
                day         = parseInt(date[2],10);
                month       = parseInt(date[1],10) - 1;
                year        = parseInt(date[0],10);
            } else {
                year        = parseInt(value.substr(0,4),10);
                month       = parseInt(value.substr(4,2),10) - 1;
                day         = parseInt(value.substr(6,2),10);
                hour        = parseInt(value.substr(8,2),10);
                minute      = parseInt(value.substr(10,2),10);
                second      = parseInt(value.substr(12,2),10);
            }
        }
        if (isNaN(year) || isNaN(month) || isNaN(day) || day == 0) {
            dt      = new Date();
            year    = dt.getFullYear();
            month   = dt.getMonth();
            day     = dt.getDate();
        }
        if (isNaN(hour) || isNaN(minute) || isNaN(second)) {
            dt      = new Date();
            hour    = dt.getHours();
            minute  = dt.getMinutes();
            second  = dt.getSeconds();
        }
    }
}

/**
 * Returns date from calendar.
 *
 * @param   string     date text
 */
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();
}