﻿//2006-8-10 21:27:30
//记录控件位置
function Pos()
{
	this.x = 0;
	this.y = 0;
	this.offsetLeft = 0;
	this.offsetTop = 0;
	this.offsetHeight = 0;
	this.offsetWidth = 0;
}

//2006-8-10 21:27:27
//获取控件相对于文档的座标以及相对于其父对象的座标, 返回一个 Pos 对象的实例
function GetElementPos(obj)
{
	var pos = null;
	if (obj != null)
	{
		pos = new Pos();
		var element = obj;
		pos.offsetLeft = element.offsetLeft;
		pos.offsetTop = element.offsetTop;
		pos.offsetHeight = element.offsetHeight;
		pos.offsetWidth = element.offsetWidth;
		while(element)
		{
			//alert(element.outerHTML + "\n" + element.offsetTop);
			pos.x += parseInt(element.offsetLeft);
			pos.y += parseInt(element.offsetTop);
			element = element.parentElement;
		}
		//alert("x:" + pos.x + "\ny:" + pos.y + "\noffsetLeft:" + pos.offsetLeft + "\noffsetTop:" + pos.offsetTop + "\noffsetHeight:" + pos.offsetHeight + "\noffsetWidth:" + pos.offsetWidth);
	}
	return pos;
}

//2006-8-10 21:27:24
//将一个隐藏的DIV作为弹出信息显示出来
function Popup()
{
	this.previous = null;
	this.Show = function (itemID)
	{
		this.Close();
		var item = document.getElementById(itemID);
		if (item != null)
		{
			item.style.display = item.style.display == 'block' ? 'none' : 'block';
			pos = new Pos();
			pos.x = event.x;
			pos.y = event.y;
			item.style.left = pos.x;
			item.style.top = pos.y;
			this.previous = item;
		}
	}

	this.Close = function()
	{
		if (this.previous != null)
		{
			this.previous.style.display = 'none';
		}
	}
}

