

var bMoveable = true;
var strFrame;

document.writeln('<iframe id=endDateLayer frameborder=0 width=300 height=190 style="position: absolute;  z-index: 9998; display: none"></iframe>');
strFrame = '<style>';
strFrame += 'INPUT.button{BORDER-RIGHT: #467EF0 1px solid;BORDER-TOP: #467EF0 1px solid;BORDER-LEFT: #467EF0 1px solid;';
strFrame += 'BORDER-BOTTOM: #467EF0 1px solid;BACKGROUND-COLOR: #467EF0;font-family:宋体;}';
strFrame += 'TD{FONT-SIZE: 12px;font-family:宋体;}';
strFrame += '.tbody{border-color:#467EF0; background-color:#FFFFFF; border-collapse:collapse; }';
strFrame += '</style>';
strFrame += '<scr' + 'ipt>';
strFrame += 'var datelayerx,datelayery;';
strFrame += 'var bDrag;';
strFrame += 'function document.onmousemove()';
strFrame += '{if(bDrag && window.event.button==1)';
strFrame += '	{var DateLayer=parent.document.all.endDateLayer.style;';
strFrame += '		DateLayer.posLeft += window.event.clientX-datelayerx;';
strFrame += '		DateLayer.posTop += window.event.clientY-datelayery;}}';
strFrame += 'function DragStart()';
strFrame += '{var DateLayer=parent.document.all.endDateLayer.style;';
strFrame += '	datelayerx=window.event.clientX;';
strFrame += '	datelayery=window.event.clientY;';
strFrame += '	bDrag=true;}';
strFrame += 'function DragEnd(){';
strFrame += '	bDrag=false;}';
strFrame += '</scr' + 'ipt>';
strFrame += '<div style="z-index:9999;position: absolute; left:0; top:0;" onselectstart="return false">';
// 第一个月
strFrame += '<table class="tbody" border="1" width=284 height=150><tr><td width=142 ><table class="tbody" width=142>';
strFrame += '    <tr><td width=142 height=21  bgcolor=#467EF0>';
strFrame += '        <table height=21>';
strFrame += '            <tr align=center >';
strFrame += '                <td width=21 align="center" style="color:#FFFFFF"';
strFrame += '					   onclick="parent.PreviousMonth()" title="上一月" style="cursor:pointer;font-size:14px;">←</td>';
strFrame += '				   <td align="left" style="color:#FFFFFF;font-size:10pt;">';
strFrame += '					   &nbsp;&nbsp;<span id=YearHead></span><span id=MonthHead></span></td>';
strFrame += '			   </tr>';
strFrame += '		   </table></td></tr>';
strFrame += '	   <tr><td width=142 height=18 >';
strFrame += '		   <table bgcolor="#C4D2F0">';
strFrame += '	   <tr><td width=20 align=center>日</td>';
strFrame += '<td width=20 align=center>一</td><td width=20 align=center>二</td>';
strFrame += '<td width=20 align=center>三</td><td width=20 align=center>四</td>';
strFrame += '<td width=20 align=center>五</td><td width=20 align=center>六</td></tr>';
strFrame += '</table></td></tr>';
strFrame += '  <tr ><td width=142 height=120>';
strFrame += '    <table width=142>';
var n = 0; for (j = 0; j < 5; j++) {
    strFrame += '<tr>'; for (i = 0; i < 7; i++) {
        strFrame += '<td width=20 height=20 align=center  id=Day' + n + ' style="font-size:12px;" onclick=parent.DayClick(this.innerText,0)></td>'; n++;
    }
    strFrame += '</tr>';
}
strFrame += '      <tr align=center>';
for (i = 35; i < 37; i++) strFrame += '<td width=20 height=20 id=Day' + i + ' style="font-size:12px;"  onclick="parent.DayClick(this.innerText,0)"></td>';
strFrame += '        <td colspan=5 align=right></td></tr>';
strFrame += '    </table></td></tr></table></td>';
// 第二个月
strFrame += '   <td width=142><table class="tbody" width=142>';
strFrame += '    <tr><td width=142 height=21  bgcolor=#467EF0>';
strFrame += '        <table width=142 height=21>';
strFrame += '            <tr align=center >';
strFrame += '				   <td width=121 align="right" style="color:#FFFFFF;font-size:10pt;">';
strFrame += '					   <span  id=YearHead2></span><span id=MonthHead2></span>&nbsp;&nbsp;</td>';
strFrame += '                <td width=21 align="center" style="color:#FFFFFF"';
strFrame += '					   onclick="parent.NextMonth()" title="下一月" style="cursor:pointer;font-size:14px;">→</td>';
strFrame += '			   </tr>';
strFrame += '		   </table></td></tr>';
strFrame += '	   <tr><td width=142 height=18 >';
strFrame += '		   <table bgcolor="#C4D2F0">';
strFrame += '	   <tr><td width=20 align=center>日</td>';
strFrame += '<td width=20 align=center>一</td><td width=20 align=center>二</td>';
strFrame += '<td width=20 align=center>三</td><td width=20 align=center>四</td>';
strFrame += '<td width=20 align=center>五</td><td width=20 align=center>六</td></tr>';
strFrame += '</table></td></tr>';
strFrame += '  <tr ><td width=142 height=120>';
strFrame += '    <table width=142>';
var n = 0; for (j = 0; j < 5; j++) {
    strFrame += ' <tr align=center >'; for (i = 0; i < 7; i++) {
        strFrame += '<td width=20 height=20 id=DayT' + n + ' style="font-size:12px" onclick=parent.DayTClick(this.innerText,0)></td>'; n++;
    }
    strFrame += '</tr>';
}
strFrame += '      <tr align=center>';
for (i = 35; i < 37; i++) strFrame += '<td width=20 height=20 id=DayT' + i + ' style="font-size:12px"  onclick="parent.DayTClick(this.innerText,0)"></td>';
strFrame += '        <td colspan=5 align=right style="color:#1478eb"></td></tr>';
strFrame += '    </table></td></tr>';
strFrame += '	</table></td></tr></table></div>';

