

function DateTimePicker(id) {
	this.ID = id;
	this.CID = '_'+this.ID;
	this.DateTime = null;
	this.DateFormat = "DD-MM-YYYY";//"DD-MM-YYYY hh:mm";
	this.TimeFormat = "hh:mm:ss";
	this.ID_TIME_BTN = this.ID;
	this.ID_DATE_BTN = this.ID + '_date_btn';
	this.ID_POP_UP = this.ID + '_pop_up';
	this.CalendarVisibleDate = null;
	this.CalendarVisible = false;
	this.TimeTableVisible = false;
}
DateTimePicker.prototype.ShowTimeTable = function() {
	if(this.TimeTableVisible) {
		this.HidePopUp();		
	}else {
		$(this.ID_POP_UP).set('html',this.BuildTimeTableHtml(null));
		var y = $(this.ID_TIME_BTN).getCoordinates().top + $(this.ID_TIME_BTN).getCoordinates().height;
		var x = $(this.ID_TIME_BTN).getCoordinates().left + $(this.ID_TIME_BTN).getCoordinates().width;
		$(this.ID_POP_UP).setStyle('top', y + "px");
		$(this.ID_POP_UP).setStyle('left', x + "px");
		$(this.ID_POP_UP).setStyle('display','');
		this.TimeTableVisible = true;
	}
}
DateTimePicker.prototype.ShowCalendar = function() {
	if(this.TimeTableVisible) {
		this.HidePopUp();		
	}
	if(!this.CalendarVisible) {
		$(this.ID_POP_UP).set('html',this.BuildMonthHtml(null)); 
		var y = $(this.ID_DATE_BTN).getCoordinates().top + $(this.ID_DATE_BTN).getCoordinates().height;
		var x = $(this.ID_DATE_BTN).getCoordinates().left + $(this.ID_DATE_BTN).getCoordinates().width;
		$(this.ID_POP_UP).setStyle('top', y + "px");
		$(this.ID_POP_UP).setStyle('left', x + "px");
		$(this.ID_POP_UP).setStyle('display','');
		this.CalendarVisible = true;
	}else {
		this.HidePopUp();
	}
}
DateTimePicker.prototype.HidePopUp = function() {
	$(this.ID_POP_UP).setStyle('display','none');
	this.CalendarVisible = false;
	this.TimeTableVisible = false;
}
DateTimePicker.prototype.BuildMonthHtml = function(date) {
	var toDay = new Date();	
	if(date == null){
		if(this.DateTime == null){
			date = toDay;	
		}else {
			date = this.DateTime;	
		}
	}
	var days_count = this.daysInMonth(date.getMonth(),date.getFullYear());
	var tableHtml = '<table cellpdding="0" cellspacing="0" style="background-color: #EBEBEB; border-left: solid 1px #EBEBEB;"><tbody>';
	tableHtml += '<tr>';
	tableHtml += '<td style="width: 10px;"><img src="gfx/Cimg/prev.gif" style="cursor: pointer;" onclick="' + this.CID + '.MoveMonthBackward();" /></td>';
	tableHtml += '<td colspan="5" class="Default_DateTimePicker_CalendarMYinfo">' + this.GetMonthString(date.getMonth()) + ' ' + date.getFullYear() + '</td>';
	tableHtml += '<td style="width: 10px; text-align: right;"><img src="gfx/Cimg/next.gif" style="cursor: pointer;" onclick="' + this.CID + '.MoveMonthForward();" /></td>';
	tableHtml += '</tr>';
	//tableHtml += '</tbody></table>';
	//tableHtml += '<table cellpdding="0" cellspacing="0"><tbody>';
	tableHtml += '<tr>';
	tableHtml += '<td class="Default_DateTimePicker_CalendarDayinfo">ND</td>';
	tableHtml += '<td class="Default_DateTimePicker_CalendarDayinfo">PN</td>';
	tableHtml += '<td class="Default_DateTimePicker_CalendarDayinfo">WT</td>';
	tableHtml += '<td class="Default_DateTimePicker_CalendarDayinfo">SR</td>';
	tableHtml += '<td class="Default_DateTimePicker_CalendarDayinfo">CZ</td>';
	tableHtml += '<td class="Default_DateTimePicker_CalendarDayinfo">PT</td>';
	tableHtml += '<td class="Default_DateTimePicker_CalendarDayinfo">SO</td>';
	tableHtml += '</tr>';
	
	this.CalendarVisibleDate = new Date(date.getFullYear(), date.getMonth(), 1);
	var dayofWeek = this.CalendarVisibleDate.getDay();
	
	var row = 0;
	var i = 1;
	var synchday = true;
	while(i <= days_count){
		tableHtml += '<tr>';
		for(var weekDay = 0; weekDay < 7; weekDay++) {
			if(row == 0){
				if(dayofWeek != 0 && synchday == true) {
					synchday = false;
					for(var j=0; j < dayofWeek; j++) {
						tableHtml += '<td class="Default_DateTimePicker_CalendarDay">&nbsp;</td>';
					}
					weekDay = dayofWeek;
				}		
			}
			if(i > days_count){
				tableHtml += '<td class="Default_DateTimePicker_CalendarDay">&nbsp;</td>';
			}else {
				var wasDraw = false;
				if(this.DateTime != null){
					if(this.DateTime.getDate() == i && date.getFullYear() == this.DateTime.getFullYear() && date.getMonth() == this.DateTime.getMonth()){
						tableHtml += '<td class="Default_DateTimePicker_CalendarDay_Selected" onclick="' + this.CID + '.PickDate(new Date(' + date.getFullYear() + ',' + date.getMonth() + ',' + i + '));">' + i + '</td>';
						wasDraw = true;
					}
				} 
				if(!wasDraw) {
					if(toDay.getDate() == i && date.getFullYear() == toDay.getFullYear() && date.getMonth() == toDay.getMonth()){
						tableHtml += '<td class="Default_DateTimePicker_CalendarDay_ToDay" onclick="' + this.CID + '.PickDate(new Date(' + date.getFullYear() + ',' + date.getMonth() + ',' + i + '));">' + i + '</td>';
					} else {
						tableHtml += '<td class="Default_DateTimePicker_CalendarDay" onclick="' + this.CID + '.PickDate(new Date(' + date.getFullYear() + ',' + date.getMonth() + ',' + i + '));">' + i + '</td>';
					}
				}
			}
			i++;
		}
		tableHtml += '</tr>';
		row++;
	}
	tableHtml += '</tbody></table>';
	return tableHtml;
}
DateTimePicker.prototype.BuildTimeTableHtml = function(date) {
	var tableHtml = '<table cellpdding="0" cellspacing="0" style="background-color: #EBEBEB; border-left: solid 1px #EBEBEB;"><tbody>';
	var licznik = 1;
	var godzina;
	for(var i=0; i < 24; i++) {
		if(licznik == 1)	{
			tableHtml += '<tr>';
		}	
		if(i < 10)
			godzina = "0" + i + ":00";
		else 
			godzina = i + ":00";
		tableHtml += '<td class="Default_DateTimePicker_CalendarDay" onclick="' + this.CID + '.PickTime(' + i + ')">' + godzina + '</td>';
		if(licznik == 2)	{
			tableHtml += '</tr>';
			licznik = 1;
		}else {
			licznik++;	
		}
	}
	tableHtml += '</tbody></table>';
	return tableHtml;
}
DateTimePicker.prototype.SetDate = function(date) {
	this.DateTime = date;
	
	var dzien;
	var miesiac;
	var rok;
	var godziny;
	var minuty;
	dzien = this.DateTime.getDate();
	
	if(dzien < 10) {
		dzien = "0" + dzien;	
	}
	miesiac = this.DateTime.getMonth();
	miesiac++;
	if(miesiac < 10) {
		miesiac = "0" + miesiac;	
	}
	rok = this.DateTime.getFullYear();
	godziny = this.DateTime.getHours();
	if(godziny < 10){
		godziny = "0" + godziny;	
	}
	minuty = this.DateTime.getMinutes();
	if(minuty < 10){
		minuty = "0" + minuty;	
	}
	var dateString = this.DateFormat;
	dateString = dateString.replace("MM",miesiac);
	dateString = dateString.replace("DD",dzien);
	dateString = dateString.replace("YYYY",rok);
	dateString = dateString.replace("hh",godziny);
	dateString = dateString.replace("mm",minuty);
	$(this.ID).setProperty('value',dateString);
}
DateTimePicker.prototype.PickDate = function(date) {
	if(this.DateTime == null){
		date.setHours(0);
		date.setMinutes(0);	
	}else {
		date.setHours(this.DateTime.getHours());
		date.setMinutes(this.DateTime.getMinutes());	
	}
	this.SetDate(date);
	$(this.ID_POP_UP).set('html',this.BuildMonthHtml(date)); 
	this.HidePopUp();
}
DateTimePicker.prototype.PickTime = function(hour) {
	if(this.DateTime == null) {
		this.DateTime = new Date();	
	}
	this.DateTime.setHours(hour);
	this.DateTime.setMinutes(0);
	this.SetDate(this.DateTime);
	$(this.ID_POP_UP).set('html',this.BuildTimeTableHtml(null));
	this.HidePopUp();
}
DateTimePicker.prototype.MoveMonthForward = function() {
	var miesiac = this.CalendarVisibleDate.getMonth();
	var rok = this.CalendarVisibleDate.getFullYear();
	if(miesiac == 11) {
		rok++;
		miesiac = 0;
	}else {
		miesiac++;	
	}
	this.CalendarVisibleDate.setDate(1);
	this.CalendarVisibleDate.setFullYear(rok);
	this.CalendarVisibleDate.setMonth(miesiac);
	$(this.ID_POP_UP).set('html',this.BuildMonthHtml(this.CalendarVisibleDate)); 
}
DateTimePicker.prototype.MoveMonthBackward = function() {
	var miesiac = this.CalendarVisibleDate.getMonth();
	var rok = this.CalendarVisibleDate.getFullYear();
	if(miesiac == 0) {
		rok--;
		miesiac = 11;
	}else {
		miesiac--;	
	}
	this.CalendarVisibleDate.setDate(1);
	this.CalendarVisibleDate.setFullYear(rok);
	this.CalendarVisibleDate.setMonth(miesiac);
	$(this.ID_POP_UP).set('html',this.BuildMonthHtml(this.CalendarVisibleDate)); 
}

DateTimePicker.prototype.daysInMonth = function(iMonth, iYear) {
	return 32 - new Date(iYear, iMonth, 32).getDate();
}
DateTimePicker.prototype.GetMonthString = function(month) {
	switch(month) {
		case 0:
			return "styczeń";
		case 1:
			return "luty";
		case 2:
			return "marzec";
		case 3:
			return "kwiecień";
		case 4:
			return "maj";
		case 5:
			return "czerwiec";
		case 6:
			return "lipiec";
		case 7:
			return "sierpień";
		case 8:
			return "wrzesień";
		case 9:
			return "październik";
		case 10:
			return "listopad";
		case 11:
			return "grudzień";
	}
}

DateTimePicker.prototype.GetValue = function() {
	return $(this.ID).getProperty('value');
}




