﻿
WSCL2={
Style: 
	"#wscl{width:198px;height:178px;font:10px verdana;color:black;border:solid 1px #999999;background:white;border-collapse:collapse;text-align:center;cursor:default;position:absolute;}"+
	"#wscl td{width:28px;padding:4px;}"+
	"#wscl th{font-size:12px;background-color:#999999;padding:4px;}"+
	"#wscl div{text-align:center;line-height:18px;}"+
	"#wscl select{position:absolute;}"+
	"#wscl input{width:35px;height:18px;border:solid 1px #999999;}"+ // font-size:9px; 英文版
	"#wscl .head th{background-color:#cccccc;}"+
	"#wscl .today{background-color:#cccccc;}"+
	"#wscl .weekend{background-color:#ffffcc;}"+
	"#wscl .disabled{color:silver;}"+
	"#wscl .hover{text-decoration:underline;color:navy;}",
DayText:["日","一","二","三","四","五","六","年","月","本月","重置","关闭"], //中文版
//DayText:["S","M","T","W","T","F","S","","","Today","Reset","Close"], //英文版
Create:
	function(sender,arg){
		with(WSCL2)
		{
			Sender=sender;
			Argument=arg;
			if (!Instance)
			{
				Current=new Date();
				Current.setDate(1);
				Instance=$a($$("table"));
				with(Instance)
				{
					id="wscl";cellSpacing="0";cellPadding="0";border="0";
					Instance._cols=[];
					for(var i=0;i<7;i++)
						_cols[i]=appendChild($$("colgroup"));
					_cols[0].className=_cols[6].className="weekend";
					Instance._rows={month:insertRow(-1),head:insertRow(-1),cells:[]};
					Instance._buttons={today:null,reset:null,close:null};
					with(_rows.month)
					{
						_rows.month._prev=appendChild($$("th"));
						_rows.month._year=appendChild($$("th"));
						_rows.month._month=appendChild($$("th"));
						_rows.month._next=appendChild($$("th"));

						AttachEvent(_prev,Events.PrevMonth);
						AttachEvent(_year,Events.ChooseYear);
						AttachEvent(_month,Events.ChooseMonth);
						AttachEvent(_next,Events.NextMonth);

						$t(_prev,"<");
						_year.style.padding="0px";
						_year.style.textAlign="left";
						_year.sel=_year.appendChild($$("select"));
						_year.sel.style.width="84px";
						_year.sel.style.display="none";
						_year.sel.onchange=function(){Current.setYear(this.options[this.selectedIndex].value);Show();}
						_year.colSpan="3";
						_year._text=_year.appendChild($$("div"));
						_year._text.style.textAlign="center";
						_month.style.padding="0px";
						_month.style.textAlign="left";
						_month.sel=_month.appendChild($$("select"));
						_month.sel.style.width="56px";
						_month.sel.style.display="none";
						_month.sel.onchange=function(){Current.setMonth(this.options[this.selectedIndex].value);Show();}
						for(var i=0;i<12;i++)
						{
							var opt=_month.sel.options[i]=$$("option");
							opt.value=i;
							opt.text=(i+1).toString()+DayText[8];
						}
						_month.colSpan="2";
						_month._text=_month.appendChild($$("div"));
						_month._text.style.textAlign="center";
						$t(_next,">");
					}
					with(_rows.head)
					{
						className="head";
						_rows.head._cells=[];
						for(var i=0;i<7;i++)
						{
							_cells[i]=appendChild($$("th"));
							$t(_cells[i],DayText[i]);
						}
					}
					for(var i=0;i<6;i++)
					{
						var tr=insertRow(-1);
						for(var j=0;j<7;j++)
						{
							var idx=i*7+j;
							with(tr)
							{
								var td=_rows.cells[idx]=insertCell(-1);
								if (idx<38)
								{
									AttachEvent(td,Events.ChooseDate);
								}
								else if (idx==38)
								{
									td.style.padding="1px";
									td.style.fontSize="1px";
									td.style.textAlign="left";
									var nobr=td.appendChild($$("nobr"));
									var div=nobr.appendChild($$("div"));
									nobr.style.position="relative";
									div.style.position="absolute";
									div.style.width="84px";
									_buttons.today=$$("input");
									_buttons.today.type="button";
									div.appendChild(_buttons.today);
									_buttons.today.value=DayText[9];
									AttachEvent(_buttons.today, Events.TodayCalendar);
									$t(div," ");
									_buttons.reset=$$("input");
									_buttons.reset.type="button";
									div.appendChild(_buttons.reset);
									_buttons.reset.value=DayText[10];
									AttachEvent(_buttons.reset, Events.ResetCalendar);
									$t(div," ");
									_buttons.close=$$("input");
									_buttons.close.type="button";
									div.appendChild(_buttons.close);
									_buttons.close.value=DayText[11];
									AttachEvent(_buttons.close, Events.CloseCalendar);
									var div2=td.appendChild($$("div"));
									$t(div2,"\u00a0");
									continue;
								}
								$t(td,"\u00a0");
							}
						}
					}
				}
			}
		}
		return WSCL2;
	},
AttachEvent:
	function(obj,handler){
		obj.onmouseover=function(){if(!obj._ocs){obj._ocs=obj.className?obj.className:" ";obj.style.cursor="pointer";}obj.className=obj._ocs+" hover";};
		obj.onmouseout=function(){if(!obj._ocs){obj._ocs=obj.className?obj.className:" ";}obj.className=obj._ocs;};
		if(handler)obj.onclick=handler;
		else obj.onlick=function(){alert("building...");}
	},
Show:
	function(){
		with(WSCL2)
		{
			var mon=Current.getMonth();
			var year=Current.getFullYear();
			Instance._rows.month._year._text.innerText=year+DayText[7];
			Instance._rows.month._year._text.textContent=year+DayText[7];
			Instance._rows.month._year.sel.style.display="none";
			Instance._rows.month._month._text.innerText=(mon+1).toString()+DayText[8];
			Instance._rows.month._month._text.textContent=(mon+1).toString()+DayText[8];
			Instance._rows.month._month.sel.style.display="none";
			Current.setDate(1-Current.getDay());
			for(var i=0;i<38;i++)
			{
				var date=Current.getDate();
				$T(Instance._rows.cells[i],date);
				Instance._rows.cells[i].className="";
				Instance._rows.cells[i]._ocs="";
				if(Current.getMonth()!=mon)
					Instance._rows.cells[i].className+=" disabled";
				if(Current.getDate()==Today.getDate()&&Current.getMonth()==Today.getMonth())
					Instance._rows.cells[i].className+=" today";
				Instance._rows.cells[i].value=Current.getFullYear()+"-"+(Current.getMonth()+1).toString()+"-"+Current.getDate();
				Current.setDate(date+1);
			}
			Current.setYear(year);
			Current.setMonth(mon);
			Current.setDate(1);
			var srcObj=Sender;
			var t = 0,  h = Sender.clientHeight, l = 0;
			var st = 0, sl = 0;
			if(!!/MSIE/.test(window.navigator.userAgent))
			{
				while (srcObj!=document.body)
				{
		            st = srcObj.scrollTop;
		            sl = srcObj.scrollLeft;
		            t += srcObj.offsetTop - st;
		            l += srcObj.offsetLeft - sl;
					srcObj=srcObj.offsetParent;
				}
	            t += st;
	            l += sl+srcObj.offsetLeft-srcObj.scrollLeft;
			}
			else
			{
				while (srcObj!=document)
				{
                    var tag=srcObj.tagName.toLowerCase();
					if(tag!="div"&&tag!="tr"&&tag!="form"){
					    t+=srcObj.offsetTop;
					    l+=srcObj.offsetLeft;
					}
					srcObj=srcObj.parentNode;
				}
			}

			//Instance.style.left=l+"px";
			Instance.style.top=(t+h+4).toString()+"px";
			Instance.style.left=l.toString()+"px";

			Instance.style.display="";
			if(!document.body._ocl)
				document.body._ocl=document.body.onclick;
			document.body.onclick=function(event)
			{
				var srcObj=window.event?window.event.srcElement:event.target;
				if(srcObj==Sender)return;
				while(srcObj!=document.body)
				{
					if (srcObj==Instance)return;
					srcObj=srcObj.parentNode;
				}
				Events.CloseCalendar();
			}
			Sender.blur();
		}
	},
Hide:
	function(){
		Instance.style.display="none";
	},
Today:new Date(),
Current:null,
Instance:null,
Sender:null,
Argument:null,
Events:{
	PrevMonth:function(){with(WSCL2){Current.setMonth(Current.getMonth()-1);Show();}},
	NextMonth:function(){with(WSCL2){Current.setMonth(Current.getMonth()+1);Show();}},
	ChooseYear:function(event){
		var srcObj=window.event?window.event.srcElement:event.target;
		WSCL2.Instance._rows.month._month.sel.style.display="none";
		with(WSCL2.Instance._rows.month._year)
		{
			if(srcObj==sel)return;
			while(sel.options.length>0)sel.options.remove(0);
			var start=WSCL2.Current.getFullYear()-5;
			for(var i=start;i<=start+10;i++)
				sel.options.add(new Option(i+WSCL2.DayText[7],i));
			sel.options[5].selected=true;
			sel.style.display="";
		}
	},
	ChooseMonth:function(event){
		var srcObj=window.event?window.event.srcElement:event.target;
		WSCL2.Instance._rows.month._year.sel.style.display="none";
		with(WSCL2.Instance._rows.month._month)
		{
			if(srcObj==sel)return;
			sel.options[WSCL2.Current.getMonth()].selected=true;
			sel.style.display="";
		}
	},
	TodayCalendar:function(){with(WSCL2){Current=new Date();Current.setDate(1);Show();}},
	ChooseDate:function(){with(WSCL2){Sender.value=this.value;Events.CloseCalendar();}},
	ResetCalendar:function(){with(WSCL2){Sender.value="";Events.CloseCalendar();}},
	CloseCalendar:function(){WSCL2.Instance.style.display="none";document.body.onclick=document.body._ocl;}
},
$:function(id){return document.getElementById(id);},
$$:function(tag){return document.createElement(tag);},
$a:function(obj){return document.body.appendChild(obj);},
$t:function(obj,text){return obj.appendChild(document.createTextNode(text));},
$T:function(obj,text){obj.childNodes[0].data=text;}
}
document.write("<style>"+WSCL2.Style+"</style>");
function calendar(event){WSCL2.Create(window.event?window.event.srcElement:event.target,{event:window.event?window.event:event}).Show();};