window.frames.endDateLayer.document.writeln(strFrame);
window.frames.endDateLayer.document.close(); 	//解决ie进度条不结束的问题


//==================================================== WEB 页面显示部分 ======================================================
var outObject;
var outButton; 	//点击的按钮

var outDate = ""; 	//存放对象的日期
var odatelayer = window.frames.endDateLayer.document.all; 	//存放日历对象
var odatelayer = window.endDateLayer.document.all;

function setday(tt, obj) //主调函数
{
    if (arguments.length > 2) { alert("对不起！传入本控件的参数太多！"); return; }
    if (arguments.length == 0) { alert("对不起！您没有传回本控件任何参数！"); return; }
    var dads = document.all.endDateLayer.style;
    var th = tt;
    var ttop = tt.offsetTop; //TT控件的定位点高

    var thei = tt.clientHeight; //TT控件本身的高
    var tleft = tt.offsetLeft; //TT控件的定位点宽

    var ttyp = tt.type; //TT控件的类型
   

    while (tt = tt.offsetParent) { ttop += tt.offsetTop; tleft += tt.offsetLeft; }
    dads.top = (ttyp == "image") ? ttop + thei : ttop + thei + 6;
    dads.left = tleft;
    // outObject = (arguments.length == 1) ? th : obj;
    // outButton = (arguments.length == 1) ? null : th;	//设定外部点击的按钮
    outButton = window.event.srcElement;	//设定外部点击的按钮

    
    outObject = th;
    //outButton = null;
    //根据当前输入框的日期显示日历的年月
    var reg = /^(\d+)-(\d{1,2})-(\d{1,2})/; 	//不含时间
    var r = (arguments.length == 1) ? outObject.value.match(reg) : obj.value.match(reg);
    if (r != null) {

        r[2] = r[2] - 1;
        var d = new Date(r[1], r[2], r[3]);
        if (d.getFullYear() == r[1] && d.getMonth() == r[2] && d.getDate() == r[3]) {
            outDate = d;
            parent.TheYear = r[1];
            parent.TheMonth = r[2];
            parent.TheDate = r[3];
        }
        else {
            outDate = "";
        }
        SetDay(r[1], r[2] + 1);
    }
    else {
        outDate = "";

       SetDay(new Date().getFullYear(), new Date().getMonth() + 1);
    }
         
    dads.display = '';

    WriteHead(TheYear, TheMonth);

    try {
        event.returnValue = false;
    }
    catch (e) {
        //此处排除错误，错误原因暂未找到。
    }
}

