/**
 * @classDescription prototype.js
 */
if(!Control) var Control = {};
Control.cypopupCalendars = [];


Control.CypopupCalendar = Class.create({
/**
 *
 * @param {string} field        ターゲットフィールドのID
 * @param {Object} nextfield    Next TabIndex Field   ID
 * @param {Object} st           受付開始日付 yyyy/mm/dd
 * @param {Object} days         予約可能日数
 * @param {string} ajaxUri      Open時Ajaxリクエスト

 * @param {Object} tw_f         true -> 2月表示
 */
    initialize : function(field, nextfield, st, days, ajaxUri, tw_f) {
        var calendar = this;
        Control.cypopupCalendars.push(calendar); //配列の最後に自分を追加
        this.myField = $(field);
        this.nextfield = $(nextfield);
        this.fieldName = this.myField.id;
        this.isOpen = false;
        this.isClick = false;
        this.eventSeted = false;

        this.doubleBooking = false;
        //予約可能月分の休業日this.offPlan['2008-12']="1,2,3"
        this.offPlan = $H({});//ハッシュ
        this.offDays = [];
        this.holidays = [];
        this.stdDays = [];
        this.fullDays = [];
        this.ajaxUri = ajaxUri;
        this.orgYmd = "";       //予約訂正時　自分の予約日でフルになっている場合のフル除去のため
        this.today = new Date(st);
        this.endDay = new Date();
        this.endDay.setTime(this.today.getTime() + (days * 24 * 3600 * 1000));
//----------------------------2009/09/24

        this.selectedDay = new Date();
        this.firstStd = new Date();
        this.lastStd = new Date();
        this.selfFocus = false;
//----------------------------
        if(tw_f){
            this.disp2month = true;
        }
        else{
            this.disp2month = false;

        }
        if (!Control.CypopupCalendar.CONTROL) {
            Control.CypopupCalendar.CONTROL = {};
            if(!$("cypopupcalendar")) {
                var control = Builder.node('div', {id:'cypopupcalendar'});
                control.innerHTML =
                    'dumm';
                document.body.appendChild(control);
            }
            Control.CypopupCalendar.CONTROL = {
                popUp : $("cypopupcalendar")
            };
            Element.hide($("cypopupcalendar"));
        }

        this.control = Control.CypopupCalendar.CONTROL;



//      this.fieldOnFocusListener = this.open.bindAsEventListener(this);
        this.fieldOnFocusListener = this.offPlanSet.bindAsEventListener(this);
        this.fieldKeydownListener = this.fieldKeydown.bindAsEventListener(this);
        Event.observe(this.myField, "focus", this.fieldOnFocusListener,false);
//        Event.observe(this.myField, "click", this.fieldOnFocusListener,false);
        Event.observe(this.myField, "keydown", this.fieldKeydownListener,false);

        this.popupOnClickListener = this.click.bindAsEventListener(this);
        this.docOnClickListener   = this.docClick.bindAsEventListener(this);
        Event.observe(document, 'click', this.docOnClickListener,false);
        Event.observe(this.control.popUp, "click", this.popupOnClickListener,false);

//      this.offPlanSet();


    },

    _planGetHost : function(Plf) {
       var Plans = Plf[0].split('#');
        var m_set, m_prop;
        for(var i = 0; i < Plans.length; i++) {
            m_set = Plans[i].split(':');
            m_prop = m_set[0].split('-');
            m_prop[2] -= 0;
            this.offPlan.set("" + m_prop[1] + '-' + m_prop[2], m_set[1]);
        }
        this.fullDays.clear();
        if (this.fullDays) {
            this.fullDays = Plf[1].split(',');
        }

    },


    planGetHost : function(httpObj) {

        var Plf;
        Plf = httpObj.responseJSON;
        if (Plf.offdays) {
            this.offPlan = $H(Plf.offdays);
        }
        else{
            this.offPlan = $H({});
        }

        this.fullDays.clear();
        if (Plf.fulldays) {
            this.fullDays = Plf.fulldays.toArray();
        }
        this.open();


    },
    offPlanSet : function() {
/*
        if(Control.CypopupCalendar.activeCalendar == this){
            return;
        }
*/
        if(this.selfFocus){
            this.selfFocus = false;
            return;
        }
        Control.CypopupCalendar.activeCalendar = this;

        this.planGetHostListener = this.planGetHost.bindAsEventListener(this);
        var dd = new Date();
        dd = dd.getTime();
        var syy = this.today.getYear();
        if(syy < 2000)
            syy += 1900;
        var smm = this.today.getMonth() + 1;
        var eyy = this.endDay.getYear();
        if(eyy < 2000)
            eyy += 1900;
        var emm = this.endDay.getMonth() + 1;
        var data = 'syy='+syy+'&'+'smm='+smm+'&'+'eyy='+eyy+'&'+'emm='+emm+'&'+'tm='+ dd;
        var options = {
            method:'post',
            onComplete:this.planGetHostListener,
            parameters:data,
            evalJSON:true

        };
        new Ajax.Request(this.ajaxUri, options);
    },
    open : function() {

        Control.CypopupCalendar.activeCalendar = this;
        if(this.myField.value != "") {
            var dval = this.myField.value.split("/");
            this.YY = dval[0] - 0;
            this.MM = dval[1] - 0;
            this.DD = dval[2] - 0;
            if(this.DD == 0){ // 時間超過で予約可能時間帯がなくなった
                var y;
                var trg;
                while(this.today.getTime() <= this.endDay.getTime()){
                    this.today.setTime(this.today.getTime() + (24 * 3600 * 1000));
                    y = this.today.getYear();
                    if(y < 2000){
                        y += 1900;
                    }
                    trg = 'cyc-' + y + '-' + (this.today.getMonth() + 1) + '-' + this.today.getDate();
                    if(this.stdDays.include(trg))
                        break;
                }
                this.myField.value = "";
                this.YY = this.today.getYear();
                this.MM = this.today.getMonth() + 1;
                this.DD = this.today.getDate();
                if (this.YY < 2000) {
                    this.YY += 1900;
                }
            }
        }
        else {
            this.YY = this.today.getYear();
            this.MM = this.today.getMonth() + 1;
            this.DD = this.today.getDate();
            if(this.YY < 2000) {
                this.YY += 1900;
            }
        }
        this.YYY = this.YY;
        this.MMM = this.MM;
//----------------------------2009/09/24

        this.selectedDay = new Date(this.YY,  this.MM - 1, this.DD);
//      Event.stopObserving(this.myField, 'focus');
        this.renderPopup();

    },

    renderPopup : function(){

        if(this.disp2month){
            this.render2month();
        }
        else {
            this.render();
        }

    },

    /**
     * Dayはカレンダー表示行列の何行目
     * か（０スタートベース）を
     * this.weekNumにセットする
     * @param Date Day
     */
    encodeWeekofDay : function(Day){
        this.weekDay = Day.getDay();
        var fastVal = new Date();
        fastVal.setTime(this.startDay.getTime() + (this.weekDay * 24 * 3600 * 1000));
        this.weekNum = (Day.getTime() - fastVal.getTime()) / (7 * 24 * 3600 * 1000);
    },
    /**
     * 対象月のdd日が日曜なら
     * 次の日を返す
     * @param integer dd
     * @return Date
     */
    getholi : function(dd){
        var Day = new Date(this.YY, this.MM - 1, dd);
        if(Day.getDay() == 0){
            Day.setTime(Day.getTime() + (24 * 3600 * 1000));
        }
        return Day;
    },
    /**
     * 現在月のweekNum週目のweekDay曜日の日付
     * を求める
     *
     * @param integer weekNum 1-5
     * @param integer weekDay 0-6
     * @return Date
     */
    encodeDayofWeek : function(weekNum, weekDay) {
        //１週目のweekDay曜日は
        var day = new Date();
        var firstDay = new Date(this.YY, this.MM - 1, 1);
        var firstWeek = firstDay.getDay();
        day.setTime(this.startDay.getTime() + (weekDay * 24 * 3600 * 1000));
        if(firstWeek <= weekDay)
            weekNum -= 1; //0ベースに変更
        day.setTime(day.getTime() + (weekNum * 7 * 24 * 3600 * 1000));
        if(day.getMonth() + 1 != this.MM) { //月外日付
            return false;
        }
        else {
            return day;
        }
    },
    getStadHolidays : function(){
        this.holidays.clear();
        this.holidays.push(0);
        var dVal;
        var iVal;
        var Day;
        switch(this.MM){
            case 1:
                this.holidays.push(this.getholi(1).getDate());//元旦
                this.holidays.push(this.encodeDayofWeek(2, 1).getDate());//成人の日
                break;
            case 2:
                this.holidays.push(this.getholi(11).getDate());//建国記念日
                break;
            case 3:
                dVal = 20.8431 + (0.241294 * (this.YY - 1980));
                iVal = Math.floor((this.YY - 1980) / 4);
                this.holidays.push(this.getholi(Math.floor(dVal - iVal)).getDate());//春分の日
                break;
            case 4:
                this.holidays.push(this.getholi(29).getDate());//昭和の日
                break;
            case 5:
                Day = new Date(this.YY, this.MM - 1, 3);
                if(Day.getDay() == 0){
                    Day.setTime(Day.getTime() + (3 * 24 * 3600 * 1000));
                }
                this.holidays.push(Day.getDate());//憲法記念日
                Day.setDate(4);
                if(Day.getDay() == 0){
                    Day.setTime(Day.getTime() + (2 * 24 * 3600 * 1000));
                }
                this.holidays.push(Day.getDate());//みどりの日
                this.holidays.push(this.getholi(5).getDate());//こどもの日
                break;
            case 7:
                this.holidays.push(this.encodeDayofWeek(3, 1).getDate()); //海の日
                break;
            case 9:
                this.holidays.push(this.encodeDayofWeek(3, 1).getDate()); //敬老の日
                dVal = 23.2488 + (0.242194 * (this.YY - 1980));
                iVal = Math.floor((this.YY - 1980) / 4);
                this.holidays.push(this.getholi(Math.floor(dVal - iVal)).getDate());//秋分の日
                Day = new Date(this.YY, this.MM - 1, this.holidays[1] + 2);
                if(Day.getDate() == this.holidays[2]) {
                    this.holidays.push(this.holidays[1] + 1);
                }
                break;
            case 10:
                this.holidays.push(this.encodeDayofWeek(2, 1).getDate()); //体育の日
                break;
            case 11:
                this.holidays.push(this.getholi(3).getDate());//文化の日
                this.holidays.push(this.getholi(23).getDate());//勤労感謝の日
                break;
            case 12:
                this.holidays.push(this.getholi(23).getDate());//天皇誕生日
                break;
            default:
                break;
        }
    },
    initFactors :function(){
        var i, j, k, yy, mm, work;
        for(i = 0; i < this.offDays.length; i++) {
            this.offDays[i] = this.offDays[i] - 0;
        }
        if(this.startDay)
            delete this.startDay;
        this.startDay = new Date(this.YY, this.MM - 1, 1);
        this.startDay.setTime(this.startDay.getTime() - (this.startDay.getDay() * 24 * 3600 * 1000));
        if(this.MM == 12) {
            yy = this.YY + 1;
            mm = 0;
        }
        else{
            yy = this.YY;
            mm = this.MM;
        }
        if(this.lastDay)
            delete this.lastDay;
        this.lastDay = new Date(yy, mm, 1);
        this.lastDay.setTime(this.lastDay.getTime() - (1 * 24 * 3600 * 1000));

        this.encodeWeekofDay(this.lastDay);//カレンダー表示行数を0ベースでthis.weekNumに格納
        this.weekCnt = this.weekNum + 1;
        this.getStadHolidays();//祝日配列のセット

    },
/**
 *
 * @param {int} month_f 0:単票 1:2月表示の1月目 2:2月表示の2月目
 */
    makeHtml : function(month_f){
        var nowVal = this.today.getTime();
        var endVal = this.endDay.getTime();
        var outVal = new Date();
        var weekTitle = new Array('日','月','火','水','木','金','土');
        var i, j, work, yy, mm, dd, tt, idVal, Ymd;
//---------------------2009/09/24
        var f;
//---------------------

        outVal.setTime(this.startDay.getTime());
        var html  = '<center>' + "\n";
        html += '<table class="cycalendar" id="cyc-' + this.YY + '-' + this.MM + '" >' + "\n";
        html += '<tr>' + "\n";
/*
        html += '<td colspan="2" class="cycalen-head" id="cyc-mm-lt" title="前月へ"><b>&lt;&lt;</b></td>';
        html += '<td colspan="3" class="cycalen-head">';
        html += (this.YY-1988) + '年' + this.MM + '月</td>';
        html += '<td colspan="2" class="cycalen-head" id="cyc-mm-gt" title="翌月へ"><b>&gt;&gt;</b></td>';
*/
        html += '<td colspan="7" class="cycalen-head">' + "\n";
        switch(month_f){
            case 0:
                html += '<span title="前月へ" id="cyc-mm-lt"><b>&lt;&lt;</b></span>' + "\n";
                html += '<span id="cyc-mouse-over" class="cyc-mover-color">&nbsp;</span>' + this.YY  + '年' + this.MM + '月&nbsp;' + "\n";
                html += '<span title="翌月へ" id="cyc-mm-gt"><b>&gt;&gt;</b></span>' + "\n";
                break;
            case 1:
                html += '<span title="前月へ" id="cyc-mm-lt"><b>&lt;&lt;</b></span>' + "\n";
                html += '<span id="cyc-mouse-over" class="cyc-mover-color">&nbsp;&nbsp;</span>' + this.YY  + '年' + this.MM + '月&nbsp;&nbsp;&nbsp;' + "\n";
                break;
            default:
                html += '&nbsp;&nbsp;&nbsp;' + this.YY  + '年' + this.MM + '月&nbsp;' + "\n";
                html += '<span title="翌月へ" id="cyc-mm-gt"><b>&gt;&gt;</b></span>' + "\n";

        }
        html += '</td>' + "\n";
        html += '</tr>' + "\n";
        html += '<tr>' + "\n";
        for(i = 0; i < 7; i++) {
            html += '<td class="cyc-base ';
            switch (i) {
                case 0:
                    html += 'cyc-week-sun-mj-color ';
                    break;
                case 6:
                    html += 'cyc-week-sat-mj-color ';
                    break;
                default:
                    html += 'cyc-week-std-mj-color ';
            }
            html += 'cyc-weeks-bg-color">' + weekTitle[i] + '</td>' + "\n";
        }
        html += '</tr>' + "\n";
        for(i = 0; i < this.weekCnt; i++){
            html += '<tr>' + "\n";
            if(i >= 4)
                html += '<!-- -->'
            for(j = 0; j < 7; j++){
                f = false;//-------------------2009/09/24

                yy = outVal.getYear();
                if(yy < 2000)
                    yy += 1900;
                mm = outVal.getMonth() + 1;
                dd = outVal.getDate();
                tt = outVal.getTime();
                html += '<td class="cyc-base ';
                if(yy != this.YY || mm != this.MM || tt < nowVal || tt > endVal) {
                    html += 'cyc-out-mj-color cyc-out-bg-color">';
                }
                else{
                    Ymd = "" + yy + '-' + mm + '-' + dd;
                    if (this.fullDays.include(Ymd) && (Ymd != this.orgYmd)) {
                        html += 'cyc-full-mj-color cyc-full-bg-color" title="予約満席">';
                    }
                    else {

                        if (j == 0 || this.holidays.include(dd)) {
                            html += 'cyc-sun-mj-color ';
                        }
                        else
                            if (j == 6) {
                                html += 'cyc-sat-mj-color ';
                            }
                            else {
                                html += 'cyc-std-mj-color ';
                            }
                        if (this.offDays.include(dd)) {
                            html += 'cyc-off-bg-color" title="休院日">';
                        }
                        else {
                            f = true;
                            idVal = 'cyc-' + yy + '-' + mm + '-' + dd;
                            html += 'cyc-std-bg-color" id="' + idVal + '" title="予約可">';
                            this.stdDays.push(idVal);
                        }
                    }
                }
                //--------------2009/09/24
/*
                if(yy == this.YYY && mm == this.MMM && dd == this.DD && f==true){
                    html += '<div class="cyc-today-style">' + dd + '</div>';
                }
*/
                if(outVal.getTime() == this.selectedDay.getTime() && f==true){
                    html += '<div class="cyc-today-style">' + dd + '</div>';
                }
                else{
                    html += dd;
                }
                html += '</td>' + "\n";
                outVal.setTime(outVal.getTime() + (24*3600*1000));
            }
            html += '</tr>' + "\n";
        }
        html += '</table>';
        html+='</center>';
        return html;
    },

    makeId : function (dateVal){//----------2009/09/24
        var yy = dateVal.getYear();
        if(yy < 2000)
            yy += 1900;
        var mm = dateVal.getMonth() + 1;
        var dd = dateVal.getDate();
        return 'cyc-'+ yy + '-' + mm + '-' + dd;
    },
    id2Date : function (id){//---------------2009/09/24
        if(!id)
            return;
        var val = id.split("-");
        var date = new Date(val[1] - 0, val[2] - 1, val[3] - 0);
        return date.getTime();
    },




    eraseEvents : function(){
        var i;
        Event.stopObserving($("cyc-mm-gt"), 'click');
        Event.stopObserving($("cyc-mm-gt"), 'mouseover');
        Event.stopObserving($("cyc-mm-gt"), 'mouseout');
        Event.stopObserving($("cyc-mm-lt"), 'click');
        Event.stopObserving($("cyc-mm-lt"), 'mouseover');
        Event.stopObserving($("cyc-mm-lt"), 'mouseout');
        Event.stopObserving($("cyc-mm-lt"), 'mouseout');
        Event.stopObserving($("cyc-mm-lt"), 'mouseout');

//        Event.stopObserving(document, 'click');



        for(i = 0; i < this.stdDays.length; i++){
            Event.stopObserving($(this.stdDays[i]) ,'click');
            Event.stopObserving($(this.stdDays[i]) ,'mouseover');
            Event.stopObserving($(this.stdDays[i]) ,'mouseout');
        }
        this.eventSeted = false;
    },
    mmltClick : function(event){
        Event.stop(event);
        this.MM -= 1;
        if(this.MM < 1){
            this.MM = 12;
            this.YY -= 1;
        }
        this.renderPopup();
        this.selfFocus = true;
        this.myField.focus();
/*
        if(this.disp2month){
            this.render2month();
        }
        else {
            this.render();
        }
*/
    },

    mmgtClick :function(event){
        Event.stop(event);
        this.MM += 1;
        if(this.MM > 12){
            this.MM = 1;
            this.YY += 1;
        }
        this.renderPopup();
        this.selfFocus = true;
        this.myField.focus();
/*
        if(this.disp2month){
            this.render2month();
        }
        else {
            this.render();
        }
*/
    },
    stdDayClick : function(event){
        if(this.doubleBooking)
            return;
        var ele = Event.element(event);
        var id = ele.id;
        if(id == ""){
            id = ele.parentNode.id
        }

        var val = id.split("-");
        if (val[1]) {
            Event.stop(event);
            if (val[2] < 10)
                val[2] = '0' + val[2];
            if (val[3] < 10)
                val[3] = '0' + val[3];
            this.myField.value = val[1] + '/' + val[2] + '/' + val[3];
            this.close(event);
            this.nextfield.focus();
        }
    },
    setMouseOver : function(event){
        var styles = Event.element(event).className.split(' ');
        switch(styles.length) {
            case 3:
                this.stkColor = styles[1];
                Event.element(event).className = styles[0] + ' ' + "cyc-mover-color" + ' ' + styles[2];
                break;
            case 1:
                Event.element(event).className = styles[0] + ' ' + "cyc-mover-color";
                break;
            default:
                this.stkColor = styles[0];
                Event.element(event).className = "cyc-mover-color"
        }
        var ele = Event.element(event);
        if(!ele)
            return;
        var id = ele.id;
        if(!id)
            return;
        //----------------------------2009/09/24
        var oldId;
        if (this.selectedDay) {
            oldId = this.makeId(this.selectedDay);
        }
        else
            return;
        if (oldId) {
            if (oldId != id) {
                if ($(oldId)) {
                    Element.update($(oldId), this.selectedDay.getDate());
                }
                this.selectedDay.setTime(this.id2Date(id));


                Element.update(ele, "");
                var child = Builder.node("div", {
                    className: "cyc-today-style"
                }, this.selectedDay.getDate() + "");
                ele.appendChild(child);

            }
        }
    },
    setMouseOut : function(event){
        //----------------------------2009/09/24
/*
        var ele = Event.element(event);
        var id = ele.id;
        if (id) {
            this.selectedDay.setTime(this.id2Date(id));
        }
*/
        var styles = Event.element(event).className.split(' ');
        switch (styles.length) {
            case 3:
                Event.element(event).className = styles[0] + ' ' + this.stkColor + ' ' + styles[2];
                break;
            case 2:
                Event.element(event).className = styles[0];
                break;
            default:
                Event.element(event).className = "";
        }
    },
    setEvents : function(){
        var i;
        if (this.stdDays.length >= 1) {
            this.firstStd.setTime(this.id2Date(this.stdDays[0]));
            this.lastStd.setTime(this.id2Date(this.stdDays[this.stdDays.length - 1]));
        }



        this.mmgtOnClickListener = this.mmgtClick.bindAsEventListener(this);
        this.mmltOnClickListener = this.mmltClick.bindAsEventListener(this);
        this.stdDayOnClickListener = this.stdDayClick.bindAsEventListener(this);
        this.setMouseOverListener = this.setMouseOver.bindAsEventListener(this);
        this.setMouseOutListener = this.setMouseOut.bindAsEventListener(this);
        Event.observe($("cyc-mm-gt"), 'click', this.mmgtOnClickListener);
        Event.observe($("cyc-mm-gt"), 'mouseover', this.setMouseOverListener);
        Event.observe($("cyc-mm-gt"), 'mouseout', this.setMouseOutListener);
        Event.observe($("cyc-mm-lt"), 'click', this.mmltOnClickListener);
        Event.observe($("cyc-mm-lt"), 'mouseover', this.setMouseOverListener);
        Event.observe($("cyc-mm-lt"), 'mouseout', this.setMouseOutListener);
//      Event.observe(document, 'click', this.docOnClickListener);

        for(i = 0; i < this.stdDays.length; i++){
            Event.observe($(this.stdDays[i]) ,'click' , this.stdDayOnClickListener);
            Event.observe($(this.stdDays[i]) ,'mouseover' , this.setMouseOverListener);
            Event.observe($(this.stdDays[i]) ,'mouseout' , this.setMouseOutListener);

        }

        this.eventSeted = true;
    },
    render : function(){
        if(Control.CypopupCalendar.activeCalendar != this)
            return;
        var pName = this.YY + '-' + this.MM;

        var i;
        var val = this.offPlan.get(pName);
        if(this.offDays)
            this.offDays.clear();
        if (val) {
            this.offDays = val.split(",");
        }
        this.initFactors();
        if(this.eventSeted)
            this.eraseEvents();

        this.stdDays.clear();

        var outHtml = this.makeHtml(0);

        Element.update(this.control.popUp, outHtml);
        this.setEvents();



        Element.show(this.control.popUp);

/*
        this.control.popUp.visualEffect('Opacity',{
            from:0.0,
            to : 1.0
            });



        Element.show(this.control.popUp.visualEffect('Opacity',{
            from:0.0,
            to : 1.0
            }));
*/
        var pos = Position.cumulativeOffset(this.myField);
        pos[1] = pos[1] + this.myField.getHeight();

        this.isOpen = true;
        this.control.popUp.style.left = (pos[0]) + "px";
        this.control.popUp.style.top = (pos[1]) + "px";
        //IE は 今focusしているコントロールをreadonlyに出来ないため

/*
        this.control.popUp.focus();
        this.myField.readOnly = "readonly";
*/
        document.body.style.cursor = "default";
        this.myField.style.cursor = "default";
        this.control.popUp.style.cursor = "default";
        $("cyc-mm-gt").style.cursor= "pointer";
        $("cyc-mm-lt").style.cursor= "pointer";
        for(i = 0; i < this.stdDays.length; i++){
           $(this.stdDays[i]).style.cursor= "pointer";
        }
//        this.setEvents();
        this.myField.focus();
    },
    render2month : function(){
/*
        if(Control.CypopupCalendar.activeCalendar != this)
            return;
*/
        var i, w1, w2;
        var pName = this.YY + '-' + this.MM;
        var val = this.offPlan.get(pName);
        if(this.eventSeted)
            this.eraseEvents();

        if(this.offDays)
            this.offDays.clear();
        if (val) {
            this.offDays = val.split(",");
        }
        this.initFactors();
        w1 = this.weekCnt;
        this.stdDays.clear();

        var outHtml = '<div style="float:left;display:inline;">';
        outHtml += this.makeHtml(1);
        outHtml += '</div><div style="float:left;display:inline;">';
        this.MM++;
        if(this.MM > 12) {
            this.YY += 1;
            this.MM = 1;
        }
        pName = this.YY + '-' + this.MM;

        val = this.offPlan.get(pName);
        if (val) {
            this.offDays = val.split(",");
        }
        this.initFactors();
        w2 = this.weekCnt;
        outHtml += this.makeHtml(2);
        outHtml += '</div>';
        this.MM--;
        if(this.MM < 1) {
            this.YY -= 1;
            this.MM = 12;
        }

        Element.update(this.control.popUp, outHtml);
        this.setEvents();
/*
        this.control.popUp.visualEffect('Opacity',{
            from:0.0,
            to : 1.0,
            duration:0.5
            });
*/

           Element.show(this.control.popUp);

/*
            Element.show(this.control.popUp.visualEffect('Opacity',{
            from:0.0,
            to : 1.0
            }));
*/
        var pos = Position.cumulativeOffset(this.myField);
        pos[1] = pos[1] + this.myField.getHeight();

        this.control.popUp.style.left = (pos[0]) + "px";
        this.control.popUp.style.top = (pos[1]) + "px";
        if(w1 == w2){
            var m = this.MM;
            var y = this.YY;
            var n1 = "cyc-" + y + "-" + m;
            m++;
            if(m > 12){
                m = 1;
                y += 1;
            }
            var n2 = "cyc-" + y + "-" + m;
            var h1 = Element.getHeight($(n1));
            var h2 = Element.getHeight($(n2));
            if(h1 != h2){
                if(h1 > h2){
                    h1 += 'px';
                    Element.setStyle($(n2),{'height':h1});
                    Element.setStyle($(n1),{'height':h1});
                }
                else{
                    h2 += 'px';
                    Element.setStyle($(n1),{'height':h2});
                    Element.setStyle($(n2),{'height':h2});
                }
            }
//           Element.show(this.control.popUp);
/*
            var aw = Element.getWidth(this.control.popUp) + 1;
            Element.setStyle(this.control.popUp,{'width':aw + 'px'});

            aw = Element.getWidth(this.control.popUp) - 1;
            Element.setStyle(this.control.popUp,{'width':aw + 'px'});
*/
        }

        this.isOpen = true;
        //IE は 今focusしているコントロールをreadonlyに出来ないため

/*

        this.control.popUp.focus();
        this.myField.readOnly = "readonly";
*/
        document.body.style.cursor = "default";
        this.myField.style.cursor = "default";
        this.control.popUp.style.cursor = "default";
        $("cyc-mm-gt").style.cursor= "pointer";
        $("cyc-mm-lt").style.cursor= "pointer";
/*

        $("cyc-2009-7-23").style.backgroundColor = '#33ffcc';
        $("cyc-2009-7-23").title = "何じゃ羅門じゃら";
*/
        for(i = 0; i < this.stdDays.length; i++){
           $(this.stdDays[i]).style.cursor= "pointer";

        }
//        this.myField.focus();
//        this.setEvents();

    },

    close : function(event) {

//        if (this.isOpen) {
            this.isOpen = false;
//            Event.observe(this.myField, "focus", this.fieldOnFocusListener);
            if (Control.CypopupCalendar.activeCalendar == this) {
//                Control.CypopupCalendar.activeCalendar = null;
                this.myField.style.cursor = "default";
                document.body.style.cursor = "default";

            }
            Element.hide(this.control.popUp);
            this.control.popUp.style.top = null;
            this.control.popUp.style.left = null;

            if(this.eventSeted)
                this.eraseEvents();
            if(this.myField.value != ""){
                if(!this.myField.value.match(/^2\d{3}\/(0?[1-9]|1[0-2])\/(0?[1-9]|[12][0-9]|3[01])$/))
                    this.myField.value = "";
            }

//        }

    },

    docClick : function(event) {
        var ele = Event.element(event);
        if (ele != this.myField && ele != this.control.popUp) {
             if (ele != this.nextfield) {
                 if (Control.CypopupCalendar.activeCalendar == this) {
                     this.close();
                 }
             }
/*
             else{
                if(this.myField.value != ""){
                    if (this.myField.value.match(/^2\d{3}\/(0?[1-9]|1[0-2])\/(0?[1-9]|[12][0-9]|3[01])$/)) {
                        this.close();
                    }

                    else{
                        this.myField.value = "";
                    }

                }

             }
*/
        }

    },
    SelectDayMove : function(isRight){
        if(this.stdDays.length <= 0)
            return;
        var dval = new Date();
        var newId = "";
        var oldId = this.makeId(this.selectedDay);
        if($(oldId)){
            dval.setTime(this.selectedDay.getTime());
            while(1){
                if(isRight){
                    dval.setTime(dval.getTime() + (24 * 3600 * 1000));
                    if(dval.getTime() > this.lastStd.getTime()){
                        break;
                    }
                }
                else{
                    dval.setTime(dval.getTime() - (24 * 3600 * 1000));
                    if(dval.getTime() < this.firstStd.getTime()){
                        break;
                    }
                }
                newId = this.makeId(dval);
                if(this.stdDays.include(newId)){
                    Element.update($(oldId), this.selectedDay.getDate());
                    this.selectedDay.setTime(dval.getTime());
                    Element.update($(newId), "");
                    var child = Builder.node("div", {
                                className: "cyc-today-style"
                                }, this.selectedDay.getDate() + "");
                    $(newId).appendChild(child);
                    break;
                }
            }
        }
        else{
            if(isRight){
                this.selectedDay.setTime(this.firstStd.getTime());
            }
            else{
                this.selectedDay.setTime(this.lastStd.getTime());
            }
            newId = this.makeId(this.selectedDay);
            Element.update($(newId), "");
            var child = Builder.node("div", {
                        className: "cyc-today-style"
                        }, this.selectedDay.getDate() + "");
            $(newId).appendChild(child);

        }

    },

    SelectDayUpDn : function(isDown){
        if(this.stdDays.length <= 0)
            return;
        var dval = new Date();
        var oldId = this.makeId(this.selectedDay);
        if ($(oldId)) {
            if (isDown) {
                dval.setTime(this.selectedDay.getTime() + (7 * 24 * 3600 * 1000));
            }
            else {
                dval.setTime(this.selectedDay.getTime() - (7 * 24 * 3600 * 1000));
            }
            newId = this.makeId(dval);
            if (this.stdDays.include(newId)) {
                Element.update($(oldId), this.selectedDay.getDate());
                this.selectedDay.setTime(dval.getTime());
                Element.update($(newId), "");
                var child = Builder.node("div", {
                    className: "cyc-today-style"
                }, this.selectedDay.getDate() + "");
                $(newId).appendChild(child);
            }
        }
        else{
            if(isDown){
                this.selectedDay.setTime(this.firstStd.getTime());
            }
            else{
                this.selectedDay.setTime(this.lastStd.getTime());
            }
            newId = this.makeId(this.selectedDay);
            Element.update($(newId), "");
            var child = Builder.node("div", {
                        className: "cyc-today-style"
                        }, this.selectedDay.getDate() + "");
            $(newId).appendChild(child);

        }
    },
    PageUp:function(){
        this.MM -= 1;
        if(this.MM < 1){
            this.MM = 12;
            this.YY -= 1;
        }
        this.renderPopup();

    },
    PageDown:function(){
        this.MM += 1;
        if(this.MM > 12){
            this.MM = 1;
            this.YY += 1;
        }
        this.renderPopup();

    },
    setYmd : function(event){
        if(this.stdDays.length <= 0)
            return;
        var Id = this.makeId(this.selectedDay);
        if ($(Id)){
            var val = Id.split("-");
            if (val[1]) {
                Event.stop(event);
                if (val[2] < 10)
                    val[2] = '0' + val[2];
                if (val[3] < 10)
                    val[3] = '0' + val[3];
                this.myField.value = val[1] + '/' + val[2] + '/' + val[3];
                this.close(event);
                if(this.nextfield != null)
                    this.nextfield.focus();
            }
        }
    },

    fieldKeydown : function(event) {
        var code = event.keyCode;
        if(Control.CypopupCalendar.activeCalendar == this){
            switch(code){
                case 9://TAB
                    this.close(event);
                    break;
                case 13:
                    this.setYmd(event)
                    Event.stop(event);
                    break;
                case 33://PAGEUp
                    this.PageUp();
                    Event.stop(event);
                    break;
                case 34://PageDown
                    this.PageDown();
                    Event.stop(event);

                case 37://KEY_LEFT:
                    this.SelectDayMove(false);
                    Event.stop(event);

                    break;
                case 38://KEY_UP
                    this.SelectDayUpDn(false);
                    Event.stop(event);
                    break;
                case 39://KEY_RIGHT:
                    this.SelectDayMove(true);
                    Event.stop(event);
                    break;
                case 40://KEY_Down
                    this.SelectDayUpDn(true);
                    Event.stop(event);
                    break;
                default:
                    Event.stop(event);
            }



        }
        else
            Event.stop(event);
    },
    //popupをクリック
    click: function(event){
        Event.stop(event);
    }
});

