// Powered by: amo
// Improved by Chris.J

var oCalendarEn=new PopupCalendar("oCalendarEn");	//初始化控件时,请给出实例名称如:oCalendarEn
oCalendarEn.weekDaySting=new Array("S","M","T","W","T","F","S");
oCalendarEn.monthSting=new Array("January","February","March","April","May","June","July","August","September","October","November","December");
oCalendarEn.oBtnTodayTitle="Today";
oCalendarEn.oBtnCancelTitle="Cancel"; 
oCalendarEn.Init();
var oCalendarChs=new PopupCalendar("oCalendarChs");	//初始化控件时,请给出实例名称:oCalendarChs
oCalendarChs.weekDaySting=new Array("日","一","二","三","四","五","六");
oCalendarChs.monthSting=new Array("一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月");
oCalendarChs.oBtnTodayTitle="今天";
oCalendarChs.oBtnCancelTitle="取消"; 
//oCalendarChs.weekDaySting=new Array("S","M","T","W","T","F","S");
//oCalendarChs.monthSting=new Array("January","February","March","April","May","June","July","August","September","October","November","December");
//oCalendarChs.oBtnTodayTitle="Today";
//oCalendarChs.oBtnCancelTitle="Cancel"; 
oCalendarChs.Init();

document.onclick=DocumentClick;
var IS_IE= (navigator.userAgent.toLowerCase().indexOf("msie") != -1 && document.all) ;
if(!IS_IE && typeof Event == "function")
{
	Window.prototype.__defineGetter__("event", GetEvent);
	//window.event.__defineGetter__("srcElement", GetEventSrcElement);
	Event.prototype.__defineGetter__("srcElement", GetEventSrcElement);
}

function PopupCalendar(InstanceName)
{
	///Global Tag
	this.instanceName=InstanceName;
	///Properties
	this.separator="-"
	this.oBtnTodayTitle="Today"
	this.oBtnCancelTitle="Cancel"
	this.weekDaySting=new Array("S","M","T","W","T","F","S");
	this.monthSting=new Array("January","February","March","April","May","June","July","August","September","October","November","December");
	this.Width=200;
	this.currDate=new Date();
	this.today=new Date();
	this.startYear=1970;
	this.endYear=new Date().getFullYear() + 5;
	///Css
	this.normalfontColor="#666666";
	this.selectedfontColor="red";
	this.divBorderCss="1px solid #BCD0DE";
	this.titleTableBgColor="#98B8CD";
	this.tableBorderColor="#CCCCCC"
	///Method
	this.Init=CalendarInit;
	this.Fill=CalendarFill;
	this.Refresh=CalendarRefresh;
	this.Restore=CalendarRestore;
	///HTMLObject
	this.oTaget=null;
	this.oPreviousCell=null;
	this.sDIVID=InstanceName+"_Div";
	this.sTABLEID=InstanceName+"_Table";
	this.sMONTHID=InstanceName+"_Month";
	this.sYEARID=InstanceName+"_Year";
	this.sDAYTDID=InstanceName+"_DayTD";
	this.sTODAYBTNID=InstanceName+"_TODAYBTN";
	this.sCANCELID = InstanceName + "_Cancel";
	this.bgIframeID = InstanceName + "_Iframe";
	
}

