// 统一活动日历JS模版
// 王凌 20070320 整理
// 最后修改：2007-03-20 18：47
var HaveDiaryDateString;

function sndaactive_calendar(obj,y,m,d,chgfunction)
{
	var calhtml		= ""	//"当前日期:" + y + "年" + (m+1) + "月" + d + "日"
	var nowdate		= y + "-" + m + "-" + d
	
	var lastdate	= new Date(y, m-1, d, 0, 0, 0)
	var lasty		= lastdate.getFullYear()
	var lastm		= lastdate.getMonth()

	var nextdate	= new Date(y, m+1, d, 0, 0, 0)
	var nexty		= nextdate.getFullYear()
	var nextm		= nextdate.getMonth()
	var acvdayArr;
	var acvday;


	acvday=HaveDiaryDateString;
	if(acvday!="")
	{
		acvdayArr=acvday.split(",");
	}
	calhtml += "<div id='calendarHead'>"
	calhtml	+= "<a href='###' class='preMonth'>&lt;</a>&nbsp;&nbsp;"
	calhtml	+= "<span id='monthOfYear'>" + y + "年" + (m+1) + "月" + "</span>"
	calhtml	+= "&nbsp;&nbsp;<a href='###' class='nextMonth'>&gt;</a>"
//	calhtml	+= "<a href='###'>返回今日</a>"
	calhtml += "</div>"

	var thismonth	= new Date(y, m, 1, 0, 0, 0)
	var nextmonth	= new Date(y, m+1, 1, 0, 0, 0)
	var num			= new Date(y, m+1, 0, 0, 0, 0).getDate()
	var firstday	= thismonth.getDay()
	
	var currenty	= new Date().getFullYear()
	var currentm	= new Date().getMonth()
	var currentd	= new Date().getDate()

	calhtml += "<table style='border-collapse:collapse;' id='calendarBox'>" + "\n"
	calhtml += "<tr id='calendarWeek'>" + "\n"
	calhtml += "<td class='calendarDay'>日</td>" + "\n"
	calhtml += "<td class='calendarDay'>一</td>" + "\n"
	calhtml += "<td class='calendarDay'>二</td>" + "\n"
	calhtml += "<td class='calendarDay'>三</td>" + "\n"
	calhtml += "<td class='calendarDay'>四</td>" + "\n"
	calhtml += "<td class='calendarDay'>五</td>" + "\n"
	calhtml += "<td class='calendarDay'>六</td>" + "\n"
	calhtml += "</tr>"
	//alert(acvdayArr.length);
	for(var n=1;n<=6;n++)
	{
		calhtml += "<tr>"
		
		for (var i=0;i<=6;i++)
		{
			thisday=7 * (n-1) + i - firstday + 1
			//alert("thisday=" +thisday);
			if (thisday > num)
			{
				calhtml	+= "<td class='normalDay'>&nbsp;</td>"
				continue
			}
				
			if (n==1 && i < firstday)
			{
				display="&nbsp;"
			}
			else if (y == currenty && m == currentm && thisday == currentd)
			{
				display="<a href='###' isdate='"+thisday+"' id='thisDay' >" + thisday +"</a>"
			}
			else
			{
				if(typeof(acvdayArr)=="object")
				{
					if(acvdayArr.length>0)
					{
						haveDiary=false;
						
						for(var k=0;k<acvdayArr.length;k++)
						{
							
							acvday=parseInt(acvdayArr[k]);
							if(thisday==acvday)
							{
								haveDiary=true;	
							}
						}
						
						if(haveDiary)
						{
							display="<a href='###' isdate='"+thisday+"' class='haveDiaryDay'>" + thisday +"</a>"
						}
						else
						{
							display="" + thisday +""
						}
					}
					else
					{
						display="" + thisday +""
					}
				}

				else
				{
					display="" + thisday +""
				}
			}
			
			calhtml	+= "<td class='normalDay'>" + display + "</td>"
		}		
		
		calhtml += "</tr>"
	}

	calhtml	+= "</table>"
	obj.innerHTML = calhtml
	HaveDiaryDateString="";
	var objChildren	= obj.children
	var objAll		= obj.all
	
	var __default_calc_function	= function (y,m,d,t)
	{
		window.open("http://cal.sdo.com/calendar/index_user.aspx?date=" + y +"-"+ (m+1) +"-" + d, "_blank")
	}
	
	objChildren[0].children[0].onclick=function ()
	{
		sndaactive_calendar(obj, lasty, lastm, d, chgfunction)
		if (typeof(chgfunction) == "function")	chgfunction(lasty, lastm, d, "m")
	}
	objChildren[0].children[2].onclick=function ()
	{
		sndaactive_calendar(obj, nexty, nextm, d, chgfunction)
		if (typeof(chgfunction) == "function")	chgfunction(nexty, nextm, d, "m")
	}
/*	objChildren[0].children[3].onclick=function ()
	{
		sndaactive_calendar(obj,currenty,currentm,currentd, chgfunction)
		if (typeof(chgfunction) == "function")	chgfunction(currenty, currentm, currentd, "c")
	}*/
	
	for (var i=0; i<objAll.length; i++)
	{
		if (objAll[i].tagName == "A" && objAll[i].getAttribute("isdate") != null)
		{
			objAll[i].onclick=function()
			{
				if (typeof(chgfunction) == "function")
					chgfunction(thismonth.getFullYear(), thismonth.getMonth(), this.getAttribute("isdate"), "d")
				else
					__default_calc_function(thismonth.getFullYear(), thismonth.getMonth(), this.getAttribute("isdate"), "d")
			}
		}
	}
}

if (!window.ActiveXObject)
{
	HTMLElement.prototype.__defineGetter__("children", 
		 function ()
		 { 
			 var returnValue = new Object(); 
			 var number = 0; 
			 for (var i=0; i<this.childNodes.length; i++)
			 { 
				 if (this.childNodes[i].nodeType == 1)
				 { 
					 returnValue[number] = this.childNodes[i]; 
					 number++; 
				 } 
			 } 
			 returnValue.length = number; 
			 return returnValue; 
		 } 
	 )
	 
	 HTMLElement.prototype.__defineGetter__("all",
		 function()
		 {
			var a=this.getElementsByTagName("*");
			var node=this;
			a.tags=function(sTagName)
			{
				return node.getElementsByTagName(sTagName);
			}
			return a;
		}
	)
 }