﻿//样式表
document.write("<style>");
document.write("#__calendar{width:147px;margin:0;padding:0;}");
document.write("#calendarTable{    margin:0;padding:0;border:1px solid #000;}");
document.write("th,td{margin:0;padding:0px}");
document.write("#calendarTable th ,#calendarTable td{font:12px/20px 宋体,Arial,sans-serif;text-align:center;}");
document.write("#calendarTable thead th.week{border-top:1px solid #CCC;border-bottom:1px solid #ccc;background:#EEE;}");
document.write("#calendarTable thead tr.function th{border:1px solid #fff}");
document.write("#calendarTable thead tr.top{letter-spacing:1px;}");
document.write("#calendarTable thead a{color:#000;width:19px;height:18px;text-decoration:none;display:block;border:1px solid #ccc;background:#E1F1FF;}");
document.write("#calendarTable tbody a:hover{color:#990;width:19px;height:18px;text-decoration:none;display:block;border:1px solid #ccc;background:#E1F1FF;}");
document.write("#calendarTable thead a.today{width:98% !important;width:100%}");
document.write("#calendarTable thead a.today:hover{width:98% !important;width:100%}");
document.write("#calendarTable td{width:21px;height:20px;border-bottom:1px solid #E6E6E6;color:#fff;}");
document.write("#calendarTable th{height:21px;}");
document.write("#calendarTable tr{background:#fff;}"); //初始化时日期数字后面背景颜色,不加这行则为透明(Mars20061020)
document.write("#calendarTable tr.com{background:#fff;}");
document.write("#calendarTable tr.cur{background:#F0FAFF;}");
document.write("#calendarTable tbody a{color:#000;width:19px;height:18px;text-decoration:none;display:block;border:1px solid #fff;}");
document.write("#calendarTable tbody a:hover{color:#990;width:19px;height:18px;text-decoration:none;border:1px solid #E6E6E6;background:#E1F1FF;display:block;}");
document.write("#calendarTable tbody a.today{color:#c00;border:1px solid #DBDBDB;background:#CCE4FF;}");
document.write("#calendarTable tbody a.today:hover{color:#990;border:1px solid #DBDBDB;background:#CCE4FF;}");
document.write("#calendarTable tbody a.selectday{border:1px solid #DBDBDB;background:#EEE;}");
document.write("#calendarTable tbody a.week{color:#c00;}");
document.write("</style>");
//显示HTML
document.write("<div id='__calendar' style='position:absolute;display:none;left: 100px; top: 100px;z-index :99;'></div>");
document.write("<table cellspacing=\"0\" cellpadding=\"0\" id=\"calendarTable\">");
document.write("	<thead>");
document.write("		<tr class=\"top\"><th colspan=\"5\" id=\"calendarsohwdate\"></th><th colspan=\"2\"><a href=\"javascript:void(0);\" class=\"today\" title=\"close\" onclick=\"shut()\">关闭</a></th></tr>");
document.write("		<tr class=\"function\"><th><a href=\"javascript:void(0);\" title=\"向前翻1年\" onclick=\"preYear()\">&lt;&lt;</a></th><th><a href=\"javascript:void(0);\" title=\"向前翻1月\" onclick=\"preMonth()\">&lt;</a></th><th colspan=\"3\"><a href=\"javascript:void(0);\" class=\"today\" title=\"今天\" onclick=\"getDate('0')\">今天</a></th><th><a href=\"javascript:void(0);\" title=\"向后翻1月\" onclick=\"nextMonth()\">&gt;</a></th><th><a href=\"javascript:void(0);\" title=\"向后翻1年\" onclick=\"nextYear()\">&gt;&gt;</a></th></tr>");
document.write("		<tr><th class=\"week\">日</th><th class=\"week\">一</th><th class=\"week\">二</th><th class=\"week\">三</th><th class=\"week\">四</th><th class=\"week\">五</th><th class=\"week\">六</th></tr>");
document.write("	</thead>");
document.write("	<tbody id=\"calendarTbody\"></tbody>");
document.write("</table>");

var _calendar_objouter;
var _calendar_objInput;
var _calendar_isShow = true;
_calendar_objouter=document.getElementById("__calendar"); 
var calendarTable = document.getElementById("calendarTable");
_calendar_objouter.appendChild(calendarTable);