function CalendarInit()				///Create panel
{
	var sMonth,sYear
	sMonth = this.currDate.getMonth();
	sYear = this.currDate.getYear();
	var htmlAll = "<div id='" + this.sDIVID + "' style='display:none;Z-INDEX: 999;position:absolute;width:" + this.Width + ";border:" + this.divBorderCss + ";padding:2px;background-color:#FFFFFF'>";
	htmlAll += "<div align='center'>";
	/// Month
	htmloMonth="<select id='" + this.sMONTHID + "' onchange=CalendarMonthChange(" + this.instanceName + ") style='width:50%'>";
	for(i=0; i<12; i++)
	{			
		htmloMonth += "<option value='" + i + "'>" + this.monthSting[i] + "</option>";
	}
	htmloMonth += "</select>";
	/// Year
	htmloYear = "<select id='" + this.sYEARID + "' onchange=CalendarYearChange(" + this.instanceName + ") style='width:50%'>";
	for(i=this.startYear; i<=this.endYear; i++)
	{
		htmloYear += "<option value='" + i + "'>" + i + "</option>";
	}
	htmloYear += "</select></div>";
	/// Day
	htmloDayTable = "<table id='" + this.sTABLEID + "' width='100%' border=0 cellpadding=0 cellspacing=1 bgcolor='" + this.tableBorderColor + "'>";
	htmloDayTable += "<tbody bgcolor='#ffffff'style='font-size:13px'>";
	for(i=0;i<=6;i++)
	{
		if(i==0)
		{
			htmloDayTable += "<tr bgcolor='" + this.titleTableBgColor + "'>";
		}
		else
		{
			htmloDayTable += "<tr>";
		}
		for(j=0;j<7;j++)
		{

			if(i==0)
			{
				htmloDayTable += "<td height='20' align='center' valign='middle' style='cursor: pointer'>";
				htmloDayTable += this.weekDaySting[j] + "</td>"
			}
			else
			{
				var DayID = this.sDAYTDID + i + j;
				htmloDayTable += "<td id=" + DayID + " height='20' align='center' valign='middle' style='cursor: pointer'";
				htmloDayTable += " onmouseover=CalendarCellsMsOver(" + this.instanceName + ")";
				htmloDayTable += " onmouseout=CalendarCellsMsOut(" + this.instanceName + ")";
				htmloDayTable += " onclick=CalendarCellsClick(this," + this.instanceName + ")>";
				htmloDayTable += "&nbsp;</td>"
				
				//Add Event Handler for Day_TD (Mainly for FireFox)
				//AddElementEvent(DayID, 'onmouseover', function(){CalendarCellsMsOver(this.instanceName)});
				//AddElementEvent(DayID, 'onmouseout', function(){CalendarCellsMsOut(this.instanceName)});
				//AddElementEvent(DayID, 'onclick', function(){CalendarCellsClick(this.instanceName)});
			}
		}
		htmloDayTable+="</tr>";	
	}
	htmloDayTable+="</tbody></table>";
	
	/// Today Button
	htmloButton="<div align='center' style='padding:3px'>"
	htmloButton+="<input type='button' id='" + this.sTODAYBTNID + "' style='width:40%;border:1px solid #BCD0DE;background-color:#eeeeee;cursor: pointer;height:22px;font-size:12px'"
	htmloButton+=" onclick=CalendarTodayClick("+this.instanceName+") value='" + this.oBtnTodayTitle + "'>&nbsp;"
	htmloButton+="<input type='button' id='" + this.sCANCELID + "' style='width:40%;border:1px solid #BCD0DE;background-color:#eeeeee;cursor: pointer;height:22px;font-size:12px'"
	htmloButton+=" onclick=CalendarCancel("+this.instanceName+") value='" + this.oBtnCancelTitle + "'>"
	htmloButton+="</div>"
	
	/// All
	htmlAll=htmlAll+htmloMonth+htmloYear+htmloDayTable+htmloButton+"</div>";
	htmlAll += "<iframe id=" + this.bgIframeID + " scrolling='no' frameborder='0' style='position:absolute; top:0px; left:0px; Z-INDEX: 998; display:none;filter=progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0);'></iframe>";
	document.write(htmlAll);
	
	this.Fill();	
	
	//添加事件处理(Mainly for FireFox, 也可以去掉Html中inline写法，直接使用此方法以使用IE)
	//AddElementEvent(this.sTODAYBTNID, 'onclick', function(){CalendarTodayClick(this.instanceName)});
	//AddElementEvent(this.sCANCELID, 'onclick', function(){CalendarCancel(this.instanceName)});
	//AddElementEvent(this.sMONTHID, 'onchange', function(){CalendarMonthChange(this.instanceName)});
	//AddElementEvent(this.sYEARID, 'onchange', function(){CalendarYearChange(this.instanceName)});
}

function CalendarFill()			///
{
	var sMonth,sYear,sWeekDay,sToday,oTable,currRow,MaxDay,iDaySn,sIndex,rowIndex,cellIndex,oSelectMonth,oSelectYear
	sMonth=this.currDate.getMonth();
	sYear=this.currDate.getFullYear();
	sWeekDay=(new Date(sYear,sMonth,1)).getDay();
	sToday=this.currDate.getDate();
	iDaySn=1
	oTable=document.getElementById(this.sTABLEID);
	currRow=oTable.rows[1];
	MaxDay=CalendarGetMaxDay(sYear,sMonth);
	
	oSelectMonth=document.getElementById(this.sMONTHID);
	oSelectMonth.selectedIndex=sMonth;
	oSelectYear=document.getElementById(this.sYEARID);
	for(i=0;i<oSelectYear.length;i++)
	{
		if(parseInt(oSelectYear.options[i].value)==sYear)oSelectYear.selectedIndex=i;
	}
	////
	for(rowIndex=1;rowIndex<=6;rowIndex++)
	{
		if(iDaySn>MaxDay)break;
		currRow = oTable.rows[rowIndex];
		cellIndex = 0;
		if(rowIndex==1)cellIndex = sWeekDay;
		for(;cellIndex<currRow.cells.length;cellIndex++)
		{
			if(iDaySn==sToday)
			{
				currRow.cells[cellIndex].innerHTML="<font color='"+this.selectedfontColor+"'><i><b>"+iDaySn+"</b></i></font>";
				this.oPreviousCell=currRow.cells[cellIndex];
			}
			else
			{
				currRow.cells[cellIndex].innerHTML=iDaySn;	
				currRow.cells[cellIndex].style.color=this.normalfontColor;
			}
			CalendarCellSetCss(0,currRow.cells[cellIndex]);
			iDaySn++;
			if(iDaySn>MaxDay) break;	
		}
	}
}

