/**
 * v 1.2.1
 * 纠正了onclick事件对整体的影响，
 * 同时纠正了页面滚动时引起的layer错位
 * 使得弹出的layer能够遮住select对象。
 * 修复了对于形如"1999-02-11asdf"这样的日期类型的识别错误
 *
 * 本javascript脚本用于日历式的日期输入
 * 方法是：先用一个<script>标记连接此脚本，再用如下的页面代码连接：
 * <input name=birth value="聚焦选择日期" onfocus="pickDate(this)">
 * 在页面上可以对多个<input>标记进行日期选择，这些<input>标记可以在<form>标记内，也可以不在；
 * 而且，标记的名称可有可无，重名也可，也就是说，不作任何限制。
 * 具体使用请参照 pickDate.js.htm 示范页面。
 *
 * 本脚本中使用了下列全局javascript变量或函数名：
 * spanPickDate,pickDate_year,pickDate_month,pickDate_currentInput,pickDate_currentDate
 * pickDate(),pickDate_listDays(),pickDate_hideLayer()
 * 因此调用本脚本的页面最好避免使用上面列出的javascript变量或函数名称。
 */

function pickDate(tag) {
	if(typeof(tag) == 'undefined' || tag == null) {
		alert("非法的标记："+tag);
		return;
	}

    var cal = document.getElementById("spanPickDate");
    if(cal == null) {
        cal = document.body.insertBefore(document.createElement("span"),document.body.firstChild);
        cal.style.display = "none";
        cal.style.position = "absolute";
        cal.id = "spanPickDate";
        cal.innerHTML =
            "<br><table style='font-size:9pt; background-color:lightyellow; border:1px solid black' cellspacing=0 cellpadding=1 border=0>"+
              "<tr>"+
                "<td align=center nowrap>"+
                  "<a style='text-decoration:none; color:blue' href='#' onclick='pickDate_year.innerHTML--;pickDate_year.onclick();return false;'>&lt;-</a>"+
                  "<a id=pickDate_year style='color:red' onclick='pickDate_listDays(pickDate_year.innerHTML,pickDate_month.innerHTML);return false;'>9999</a>"+
                  "<a style='text-decoration:none; color:blue' href='#' onclick='pickDate_year.innerHTML++;pickDate_year.onclick();return false;'>-&gt;</a>年"+
                  "<a style='text-decoration:none; color:blue' href='#' onclick='if(pickDate_month.innerHTML>1)pickDate_month.innerHTML--;else{pickDate_month.innerHTML=12;pickDate_year.innerHTML--;}; pickDate_month.onclick();return false;'>&lt;-</a>"+
                  "<a id=pickDate_month style='color:red' onclick='if(innerHTML<10)innerHTML=&quot;0&quot;+innerHTML; pickDate_listDays(pickDate_year.innerHTML,pickDate_month.innerHTML); return false;'>99</a>"+
                  "<a style='text-decoration:none; color:blue' href='#' onclick='if(pickDate_month.innerHTML<12)pickDate_month.innerHTML++;else{pickDate_month.innerHTML=1;pickDate_year.innerHTML++;}; pickDate_month.onclick();return false;'>-&gt;</a>月 "+
                  "<a onclick='pickDate_hideLayer();return false' href='#' title='关闭本日历' style='text-decoration:none; color:blue; border: 1px solid blue;'><b>×</b></a>"+
                "</td>"+
              "</tr>"+
              "<tr>"+
                "<td>"+
                  "<span id=pickDate_days></span>"+
                "</td>"+
              "</tr>"+
            "</table><style></style>";
    }

    var dt = tag.value;
    //取默认选中的时间：如果输入框已有日期，取该日期，否则取当前日期
    var matchResult=dt.match(/(\d{4,4})-(\d{2,2})-(\d{2,2})/);
    if(matchResult)
    {
    	if(matchResult[2]>0&&matchResult[2]<13&&matchResult[3]>0&&matchResult[3]<32)
    	{
        	dt = new Date(matchResult[1],matchResult[2]-1,matchResult[3]);
        }else{
        	dt=new Date();
	}
    } else {
        dt = new Date();
    }

    pickDate_currentDate = dt;
    pickDate_currentInput = tag;
    pickDate_year.innerHTML = dt.getFullYear();
    pickDate_month.innerHTML = dt.getMonth()+1;
    pickDate_month.onclick();

    if(navigator.appName.search(/^Microsoft/)>=0 && cal.style.display == 'none') {
        document.styleSheets[0].addRule("select","visibility:hidden",0);
    }

    tag.parentNode.insertBefore(cal,tag);
    cal.style.display = "";

}

var pickDate_currentInput = null;

function pickDate_hideLayer() {
    spanPickDate.style.display='none';

    if(navigator.appName.search(/^Microsoft/)>=0)
        document.styleSheets[0].removeRule(0);
}

function pickDate_listDays(year,month) {
    var days = "<table width=100% style='font-size:9pt'><tr><td style='color:red'>日</td><td>一</td><td>二</td><td>三</td><td>四</td><td>五</td><td style='color:red'>六</td></tr>";
    //在此将每天的链接生成：
    var day = new Date(year,month-1,1).getDay();
    days += "\n<tr>";
    for(var i = 0; i < day; i++) days += "<td></td>";

    var daysOfMonth = 31;
    if(month == 4 || month == 6 || month == 9 || month == 11) daysOfMonth = 30;
    if(month == 2) {
        if(year % 4 != 0 || year % 100 == 0 && year % 400 != 0) daysOfMonth = 28; //非闰年？
        else daysOfMonth = 29;
    }

    for(var i = 1; i <= daysOfMonth; i++) {
        var isToday = pickDate_currentDate.getFullYear() == year && pickDate_currentDate.getMonth()+1 == month
            && pickDate_currentDate.getDate() == i;
        days += "<td onmousemove='style.backgroundColor=&quot;pink&quot;' onmouseout='style.backgroundColor=&quot;&quot;' bgcolor='"+(isToday?'aquamarine':'')
            +"' onclick='pickDate_currentInput.value = pickDate_year.innerHTML+&quot;-&quot;+pickDate_month.innerHTML+&quot;-&quot;+innerHTML; pickDate_hideLayer()' "
            +" style='cursor:default; color:"+(day == 0 || day == 6 ? "red" : "green")+"'>"+(i < 10 ? "0" : "")+i+"</td>";

        if(++day == 7) {
            days += "</tr>\n</tr>";
            day = 0;
        }
    }

    days += "</tr></table>";
    pickDate_days.innerHTML = days;
}

