// definice novych verejnych metod objektu Date
Date.prototype.format = dtFormat;
Date.prototype.formatDate = dtFormatDate;
Date.prototype.formatTime = dtFormatTime;

Date.prototype.add = dtAdd;

Date.prototype.getDateValue = dtGetDateValue;
Date.prototype.getTimeValue = dtGetTimeValue;

Date.prototype.setNewDate = dtSetNewDate;
Date.prototype.setD = dtSetDay;
Date.prototype.setM = dtSetMonth;
Date.prototype.setY = dtSetYear;

Date.prototype.setNewTime = dtSetNewTime;
Date.prototype.setH = dtSetHour;
Date.prototype.setMM = dtSetMinute;
Date.prototype.setS = dtSetSecond;
Date.prototype.setMS = dtSetMilliSecond;

Date.prototype.getH = dtGetHour;
Date.prototype.getMM = dtGetMinute;
Date.prototype.getS = dtGetSecond;
Date.prototype.getMS = dtGetMilliSecond;

Date.prototype.getMonthName = dtGetMonthName;
Date.prototype.getD = dtGetDay;
Date.prototype.getM = dtGetMonth;
Date.prototype.getY = dtGetYear;

Date.prototype.getFirstDayOfWeek = dtGetFirstDayOfWeek;
Date.prototype.getLastDayOfWeek = dtGetLastDayOfWeek;
Date.prototype.getWeekDay = dtGetWeekDay;
Date.prototype.getDayName = dtGetDayName;

Date.prototype.clone = dtClone;
Date.prototype.clear = dtClear;

Date.prototype.clearDate = dtClearDate;
Date.prototype.clearTime = dtClearTime;

// definice privatnich metod objektu Date
Date.prototype.getDayExtension = dtGetDayExt;

/* ================================================================== */

/* dtGetTimeValue(); metoda objektu Date.getTimeValue()
metoda vrati pocet ms odpovidajici casove slozce data
*/
function dtGetTimeValue() {
  var vDate = this.clone();
  vDate.clearDate();
  return vDate.valueOf();
}

/* dtGetDateValue(); metoda objektu Date.getDateValue()
metoda vrati pocet ms odpovidajici datumove slozce data
*/
function dtGetDateValue() {
  var vDate = this.clone();
  vDate.clearTime();
  return vDate.valueOf();
}

/* dtClearTime(); metoda objektu Date.clearTime()
metoda vynuluje cas
*/
function dtClearTime() {
  this.setH(0);
  this.setMM(0);
  this.setS(0);
  this.setMS(0)
}

/* dtClearDate(); metoda objektu Date.clearDate()
metoda vynuluje datum, nastavi na 1.1.1970
*/
function dtClearDate() {
  this.setD(1);
  this.setM(1);
  this.setY(1970);
}

/* dtClone(); metoda objektu Date.clone()
metoda vrati kopii sama sebe, novou referenci
*/
function dtClone() {
  return new Date(this.valueOf());
}

/* dtClear(); metoda objektu Date.clear()
metoda nastavi datum na 0 ms
*/
function dtClear() {
  var ms = new Number(this.valueOf());
  this.add("ms", -ms);
}

/* dtGetLastDayOfWeek(lng); metoda objektu Date.getLastDayOfWeek()
metoida wrati datum posledniho dne tydne v zavislosti na lng
parametry:
lng - jazyk (cs/en)(nepovinny = cs)
*/
function dtGetLastDayOfWeek(lng) {
	var vDate = this.clone();
	if ( !lng ) lng = "cs";

	vDate.add("d", 7-this.getWeekDay(lng));
	return vDate
}

/* dtGetFirstDayOfWeek(lng); metoda objektu Date.getFirstDayOfWeek()
metoida wrati datum prvniho dne tydne v zavislosti na lng
parametry:
lng - jazyk (cs/en)(nepovinny = cs)
*/
function dtGetFirstDayOfWeek(lng) {
	var vDate = this.clone();
	if ( !lng ) lng = "cs";

	//vDate.add("d", -this.getWeekDay(lng)+1);
	vDate.setDate(vDate.getDate() + 1 - vDate.getWeekDay(lng) );
	return vDate
}