var MonHead = new Array(12); //定义阳历中每个月的最大天数

MonHead[0] = 31; MonHead[1] = 28; MonHead[2] = 31; MonHead[3] = 30; MonHead[4] = 31; MonHead[5] = 30;
MonHead[6] = 31; MonHead[7] = 31; MonHead[8] = 30; MonHead[9] = 31; MonHead[10] = 30; MonHead[11] = 31;

var TheYear = new Date().getFullYear(); //定义年的变量的初始值

var TheMonth = new Date().getMonth() + 1; //定义月的变量的初始值

var TheDate = new Date().getDate(); //定义日的变量的初始值

var WDay = new Array(37); //定义写日期的数组

var WDayT = new Array(37); //定义写日期的数组 第二个月

function document.onclick() //任意点击时关闭该控件	//ie6的情况可以由下面的切换焦点处理代替
{

    with (window.event) {
       
        if (srcElement != outObject && srcElement != outButton)
            closeLayer();
    }
}


function document.onkeyup()		//按Esc键关闭，切换焦点关闭
{
    if (window.event.keyCode == 27) {
        if (outObject) outObject.blur();
        closeLayer();
    }
    else if (document.activeElement) {
        if (document.activeElement != outObject && document.activeElement != outButton) {
            closeLayer();
        }
    }
}

function WriteHead(yy, mm, ss)	//往 head 中写入当前的年与月
{
    odatelayer.YearHead.innerText = yy + ".";
    odatelayer.MonthHead.innerText = format(mm);
    if (mm == 12) {
        var y2 = parseInt(yy) + 1;
        odatelayer.YearHead2.innerText = y2 + ".";
        odatelayer.MonthHead2.innerText = 1;
    }
    else {
        odatelayer.YearHead2.innerText = yy + ".";
        odatelayer.MonthHead2.innerText = format(mm + 1);
    }
}

function closeLayer()	//这个层的关闭
{
    var o = document.getElementById("endDateLayer");

    if (o != null) {
        o.style.display = "none";
    }
}

function showLayer()	//这个层
{
    document.all.endDateLayer.style.display = "";
}

function IsPinYear(year)	//判断是否闰平年
{
    if (0 == year % 4 && ((year % 100 != 0) || (year % 400 == 0))) return true; else return false;
}

function GetMonthCount(year, month)	//闰年二月为29天
{
    var c = MonHead[month - 1]; if ((month == 2) && IsPinYear(year)) c++; return c;
}

function GetDOW(day, month, year)	//求某天的星期几
{
    var dt = new Date(year, month - 1, day).getDay() / 7; return dt;
}

