	var CalendarStatus = '';
	var CalendarStartDate;
	var CalendarEndDate;
	var CalendarDIV;
	var hideCalendar;
	var showCalendar;
	var hasFocusCalendar;
	var CalendarCurrentMonth;

	var dayLables = new Array(
		new Array('Sun', 'Su'),
		new Array('Mon', 'Mo'),
		new Array('Tue', 'Tu'),
		new Array('Wed', 'We'),
		new Array('Thu', 'Th'),
		new Array('Fri', 'Fr'),
		new Array('Sat', 'Sa')
	);

	var monthLables = new Array(
		'January',
		'February',
		'March',
		'April',
		'May',
		'June',
		'July',
		'August',
		'September',
		'October',
		'November',
		'December'
	);

	function makeMonth(month) {
		nowDate = new Date();
		nowDate.setTime(Date.parse((nowDate.getMonth()+1)+'/'+(nowDate.getDate())+'/'+(nowDate.getYear() > 1900 ? nowDate.getYear() : nowDate.getYear() + 1900)));

		fromDate = CalendarStartDate.value.split('/');
		if (fromDate[0] && fromDate[1] && fromDate[2]&& fromDate[0] > 0 && fromDate[1] > 0 && fromDate[2] > 0) {
			fromDate = new Date();
			fromDate.setTime(Date.parse(CalendarStartDate.value));
		} else {
			fromDate = false;
		};

		toDate = CalendarEndDate.value.split('/');
		if (toDate[0] && toDate[1] && toDate[2] && toDate[0] > 0 && toDate[1] > 0 && toDate[2] > 0) {
			toDate = new Date();
			toDate.setTime(Date.parse(CalendarEndDate.value));
		} else {
			toDate = false;
		};

		currentDate = new Date();
		currentDate.setTime(Date.parse((currentDate.getMonth()+1+month)+'/'+(1)+'/'+(currentDate.getYear() > 1900 ? currentDate.getYear() : currentDate.getYear() + 1900)));

		month = currentDate.getMonth();

		htmlCode = '';

		htmlCode += '<tr>';
		htmlCode += '<td colspan="2" align="center">';
		htmlCode += '<table boreder="0" cellspacing="0" celspadding="2" class="calendar_month_table">';

		htmlCode += '<tr>';
		htmlCode += '<th colspan="'+dayLables.length+'" align="center">';
		htmlCode += monthLables[month];
		htmlCode += '</th>';
		htmlCode += '</tr>';

		htmlCode += '<tr class="calendar_weekday_item">';
		for (i = 0; i < dayLables.length; i++) {
			htmlCode += '<td align="center">';
			htmlCode += dayLables[i][1];
			htmlCode += '</td>';
		};
		htmlCode += '</tr>';

		while (currentDate.getMonth() == month) {
			htmlCode += '<tr>';

			for (i = 0; i < dayLables.length; i++) {
				weekDay = new Date();
				weekDay.setTime(currentDate.getTime() - (1000 * 60 * currentDate.getTimezoneOffset()));
				weekDay = weekDay.toGMTString();
				weekDay = weekDay.split(',');
				weekDay = weekDay[0];

				if (weekDay == dayLables[i][0] && currentDate.getMonth() == month) {
					if (nowDate > currentDate) {
						htmlCode += '<td class="calendar_lost_day_item" align="center">';
						htmlCode += '&nbsp;';
						htmlCode += currentDate.getDate();
					} else if ((fromDate && fromDate.getTime() == currentDate.getTime()) || (toDate && toDate.getTime() == currentDate.getTime())) {
						htmlCode += '<td class="calendar_selected_day_item" align="center">';
						htmlCode += '&nbsp;';
						htmlCode += '<a href="./" onclick="document.getElementById(\''+CalendarStatus+'\').value=\''+((currentDate.getMonth()+1) > 9 ? (currentDate.getMonth()+1) : '0'+(currentDate.getMonth()+1))+'/'+(currentDate.getDate() > 9 ? currentDate.getDate() : '0'+currentDate.getDate())+'/'+(currentDate.getYear() > 1900 ? currentDate.getYear() : currentDate.getYear() + 1900)+'\'; makeCalendar(); hide_calendar(1, true); return false;">';
						htmlCode += currentDate.getDate();
						htmlCode += '</a>';
					} else if (fromDate && toDate && currentDate > fromDate && currentDate < toDate) {
						htmlCode += '<td class="calendar_range_day_item" align="center">';
						htmlCode += '&nbsp;';
						htmlCode += '<a href="./" onclick="document.getElementById(\''+CalendarStatus+'\').value=\''+((currentDate.getMonth()+1) > 9 ? (currentDate.getMonth()+1) : '0'+(currentDate.getMonth()+1))+'/'+(currentDate.getDate() > 9 ? currentDate.getDate() : '0'+currentDate.getDate())+'/'+(currentDate.getYear() > 1900 ? currentDate.getYear() : currentDate.getYear() + 1900)+'\'; makeCalendar(); hide_calendar(1, true); return false;">';
						htmlCode += currentDate.getDate();
						htmlCode += '</a>';
					} else {
						htmlCode += '<td class="calendar_day_item" align="center">';
						htmlCode += '&nbsp;';
						htmlCode += '<a href="./" onclick="document.getElementById(\''+CalendarStatus+'\').value=\''+((currentDate.getMonth()+1) > 9 ? (currentDate.getMonth()+1) : '0'+(currentDate.getMonth()+1))+'/'+(currentDate.getDate() > 9 ? currentDate.getDate() : '0'+currentDate.getDate())+'/'+(currentDate.getYear() > 1900 ? currentDate.getYear() : currentDate.getYear() + 1900)+'\'; makeCalendar(); hide_calendar(1, true); return false;">';
						htmlCode += currentDate.getDate();
						htmlCode += '</a>';
					};
					currentDate.setDate(currentDate.getDate() + 1);
				} else {
					htmlCode += '<td class="calendar_day_item" align="center">';
					htmlCode += '&nbsp;';
				}

				htmlCode += '&nbsp;';
				htmlCode += '</td>';
			};

			htmlCode += '</tr>';
		};
		htmlCode += '<tr>';
		htmlCode += '<td colspan="'+dayLables.length+'" class="calendar_day_item">';
		htmlCode += '&nbsp;';
		htmlCode += '</td>';
		htmlCode += '</tr>';

		htmlCode += '</table>';
		htmlCode += '</td>';
		htmlCode += '</tr>';

		return htmlCode;
	}

	function makeCalendar() {
		positionMark = document.getElementById(CalendarStatus);
		if (!positionMark) {
			return;
		};

		CalendarDIV.innerHTML = '';

		prarent = 0;
		own = positionMark;
		for (o in own) {
			if(o.indexOf("offsetParent") != -1){
				prarent = 1;
			};
		}
		_left = own.offsetLeft;
		_top = own.offsetTop;
		while (prarent > 0)
		{
			own = own.offsetParent;
			prarent = 0;
			for (o in own) {
				if(o.indexOf("offsetParent") != -1){
					prarent = 1;
				};
			};
			if(prarent > 0){
				_left = _left + own.offsetLeft;
				_top = _top + own.offsetTop;
			};
		};

		CalendarDIV.style.visibility = 'hidden';
		CalendarDIV.style.position = 'absolute';
		CalendarDIV.style.top = 0;
		CalendarDIV.style.left = 0;


		htmlCode = '<table width="200" boreder="0" celspacing="0" celspadding="0" class="calendar_main_table">';

		htmlCode += '<tr>';
		htmlCode += '<td class="calendar_main_label">';
		htmlCode += 'Select a Date:';
		htmlCode += '</td>';
		htmlCode += '<td align="right">';
		htmlCode += '<a href="./" onclick="close_calendar(); return false;" class="calendar_nav_link">Close</a>'
		htmlCode += '</td>';
		htmlCode += '</tr>';

		htmlCode += '<tr>';
		htmlCode += '<td colspan="2" align="center" class="calendar_main_label">';
		htmlCode += '&nbsp;';
		if (CalendarCurrentMonth > 0) {
			htmlCode += '<a href="./" onclick="CalendarCurrentMonth--;makeCalendar(); return false;" class="calendar_nav_link">previous month</a>';
		};
		htmlCode += '&nbsp;';
		htmlCode += '</td>';
		htmlCode += '</tr>';

		htmlCode += makeMonth(CalendarCurrentMonth);

		htmlCode += '<tr>';
		htmlCode += '<td colspan="2" align="center" class="calendar_main_label">';
		htmlCode += '&nbsp;';
		htmlCode += '</td>';
		htmlCode += '</tr>';

		htmlCode += makeMonth(CalendarCurrentMonth+1);

		htmlCode += '<tr>';
		htmlCode += '<td colspan="2" align="center" class="calendar_main_label">';
		htmlCode += '&nbsp;';
		if (CalendarCurrentMonth < 10) {
			htmlCode += '<a href="./" onclick="CalendarCurrentMonth++;makeCalendar(); return false;" class="calendar_nav_link">next month</a>';
		};
		htmlCode += '&nbsp;';
		htmlCode += '</td>';
		htmlCode += '</tr>';

		htmlCode += '</table>';

		CalendarDIV.innerHTML = htmlCode;

		max_x = document.body.clientWidth;
		max_y = document.body.clientHeight;

		_x = _left + positionMark.offsetWidth + 10;
		_y = _top - (CalendarDIV.offsetHeight / 2);

		if (_x < 5) {_x = 5};
		if (_y < 5) {_y = 5};

		if ((_x + CalendarDIV.offsetWidth + 5) > max_x) {_x = max_x - CalendarDIV.offsetWidth - 5;};
		if ((_y + CalendarDIV.offsetHeight + 5) > max_y) {_y = max_y - CalendarDIV.offsetHeight - 5;};

		if (_x <= _left && (_x + CalendarDIV.offsetWidth) >= _left) {
			_x = _left - CalendarDIV.offsetWidth - 5;
		};
		if (_x < 5) {_x = 5};

		CalendarDIV.style.top = _y;
		CalendarDIV.style.left = _x;

		CalendarDIV.style.visibility = 'visible';

		//alert(now.toGMTString());
		//obj = now;
		//for (o in obj) {
		//	alert(o+'='+obj[o]());
		//};
	};

	function show_calendar(save_to, start_date, end_date, container_id) {
		clearTimeout(hideCalendar);

		if ((save_to.id == '') || (save_to.id == undefined)) {
			alert('Can not find id of store object');
			return;
		};

		if (CalendarStatus == save_to.id) {
			return;
		};

		CalendarStartDate = document.getElementById(start_date);
		if (!CalendarStartDate) {
			alert('Can not find start date object');
			return;
		};

		CalendarEndDate = document.getElementById(end_date);
		if (!CalendarEndDate) {
			alert('Can not find end date object');
			return;
		};

		CalendarDIV = document.getElementById(container_id);
		if (!CalendarDIV) {
			alert('Can not find calendar container');
			return;
		};

		CalendarStatus = save_to.id;
		CalendarCurrentMonth = 0;

		hasFocusCalendar = false;
		CalendarDIV.onmouseout = calendar_onmouseout;
		CalendarDIV.onmouseover = calendar_onmouseover;

		makeCalendar();

		showCalendar = 0;
		setTimeout('showCalendar = 1;', 200);
	};

	function calendar_onmouseout() {
		hasFocusCalendar = false;
	};

	function calendar_onmouseover() {
		hasFocusCalendar = true;
	};

	function close_calendar() {
		if (CalendarStatus != '' && CalendarDIV) {
			CalendarStatus = '';
			CalendarDIV.style.visibility = 'hidden';
			showCalendar = 0;
		};
	};

	function hide_calendar(a, notCheckFocus) {
		if ((showCalendar > 0) && (notCheckFocus || !hasFocusCalendar)) {
			hideCalendar = setTimeout('close_calendar();', 200);
		};
	};

window.onclick=hide_calendar;
window.document.onclick=hide_calendar;