/* dtGetWeekDay(lng); metoda objektu Date.getWeekDay()
metoda vraci cislo dne v tydnu v zavislosti na lng
parametry:
 lng - jazyk (cs/en)(nepovinny = cs)(en = su-so)
*/
function dtGetWeekDay(lng) {
	if ( !lng ) lng = "cs";
  var i;
	
	if ( lng == "en" ) i = this.getDay() + 1;
	else i = ( this.getDay() == 0 ? 7 : this.getDay() );

  return i;
}

/* dtAdd(number, interval); metoda objektu Date.add()
metoda pricte k datumu pocet jednotek udanych v parametru interval
parametry:
number - pocet
interval
 Y - rok
 M - mesic
 D - den
 H - hodina
 MM - minuta
 S - vterina
 MS - mili vterina
 W - tyden
*/
function dtAdd(interval, vNumber) {
	if ( typeof(interval) == "undefined" ) { alert("Neplatý parametr Interval při volání metody Add objektu Date.", "e"); return; }
	if ( typeof(vNumber) == "undefined" ) { alert("Neplatý parametr vNumber při volání metody Add objektu Date.", "e"); return;	}
	
	switch ( interval.toUpperCase() ) {
		case "Y": this.setY(this.getY() + vNnumber); break;
		case "M": this.setM(this.getM() + vNumber); break;
		case "D": this.setD(this.getD() + vNumber); break;
		case "H": this.setH(this.getH() + vNumber); break;
		case "MM": this.setMM(this.getMM() + vNumber); break;
		case "S": this.setS(this.getS() + vNumber); break;
		case "MS": this.setMS(this.getMS() + vNumber); break;
		case "W": this.setD(this.getD() + 7*vNumber); break;
	}
}

/* dtFormat(dformat, lng, tformat, ms); metoda objektu Date.format()
metoda zformatuje datum a cas dle parametru
vystupem je retezec intpretujici cele datum
parametry:
dformat - format datumu (1-x)(nepovinne=1)
lng - jazykova verze(nepovinne=cs)
tformat - format casu (1-x)(nepovinne=1)
ms - ukazovat milivteriny (true/false)(nepovinne=true)
*/
function dtFormat(dformat, lng, tformat, ms) {
	return this.formatDate(dformat, lng) + " " + this.formatTime(tformat, ms);
}

/* dtSetNewTime(hour, minute, second, msesond); metoda objektu Date.setNewTime()
metoda nastavi cas dle parametru
parametry:
hour - hodina (0-24)
minute - minuta (0-59)
second - vretina (0-59)
msecond - milivterina (0-999)
*/
function dtSetNewTime(hour, minute, second, msecond) {
	this.setH(hour);
	this.setMM(minute);
	this.setS(second);
	this.setMS(msecond);
}

/* dtSetMilliSecond(msecond); metoda objektu Date.setMS()
metoda nastavi milivteriny dle parametru
parametry:
msecond - milivterina (0-999)
*/
function dtSetMilliSecond(msecond) {
	if ( msecond ) this.setMilliseconds(msecond);
}

/* dtSetSecond(second); metoda objektu Date.setS()
metoda nastavi vteriny dle parametru
parametry:
second - vterina (0-59)
*/
function dtSetSecond(second) {
	if ( second ) this.setSeconds(second);
}

/* dtSetMinute(minute); metoda objektu Date.setMM()
metoda nastavi minuty dle parametru
parametry:
minute - minuta (0-59)
*/
function dtSetMinute(minute) {
	if ( minute ) this.setMinutes(minute);
}

/* dtSetHour(hour); metoda objektu Date.setH()
metoda nastavi hodinu dle parametru
parametry:
hour - hodina (0-23)
*/
function dtSetHour(hour) {
	if ( hour ) this.setHours(hour);
}