function PrevY()	//往前翻 Year
{
    if (TheYear > 999 && TheYear < 10000) { TheYear--; }
    else { alert("年份超出范围（1000-9999）！"); }
    SetDay(TheYear, TheMonth);
}
function NextY()	//往后翻 Year
{
    if (TheYear > 999 && TheYear < 10000) { TheYear++; }
    else { alert("年份超出范围（1000-9999）！"); }
    SetDay(TheYear, TheMonth);
}
function setNull() {
    outObject.value = '';
    closeLayer();
}
function Today()	//Today Button
{
    parent.TheYear = new Date().getFullYear();
    parent.TheMonth = new Date().getMonth() + 1;
    parent.TheDate = new Date().getDate();

    if (TheMonth < 10 && TheMonth.length < 2)	//格式化成两位数字
    {
        parent.TheMonth = "0" + parent.TheMonth;
    }
    if (parent.TheDate < 10 && parent.TheDate.length < 2)	//格式化成两位数字
    {
        parent.TheDate = "0" + parent.TheDate;
    }
    //SetDay(TheYear,TheMonth);
    if (outObject) {
        outObject.value = parent.TheYear + "-" + format(parent.TheMonth) + "-" +
							format(parent.TheDate); //注：在这里你可以输出改成你想要的格式

    }
    closeLayer();
}
function PreviousMonth()	//往前翻月份
{
    if (TheMonth > 1) { TheMonth-- } else { TheYear--; TheMonth = 12; }
    SetDay(TheYear, TheMonth);
}
function NextMonth()	//往后翻月份
{
    if (TheMonth == 12) { TheYear++; TheMonth = 1 } else { TheMonth++ }
    SetDay(TheYear, TheMonth);
}