function setday(obj)
{
	_calendar_objInput = obj;
	initDate();//初始化日期,让输入框中的日期自动填充,并显示为这个日期的年月
	writeDate();
	sohwDate();
	//在top和left数值后面加px,已保证在XHTML 1.0下FireFox中正常显示
	_calendar_objouter.style.top =getAbsoluteHeight(_calendar_objInput)+getAbsoluteTop(_calendar_objInput)+"px";
	_calendar_objouter.style.left =getAbsoluteLeft(_calendar_objInput)+"px";
	_calendar_objouter.style.display = "block";
}

function initDate()
{
	var initDay = new Date();
	initDay.setFullYear(initDay.getFullYear());
    if (_calendar_objInput.value != "")
	{    
	    initDay = strToDate(_calendar_objInput.value);
    }
	//NaN为IE错误日期返回值,Invalid Date为FireFox错误日期返回值
	if(initDay!="NaN" && initDay!="Invalid Date")
	{
	
		_calendar_selectDay = initDay;
		_calendar_tempYear = initDay.getFullYear();
		_calendar_tempMonth = initDay.getMonth();
		_calendar_tempDay = initDay.getDate();
	}
}
//格式化字符串转换为日期型
function strToDate(str)
{
  var arys= new Array();
  arys=str.split('-');
  var newDate=new Date(arys[0],arys[1]-1,arys[2]); 
  return newDate;
}
function getAbsoluteHeight(ob){return ob.offsetHeight;}
function getAbsoluteWidth(ob){return ob.offsetWidth;}
function getAbsoluteLeft(ob){var s_el=0;el=ob;while(el){s_el=s_el+el.offsetLeft;el=el.offsetParent;}; return s_el}
function getAbsoluteTop(ob){var s_el=0;el=ob;while(el){s_el=s_el+el.offsetTop ;el=el.offsetParent;}; return s_el}




var _calendar_daysInMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); 
var _calendar_toDay = new Date();
var _calendar_tempYear = _calendar_toDay.getFullYear();
var _calendar_tempMonth = _calendar_toDay.getMonth();
var _calendar_selectDay = new Date();
var _calendar_tempDay = -1;
var _calendar_tbody = document.getElementById("calendarTbody"); 
var _calendar_sohwId = document.getElementById("calendarsohwdate");
var _calendar_thisYear = _calendar_toDay.getFullYear();
var _calendar_thisMonth = _calendar_toDay.getMonth();
function getDays(month, year)
{     
    if (1 == month) return ((0 == year % 4) && (0 != (year % 100))) || (0 == year % 400) ? 29 : 28; 
    else return _calendar_daysInMonth[month]; 
} 

function writeDate() {     
    var curCal = new Date(_calendar_tempYear,_calendar_tempMonth ,1);
    var startDay = curCal.getDay();
    var daily = 0;
    var today = _calendar_toDay.getDate();
    if(_calendar_tempYear != _calendar_toDay.getFullYear() || _calendar_tempMonth != _calendar_toDay.getMonth()) today = -1;
    var selectday = _calendar_tempDay;
    if(_calendar_tempYear != _calendar_selectDay.getFullYear() || _calendar_tempMonth != _calendar_selectDay.getMonth()) selectday = -1;
    var todayStyle = "";
    var weekEndStyle = "";
    var _calendar_tempDate;
    clear();
    var intDaysInMonth =getDays(curCal.getMonth(), curCal.getFullYear());
    var weeks = (intDaysInMonth + startDay) % 7 == 0 ? (intDaysInMonth + startDay) / 7 : parseInt((intDaysInMonth + startDay ) / 7) + 1;    

    for (var intWeek = 1; intWeek <= weeks; intWeek++){ 
        var tr = document.createElement("tr");
        tr.setAttribute("onmouseover","javascript:this.className='cur'");        
        tr.setAttribute("onmouseout","javascript:this.className='com'");
        tr.onmouseover = function (){this.className = "cur";}
        tr.onmouseout = function (){this.className = "com";}
        for (var intDay = 0;intDay < 7;intDay++){
            var td = document.createElement("td");
            if ((intDay == startDay) && (0 == daily)) 
                daily = 1; 
            
            //对不同的日期,选择不同的样式显示形式
            if(today == daily)
				todayStyle="today";
            else if(selectday == daily)
				todayStyle="selectday";
			else if (intDay == 6 || intDay == 0) 
				weekEndStyle = "week" ;
            _calendar_tempDate=new Date(_calendar_tempYear,_calendar_tempMonth,daily+1)
            
            if ((daily > 0) && (daily <= intDaysInMonth)) //显示时限定选择
            { 
                if (_calendar_toDay.getTime()>_calendar_tempDate.getTime())
                {
                td.innerHTML = "<font color=#888888>" + daily + "</font>"
                }else{
                td.innerHTML = "<a href=\"javascript:void(0);\" class=\""+ weekEndStyle + todayStyle +"\" onclick=\"getDate('"+daily+"')\" title=\""+eval(_calendar_tempMonth+1)+"月"+daily+"日\">" + daily + "</a>";
                }
                todayStyle = "";
                weekEndStyle = "";
                daily++;
            }else{ 
                td.innerHTML = "&nbsp;"; 
                todayStyle = "";
                weekEndStyle = "";
            }            
            tr.appendChild(td);            
        }
        _calendar_tbody.appendChild(tr);
    } 
}