function Calendar(id, date)
{
	this.id = id;
	this.date = date;
	this.selectedYear = this.date.getYear();
	this.selectedMonth = this.date.getMonth();
	this.selectedMonthDayNum = 0;
	this.yearSelectLowBound = 2005;
	this.yearSelectUpperBound = 2008;
	this.dayNameStyle = 0;
		this.dayName = new Array(2);
		this.dayName[0] = new Array('日', '一', '二', '三', '四', '五', '六');
		this.dayName[1] = new Array('周日', '周一', '周二', '周三', '周四', '周五', '周六');
	this.dayOrderStyle = 0;
	this.dayOrder = new Array(2);
		this.dayOrder[0] = new Array(0, 1, 2, 3, 4, 5, 6);
		this.dayOrder[1] = new Array(6, 0, 1, 2, 3, 4, 5);
		this.dayOrder[2] = new Array(1, 2, 3, 4, 5, 6, 0);
	this.days = null;

	//
	this.preRenderDay = function(cell, date, weekIndex, dayIndex)
	{
		return true;
	}

	this.aftRenderDay = function(cell, date, weekIndex, dayIndex)
	{
		if (this.showOtherMonthDay || !this.isOtherMonthDay(date))
		{
			var controlID = this.id;
			cell.attachEvent("onclick", function(){ eval(controlID + ".selectDate(" + controlID + ".days[" + (weekIndex * 7 + dayIndex) + "])"); });
		}
	}
	
	this.onRenderDay = function(cell, date, weekIndex, dayIndex)
	{
		var dateC = document.createElement("span");
		if (this.dayTextClassName != null) dateC.className = this.dayTextClassName;
		dateC.innerText = this.days[weekIndex * 7 + dayIndex].getDate();
		cell.appendChild(dateC);
	}

	//样式
	this.dayClassName = "day";
	this.todayClassName = "today";
	this.otherMonthDayClassName = "otherMonthDay";
	this.showOtherMonthDay = true;
	this.weekEndClassName = "weekEnd";
	
	this.dayTextClassName = null; //日期单元格中的第一个包含当天日期的控的件样式
	this.dayEventClassName = null;
	
	//
	//主表格
	this.calendarTable = null;
	this.calendarTableID = this.id + "_calendarTable";
		//头行
		this.calendarHeadTableRow = null;
		this.calendarHeadTableRowID = this.id + "_calendarHeadTableRow";
			//头行单元格
			this.calendarHeadTableRowCell = null;
			this.calendarHeadTableRowCellID = this.id + "_calendarHeadTableRowCell";
				//头表格
				this.headTable = null;
				this.headTableID = this.id + "_headTable";
					//头表格唯一行
					this.headTableRow = null;
					this.headTableRowID = this.id + "_headTableRow";
					//头表格唯一行中的,上月,当前月
					this.headTableRowCellPrevMonth = null;
					this.headTableRowCellPrevMonthID = this.id + "_headTableRowCellPrevMonth";
					this.headTableRowCellCurrentMonth = null;
					this.headTableRowCellCurrentMonthID = this.id + "_headTableRowCellCurrentMonth";
						//年
						this.yearSelect = null;
						this.yearSelectID = this.id + "_yearSelect";
						//月
						this.monthSelect = null;
						this.monthSelectID = this.id + "_monthSelect";
					this.headTableRowCellNextMonth = null;
					this.headTableRowCellNextMonthID = this.id + "_headTableRowCellNextMonth";
		this.calendarRowDayName = null;
		this.calendarRowDayNameID = this.id + "_calendarRowDayName";
		
		this.calendarRowsWeek = null;
		this.calendarRowsWeekID = this.id + "_calendarRowsWeek";

	this.init = function(obj)
	{
		this.renderFrameWork();
		this.renderHead();
		this.renderDayName();
		this.renderWeek();
		if (obj == null)
		{
			document.body.appendChild(this.calendarTable);
		}
		else
		{
			obj.appendChild(this.calendarTable);
		}
	}

	this.renderFrameWork = function()
	{
		//主表格初始化
		if (this.calendarTable != null)
		{
			this.calendarTable.removeNode(true);
			this.calendarTable = null;
		}
		this.calendarTable = document.createElement("table");
		this.calendarTable.id = this.calendarTableID;
		//补充样式
		this.calendarTable.cellSpacing = 1;
		this.calendarTable.cellPadding = 0;
	}

	this.renderHead = function()
	{
		if (this.calendarTable != null && this.calendarHeadTableRow != null)
		{
			this.calendarHeadTableRow.removeNode(true);
			this.calendarHeadTableRow = null;
		}
		//头行初始化
		this.calendarHeadTableRow = this.calendarTable.insertRow(this.calendarTable.rows.length);
		this.calendarHeadTableRow.id = this.calendarHeadTableRowID;
			//头行单元格初始化
			this.calendarHeadTableRowCell = this.calendarHeadTableRow.insertCell(this.calendarHeadTableRow.cells.length);
			this.calendarHeadTableRowCellID =  this.calendarHeadTableRowCellID;
			this.calendarHeadTableRowCell.colSpan = 7;

		var controlID = this.id;
		//头表格初始化
		this.headTable = document.createElement("table");
		this.headTable.id = this.headTableID;
			//头表格唯一行初始化
			this.headTableRow = this.headTable.insertRow(this.headTable.rows.length);
			this.headTableRow.id = this.headTableRowID;
				//头表格唯一行中的,上月,当前月,下月初始化
				this.headTableRowCellPrevMonth = this.headTableRow.insertCell(this.headTableRow.cells.length);
				this.headTableRowCellPrevMonth.id = this.headTableRowCellPrevMonthID;
					this.headTableRowCellPrevMonth.innerHTML = "←";
					this.headTableRowCellPrevMonth.attachEvent("onclick", function(){eval(controlID + ".movePrevMonth()")}); 
				this.headTableRowCellCurrentMonth = this.headTableRow.insertCell(this.headTableRow.cells.length);
				this.headTableRowCellCurrentMonth.id = this.headTableRowCellCurrentMonthID;
					//年
					this.yearSelect = document.createElement("select");
					this.yearSelect.id = this.yearSelectID;
					for(var i = this.yearSelectLowBound; i <= this.yearSelectUpperBound; i++)
					{
						this.yearSelect.options[this.yearSelect.options.length] = new Option(i + "年", i);
						if (this.yearSelect.options[this.yearSelect.options.length-1].value == this.selectedYear)
						{
							this.yearSelect.options[this.yearSelect.options.length-1].selected = true;
						}
					}
					this.yearSelect.attachEvent("onchange", function(){ eval(controlID + ".getSelectYear()") });
					this.headTableRowCellCurrentMonth.appendChild(this.yearSelect);
					//月
					this.monthSelect = document.createElement("select");
					this.monthSelect.id  = this.monthSelectID;
					for(var i = 0; i < 12; i++)
					{
						this.monthSelect.options[this.monthSelect.options.length] = new Option((i + 1) + "月", i);
						if (this.monthSelect.options[this.monthSelect.options.length - 1].value == this.selectedMonth)
						{
							this.monthSelect.options[this.monthSelect.options.length - 1].selected = true;
						}
					}
					this.monthSelect.attachEvent("onchange", function(){ eval(controlID + ".getSelectMonth()") });
					this.headTableRowCellCurrentMonth.appendChild(this.monthSelect);
				this.headTableRowCellNextMonth = this.headTableRow.insertCell(this.headTableRow.cells.length);
				this.headTableRowCellNextMonth.id = this.headTableRowCellNextMonthID;
					this.headTableRowCellNextMonth.innerHTML = "→";
					this.headTableRowCellNextMonth.attachEvent("onclick", function(){eval(controlID + ".moveNextMonth()")}); 
				this.calendarHeadTableRowCell.appendChild(this.headTable);
		//补充样式
		this.calendarHeadTableRowCell.className = "calendarHeadTableRowCell";
		this.headTableRowCellPrevMonth.className = "headTableRowCellPrevMonth";
		this.headTableRowCellNextMonth.className = "headTableRowCellNextMonth";
		this.headTableRowCellCurrentMonth.className = "headTableRowCellCurrentMonth";
		this.yearSelect.className = "yearSelect";
		this.monthSelect.className = "monthSelect";
		var sp1 = this.headTableRow.insertCell(1);
		sp1.style.width = "1px";
		sp1.style.backgroundColor  = "white";
		var sp2 = this.headTableRow.insertCell(3);
		sp2.style.width = "1px";
		sp2.style.backgroundColor  = "white";
		this.headTable.className = "headTable";
		this.headTable.cellSpacing = 0;
		this.headTable.cellPadding = 0
	}

	this.renderDayName = function()
	{
		if (this.calendarTable != null && this.calendarTable.rows != null && this.calendarTable.rows.length > 1)
		{
			for(var i = 1; i < this.calendarTable.rows.length; i++)
			{
				this.calendarTable.rows[i].removeNode(true);
			}
		}
		if (this.calendarRowDayName != null)
		{
			this.calendarRowDayName.removeNode(true);
			alert("错误!检查 if (this.calendarRowDayName != null)");
		}

		this.calendarRowDayName = this.calendarTable.insertRow(this.calendarTable.rows.length);
		this.calendarRowDayName.id = this.calendarRowDayNameID;
		//条件填充日期名
		for(var i = 0; i < 7; i++)
		{
			var calendarRowDayName = this.calendarRowDayName.insertCell(this.calendarRowDayName.cells.length);
			calendarRowDayName.id = this.id + "_calendarRowDayName" + "_" +  (this.dayOrder[this.dayOrderStyle][i]); //this.dayName[this.dayNameStyle][this.dayOrder[this.dayOrderStyle]]
			/*
			if (this.weekTableRowDayNameClassName != null)
			{
				weekTableRowCellDayName.className = this.weekTableRowDayNameClassName;
			} */
			//alert((this.dayOrder[this.dayOrderStyle][i] - 1) + " " + this.dayName[this.dayNameStyle][this.dayOrder[this.dayOrderStyle][i]-1] );
			calendarRowDayName.innerHTML = this.dayName[this.dayNameStyle][this.dayOrder[this.dayOrderStyle][i]];
			
			//补充样式
			calendarRowDayName.className = "calendarRowDayName";
		}		
	}

	this.renderWeek = function()
	{
		
		if (this.calendarTable != null && this.calendarTable.rows != null)
		{
			if (this.calendarRowsWeek != null && this.calendarRowsWeek.length > 0)
			{
				for(var i = 0; i < this.calendarRowsWeek.length; i++)
				{
					this.calendarRowsWeek[i].removeNode(true);
				}
				this.calendarRowsWeek = null;
			}
		}
		
		this.initCalendarMonthDays();
		var weeks = this.days.length / 7;
		this.calendarRowsWeek = new Array(weeks);
		for(var weekIndex = 0; weekIndex < this.calendarRowsWeek.length; weekIndex++)
		{
			this.calendarRowsWeek[weekIndex] = this.calendarTable.insertRow(this.calendarTable.rows.length);
			//alert("w:" + weekIndex);
			for(var dayIndex = 0; dayIndex < 7; dayIndex++)
			{
				this.renderDay(weekIndex, dayIndex);
				//alert("d" + dayIndex);
				//var cell = this.calendarRowsWeek[weekIndex].insertCell(this.calendarRowsWeek[weekIndex].cells.length);
				//cell.innerText = this.days[weekIndex * 7 + dayIndex].getDate();
			}
		}
	}

	this.renderDay = function(weekIndex, dayIndex)
	{
		//alert(weekIndex);
		var cell = this.calendarRowsWeek[weekIndex].insertCell(this.calendarRowsWeek[weekIndex].cells.length);
		var date = this.days[weekIndex * 7 + dayIndex];
		var render = this.preRenderDay(cell, date, weekIndex, dayIndex);
		if (render == true)
		{
			if (this.showOtherMonthDay || !this.isOtherMonthDay(date))
			{
				this.onRenderDay(cell, date, weekIndex, dayIndex);
			}
			if (this.dayClassName != null)
			{
				cell.className = this.dayClassName;
			}
			if (this.isWeekEnd(date) && this.weekEndClassName != null)
			{
				cell.className = this.weekEndClassName;
			}
			if (this.isOtherMonthDay(date) && this.otherMonthDayClassName != null)
			{
				cell.className = this.otherMonthDayClassName;
			}
			if (this.isToday(date) && this.todayClassName != null)
			{
				cell.className = this.todayClassName;
			}
		}
		this.aftRenderDay(cell, date, weekIndex, dayIndex);
	}

	this.getSelectYear = function()
	{
		this.selectedYear = this.yearSelect.value;
		this.selectedMonth = this.monthSelect.value;
		this.renderWeek();
		return this.selectedYear;
	}
	
	this.getSelectMonth = function()
	{
		this.selectedYear = this.yearSelect.value;
		this.selectedMonth = this.monthSelect.value;
		this.renderWeek();
		return this.selectedMonth;
	}
	
	this.movePrevMonth = function()
	{
		//alert("selectedYear:"+ this.selectedYear +" | " + "selectedMonth:" + this.selectedMonth);
		var d = new Date(this.selectedYear, this.selectedMonth);
		//alert(d.toLocaleString());
		d.setMonth(this.selectedMonth - 1);
		//alert(d.toLocaleString());
		if (d.getYear() < this.yearSelectLowBound)
		{
			alert('您可以选择的最小年份为:' + this.yearSelectLowBound + "年");
		}
		else
		{
			this.selectedYear = d.getYear();
			this.selectedMonth = d.getMonth();
			this.syncYmSelect();
		}
		this.renderWeek();
	}
	
	this.moveNextMonth = function()
	{
		//alert("selectedYear:"+ this.selectedYear +" | " + "selectedMonth:" + this.selectedMonth);
		var d = new Date(this.selectedYear, this.selectedMonth);
		d.setMonth(parseInt(this.selectedMonth) + 1);
		if (d.getYear() > this.yearSelectUpperBound)
		{
			alert('您可以选择的最大年份为:' + this.yearSelectUpperBound + "年");
		}
		else
		{
			this.selectedYear = d.getYear();
			this.selectedMonth = d.getMonth();
			this.syncYmSelect();
		}
		this.renderWeek();
	}

	this.syncYmSelect = function()
	{
		var year = this.selectedYear;
		var month = this.selectedMonth;
		for(var i = 0; i < this.yearSelect.options.length; i++)
		{
			this.yearSelect.options[i].selected = false;
			if (this.yearSelect.options[i].value == year)
			{
				this.yearSelect.options[i].selected = true;
			}
		}
		for(var i = 0; i < this.monthSelect.options.length; i++)
		{
			this.monthSelect.options[i].selected = false; 
			if (this.monthSelect.options[i].value == month)
			{
				this.monthSelect.options[i].selected = true;
			}
		}
	}

	this.setDayNameClass = function(className, index)
	{
		if (this.calendarRowDayName != null && this.calendarRowDayName.cells != null && this.calendarRowDayName.cells.length > 0)
		{
			var setAll = index == null ? true : false;
			for(var i = 0; i < 7; i++)
			{
				if (i == index || setAll)
				{
					this.calendarRowDayName.cells[i].className = className;
				}
			}
		}
		else
		{
			alert('无法对 calendarRowDayName 中的日期名(cells)进行设置,可能是由于它或它的子项还没有创建');
		}
	}

	this.initCalendarMonthDays = function()
	{
		var currentMonth = new Date(this.selectedYear, this.selectedMonth);
		//var currentMonth2 = new Date(this.selectedYear, this.selectedMonth, 0);	//当为零时,可以获取当月天数
		var nextMonth = new Date(this.selectedYear, parseInt(this.selectedMonth) + 1);
		var prevMonth = new Date(this.selectedYear, parseInt(this.selectedMonth) - 1);
		this.selectedMonthDayNum = (nextMonth - currentMonth)  / 1000 / 3600 / 24;//currentMonth2.getDate(); //
		var currentMonthDays = new Array(this.selectedMonthDayNum);
		for(var i = 0; i < this.selectedMonthDayNum; i++)
		{
			currentMonthDays[i]  = new Date(currentMonth.getYear(), currentMonth.getMonth(), i + 1);
		}
		//
		var monthBeginAt = this.getDayPosition(new Date(currentMonth.getYear(), currentMonth.getMonth(), 1));	//当前选择月的第一天处在 this.dayOrder[x] 数组的第几个索引位上
		var monthEndAt = this.getDayPosition(new Date(currentMonth.getYear(), currentMonth.getMonth(), this.selectedMonthDayNum));	//当前选择月的最后一天处在 this.dayOrder[x] 数组的第几个索引位上
		var paddingPrevMonthDays = new Array(monthBeginAt);	//根据索引位置, 设置左边还需要填充几个日期
		var paddingNextMonthDays = new Array(6 - monthEndAt);	//右边的填充日期数量, this.dayOrder[x] 数组为包含 7 个元素 0 - 6 的索引.
		//alert("paddingPrevMonth.length:" + paddingPrevMonth.length);
		//alert("paddingNextMonth.length:" + paddingNextMonth.length);
		if (paddingPrevMonthDays.length > 0)
		{
			for(var i = 0; i < paddingPrevMonthDays.length; i++)
			{
				 paddingPrevMonthDays[paddingPrevMonthDays.length - i - 1] = new Date(currentMonth.getYear(), currentMonth.getMonth(), 0-i);
			}
		}
		if (paddingNextMonthDays.length >0)
		{
			for(var i = 0; i < paddingNextMonthDays.length; i++)
			{
				 paddingNextMonthDays[i] = new Date(currentMonth.getYear(), currentMonth.getMonth() + 1, i+1);
			}
		}
		var currentCalendarDays = paddingPrevMonthDays.concat(currentMonthDays, paddingNextMonthDays);
		this.days = currentCalendarDays;
		//alert(this.days);
		//alert(monthBeginAt + "," + monthEndAt);
		//alert(paddingPrevMonth.toLocaleString());
		//alert(paddingNextMonth.toLocaleString());
	}

	this.getDayPosition = function(date)
	{
		for(var i = 0; i < 7; i++)
		{
			//alert(this.dayOrder[this.dayOrderStyle][i] + " " + dayNumber);
			//alert(this.dayOrder[this.dayOrderStyle][i] + "=" + date.getDay());
			if (this.dayOrder[this.dayOrderStyle][i] == (date.getDay()))
			{
				return i;
			}
		}
		return null;
	}

	this.selectDate = function(date)
	{
		if (date.getYear() < this.yearSelectLowBound || date.getYear() > this.yearSelectUpperBound)
		{
			alert('不能选择这个日期,它超出了允许使用的范围.');
			return;
		}
		this.date = date;
		this.selectedYear = this.date.getYear();
		this.selectedMonth = this.date.getMonth();
		for(var i = 0; i < this.yearSelect.options.length; i++)
		{
			this.yearSelect.options[i].selected = false;
			if (this.yearSelect.options[i].value == this.selectedYear)
			{
				this.yearSelect.options[i].selected = true;
			}
		}
		for(var i = 0; i < this.monthSelect.options.length; i++)
		{
			this.monthSelect.options[i].selected = false;
			if (this.monthSelect.options[i].value == this.selectedMonth)
			{
				this.monthSelect.options[i].selected = true;
			}
		}
		this.renderWeek();
	}

	this.isToday = function(date)
	{
		var currentDate = this.date;
		return date.getYear() == currentDate.getYear() && date.getMonth() == currentDate.getMonth() && date.getDate() == currentDate.getDate() ? true : false;
	}

	this.isOtherMonthDay = function(date)
	{
		var currentDate = new Date(this.selectedYear, this.selectedMonth);
		//alert(currentDate.getYear() + "," + currentDate.getMonth() + " || " + date.getYear() + "," + date.getMonth() + " : " );
		return date.getYear() != currentDate.getYear() || date.getMonth() != currentDate.getMonth() ? true : false;
	}

	this.isWeekEnd = function(date)
	{
		return date.getDay() == 0 || date.getDay() == 6 ? true : false;
	}
	
	this.dateToString = function(date, format)
	{
		var dateString = format;
		dateString = dateString.replace("yyyy", date.getYear());
		dateString = dateString.replace("mm", date.getMonth()+1);
		dateString = dateString.replace("dd", date.getDate());
		return dateString;
	}
	
	//应用1,为文本框绑定
	this.bindToTextControl = function(obj)
	{
		var date;
		if (obj != null)
		{
			try
			{
				date = new Date(Date.parse(obj.value));
			}
			catch(e)
			{
			}
		}
		
		if (isNaN(date) || date == null) { date = new Date(); }
		this.selectDate(date);
		
		var pos = GetElementPos(obj);
		
		this.aftRenderDay = function(cell, date, weekIndex, dayIndex)
		{
			if (this.showOtherMonthDay || !this.isOtherMonthDay(date))
			{
				var controlID = this.id;
				var cmd = "$1.selectDate($1.days[$2]);document.getElementById('$3').value=$1.dateToString($1.date,'mm-dd-yyyy');$1.close();";
				var p = /\$1/g;
				cmd = cmd.replace(p, this.id);
				p = /\$2/g;
				cmd = cmd.replace(p, (weekIndex * 7 + dayIndex));
				p = /\$3/g;
				cmd = cmd.replace(p, obj.id);
				cell.attachEvent("onclick", function(){ eval(cmd); });
			}
			return true;
		}
		this.renderWeek();
	}
	
	//设置跟随显示的控件
	this.followControl = function(obj, x, y)
	{
		var pos = GetElementPos(obj);
		this.calendarTable.style.position = 'absolute';
		this.calendarTable.style.display = this.calendarTable.style.display == 'block' ? 'none' : 'block';
		this.calendarTable.style.left = x == null ? pos.x : x;
		this.calendarTable.style.top = y == null ? pos.y : y;
		
	}
	
	this.close = function()
	{
		if (this.calendarTable.style.position.toLowerCase() == 'absolute')
		{
			this.calendarTable.style.display = 'none';
		}
	}
}
////