function SetDay(yy, mm)	//主要的写程序**********
{

   WriteHead(yy, mm);
    //设置当前年月的公共变量为传入值

    TheYear = yy;
    TheMonth = mm;
    //第一个月
    for (var i = 0; i < 37; i++) { WDay[i] = "" }; //将显示框的内容全部清空

    var day1 = 1, day2 = 1, firstday = new Date(yy, mm - 1, 1).getDay(); //某月第一天的星期几


    for (i = firstday; day1 < GetMonthCount(yy, mm) + 1; i++) { WDay[i] = day1; day1++; }

    for (i = 0; i < 37; i++) {
        var da = eval("odatelayer.Day" + i)	//书写新的一个月的日期星期排列

        //初始化
        da.style.backgroundColor = "#ffffff";
        da.onmouseover = Function("onMouseOut(this)");
        da.onmouseout = Function("onMouseOut(this)");

        if (WDay[i] != "") {
            //初始化边框
            da.style.color = "#467EF0";

            //本月的部分
            da.innerHTML = "<b>" + WDay[i] + "</b>";
            da.title = mm + "月" + WDay[i] + "日";
            da.onclick = Function("DayClick(this.innerText,0)"); 	//给td赋予onclick事件的处理
       
            //如果是当前选择的日期，则显示亮蓝色的背景；如果是当前日期，则显示暗黄色背景
            if (!outDate) {
                if ((yy < new Date().getFullYear()) || (yy == new Date().getFullYear() && mm < new Date().getMonth() + 1) || (yy == new Date().getFullYear() && mm == new Date().getMonth() + 1 && WDay[i] < new Date().getDate())) {
                    da.style.color = "#CCCCCC";

                }
                else if ((yy == new Date().getFullYear() && mm == new Date().getMonth() + 1 && WDay[i] == new Date().getDate())) {
                    da.style.backgroundColor = "#7DA3F0";

                    da.onmouseover = Function("onMouseOver(this)");
                    da.onmouseout = Function("onMouseOutToday(this)");
                }
                else {

                    da.onmouseover = Function("onMouseOver(this)");
                }
            }
            else {
                if ((yy < new Date().getFullYear()) || (yy == new Date().getFullYear() && mm < new Date().getMonth() + 1) || (yy == new Date().getFullYear() && mm == new Date().getMonth() + 1 && WDay[i] < new Date().getDate())) {
                    da.style.color = "#CCCCCC";
                }
                else if (yy == new Date().getFullYear() && mm == new Date().getMonth() + 1 && WDay[i] == new Date().getDate()) {
                    da.style.backgroundColor = "#7DA3F0";

                    da.onmouseover = Function("onMouseOver(this)");
                    da.onmouseout = Function("onMouseOutToday(this)");
                }
                else if (yy == outDate.getFullYear() && mm == outDate.getMonth() + 1 && WDay[i] == outDate.getDate()) {
                    da.style.backgroundColor = "#C5D3F0";

                    da.onmouseover = Function("onMouseOver(this)");
                    da.onmouseout = Function("onMouseOutSelected(this)");

                }
                else {
                    da.onmouseover = Function("onMouseOver(this)");
                }
            }
            da.style.cursor = "hand";
        }
        else { da.innerHTML = ""; da.style.backgroundColor = ""; da.style.cursor = "default"; }
    }
    //第二个月
    for (i = 0; i < 37; i++) { WDayT[i] = "" }; //将显示框的内容全部清空

    day1 = 1, day2 = 1, firstday = new Date(yy, mm, 1).getDay(); //某月第一天的星期几

    if (mm == 12) {
        yy++; mm = 1;
    }

    else {
        mm++;
    }

    for (i = firstday; day1 < GetMonthCount(yy, mm) + 1; i++) { WDayT[i] = day1; day1++; }

    for (i = 0; i < 37; i++) {
        var da2 = eval("odatelayer.DayT" + i)	//书写新的一个月的日期星期排列

        //初始化
        da2.style.backgroundColor = "#ffffff";
        da2.onmouseover = Function("onMouseOut(this)");
        da2.onmouseout = Function("onMouseOut(this)");

        if (WDayT[i] != "") {
            //初始化边框
            da2.style.color = "#467EF0";

            //本月的部分
            da2.innerHTML = "<b>" + WDayT[i] + "</b>";
            da2.title = mm + "月" + WDayT[i] + "日";
            da2.onclick = Function("DayTClick(this.innerText,0)"); 	//给td赋予onclick事件的处理

            //如果是当前选择的日期，则显示亮蓝色的背景；如果是当前日期，则显示暗黄色背景
            if (!outDate) {
                if ((yy < new Date().getFullYear()) || (yy == new Date().getFullYear() && mm < new Date().getMonth() + 1) || (yy == new Date().getFullYear() && mm == new Date().getMonth() + 1 && WDayT[i] < new Date().getDate())) {
                    da2.style.color = "#CCCCCC";
                }
                else if ((yy == new Date().getFullYear() && mm == new Date().getMonth() + 1 && WDayT[i] == new Date().getDate())) {
                    da2.style.backgroundColor = "#C5D3F0";
                    da2.onmouseover = Function("onMouseOver(this)");
                    da2.onmouseout = Function("onMouseOutToday(this)");
                }
                else {
                    da2.onmouseover = Function("onMouseOver(this)");
                }
            }
            else {
                if ((yy < new Date().getFullYear()) || (yy == new Date().getFullYear() && mm < new Date().getMonth() + 1) || (yy == new Date().getFullYear() && mm == new Date().getMonth() + 1 && WDayT[i] < new Date().getDate())) {
                    da2.style.color = "#CCCCCC";
                }
                else if (yy == new Date().getFullYear() && mm == new Date().getMonth() + 1 && WDayT[i] == new Date().getDate()) {
                    da2.style.backgroundColor = "#C5D3F0";
                    da2.onmouseover = Function("onMouseOver(this)");
                    da2.onmouseout = Function("onMouseOutToday(this)");
                }
                else if (yy == outDate.getFullYear() && mm == outDate.getMonth() + 1 && WDayT[i] == outDate.getDate()) {
                    da2.style.backgroundColor = "#C5D3F0";
                    da2.onmouseover = Function("onMouseOver(this)");
                    da2.onmouseout = Function("onMouseOutSelected(this)");

                }
                else {
                    da2.onmouseover = Function("onMouseOver(this)");
                }
            }
            da2.style.cursor = "hand";
        }
        else { da2.innerHTML = ""; da2.style.backgroundColor = ""; da2.style.cursor = "default"; }
    }
}

