// Calendar Display
// www.mredkj.com
// June 08, 2002 - version 1.0.0
// November 14, 2002 - version 1.1.0
// March 07, 2006 - version 2.BETA.20060307
// May 08, 2006 - version 2.BETA.20060508
// July 25, 2006 - version 2.BETA.20060725
// October 10, 2006 - version 2.BETA.20061010
//
// to do: If click away from the calendar, then hide it. This will help avoid the orphan calendar when deleting text boxes.
//
function toggleCalendar(txtObj)
{	
	cObj = txtObj.myCalendar;
	if (!cObj) {
		cObj = new CalendarDisplay(txtObj);
		document.body.appendChild(cObj.cDiv);
		txtObj.myCalendar = cObj;
		
	}
	
	cObj.toggle();
}

CalendarDisplay = function(txtObj) {
	this.txtObj = txtObj;
	this.tBox = this.txtObj;
	this.cDiv = document.createElement('div');
	this.cDiv.style.position = 'absolute';
	this.cDiv.style.display = 'none';
}

CalendarDisplay.prototype.MONTHS_CALENDAR = new Array("Styczeń", "Luty", "Marzec", "Kwiecień", "Maj", "Czerwiec", "Lipiec", "Sierpień", "Wrzesień", "Październik", "Listopad", "Grudzień");
CalendarDisplay.prototype.DAYS_1_CALENDAR = new Array("N", "P", "W", "Ś", "Cz", "P", "S");
CalendarDisplay.prototype.DAYS_2_CALENDAR = new Array("N", "P", "W", "Ś", "Cz", "P", "S");

CalendarDisplay.prototype.toggle = function() {
	if (this.cDiv.style.display == 'none') {
		this.adjustPosition();
		this.fillCalendar(this.grabDate());
		this.cDiv.style.display = 'block';
	} else {
		this.cDiv.style.display = 'none';
	}
}

CalendarDisplay.prototype.grabDate = function() {
	var tempDate = new Date(this.tBox.value);
	if (!tempDate.getYear()) {
		tempDate = new Date();
	}
	return tempDate;
}

CalendarDisplay.prototype.fillCalendar = function(theDate) {
	if (this.cDiv.firstChild) {
		this.cDiv.removeChild(this.cDiv.firstChild);
	}
	this.adjustPosition();
	this.cDiv.appendChild(this.getCalendar(theDate));
}

CalendarDisplay.prototype.adjustPosition = function() {
	var pixel_top = Number();
	var pixel_left = Number();
	pixel_top = -40;
	pixel_left = -300;
	
	this.cDiv.style.top = this.tBox.offsetHeight + this.findPosY(this.tBox)+ pixel_top + 'px';
// !! IE, FF, and Opera positions can be slightly different depending on the page layout.
// !! I think it has to do with the page padding
	this.cDiv.style.left = this.findPosX(this.tBox) + pixel_left + 'px';
}

CalendarDisplay.prototype.getCalendar = function(theDate) {
	
	var theYear = theDate.getFullYear();
	var theMonth = theDate.getMonth();
	var theDay = theDate.getDate();

	var theTable = document.createElement('table');
	theTable.id = 'calendartable';
	var theTHead = theTable.createTHead();
	var theTBody = document.createElement('tbody');
	theTable.appendChild(theTBody);
	
	var monthRow = theTHead.insertRow(0);
	var navLeftCell = monthRow.insertCell(0);
	var monthCell = monthRow.insertCell(1);
	var navRightCell = monthRow.insertCell(2);
	monthCell.colSpan = 5;
	monthCell.appendChild(document.createTextNode(this.MONTHS_CALENDAR[theMonth] + ', ' + theYear));
	var leftLink = document.createElement('a');
	leftLink.href = '#rezerwacja';
	this.setCalendarPrevious(leftLink, this.txtObj, theYear, theMonth, theDay);
	leftLink.appendChild(document.createTextNode('<'));
	
	navLeftCell.appendChild(leftLink);
	var rightLink = document.createElement('a');
	rightLink.href = '#rezerwacja';
	this.setCalendarNext(rightLink, this.txtObj, theYear, theMonth, theDay);
	rightLink.appendChild(document.createTextNode('>'));
	navRightCell.appendChild(rightLink);
	
	var weeksRow = theTHead.insertRow(1);
	for (var i=0; i<7; i++) {
		var tempWeeksCell = weeksRow.insertCell(i);
		tempWeeksCell.appendChild(document.createTextNode(this.DAYS_2_CALENDAR[i]));
	}
	
	var temporaryDate1 = new Date(theYear, theMonth, 1);
	var startDayOfWeek = temporaryDate1.getDay();
	var temporaryDate2 = new Date(theYear, theMonth + 1, 0);
	var lastDateOfMonth = temporaryDate2.getDate();
	var dayCount = 1;
		
	for (var r=0; r<6; r++) {
		var tempDaysRow = theTable.tBodies[0].insertRow(r);
		tempDaysRow.className = 'dayrow';
		for (var c=0; c<7; c++) {
			var tempDaysCell = tempDaysRow.insertCell(c);
			var mysteryNode;
			if ((r > 0 || c >= startDayOfWeek) && dayCount <= lastDateOfMonth) {
				tempDaysCell.className = 'yestext';
				var mysteryNode = document.createElement('a');
				mysteryNode.href = '#rezerwacja';
				this.setCalendarClick(mysteryNode, this.txtObj, theYear, theMonth, dayCount);
				
				mysteryNode.appendChild(document.createTextNode(dayCount));
				dayCount++;
			} else {
				tempDaysCell.className = 'notext';
				mysteryNode = document.createTextNode('');
			}
			tempDaysCell.appendChild(mysteryNode);
		}
	}
	
	return theTable;
}
CalendarDisplay.prototype.setCalendarClick = function (node, theObj, theYear, theMonth, theDay) {
	node.onclick = function() {fillInFields(theObj, theYear, (theMonth + 1), theDay); return false;}
}
CalendarDisplay.prototype.setCalendarPrevious = function (node, theObj, theYear, theMonth, theDay) {
	node.onclick = function() {showPrevious(theObj, theYear, theMonth, theDay); return false;}
}
CalendarDisplay.prototype.setCalendarNext = function (node, theObj, theYear, theMonth, theDay) {
	node.onclick = function() {showNext(theObj, theYear, theMonth, theDay); return false;}
}
	

