Date.prototype.toShortDateFormat = function() {
    var _shortDateFormat = document.cookie.indexOf("shortdateformat=1")!=-1 ? 1 : 0;
    var _dateSeparator = document.cookie.indexOf("dateseparator=/")!=-1 ? "/" : "-";
    switch (_shortDateFormat) {
        case 1:
            // mm/dd/yy
            return this._toTwoDecimals(this.getMonth()+1)
			   + _dateSeparator + this._toTwoDecimals(this.getDate())
			   + _dateSeparator + this.getFullYear();
        default:
            // dd/mm/yy
            return this._toTwoDecimals(this.getDate())
			   + _dateSeparator + this._toTwoDecimals(this.getMonth()+1)
			   + _dateSeparator + this.getFullYear();
    }
};

Date.prototype.toTimeOfDay = function() {
    return this._toTwoDecimals(this.getHours()) + ":" + this._toTwoDecimals(this.getMinutes());
};

Date.prototype.toShortFormat = function() {
    return this.toShortDateFormat() + " " + this.toTimeOfDay();
};

Date.prototype._toTwoDecimals = function(n) {
	return (n < 10) ? ("0" + n) : n;
};

Date.prototype.MONTH = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
Date.prototype.DAY = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
Date.prototype.DAY_IN_MILLISECONDS = 24*60*60*1000;
Date.prototype.WEEK_IN_MILLISECONDS = 7*24*60*60*1000;
Date.prototype.RELATIVE_DAY = ["Yesterday", "Today", "Tomorrow"];

Date.prototype.isSameDay = function(date) {
    return this.getYear() == date.getYear() && this.getMonth() == date.getMonth() && this.getDate() == date.getDate();
};

Date.prototype.clearHours = function() {
	this.setMinutes(0);
	this.setSeconds(0);
	this.setMilliseconds(0);
};

Date.prototype.clearDay = function() {
	this.setHours(0);
	this.clearHours();
};

Date.prototype.toLongFormat = function() {
    var _longDateFormat = document.cookie.indexOf("longdateformat=1")!=-1 ? 1 : 0;
    switch (_longDateFormat) {
	    case 1:
	        return this.MONTH[this.getMonth()] + " " + this.getDate() + ", " + this.getFullYear();

        default:
            return this.getDate() + " " + this.MONTH[this.getMonth()] + ", " + this.getFullYear();
	}
};

Date.prototype.toRelativeDateFormat = function(time) {
	if (time!=null) this.setTime(time);
	var day = "";
	var currentDate = new Date();
	currentDate.clearDay();
	var diffDays = Math.floor((this.getTime() - currentDate.getTime())/this.DAY_IN_MILLISECONDS);
	if (-1<=diffDays && diffDays<=1) {
		day = this.RELATIVE_DAY[diffDays+1] + ", ";
	} else
	if (1<diffDays && diffDays<7){
		day = this.DAY[this.getDay()] + ", ";
	} else {
		day = "";
	}
    return day + this.toLongFormat();
};

Date.prototype.toRelativeFormat = function(time) {
	var timeFormatted = this._toTwoDecimals(this.getHours()) + ":" + this._toTwoDecimals(this.getMinutes());
	return this.toRelativeDateFormat(time) + ", " + timeFormatted;
};

function _oneDecimal(number) {
    return Math.round(number*10)/10;
}

function formatFileSize(size) {
    var kilobytes = size/1024;
    return Math.ceil(kilobytes) + " KB";
}

function formatFileSizeAuto(size) {
    var megabytes = size/1048576;
    var kilobytes = size/1024;
    return megabytes>=1 ? _oneDecimal(megabytes)+" MB" : Math.ceil(kilobytes)+" KB";
}