﻿
var CalendarHelper={CalendarList:{},RegCalendar:function(cal)
{this.CalendarList[cal.ID]=cal;},GetCalendar:function(calid)
{return CalendarHelper.CalendarList[calid];},_idcounter:0,GetUniqueID:function(target)
{if(target.id)
return target.id;if(isIE)
target.id=target.uniqueID;else
target.id="__Calendar_"+new Date().valueOf()+"_"+(++this._idcounter);return target.id;}}
function CalendarEntity(date,callback,properties,doublemonth)
{this.Date=date?date:new Date();this.CallBack=callback;this.Properties=properties;this.Body=$C("div");this.CalendarTitle=["S","M","T","W","T","F","S"];this._yearPicker=null;this._monthPicker=null;this._dayPicker=null;this._dayPicker2=null;this.DoubleMonth=doublemonth;this.Init();CalendarHelper.RegCalendar(this);}
CalendarEntity.prototype.Init=function(doublemonth)
{with(this.Body)
{if(this.Properties)
{if(this.Properties.id)
id=this.Properties.id;if(this.Properties.className)
className=this.Properties.className;if(this.Properties.style)
style=this.Properties.style;}
style.display="none";style.position="absolute";window.document.body.appendChild(this.Body);this.ID=CalendarHelper.GetUniqueID(this.Body);this.BuildBody(this.Body);this.Refresh(this.Date);}}
CalendarEntity.prototype.BuildBody=function(target)
{var d=new Date();var cTitle=$C("div");this._yearPicker=$C("select");eval("this._yearPicker.onchange = function() { CalendarHelper.GetCalendar(\""+this.ID+"\").Refresh(null, true); }");with(this._yearPicker)
{for(var i=d.getFullYear();i<d.getFullYear()+2;i++)
{var o=$C("option");o.text=o.value=i;options.add(o);}}
this._monthPicker=$C("select");eval("this._monthPicker.onchange = function() { CalendarHelper.GetCalendar(\""+this.ID+"\").Refresh(null, true); }");with(this._monthPicker)
{for(var i=1;i<13;i++)
{var o=$C("option");o.text=i;o.value=i-1;options.add(o);}}
cTitle.appendChild(this._yearPicker);cTitle.appendChild($C("label",{innerHTML:"年 ",className:"text"}));cTitle.appendChild(this._monthPicker);cTitle.appendChild($C("label",{innerHTML:"月",className:"text"}));target.appendChild(cTitle);this._dayPicker=this.BuildDayPicker();target.appendChild(this._dayPicker);if(this.DoubleMonth)
{target.appendChild($C("div",{innerHTML:"下个月",className:"nextmonth"}));this._dayPicker2=this.BuildDayPicker(true);target.appendChild(this._dayPicker2);}}
CalendarEntity.prototype.BuildDayPicker=function(noheader)
{var dp=$C("table");dp.cellSpacing=0;dp.cellPadding=2;var tb=$C("tbody");dp.appendChild(tb);for(var i=0;i<7;i++)
{var r=$C("tr");if(noheader&&i<1)
continue;tb.appendChild(r);for(var j=0;j<7;j++)
{var c;if(i<1)
{c=$C("th");c.innerHTML=this.CalendarTitle[j];}
else
{c=$C("td");c.innerHTML="";c.onmouseover=this.CellHighlight;c.onmouseout=this.CellDehighlight;}
r.appendChild(c);}}
return dp;}
CalendarEntity.prototype.Refresh=function(date,ignoreselect)
{if(!date)
date=new Date(this._yearPicker.value,this._monthPicker.value,1);var y=date.getFullYear();var m=date.getMonth();if(!ignoreselect)
{with(this._yearPicker)
for(var i=0;i<options.length;i++)
options[i].selected=(options[i].value==y);with(this._monthPicker)
for(var i=0;i<options.length;i++)
options[i].selected=(options[i].value==m);}
this.RefreshDayPicker(y,m,this._dayPicker);if(this.DoubleMonth)
{date.setMonth(m+1);this.RefreshDayPicker(date.getFullYear(),date.getMonth(),this._dayPicker2);}}
CalendarEntity.prototype.RefreshDayPicker=function(y,m,target)
{var nd=new Date();var cm=y==nd.getFullYear()&&m==nd.getMonth();var cd=nd.getDate();var z=0,zz=0,c=new Date(y,m,1).getDay(),dcount=new Date(y,m+1,0).getDate();var r=target.rows.length==7?1:0;for(var i=r;i<r+6;i++)
for(var j=0;j<7;j++)
{var cell=target.rows[i].cells[j];var isToday=cm&&cd==zz+1;if(++z>c&&++zz<dcount)
{cell.innerHTML=zz;eval("cell.onclick = function(e) { CalendarHelper.GetCalendar(\""+this.ID+"\").CellSelect(e, "+y+", "+m+"); }");}
else
{cell.innerHTML="";cell.onclick=null;}
cell.title=isToday?"今天":null;with(cell.style)
{textDecoration=isToday?"underline":"";backgroundColor=(isToday&&cell.innerHTML!="")?"#4984D7":"";color=isToday?"white":"";}}}
CalendarEntity.prototype.CellHighlight=function(e)
{if($E_T(e).innerHTML)
$E_T(e).className="HL";}
CalendarEntity.prototype.CellDehighlight=function(e)
{$E_T(e).className="";}
CalendarEntity.prototype.CellSelect=function(e,y,m)
{if(this.CallBack!=null&&!isNaN(parseInt($E_T(e).innerHTML)))
this.CallBack(new Date(y,m,$E_T(e).innerHTML));}
CalendarEntity.prototype.Open=function(x,y)
{with(this.Body.style)
{if(x!=null&&x.constructor==Number&&y!=null&&y.constructor==Number)
{left=x+"px";top=y+"px";}
else if(x!=null&&y==null)
{$XY(this.Body,$E_T(x),50,50);}
display="";}}
CalendarEntity.prototype.Close=function()
{this.Body.style.display="none";}