// http://www.quirksmode.org/js/findpos.html
CalendarDisplay.prototype.findPosX = function(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	}
	else if (obj.x) {
		curleft += obj.x;
	}
	return curleft;
}

// http://www.quirksmode.org/js/findpos.html
CalendarDisplay.prototype.findPosY = function(obj) {
	var curtop = 0;
	if (obj.offsetParent)	{
		while (obj.offsetParent) {
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
	}
	else if (obj.y) {
		curtop += obj.y;
	}
	return curtop;
}

function fillInFields(obj, year, month, day)
{
		var Today = new Date();
	 //alert(Today);
   var dzien=Today.getDate()
	var miesiac=Today.getMonth()+1
	var rok=Today.getYear()
	//alert(year)
	
	/* var dzien=29;
	var miesiac=12;
	var rok=2006;
	*/
	//alert(getTime())
	
	if(year>=rok)
	{
		if(month >= miesiac)
		{
			if(day >=dzien)
			{
			obj.value = (month < 10 ? '0'+month : month) + '-' + (day < 10 ? '0'+day : day) + '-' + year;
			cObj = obj.myCalendar;
			cObj.toggle();
			zmien_date(obj.value)
			}
			else
			{	
				if(month > miesiac)
				{
					obj.value = (month < 10 ? '0'+month : month) + '-' + (day < 10 ? '0'+day : day) + '-' + year;
					cObj = obj.myCalendar;
					cObj.toggle();
					zmien_date(obj.value)
				}
				else
				{
					alert('Wybrana data nie może być mniejsza niz dzisiejsza');	
				}
					
			}
		}
		else
		{
			if(month >=miesiac)
			{
			obj.value = (month < 10 ? '0'+month : month) + '-' + (day < 10 ? '0'+day : day) + '-' + year;
			cObj = obj.myCalendar;
			cObj.toggle();
			zmien_date(obj.value)
			}
			else
			{	
				if(year > rok)
				{
					obj.value = (month < 10 ? '0'+month : month) + '=' + (day < 10 ? '0'+day : day) + '=' + year;
					cObj = obj.myCalendar;
					cObj.toggle();
					zmien_date(obj.value)// funkcja zamienia date na sekundy
				}
				else
				{
					alert('Wybrana data nie może być mniejsza niz dzisiejsza');	
				}
					
			}
		}
	}
	else
	{
		alert('Wybrana data nie może być mniejsza niz dzisiejsza');	
	}
}

function showPrevious(obj, year, month, day)
{
	cObj = obj.myCalendar;
	var lastMonth = new Date(year, month - 1, day)
	cObj.fillCalendar(lastMonth);
}
function showNext(obj, year, month, day)
{
	cObj = obj.myCalendar;
	var nextMonth = new Date(year, month + 1, day)
	cObj.fillCalendar(nextMonth);
}

function zmien_date(data)
{


	//alert(data)
	exploded_data = data.split("-");
	var d = new Date()
	d.setFullYear(exploded_data[2],exploded_data[0]-1,exploded_data[1])
	///alert(d)
	layer=document.getElementById('temp').value;
	//alert(layer)
	document.getElementById('data_'+layer).value=Math.ceil(d.getTime()/1000);
	year = d.getFullYear();
	month = d.getMonth()+1;
	day = d.getUTCDate();
	document.getElementById('dataformat_'+layer).value=year+'-'+(month < 10 ? '0'+month : month)+'-'+(day < 10 ? '0'+day : day);
	
	build_rooms(layer);	
		
	sprawdz_promocje(layer,Math.ceil(d.getTime()));
	//alert(d.getTime())
}
function send_obj(obj)
{
	document.getElementById('temp').value=obj;
	//alert(obj)
}
