// ***********************************************************
// Calculate remaining days for birthday :-)

var birth_year = 1977;
var the_day = "October 20";
var the_year = birth_year;
var birthday = new Date(the_day + ", " + the_year + " 00:00:00");

// Variables to display today's date.
var now = new Date();
var yr = now.getYear();
var mName = now.getMonth();
var dName = now.getDay();
var dayNr = ((now.getDate()<10) ? "0" : "")+ now.getDate();

//  Calculate difference  between dates - increment if  necessary.
function calculate()
{
  var birthday = new Date(the_day + ", " + the_year + " 00:00:00");
  var b_day = ((birthday.getDate()<10) ? "0" : "")+ birthday.getDate();
  msDifference = birthday.getTime() - now.getTime();

   // Kick out of function if today is the day.
  if( (birthday.getMonth()==now.getMonth()) && (b_day==dayNr) )
     {
     msDifference = 0;
     return;
     }

  // Increment designated date by 1 year if necessary.
  if( msDifference <= 0 )
    {
      the_year++;
      calculate();
    }
  else
    return;
}

calculate();

// Calculate number of days left.
msPerDay = 24 * 60 * 60 * 1000 ; // Number of milliseconds per day
daysLeft = msDifference / msPerDay;
daysLeft = Math.floor(daysLeft);

// Array for today's date - day.
Day= new Array(7)
Day[0]="Sunday";
Day[1]="Monday";
Day[2]="Tuesday";
Day[3]="Wednesday";
Day[4]="Thursday";
Day[5]="Friday";
Day[6]="Saturday";

// Array for today's date - month.
Month= new Array(12)
Month[0]="January";
Month[1]="February";
Month[2]="March";
Month[3]="April";
Month[4]="May";
Month[5]="June";
Month[6]="July";
Month[7]="August";
Month[8]="September";
Month[9]="October";
Month[10]="November";
Month[11]="December";

// String to display current date.
if(now.getYear() <= 99)   var todaysDate =(" " + Day[dName] + " " + Month[mName] + " " + dayNr + ", " + "19" + yr + "<BR>");
else
   var todaysDate =(" " + Day[dName] + " " + Month[mName] + " " + dayNr + ", " + yr + "<BR>");
  
function Mesgbox()
{
  var age = (the_year - birth_year);
  var postfix = "th";

  if((age > 20) || (age <= 3))
  {
    age = age + ""; // convert to string

    if(age.charAt(age.length - 1) == '1')
    {
      postfix = "st";
    }
    if(age.charAt(age.length - 1) == '2')
    {
      postfix = "nd";
    }
    if(age.charAt(age.length - 1) == '3')
    {
      postfix = "rd";
    }
  }

  daysLeft++; // HACK!

  if(msDifference >= 1)
  {
    if(daysLeft == 1)
    {
      Text="There is just 1 day left until my " + age + postfix + " B-day on " + the_day + ", " + the_year + " !";
    }
    else
    {
      Text="There are " + daysLeft + " days left until my " + age + postfix + " B-day on " + the_day + ", " + the_year + " !";
    }
  }
  else
  {
    Text="Happy Birthday Stefan!!!";
  }

  return(Text);
}