Control.CypopupCalendar.activeCalendar;
Control.CypopupCalendar.CONTROL;

Control.CymasterCalendar = Class.create(Control.CypopupCalendar, {

    initialize : function(field, nextfield, st, days, ajaxUri, tw_f) {
        var calendar = this;
        Control.cypopupCalendars.push(calendar); //配列の最後に自分を追加
        this.myField = $(field);
        this.nextfield = $(nextfield);
        this.fieldName = this.myField.id;
        this.isOpen = false;
        this.isClick = false;
        this.eventSeted = false;
        this.doubleBooking = false;
        //予約可能月分の休業日this.offPlan['2008-12']="1,2,3"
        this.offPlan = $H({});//ハッシュ
        this.offDays = [];
        this.holidays = [];
        this.stdDays = [];
        this.fullDays = [];

        this.packedPar = $H({});

        this.orgYmd = "";       //予約訂正時　自分の予約日でフルになっている場合のフル除去のため
        this.ajaxUri = ajaxUri;

        this.today = new Date(st);
        this.endDay = new Date();
        this.endDay.setTime(this.today.getTime() + (days * 24 * 3600 * 1000));
//----------------------------2009/09/24

        this.selectedDay = new Date();
        this.firstStd = new Date();
        this.lastStd = new Date();
        this.selfFocus = false;
//----------------------------



        if(tw_f){
            this.disp2month = true;
        }
        else{
            this.disp2month = false;

        }
        if (!Control.CypopupCalendar.CONTROL) {
            Control.CypopupCalendar.CONTROL = {};
            if(!$("cypopupcalendar")) {
                var control = Builder.node('div', {id:'cypopupcalendar'});
                control.innerHTML =
                    'dumm';
                document.body.appendChild(control);
            }
            Control.CypopupCalendar.CONTROL = {
                popUp : $("cypopupcalendar")
            };
            Element.hide($("cypopupcalendar"));
        }

        this.control = Control.CypopupCalendar.CONTROL;


        this.fieldOnFocusListener = this.offPlanSet.bindAsEventListener(this);
        this.fieldKeydownListener = this.fieldKeydown.bindAsEventListener(this);
        Event.observe(this.myField, "focus", this.fieldOnFocusListener,false);
//        Event.observe(this.myField, "click", this.fieldOnFocusListener,false);
        Event.observe(this.myField, "keydown", this.fieldKeydownListener,false);

        this.popupOnClickListener = this.click.bindAsEventListener(this);
        this.docOnClickListener   = this.docClick.bindAsEventListener(this);
        Event.observe(document, 'click', this.docOnClickListener,false);
        Event.observe(this.control.popUp, "click", this.popupOnClickListener,false);

//      this.offPlanSet();
    },

    offPlanSet : function() {
/*
        if(Control.CypopupCalendar.activeCalendar == this){
            return;
        }
*/
        if(this.selfFocus){
            this.selfFocus = false;
            return;
        }

        Control.CypopupCalendar.activeCalendar = this;


        this.planGetHostListener = this.planGetHost.bindAsEventListener(this);
        var dd = new Date();
        dd = dd.getTime();
        var syy = this.today.getYear();
        if(syy < 2000)
            syy += 1900;
        var smm = this.today.getMonth() + 1;
        var eyy = this.endDay.getYear();
        if(eyy < 2000)
            eyy += 1900;
        var emm = this.endDay.getMonth() + 1;

        var sdd = this.today.getDate();
        var edd = this.endDay.getDate();
        var symd = 'symd=' + syy + '-' + smm + '-' + sdd;
        var eymd = 'eymd=' + eyy + '-' + emm + '-' + edd;
        var data = 'syy='+syy+'&'+'smm='+smm+'&'+'eyy='+eyy+'&'+'emm='+emm+'&'+
                    symd + '&' + eymd + '&' + 'tm=' + dd;
        var options = {
            method:'post',
            onSuccess:this.planGetHostListener,
            parameters:data,
            evalJSON:true,
            sanitizeJSON:false
        };
        new Ajax.Request(this.ajaxUri, options);
    },
    planGetHost: function(httpObj){
        var Plf;
        Plf = httpObj.responseJSON;
        if (Plf.offdays) {
            this.offPlan = $H(Plf.offdays);
        }
        else{
            this.offPlan = $H({});
        }
        this.fullDays.clear();
        if (Plf.fulldays) {
            this.fullDays = Plf.fulldays.toArray();
        }
        this.packedPar = $H(Plf.bookedLnk);

//      this.offPlan = $H(Plf.offdays);
        this.open();

    },
    renderPopup:function(){
        if(this.disp2month){
            this.render2month();
        }
        else {
            this.render();
        }
        var days = this.stdDays;
        this.packedPar.each(function(s){
            if (days.include(s.key)) {
                switch(s.value){
                    case 'A':
                        $(s.key).style.backgroundColor = '#FFE4BE';
                        $(s.key).title = "予約率７５％以上"
                        break;
                    case 'B':
                        $(s.key).style.backgroundColor = '#D7FFD2';
                        $(s.key).title = "予約率５０％以上"
                        break;
                    case 'C':
                        $(s.key).style.backgroundColor = '#D2F6FF';
                        $(s.key).title = "予約率２５％以上"
                        break;
                }
            }
        });
    }

});

