
Harry.Calendar = new function() {
    this.NewName = "";
    this.insertId = "";
    this.Moveable = true;
    this.ClickObject = null;
    this.InputObject = null;
    this.InputDate = null;
    this.IsOpen = false;
    this.MouseX = 0;
    this.MouseY = 0;

    this.checkoutObj = null;
    this.L_TheYear = new Date().getMonth() + 1; //定义月的变量的初始值
    this.L_TheMonth = new Date().getDay(); //定义月的变量的初始值
    this.L_TheDay = new Date().getFullYear(); //定义年的变量的初始值
    this.L_WDay = new Array(39); //定义写日期的数组
    this.MonHead = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
    this.CurYear = new Date().getFullYear();
    this.GetDateLayer = function() {
        return Harry.frmObj;
    }
    this.CreateHTML = function(obj) {
        var htmlstr = "";
        htmlstr += "<div class='maindiv'>";
        htmlstr += "<div id=\"L_calendar\" class=L_calendar>\r\n";
        htmlstr += "<span id=\"SelectYearLayer\"></span>\r\n";
        htmlstr += "<div id=\"L_calendar-year-month\" class=L_calendar-year-month><div id=\"L_calendar-PrevM\" class=L_calendar-PrevM onclick=\"parent.Harry.Calendar.PrevM()\" title=\"前一月\"><b><<</b></div><div id=\"L_calendar-year\" class=L_calendar-year ></div><div id=\"L_calendar-month\" class=L_calendar-month ></div></div>\r\n";
        htmlstr += "<div id=\"L_calendar-week\" class=L_calendar-week><ul  onmouseup=\"StopMove()\"><li>日</li><li>一</li><li>二</li><li>三</li><li>四</li><li>五</li><li>六</li></ul></div>\r\n";
        htmlstr += "<div id=\"L_calendar-day\" class=L_calendar-day>\r\n";
        htmlstr += "<ul>\r\n";
        for (var i = 0; i < this.L_WDay.length; i++) {
            htmlstr += "<li id=\"L_calendar-day_" + i + "\"></li>";

        }

        htmlstr += "</ul>\r\n";
        htmlstr += "</div>\r\n";
        htmlstr += "</div>";
        //第二屏
        htmlstr += "<div id=\"L_calendar2\" class=L_calendar>";
        htmlstr += "<span id=\"SelectYearLayer2\"></span>";
        htmlstr += "<div id=\"L_calendar-year-month2\" class=L_calendar-year-month><div id=\"L_calendar-year2\" class=L_calendar-year ></div><div id=\"L_calendar-month2\" class=L_calendar-month ></div><div id=\"L_calendar-NextM2\" class=L_calendar-NextM onclick=\"parent.Harry.Calendar.NextM()\" title=\"后一月\"><b>>></b></div></div>\r\n";
        htmlstr += "<div id=\"L_calendar-week2\" id='week2' class=L_calendar-week><ul  onmouseup=\"StopMove()\"><li>日</li><li>一</li><li>二</li><li>三</li><li>四</li><li>五</li><li>六</li></ul></div>\r\n";
        htmlstr += "<div id=\"L_calendar-day2\" class=L_calendar-day>\r\n";
        htmlstr += "<ul>\r\n";
        for (var i = 0; i < this.L_WDay.length; i++) {
            htmlstr += "<li id=\"L_calendar-day2_" + i + "\" ></li>";
        }
        htmlstr += "</ul>\r\n";
        htmlstr += "</div>\r\n";
        htmlstr += "</div></div>";
        //第二屏结束		
        var stylestr = "";
        //css
        stylestr += "<style type=\"text/css\">";

        stylestr += "body{background:#fff;font-size:12px;margin:0px;padding:0px;text-align:left}\r\n";
        stylestr += ".maindiv{float:left;width:288px !important;_width:280px;height:160px;}";
        //框休憩 width:115px !important;width:120px;padding:5px;
        stylestr += ".L_calendar{border:1px solid #999999; float:left;width:140px;padding:1px;text-align:center;color:#000444}";
        //当前年月
        stylestr += ".L_calendar-year{line-height:23px;cursor:default}\r\n";
        //上一页
        stylestr += ".L_calendar-PrevM{position: absolute;top: 3; left: 8;cursor:pointer}"
        //下一页
        stylestr += ".L_calendar-NextM{position: absolute;top: 3; left: 260;cursor:pointer}"
        //星期
        stylestr += ".L_calendar-week{background-color:#7BA5CE;height:17px;line-height:17px;z-index:9998;color:#fff;}\r\n";
        stylestr += ".L_calendar-week ul{cursor:move;list-style:none;margin:0px;padding:0px;}\r\n";
        stylestr += ".L_calendar-week li{width:17px;height:15px;float:left;;margin:1px;padding:0px;text-align:center;}\r\n";

        //天
        stylestr += ".L_calendar-day{z-index:9998;}\r\n";
        stylestr += ".L_calendar-day ul{list-style:none;margin:0px;padding:0px;}\r\n";
        stylestr += ".L_calendar-day li{cursor:pointer;width:17px;height:17px;float:left;;margin:1px;padding:0px;}\r\n";
        //当天日期
        stylestr += ".selectLi{background:#3399CC;}";
        //mouseMove
        stylestr += ".MoveLi{background:#3399CC;}";

        stylestr += "</style>";
        var TempLateContent = stylestr + htmlstr;

        Harry.Base.ShowIframe(TempLateContent, '', obj);
        this.checkoutObj = null;
    }
    this.InsertHTML = function(id, htmlstr) {

        Harry.Base.GetContentDoc().document.getElementById(id).innerHTML = htmlstr;

    }
    this.WriteHead = function(yy, mm, layer)  //往 head 中写入当前的年与月
    {
        this.InsertHTML("L_calendar-year" + layer, yy + "&nbsp;年&nbsp;" + mm + "&nbsp;月");
    }
    this.IsPinYear = function(year)            //判断是否闰平年
    {
        if (0 == year % 4 && ((year % 100 != 0) || (year % 400 == 0))) return true; else return false;
    }
    this.GetMonthCount = function(year, month)  //闰年二月为29天
    {
        var c = this.MonHead[month - 1]; if ((month == 2) && this.IsPinYear(year)) c++; return c;
    }
    this.GetDOW = function(day, month, year)     //求某天的星期几
    {
        var dt = new Date(year, month - 1, day).getDay() / 7; return dt;
    }
    this.GetText = function(obj) {
        if (obj.innerText) { return obj.innerText }
        else { return obj.textContent }
    }
    this.CurrentM = function()//当前月份
    {
        //当前月在每一个格内
        this.SetDay(this.L_TheYear, this.L_TheMonth, "");
        this.CurYear = this.L_TheYear;
        if (this.L_TheMonth == 12) {
            this.L_TheMonth = 0;
            this.L_TheYear += 1;
        }
        this.SetDay(this.L_TheYear, this.L_TheMonth + 1, "2");
    }
    this.PrevM = function()  //往前翻月份
    {
        this.L_TheMonth -= 3;
        if (this.L_TheMonth <= 0) {
            this.L_TheMonth = 12 + this.L_TheMonth;
            this.L_TheYear -= 1;
        }
        this.CurrentM();
    }
    this.NextM = function()  //往后翻月份
    {
        if (this.L_TheMonth == 12) {
            this.L_TheYear++;
            this.L_TheMonth = 1;

        }
        else {
            this.L_TheMonth++;
        }
        this.CurrentM();
    }

    this.SetDay = function(yy, mm, layer)   //主要的写程序**********
    {

        this.WriteHead(yy, mm, layer);

        //设置当前年月的公共变量为传入值
        this.L_TheYear = yy;
        this.L_TheMonth = mm;
        for (var i = 0; i < this.L_WDay.length; i++) { this.L_WDay[i] = "" };  //将显示框的内容全部清空
        var day1 = 1, firstday = new Date(yy, mm - 1, 1).getDay();  //某月第一天的星期几	
        for (i = firstday; day1 < this.GetMonthCount(yy, mm) + 1; i++) { this.L_WDay[i] = day1++; }
        var da = null;

        for (i = 0; i < this.L_WDay.length; i++) {
            da = Harry.Base.GetContentDoc().document.getElementById("L_calendar-day" + layer + "_" + i);

            da.innerHTML = "";
            var month, day;
            day = this.L_WDay[i];
            if (day != "") {

                if (i >= firstday + this.GetMonthCount(yy, mm)) {
                    month = (mm == 1 ? 12 : mm + 1);
                }
                else {
                    month = mm;
                }
                //单击事件
                var sp = this.InputObject.getAttribute("lapse") || "false";
                var tMDate = new Date(yy + "/" + mm + "/" + (day + 1));
                if (sp == "true" && tMDate < new Date()) {
                    da.innerHTML = "<font style='color:#999'>" + day + "</font>";
                    da.style.cursor = "default";
                    da.onmouseover = '';
                    da.onclick = "javascript:return false;";
                }
                else {
                    if (Harry.Base.IsIE()) {
                        da.onclick = Function("Harry.Calendar.DayClick(" + month + "," + day + "," + this.L_TheYear + ");");
                        da.onmouseover = function() { if (this.className != 'selectLi') this.className = 'MoveLi'; };
                        da.onmouseout = function() { if (this.className != 'selectLi') this.className = ''; };
                    }
                    else {

                        da.setAttribute("onclick", "parent.Harry.Calendar.DayClick(" + month + "," + day + "," + this.L_TheYear + ");");
                        da.setAttribute("onmouseover", "javascript:if (this.className!='selectLi') this.className='MoveLi';");
                        da.setAttribute("onmouseout", "javascript:if (this.className!='selectLi') this.className='';");
                    }
                    da.innerHTML = this.L_WDay[i];
                }

                da.title = month + "月" + day + "日";
                //当前日期颜色		
                if (yy == this.InputDate.getFullYear() && month == this.InputDate.getMonth() + 1 && day == this.InputDate.getDate()) {
                    Harry.Base.SetCss(da, "selectLi");
                }
                else {
                    Harry.Base.SetCss(da, "");
                }

            }
        }
    }
    this.DayClick = function(mm, dd, year)  //点击显示框选取日期，主输入函数*************
    {
        var yy = year;
        //if (this.CurYear != this.L_TheYear && this.L_TheMonth == 1 && layer != "2" && layer !=null)
        //{
        //  yy-=1;
        //}
        //判断月份，并进行对应的处理
        if (mm < 1) {
            yy--; mm = 12 + mm;
        }
        else if (mm > 12) {
            yy++; mm = mm - 12;
        }
        if (mm < 10) {
            mm = "0" + mm;
        }
        if (this.ClickObject) {
            if (!dd)
                return;
            if (dd < 10) {
                dd = "0" + dd;
            }
            this.InputObject.value = yy + "-" + mm + "-" + dd; //注：在这里你可以输出改成你想要的格式  
        }
        Harry.Base.CloseIframe();
        //关闭
        var nextObj = this.InputObject.getAttribute("NextFocus") || "";
        if (nextObj != "")
            if (Harry.Base.$ID(nextObj))
            document.getElementById(nextObj).focus();

        if (this.checkoutObj != null) {
            this.SetDate(this.checkoutObj);

        }
    }
    //入口,单击显示日历
    this.SetDate = function(obj) {
        if (obj != this.checkoutObj) {
            this.checkoutObj = null;
        }
        this.InputObject = obj;
        this.ClickObject = obj;
        var patrn = /^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$/;
        var tDate = patrn.exec(obj.value) ? new Date(obj.value.replace(/-/g, '/')) : new Date();

        this.L_TheYear = tDate.getFullYear();
        this.L_TheMonth = tDate.getMonth() + 1;
        //alert(this.L_TheMonth);
        this.InputDate = tDate;
        //当日期文本框获得焦点时，将事件触发对象给clickObj Author:YueBing Dong Date:2009.04.14 [FireFox]不支持本段代码------------start----------
        //        e = window.event || arguments[0];
        //        var srcElement = e.srcElement || e.target;
        //        var srcElement = event.srcElement;
        //        Harry.clickObj = srcElement;
        //------------------------------------------------------------------------------------------------End--------------
        this.CreateHTML(obj);

        var top = Harry.Base.GetY();
        var left = Harry.Base.GetX("bottom");
        this.CurrentM();
    }
    //入口,单击显示日历
    this.ShowCheckOutCalendar = function(obj1, obj2) {
        this.SetDate(obj1);
        this.checkoutObj = obj2;

    }
}
//  <input type="text" id="textfield" onclick="Harry.Calendar.SetDate(this);" lapse=true NextFocus="objtxt"  name="textfield2" />
