/**
 * Klassendefinition für Geburtstagsfunktion, v1.1, 2005-10-08
 *
 * http://www.free.pages.at/casandra/jsbday/index.htm
 */

/**
 * class User
 */
function User (name, birthDate) {
	this.name = name;
	this.birthDate = birthDate;
	var bDayChild = null;

	/**
	 * Vergleicht Geburtstag mit heutigem Tag.
	 *   -1 : hatte dieses Jahr schon Geburtstag
	 *    0 : hat heute Geburtstag
	 *    1 : hatte dieses Jahr noch nicht Geburtstag
	 */
	this.isBdayChild = function () {
		// neu berechnen, falls Cache nicht gesetzt
		if (bDayChild == null) {
			var t = new Date();
			// heutiger Tag und Monat, aber Geburtsjahr!
			var today = new Date(this.birthDate.getFullYear(), t.getMonth(), t.getDate());

			if (today.getTime() > this.birthDate.getTime())
				bDayChild = -1;
			else if (today.getTime() < this.birthDate.getTime())
				bDayChild = 1;
			else
				bDayChild = 0;
		}
		return bDayChild;
	}

	/**
	 * Liefert das Alter unter Berücksichtigung des Geburtstages.
	 */
	this.getAge = function () {
		var today = new Date();
		var year = today.getFullYear();
		var bYear = this.birthDate.getFullYear();
		return (this.isBdayChild() <= 0) ? year - bYear : year - bYear - 1;
	}
}

/**
 * class UserList
 */
function UserList () {
	this.users = new Array();

	this.addUser = function (name, bDay, bMon, bYear) {
		this.users[this.users.length] = new User(name, new Date(bYear, bMon-1, bDay));
	}

	/**
	 * Liefert eine kommaseparierte Liste aller Geburtstagskinder.
	 * Wenn showAge TRUE ist, wird in Klammern jeweils das Alter
	 * des Geburtstagskindes angehängt.
	 */
	this.getBdayChildren = function (showAge) {
		var list = '';
		for (var i = 0; i < this.users.length; i++) {
			if (this.users[i].isBdayChild() == 0) {
				if (list != '') list += ', ';
				list += this.users[i].name;
				if (showAge) list += ' (' + this.users[i].getAge() + ')';
			}
		}
		return (list == '') ? 'Keine Geburtstagskinder.' : list;
	}
}