Control.CyTscopedCalendar = Class.create(Control.CymasterCalendar, {

    initialize : function(field, priorfield, nextfield, st, days, ajaxUri, tw_f, rSpan) {
        var calendar = this;
        Control.cypopupCalendars.push(calendar); //配列の最後に自分を追加
        this.myField = $(field);
        this.priorfield = $(priorfield);
        this.nextfield = $(nextfield);
        this.fieldName = this.myField.id;
        this.isOpen = false;
        this.isClick = false;
        this.eventSeted = false;
        this.doubleBooking = false;
        //予約可能月分の休業日this.offPlan['2008-12']="1,2,3"
        this.offPlan = $H({});　//ハッシュ
        this.offDays = [];
        this.holidays = [];
        this.stdDays = [];
        this.fullDays = [];

        this.packedPar = $H({});

        this.orgYmd = "";       //予約訂正時　自分の予約日でフルになっている場合のフル除去のため
        this.ajaxUri = ajaxUri;

        this.today = new Date(st);
        this.endDay = new Date();
        this.endDay.setTime(this.today.getTime() + (days * 24 * 3600 * 1000));
//----------------------------2009/09/24

        this.selectedDay = new Date();
        this.firstStd = new Date();
        this.lastStd = new Date();
        this.selfFocus = false;
//----------------------------




        this.rSpan = rSpan * 60 * 1000;
        //追加
        var t = new Date(st);
        this.InitToday =  t.getTime();

        if(tw_f){
            this.disp2month = true;
        }
        else{
            this.disp2month = false;

        }
        if (!Control.CypopupCalendar.CONTROL) {
            Control.CypopupCalendar.CONTROL = {};
            if(!$("cypopupcalendar")) {
                var control = Builder.node('div', {id:'cypopupcalendar'});
                control.innerHTML =
                    'dumm';
                document.body.appendChild(control);
            }
            Control.CypopupCalendar.CONTROL = {
                popUp : $("cypopupcalendar")
            };
            Element.hide($("cypopupcalendar"));
        }

        this.control = Control.CypopupCalendar.CONTROL;


        this.fieldOnFocusListener = this.offPlanSet.bindAsEventListener(this);
        this.fieldKeydownListener = this.fieldKeydown.bindAsEventListener(this);
        Event.observe(this.myField, "focus", this.fieldOnFocusListener,false);
//        Event.observe(this.myField, "click", this.fieldOnFocusListener,false);
        Event.observe(this.myField, "keydown", this.fieldKeydownListener,false);

        this.popupOnClickListener = this.click.bindAsEventListener(this);
        this.docOnClickListener   = this.docClick.bindAsEventListener(this);
        Event.observe(document, 'click', this.docOnClickListener,false);
        Event.observe(this.control.popUp, "click", this.popupOnClickListener,false);
        this.planGetHostListener = this.planGetHost.bindAsEventListener(this);
    },

    open : function() {

        Control.CypopupCalendar.activeCalendar = this;
        if(this.myField.value != "") {
            var dval = this.myField.value.split("/");
            this.YY = dval[0] - 0;
            this.MM = dval[1] - 0;
            this.DD = dval[2] - 0;
            if(this.DD == 0){ // 時間超過で予約可能時間帯がなくなった
                var y;
                var trg;
                while(this.today.getTime() <= this.endDay.getTime()){
                    this.today.setTime(this.today.getTime() + (24 * 3600 * 1000));
                    y = this.today.getYear();
                    if(y < 2000){
                        y += 1900;
                    }
                    trg = 'cyc-' + y + '-' + (this.today.getMonth() + 1) + '-' + this.today.getDate();
                    if(this.stdDays.include(trg))
                        break;
                }
                this.myField.value = "";
                this.YY = this.today.getYear();
                this.MM = this.today.getMonth() + 1;
                this.DD = this.today.getDate();
                if (this.YY < 2000) {
                    this.YY += 1900;
                }
            }
            else{
                var myTime = new Date(this.myField.value);
                var tscope = this.priorfield.value.split(",");
                var ss = myTime.getTime() +((tscope[1] - 0) * 60 * 1000) - this.rSpan;
                var nn = new Date();
                if(nn.getTime() >= ss){
                    this.myField.value = "";
                }
            }
        }
 //     else {
            this.today.setTime(this.InitToday);
            var tscope = this.priorfield.value.split(",");
            var sttime = this.today.getTime() +((tscope[1] - 0) * 60 * 1000) - this.rSpan;
            var ntime = new Date();
            var y;
            if(ntime.getTime() >= sttime){
                this.today.setTime(this.today.getTime() + (24 * 3600 * 1000));
            }
            this.todayReset();

            this.YY = this.today.getYear();
            this.MM = this.today.getMonth() + 1;
            this.DD = this.today.getDate();
            if(this.YY < 2000) {
                this.YY += 1900;
            }


//        }


        this.YYY = this.YY;
        this.MMM = this.MM;
//----------------------------2009/09/24

        this.selectedDay = new Date(this.YY,  this.MM - 1, this.DD);

//      Event.stopObserving(this.myField, 'focus');
        this.renderPopup();

    },

    todayReset:function(){
        var targ = [];
        targ = targ.concat(this.fullDays);
        var keys = this.offPlan.keys();
        var vals = this.offPlan.values();
        var i,j,key,val, ele, dd, y;
        for(i =0; i < keys.length; i++){
            key = keys[i];
            ele = vals[i].split(",");
            for(j=0; j < ele.length; j++){
                targ.push(key + '-' + ele[j]);
            }
        }
        while(this.today.getTime() <= this.endDay.getTime()){
            y = this.today.getYear();
            if(y < 2000){
                 y += 1900;
            }
            dd = y + '-' + (this.today.getMonth() + 1) + '-' + this.today.getDate();
            if (!targ.include(dd)) {
                break;
            }
            else{
                this.today.setTime(this.today.getTime() + (24 * 3600 * 1000));
            }
       }
    },

    offPlanSet: function(event){
/*
        if(Control.CypopupCalendar.activeCalendar == this){
            return;
        }
*/
        if(this.selfFocus){
            this.selfFocus = false;
            return;
        }
        Control.CypopupCalendar.activeCalendar = this;


/*
        if (!this.priorfield.value.match(/([01][0-9]|2[0-3]):[0-5][0-9]\s～\s([01][0-9]|2[0-3]):[0-5][0-9]$/)) {
            event.stop();
            this.priorfield.focus();
        }
        var tscope = this.priorfield.value.split(" ～ ");
*/
        var tscope = this.priorfield.value.split(",");

        var dd = new Date();
        dd = dd.getTime();
        this.today.setTime(this.InitToday);

        var syy = this.today.getYear();
        if (syy < 2000)
            syy += 1900;
        var smm = this.today.getMonth() + 1;
        var eyy = this.endDay.getYear();
        if (eyy < 2000)
            eyy += 1900;
        var emm = this.endDay.getMonth() + 1;

        var sdd = this.today.getDate();
        var edd = this.endDay.getDate();
        var symd = 'symd=' + syy + '-' + smm + '-' + sdd;
        var eymd = 'eymd=' + eyy + '-' + emm + '-' + edd;
        var data = 'syy=' + syy + '&' + 'smm=' + smm + '&' + 'eyy=' + eyy + '&' + 'emm=' + emm + '&' +
                    symd +'&' + eymd + '&stime=' + tscope[0] + '&etime=' + tscope[1];
        var options = {
            method: 'post',
            onSuccess: this.planGetHostListener,
            parameters: data,
            evalJSON: true,
            sanitizeJSON: false
        };
        new Ajax.Request(this.ajaxUri, options);
    }

});


