if ( typeof(BCL) == 'undefined' ) BCL = function() {};


BCL.Calendar = function ( eid, fid, valname ) {
    this.eid = eid;
    this.formid = fid;
    this.valname = valname;
    this.__dispelem = null;
    this.__textelem = null;
    this.__opaciobj = null;
    this.style = new BCL.Calendar.Style();
    return this;
};

BCL.Calendar.VERSION = "0.13";

BCL.Calendar.prototype.spliter = "/";
BCL.Calendar.prototype.date = null;
BCL.Calendar.prototype.min_date = null;
BCL.Calendar.prototype.max_date = null;

BCL.Calendar.prototype.start_day = 0;

BCL.Calendar.prototype.draw_border = true;

BCL.Calendar.prototype.selectable_days = new Array(true,true,true,true,true,true,true);

BCL.Calendar.prototype.zindex = 10;

BCL.Calendar.Style = function() {
    return this;
};

BCL.Calendar.Style.prototype.frame_width        = "150px";
BCL.Calendar.Style.prototype.frame_color        = "#999999";
BCL.Calendar.Style.prototype.font_size          = "12px";
BCL.Calendar.Style.prototype.day_bgcolor        = "#F0F0F0";
BCL.Calendar.Style.prototype.month_color        = "#FFFFFF";
BCL.Calendar.Style.prototype.month_hover_color  = "#666666";
BCL.Calendar.Style.prototype.month_hover_bgcolor= "#CCCCCC";
BCL.Calendar.Style.prototype.weekday_color      = "#404040";
BCL.Calendar.Style.prototype.saturday_color     = "#0040D0";
BCL.Calendar.Style.prototype.sunday_color       = "#D00000";
BCL.Calendar.Style.prototype.others_color       = "#999999";
BCL.Calendar.Style.prototype.day_hover_bgcolor  = "#66CCFF";
BCL.Calendar.Style.prototype.cursor             = "pointer";

BCL.Calendar.Style.prototype.today_color        = "#000000";
BCL.Calendar.Style.prototype.today_border_color = "#CCCCCC";
BCL.Calendar.Style.prototype.others_border_color= "#CCCCCC";

BCL.Calendar.Style.prototype.today_bgcolor      = "#FF9900";
BCL.Calendar.Style.prototype.unselectable_day_bgcolor = "#D1D1D1"; 


BCL.Calendar.Style.prototype.set = function(key,val) { this[key] = val; }
BCL.Calendar.Style.prototype.get = function(key) { return this[key]; }
BCL.Calendar.prototype.setStyle = function(key,val) { this.style.set(key,val); };
BCL.Calendar.prototype.getStyle = function(key) { return this.style.get(key); };

BCL.Calendar.prototype.initDate = function ( dd ) {
    if ( ! dd ) dd = new Date();
    var year = dd.getFullYear();
    var mon  = dd.getMonth();
    var date = dd.getDate();
    this.date = new Date( year, mon, date );
    this.getFormValue();
    return this.date;
}

BCL.Calendar.prototype.getOpacityObject = function () {
    if ( this.__opaciobj ) return this.__opaciobj;
    var cal = this.getCalendarElement();
    if ( ! BCL.Opacity ) return;
    this.__opaciobj = new BCL.Opacity( cal );
    return this.__opaciobj;
};

BCL.Calendar.prototype.getCalendarElement = function () {
    if ( this.__dispelem ) return this.__dispelem;
    this.__dispelem = document.getElementById( this.eid )
    return this.__dispelem;
};

BCL.Calendar.prototype.getFormElement = function () {
    if ( this.__textelem ) return this.__textelem;
    var frmelms = document.getElementById( this.formid );
    if ( ! frmelms ) return;
    for( var i=0; i < frmelms.elements.length; i++ ) {
        if ( frmelms.elements[i].name == this.valname ) {
            this.__textelem = frmelms.elements[i];
        }
    }
    return this.__textelem;
};

BCL.Calendar.prototype.setDateYMD = function (ymd) {
    var splt = ymd.split( this.spliter );
    if ( splt[0]-0 > 0 &&
         splt[1]-0 >= 1 && splt[1]-0 <= 12 && 
         splt[2]-0 >= 1 && splt[2]-0 <= 31 ) {
        if ( ! this.date ) this.initDate();
            this.date.setDate( splt[2] );
            this.date.setMonth( splt[1]-1 );
            this.date.setFullYear( splt[0] );
    } else {
        ymd = "";
    }
    return ymd;
};

BCL.Calendar.prototype.getDateYMD = function ( dd ) {
    if ( ! dd ) {
        if ( ! this.date ) this.initDate();
        dd = this.date;
    }
    var mm = "" + (dd.getMonth()+1);
    var aa = "" + dd.getDate();
    if ( mm.length == 1 ) mm = "" + "0" + mm;
    if ( aa.length == 1 ) aa = "" + "0" + aa;
    return dd.getFullYear() + this.spliter + mm + this.spliter + aa;
};