function CalendarRestore()					/// Clear Data
{	
	var i,j,oTable
	oTable=document.getElementById(this.sTABLEID);
	for(i=1;i<oTable.rows.length;i++)
	{
		for(j=0;j<oTable.rows[i].cells.length;j++)
		{
			CalendarCellSetCss(0,oTable.rows[i].cells[j]);
			oTable.rows[i].cells[j].innerHTML="&nbsp;";
		}
	}	
}

function CalendarRefresh(newDate)					///
{
	this.currDate=newDate;
	this.Restore();	
	this.Fill();	
}

function CalendarCellsMsOver(oInstance)				/// Cell MouseOver
{
	var myCell = GetEventSrcElement();
	CalendarCellSetCss(0,oInstance.oPreviousCell);
	if(myCell)
	{
		CalendarCellSetCss(1,myCell);
		oInstance.oPreviousCell=myCell;
	}
}

function CalendarCellsMsOut(oInstance)				////// Cell MouseOut
{
	var myCell = GetEventSrcElement();
	CalendarCellSetCss(0,myCell);	
}

function CalendarYearChange(oInstance)				/// Year Change
{
	var sDay,sMonth,sYear,newDate
	sDay=oInstance.currDate.getDate();
	sMonth=oInstance.currDate.getMonth();
	sYear=document.getElementById(oInstance.sYEARID).value;
	newDate=new Date(sYear,sMonth,sDay);
	oInstance.Refresh(newDate);
}

function CalendarMonthChange(oInstance)				/// Month Change
{
	var sDay,sMonth,sYear,newDate
	sDay=oInstance.currDate.getDate();
	sMonth=document.getElementById(oInstance.sMONTHID).value;
	sYear=oInstance.currDate.getYear();
	newDate=new Date(sYear,sMonth,sDay);
	oInstance.Refresh(newDate);	
}

function CalendarCellsClick(oCell,oInstance)
{
	var sDay,sMonth,sYear,newDate
	sYear=oInstance.currDate.getFullYear();
	sMonth=oInstance.currDate.getMonth();
	sDay=oInstance.currDate.getDate();
	var InnerText = oCell.innerText || oCell.textContent;
	if(InnerText.length > 0 && InnerText != " " && InnerText.charCodeAt(0) != 32 && InnerText.charCodeAt(0) != 160)
	{
		sDay=parseInt(InnerText);
		if(sDay!=oInstance.currDate.getDate())
		{
			newDate=new Date(sYear,sMonth,sDay);
			oInstance.Refresh(newDate);
		}
	}
	sDateString=sYear+oInstance.separator+CalendarDblNum(sMonth+1)+oInstance.separator+CalendarDblNum(sDay);		///return sDateString
	if(oInstance.oTaget.tagName.toLowerCase()=="input")
	{
		oInstance.oTaget.value=sDateString;
	}
	CalendarCancel(oInstance);
	return sDateString;
}

function CalendarTodayClick(oInstance)				/// "Today" button Change
{	
	oInstance.Refresh(new Date());		
}

var ActiveInstance;	//当前活动的Calendar
function getDateString(oInputSrc,oInstance)
{
	if(oInputSrc && oInstance) 
	{
		var CalendarDiv = document.getElementById(oInstance.sDIVID);
		var bgIframe = document.getElementById(oInstance.bgIframeID);
		oInstance.oTaget = oInputSrc;
		ActiveInstance = oInstance;
		
		bgIframe.style.pixelLeft = CalendarDiv.style.left = CalendarDiv.style.pixelLeft = CalendargetPos(oInputSrc, "Left");
		bgIframe.style.pixelTop = CalendarDiv.style.top = CalendarDiv.style.pixelTop = CalendargetPos(oInputSrc, "Top") + oInputSrc.offsetHeight;
		try{
			bgIframe.style.display = CalendarDiv.style.display = (CalendarDiv.style.display == "none") ? "" : "none";	
			bgIframe.style.visibility = CalendarDiv.style.visibility = (CalendarDiv.style.visibility == "hide") ? "visible" : "hide";	
		}catch(err){}
		
		bgIframe.width = CalendarDiv.scrollWidth;
		bgIframe.height = CalendarDiv.scrollHeight;
	}	
}

