function DateSelect(_name) {

	var now = new Date();
	var elementDay, elementMonth, elementYear, elementHour, elementMinute, elementButton;

	this.name = _name;
	this.id = "calendar" + _name;
	this.day = now.getDate();
	this.month = now.getMonth() + 1;
	this.year = now.getFullYear();
	
	this.onShown = function() { return true };
	this.onHidden = function() { return true };
	this.onDateChoosen = function() { return true };

	this.jumpToDate = function(_month, _year) {
	
		this.month = _month;
		this.year = _year;
		this.drawDate();
		
	}
	
	this.popup = null;
	
	this.setEnabled = function(_val) {
		
		elementDay.disabled = !_val;
		elementMonth.disabled = !_val;
		elementYear.disabled = !_val;
		if(elementHour)
			elementHour.disabled = !_val;
		if(elementMinute)
			elementMinute.disabled = !_val;
		if(_val) {
			elementButton.style.cursor = "pointer";
			elementButton.onclick = delegate(this, this.show);
			elementButton.src = elementButton.src.replace("_dis", "");
		} else {
			elementButton.style.cursor = "default";
			elementButton.onclick = function() { }
			elementButton.src = elementButton.src.replace(".png", "_dis.png");
		}
	}

	this.assign = function(_id) {

		var length = this.name.length;
		var weekName = getById(this.name.substr(0, length - 3) + "Week");
		
		elementDay = getById(_id + "Day");
		elementMonth = getById(_id + "Month");
		elementYear = getById(_id + "Year");
		if(getById(_id + "Hour"))
			elementHour = getById(_id + "Hour");
		if(getById(_id + "Minute"))
			elementMinute = getById(_id + "Minute");

		var html = "";

		html += '<div style="display: block; position: relative; margin: 18px 10px 0px 5px;" class="floatedElement">';
		html += "<img id='" + this.id + "Button' onclick='" + this.name + ".show()' src='" + iconPath + "/calendar_16.png' style='cursor: pointer; position: relative; top: 3px' />";
		html += '</div>';
		
		var popup = "<div id='" + this.id + "' style='position: absolute; display: none; cursor: default; background-color: #FFFFFF; border: solid 1px #9B95A6; padding: 2px; z-index: 100;'>"
			+ "<table style='border-collapse: collapse; font-family: Verdana; font-size: 9px; color: #000000; text-align: center; width: 130px'>"
			+ "<tr style='height: 14px'>"
			+ "<td id='" + this.id + "Prev' style='border: #9B95A6 1px solid; cursor: pointer; text-align: center; font-weight: bold;' onMouseOver='this.style.background=\"#bbc1c6\"; this.style.color=\"#FFFFFF\";' onMouseOut='this.style.background=\"\"; this.style.color=\"\";'>&laquo;</td>"
			+ "<td id='" + this.id + "TitleMonth' colspan='5' style='text-align: center; font-weight: bold;'></td>"
			+ "<td id='" + this.id + "Next' style='border: #9B95A6 1px solid; cursor: pointer; text-align: center; font-weight: bold;' onMouseOver='this.style.background=\"#bbc1c6\"; this.style.color=\"#FFFFFF\";' onMouseOut='this.style.background=\"\"; this.style.color=\"\";'>&raquo;</td>"
			+ "</tr>"
			+ "<tr style='height: 14px'>"
			+ "<td id='" + this.id + "PrevYear' style='border: #9B95A6 1px solid; cursor: pointer; text-align: center; font-weight: bold;' onMouseOver='this.style.background=\"#bbc1c6\"; this.style.color=\"#FFFFFF\";' onMouseOut='this.style.background=\"\"; this.style.color=\"\";'>&laquo;</td>"
			+ "<td id='" + this.id + "TitleYear' colspan='5' style='text-align: center; font-weight: bold;'></td>"
			+ "<td id='" + this.id + "NextYear' style='border: #9B95A6 1px solid; cursor: pointer; text-align: center; font-weight: bold;' onMouseOver='this.style.background=\"#bbc1c6\"; this.style.color=\"#FFFFFF\";' onMouseOut='this.style.background=\"\"; this.style.color=\"\";'>&raquo;</td>"
			+ "</tr>"
			+ "<tr style='height: 14px; font-weight: bold; text-align: center'>"
			+ "<td style='width: 18px; text-align: center;'>" + weekName[0].text.substr(0,2) + "</td>"
			+ "<td style='width: 18px; text-align: center;'>" + weekName[1].text.substr(0,2) + "</td>"
			+ "<td style='width: 18px; text-align: center;'>" + weekName[2].text.substr(0,2) + "</td>"
			+ "<td style='width: 18px; text-align: center;'>" + weekName[3].text.substr(0,2) + "</td>"
			+ "<td style='width: 18px; text-align: center;'>" + weekName[4].text.substr(0,2) + "</td>"
			+ "<td style='width: 18px; text-align: center;'>" + weekName[5].text.substr(0,2) + "</td>"
			+ "<td style='width: 18px; text-align: center;'>" + weekName[6].text.substr(0,2) + "</td>"
			+ "</tr>"
			+ "<tr style='height: 14px'>"
			+ "<td id='" + this.id + "1'></td>"
			+ "<td id='" + this.id + "2'></td>"
			+ "<td id='" + this.id + "3'></td>"
			+ "<td id='" + this.id + "4'></td>"
			+ "<td id='" + this.id + "5'></td>"
			+ "<td id='" + this.id + "6'></td>"
			+ "<td id='" + this.id + "7'></td>"
			+ "</tr>"
			+ "<tr style='height: 14px'>"
			+ "<td id='" + this.id + "8'></td>"
			+ "<td id='" + this.id + "9'></td>"
			+ "<td id='" + this.id + "10'></td>"
			+ "<td id='" + this.id + "11'></td>"
			+ "<td id='" + this.id + "12'></td>"
			+ "<td id='" + this.id + "13'></td>"
			+ "<td id='" + this.id + "14'></td>"
			+ "</tr>"
			+ "<tr style='height: 14px'>"
			+ "<td id='" + this.id + "15'></td>"
			+ "<td id='" + this.id + "16'></td>"
			+ "<td id='" + this.id + "17'></td>"
			+ "<td id='" + this.id + "18'></td>"
			+ "<td id='" + this.id + "19'></td>"
			+ "<td id='" + this.id + "20'></td>"
			+ "<td id='" + this.id + "21'></td>"
			+ "</tr>"
			+ "<tr style='height: 14px'>"
			+ "<td id='" + this.id + "22'></td>"
			+ "<td id='" + this.id + "23'></td>"
			+ "<td id='" + this.id + "24'></td>"
			+ "<td id='" + this.id + "25'></td>"
			+ "<td id='" + this.id + "26'></td>"
			+ "<td id='" + this.id + "27'></td>"
			+ "<td id='" + this.id + "28'></td>"
			+ "</tr>"
			+ "<tr style='height: 14px'>"
			+ "<td id='" + this.id + "29'></td>"
			+ "<td id='" + this.id + "30'></td>"
			+ "<td id='" + this.id + "31'></td>"
			+ "<td id='" + this.id + "32'></td>"
			+ "<td id='" + this.id + "33'></td>"
			+ "<td id='" + this.id + "34'></td>"
			+ "<td id='" + this.id + "35'></td>"
			+ "</tr>"
			+ "<tr style='height: 14px'>"
			+ "<td id='" + this.id + "36'></td>"
			+ "<td id='" + this.id + "37'></td>"
			+ "<td></td>"
			+ "<td></td>"
			+ "<td></td>"
			+ "<td></td>"
			+ "<td onClick='" + this.name + ".hide()' style='border: #9B95A6 1px solid; cursor: pointer; text-align: center; font-weight: bold;' onMouseOver='this.style.background=\"#bbc1c6\"; this.style.color=\"#FFFFFF\";' onMouseOut='this.style.background=\"\"; this.style.color=\"\";'>X</td>"
			+ "</tr>"
			+ "</table>"
			+ "</div>";
		
		getById('cal_container_' + this.name).innerHTML = html;
		
		if (!IE) {
			var divElement = document.createElement("div");
			divElement.innerHTML = popup;
			getById('generatedContent').appendChild(divElement);
		}
		else {
			getById('generatedContent').innerHTML += popup;
		}
		
		elementButton = getById(this.id + "Button");
		
	}
	
	this.writeContent = function() {  
		var divElement = document.createElement("div");
		divElement.innerHTML = this.popup;
		document.body.appendChild(divElement);
	}
	
	this.drawDate = function() {
		
		var date = new Date();

		var length = this.name.length;
		var monthName = getById(this.name.substr(0, length - 3) + "Month");
		
		if(monthName[0].value.length == 0)
			var m = 1;
		else
			var m = 0;
		
		var months = new Array(monthName[m].text, monthName[m+1].text,
							 monthName[m+2].text, monthName[m+3].text,
							 monthName[m+4].text, monthName[m+5].text,
							 monthName[m+6].text, monthName[m+7].text, 
							 monthName[m+8].text, monthName[m+9].text, 
							 monthName[m+10].text, monthName[m+11].text);
							 
		
		if(this.month >= 0 && this.year >= 0) {
			// Set month and year if both are bigger than 0
			date.setMonth(this.month - 1); // Months start at 0
			date.setYear(this.year);
		}
		
		var month = date.getMonth();
		var year = date.getFullYear();
		getById(this.id + "TitleMonth").innerHTML = months[month];
		getById(this.id + "TitleYear").innerHTML = year;
		
		// Let's find the first day in this month
		date.setDate(1);
		var startday = date.getDay();
		if(startday == 0)
			startday = 7;
				
	//	alert(date.toLocaleString());	
		var dateNow = new Date();
		for(i = 1; i <= 37; i++) {
			if(i - startday + 1 > 0 && checkDate(i - startday + 1, month, year)) {
				// Show day in calendar if it exists
				date.setDate(i - startday + 1);
				var currentDay = i - startday + 1;
				getById(this.id + i).innerHTML = "<center>" + currentDay + "</center>";
				getById(this.id + i).style.cursor = "pointer";

				if(currentDay == dateNow.getDate() && month == dateNow.getMonth() && year == dateNow.getYear() + 1900)
					getById(this.id + i).style.fontWeight = "bold";
				else
					getById(this.id + i).style.fontWeight = "normal";
					
				getById(this.id + i).onclick = new Function(this.name + ".chooseDate(" + currentDay + "); " + this.name + ".hide()");
				getById(this.id + i).onmouseover = new Function("this.style.background = '#bbc1c6'; this.style.color = '#FFFFFF';");
				getById(this.id + i).onmouseout = new Function("this.style.background = ''; this.style.color = '';");
			} else {
				// Don't show cell if day doesn't exist
				getById(this.id + i).innerHTML = "";
				getById(this.id + i).style.border = "";
				getById(this.id + i).onclick = new Function("return false");
				getById(this.id + i).onmouseover = new Function("return false");
				getById(this.id + i).onmouseout = new Function("return false");
			}
		}
		
		var monthPrev = month; // Remember: months start at 0 internally
		var monthNext = month + 2;
		var yearPrev = year;
		var yearNext = year;
		
		if(monthPrev == 0) {
			// Switch back one year if month is "negative"
			monthPrev = 12;
			yearPrev--;
		}
		
		if(monthNext == 13) {
			// Switch forward one year if month is too high
			monthNext = 1;
			yearNext++;
		}

		getById(this.id + "Prev").onclick = new Function(this.name + ".jumpToDate(" + monthPrev + ", " + yearPrev + ")");
		getById(this.id + "Next").onclick = new Function(this.name + ".jumpToDate(" + monthNext + ", " + yearNext + ")");


		getById(this.id + "PrevYear").onclick = new Function(this.name + ".jumpToDate(" + (month + 1) + ", " + (yearPrev-1) + ")");
		getById(this.id + "NextYear").onclick = new Function(this.name + ".jumpToDate(" + (month + 1) + ", " + (year+1) + ")");
		
	}

	this.show = function() {

		this.drawDate();
		
		var top = posY(getById(this.id + "Button"));
		var left = posX(getById(this.id + "Button"));
		
		if(IE) {
			top += 23;
		}

		getById(this.id).style.left = left + "px";
		getById(this.id).style.top = top + "px";
		getById(this.id).style.display = "block";
		
		this.onShown();

	}

	this.hide = function() {
		getById(this.id).style.display = "none";
		this.onHidden();
	}
	
	this.chooseDate = function(_day) {
		
		this.day = _day;
		if(elementDay.options.length == 32)
			elementDay.options[this.day].selected = true;
		else
			elementDay.options[this.day - 1].selected = true;
		
		if(elementMonth.options[0].value.length == 0)
			var j = 0;
		else
			var j = 1;	
		
		elementMonth.options[this.month - j].selected = true;
		elementYear.value = this.year;
		this.onDateChoosen();
	}

}