/**
 * Retrieve the full name of this Date object's month, in English.
 */
Date.getMonthName = function(month) {
	return ['January', 'February', 'March', 'April',
		'May', 'June', 'July', 'August', 'September',
		'October', 'November', 'December'][month];
}
Date.prototype.getMonthName = function() {
	return Date.getMonthName(this.getMonth());
}

/**
 * Retrieve the full name of this Date object's day of the week, in English.
 */
Date.getDayName = function(day) {
	return ['Sunday', 'Monday', 'Tuesday', 'Wednesday',
		'Thursday', 'Friday', 'Saturday'][day];
}
Date.prototype.getDayName = function() {
	return Date.getDayName(this.getDay());
}

/**
 * Retrieve the ordinal suffix (st, nd, rd or th) of this Date object's day of the month.
 */
Date.prototype.getDateOrdinalSuffix = function() {
	var date = this.getDate();
	if (date == 11 || date == 12 || date == 13) return 'th';
	switch (date % 10) {
		case 1: return 'st';
		case 2: return 'nd';
		case 3: return 'rd';
		default: return 'th';
	}
}

/**
 * Calculates the number of days in the specified month. Note that month is 0 based.
 */
Date.daysInMonth = function(year, month) {
	var d = new Date(year, month + 1, 1);
	d.setTime(d.getTime() - 1)
	return d.getDate();
}

/**
 * Formats the Date object into a human-readable form. Similar to:
 * <http://java.sun.com/j2se/1.4.2/docs/api/java/text/SimpleDateFormat.html>
 */
Date.prototype.format = function () {

	var pattern = arguments[0] || 'd MMMM yyyy';

	var values = {
		'y':    this.getFullYear() + '',
		'yyyy': this.getFullYear() + '',
		'yy':   (this.getFullYear() + '').substring(2,4),
		'M':    (this.getMonth() + 1) + '',
		'MM':   (this.getMonth() < 9 ? '0' + (this.getMonth() + 1) : (this.getMonth() + 1) + ''),
		'MMM':  this.getMonthName().substring(0, 3),
		'MMMM': this.getMonthName(),
		'd':    this.getDate() + '',
		'dd':   (this.getDate() < 10 ? '0' + this.getDate() : this.getDate() + ''),
		'EEE':  this.getDayName().substring(0, 3),
		'EEEE': this.getDayName(),
		'S':    this.getDateOrdinalSuffix()
	}

	var result = '';
	var index = 0;
	while (index < pattern.length) {
		var c = pattern.charAt(index);
		var token = '';
		while ((pattern.charAt(index) == c) && (index < pattern.length)) {
			token += pattern.charAt(index++);
		}
		if (values[token] != null) {
			result = result + values[token];
		} else {
			result = result + token;
		}
	}
	
	return result;

}