/*------------------------------------------*/
/*                                          */
/*      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;
}

function month_string(month)
{
  switch (month)
  {
    case 0 :
      return "January";
    case 1 :
      return "Febrary"
    case 2 :
      return "March"
    case 3 :
      return "April"
    case 4 :
      return "May"
    case 5 :
      return "June"
    case 6 :
      return "July"
    case 7 :
      return "August"
    case 8 :
      return "September"
    case 9 :
      return "October"
    case 11 :
      return "November"
    default :
      return "December"
  };
}

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

//
// 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? "one day": diffDays + " days")+ " and " + (diffHours==1? "one hour": diffHours + " hours ago");
}

// 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() + ";";
}