var NextPopUpID = ""; // 如果非空串, 则设置完后自动弹出该ID对应的控件的日历
function DayClick(n, ex)	//点击显示框选取日期，主输入函数*************
{
    parent.TheDate = n;
    var yy = TheYear;
    var mm = parseInt(TheMonth) + ex; //ex表示偏移量，用于选择上个月份和下个月份的日期
    //判断月份，并进行对应的处理

    if (mm < 1) {
        yy--;
        mm = 12 + mm;
    }
    else if (mm > 12) {
        yy++;
        mm = mm - 12;
    }

    if (mm < 10) { mm = "0" + mm; }

    if (outObject) {
        if (!n) {	//outObject.value="";
            return;
        }
        if (n < 10) { n = "0" + n; }

        if ((yy < new Date().getFullYear()) || (yy == new Date().getFullYear() && mm < new Date().getMonth() + 1) || (yy == new Date().getFullYear() && mm == new Date().getMonth() + 1 && n < new Date().getDate())) {

        }
        else {
            WriteDateTo(yy, mm, n);

            var obj = null;
            if (NextPopUpID != null && NextPopUpID != "") {
                obj = document.getElementById(NextPopUpID);
                NextPopUpID = "";
            }
            
            closeLayer();

            if (obj != null) {
                setday(obj, outObject);
            }
        }
    }
    else { closeLayer(); alert("您所要输出的控件对象并不存在！"); }
}



function DayTClick(n, ex)	//点击显示框选取日期，主输入函数*************
{
    parent.TheDate = n;
    var yy;
    var mm;
    if (TheMonth == 12) { yy = parseInt(TheYear) + 1; mm = 1; }
    else { yy = TheYear; mm = parseInt(TheMonth) + 1 + ex; }
    //判断月份，并进行对应的处理

    if (mm < 1) {
        yy--;
        mm = 12 + mm;
    }
    else if (mm > 12) {
        yy++;
        mm = mm - 12;
    }

    if (mm < 10) { mm = "0" + mm; }

    if (outObject) {
        if (!n) {	//outObject.value="";
            return;
        }
        if (n < 10) { n = "0" + n; }

        if ((yy < new Date().getFullYear()) || (yy == new Date().getFullYear() && mm < new Date().getMonth() + 1) || (yy == new Date().getFullYear() && mm == new Date().getMonth() + 1 && n < new Date().getDate())) {

        }
        else {
            WriteDateTo(yy, mm, n);

            var obj = null;
            if (NextPopUpID != null && NextPopUpID != "") {
                obj = document.getElementById(NextPopUpID);
                NextPopUpID = "";
            }

            closeLayer();

            if (obj != null) {
                setday(obj, outObject);
            }
        }
    }
    else { closeLayer(); alert("您所要输出的控件对象并不存在！"); }
}

function format(n)	//格式化数字为两位字符表示
{
    var m = new String();
    var tmp = new String(n);
    if (n < 10 && tmp.length < 2) {
        m = "0" + n;
    }
    else {
        m = n;
    }
    return m;
}

function WriteDateTo(yy, mm, n) {


    outObject.style.color = "#000000";
    outObject.value = yy + "-" + format(mm) + "-" + format(n); //注：在这里你可以输出改成你想要的格式

}


function onMouseOver(obj) {
    obj.style.backgroundColor = "#C5D3F0";
}

function onMouseOut(obj) {
    obj.style.backgroundColor = "#ffffff";
}

function onMouseOutToday(obj) {
    obj.style.backgroundColor = "#C5D3F0";
}
function onMouseOutSelected(obj) {
    obj.style.backgroundColor = "#C5D3F0";
}

function showCalendar(obj, type, idName) {
 
    
    
    if (type && type < 10) {
        if (obj.value == "") {
            obj.style.color = "#767677";
            obj.value = "yyyy-mm-dd";
        }
    } else {
      
        if (obj.value == "yyyy-mm-dd") {
            obj.style.color = "#000000";
            obj.value = "";
        }
        if (type && type == 10) {
            if (arguments.length < 3) {
                NextPopUpID = "outDate";
            } else {
                NextPopUpID = idName;
            }
        } else {
            NextPopUpID = "";
        }
        setday(obj);
    }
}



