﻿    
	var _cale_date=new Date();
    var _my_date=new Date();
	var _retdstr=null;
	var rlbox=null;
	var currentdate="今天是"+_my_date.getFullYear()+"-"+(_my_date.getMonth()+1)+"-"+_my_date.getDate();
	var minDay=1;//任何月中最小日为1
	var theDay=0;//得到这个月1号是周几
	var maxDay=0;//月中最大日

	//判断一个日期，得到该日期月份包含的天数，以及该月1号是周几
	function initMonth(_cale_date){
		theDay=(new Date(_cale_date.getFullYear(),_cale_date.getMonth(),1)).getDay();
		switch(_cale_date.getMonth()){
			case 0:
				maxDay=31;
				break;
			case 1:
				//如果是润年，二月份为29天，不是润年为28天
				if(_cale_date.getFullYear()%400==0||(_cale_date.getFullYear()%4==0&&_cale_date.getFullYear()%100!=0)){
					maxDay=29;
				}else{
					maxDay=28;
				}
				break;
			case 2:
				maxDay=31;
				break;
			case 3:
				maxDay=30;
				break;
			case 4:
				maxDay=31;
				break;
			case 5:
				maxDay=30;
				break;
			case 6:
				maxDay=31;
				break;
			case 7:
				maxDay=31;
				break;
			case 8:
				maxDay=30;
				break;
			case 9:
				maxDay=31;
				break;
			case 10:
				maxDay=30;
				break;
			case 11:
				maxDay=31;
				break;
		}
	}
	function resetyear(y){
	    for(i=0;i<cyear.options.length;i++){
	        if(y==cyear.options[i].text){
	            cyear.options.selectedIndex=i;
	            break;
	        }
	    }
	}
	function resetmonth(m){
	    for(i=0;i<cmonth.options.length;i++){
	        if(m==cmonth.options[i].text){
	            cmonth.options.selectedIndex=i;
	            break;
	        }
	    }
	}
	//给builderDate函数提供一个日期参数
	function startBuilderDate(){
		buildDate(new Date(parseInt(cyear.options[cyear.selectedIndex].text),parseInt(cmonth.options[cmonth.selectedIndex].text)-1,1));
	}
    document.write('<div class="calendar" id="rl" onclick="event.cancelBubble=true;"><table align="center" border="1" style="text-align:center;background-color:#e50b0b;"><tr><td colspan="7" id="dd" style="color:#ffeeee;"><select id="cyear" size="1" onchange="startBuilderDate();">');
    for(i=1938;i<2050;i++){
        if(i==_cale_date.getFullYear()){
            document.write("<option selected>"+i+"年</option>");
        }else{
            document.write("<option>"+i+"年</option>");
        }
    }
    document.write('</select><select id="cmonth" size="1" onchange="startBuilderDate();">');
    for(i=1;i<13;i++){
        document.write("<option>"+i+"月</option>");
    }
    document.write('</select>&nbsp;&nbsp;&nbsp;&nbsp;');
    cmonth.selectedIndex=_cale_date.getMonth();
    document.write('</td></tr><tr style="color:#eeffee;"><td>周日</td><td>周一</td><td>周二</td><td>周三</td><td>周四</td><td>周五</td><td>周六</td></tr>');
    /*初始化当前日期*/
	initMonth(_cale_date);
	var sum=42;
	var index=1;
	var minTheDay=0;
	while(minTheDay<sum){
		document.write("<tr>");
		for(i=0;i<7;i++){
			if(minTheDay>=theDay){
				if(index<=maxDay){
					document.write("<td id='d"+minTheDay+"' onclick='rlbox.value=parseInt(cyear.options[cyear.selectedIndex].text)+\"-\"+parseInt(cmonth.options[cmonth.selectedIndex].text)+\"-\"+(this.innerText==\" \"?\"1\":this.innerText);document.getElementById(\"rl\").style.visibility=\"hidden\"' onmouseover='this.style.backgroundColor=\"#e50b0b\";this.style.color=\"white\"' onmouseout='this.style.backgroundColor=\"#fff2f2\";this.style.color=\"black\"' style='background-color:#fff2f2;cursor:hand;'>"+index+"</td>");
				}else{
					document.write("<td id='d"+minTheDay+"' onclick='rlbox.value=parseInt(cyear.options[cyear.selectedIndex].text)+\"-\"+parseInt(cmonth.options[cmonth.selectedIndex].text)+\"-\"+(this.innerText==\" \"?\"1\":this.innerText);document.getElementById(\"rl\").style.visibility=\"hidden\"'  onmouseover='this.style.backgroundColor=\"#e50b0b\";this.style.color=\"white\"' onmouseout='this.style.backgroundColor=\"#fff2f2\";this.style.color=\"black\"' style='background-color:#ffffff;cursor:hand;'>&nbsp</td>");
				}
				index++;
			}
			else{
					document.write("<td id='d"+minTheDay+"' onmouseover='' style='background-color:#ffffff;cursor:hand;'>&nbsp</td>");
			}
			minTheDay++;
			if(minTheDay==sum){
				break;
			}
		}
		document.write("</tr>");
	}
	/*初始化当前日期完*/

	//根据指定日期重新排整日历
	function buildDate(currentDate){
		initMonth(currentDate);
		var sum=42;
		var index=1;
		var minTheDay=0;
		while(minTheDay<sum){
			for(i=0;i<7;i++){
				if(minTheDay>=theDay){
					if(index<=maxDay){
						eval("d"+minTheDay).innerHTML=index+"";
						eval("d"+minTheDay).style.backgroundColor="#fff2f2";
					}else{
						eval("d"+minTheDay).innerHTML="&nbsp;";
						eval("d"+minTheDay).style.backgroundColor="#ffffff";
					}
					index++;
				}
				else{
					eval("d"+minTheDay).innerHTML="&nbsp;";
					eval("d"+minTheDay).style.backgroundColor="#ffffff";
				}
				minTheDay++;
				if(minTheDay==sum){
					break;
				}
			}
		}
	}
	document.write('<tr><td colspan="7"><div id="msg" style="color:white;">'+currentdate+'</div></td></tr></table></div>');
