/***********************************************************************************************************************
 File		: DateCalc.German.uni.js
 File Type	: Javascript (Internet Explorer > 5.5)

 Copyright 	: xtendo technologies Kurt Schwedes, 76185 Karlsruhe, Germany
			  All rights reserved
.......................................................................................................................
 Created		: 11.01.05 (29.03.05 as DateCalc.js, last version: 04.01.06)
 Author			: Kurt Schwedes, xtendo technologies
 Last Change	: 16.01.06
.......................................................................................................................

 Extention for standard Date-object
 
.......................................................................................................................
 Develeped for 	: xtendo technologies Kurt Schwedes, 76185 Karlsruhe, Germany
 Contact		: kurt.schwedes@xtendo.de
 Documentation 	: ---
***********************************************************************************************************************/
// DEFINITIONS

/*----------------------------------------------------------------------------------------------------------------------
 CLASS CONSTANTS/DEFINITIONS
----------------------------------------------------------------------------------------------------------------------*/

Date.names = {
	months: 		["Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August",
					 "September", "Oktober", "November", "Dezember"],
	monthsShort: 	["Jan", "Feb", "Mar", "Apr", "Mai", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dez"],
	days: 			["Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag"],
	daysShort: 		["So", "Mo", "Di", "Mi", "Do", "Fr", "Sa"],
	seasons: 		["Frühling", "Sommer", "Herbst", "Winter"]
};

Date.prototype.firstDayOfWeek	= 1;	// Montag
Date.prototype.formats = {date: '%d.%m.%Y', time: '%k:%I', datetime: '%d.%m.%Y %k:%I:%S'};

/*
formats Specifier Description
---------------------------------------------------------------------------------------------------
%a Abbreviated weekday name (Sun..Sat)  
%b Abbreviated month name (Jan..Dec)  
%C Month, numeric (01..12)  
%c Month, numeric (1..12)  
%D Day of the month with English suffix (0th, 1st, 2nd, 3rd, ...)  
%d Day of the month, numeric (00..31)  
%e Day of the month, numeric (0..31)  
%H Hour (00..23)  
%h Hour (01..12)  
%I Minutes, numeric (00..59)  
%i Minutes, numeric (0..59)  
%j Day of year (001..366)  
%k Hour (0..23)  
%l Hour (1..12)  
%M Month name (January..December)  
%m Month, numeric (01..12)  
%p AM or PM  
%r Time, 12-hour (hh:mm:ss followed by AM or PM)  
%S Seconds (00..59)  
%s Seconds (0..59)  
%T Time, 24-hour (hh:mm:ss)  
%U Week (00..53), where Sunday is the first day of the week  
%u Week (0..53), where Monday is the first day of the week  
%V Week (01..53), where Sunday is the first day of the week; used with %X  
%v Week (1..53), where Monday is the first day of the week; used with %x  
%W Weekday name (Sunday..Saturday)  
%w Day of the week (0=Sunday..6=Saturday)  
%X Year for the week where Sunday is the first day of the week, numeric, four digits; used with %V  
%x Year for the week, where Monday is the first day of the week, numeric, four digits; used with %v  
%Y Year, numeric, four digits  
%y Year, numeric, two digits  
%Z Year for the week, where Monday is the first day of the week, numeric, two digits; used with %v 
*/
Date.formatter = {
	'%a' : 'getDayOfWeekShortName',  
	'%b' : 'getMonthShortName',
	'%C' : 'getMonthNo',
	'%c' : 'getMonth',  
	//'%D' : 'Day of the month with English suffix (0th, 1st, 2nd, 3rd, ...)  
	'%d' : 'getDayNo', 
	'%e' : 'getDate',  
	'%H' : 'getHoursNo', 
	'%h' : 'get12HoursNo',  
	'%I' : 'getMinutesNo',   
	'%i' : 'getMinutes',  
	'%j' : 'getDayOfYear', 
	'%k' : 'getHours',  
	'%l' : 'get12Hours', 
	'%M' : 'getMonthName',  
	'%m' : 'getMonthNo',  
	'%p' : 'getAMPM',  
	'%r' : 'get12Time',  
	'%S' : 'getSecondsNo',  
	'%s' : 'getSeconds', 
	'%T' : 'get24Time',   
	//'%U' : 'Week (00..53), where Sunday is the first day of the week  
	'%u' : 'getCalendarWeekNo', 
	//'%V' : 'Week (01..53), where Sunday is the first day of the week; used with %X  
	'%v' : 'getCalendarWeek',  
	'%W' : 'getDayOfWeekName', 
	'%w' : 'getDayOfWeek',  
	//'%X' : 'Year for the week where Sunday is the first day of the week, numeric, four digits; used with %V  
	'%x' : 'getCalendarWeek', 
	'%Y' : 'getFullYear',  
	'%y' : 'getYear',
	'%Z' : 'getCalendarWeek'
}

/*----------------------------------------------------------------------------------------------------------------------
 PUBLIC OBJECT METHODS
----------------------------------------------------------------------------------------------------------------------*/

Date.prototype.getSerial = function ()
{
	with (this)
		return getFullYear().toString().concat(getMonthNo()).concat(getDayNo());
}

Date.prototype.getWeekSerial = function ()
{
	with (this){
		var w = getCalendarWeek();
		if (w.week < 10) w.week = '0' + w.week;
		return w.year.toString().concat(w.week);
	}
}

Date.prototype.getYear = function ()
{
	var Y = this.getFullYear();
	Y = Y < 1900 ? Y : Y % 1000;
	return Y < 10 ? '0' + Y : Y;
}

Date.prototype.getLeapYear = function ()
{
	var Y = this.getFullYear();
	return (!(Y % 4) && (Y % 100)) || !(Y % 400);
}

Date.prototype.getDaysPerMonth = function ()
{
	return [31, this.getLeapYear() ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
}

Date.prototype.getDayOfWeek	= function ()
{
	return this.getDay();
}

Date.prototype.getDayOfWeekName = function ()
{
	return Date.names.days[this.getDay()];
}

Date.prototype.getDayOfWeekShortName = function ()
{
	return Date.names.daysShort[this.getDay()];
}

Date.prototype.getLastDayOfMonth = function (v)
{
	return	this.getDaysPerMonth()[this.getMonthNo(v) - 1];
}

Date.prototype.getMonthNo = function (v)
{
	var m = 0;
	if (v) {
		var z = v * 1;
		if (z > 0) return z;
		else {
			v = v.toLowerCase();
			var n = Date.names;
			if (! n.monthNames){
				n.monthNames = {};
				n.monthShortNames = {};
				for (m = 0; m < 12; m++) {
					n.monthNames[n.months[m].toLowerCase()] = m + 1;
					n.monthShortNames[n.monthsShort[m].toLowerCase()] = m + 1;
				}
			}
			m = n.monthNames[v] || n.monthShortNames[v];
		}
	}
	else
		m = this.getMonth() + 1;
	
	return m < 10 ? '0' + m : m;
}

Date.prototype.getMonthName = function (v)
{
	return Date.names.months[this.getMonthNo(v) - 1];
}

Date.prototype.getMonthShortName = function (v)
{
	return Date.names.monthsShort[this.getMonthNo(v) - 1];
}

Date.prototype.getDayNo = function ()
{
	var m = this.getDate();
	return m < 10 ? '0' + m : m;
}

Date.prototype.getDayOfYear	= function ()
{
	var	m	= this.getDaysPerMonth();
	var	sum	= 0;
	var month = this.getMonth();
	
	for(var i = 0; i < month; i++)
		sum	+= m[i];

	return sum + this.getDate();
}

Date.prototype.getFirstJanDay = function (year)
{
   if (! year) year = this.getFullYear();
   return new Date(year, 0, 1).getDay();
}

Date.prototype.getCalendarWeek = function(d)
{
	if (! d) d = this;
	var thu = d.getThursdayOfWeek();
	var cwY = thu.getFullYear();
	var thuCw1 = this.getThursdayOfWeek(new Date(cwY, 0, 4));
	return {
		week: Math.floor(1.5 + (thu.getTime() - thuCw1.getTime()) / 86400000 / 7),
		year: thu.getYear(),
		fullYear: cwY
	}; 
}

Date.prototype.getThursdayOfWeek = function(d)
{
	if (! d) d = this;
	var thu = new Date();
	thu.setTime(d.getTime() + (3-((d.getDay() + 6) % 7)) * 86400000);
	return thu;
}

Date.prototype.getCalendarWeekNo = function()
{
	var w = this.getCalendarWeek();
	return w.fullYear != this.getFullYear() ? 0 : w.week;
}

Date.prototype.getSeason = function()
{
	var	m	= this.getMonth();
	var	d	= this.getDate();
	var	f	= this.getLeapYear() ? 0 : 1;
	if((m < 2) || ((m == 2) && (d < (20 + f))))
		return 3;
	if((m < 5) || ((m == 5) && (d < (20 + f))))
		return 0;
	if((m < 8) || ((m == 8) && (d < (22 + f))))
		return 1;
	if((m < 11) || ((m == 11) && (d < (21 + f))))
		return 2;
	return 3;
}

Date.prototype.getSeasonName = function()
{
	return Date.names.seasons[this.getSeason()];
}

Date.prototype.get12Hours = function ()
{
	var t = this.getHours();
	if (t == 0) t = 12;
	else if (t > 12) t -= 12;
	return t;
}

Date.prototype.get12HoursNo = function ()
{
	var t = this.get12Hours();
	return t < 10 ? '0' + t : t;
}

Date.prototype.getHoursNo = function ()
{
	var t = this.getHours();
	return t < 10 ? '0' + t : t;
}

Date.prototype.getMinutesNo = function ()
{
	var t = this.getMinutes();
	return t < 10 ? '0' + t : t;
}

Date.prototype.getSecondsNo = function ()
{
	var t = this.getSeconds();
	return t < 10 ? '0' + t : t;
}

Date.prototype.getAMPM = function ()
{
	var t = this.getHours();
	return t < 12 ? 'AM' : 'PM';
}

Date.prototype.get12Time = function ()
{
	with (this)
		return get12HoursNo() + ':' + getMinutesNo() + ':' + getSecondsNo() + ' ' + getAMPM()
}

Date.prototype.get24Time = function ()
{
	with (this)
		return getHoursNo() + ':' + getMinutesNo() + ':' + getSecondsNo();
}

Date.prototype.byFormat = function (f)
{
	if (f){
		var obj = this;
		var xF = Date.formatter;
		// want calendar week data?
		if (f.match(/%[vxZ]/)){
			var cw = obj.getCalendarWeek();
			f = f.replace(/(%\w)/g,
				function (s, m)
				{
					if (m && xF[m]) {	
						if (m == '%v' || m == '%x' || m == '%Z')
							return m == '%v' ? cw.week : m == '%x' ? cw.fullYear : cw.year;
						return m && xF[m] ? obj[xF[m]]() : ''
					}
					return '';
				}
			);
		}
		else f = f.replace(/(%\w)/g, function (s, m) { return m && xF[m] ? obj[xF[m]]() : '' });
	}
	return f || '';
}

/*----------------------------------------------------------------------------------------------------------------------
 MAIN
----------------------------------------------------------------------------------------------------------------------*/

window.TODAY = new Date();
/*
_test();

function _test ()
{
	//alertProperties(Date.names);
	var d = new Date();
	//var d = new Date("january 02, 2006 22:11:09");
	//alertProperties(d.formats);
	//alert(d.byFormat(d.formats.datetime));
	//alert(d.byFormat('%v/%Z'));
	alert('\ngetYear: ____________________ ' + d.getYear() +
		  '\ngetFullYear: ________________ ' + d.getFullYear() + 
		  '\ngetLeapYear: ________________ ' + d.getLeapYear() +
		  '\ngetDaysPerMonth: ____________ ' + d.getDaysPerMonth() +
		  '\ngetDayOfWeek: _______________ ' + d.getDayOfWeek() +
		  '\ngetDayOfWeekName: ___________ ' + d.getDayOfWeekName() +
		  '\ngetDayOfWeekShortName: ______ ' + d.getDayOfWeekShortName() +
		  '\ngetLastDayOfMonth: __________ ' + d.getLastDayOfMonth() +
		  '\ngetLastDayOfMonth("FEBRUAR"): ' + d.getLastDayOfMonth('FEBRUAR') +
		  '\ngetMonthNo: _________________ ' + d.getMonthNo() +
		  '\ngetMonthName: _______________ ' + d.getMonthName() +
		  '\ngetMonthShortName: __________ ' + d.getMonthShortName() +
		  '\ngetMonthNo("März"): _________ ' + d.getMonthNo('März') +
		  '\ngetMonthName("März"): _______ ' + d.getMonthName('März') +
		  '\ngetMonthShortName("März"): __ ' + d.getMonthShortName('März') +
		  '\ngetDayNo: ___________________ ' + d.getDayNo() +
		  '\ngetDayOfYear: _______________ ' + d.getDayOfYear() +
		  '\ngetCalendarWeek().week:______ ' + d.getCalendarWeek().week +
		  '\ngetCalendarWeek().year: _____ ' + d.getCalendarWeek().year +
		  '\ngetCalendarWeek().fullYear: _ ' + d.getCalendarWeek().fullYear +
		  '\ngetCalendarWeekNo ___________ ' + d.getCalendarWeekNo() +
		  '\ngetSeason: __________________ ' + d.getSeason() +
		  '\ngetSeasonName: ______________ ' + d.getSeasonName() +
		  '\ngetTime: ____________________ ' + d.getTime()+
		  '\nget12Time: __________________ ' + d.get12Time() +
		  '\nget24Time: __________________ ' + d.get24Time() +
		  '\ngetHours: ___________________ ' + d.getHours() +
		  '\ngetMinutes: _________________ ' + d.getMinutes() +
		  '\ngetSeconds: _________________ ' + d.getSeconds() +
		  '\nget12HoursNo: _______________ ' + d.get12HoursNo() +
		  '\ngetAMPM: ____________________ ' + d.getAMPM() +
		  '\ngetHoursNo: _________________ ' + d.getHoursNo() +
		  '\ngetMinutesNo: _______________ ' + d.getMinutesNo() +
		  '\ngetSecondsNo: _______________ ' + d.getSecondsNo() +
		  '\nbyFormat("%v/%Z"): __________ ' + d.byFormat('%v/%Z')
		);
	
	var s = '';
	for (var p in Date.formatter)
		s += p + ':  ' + d[Date.formatter[p]]() + "\n";
	alert('Available formats:\n\n' +  s);
}
*/
