// DHTML Calendar
// $Author: misnet $
// $Date: 2007-11-20 17:41:02 +0800 (星期二, 20 十一月 2007) $
// $Revision: 306 $

function Calendar (cname, id, date)
{
	// Used to notify the calendar that it is attached to a single html field.
	this.fallback_single = 0;
	
	// Used to notify the claendar that it is attached to 3 html fields.
	this.fallback_multi = 1;
	
	// Used to notify the calendar that it is attached to both field sets.
	this.fallback_both = 2;
	
	// Read-only calendar
	this.viewOnly = false;
	
	// Allows the user to select weekends
	this.allowWeekends = true;
	
	// Allows the user to select weekdays
	this.allowWeekdays = true;
	
	// The minimum date that the user can select (inclusive)
	this.minDate = "--";
	
	// The maximum date that the user can select (exclusive)
	this.maxDate = "--";
	
	// Allow the user to scroll dates
	this.scrolling = true;
	
	// The id of this calendar
	this.name = cname;
	
	// The first day of the week in the calendar (0-Sunday, 6-Saturday)
	this.firstDayOfWeek = 0;
	
	// Fallback method
	this.fallback = this.fallback_both;
	
	// Sets the date and strips out time information
	this.calendarDate = new Date();
	//this.calendarDate.setUTCHours(0);
	//this.calendarDate.setUTCMinutes(0);
	//this.calendarDate.setUTCSeconds(0);
	//this.calendarDate.setUTCMilliseconds(0);
	//add by wwq
	this.calendarCurrentDate = new Date();
	todayMonth = this.calendarCurrentDate.getMonth()+1;
	if(todayMonth<10)
		todayMonth = "0" + todayMonth;
	todayDay = this.calendarCurrentDate.getDate();
	if(todayDay<10)
		todayDay = "0" + todayDay;
    this.todayDateString = this.calendarCurrentDate.getFullYear() + "/" +todayMonth + "/" + todayDay;
    
	//end
	// The field id that the calendar is attached to.
	// For single input, this is used "as is". for the
	// Multi-input, it is given a suffix for _day, _month
	// and _year inputs.
	this.attachedId = id;
	
	// The left and right month control icons
	this.controlLeft = "&#171;";
	this.controlRight = "&#187;";
		
	// The left and right month control icons (when disabled)
	this.controlLeftDisabled = "";
	this.controlRightDisabled = "";
	
	// The css classes for the calendar and header
	this.calendarStyle = "myCalendar";
	this.headerStyle = "myCalendarHeader";
	this.headerCellStyle = "myCalendarWeekday";
	this.headerCellStyleLabel = "myCalendarLabel";
	//add by wwq
	this.headerControllStyle = 'myCalendarMonthArrow';
	//end
	// The css classes for the rows
	this.evenWeekStyle = "cal_evenweek";
	this.oddWeekStyle = "cal_oddweek";
	
	// The css classes for the day elements
	this.disabledDayStyle = "myCalendarDisabled";
	this.commonDayStyle = "myCalendarCommon";
	this.holidayDayStyle = "myCalendarHoliday";
	this.eventDayStyle = "myCalendarEvent";
	this.todayDayStyle = "myCalendarToday";
	
	// specifies the labels for this calendar
	this.dayLabels = new Array("Sun", "Mon", "Thu", "Wed", "Tue", "Fri", "Sat");
	this.monthLabels = new Array(
		"January", "February", "March", "April"
		, "May", "June", "July", "August"
		, "September", "October", "November", "December");
	
	// Specifies the dates of any event. The events are to be defined as arrays,
	// with element 0 being the date and element 1 being an id.
	this.eventDates = new Array();
	
	// Attach event handlers to any fallback fields.
	if (this.viewOnly == false) {
	
		setFieldValue(this.attachedId, this.calendarDate);
		if ((this.fallback = this.fallback_both) || (this.fallback = this.fallback_single)) {
			eval("document.getElementById(\"" + this.attachedId + "\").onchange = function () {updateFromSingle("+this.name+", this);}");
		}

		if ((this.fallback = this.fallback_both) || (this.fallback = this.fallback_multi)) {

			//eval("document.getElementById(\"" + this.attachedId + "_day\").onchange = function () {updateFromMultiDay("+this.name+", this);}");
			//eval("document.getElementById(\"" + this.attachedId + "_month\").onchange = function () {updateFromMultiMonth("+this.name+", this);}");
			//eval("document.getElementById(\"" + this.attachedId + "_year\").onchange = function () {updateFromMultiYear("+this.name+", this);}");
			
		}
	} 
	
	selectEvent = new Function();
}
//点击打开链接
function clickDay(tdobj,calendar,clickdate,clickUrl){
	flag = 0;
	var popObj = new Array();
	var t = new Date(clickdate);
	var d = clickdate.replace(/\//g,'');
	//loadLogByCalendarDate(d);
	//location.href='/index/index/date/'+d;
	if(clickUrl)
		location.href=clickUrl;
}
function updateFromSingle (sender, helper) {
	newDate = new Date (helper.value);
	newDate.setDate(newDate.getDate()+1);
	sender.calendarDate = newDate;

	renderCalendar (sender);
	setFieldValue(sender.attachedId, sender.calendarDate);
}

function updateFromMultiDay (sender, helper) {

	if (isNaN(helper.value)) {
		helper.value = sender.calendarDate.getDate();
		return false;
	}

	sender.calendarDate.setDate(helper.value);
	renderCalendar (sender);
	setFieldValue(sender.attachedId, sender.calendarDate);
}

function updateFromMultiMonth (sender, helper) {

	if (isNaN(helper.value)) {
		helper.value = sender.calendarDate.getMonths() -1;
		return false;
	}
	
	sender.calendarDate.setMonth(helper.value-1);
	renderCalendar (sender);
	setFieldValue(sender.attachedId, sender.calendarDate);
}

function updateFromMultiYear (sender, helper) {

	if (isNaN(helper.value)) {
		helper.value = sender.calendarDate.getFullYear();
		return false;
	}
	
	sender.calendarDate.setFullYear(helper.value);
	renderCalendar (sender);
	setFieldValue(sender.attachedId, sender.calendarDate);
}

function getFirstCalendarDate (calendar)
{
	return new Date (
		calendar.calendarDate.getFullYear()
		, calendar.calendarDate.getMonth()
		, 1
	);
}

function renderCalendar (calendar)
{
	//calHtml1 =  ("<table id=\"cal_" + calendar.attachedId + "\" class=\"" + calendar.calendarStyle +"\" cellpadding=\"1\" cellspacing=\"1\">");
	calHtml1 =  ("<table width=\"100%\" align=\"center\" cellpadding=\"0\" cellspacing=\"1\" id=\"cal_" + calendar.attachedId + "\" class=\"" + calendar.calendarStyle +"\">");
	calHtml1 += ((calendar.scrolling)?buildHeader(calendar):buildStaticHeader(calendar));
	calHtml1 += buildCalendarTable (calendar);
	calHtml1 += ("</table>");
	
	document.getElementById("cal_" + calendar.attachedId + "_display").innerHTML = calHtml1;
}

function scrollMonthBack (calendar)
{
	calendar.calendarDate.setMonth(calendar.calendarDate.getMonth() - 1);
	setFieldValue(calendar.attachedId, calendar.calendarDate);
	renderCalendar (calendar);
}

function selectDate (calendar, day)
{
	if (!calendar.viewOnly) {
		calendar.calendarDate.setDate(day);
		setFieldValue(calendar.attachedId, calendar.calendarDate);
		renderCalendar (calendar);
	}
}

function scrollMonthForward (calendar)
{
	calendar.calendarDate.setMonth(calendar.calendarDate.getMonth() + 1);
	setFieldValue(calendar.attachedId, calendar.calendarDate);
	renderCalendar (calendar);
}

function setFieldValue(fieldId, date) {
//	document.getElementById(fieldId).value = date.getFullYear() + "/" + (date.getMonth()+1) + "/" + date.getDate();
//	document.getElementById(fieldId + "_year").value = date.getFullYear();
//	document.getElementById(fieldId + "_month").selectedIndex = date.getMonth();
//	document.getElementById(fieldId + "_day").value = date.getDate();
}

function buildHeader (calendar)
{

	enableLeft = true;
	enableRight = true;
	
	if (calendar.minDate != "--") 
	{
		if (calendar.calendarDate.getFullYear() <= calendar.minDate.getFullYear())
		{
			if (calendar.calendarDate.getMonth() <= calendar.minDate.getMonth())
			{
				enableLeft = false;
			}
		}
	}

	if (calendar.maxDate != "--") 
	{
		if (calendar.calendarDate.getFullYear() >= calendar.maxDate.getFullYear())
		{
			if (calendar.calendarDate.getMonth() >= calendar.maxDate.getMonth())
			{
				enableRight = false;
			}
		}
	}

	calHtml2 = "";
	
	calHtml2 +=  (
		"<tr class=\""
		+ calendar.headerStyle
		+ "\">");
	calHtml2 +=  (
		"<td class=\""
		+ calendar.headerControllStyle
		+ ((enableLeft)?("\" onclick=\"scrollMonthBack(" + calendar.name + ")"):"")
		+ "\">"
		+ ((enableLeft)?calendar.controlLeft:calendar.controlLeftDisabled)
		+ "</td>");
	calHtml2 +=  (
		"<td colspan=\"5\" class=\""
		+ calendar.headerCellStyleLabel
		+ "\">"
		+ calendar.monthLabels[calendar.calendarDate.getMonth()] 
		+ ", " + calendar.calendarDate.getFullYear()
		+ "</td>");
	calHtml2 +=  (
		"<td class=\""
		+ calendar.headerControllStyle
		+ ((enableRight)?("\" onclick=\"scrollMonthForward(" + calendar.name + ")"):"")
		+ "\">"
		+ ((enableRight)?calendar.controlRight:calendar.controlRightDisabled)
		+ "</td>");
	
	calHtml2 += ("</tr>");
	
	calHtml2 +=  (
		"<tr class=\""
		+ calendar.headerStyle
		+ "\">")

	for (i = 0; i < 7; i++) {
		showDay = i + calendar.firstDayOfWeek;
		if (showDay > 6) showDay = showDay - 7;
		calHtml2 +=  (
			"<td class=\""
			+ calendar.headerCellStyle
			+ "\">"
			+ calendar.dayLabels[showDay]
			+ "</td>");
	}

	calHtml2 += ("</tr>");
	return calHtml2
}

function buildStaticHeader (calendar)
{
	calHtml2 = "";
	
	calHtml2 +=  (
		"<tr class=\""
		+ calendar.headerStyle
		+ "\">");
	calHtml2 +=  (
		"<td colspan=\"7\" class=\""
		+ calendar.headerCellStyleLabel
		+ "\">"
		+ calendar.monthLabels[calendar.calendarDate.getMonth()] 
		+ ", " + calendar.calendarDate.getFullYear()
		+ "</td>");	
	calHtml2 += ("</tr>");
	
	calHtml2 +=  (
		"<tr class=\""
		+ calendar.headerStyle
		+ "\">")

	for (i = 0; i < 7; i++) {
		showDay = i + calendar.firstDayOfWeek;
		if (showDay > 6) showDay = showDay - 7;
		calHtml2 +=  (
			"<td class=\""
			+ calendar.headerCellStyle
			+ "\">"
			+ calendar.dayLabels[showDay]
			+ "</td>");
	}

	calHtml2 += ("</tr>");
	return calHtml2
}




function RenderDayDisabled (calendar, currentDate)
{
	calHtml += ('<td class="'+calendar.disabledDayStyle+'">');
	calHtml += (currentDate.getDate());
	calHtml += ("</td>");
}

function RenderDayEnabled (calendar, currentDate, dayStyle)
{
	currentDayStyle = dayStyle;
	//calHtml += ("<td class=\"" + dayStyle + "\"  onclick=\"clickDay(this,"+calendar.name+","+currentDate.getDate()+");selectDate(" + calendar.name + ", " + currentDate.getDate() + ")\">");
	//没有事件不让点击
	calHtml += ("<td class=\"" + dayStyle + "\">");
	calHtml += (currentDate.getDate());
	calHtml += ("</td>");
}

function RenderDayEvent (calendar, currentDate, dayStyle, eventId,eventUrl)
{
	currentDayStyle = dayStyle;
//	calHtml += ('<td class="day">');
//	calHtml += ("<span class=\"" + dayStyle + "\" onclick=\"selectDate(" + calendar.name + ", " + currentDate.getDate() + "); " + calendar.name + ".selectEvent('" + eventId + "')\">");
//	calHtml += (currentDate.getDate());
//	calHtml += ("</span>");
//	calHtml += ("</td>");

	if(dayStyle.indexOf(calendar.eventDayStyle)!=-1)
		calHtml += ("<td class=\"" + dayStyle + "\"  onclick=\"clickDay(this," + calendar.name + ",'" + eventId + "','"+eventUrl+"');\">");
	else
		calHtml += ("<td class=\"" + dayStyle + "\"  >");
	calHtml += (currentDate.getDate());
	calHtml += ("</td>");
}

function buildCalendarTable (calendar)
{
	currentDate = getFirstCalendarDate(calendar);
	odd = 0;
	while (currentDate.getDay() != calendar.firstDayOfWeek)
	{
		currentDate.setDate(currentDate.getDate() - 1);
	}
	
	calHtml = "";
	do
	{
		odd += 1;

		calHtml +=  (
			"<tr class=\"" + (((odd%2)==0) ? calendar.evenWeekStyle : calendar.oddWeekStyle) + "\">")

		for (i = 0;i < 7;i++)
		{
			
			currentEventStyle = calendar.commonDayStyle;
			//modify by wwq
			//月，日长度设为2位
			month = currentDate.getMonth()+1;
			if(month<10)
				month = "0" + month;
			day = currentDate.getDate();
			if(day<10)
				day = "0" + day;
			
			currentDateString = currentDate.getFullYear() + "/" + month + "/" + day;

			if (currentDate < calendar.minDate){
				RenderDayDisabled (calendar, currentDate);
			}else if (currentDate > calendar.maxDate) {
				RenderDayDisabled (calendar, currentDate);
			}else if (currentDate.getMonth() != calendar.calendarDate.getMonth()){
				RenderDayDisabled (calendar, currentDate);
			}
			/*else if (currentDate.getDate() == calendar.calendarDate.getDate()){
			    //begin a
			    if ((currentDate.getDay() == 0) || (currentDate.getDay() == 6))
				{
					if (calendar.allowWeekends == true)
					{
						RenderDayEnabled (calendar, currentDate, calendar.todayDayStyle);
					} 
					else 
					{
						RenderDayDisabled (calendar, currentDate);	
						month = calendar.calendarDate.getMonth();
						calendar.calendarDate.setDate(calendar.calendarDate.getDate()+1);
						if (month != calendar.calendarDate.getMonth())
						{
							renderCalendar(calendar);
						}
						setFieldValue(calendar.attachedId, calendar.calendarDate);
					}
				} else {
					if (calendar.allowWeekdays == true)
					{
						RenderDayEnabled (calendar, currentDate, calendar.todayDayStyle);
					} 
					else 
					{
						RenderDayDisabled (calendar, currentDate);	
						month = calendar.calendarDate.getMonth();
						calendar.calendarDate.setDate(calendar.calendarDate.getDate()+1);
						if (month != calendar.calendarDate.getMonth())
						{
							renderCalendar(calendar);
						}
						setFieldValue(calendar.attachedId, calendar.calendarDate);
					}
				}
			}//end a
			*/
			else if ((currentDate.getDay() == 0) || (currentDate.getDay() == 6))
			{
			//begin b
			    if (calendar.allowWeekends == true)
				{
				
					style = calendar.holidayDayStyle
					
					for (j=0; j < calendar.eventDates.length; j++)
					{
						if (calendar.eventDates[j][0] == currentDateString) 
						{
							style = calendar.eventDayStyle;
							//这一天是有事件的天,同时也是周末,也是今天,那么要显示事件的样式,也要显示今天的样式
							if(currentDateString==calendar.todayDateString){
							    style= calendar.eventDayStyle + " " + calendar.todayDayStyle;
							}
							RenderDayEvent (calendar, currentDate, style, calendar.eventDates[j][0],calendar.eventDates[j][1]);
						}
					}
					
					if (style == calendar.holidayDayStyle)
					{
						//周末+今天样式
					    if(currentDateString==calendar.todayDateString){
							style= calendar.holidayDayStyle + " " + calendar.todayDayStyle;
						}
					    RenderDayEnabled (calendar, currentDate, style);
					}
				} 
				else 
				{
					RenderDayDisabled (calendar, currentDate);	
				}
			//end b
			} else {
				if (calendar.allowWeekdays == true)
				{
					style = calendar.commonDayStyle

					for (j=0; j < calendar.eventDates.length; j++)
					{
						if (calendar.eventDates[j][0] == currentDateString) 
						{
							style = calendar.eventDayStyle;
							//如果是今天则外加显示今天的样式
    					    if(currentDateString == calendar.todayDateString){
    						    style= calendar.eventDayStyle + " " + calendar.todayDayStyle;
    						}
							RenderDayEvent (calendar, currentDate, style, calendar.eventDates[j][0],calendar.eventDates[j][1]);
						}
					}

					if (style == calendar.commonDayStyle)
					{
					    //如果是今天则显示今天的样式
					    if(currentDateString == calendar.todayDateString){
						    style = calendar.todayDayStyle;
						}
					    RenderDayEnabled (calendar, currentDate, style);
					}
				} 
				else 
				{
					RenderDayDisabled (calendar, currentDate);	
				}
			}

			currentDate.setDate(currentDate.getDate() + 1);	
		}
		
		calHtml += ("</tr>");
		

	} while (currentDate.getMonth() == calendar.calendarDate.getMonth());
	return calHtml;
}
