
function Calendar(
	startDate,
	endDate,
	leftMarginDate,
	rightMarginDate,
	selectedDate,
	showDate,
	objName,
	destinationObj,
	destinationTypeDate,
	destinationIdDate
	)
{
	this.className = "Calendar";
	this.objName = objName;
	this.version = "1.0";

	//od jakiego dnia można wybrać date
	this.startDate = startDate;
	//do jakiego dnia można wybrać date
	this.endDate = endDate;
	//od jakiego miesiąca można wyświetlić kalendarz
	this.leftMarginDate = leftMarginDate;
	//do jakiego miesiąca można wyświetlić kalendarz
	this.rightMarginDate = rightMarginDate;
	//jaka data jest zaznaczona
	this.selectedDate = selectedDate;
	//miesiac jaki ma byc wyswietlony
	if (showDate == null)
	{
		if (selectedDate == null)
			this.showDate = new Date();
		else
			this.showDate = selectedDate;
	}	
	else
		this.showDate = showDate;
	
	//id obiektu(ów) w którym należy umieścić kalendarz
	this.destinationIdDate = destinationIdDate;
	//typ daty jaki ma być zwrócony
	this.destinationTypeDate = destinationTypeDate;
	
	//jaki miesiac ma sie pojawic po przeladowaniu strony
	this.monthToDisplay = this.showDate.getMonth();
	this.yearToDisplay = this.showDate.getFullYear();
	
	this.destinationObj = destinationObj;
	
	//uzywane do styli
	this.destinationObj.className="calendar";		
	
	this.daysLabel = Array("Su","Mo","Tu","We","Th","Fr","Sa","Su");
	this.monthLabel = Array("January","February","March","April","May","June","July","August","September","October","November","December");
	this.dayOfMonth = Array(31,null,31,30,31,30,31,31,30,31,30,31);
	
	this.showPrevAndNextHeader = false;
	this.showMonthAndYearHeader = false;
	this.showCreateCloseFutter = false;
	
	this.beforeOnclick = "";
	this.afterOnclick = "";
	
	this.isBuild = false;

	this.displayCalendar=function()
	{
		this.selectedDate = this.getDateFromDestinationInput();
		this.builtCalendar();
	}

	this.builtCalendar=function()
	{
		this.isBuild = true;
		var today = new Date();
		
		var buffor = "";
		
		var dateTmp = new Date();
		dateTmp.setFullYear(this.yearToDisplay,this.monthToDisplay,1);
		
		if (this.showPrevAndNextHeader)
			buffor += this.prevAndNextHeader();
		if (this.showMonthAndYearHeader)
			buffor += this.createMonthAndYearHeader(this.monthToDisplay,this.yearToDisplay);

		buffor += this.createDaysHeader();
	
		var countInLine = 0;

		//dopelnienie pustych pól
		//jesli niedziela
		if (dateTmp.getDay() == 0)
		{
			countInLine=6;
			buffor += "<div class=\"dayOfMonth blankDayOfMonth\"></div><div class=\"dayOfMonth blankDayOfMonth\"></div><div class=\"dayOfMonth blankDayOfMonth\"></div><div class=\"dayOfMonth blankDayOfMonth\"></div><div class=\"dayOfMonth blankDayOfMonth\"></div><div class=\"dayOfMonth blankDayOfMonth\"></div>";
		}
		else
		{
			for(var i=1; i<dateTmp.getDay(); i++)
			{
				countInLine++;
				buffor += "<div class=\"dayOfMonth blankDayOfMonth\"></div>";
			}
		}
	
	
		var classDiv = "";
		var onClickDiv = "";
		if (this.monthToDisplay == 1)
			this.dayOfMonth[1] = this.fabruaryDays(this.yearToDisplay);
		for (var i=1; i<=this.dayOfMonth[this.monthToDisplay];i++)
		{
			countInLine++;
			classDiv = "dayOfMonth";
			onClickDiv = this.beforeOnclick+";"+this.actionOnOnclick(i,this.monthToDisplay,this.yearToDisplay)+";"+this.afterOnclick;
			//jesli nie aktywny - z lewej strony
			if 	(this.startDate)
				if (
					( (i < this.startDate.getDate()) && (dateTmp.getMonth() == this.startDate.getMonth()) && (dateTmp.getFullYear() == this.startDate.getFullYear()) )
					|| 
					( (dateTmp.getMonth() < this.startDate.getMonth()) && (dateTmp.getFullYear() == this.startDate.getFullYear()) ) 
					|| 
					(dateTmp.getFullYear() < this.startDate.getFullYear())
					)
				{
					classDiv += " inactiveDayOfMonth";
					onClickDiv = "";
				}
			//jesli nie aktywny - z prawej strony
			if (this.endDate)
				if (
					( (i > this.endDate.getDate()) && (dateTmp.getMonth() == this.endDate.getMonth()) && (dateTmp.getFullYear() == this.endDate.getFullYear()) )
					|| 
					( (dateTmp.getMonth() > this.endDate.getMonth()) && (dateTmp.getFullYear() == this.endDate.getFullYear()) ) 
					|| 
					(dateTmp.getFullYear() > this.endDate.getFullYear())
					)
				{
					classDiv += " inactiveDayOfMonth";
					onClickDiv = "";
				}	
				
			//jesli niedziela
			if (countInLine == 7)
				classDiv += " sundayDayOfMonth";

			//jesli dzisiaj
			if ((i == today.getDate()) && (dateTmp.getMonth() == today.getMonth()) && (dateTmp.getFullYear() == today.getFullYear()))
				classDiv += " currentDayOfMonth";
				
			//jesli juz zaznaczony
			if (
				this.selectedDate 
				&& 
				(i == this.selectedDate.getDate()) 
				&& 
				(dateTmp.getMonth() == this.selectedDate.getMonth()) 
				&& 
				(dateTmp.getFullYear() == this.selectedDate.getFullYear())
				)
			{
				
			classDiv += " selectedDayOfMonth";
			}	
			buffor += "<div onmouseover=\"this.className='"+classDiv+" onMouseOver'\" onmouseout=\"this.className='"+classDiv+"'\" class=\""+classDiv+"\" onclick=\""+onClickDiv+" \">"+i+"</div>";
			if (countInLine>=7)
			{
				countInLine = 0;
				buffor += "<div class=\"clearBoth\"></div>";
			}
		}
		//dopelnienie na końcu pustymi divami
		while((countInLine != 0) && (countInLine < 7))
		{
			countInLine++;
			buffor += "<div class=\"dayOfMonth blankDayOfMonth\"></div>";
		}
		
		if (this.showCreateCloseFutter)
			buffor += this.createCloseFutter();
		
		this.destinationObj.innerHTML = buffor;

	}
	
	this.createDaysHeader=function()
	{
		var header = "<div class=\"daysHeader\">";
		for (var i=1; i<7; i++)
		{
			if (i>1)
			{
				header += "<div style=\"background-position:-22px 100%;\" class=\"daysLabel\"><b>" + this.daysLabel[i] + "</b></div>";
			}
			else {
				header += "<div class=\"daysLabel\"><b>" + this.daysLabel[i] + "</b></div>";
			}
		}
		header += "<div style=\"background-position:-132px 100%;\"class=\"daysLabel sundayDayOfMonth\"><b>" + this.daysLabel[7] + "</b></div>";

		header += "</div>";
		header += "<div class=\"clearBoth\"></div>";
		return header;
	}
	
	this.createMonthAndYearHeader=function(month,year)
	{
		var header = "<div class=\"monthAndYearHeader\">";
		header += this.monthLabel[month];
		header += " "+year;
		header += "</div>";
		header += "<div class=\"clearBoth\"></div>";
		return header;
	}
	
	this.createCloseFutter=function()
	{
		return "";
	}
	
	this.actionOnOnclick=function(day,month,year)
	{
		var returnString = "";
		
		switch(this.destinationTypeDate)
		{
			case "STANDARD" :

				returnString += "document.getElementById('miniform').elements('"+destinationIdDate[0]+"').value='"+day+"';";
				returnString += "document.getElementById('miniform').elements('"+destinationIdDate[1]+"').value='"+(month+1)+"';";
				returnString += "document.getElementById('miniform').elements('"+destinationIdDate[2]+"').value='"+year+"';";

				return returnString;
						
			case "INONEINPUT" :
				//returnString += "document.getElementById('"+destinationIdDate[0]+"').value='"+day+"."+(month+1)+"."+year+"';";
				return returnString;
				
			case "TWOPLACES" :
				returnString += "document.getElementById('"+destinationIdDate[0]+"').value='"+day+"';";
				returnString += "document.getElementById('"+destinationIdDate[1]+"').value='"+(month+1)+"';";
				returnString += "document.getElementById('"+destinationIdDate[2]+"').value='"+year+"';";
				returnString += "document.getElementById('"+destinationIdDate[3]+"').value='"+day+"."+(month+1)+"."+year+"';";
				return returnString;
			case "TWOPLACESMINI" :
				returnString += "document.getElementById('"+destinationIdDate[3]+"').value='"+day+"."+(month+1)+"."+year+"';";
				returnString += "document.getElementById('miniform').elements['"+destinationIdDate[0]+"'].value='"+day+"';";
				returnString += "document.getElementById('miniform').elements['"+destinationIdDate[1]+"'].value='"+(month+1)+"';";
				returnString += "document.getElementById('miniform').elements['"+destinationIdDate[2]+"'].value='"+year+"';";
				return returnString;
			case "DDMMYYYY" :
				returnString += "document.getElementById('"+destinationIdDate[0]+"').value='"+day+"';";
				returnString += "document.getElementById('"+destinationIdDate[1]+"').value='"+(month+1)+"';";
				returnString += "document.getElementById('"+destinationIdDate[2]+"').value='"+year+"';";
				returnString += "document.getElementById('"+destinationIdDate[3]+"').value='"+day+"/"+(month+1)+"/"+year+"';";
				return returnString;
		}
		
		
	}
	
	this.getDateFromDestinationInput=function()
	{
		var returnDate = new Date();
		alert(this.destinationTypeDate);
		switch(this.destinationTypeDate)
		{
			case "STANDARD" :
			case "TWOPLACES" :
				day = document.getElementById(destinationIdDate[0]).value;
				month = document.getElementById(destinationIdDate[1]).value;
				year = document.getElementById(destinationIdDate[2]).value;
				if ((day > 0) && (day < 32) && (month >= 0) && (month < 12) && (year > 1990) && (year < 2050))  
					returnDate.setFullYear(year,month-1,day);
				break;
			case "TWOPLACESMINI" :
				day = document.getElementById('miniform').elements[destinationIdDate[0]].value;
				month = document.getElementById('miniform').elements[destinationIdDate[1]].value;
				year = document.getElementById('miniform').elements[destinationIdDate[2]].value;
				alert(day);
				if ((day > 0) && (day < 32) && (month >= 0) && (month < 12) && (year > 1990) && (year < 2050))  
					returnDate.setFullYear(year,month-1,day);
				break;
			case "INONEINPUT" :
				//returnString += "document.getElementById('data').value='"+day+"."+(month+1)+"."+year+"';";
			case "DDMMYYYY" :
				day = document.getElementById(destinationIdDate[0]).value;
				month = document.getElementById(destinationIdDate[1]).value;
				year = document.getElementById(destinationIdDate[2]).value;
				if ((day > 0) && (day < 32) && (month >= 0) && (month < 12) && (year > 1990) && (year < 2050))  
					returnDate.setFullYear(year,month-1,day);
				break;
				
		}

		this.monthToDisplay = returnDate.getMonth();
		this.yearToDisplay = returnDate.getFullYear();
		if (this.startDate != null)
		{
			if (returnDate < this.startDate)
			{
				this.monthToDisplay = startDate.getMonth();
				this.yearToDisplay = startDate.getFullYear();
			}
		}
		if (this.endDate != null)
		{
			if (returnDate > this.endDate)
			{
				this.monthToDisplay = endDate.getMonth();
				this.yearToDisplay = endDate.getFullYear();
			}
		}
		return returnDate;
	}
	
	this.fabruaryDays=function(year)
	{
		if ( ( ( (year % 4) == 0) && ( (year % 100) != 0) ) || ( (year % 400) == 0) )
			return 29;
	    else
	        return 28;
	}
	
	this.prevAndNextHeader=function()
	{
		var returnString = "";
		returnString += "<div>";
			returnString += "<a href=\"javascript:"+this.objName+".prevMonth()\">prev</a>";
			returnString += "<a href=\"javascript:"+this.objName+".nextMonth()\">next</a>";
		returnString += "</div>";
		
		return returnString;
	}
	
	this.prevMonth=function()
	{
		if (this.isPrevMonth())
		{
			this.monthToDisplay--;
			if (this.monthToDisplay < 0)
			{
				this.monthToDisplay = 11;
				this.yearToDisplay--;
			}
			this.builtCalendar();
		}
	}
	
	this.nextMonth=function()
	{
		if (this.isNextMonth())
		{
			this.monthToDisplay++;
			if (this.monthToDisplay > 11)
			{
				this.monthToDisplay = 0;
				this.yearToDisplay++;
			}
			this.builtCalendar();
		}
	}
	
	this.isPrevMonth=function()
	{
		if (this.leftMarginDate)
		{
			if ( (this.monthToDisplay == this.leftMarginDate.getMonth()) && (this.yearToDisplay == this.leftMarginDate.getFullYear()) )
				return false;
		}
		return true;
	}
	
	this.isNextMonth=function()
	{
		if (this.rightMarginDate)
		{
			if ( (this.monthToDisplay == this.rightMarginDate.getMonth()) && (this.yearToDisplay == this.rightMarginDate.getFullYear()) )
				return false;
		}
		return true;
	}
	
	this.getMonth=function()
	{
		return this.monthToDisplay;
	}
	
	this.getYear=function()
	{
		return this.yearToDisplay;
	}
	
	this.setSelectedDate=function(date)
	{
		//TODO przebudowanie kalendarzy ktore są zależne
		this.selectedDate = date;
	}
	
	this.setStartDate=function(date)
	{
		this.startDate = date;
	}
	
	this.setEndDate=function(date)
	{
		this.endDate = date;
	}
	
	this.getMonthName=function()
	{
		return this.monthLabel[this.monthToDisplay];
	}
	
	this.setShowDate=function(showDate)
	{
		this.showDate = showDate;
	}

}