BCL.Calendar.prototype.getFormValue = function () {
    var form1 = this.getFormElement();
    if ( ! form1 ) return "";
    var date1 = this.setDateYMD( form1.value );
    return date1;
};

BCL.Calendar.prototype.setFormValue = function (ymd) {
    if ( ! ymd ) ymd = this.getDateYMD();
    var form1 = this.getFormElement();
    if ( form1 ) form1.value = ymd;
    if(typeof form1.onchange == "function") form1.onchange();
};

BCL.Calendar.prototype.show = function () {
    this.getCalendarElement().style.display = "";
};

BCL.Calendar.prototype.hide = function () {

    this.getCalendarElement().style.display = "none";
};

BCL.Calendar.prototype.fadeOut = function (s) {
    if ( BCL.Opacity ) {
        this.getOpacityObject().fadeOut(s);
    } else {
        this.hide();
    }
};

BCL.Calendar.prototype.moveMonth = function ( mon ) {

    if ( ! this.date ) this.initDate();
    for( ; mon<0; mon++ ) {
        this.date.setDate(1); 
        this.date.setTime( this.date.getTime() - (24*3600*1000) );
    }
    for( ; mon>0; mon-- ) {
        this.date.setDate(1); 
        this.date.setTime( this.date.getTime() + (24*3600*1000)*32 );
    }
    this.date.setDate(1);  
    this.write();
};


BCL.Calendar.prototype.addEvent = function ( elem, ev, func ) {
    if ( window.Event && Event.observe ) {
        Event.observe( elem, ev, func, false );
    } else {
        elem["on"+ev] = func;
    }
}

