secsPerDay = 86400;
secsPerWeek = 604800;
dayNames = new Array ("Zondag", "Maandag", "Dinsdag", "Woensdag", "Donderdag", "Vrijdag", "Zaterdag");
monthNames = new Array ("Januari", "Februari", "Maart", "April", "Mei", "Juni", "Juli", "Augustus", "September", "Oktober", "November", "December");

//Find next interesting number
function nextInteresting (num) {
    multiplier = 1;
    next = num;
    
    if (-10 <= next && next < 10) {
        next = Math.floor (next + 1);
    } else {
        while ((next <= -100) || (100 <= next)) {
            next = Math.floor (next/10.0);
            multiplier = multiplier * 10;
        }
        next = 5 * Math.floor (next/5.0 + 1);
        next = next * multiplier;
    }
    return (next);
}

// Find next interval or something
function findNextInterval (currNum, intervalLen) {
    elapsedIntervals = currNum/intervalLen;
    nextIntervals = nextInteresting (elapsedIntervals);
    return (nextIntervals);
}

// Get Date entered by the user. Returns a Date object.
function getEnteredDate () {
    month = document.form.month.selectedIndex;
    day = parseInt(document.form.day.value);
    year = parseInt(document.form.year.value);
    theDate = new Date (year, month, day);
    return theDate;
}

// Format the date (change to whatever format I end up using)
function myFormatUS () {
    year = this.getFullYear();
    day = this.getDay(); // Day of week
    date = this.getDate(); // Date of month
     month = this.getMonth();
    return (dayNames[day] + " " + date + " " + monthNames[month] + " " + year);
}

// Find interesting birthday.
function findInterestingBirthday (bornDate) {
    ret = new Object;
    today = new Date();
//    today.setHours (12);
    secondsAgo = (today.getTime() - theDate.getTime())/1000;
    hoursAgo = secondsAgo / 3600;
    daysAgo = hoursAgo/24;
    weeksAgo = daysAgo/7;
    
    nextDays = nextInteresting (daysAgo);
    nextWeeks = nextInteresting (weeksAgo);

    newDays = findNextInterval (secondsAgo, secsPerDay);  // In days
    newDaysSecs = newDays * secsPerDay;
// BL: add 12 hours due to pc-bug:
// PC takes DLS into account, this can cause a variation
// of a couple of hours in the newDaysDate causing a result
// of 23:00 hours on the day before: The wrong date.
// So it's better to calculate the date based on noon-time.
//  newDaysDate = new Date (theDate.getTime() + (newDaysSecs * 1000);
    newDaysDate = new Date (theDate.getTime() + ((newDaysSecs + (12 * 3600))) * 1000);
    
    ret.daysText = "You will be " + newDays + " days old on " + newDaysDate.myFormat();
    ret.daysTextNL = "" + newDays + " dagen oud bent op " + newDaysDate.myFormat();
    
    
    newWeeks = findNextInterval (secondsAgo, secsPerWeek);  // In weeks
    newWeeksSecs = newWeeks * secsPerWeek;
    newWeeksDate = new Date (theDate.getTime() + newWeeksSecs * 1000);
    
    ret.weeksText = "You will be " + newWeeks + " weeks old on " + newWeeksDate.myFormat();
    ret.weeksTextNL = "" + newWeeks + " weken oud bent op " + newWeeksDate.myFormat();
    return (ret);
}

// Update the birthdays
function makeDate() {
    theDate = getEnteredDate();
    foo = findInterestingBirthday (theDate);
    document.form.the_weeks.value = foo.weeksTextNL;
    document.form.the_days.value = foo.daysTextNL;
}

Date.prototype.myFormat = myFormatUS;

makeDate();