/* dtFormatTime(format, ms); metoda objektu Date.formatTime()
metoda zformatuje cas dle formatu
vystupem je retezec intpretujici cas
parametry:
format (cislo) -	1 = h:mm:ss ; 7:05:02
(nepovinny = 1)		2 = hh:mm:ss ; 07:05:02
									3 = h:m:s ; 7:5:2
									4 = h:mm ; 7:05
									5 = hh:mm ; 07:05
									6 = h:m ; 7:5
									7 = mm:ss ; 05:02
									8 = m:ss ; 5:02
									9 = m:s ; 5:2
ms - uvadet udaj o ms (nepovinny = true)
*/
function dtFormatTime(format, ms) {
	var str = "";
	
	// nastaveni vychozich hodnot v pripade ze nejsou uvedeny
	if ( !format ) format = 1;
	if ( !ms ) ms = true;
	
	switch (format) {
		case 1: str = this.getH() + ":" + this.getMM().formatDigits(2) + ":" + this.getS().formatDigits(2); break;
		case 2: str = this.getH().formatDigits(2) + ":" + this.getMM().formatDigits(2) + ":" + this.getS().formatDigits(2); break;
		case 3: str = this.getH() + ":" + this.getMM() + ":" + this.getS(); break;
		case 4: str = this.getH() + ":" + this.getMM().formatDigits(2); break;
		case 5: str = this.getH().formatDigits(2) + ":" + this.getMM().formatDigits(2); break;
		case 6: str = this.getH() + ":" + this.getMM(); break;
		case 7: str = this.getMM().formatDigits(2) + ":" + this.getS().formatDigits(2); break;
		case 8: str = this.getMM() + ":" + this.getS().formatDigits(2); break;
		case 9: str = this.getMM() + ":" + this.getS(); break;
	}
	
	if ( ms ) str += "." + this.getMS().formatDigits(3);
	return str;
}

/* dtGetMilliSecond(); metoda objektu Date.getMS()
metoda vrati milisekundu
*/
function dtGetMilliSecond() {
	return this.getMilliseconds();
}

/* dtGetSecond(); metoda objektu Date.getS()
metoda vrati sekundu
*/
function dtGetSecond() {
	return this.getSeconds();
}

/* dtGetMinute(); metoda objektu Date.getMM()
metoda vrati minutu
*/
function dtGetMinute() {
	return this.getMinutes();
}

/* dtGetHour(); metoda objektu Date.getH()
metoda vrati hodinu lokalniho casu
*/
function dtGetHour() {
	return this.getHours();
}

/* dtSetNewDate(year, month, day); metoda objektu objDate.setNewDate()
metoda nastavi datum dle parametru
parametry:
day - den (1-31)
month - mesic (1-12)
year - rok
*/
function dtSetNewDate(year, month, day) {
	this.setD(day);
	this.setM(month);
	this.setY(year);
}

/* dtSetYear(year); metoda objektu objDate.setY()
metoda nastavi rok dle parametru
parametry:
year - rok
*/
function dtSetYear(year) {
	if ( year ) this.setFullYear(year);
}

/* dtSetMonth(month); metoda objektu objDate.setM()
metoda nastavi mesic dle parametru
parametry:
month - mesic (1-12)
*/
function dtSetMonth(month) {
	if ( month ) this.setMonth(month-1);
}

/* dtSetDay(day); metoda objektu objDate.setD()
metoda nastavi den dle parametru
parametry:
day - den (1-31)
*/
function dtSetDay(day) {
	if ( day ) this.setDate(day);
}

/* dtFormatDate(format, lng); metoda objektu objDate.formatDate()
metoda zformatuje datum dle formatu
vystupem je retezec intpretujici datum
parametry:
format (cislo) -	1 = d. m. yyyy ; 9. 3. 2004
(nepovinny = 1)		2 = d. mmm yyyy ; 9. bžezen 2004
									3 = d. mmmm yyyy ; 9. bžezna 2004
									4 = yyyy/m/d ; 2004/3/9
									5 = yy/mm/dm ; 04/03/09
									6 = mmm yyyy ; Březen 2004
									7 = mmm d, yyyy ; Sep 18th, 2004 = en 
									8 = mmmm d, yyyy ; September 8th, 2004 = en 
lng - jazyk (cs/en/...)(nepovinny = cs)
*/
function dtFormatDate(format, lng) {
	var str = "";
	
	// nastaveni vychozich hodnot v pripade ze nejsou uvedeny
	if ( !format ) format = 1;
	if ( !lng ) lng = "cs";
	
	switch (format) {
		case 1: str = this.getD() + ". " + this.getM() + ". " + this.getY(); break;
		case 2: str = this.getD() + ". " + this.getMonthName(lng).toLowerCase() + " " + this.getY(); break;
		case 3: str = this.getD() + ". " + this.getMonthName(lng, true).toLowerCase() + " " + this.getY(); break;
		case 4: str = this.getY() + "/" + this.getM().formatDigits(2) + "/" + this.getD().formatDigits(2); break;
		case 5: str = this.getY().formatDigits(2) + "/" + this.getM().formatDigits(2) + "/" + this.getD().formatDigits(2); break;
		case 6: str = this.getMonthName() + " " + this.getY(); break;
		case 7: str = this.getMonthName("en").left(3) + " " + this.getD() + this.getDayExtension() + ", " + this.getY(); break;
		case 8: str = this.getMonthName("en") + " " + this.getD() + this.getDayExtension() + ", " + this.getY(); break;
	}
	return str;
}