BCL.Calendar.prototype.write = function () {
    var date = new Date();
    if ( ! this.date ) this.initDate();
    date.setTime( this.date.getTime() );

    var year = date.getFullYear();    
    var mon  = date.getMonth();      
    var today = date.getDate();        
    var form1 = this.getFormElement();

    var min;
    if ( this.min_date ) {
        var tmp = new Date( this.min_date.getFullYear(),
            this.min_date.getMonth(), this.min_date.getDate() );
        min = tmp.getTime();
    }
    var max;
    if ( this.max_date ) {
        var tmp = new Date( this.max_date.getFullYear(),
            this.max_date.getMonth(), this.max_date.getDate() );
        max = tmp.getTime();
    }

    date.setDate(1);
    var wday = date.getDay();      

    if ( wday != this.start_day ) {
        date.setTime( date.getTime() - (24*3600*1000)*((wday-this.start_day+7)%7) );
    }

    var list = new Array();
    for( var i=0; i<42; i++ ) {
        var tmp = new Date();
        tmp.setTime( date.getTime() + (24*3600*1000)*i );
        if ( i && i%7==0 && tmp.getMonth() != mon ) break;
        list[list.length] = tmp;
    }

    var month_table_style = 'width: 100%; ';
    month_table_style += 'background: '+this.style.frame_color+'; ';
    month_table_style += 'border: 1px solid '+this.style.frame_color+';';

    var week_table_style = 'width: 100%; ';
    week_table_style += 'background: '+this.style.day_bgcolor+'; ';
    week_table_style += 'border-left: 1px solid '+this.style.frame_color+'; ';
    week_table_style += 'border-right: 1px solid '+this.style.frame_color+'; ';

    var days_table_style = 'width: 100%; ';
    days_table_style += 'background: '+this.style.day_bgcolor+'; ';
    days_table_style += 'border: 1px solid '+this.style.frame_color+'; ';

    var month_td_style = "";
    month_td_style += 'background: '+this.style.frame_color+'; ';
    month_td_style += 'font-size: '+this.style.font_size+'; ';
    month_td_style += 'color: '+this.style.month_color+'; ';
    month_td_style += 'padding: 4px 0px 2px 0px; ';
    month_td_style += 'text-align: center; ';
    month_td_style += 'font-weight: bold;';

    var week_td_style = "";
    week_td_style += 'background: '+this.style.day_bgcolor+'; ';
    week_td_style += 'font-size: '+this.style.font_size+'; ';
    week_td_style += 'padding: 2px 0px 2px 0px; ';
    week_td_style += 'font-weight: bold;';
    week_td_style += 'text-align: center;';

    var days_td_style = "";
    days_td_style += 'background: '+this.style.day_bgcolor+'; ';
    days_td_style += 'font-size: '+this.style.font_size+'; ';
    days_td_style += 'padding: 1px; ';
    days_td_style += 'text-align: center; ';
    days_td_style += 'font-weight: bold;';

    var days_unselectable = "font-weight: normal; color:#E3E3E3;";

    var src1 = "";

    src1 += '<div id="'+this.eid+'_screen" style="position:relative;z-index:'+(this.zindex+1)+';">\n';

    src1 += '<table border="0" cellpadding="0" cellspacing="0" style="'+month_table_style+'"><tr>';
    src1 += '<td id="__'+this.eid+'_btn_prev" title="前の月へ" style="'+month_td_style+'">≪</td>';
    src1 += '<td style="'+month_td_style+'">&nbsp;</td>';

    src1 += '<td id="__'+this.eid+'_btn_today" style="'+month_td_style+'">'+(year)+'年 '+(mon+1)+'月</td>';

    src1 += '<td id="__'+this.eid+'_btn_close" title="閉じる" style="'+month_td_style+'">×</td>';
    src1 += '<td id="__'+this.eid+'_btn_next" title="次の月へ" style="'+month_td_style+'">≫</td>';
    src1 += "</tr></table>\n";
    src1 += '<table border="0" cellpadding="0" cellspacing="0" style="'+week_table_style+'"><tr>';

    for(var i = this.start_day; i < this.start_day + 7; i++){
        var _wday = i%7;
        if(_wday == 0){
             src1 += '<td style="color: '+this.style.sunday_color+'; '+week_td_style+'">日</td>';
        }else if(_wday == 6){
             src1 += '<td style="color: '+this.style.saturday_color+'; '+week_td_style+'">土</td>';
        }else{
             src1 += '<td style="color: '+this.style.weekday_color+'; '+week_td_style+'">';
            if(_wday == 1)        src1 += '月</td>';
            else if(_wday == 2)    src1 += '火</td>';
            else if(_wday == 3)    src1 += '水</td>';
            else if(_wday == 4)    src1 += '木</td>';
            else if(_wday == 5)    src1 += '金</td>';
        }
    }

    src1 += "</tr></table>\n";
    src1 += '<table border="0" cellpadding="0" cellspacing="0" style="'+days_table_style+'">';

    var curutc;
    if ( form1 && form1.value ) {
        var splt = form1.value.split(this.spliter);
        if ( splt[0] > 0 && splt[2] > 0 ) {
            var curdd = new Date( splt[0]-0, splt[1]-1, splt[2]-0 );
            curutc = curdd.getTime(); 
        }
    }

    var realdd = new Date();
    var realutc = (new Date(realdd.getFullYear(),realdd.getMonth(),realdd.getDate())).getTime();

    for ( var i=0; i<list.length; i++ ) {
        var dd = list[i];
        var ww = dd.getDay();
        var mm = dd.getMonth();

        if ( ww == this.start_day ) {
            src1 += "<tr>";
        }
        var cc = days_td_style;
        var utc = dd.getTime();

        if ( mon == mm ) {

            if ( utc == realutc ){
                cc += "color: "+this.style.today_color+";";
            } else

            if ( ww == 0 ) {
                cc += "color: "+this.style.sunday_color+";";
            } else if ( ww == 6 ) {
                cc += "color: "+this.style.saturday_color+";";
            } else {
                cc += "color: "+this.style.weekday_color+";";
            }
        } else {
            cc += "color: "+this.style.others_color+";";
        }

        if (( min && min > utc ) || ( max && max < utc ) || ( !this.selectable_days[dd.getDay()] )) {
            cc += days_unselectable;
        }
        if ( utc == curutc ) {
            cc += "background: "+this.style.day_hover_bgcolor+";";
        }
        else if ( mon == mm && utc == realutc ) {
            cc += "background: "+this.style.today_bgcolor+";";
        }
        else if (( min && min > utc ) || ( max && max < utc ) || ( !this.selectable_days[dd.getDay()] )) {
            cc += 'background: '+this.style.unselectable_day_bgcolor+';';
        }
        if ( this.draw_border ){
            if ( mon == mm && utc == realutc ){
                cc += "border:solid 1px "+this.style.today_border_color+";";
            } else {
                cc += "border:solid 1px "+this.style.others_border_color+";";
            }
        }

        var ss = this.getDateYMD(dd);
        var tt = dd.getDate();

        src1 += '<td style="'+cc+'" title='+ss+' id="__'+this.eid+'_td_'+ss+'">'+tt+'</td>';

        if ( ww == (this.start_day+6)%7 ) {
            src1 += "</tr>\n"; 
        }
    }
    src1 += "</table>\n";

    src1 += '</div>\n';

    var cal1 = this.getCalendarElement();
    if ( ! cal1 ) return;
    cal1.style.width = this.style.frame_width;
    cal1.style.position = "absolute";
    cal1.innerHTML = src1;

    var ua = navigator.userAgent;
    if( ua.indexOf("MSIE 5.5") >= 0 || ua.indexOf("MSIE 6") >= 0 ){

        this.show();
        var screenHeight = cal1.document.getElementById(this.eid+"_screen").offsetHeight;
        this.hide();

        src1 += '<div style="position:absolute;z-index:'+this.zindex+';top:0px;left:0px;">';
        src1 += '<iframe /?scid="dummy.htm" frameborder=0 scrolling=no width='+this.style.frame_width+' height='+screenHeight+'></iframe>';
        src1 += '</div>\n';

        cal1.innerHTML = src1;
    }

    var __this = this;
    var get_src = function (ev) {
        ev  = ev || window.event;
        var src = ev.target || ev.srcElement;
        return src;
    };
    var month_onmouseover = function (ev) {
        var src = get_src(ev);
        src.style.color = __this.style.month_hover_color;
        src.style.background = __this.style.month_hover_bgcolor;
    };
    var month_onmouseout = function (ev) {
        var src = get_src(ev);
        src.style.color = __this.style.month_color;
        src.style.background = __this.style.frame_color;
    };
    var day_onmouseover = function (ev) {
        var src = get_src(ev);
        src.style.background = __this.style.day_hover_bgcolor;
    };
    var day_onmouseout = function (ev) {
        var src = get_src(ev);
        var today = new Date();
        if( today.getMonth() == __this.date.getMonth() && src.id == '__'+__this.eid+'_td_'+__this.getDateYMD(today) ){
            src.style.background = __this.style.today_bgcolor;
        }else{
            src.style.background = __this.style.day_bgcolor;
        }
    };
    var day_onclick = function (ev) {
        var src = get_src(ev);
        var srcday = src.id.substr(src.id.length-10);
        __this.setFormValue( srcday );
        __this.fadeOut( 1.0 );
		  getval(src.id);
    };

    var tdprev = document.getElementById( "__"+this.eid+"_btn_prev" );
    var tmpDate = new Date(year,mon,1);
    tmpDate.setTime( tmpDate.getTime() - (24*3600*1000) );
    if ( !min || this.min_date <= tmpDate ){
        tdprev.style.cursor = this.style.cursor;
        this.addEvent( tdprev, "mouseover", month_onmouseover );
        this.addEvent( tdprev, "mouseout", month_onmouseout );
        this.addEvent( tdprev, "click", function(){ __this.moveMonth( -1 ); });
    }
    else{
        tdprev.title = "前の月は選択できません";
    }

    var nMov = (realdd.getFullYear() - year) * 12 + (realdd.getMonth() - mon);
    if ( nMov != 0 ){
        var tdtoday = document.getElementById( "__"+this.eid+"_btn_today" );
        tdtoday.style.cursor = this.style.cursor;
        tdtoday.title = "現在の月へ";
        this.addEvent( tdtoday, "mouseover", month_onmouseover );
        this.addEvent( tdtoday, "mouseout", month_onmouseout );
        this.addEvent( tdtoday, "click", function(){ __this.moveMonth( nMov ); });
    }

    var tdclose = document.getElementById( "__"+this.eid+"_btn_close" );
    tdclose.style.cursor = this.style.cursor;
    this.addEvent( tdclose, "mouseover", month_onmouseover );
    this.addEvent( tdclose, "mouseout", month_onmouseout );

    this.addEvent( tdclose, "click", function(){ __this.getFormValue(); __this.hide(); });

    var tdnext = document.getElementById( "__"+this.eid+"_btn_next" );

    var tmpDate = new Date(year,mon,1);
    tmpDate.setTime( tmpDate.getTime() + (24*3600*1000)*32 );
    tmpDate.setDate(1);

    if ( !max || this.max_date >= tmpDate ){
        tdnext.style.cursor = this.style.cursor;
        this.addEvent( tdnext, "mouseover", month_onmouseover );
        this.addEvent( tdnext, "mouseout", month_onmouseout );
        this.addEvent( tdnext, "click", function(){ __this.moveMonth( +1 ); });
    }
    else{
        tdnext.title = "次の月は選択できません";
    }

    for ( var i=0; i<list.length; i++ ) {
        var dd = list[i];
        if ( mon != dd.getMonth() ) continue;

        var utc = dd.getTime();
        if ( min && min > utc ) continue;
        if ( max && max < utc ) continue;
        if ( utc == curutc ) continue;  
        if ( !this.selectable_days[dd.getDay()] ) continue;

        var ss = this.getDateYMD(dd);
        var cc = document.getElementById( "__"+this.eid+"_td_"+ss );
        if ( ! cc ) continue;

        cc.style.cursor = this.style.cursor;
        this.addEvent( cc, "mouseover", day_onmouseover );
        this.addEvent( cc, "mouseout", day_onmouseout );
        this.addEvent( cc, "click", day_onclick );
    }

    this.show();
};

BCL.Calendar.prototype.getCalenderElement = BCL.Calendar.prototype.getCalendarElement;
BCL.Calender = BCL.Calendar;