////
function CalendarView(id, date)
{
	this.id = id;
	this.date = date;
	this.selectedYear = this.date.getYear();
	this.selectedMonth = this.date.getMonth();
	this.selectedMonthDayNum = 0;
	this.yearSelectLowBound = 2005;
	this.yearSelectUpperBound = 2008;
	this.dayNameStyle = 0;
		this.dayName = new Array(2);
		this.dayName[0] = new Array('日', '一', '二', '三', '四', '五', '六');
		this.dayName[1] = new Array('周日', '周一', '周二', '周三', '周四', '周五', '周六');
	this.dayOrderStyle = 0;
	this.dayOrder = new Array(2);
		this.dayOrder[0] = new Array(0, 1, 2, 3, 4, 5, 6);
		this.dayOrder[1] = new Array(6, 0, 1, 2, 3, 4, 5);
		this.dayOrder[2] = new Array(1, 2, 3, 4, 5, 6, 0);
	this.days = null;
	this.events = null;
	
	this.bindPrevMonthEvent = function()
	{
		var controlID = this.id;
		this.headTableRowCellPrevMonth.attachEvent("onclick", function(){eval(controlID + ".movePrevMonth()")}); 
	}
	
	this.bindNextMonthEvent = function()
	{
		var controlID = this.id;
		this.headTableRowCellNextMonth.attachEvent("onclick", function(){eval(controlID + ".moveNextMonth()")}); 
	}
	

	//
	this.preRenderDay = function(cell, date, weekIndex, dayIndex)
	{
		return true;
	}

	this.aftRenderDay = function(cell, date, weekIndex, dayIndex)
	{
		if (this.showOtherMonthDay || !this.isOtherMonthDay(date))
		{
			var controlID = this.id;
			cell.attachEvent("onclick", function(){ eval(controlID + ".selectDate(" + controlID + ".days[" + (weekIndex * 7 + dayIndex) + "])"); });
		}
	}
	
	this.onRenderDay = function(cell, date, weekIndex, dayIndex)
	{
		var dateC = document.createElement("span");
		if (this.dayTextClassName != null) dateC.className = this.dayTextClassName;
		dateC.innerText = this.days[weekIndex * 7 + dayIndex].getDate();
		cell.appendChild(dateC);
		this.onRenderDayEvents(cell, date, weekIndex, dayIndex);
	}
	
	this.onRenderDayEvents = function(cell, date, weekIndex, dayIndex)
	{
		if (this.events != null)
		{
			var dateStr = this.dateToString(date, 'mm-dd-yyyy');
			var dayEvents = this.events[dateStr];
			if (dayEvents != null && dayEvents.length > 0)
			{
				for(var i = 0; i < dayEvents.length; i++)
				{
					cell.innerHTML += dayEvents[i];
				}
			}
			cell.title = '双击鼠标左键添加当日的安排';
			cell.attachEvent('ondblclick', function(){location = 'CalendarEditor.aspx?date=' + dateStr;});
		}
	}
	
	this.addEvent = function(dateString, html)
	{
		if (this.events == null)
		{
			this.events = new Array();
		}
		if (this.events[dateString] == null)
		{
			this.events[dateString] = new Array();
		}
		this.events[dateString][this.events[dateString].length] = html;
	}

	//样式
	this.dayClassName = "day";
	this.todayClassName = "today";
	this.otherMonthDayClassName = "otherMonthDay";
	this.showOtherMonthDay = true;
	this.weekEndClassName = "weekEnd";
	
	this.dayTextClassName = null; //日期单元格中的第一个包含当天日期的控的件样式
	this.dayEventClassName = null;
	
	//
	//主表格
	this.calendarTable = null;
	this.calendarTableID = this.id + "_calendarTable";
		//头行
		this.calendarHeadTableRow = null;
		this.calendarHeadTableRowID = this.id + "_calendarHeadTableRow";
			//头行单元格
			this.calendarHeadTableRowCell = null;
			this.calendarHeadTableRowCellID = this.id + "_calendarHeadTableRowCell";
				//头表格
				this.headTable = null;
				this.headTableID = this.id + "_headTable";
					//头表格唯一行
					this.headTableRow = null;
					this.headTableRowID = this.id + "_headTableRow";
					//头表格唯一行中的,上月,当前月
					this.headTableRowCellPrevMonth = null;
					this.headTableRowCellPrevMonthID = this.id + "_headTableRowCellPrevMonth";
					this.headTableRowCellCurrentMonth = null;
					this.headTableRowCellCurrentMonthID = this.id + "_headTableRowCellCurrentMonth";
						//年
						this.yearSelect = null;
						this.yearSelectID = this.id + "_yearSelect";
						//月
						this.monthSelect = null;
						this.monthSelectID = this.id + "_monthSelect";
					this.headTableRowCellNextMonth = null;
					this.headTableRowCellNextMonthID = this.id + "_headTableRowCellNextMonth";
		this.calendarRowDayName = null;
		this.calendarRowDayNameID = this.id + "_calendarRowDayName";
		
		this.calendarRowsWeek = null;
		this.calendarRowsWeekID = this.id + "_calendarRowsWeek";

	this.init = function(obj)
	{
		this.renderFrameWork();
		this.renderHead();
		this.renderDayName();
		this.renderWeek();
		if (obj == null)
		{
			document.body.appendChild(this.calendarTable);
		}
		else
		{
			obj.appendChild(this.calendarTable);
		}
	}

	this.renderFrameWork = function()
	{
		//主表格初始化
		if (this.calendarTable != null)
		{
			this.calendarTable.removeNode(true);
			this.calendarTable = null;
		}
		this.calendarTable = document.createElement("table");
		this.calendarTable.id = this.calendarTableID;
		//补充样式
		this.calendarTable.cellSpacing = 1;
		this.calendarTable.cellPadding = 0;
	}

	this.renderHead = function()
	{
		if (this.calendarTable != null && this.calendarHeadTableRow != null)
		{
			this.calendarHeadTableRow.removeNode(true);
			this.calendarHeadTableRow = null;
		}
		//头行初始化
		this.calendarHeadTableRow = this.calendarTable.insertRow(this.calendarTable.rows.length);
		this.calendarHeadTableRow.id = this.calendarHeadTableRowID;
			//头行单元格初始化
			this.calendarHeadTableRowCell = this.calendarHeadTableRow.insertCell(this.calendarHeadTableRow.cells.length);
			this.calendarHeadTableRowCellID =  this.calendarHeadTableRowCellID;
			this.calendarHeadTableRowCell.colSpan = 7;

		var controlID = this.id;
		//头表格初始化
		this.headTable = document.createElement("table");
		this.headTable.id = this.headTableID;
			//头表格唯一行初始化
			this.headTableRow = this.headTable.insertRow(this.headTable.rows.length);
			this.headTableRow.id = this.headTableRowID;
				//头表格唯一行中的,上月,当前月,下月初始化
				this.headTableRowCellPrevMonth = this.headTableRow.insertCell(this.headTableRow.cells.length);
				this.headTableRowCellPrevMonth.id = this.headTableRowCellPrevMonthID;
					this.headTableRowCellPrevMonth.innerHTML = "←";
					this.bindPrevMonthEvent();
					
				this.headTableRowCellCurrentMonth = this.headTableRow.insertCell(this.headTableRow.cells.length);
				this.headTableRowCellCurrentMonth.id = this.headTableRowCellCurrentMonthID;
					//年
					this.yearSelect = document.createElement("select");
					this.yearSelect.id = this.yearSelectID;
					for(var i = this.yearSelectLowBound; i <= this.yearSelectUpperBound; i++)
					{
						this.yearSelect.options[this.yearSelect.options.length] = new Option(i + "年", i);
						if (this.yearSelect.options[this.yearSelect.options.length-1].value == this.selectedYear)
						{
							this.yearSelect.options[this.yearSelect.options.length-1].selected = true;
						}
					}
					this.yearSelect.attachEvent("onchange", function(){ eval(controlID + ".getSelectYear()") });
					this.headTableRowCellCurrentMonth.appendChild(this.yearSelect);
					//月
					this.monthSelect = document.createElement("select");
					this.monthSelect.id  = this.monthSelectID;
					for(var i = 0; i < 12; i++)
					{
						this.monthSelect.options[this.monthSelect.options.length] = new Option((i + 1) + "月", i);
						if (this.monthSelect.options[this.monthSelect.options.length - 1].value == this.selectedMonth)
						{
							this.monthSelect.options[this.monthSelect.options.length - 1].selected = true;
						}
					}
					this.monthSelect.attachEvent("onchange", function(){ eval(controlID + ".getSelectMonth()") });
					this.headTableRowCellCurrentMonth.appendChild(this.monthSelect);
				this.headTableRowCellNextMonth = this.headTableRow.insertCell(this.headTableRow.cells.length);
				this.headTableRowCellNextMonth.id = this.headTableRowCellNextMonthID;
					this.headTableRowCellNextMonth.innerHTML = "→";
					//this.headTableRowCellNextMonth.attachEvent("onclick", function(){eval(controlID + ".moveNextMonth()")}); 
					this.bindNextMonthEvent();
				this.calendarHeadTableRowCell.appendChild(this.headTable);
		//补充样式
		this.calendarHeadTableRowCell.className = "calendarHeadTableRowCell";
		this.headTableRowCellPrevMonth.className = "headTableRowCellPrevMonth";
		this.headTableRowCellNextMonth.className = "headTableRowCellNextMonth";
		this.headTableRowCellCurrentMonth.className = "headTableRowCellCurrentMonth";
		this.yearSelect.className = "yearSelect";
		this.monthSelect.className = "monthSelect";
		var sp1 = this.headTableRow.insertCell(1);
		sp1.style.width = "1px";
		sp1.style.backgroundColor  = "white";
		var sp2 = this.headTableRow.insertCell(3);
		sp2.style.width = "1px";
		sp2.style.backgroundColor  = "white";
		this.headTable.className = "headTable";
		this.headTable.cellSpacing = 0;
		this.headTable.cellPadding = 0
	}

	this.renderDayName = function()
	{
		if (this.calendarTable != null && this.calendarTable.rows != null && this.calendarTable.rows.length > 1)
		{
			for(var i = 1; i < this.calendarTable.rows.length; i++)
			{
				this.calendarTable.rows[i].removeNode(true);
			}
		}
		if (this.calendarRowDayName != null)
		{
			this.calendarRowDayName.removeNode(true);
			alert("错误!检查 if (this.calendarRowDayName != null)");
		}

		this.calendarRowDayName = this.calendarTable.insertRow(this.calendarTable.rows.length);
		this.calendarRowDayName.id = this.calendarRowDayNameID;
		//条件填充日期名
		for(var i = 0; i < 7; i++)
		{
			var calendarRowDayName = this.calendarRowDayName.insertCell(this.calendarRowDayName.cells.length);
			calendarRowDayName.id = this.id + "_calendarRowDayName" + "_" +  (this.dayOrder[this.dayOrderStyle][i]); //this.dayName[this.dayNameStyle][this.dayOrder[this.dayOrderStyle]]
			/*
			if (this.weekTableRowDayNameClassName != null)
			{
				weekTableRowCellDayName.className = this.weekTableRowDayNameClassName;
			} */
			//alert((this.dayOrder[this.dayOrderStyle][i] - 1) + " " + this.dayName[this.dayNameStyle][this.dayOrder[this.dayOrderStyle][i]-1] );
			calendarRowDayName.innerHTML = this.dayName[this.dayNameStyle][this.dayOrder[this.dayOrderStyle][i]];
			
			//补充样式
			calendarRowDayName.className = "calendarRowDayName";
		}		
	}

	this.renderWeek = function()
	{
		
		if (this.calendarTable != null && this.calendarTable.rows != null)
		{
			if (this.calendarRowsWeek != null && this.calendarRowsWeek.length > 0)
			{
				for(var i = 0; i < this.calendarRowsWeek.length; i++)
				{
					this.calendarRowsWeek[i].removeNode(true);
				}
				this.calendarRowsWeek = null;
			}
		}
		
		this.initCalendarMonthDays();
		var weeks = this.days.length / 7;
		this.calendarRowsWeek = new Array(weeks);
		for(var weekIndex = 0; weekIndex < this.calendarRowsWeek.length; weekIndex++)
		{
			this.calendarRowsWeek[weekIndex] = this.calendarTable.insertRow(this.calendarTable.rows.length);
			//alert("w:" + weekIndex);
			for(var dayIndex = 0; dayIndex < 7; dayIndex++)
			{
				this.renderDay(weekIndex, dayIndex);
				//alert("d" + dayIndex);
				//var cell = this.calendarRowsWeek[weekIndex].insertCell(this.calendarRowsWeek[weekIndex].cells.length);
				//cell.innerText = this.days[weekIndex * 7 + dayIndex].getDate();
			}
		}
	}

	this.renderDay = function(weekIndex, dayIndex)
	{
		//alert(weekIndex);
		var cell = this.calendarRowsWeek[weekIndex].insertCell(this.calendarRowsWeek[weekIndex].cells.length);
		var date = this.days[weekIndex * 7 + dayIndex];
		var render = this.preRenderDay(cell, date, weekIndex, dayIndex);
		if (render == true)
		{
			if (this.showOtherMonthDay || !this.isOtherMonthDay(date))
			{
				this.onRenderDay(cell, date, weekIndex, dayIndex);
			}
			if (this.dayClassName != null)
			{
				cell.className = this.dayClassName;
			}
			if (this.isWeekEnd(date) && this.weekEndClassName != null)
			{
				cell.className = this.weekEndClassName;
			}
			if (this.isOtherMonthDay(date) && this.otherMonthDayClassName != null)
			{
				cell.className = this.otherMonthDayClassName;
			}
			if (this.isToday(date) && this.todayClassName != null)
			{
				cell.className = this.todayClassName;
			}
		}
		this.aftRenderDay(cell, date, weekIndex, dayIndex);
	}

	this.getSelectYear = function()
	{
		this.selectedYear = this.yearSelect.value;
		this.selectedMonth = this.monthSelect.value;
		//this.renderWeek();
		location.href = '?month=' + (parseInt(this.selectedMonth) + 1) + '-' + this.selectedYear;
		return this.selectedYear;
	}
	
	this.getSelectMonth = function()
	{
		this.selectedYear = this.yearSelect.value;
		this.selectedMonth = this.monthSelect.value;
		//this.renderWeek();
		location.href = '?month=' + (parseInt(this.selectedMonth) + 1) + '-' + this.selectedYear;
		return this.selectedMonth;
	}
	
	this.movePrevMonth = function()
	{
		//alert("selectedYear:"+ this.selectedYear +" | " + "selectedMonth:" + this.selectedMonth);
		var d = new Date(this.selectedYear, this.selectedMonth);
		//alert(d.toLocaleString());
		d.setMonth(this.selectedMonth - 1);
		//alert(d.toLocaleString());
		if (d.getYear() < this.yearSelectLowBound)
		{
			alert('您可以选择的最小年份为:' + this.yearSelectLowBound + "年");
		}
		else
		{
			this.selectedYear = d.getYear();
			this.selectedMonth = d.getMonth();
			this.syncYmSelect();
		}
		//this.renderWeek();
		location.href = '?month=' + (this.selectedMonth + 1) + '-' + this.selectedYear;
	}
	
	this.moveNextMonth = function()
	{
		//alert("selectedYear:"+ this.selectedYear +" | " + "selectedMonth:" + this.selectedMonth);
		var d = new Date(this.selectedYear, this.selectedMonth);
		d.setMonth(parseInt(this.selectedMonth) + 1);
		if (d.getYear() > this.yearSelectUpperBound)
		{
			alert('您可以选择的最大年份为:' + this.yearSelectUpperBound + "年");
		}
		else
		{
			this.selectedYear = d.getYear();
			this.selectedMonth = d.getMonth();
			this.syncYmSelect();
		}
		//this.renderWeek();
		location.href = '?month=' + (this.selectedMonth + 1) + '-' + this.selectedYear;
	}

	this.syncYmSelect = function()
	{
		var year = this.selectedYear;
		var month = this.selectedMonth;
		for(var i = 0; i < this.yearSelect.options.length; i++)
		{
			this.yearSelect.options[i].selected = false;
			if (this.yearSelect.options[i].value == year)
			{
				this.yearSelect.options[i].selected = true;
			}
		}
		for(var i = 0; i < this.monthSelect.options.length; i++)
		{
			this.monthSelect.options[i].selected = false; 
			if (this.monthSelect.options[i].value == month)
			{
				this.monthSelect.options[i].selected = true;
			}
		}
	}

	this.setDayNameClass = function(className, index)
	{
		if (this.calendarRowDayName != null && this.calendarRowDayName.cells != null && this.calendarRowDayName.cells.length > 0)
		{
			var setAll = index == null ? true : false;
			for(var i = 0; i < 7; i++)
			{
				if (i == index || setAll)
				{
					this.calendarRowDayName.cells[i].className = className;
				}
			}
		}
		else
		{
			alert('无法对 calendarRowDayName 中的日期名(cells)进行设置,可能是由于它或它的子项还没有创建');
		}
	}

	this.initCalendarMonthDays = function()
	{
		var currentMonth = new Date(this.selectedYear, this.selectedMonth);
		//var currentMonth2 = new Date(this.selectedYear, this.selectedMonth, 0);	//当为零时,可以获取当月天数
		var nextMonth = new Date(this.selectedYear, parseInt(this.selectedMonth) + 1);
		var prevMonth = new Date(this.selectedYear, parseInt(this.selectedMonth) - 1);
		this.selectedMonthDayNum = (nextMonth - currentMonth)  / 1000 / 3600 / 24;//currentMonth2.getDate(); //
		var currentMonthDays = new Array(this.selectedMonthDayNum);
		for(var i = 0; i < this.selectedMonthDayNum; i++)
		{
			currentMonthDays[i]  = new Date(currentMonth.getYear(), currentMonth.getMonth(), i + 1);
		}
		//
		var monthBeginAt = this.getDayPosition(new Date(currentMonth.getYear(), currentMonth.getMonth(), 1));	//当前选择月的第一天处在 this.dayOrder[x] 数组的第几个索引位上
		var monthEndAt = this.getDayPosition(new Date(currentMonth.getYear(), currentMonth.getMonth(), this.selectedMonthDayNum));	//当前选择月的最后一天处在 this.dayOrder[x] 数组的第几个索引位上
		var paddingPrevMonthDays = new Array(monthBeginAt);	//根据索引位置, 设置左边还需要填充几个日期
		var paddingNextMonthDays = new Array(6 - monthEndAt);	//右边的填充日期数量, this.dayOrder[x] 数组为包含 7 个元素 0 - 6 的索引.
		//alert("paddingPrevMonth.length:" + paddingPrevMonth.length);
		//alert("paddingNextMonth.length:" + paddingNextMonth.length);
		if (paddingPrevMonthDays.length > 0)
		{
			for(var i = 0; i < paddingPrevMonthDays.length; i++)
			{
				 paddingPrevMonthDays[paddingPrevMonthDays.length - i - 1] = new Date(currentMonth.getYear(), currentMonth.getMonth(), 0-i);
			}
		}
		if (paddingNextMonthDays.length >0)
		{
			for(var i = 0; i < paddingNextMonthDays.length; i++)
			{
				 paddingNextMonthDays[i] = new Date(currentMonth.getYear(), currentMonth.getMonth() + 1, i+1);
			}
		}
		var currentCalendarDays = paddingPrevMonthDays.concat(currentMonthDays, paddingNextMonthDays);
		this.days = currentCalendarDays;
		//alert(this.days);
		//alert(monthBeginAt + "," + monthEndAt);
		//alert(paddingPrevMonth.toLocaleString());
		//alert(paddingNextMonth.toLocaleString());
	}

	this.getDayPosition = function(date)
	{
		for(var i = 0; i < 7; i++)
		{
			//alert(this.dayOrder[this.dayOrderStyle][i] + " " + dayNumber);
			//alert(this.dayOrder[this.dayOrderStyle][i] + "=" + date.getDay());
			if (this.dayOrder[this.dayOrderStyle][i] == (date.getDay()))
			{
				return i;
			}
		}
		return null;
	}

	this.selectDate = function(date)
	{
		if (date.getYear() < this.yearSelectLowBound || date.getYear() > this.yearSelectUpperBound)
		{
			alert('不能选择这个日期,它超出了允许使用的范围.');
			return;
		}
		this.date = date;
		this.selectedYear = this.date.getYear();
		this.selectedMonth = this.date.getMonth();
		for(var i = 0; i < this.yearSelect.options.length; i++)
		{
			this.yearSelect.options[i].selected = false;
			if (this.yearSelect.options[i].value == this.selectedYear)
			{
				this.yearSelect.options[i].selected = true;
			}
		}
		for(var i = 0; i < this.monthSelect.options.length; i++)
		{
			this.monthSelect.options[i].selected = false;
			if (this.monthSelect.options[i].value == this.selectedMonth)
			{
				this.monthSelect.options[i].selected = true;
			}
		}
		this.renderWeek();
	}

	this.isToday = function(date)
	{
		var currentDate = this.date;
		return date.getYear() == currentDate.getYear() && date.getMonth() == currentDate.getMonth() && date.getDate() == currentDate.getDate() ? true : false;
	}

	this.isOtherMonthDay = function(date)
	{
		var currentDate = new Date(this.selectedYear, this.selectedMonth);
		//alert(currentDate.getYear() + "," + currentDate.getMonth() + " || " + date.getYear() + "," + date.getMonth() + " : " );
		return date.getYear() != currentDate.getYear() || date.getMonth() != currentDate.getMonth() ? true : false;
	}

	this.isWeekEnd = function(date)
	{
		return date.getDay() == 0 || date.getDay() == 6 ? true : false;
	}
	
	this.dateToString = function(date, format)
	{
		var dateString = format;
		dateString = dateString.replace("yyyy", date.getYear());
		dateString = dateString.replace("mm", date.getMonth()+1);
		dateString = dateString.replace("dd", date.getDate());
		return dateString;
	}
	
	//应用1,为文本框绑定
	this.bindToTextControl = function(obj)
	{
		var date;
		if (obj != null)
		{
			try
			{
				date = new Date(Date.parse(obj.value));
			}
			catch(e)
			{
			}
		}
		
		if (isNaN(date) || date == null) { date = new Date(); }
		this.selectDate(date);
		
		var pos = GetElementPos(obj);
		
		this.aftRenderDay = function(cell, date, weekIndex, dayIndex)
		{
			if (this.showOtherMonthDay || !this.isOtherMonthDay(date))
			{
				var controlID = this.id;
				var cmd = "$1.selectDate($1.days[$2]);document.getElementById('$3').value=$1.dateToString($1.date,'mm-dd-yyyy')";
				var p = /\$1/g;
				cmd = cmd.replace(p, this.id);
				p = /\$2/g;
				cmd = cmd.replace(p, (weekIndex * 7 + dayIndex));
				p = /\$3/g;
				cmd = cmd.replace(p, obj.id);
				cell.attachEvent("onclick", function(){ eval(cmd); });
			}
			return true;
		}
		this.renderWeek();
	}
	
	//设置跟随显示的控件
	this.followControl = function(obj)
	{
		alert('1');
		var pos = GetElementPos(obj);
		this.calendarTable.style.position = 'absolute';
		this.calendarTable.style.display = this.calendarTable.style.display == 'block' ? 'none' : 'block';
		this.calendarTable.style.left = pos.x;
		
	}
}
