// Hilfs-, Initialisierungs- und Kalender-Funktionen
// version 05.12.2011
// author Hanno Sparbier-Conradus
// copyright SpC 2006-2011

function forEach( list, action ) {
    var len = list.length;
    for ( var i = 0; i < len; i++ ) {
        action( list[ i ], i );
    }
}

function attr( e, list ) {
    for ( var  key in list ) {
        if ( list.hasOwnProperty( key ) ) {
            var value = list[ key ];
            switch ( key ) {  // Ausnahmefälle wg. Internet Explorer
                case 'class': e.className = value;
                    break;
                case 'style': e.style.cssText = value;
                    break;
                case 'colspan': e.colSpan = value;
                    break;
                default:  e.setAttribute( key, value );
            }
        }
    };
    return e;
}

function  _e( e1, attributes /*,arguments*/ ) {
    if ( typeof  e1 === 'string' ) { // id-Kennung oder html-Tag
        var e = e1.charAt( 0 ) === '#' ? document.getElementById( e1.substring( 1 ) ) : document.createElement( e1 );
    }
    else {  // Object
        var e = e1;
    }
    if ( _e.arguments.length > 1 && attributes ) {  // 2. Parameter ? dann Attribute zuweisen
        attr( e, attributes );
    }
    if ( _e.arguments.length > 2 ) {  // 3 und mehr Parameter vorhanden
        for ( var i = 2; i < _e.arguments.length; i++ ) {
            var e2 = _e.arguments[ i ];
            if( typeof  e2 === 'function' ) {
                e2 = e2( );
            }
            else if ( typeof  e2 !== 'object' ) {
                e2 = document.createTextNode( e2 );
            }
            e.appendChild( e2 );  // Objekt anhängen
        }
    }
    return e;
}

function _eEach( e1, e2, e3, attributes ) {
    var e = _e( e1 );
    forEach( e3, function( value ) {
        e.appendChild( _e( e2, attributes, value ) );
    });
    return e;
}

var _ = function ( id ) {
   return document.getElementById( id );
}

var init =function ( ) {
    forEach( document.getElementsByTagName('select'), function( s ) {  // alle selects ohne id auf auto-Submit
        if ( ! s.id ) {
            s.onchange = function( ) { this.form.submit(); }
        }
    });
    if ( ( e = _e('#sel') ) !== null ) { // Submit-Button von Auswahlzeile entfernen
        e.removeChild( e.lastChild );
    };
    forEach( document.getElementsByName('e3'), function( s ) {  // alle Formulare
        s.target = 'loader'; // target auf iframe setzen
        s.action += '&x'; // Marker im action-String setzen
    });
    forEach( _e('#content').getElementsByTagName('a'), function( s ) {  // alle content-internen Links
        if ( s.target != '_blank' && s.target != '_self' ) {
            s.target = 'loader'; // target auf iframe setzen
            if ( s.href !== null ) {
               s.href += s.href.indexOf( '?' ) === -1 ? '?x' : '&x'; // Marker im action-String setzen
            }
        }
    });
}

function openBox( id ) {
    var e, s;
    if ( ( s = _e('#e1') ) !== null ) {
        s.style.position = 'relative';  // auf relative Positionierung umsteigen
        e = _e( '#e2' );
        e.style.left = '1.3em'; e.style.top = '1.2em';   // innere Koordinaten verändern
        e = _e( '#' + id );
        e.parentNode.insertBefore( s, e ); // Box neben den auslösenden Link positionieren
        forEach( document.getElementsByName('js_active'), function( s ) {
            s.style.display = 'inline';  // Buttons einblenden, die nur mit JS funktionieren
        });
        e = _e('#last');
        e.focus( ); e.blur( ); // ggf. auf letztes Formularelement scrollen
    }
}
        
var closeBox = function( ) {
    kal.close();
    _e( '#e2', { style: 'display:none' } );
}

