﻿// Created By QHY 2007-9-3

function CalendarBox ()
{
    this._title = "";

    this._ceilingDate = new Date (2100, 0, 1);//2100-01-01
    this._floorDate = new Date (1900, 0, 1);//1900-01-01

    this._year = this._floorDate.getFullYear();
    this._month = 1;

    this._current = this._floorDate;

    this._handler = null;
    this._parentWin  = null;

    this._dayHeader = new Array("", "日", "一", "二", "三", "四", "五", "六");
    this._dayHeaderFull = new Array("", "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
    this._weekEnd = new Array("日", "六");
}

CalendarBox.prototype.initialize = function (title, year, month)
{
    this._title = (title == null ? "Date" : String (title));
    this._year = (year == null ? this._floorDate.getFullYear() : parseInt (year));
    this._month = (month == null ? 1 : parseInt (month));

    while (this._month < 1)
    {
        this._year -= 1;
        this._month += 12;
    }
    while (this._month > 12)
    {
        this._year += 1;
        this._month -= 12;
    }

    if (this._year < this._floorDate.getFullYear())
    {
        this._year = this._floorDate.getFullYear();
    }
    if (this._year > this._ceilingDate.getFullYear())
    {
        this._year = this._ceilingDate.getFullYear();
    }
}


CalendarBox.prototype.getCurrent = function ()
{
    return new Date (this._current.getTime ());
}

CalendarBox.prototype.setCurrent = function (current)
{
    this._current = (current == null ? this._floorDate : new Date (current.getTime ()));

    if (this._year <= this._floorDate.getFullYear())
    {
        var display = (this._current.getTime () <= this._floorDate.getTime() ? new Date () : this._current);

        this._year = display.getFullYear ();
        this._month = display.getMonth () + 1;
    }
}

CalendarBox.prototype.setHandler = function (handler)
{
    this._handler = (typeof (handler) == "function" ? handler : null);
}

CalendarBox.prototype.setParentWin = function (parentWin)
{
    this._parentWin = (typeof (parentWin) == "object" ? parentWin : null);
}

CalendarBox.prototype.close = function ()
{
	if (this._parentWin != null)
	{
		this._parentWin.closeCalendarBox(); 
	}
}

CalendarBox.prototype.drawContent = function (target)
{
    if (target == null) return;
    if (target.document == null) return;

    var output = target.document.open ("text/html", "replace");

    if (output == null) return;

    output.write (this.getHTMLText ());
    output.close ();
    output.focus ();

    output.calendarBox = this;
}

CalendarBox.prototype.goPrevMonth = function (target)
{
    if (this._month > 1)
    {
        this._month -= 1;
    }
    else
    {
        this._year -= 1;
        this._month = 12;
    }

    this.drawContent (target);
}

CalendarBox.prototype.goPrevYear = function (target)
{
    this._year -= 1;
    this.drawContent (target);
}

CalendarBox.prototype.goNextMonth = function (target)
{
    if (this._month < 12)
    {
        this._month += 1;
    }
    else
    {
        this._year += 1;
        this._month = 1;
    }

    this.drawContent (target);
}

CalendarBox.prototype.goNextYear = function (target)
{
    this._year += 1;
    this.drawContent (target);
}

CalendarBox.prototype.goToDate = function (target, theDate)
{
    this._month = theDate.getMonth() + 1;
    this._year = theDate.getFullYear();

    this.drawContent (target);
}

CalendarBox.prototype.markCurrent = function (target, day)
{
    if (target == null) return;
    if (target.document == null) return;

    day = (day == null ? 0 : parseInt (day));

    this._current = new Date (this._year, this._month - 1, day);

    var cells = target.document.body.all.tags ("TD");

    for (var i = 0; i < cells.length; i ++)
    {
        var cell = cells [i];

        if (cell == null) continue;
        if (cell.dayOfMonth == null) continue;

        if (cell.className != "")
        {
            cell.className = "";
        }

        if (day == parseInt (cell.dayOfMonth))
        {
            cell.className = "current";
        }
    }

    /*if (this._handler != null)
    {
        this._handler.call (this, new Date (this._current.getTime ()), this.formatDate (this._current));
    }*/
    this.chooseDate(this._current);
}

CalendarBox.prototype.chooseDate = function (date)
{
	if (this._handler != null)
    {
        this._handler.call (this, date);
    }
}

CalendarBox.prototype.formatDate = function (date1)
{
    date1 = (date1 == null ? this._floorDate : date1);

    var result = "";

    result += String ("0000" + String (date1.getFullYear ())).slice (-4) + "-";
    result += String ("00" + String (date1.getMonth () + 1)).slice (-2) + "-";
    result += String ("00" + String (date1.getDate ())).slice (-2);

    return result;
}

CalendarBox.prototype.isDateEqual = function (date1, date2)
{
    if (date1.getFullYear () != date2.getFullYear ()) return false;
    if (date1.getMonth () != date2.getMonth ()) return false;
    if (date1.getDate () != date2.getDate ()) return false;

    return true;
}



CalendarBox.prototype.getHTMLText = function ()
{
    var result = "";

    result += "<html>\n";
    result += "<head>\n";
    result += "<title></title>\n";

    result += "<link rel=\"stylesheet\" type=\"text/css\" href=\"/css/basic/calendar.css\"></link>\n";
    result += "<script language=\"JavaScript\" src=\"/script/basic/calendar_box.js\"></script>\n";

    result += "</head>\n";
    result += "<body>\n";
    result += "<div class=\"calendarbox\">\n";

    result += this.getHeaderHTML ();
    result += this.getBodyHTML ();
    result += this.getFooterHTML ();

    result += "</div>\n";
    result += "</body>\n";
    result += "</html>\n";

    return result;
}

CalendarBox.prototype.getHeaderHTML = function ()
{
    var result = "";

    result += "<div class=\"headblock\">\n";
    result += "   <table width=\"176\">\n";
    result += "      <col width=\"20\" align=\"center\"/>\n";
    result += "      <col width=\"15\" align=\"center\"/>\n";
    result += "      <col width=\"106\" align=\"center\"/>\n";
    result += "      <col width=\"15\" align=\"center\"/>\n";
    result += "      <col width=\"20\" align=\"center\"/>\n";

/*
    result += "      <tr>\n";
    result += "         <td colspan=\"2\">\n";
    result += "            <span>" + this._title + "</span>\n";
    result += "         </td>\n";
    result += "         <td align=\"center\">\n";
    result += "            <a href=\"#\" onclick=\"javascript:return(closeCalendarBoxBySelf())\" title=\"Close\">\n";
    result += "                 <span>x</span>\n";
    result += "            </a>\n";
    result += "         </td>\n";
    result += "      </tr>\n";
    */
    result += "      <tr>\n";
    result += "         <td>\n";
    result += "            <a href=\"#\" onclick=\"javascript:return(onPrevYearClicked_cb())\" title=\"上一年\">\n";
    result += "               <span>&lt;&lt;</span>\n";
    result += "            </a>\n";
    result += "         </td>\n";
    result += "         <td>\n";
    result += "            <a href=\"#\" onclick=\"javascript:return(onPrevMonthClicked_cb())\" title=\"上一月\">\n";
    result += "               <span>&lt;</span>\n";
    result += "            </a>\n";
    result += "         </td>\n";
    result += "         <td>\n";
    result += "            <span><label style=\"width:33px;text-align:center\">" + String (this._year) + "</label>年<label style=\"width:15px;text-align:center\">" +  + this._month + "</label>月</span>\n";
    result += "         </td>\n";
    result += "         <td>\n";
    result += "            <a href=\"#\" onclick=\"javascript:return(onNextMonthClicked_cb())\" title=\"下一月\">\n";
    result += "               <span>&gt;</span>\n";
    result += "            </a>\n";
    result += "         </td>\n";
    result += "         <td>\n";
    result += "            <a href=\"#\" onclick=\"javascript:return(onNextYearClicked_cb())\" title=\"下一年\">\n";
    result += "               <span>&gt;&gt;</span>\n";
    result += "            </a>\n";
    result += "         </td>\n";
    result += "      </tr>\n";
    result += "   </table>\n";
    result += "</div>\n";

    return result;
}

CalendarBox.prototype.getBodyHTML = function ()
{
    var result = "";

    result += "<div class=\"bodyblock\">\n";
    result += "   <table width=\"176\">\n";
    result += "      <col width=\"22\" align=\"center\"/>\n";
    result += "      <col width=\"22\" align=\"center\"/>\n";
    result += "      <col width=\"22\" align=\"center\"/>\n";
    result += "      <col width=\"22\" align=\"center\"/>\n";
    result += "      <col width=\"22\" align=\"center\"/>\n";
    result += "      <col width=\"22\" align=\"center\"/>\n";
    result += "      <col width=\"22\" align=\"center\"/>\n";
    result += "      <tr>\n";

    for (var i = 1; i <= 7; i ++)
    {
        result += "         <th title=\"" + this._dayHeaderFull [i] +"\">\n";
        result += "            <span" + (this.isWeekend(this._dayHeader [i])?" class=\"weekend\">":">") + this._dayHeader [i] + "</span>\n";
        result += "         </th>\n";
    }

    result += "      </tr>\n";
    result += "      <tr>\n";
    result += "         <td colspan=\"7\">\n";
    result += "            <span class=\"line\"></span>\n";
    result += "         </td>\n";
    result += "      </tr>\n";

    var date1 = new Date (this._year, this._month - 1, 1);
    var date2 = new Date ();

    date1.setDate (1 - date1.getDay ());

    for (var i = 1; i <= 6; i ++)
    {
        result += "      <tr>\n";

        for (var j = 1; j <= 7; j ++)
        {
            if (this._month != (date1.getMonth () + 1))
            {
                result += "         <td>\n";
                result += "            <span class=\"dimmed\">" + date1.getDate () + "</span>\n";
                result += "         </td>\n";
            }
            else
            {
                var class1 = "";
                var class2 = "";
                var class3 = "";

                if (this.isDateEqual (date1, this._current))
                {
                    class1 = "current";
                }

                if (this.isDateEqual (date1, date2))
                {
                    class2 += " today";
                }

                if ( this.isWeekend(this._dayHeader [j]) )
                {
                    class3 = " weekend";
                }

                result += "         <td class=\"" + class1 + "\" dayOfMonth=\"" + date1.getDate () + "\">\n";
                result += "            <a href=\"#\" onclick=\"javascript:return(onDateClicked_cb(" + date1.getDate () + "))\">\n";
                result += "               <span class=\"" + class2 + class3 + "\">" + date1.getDate () + "</span>\n";
                result += "            </a>\n";
                result += "         </td>\n";
            }

            date1.setDate (date1.getDate () + 1);
        }        

        result += "      </tr>\n";

        if (this._month != (date1.getMonth () + 1))            // next month already
        {
            for (i ++; i <= 6; i ++)
            {
                result += "      <tr>\n";
                result += "         <td colspan=\"7\">\n";
                result += "            <span class=\"empty\"></span>\n";
                result += "         </td>\n";
                result += "      </tr>\n";
            }
            break;
        }
    }

    result += "      <tr>\n";
    result += "         <td colspan=\"7\">\n";
    result += "            <span class=\"line\"></span>\n";
    result += "         </td>\n";
    result += "      </tr>\n";
    result += "   </table>\n";
    result += "</div>\n";

    return result;
}

CalendarBox.prototype.getFooterHTML = function ()
{
    var result = "";
    var now = new Date ();

    result += "<div class=\"headblock\" style=\"width:176px\">\n";
    result += "   <table width=\"176\" >\n";
    result += "      <col width=\"46\" align=\"center\"/>\n";
    result += "      <col width=\"110\" align=\"center\"/>\n";
    result += "      <col width=\"20\" align=\"center\"/>\n";
    result += "      <tr>\n";

    result += "         <td>\n";
    result += "            <a href=\"#\" onclick=\"javascript:return(onTodayClick())\" title=\"翻到当前月份 \">\n";
    result += "                <span style=\"padding: 3px 1px 1px 1px;\">本月</span>\n";
    result += "            </a>\n";
    result += "         </td>\n";
    result += "         <td>\n";
	result += "            <a href=\"#\" onclick=\"javascript:return(onSelectDateClicked_cb(" + now.getTime() +  "))\" title=\"今天 \">\n";
    result += "               <span class=\"today\">" + now.getFullYear() + "年" 
                                                      + (now.getMonth() + 1) + "月"
                                                      + now.getDate () + "日</span>\n";
    result += "            </a>\n";
    result += "         </td>\n";

    result += "         <td align=\"center\">\n";
    result += "            <a href=\"#\" onclick=\"javascript:return(onCloseClick())\" title=\"关闭 \">\n";
    result += "                 <span>x</span>\n";
    result += "            </a>\n";
    result += "         </td>\n";

    result += "      </tr>\n";
    result += "   </table>\n";
    result += "</div>\n";

    return result;
}

CalendarBox.prototype.isWeekend = function (weekDay)
{
    for (var i = 0; i < this._weekEnd.length; i++ )
    {
        if (weekDay == this._weekEnd[i])
        {
            return true;
        }
    }
    return false;
}

function onPrevMonthClicked_cb ()
{
    var calendar = document.calendarBox;

    if (calendar != null)
    {
        calendar.goPrevMonth (window);
    }

    return false;
}

function onPrevYearClicked_cb ()
{
    var calendar = document.calendarBox;

    if (calendar != null)
    {
        calendar.goPrevYear (window);
    }

    return false;
}

function onNextMonthClicked_cb ()
{
    var calendar = document.calendarBox;

    if (calendar != null)
    {
        calendar.goNextMonth (window);
    }

    return false;
}

function onNextYearClicked_cb ()
{
    var calendar = document.calendarBox;

    if (calendar != null)
    {
        calendar.goNextYear (window);
    }

    return false;
}
function onDateClicked_cb (day)
{
    var calendar = document.calendarBox;

    if (calendar != null)
    {
        calendar.markCurrent (window, day);
    }

    return false;
}

function onSelectDateClicked_cb (time)
{
	var calendar = document.calendarBox;

    if (calendar != null)
    {
        calendar.chooseDate (new Date(time));
    }

    return false;
}


function onTodayClick()
{
    var calendar = document.calendarBox;

    if (calendar != null)
    {
        calendar.goToDate (window, new Date());
    }

    return false;
}

function onCloseClick()
{
	var calendar = document.calendarBox;

    if (calendar != null)
    {
        calendar.close();
    }
	return false;
}

function onDocumentMouseUp()
{
    closeCalendarBox();
    document.detachEvent("onmousedown", onDocumentMouseUp);
}



function openCalendarBox (anchor, title, current, handler, parentWin)
{
	document.detachEvent("onmousedown", onDocumentMouseUp);
    document.attachEvent("onmousedown", onDocumentMouseUp);

    var left = 0;
    var top = 0;
    var width = 180;
    var height = 180;

    var frame = getCalendarBox();
    if ( getCalendarBox() != null )
    {
        closeCalendarBox ();
    }
    else
    {
        frame = document.createElement ("IFRAME");
        frame.id = "auto_calendar_box";
        frame.frameBorder = "no";
        frame.scrolling = "no";
        frame.title = title;

        frame.style.zIndex = "1000";
        frame.style.position = "absolute";
        frame = document.body.appendChild (frame);
    }
    if (frame != null)
    {
        frame.title = title;
        frame.style.width = String (width) + "px";
        frame.style.height = String (height) + "px";

        var p = getPopupPos(anchor, frame);

        frame.style.left = String (p.x) + "px";
        frame.style.top = String (p.y) + "px";
        

        if (frame != null)
        {
            var calendar = new CalendarBox ();

            calendar.initialize (title);
            calendar.setCurrent (current);
            calendar.setHandler (handler);
            calendar.setParentWin(parentWin);
            calendar.drawContent (frame.contentWindow);
        }
    }
	//frame.focus();
    return frame;
}

function getCalendarBox ()
{
    var frame = document.body.children ("auto_calendar_box");

    if (frame != null)
    {
        if (frame.tagName == "IFRAME") return frame;
    }

    return null;
}

function closeCalendarBox (delay)
{
    var frame = getCalendarBox ();

    if (frame != null)
    {
        if (delay != null)
        {
            window.setTimeout ("closeCalendarBox ()", parseInt (delay));
        }
        else
        {
            frame.style.left = "0px";
            frame.style.top =  "0px";
            frame.style.width = "0px";
            frame.style.height = "0px";
        }
    }
    return frame;
}

function getPopupPos(anchor, popupWin)
{
    var pos = anchor;
    var pop = popupWin;

    var pop_left = 0;
    var pop_top = 0;
    
    var div_width = parseFloat(pop.style.offsetWidth);
	/* Commentted By QHY 20070830
    for(var p = pos; p && p.tagName!='BODY'; p = p.offsetParent)
    {
        pop_left += p.offsetLeft;
        pop_top += p.offsetTop;
    }
    */
    // [Added By QHY 20070830
    var p = pos.getBoundingClientRect();
    
    pop_left = p.left;
    pop_top = p.top;

    pop_left -= 1;
    //pop_top -= 2;
    // Added By QHY 20070830]

    var pos_height = pos.offsetHeight;
    var pop_height = pop.style.pixelHeight;
    var scroll_top = document.body.scrollTop;

    if((pop_top - pop_height >= scroll_top) && 
       (pop_top + pos_height + pop_height > document.body.clientHeight + scroll_top))
    {    
        pop_top = pop_top - pop_height;
        //pop_top -= 2;
    }
    else
    {    
        pop_top = pop_top + pos_height;
    }
    var offsetX = document.body.scrollLeft;
    var offsetY = document.body.scrollTop;
    
    var r = { x: pop_left + offsetX, y: pop_top + offsetY }
    return r;
}
