
/* Hilfsfunktionen
 *    für Fenster öffnen
 *      windowOpenMsg (ohne scrollbar und toolbar)
 *      windowOpenDok (mit scrollbar und toolbar)
 *      checkDate Datum prüfen
 *      home wie der Name sagt...
 */


    function home(url) {
        if (null==self.top.opener) {
            // bei FF null, wenn schon geschlossen;
            // bei IE nicht null - auch wenn geschlossen...
            self.location.replace(url);
        }
        else {
            if (self.top.opener.closed) {
                window.open (url);
                self.close();
            }
            else {
                self.top.opener.location.replace(url);
                self.top.opener.focus();
                self.close();
            }
        }
    }

    function checkDate (field,end) {
    /*
     * prüft field.value auf Datum; gibt Date-Object zurück oder null
     * unterstützte Formate:
     * v1: yyyy.mm.dd;
     * v2: d.m.y;
     * v3: d.mon.y
     * v4: yy
     * v5: yyyy
     * end: true, wenn bei v5 31.12  anstatt 1.1 ergänzt werden soll
     */

        var fieldValue=field.value.replace(/^[ \n\r\t]+/,"").replace(/[ \n\r\t]+$/,"");
        var x;

        /* v1: yyyy.mm.dd */
        x=fieldValue.match(/^([12][0-9]{3})[^0-9]?([0-9]{2})[^0-9]?([0-9]{2})$/i);
        if (null!=x) {
            return new Date (x[1],x[2]-1,x[3]);
        }  

        /* v2: d.m.y */
        x=fieldValue.match(/^0*([1-9]|[123][0-9])[^0-9]+0*([1-9]|1[0-9])[^0-9]+([0-9]{1,4})$/i);
        if (null!=x) {
            x[3]=parseInt(x[3]);
            x[3]=x[3]<30?2000+x[3]:x[3];
            return  new Date (x[3],x[2]-1,x[1]);
        }
        x=fieldValue.match(/^([1-9]|[123][0-9])[^0-9]*(jan|feb|fev|mar|apr|mai|may|jun|jui|jul|aug|sep|okt|oct|nov|dez|dec)[^0-9]*([0-9]{1,4})$/i);
        if (null!=x) {
            x[3]=parseInt(x[3]);
            x[3]=x[3]<30 ? 2000 + x[3] : x[3];
            var mon={'jan':0,'feb':1,'fev':1,'mar':2,'apr':3,'mai':4,'may':4,'jun':5,'jui':5,'jul':6,'aug':7,'sep':8,'okt':9,'oct':9,'nov':10,'dez':11,'dec':11};
            return new Date(x[3],mon[x[2].toLowerCase()],x[1]);
        }

        /* v4 oder v5: yy oder yyyy */
        x=fieldValue.match(/^0*([1-9][0-9]{0,3})$/i);
        if (null!=x) {
            x[1]=parseInt(x[1]);
            if (x[1]>=0 && x[1]<=99 || x[1]>=1901 && x[1]<=2050){
                x[1]=x[1]<20?2000+x[1]:x[1];
                if (end) {
                    return new Date(x[1],11,31);
                }
                else {
                    return new Date(x[1],0,1);
                }
            }
        }

        /* otherwise... */
        return null;
    }

    function windowOpenMsg (url,name,relWidth,relHeight) {
        var x = window.open (
            url,
            name,
            "height=" + Math.floor(screen.availHeight * relHeight) + ",width=" + Math.floor(screen.availWidth * relWidth) + ",resizable=yes,scrollbars=yes",
            true
        );
        return x;
    }

    function windowOpenDok (url,name,relWidth,relHeight) {
        var x = window.open (
            url,
            name,
            "height=" + Math.floor(screen.availHeight * relHeight) + ",width=" + Math.floor(screen.availWidth * relWidth) + ",resizable=yes,scrollbars=yes,toolbar=yes,status=yes",
            false
        );
        return x;
    }
            
    function windowOpenDokNoScrollBars (url,name,relWidth,relHeight) {
        var x = window.open (
            url,
            name,
            "height=" + Math.floor(screen.availHeight * relHeight) + ",width=" + Math.floor(screen.availWidth * relWidth) + ",resizable=yes,scrollbars=no,toolbar=yes,status=yes",
            false
        );
        return x;
    }

function date2str (datum) {
        // datum (Datum) in string yyyy-MM-dd konvertieren
        if (null==datum){
            return ""; 
        }
        else {
            var s=(datum.getFullYear()*10000 + (1+datum.getMonth())*100 + datum.getDate()).toString();
            return (s.substr(0,4)+"-"+s.substr(4,2)+"-"+s.substr(6,2));
        }
    }




/**
*
* UTF-8 data encode / decode
* http://www.webtoolkit.info/
*
**/

    var Utf8 = {

        // public method for url encoding
        encode : function (string) {
            string = string.replace(/\r\n/g,"\n");
            var utftext = "";
            for (var n = 0; n < string.length; n++) {
                var c = string.charCodeAt(n);
                if (c < 128) {
                    utftext += String.fromCharCode(c);
                }
                else if((c > 127) && (c < 2048)) {
                    utftext += String.fromCharCode((c >> 6) | 192);
                    utftext += String.fromCharCode((c & 63) | 128);
                }
                else {
                    utftext += String.fromCharCode((c >> 12) | 224);
                    utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                    utftext += String.fromCharCode((c & 63) | 128);
                }
            }

            return utftext;
        },

    // public method for url decoding
        decode : function (utftext) {
            var string = "";
            var i = 0;
            var c = c1 = c2 = 0;
            while ( i < utftext.length ) {
                c = utftext.charCodeAt(i);
                if (c < 128) {
                    string += String.fromCharCode(c);
                    i++;
                }
                else if((c > 191) && (c < 224)) {
                    c2 = utftext.charCodeAt(i+1);
                    string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                    i += 2;
                }
                else {
                    c2 = utftext.charCodeAt(i+1);
                    c3 = utftext.charCodeAt(i+2);
                    string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                    i += 3;
                }
            }
            return string;
        }
    }



