if ( typeof(JKL) == 'undefined' ) JKL = function() {};



JKL.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 JKL.Calendar.Style();
    return this;
};



JKL.Calendar.VERSION = "0.13";



JKL.Calendar.prototype.spliter = "/";
JKL.Calendar.prototype.date = null;
JKL.Calendar.prototype.min_date = null;
JKL.Calendar.prototype.max_date = null;


JKL.Calendar.prototype.start_day = 0;


JKL.Calendar.prototype.draw_border = true;



JKL.Calendar.Style = function() {
    return this;
};



JKL.Calendar.Style.prototype.frame_width        = "200px";
JKL.Calendar.Style.prototype.frame_color        = "#ffffff";
JKL.Calendar.Style.prototype.font_size          = "12px";
JKL.Calendar.Style.prototype.day_bgcolor        = "#ffffff";
JKL.Calendar.Style.prototype.month_color        = "#000000";
JKL.Calendar.Style.prototype.month_hover_color  = "#009900";
JKL.Calendar.Style.prototype.month_hover_bgcolor= "#FFFFCC";
JKL.Calendar.Style.prototype.weekday_color      = "#404040";
JKL.Calendar.Style.prototype.saturday_color     = "#404040";
JKL.Calendar.Style.prototype.sunday_color       = "#404040";
JKL.Calendar.Style.prototype.others_color       = "#999999";
JKL.Calendar.Style.prototype.day_hover_bgcolor  = "#FF9933";
JKL.Calendar.Style.prototype.cursor             = "pointer";


JKL.Calendar.Style.prototype.today_color        = "#000000";

JKL.Calendar.Style.prototype.today_border_color = "#ffffff";
JKL.Calendar.Style.prototype.others_border_color= "#ffffff";



JKL.Calendar.Style.prototype.set = function(key,val) { this[key] = val; }
JKL.Calendar.Style.prototype.get = function(key) { return this[key]; }
JKL.Calendar.prototype.setStyle = function(key,val) { this.style.set(key,val); };
JKL.Calendar.prototype.getStyle = function(key) { return this.style.get(key); };



JKL.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;
}



JKL.Calendar.prototype.getOpacityObject = function () {
    if ( this.__opaciobj ) return this.__opaciobj;
    var cal = this.getCalendarElement();
    if ( ! JKL.Opacity ) return;
    this.__opaciobj = new JKL.Opacity( cal );
    return this.__opaciobj;
};



JKL.Calendar.prototype.getCalendarElement = function () {
    if ( this.__dispelem ) return this.__dispelem;
    this.__dispelem = document.getElementById( this.eid )
    return this.__dispelem;
};



JKL.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;
};



JKL.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.setFullYear( splt[0] );
        this.date.setMonth( splt[1]-1 );
        this.date.setDate( splt[2] );
    } else {
        ymd = "";
    }
    return ymd;
};





JKL.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;
};



JKL.Calendar.prototype.getFormValue = function () {
    var form1 = this.getFormElement();
    if ( ! form1 ) return "";
    var date1 = this.setDateYMD( form1.value );
    return date1;
};



JKL.Calendar.prototype.setFormValue = function (ymd) {
    if ( ! ymd ) ymd = this.getDateYMD();   
    var form1 = this.getFormElement();
    if ( form1 ) form1.value = ymd;
};



JKL.Calendar.prototype.show = function () {
    this.getCalendarElement().style.display = "";
};



JKL.Calendar.prototype.hide = function () {
    this.getCalendarElement().style.display = "none";
};



JKL.Calendar.prototype.fadeOut = function (s) {
    if ( JKL.Opacity ) {
        this.getOpacityObject().fadeOut(s);
    } else {
        this.hide();
    }
};



JKL.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();    
};



JKL.Calendar.prototype.addEvent = function ( elem, ev, func ) {

    if ( window.Event && Event.observe ) {
        Event.observe( elem, ev, func, false );
    } else {
        elem["on"+ev] = func;
    }
}



JKL.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 += '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: normal;';

    var week_td_style = "";
    week_td_style += 'font-size: '+this.style.font_size+'; ';
    week_td_style += 'padding: 2px 0px 2px 0px; ';
    week_td_style += 'font-weight: normal;';
    week_td_style += 'text-align: center;';

    var days_td_style = "";
    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: normal;';

    var days_unselectable = "font-weight: normal;";

    
    var src1 = "";

    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 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+'">Su</td>';
		}else if(_wday == 6){
			 src1 += '<td style="color: '+this.style.saturday_color+'; '+week_td_style+'">Sa</td>';
		}else{
			 src1 += '<td style="color: '+this.style.weekday_color+'; '+week_td_style+'">';
			if(_wday == 1)		src1 += 'Mo</td>';
			else if(_wday == 2)	src1 += 'Tu</td>';
			else if(_wday == 3)	src1 += 'We</td>';
			else if(_wday == 4)	src1 += 'Th</td>';
			else if(_wday == 5)	src1 += 'Fr</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 )) {
            cc += days_unselectable;
        }
        if ( utc == curutc ) {                                  
            cc += "background: "+this.style.day_hover_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";

    
    var cal1 = this.getCalendarElement();
    if ( ! cal1 ) return;
    cal1.style.width = this.style.frame_width;
    cal1.style.position = "absolute";
    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);
        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 );
    };

    
    var tdprev = document.getElementById( "__"+this.eid+"_btn_prev" );
    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 ); });

    
    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.hide(); });

    
    var tdnext = document.getElementById( "__"+this.eid+"_btn_next" );
    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 ); });

    
    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;

        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();
};


JKL.Calendar.prototype.getCalenderElement = JKL.Calendar.prototype.getCalendarElement;
JKL.Calender = JKL.Calendar;