function CalendarCellSetCss(sMode,oCell)			/// Set Cell Css
{
	// sMode
	// 0: OnMouserOut 1: OnMouseOver 
	if(sMode)
	{
		oCell.style.border = "1px solid #5589AA";
		oCell.style.backgroundColor = "#BCD0DE";
	}
	else
	{
		oCell.style.border = "1px solid #FFFFFF";
		oCell.style.backgroundColor = "#FFFFFF";
	}	
}

function CalendarGetMaxDay(nowYear,nowMonth)			/// Get MaxDay of current month
{
	var nextMonth,nextYear,currDate,nextDate,theMaxDay
	nextMonth=nowMonth+1;
	if(nextMonth>11)
	{
		nextYear=nowYear+1;
		nextMonth=0;
	}
	else	
	{
		nextYear=nowYear;	
	}
	currDate=new Date(nowYear,nowMonth,1);
	nextDate=new Date(nextYear,nextMonth,1);
	theMaxDay=(nextDate-currDate)/(24*60*60*1000);
	return theMaxDay;
}

function CalendargetPos(el,ePro)				/// Get Absolute Position
{
	var ePos=0;
	while(el!=null)
	{		
		ePos+=el["offset"+ePro];
		el=el.offsetParent;
	}
	return ePos;
}

function CalendarDblNum(num)
{
	if(num < 10) 
	{
		return "0" + num;
	}
	else
	{
		return num;
	}
}

function CalendarCancel(oInstance)			///Cancel
{
	var CalendarDiv = document.getElementById(oInstance.sDIVID);
	var bgIframe = document.getElementById(oInstance.bgIframeID);
	try{		
		CalendarDiv.style.display = "none";		
		bgIframe.style.display = "none";
		CalendarDiv.style.visibility = "hide";
		bgIframe.style.visibility = "hide";
	}catch(err){}
}

//Click Blank Area and Close
function DocumentClick()
{
	if(ActiveInstance)
	{
		var tEventElement = GetEventSrcElement();
		var CalendarDiv=document.getElementById(ActiveInstance.sDIVID);
		if(oCalendarChs.oTaget != tEventElement && !Contains(CalendarDiv, tEventElement))
		{
			CalendarCancel(oCalendarChs);
		}
	}
}

//获得当前事件 (IE & FireFox)
function GetEvent()
{
	var i = 0;
	if(IS_IE) 
	{
		return window.event;  
	}
	func = GetEventSrcElement.caller;	
	while(func != null)	
	{	
		var arg0 = func.arguments[0];	
		if(arg0)	
		{	
			//if(arg0.constructor == Event)
			if(arg0.target != null)
			{	
				return arg0;	
			}	
		}	
		func = func.caller;	
	}	
	return null;
}

//获得当前事件的触发对象 (IE & FireFox)
function GetEventSrcElement()
{
	if(IS_IE) 
	{
		return window.event.srcElement;  
	}
	else
	{
		var evt = GetEvent();
		if(evt != null)
		{
			return evt.target;
		}
		else
		{
			return null;
		}
	}
}

//DOM中前者是否包含后者 (IE & FireFox)
function Contains(parentNode, childNode)
{
	if(parentNode != null && childNode != null)
	{
		if(typeof parentNode.contains != "undefined")
		{
			return parentNode.contains(childNode);
		}
		else
		{
			//For FireFox
			while(childNode != null)
			{
				childNode = childNode.parentNode;
				if(childNode == parentNode)
				{
					return true;
				}
			}
			return false;
		}
	}
	else
	{
		return false;
	}
}

//为对象添加一个事件处理器 (IE & FireFox)
function AddElementEvent(CtrlID, eventStr, functionCall)
{
	if(eventStr != null && CtrlID != null)
	{
		var el = document.getElementById(CtrlID);
		if(el)
		{
			if (window.attachEvent)
			{
				if(eventStr.substr(0, 2).toLowerCase() != "on")
				{
					eventStr = "on" + eventStr;
				}
				if(typeof el[eventStr] != "function")
				{
					el.attachEvent(eventStr, functionCall);
				}
			}
			if (window.addEventListener) 
			{
				if(eventStr.substr(0, 2).toLowerCase() == "on")
				{
					eventStr = eventStr.substr(2);
				}
				//事件的阶段通过设置addEventListener的最后一个参数为false(指示冒泡)或true(指示捕获)来切换。
				//false表示添加到队列，true表示正在捕获(?)，通常为false
				el.addEventListener(eventStr, functionCall, false);
			}
		}
    }
}