//如何有问题请与我联系 mailto:c@onnpc.com
function setCalendar(obj){
Calendar.init(obj);
}
function _(id){return document.getElementById(id);}
function isDate(dateStr){
	var datePat=/^(\d{4})([\/-])(\d{1,2})([\/-])(\d{1,2})$/;
	dateStr=String(dateStr).replace(/^\s*|\s*$/g,"");
	var matchArray=dateStr.match(datePat);
	if(matchArray==null){return false;};var month=matchArray[3];
	var day=matchArray[5];var year=matchArray[1];
	if(month<1||month>12){return false;};
	if(day<1||day>31){return false;};
	if((month==4||month==6||month==9||month==11)&&day==31){return false;};
	if(month==2){var isleap=(year%4==0&&(year%100!=0||year%400==0));if(day>29||(day==29 && !isleap)){return false;}};
	return true;
}

Date.prototype.getWeek = function() {
    var determinedate = new Date();
    determinedate.setFullYear(this.getFullYear(), this.getMonth(), this.getDate());
    var D = determinedate.getDay();
    if(D == 0) D = 7;
    determinedate.setDate(determinedate.getDate() + (4 - D));
    var YN = determinedate.getFullYear();
    var ZBDoCY = Math.floor((determinedate.getTime() - new Date(YN, 0, 1, -6)) / 86400000);
    var WN = 1 + Math.floor(ZBDoCY / 7);
    return WN;
}