function getDate(day){
    var year , month ,date;
    if(day == "0"){
        year = _calendar_toDay.getFullYear();
        month = eval(_calendar_toDay.getMonth()+1) < 10 ? "0"+eval(_calendar_toDay.getMonth()+1) : eval(_calendar_toDay.getMonth()+1);
        date = _calendar_toDay.getDate() < 10 ? "0"+_calendar_toDay.getDate() : _calendar_toDay.getDate();
    }else{
        year = _calendar_tempYear;
        month = eval(_calendar_tempMonth+1) < 10 ? "0"+eval(_calendar_tempMonth+1) : eval(_calendar_tempMonth+1);
        date = day < 10 ? "0"+day : day;        
    }
    _calendar_objInput.value = year + "-" + month +"-"+ date;
    close();
}
function sohwDate(){
    _calendar_sohwId.innerHTML = _calendar_tempYear + "年" + eval(_calendar_tempMonth+1) +"月";
}
function preYear(){
    _calendar_isShow = false;
    if(_calendar_tempYear > 999 && _calendar_tempYear < 10000){
        _calendar_tempYear--;
    }else{
        alert("年份超出范围（1000-9999）！");
    }
    sohwDate();
    writeDate();
}
function nextYear(){
    _calendar_isShow = false;
    if(_calendar_tempYear > 999 && _calendar_tempYear < 10000){
        _calendar_tempYear++;
    }else{
        alert("年份超出范围（1000-9999）！");
    }
    sohwDate();
    writeDate();
}

function preMonth(){
    _calendar_isShow = false;
    if(_calendar_tempMonth >= 1){_calendar_tempMonth--}else{_calendar_tempYear--;_calendar_tempMonth = 11;}
    sohwDate();
    writeDate();
}
function nextMonth(){
    _calendar_isShow = false;
    if(_calendar_tempMonth == 11){_calendar_tempYear++;_calendar_tempMonth = 0}else{_calendar_tempMonth++}
    sohwDate();
    writeDate();
}
function clear(){
    var nodes = _calendar_tbody.childNodes;
    var nodesNum = nodes.length; 
    for(var i=nodesNum-1;i>=0;i--) { 
        _calendar_tbody.removeChild(nodes[i]); 
    }
}
function shut(){
    close();
}
function close(){
    _calendar_tempYear = _calendar_toDay.getFullYear();
    _calendar_tempMonth = _calendar_toDay.getMonth();
    _calendar_objouter.style.display = "none"
    _calendar_objouter.style.top = 0;
    _calendar_objouter.style.left = 0;
}
function vent(event){
    if(document.all){
        if(_calendar_isShow){
            if (window.event.srcElement != _calendar_objouter && window.event.srcElement != _calendar_objInput) close();
            _calendar_isShow = true;
            return;
        }
        _calendar_isShow = true;        
    }else{
        if(_calendar_isShow){
            if(event.target != _calendar_objouter && event.target != _calendar_objInput) close();
            _calendar_isShow = true;
        }
        _calendar_isShow = true;
    }
}
document.onclick = vent;