// Kalenderfunktionen
var kal = (function( ) {
    // private Eigenschaften und Funktionen
    var month_names = [
        'Januar', 'Februar', 'März', 'April', 'Mai', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'Dezember'
    ],
    year = 0, month = 0, day = 0,
    Ref = null, val = null,
    init = function( ) {
        if ( !year && !month && !day ) {
            if ( val ) {
                parts = val.split( '.' );
                day = parseInt( parts[ 0 ], 10 );
                month = parseInt( parts[ 1 ], 10 ) - 1;
                year = parseInt( parts[ 2 ], 10 );
                if ( year < 70 ) {
                    year += 2000;
                }
                if ( year <= 99 ) {
                    year += 1900;
                }
            }
            else {
                var d = new Date();
                year = d.getFullYear();
                month = d.getMonth();
                day = d.getDate();
            }
        }
        else {
            if ( month > 11 ) {
                month %= 12;
                year++;
            }
            if ( month < 0 ) {
                month += 12;
                year--;
            }
        }
        _e( Ref + 'k', null, // Tabelle aufbauen und einfügen
            _e( 'table', { 'class' : 'outline', style : 'width:17.0em;position:absolute;top:-16em;left:0em' },
                _e( 'thead', null,
                    _e( 'tr', null,
                        _e( 'th', { 'class' : 'alter', 'colspan' : '7', style : 'text-align:center;padding:1px' },
                            _e( 'img', { src : '/SC/k.png', style : 'float:left' } ),
                            _e( 'img', { src : '/SC/a.png', style : 'float:right', title : 'schließen', onclick: 'kal.close()' } ),
                            month_names[ month ],
                            ' ',
                            year
                        )
                    ),
                    _eEach( _e( 'tr', { 'class' : 'right' } ), 'th', [ 'Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa', 'So' ] )
                ),
                function( ) {
                    var  tr, tbody = _e( 'tbody' ),
                    start = msec( [ year, month, 1 ] ),
                    wday = new Date( start ).getUTCDay(),
                    cur = msec( start, wday > 0 ? -wday + 1 : -6 ),
                    act = msec( [ year, month, day ] );
                    for ( var k = 0; k < 42; k++ ) {
                        if ( ( k % 7 ) === 0 ) {  // neue Reihe einfügen
                            _e( tbody, null,
                                tr = _e( 'tr', { 'class' : 'right' } )
                            );
                        }
                        var curDt = new Date( cur );
                        _e( tr, null,
                            _e( 'td', { 'class' : cur === act ? 'head' : curDt.getUTCMonth() === month ? 'light' : 'nocol' },
                                _e( 'a', { href : 'javascript:kal.ret(\'' + cur + '\')' },
                                    curDt.getUTCDate()
                                )
                            )
                        );
                        cur = msec( cur, 1 );
                    }
                    return tbody;
                },
                _e( 'tfoot', null,
                    _e( 'tr', { 'class' : 'alter' },
                        _e( 'td', { style : 'text-align:center', colspan : '7' },
                            _e( 'button', { style : 'width:15%', title : 'ein Jahr zurück', onclick : 'kal.go(-12)' }, String.fromCharCode( 171, 171 ) ),
                            _e( 'button', { style : 'width:10%', title : 'einen Monat zurück', onclick : 'kal.go(-1)' }, String.fromCharCode( 171 ) ),
                            _e( 'button', { style : 'width:33%', onclick : 'kal.heute()' }, 'heute' ),
                            _e( 'button', { style : 'width:10%', title : 'einen Monat weiter', onclick : 'kal.go(1)' }, String.fromCharCode( 187 ) ),
                            _e( 'button', { style : 'width:15%', title : 'ein Jahr weiter', onclick : 'kal.go(12)' }, String.fromCharCode( 187, 187 ) )
                        )
                    )
                )
            )
        );
    },
    msec = function( st, days ) {
        if ( typeof st === 'object' ) {
            return Date.UTC( st[ 0 ], st[ 1 ], st[ 2 ] );
        }
        return st + days * 86400000;
    },
    datestr = function( date ) {
        var dt = new Date( date ),
        d = dt.getUTCDate(), m = dt.getUTCMonth();
        return [ d < 10 ? '0' + d : d, ++m < 10 ? '0' + m : m, dt.getUTCFullYear() ].join('.');
    },
    remove = function( ) {
        var e = _e( Ref + 'k' );
        e.removeChild( e.firstChild );
    };
    return {  // öffentliche Methoden
        open : function ( Feld ) {
            kal.close( );
            Ref = '#' + Feld;
            val = _e( Ref ).value;
            init();
        },
        close : function ( ) {
            if ( Ref ) {
                _e( Ref + 'k' ).style.width = '0.0em';
                _e( Ref + 'k' ).style.height = '0.0em';
                remove();
                year = month = day = 0;
                Ref = null;
            }
        },
        go : function( diff ) {
            remove();
            month += diff;
            init();
        },
        heute : function( ) {
            remove();
            year = month = day = val = 0;
            init();
        },
        ret : function( dat ) {
            _e( Ref ).value = datestr( parseInt( dat ) );
            kal.close();
        }
    }
}) ( );

window.onload = init;