//Calendar Class
var Calendar ={
	idObj:"",
	calendarHTML:new function(){//日历片段代码
	if(_("calendar")){document.body.removeChild(_("calendar"))}
		var div = document.createElement("div");
		div.id = "calendar",div.style.display = 'none';//div.left = '-5000px',div.top = '-5000px';//兼容低级IE所用
		
		div.innerHTML='<div id="main" onblur="Calendar.hide()">'
//		 + '<div class="head">'
//		 + '<span><select id="month" name="month"></select></span><span id="hide">×</span>'
//		 + '</div>'
		 + '<div class="head"><b>日</b><b>一</b><b>二</b><b>三</b><b>四</b><b>五</b><b>六</b><br /></div>'
//		 + '<div id="week" onClick="Calendar.hide()" onblur="Calendar.hide()">'
		 + '<div class="month"><em id="year"></em><span>年</span><em id="month"></em><span>月</span></div>'
		 + '<div id="day">日期列表选项</div>'
		 + '</div>'
		 + '<iframe id="mask" frameborder="0"></iframe>';
		 //debugger;
    	if(document.body)
    	{
    	document.body.appendChild(div);
    	}
    	//$(this).parents("body").appendChild(div);
    	
	},
	alignPos:function(){
		var pos={};
		pos.x=0,pos.y=0;
		var id=this.idObj;
		while(id!=null && id!=document.body){
		//pos.x+=this.idObj.offsetLeft;pos.y+=this.idObj.offsetTop+this.idObj.offsetHeight;
		pos.x+=id.offsetLeft;pos.y+=id.offsetTop;id=id.offsetParent;
		}
		pos.y+=this.idObj.offsetHeight;
		return pos;
	},
	getToday:new function(){ //得到今天的年,月,日
	   this.now=new Date();
	   this.year=this.now.getFullYear();
	   this.month=this.now.getMonth()+1;
	   this.day=this.now.getDate();
	},
	thisDay:function(){
	   var date=this.idObj.value;
	   var Day={};
		Day.now=this.getToday.now;
		Day.year=Day.now.getFullYear();
		Day.month=Day.now.getMonth()+1;
		Day.day=Day.now.getDate();
	   return Day;
	},
	setMonth:function(){
		var month=_("month");
		month.innerHTML=this.thisDay().month;
   	   var yearObj=_("year");
	   yearObj.innerHTML=Number(this.thisDay().year);
	},
	limitFactor:function(any,mark){
		if(!mark) return 0;
		var now=new Date().getTime();
		//---
		var today=this.thisDay().now.getTime();
		if((now-today)>86400000){
			alert("此次输入可能无效，请检查系统日期（桌面右下角）是否有误。");
			now=0;
			throw("E-mail:c@onnpc.com");
		}
		//--严格模式
		if((any<now-86400000)||(any>now+mark*86400000)) return 1;
		else return 0;
	},
	setDay:function(flag){
		var y=this.thisDay().year;
		var m=this.thisDay().month;
		if(flag){y=_("year").innerHTML,m=_("month").innerHTML;}
	    var day=this.getDays(y,m);
	    var d=new Date(y,m-1,1).getDay();
	    var _d=this.thisDay().day;
		var week=new Date(y,m-1,_d).getDay();
		var week2=new Date(y,m,1).getDay();
		var any;

	    var dayStr="<dl>";
		for(var t=0;t<week;t++){ dayStr+='<i></i>';}
		for(t=_d;t<=day;t++){
				   if(day!=t) dayStr+='<dd onmousedown="Calendar.getDay('+t+')">'+t+'</dd>';				   
				   else {
					dayStr+='<dd onmousedown="Calendar.getDay('+t+')">'+t+'</dd>';
					dayStr+='</dl><div class="month"><em id="year2"></em><span>年</span><em id="month2"></em><span>月</span></div><dl>';
				   }
			}
		for(t=0;t<week2;t++){ dayStr+='<i></i>';}
		if(day>30) _d--;
		for(t=1;t<=_d;t++){
				   any=new Date(y,m,t).getTime();
				   if(this.limitFactor(any,30)) dayStr+='<i>'+t+'</i>';
				   else dayStr+='<dd onmousedown="Calendar.getDay('+t+',1)">'+t+'</dd>';
			}
	    _("day").innerHTML = dayStr+'</dl>';
	    this.reframe();//如果日历下无select等可注释此行
	},
	onChange:function(){
	    Calendar.setDay(true);
	    Calendar.mouseDay();
	},
	onblur:function(){
		Calendar.hide();
	},
	past:function(){
		_("year").innerHTML--;
	    Calendar.setDay(true);
	    Calendar.mouseDay();
	},
	future:function(){
		_("year").innerHTML++;
	    Calendar.setDay(true);
	    Calendar.mouseDay();
	},
	getDay:function(d,next){
		if(undefined==next) next=0;
		var y = _("year").innerHTML;
		var m = parseInt(_("month").innerHTML)+next;
		if(m>12){m='01';y++;}
		if(m<10){m='0'+m;}
		if(d<10){d='0'+d;}
		this.idObj.value = y+'-'+m+'-'+d;
		this.hide();
	},
	getDays:function (y,m){//获取某年某月的天数
	   return m==2?(y%4||!(y%100)&&y%400?28:29):(/4|6|9|11/.test(m)?30:31);
	},
	mouseDay:function(){
	   var days = _("day").getElementsByTagName("dd");
		for(d=0;d<days.length;d++){
		 days[d].onmouseover=function(){this.className="focus";}
		 days[d].onmouseout =function(){this.className="";}
		}
	},
	reframe:function(){
		var mask=_("mask");
		var frame=_("calendar");
		mask.style.width=parseInt(frame.offsetWidth -2)+'px';
		mask.style.height=parseInt(frame.offsetHeight-2)+'px';
	},
	show:function(){
		var calendar =_("calendar");
		calendar.style.left = this.alignPos().x +'px',calendar.style.top = this.alignPos().y +'px';
		if(calendar.style.display=='none')	calendar.style.display='block';
		//else calendar.style.display='none';
	},
	hide:function(){//_("calendar").style.left= '-5000px',_("calendar").style.top = '-5000px';
	   _("calendar").style.display='none';
	},
	checkClendar:function(){
		var calendar =_("calendar");
		if(calendar)
		{
		    if(calendar.style.display == 'none'){
			    return false;
		    }
		}
		return true;
	},
	
	init:function(obj){
		this.idObj=obj;
//		var pastObj = _("past");
//		var futureObj=_("future");
		var monthObj= _("month");
		if(Calendar.checkClendar() == true){
			return true;
		}
		Calendar.show();
		Calendar.setMonth();
		Calendar.setDay();
		var month=parseInt(_("month").innerHTML)%12;
		_("month2").innerHTML=month+1;
		_("year2").innerHTML=parseInt(_("year").innerHTML)+1-Math.ceil(month/12);
		Calendar.mouseDay();
	}
}
//调用方法:<input type="text" onClick="setCalendar(this);">