/* dtGetYear(); metoda objektu objDate.getY()
metoda vrati rok
*/
function dtGetYear() {
	return this.getFullYear();
}

/* dtGetMonth(); metoda objektu objDate.getM()
metoda vrati cislo mesice (1-12)
*/
function dtGetMonth() {
	return this.getMonth() + 1;
}

/* dtGetDay(); metoda objektu objDate.getD()
metoda vrati cislo dne v mesici (1-31)
*/
function dtGetDay() {
	return this.getDate();
}

/* dtGetDayName(lng); metoda objektu Date.getDayName()
metoda vrati nazev dne v tydnu dle jazyka
parametry:
lng - jazyk (cs/en/...)(nepovinny -> cs)
*/
function dtGetDayName(lng) {
	var str = "";
	
	if ( !lng ) lng = "cs";
	
	switch ( lng ) {
		case "cs":
			switch ( this.getWeekDay(lng) ) {
				case 1: return "pondělí";
				case 2: return "úterý";
				case 3: return "středa";
				case 4: return "čtvrtek";
				case 5: return "pátek";
				case 6: return "sobota";
				case 7: return "neděle";
			}
			break;
		case "en":
			switch ( this.getWeekDay(lng) ) {
				case 1: return "Sunday";
				case 2: return "Monday";
				case 3: return "Tuesday";
				case 4: return "Wednesday";
				case 5: return "Thursday";
				case 6: return "Friday";
				case 7: return "Saturday";
			}
			break;
	}
}

/* dtGetMonthName(lng, gCase); metoda objektu objDate.getMonthName()
metoda vrati nazev mesice dle jazyka a gramatickeho padu
parametry:
lng - jazyk (cs/en/...)(nepovinny -> cs)
gCase - gram. pad (true/false)(nepovinny = false)
*/
function dtGetMonthName(lng, gCase) {
	var i = this.getM();
	var x = "";
	
	// nastaveni vychozich hodnot v pripade ze nejsou uvedeny
	if ( !lng ) lng = "cs";
	if ( !gCase ) gCase = false;
	
	switch (lng) {
		case "cs":
			switch (i) {
				// gCase ? "" : "" = rozlišení dle pozadavku na sklonovaný tvar
				case 1: x = gCase ? "Ledna" : "Leden"; break;
				case 2: x = gCase ? "Února" : "Únor"; break;
				case 3: x = gCase ? "Března" : "Březen"; break;
				case 4: x = gCase ? "Dubna" : "Duben"; break;
				case 5: x = gCase ? "Května" : "Květen"; break;
				case 6: x = gCase ? "Června" : "Červen"; break;
				case 7: x = gCase ? "Července" : "Červenec"; break;
				case 8: x = gCase ? "Srpna" : "Srpen"; break;
				case 9: x = gCase ? "Září" : "Září"; break;
				case 10: x = gCase ? "Října" : "Říjen"; break;
				case 11: x = gCase ? "Listopadu" : "Listopad"; break;
				case 12: x = gCase ? "Prosince" : "Prosinec"; break;
			}
			break;
		case "en":
			switch (i) {
				case 1: x = "January"; break;
				case 2: x = "February"; break;
				case 3: x = "March"; break;
				case 4: x = "April"; break;
				case 5: x = "May"; break;
				case 6: x = "June"; break;
				case 7: x = "July"; break;
				case 8: x = "August"; break;
				case 9: x = "September"; break;
				case 10: x = "October"; break;
				case 11: x = "November"; break;
				case 12: x = "December"; break;
			}
			break;
	}
	return x;
}

/* privatni metody */

/* dtGetDayExt(); metoda objektu Date.getDayExtension()
metoda vraci retezec interpretujici priponu dne anglickeho tvaru
1 - st
2 - nd
3 - rd
4-0 - th
*/
function dtGetDayExt() {
	switch (this.getD()%10) {
		case 1: return "st";
		case 2: return "nd";
		case 3: return "rd";
		case 4:
		case 5:
		case 6:
		case 7:
		case 8:
		case 9:
		case 0:
			return "th";
	}
}