Control.CydialyCalendar = Class.create(Control.CymasterCalendar, {
    initialize : function($super, field, nextfield, st, days, ajaxUri, tw_f, func) {
        $super(field, nextfield, st, days, ajaxUri, tw_f);
        this.fieldCangeListener = func.bindAsEventListener(this);
        this.fieldExitListener = this.fieldExit.bindAsEventListener(this);
//        Event.observe(this.myField, "blur", this.fieldExitListener,false);



//        Event.observe(this.myField, "change", this.fieldCangeListener,false);

    },
    fieldExit : function(){
        Event.observe(this.myField, "focus", this.fieldOnFocusListener);
    },

    makeHtml : function(month_f){
        var nowVal = this.today.getTime();
        var endVal = this.endDay.getTime();
        var outVal = new Date();
        var weekTitle = new Array('日','月','火','水','木','金','土');
        var i, j, work, yy, mm, dd, tt, idVal, Ymd;
//---------------------2009/09/24
        var f;
//---------------------
        outVal.setTime(this.startDay.getTime());
        var html  = '<center>' + "\n";
        html += '<table class="cycalendar" id="cyc-' + this.YY + '-' + this.MM + '" >' + "\n";
        html += '<tr>' + "\n";
/*
        html += '<td colspan="2" class="cycalen-head" id="cyc-mm-lt" title="前月へ"><b>&lt;&lt;</b></td>';
        html += '<td colspan="3" class="cycalen-head">';
        html += (this.YY-1988) + '年' + this.MM + '月</td>';
        html += '<td colspan="2" class="cycalen-head" id="cyc-mm-gt" title="翌月へ"><b>&gt;&gt;</b></td>';
*/
        html += '<td colspan="7" class="cycalen-head">' + "\n";
        switch(month_f){
            case 0:
                html += '<span title="前月へ" id="cyc-mm-lt"><b>&lt;&lt;</b></span>' + "\n";
                html += '<span id="cyc-mouse-over" class="cyc-mover-color">&nbsp;</span>' + this.YY  + '年' + this.MM + '月&nbsp;' + "\n";
                html += '<span title="翌月へ" id="cyc-mm-gt"><b>&gt;&gt;</b></span>' + "\n";
                break;
            case 1:
                html += '<span title="前月へ" id="cyc-mm-lt"><b>&lt;&lt;</b></span>' + "\n";
                html += '<span id="cyc-mouse-over" class="cyc-mover-color">&nbsp;&nbsp;</span>' + this.YY  + '年' + this.MM + '月&nbsp;&nbsp;&nbsp;' + "\n";
                break;
            default:
                html += '&nbsp;&nbsp;&nbsp;' + this.YY  + '年' + this.MM + '月&nbsp;' + "\n";
                html += '<span title="翌月へ" id="cyc-mm-gt"><b>&gt;&gt;</b></span>' + "\n";

        }
        html += '</td>' + "\n";
        html += '</tr>' + "\n";
        html += '<tr>' + "\n";
        for(i = 0; i < 7; i++) {
            html += '<td class="cyc-base ';
            switch (i) {
                case 0:
                    html += 'cyc-week-sun-mj-color ';
                    break;
                case 6:
                    html += 'cyc-week-sat-mj-color ';
                    break;
                default:
                    html += 'cyc-week-std-mj-color ';
            }
            html += 'cyc-weeks-bg-color">' + weekTitle[i] + '</td>' + "\n";
        }
        html += '</tr>' + "\n";
        for(i = 0; i < this.weekCnt; i++){
            html += '<tr>' + "\n";
            if(i >= 4)
                html += '<!-- -->'
            for(j = 0; j < 7; j++){
                f = false;//-------------------2009/09/24
                yy = outVal.getYear();
                if(yy < 2000)
                    yy += 1900;
                mm = outVal.getMonth() + 1;
                dd = outVal.getDate();
                tt = outVal.getTime();
                html += '<td class="cyc-base ';
/*
                if(yy != this.YY || mm != this.MM || tt < nowVal || tt > endVal) {
                    html += 'cyc-out-mj-color cyc-out-bg-color">';
                }
*/
                if(yy != this.YY || mm != this.MM || tt > endVal) {
                    html += 'cyc-out-mj-color cyc-out-bg-color">';
                }
                else{
                    Ymd = "" + yy + '-' + mm + '-' + dd;
                    if (this.fullDays.include(Ymd) && (Ymd != this.orgYmd)) {
                        idVal = 'cyc-' + yy + '-' + mm + '-' + dd;
                        html += 'cyc-full-mj-color cyc-full-bg-color" id="' + idVal +  '" title="予約満席">';
                        this.stdDays.push(idVal);
                        f = true;
                    }
                    else {

                        if (j == 0 || this.holidays.include(dd)) {
                            html += 'cyc-sun-mj-color ';
                        }
                        else
                            if (j == 6) {
                                html += 'cyc-sat-mj-color ';
                            }
                            else {
                                html += 'cyc-std-mj-color ';
                            }
                        if (this.offDays.include(dd)) {
                            html += 'cyc-off-bg-color" title="休院日">';
                        }
                        else {
                            idVal = 'cyc-' + yy + '-' + mm + '-' + dd;
                            html += 'cyc-std-bg-color" id="' + idVal + '" title="予約可">';
                            this.stdDays.push(idVal);
                            f = true;
                        }
                    }
                }
                //--------------2009/09/24
/*
                if(yy == this.YYY && mm == this.MMM && dd == this.DD && f==true){
                    html += '<div class="cyc-today-style">' + dd + '</div>';
                }
*/
                if(outVal.getTime() == this.selectedDay.getTime() && f==true){
                    html += '<div class="cyc-today-style">' + dd + '</div>';
                }
                else{
                    html += dd;
                }
                html += '</td>' + "\n";
                outVal.setTime(outVal.getTime() + (24*3600*1000));
            }
            html += '</tr>' + "\n";
        }
        html += '</table>';
        html+='</center>';
        return html;
    },
    offPlanSet : function() {
/*
        if(Control.CypopupCalendar.activeCalendar == this){
            return;
        }
*/
        if(this.selfFocus){
            this.selfFocus = false;
            return;
        }

        Control.CypopupCalendar.activeCalendar = this;
        this.planGetHostListener = this.planGetHost.bindAsEventListener(this);


        var prior = this.today.getTime() - (this.endDay.getTime() - this.today.getTime());
        var st = new Date();
        st.setTime(prior);


        var dd = new Date();
        dd = dd.getTime();
        var syy = st.getYear();
        if(syy < 2000)
            syy += 1900;
        var smm = st.getMonth() + 1;
        var eyy = this.endDay.getYear();
        if(eyy < 2000)
            eyy += 1900;
        var emm = this.endDay.getMonth() + 1;

        var sdd = st.getDate();
        var edd = this.endDay.getDate();
        var symd = 'symd=' + syy + '-' + smm + '-' + sdd;
        var eymd = 'eymd=' + eyy + '-' + emm + '-' + edd;
        var data = 'syy='+syy+'&'+'smm='+smm+'&'+'eyy='+eyy+'&'+'emm='+emm+'&'+
                    symd + '&' + eymd + '&' + 'tm=' + dd;
        var options = {
            method:'post',
            onSuccess:this.planGetHostListener,
            parameters:data,
            evalJSON:true,
            sanitizeJSON:false
        };
        new Ajax.Request(this.ajaxUri, options);
    },
    setYmd : function(event){
        if(this.stdDays.length <= 0)
            return;
        var Id = this.makeId(this.selectedDay);
        if ($(Id)){
            var val = Id.split("-");
            if (val[1]) {
                Event.stop(event);
                if (val[2] < 10)
                    val[2] = '0' + val[2];
                if (val[3] < 10)
                    val[3] = '0' + val[3];
                this.myField.value = val[1] + '/' + val[2] + '/' + val[3];
                this.close(event);
                this.fieldCangeListener();
                if(this.nextfield != null)
                    this.nextfield.focus();
                else
                    this.myField.blur();
            }
        }
    },




    stdDayClick : function(event){
        if(this.doubleBooking)
            return;
        var ele = Event.element(event);
        var id = ele.id;
        if(id == ""){
            id = ele.parentNode.id
        }

        var val = id.split("-");
        if (val[1]) {
            Event.stop(event);
            if (val[2] < 10)
                val[2] = '0' + val[2];
            if (val[3] < 10)
                val[3] = '0' + val[3];
            this.myField.value = val[1] + '/' + val[2] + '/' + val[3];
            this.close(event);
            this.fieldCangeListener();
            if(this.nextfield != null)
                this.nextfield.focus();
        }
    }



});
