/*------------------------------------------*/
/*                                          */
/*      JavaScript by Sven Beyer            */
/*       mailto:admin@tzwaenn.de            */
/*                                          */
/*------------------------------------------*/

// glabal vars
//////////////////////////////////////////////

var jetzt=new Date();   // current time/ date


// global functions
//////////////////////////////////////////////

//
// ZweiStellen returns "0" followed by 'zahl' as string if 'zahl'<10;
// else it returns symply 'zahl' as string
//
function ZweiStellen(zahl)
{
    return zahl<10? "0" + zahl : "" + zahl;
}

//
// trim returns 'str' without leading and trailing blanks
//
function trim(str)
{
    if (str.length>0)
        while(str[0]==" ")
            str=str.substring(1,str.length);
    if (str.length>0)
        while(str[str.length-1]==" ")
            str=str.substring(0,str.length-1);
    return str;
}

//
// MeinDatumString returns 'Datum' like "DD.MM.YYYY, HH:MM Uhr"
//
function MeinDatumString(Datum)
{
    year  = Datum.getYear();
    if (year < 1000)
        year += 1900;
    month = Datum.getMonth()+1
    return ZweiStellen(Datum.getDate()) + "." + ZweiStellen(month) + "." +
              year + ", " + ZweiStellen(Datum.getHours()) + ":" +
              ZweiStellen(Datum.getMinutes()) + " Uhr";
}

//
// DifferenceString returns the differenc between 'Datum' and 
// now (as represented by global var 'jetzt) like
// [vor] "X Tagen und Y Stunden" (including special cases
// "einem Tag" and "einer Stunde"
//
function DifferenceString(Datum)
{
    var diffDate  =Math.floor((jetzt.getTime()-Datum.getTime())/3600000);  //Differenz in Stunden
    if (diffDate<0)
        diffDate=0;
    var diffDays  =Math.floor(diffDate/24);
    var diffHours =Math.floor(diffDate%24);
    return (diffDays==1? "einem Tag": diffDays + " Tagen")+ " und " + (diffHours==1? "einer Stunde": diffHours + " Stunden");
}

// WertHolen returns value of cookie 'Name' as string; 
// returns "", if there is no such cookie
//
function WertHolen(Name)
{
    var Wert = "";
    if(document.cookie) 
    {
        var cookies = document.cookie.split(";");            // Cookie in einzelne Cookies splitten
        for(i=0;i<cookies.length;i++)                        // alle Cookies durchgehen
        {
            var thiscookie = cookies[i].split("=");          // einzelnes Cookie in Name und Wert splitten
            thiscookie[0]=trim(thiscookie[0]);               // Leerzeichen storen nur
            if (thiscookie[0]==Name && thiscookie.length>=1) // gefunden?
            {
                Wert=thiscookie[1];                          // Wert lesen
                break;                                       // fertig
            }
        }
    }
    return Wert;
}

//
// WertSetzen writes value 'Wert' for cookie 'Name' with expiry time 'Verfall'
//
function WertSetzen(Name, Wert, Verfall)
{
     var Auszeit = new Date(jetzt.getTime() + Verfall);
     document.cookie = Name + "=" + Wert + "; expires=" + Auszeit.toGMTString() + ";";
}

//
// wait waits for msec milliseconds
//
function wait(msec)
{
    var wait_start = new Date(); // reference time
    var ende       = wait_start.getTime() + msec;
    var current_time=new Date(); // now
    while (ende > current_time.getTime())
        current_time=new Date();
}

