/* Calendar RC2, Copyright (c) 2007 Aeron Glemann <http://electricprism.com/aeron>, MIT Style License */
var Calendar=new Class({initialize:function(D,B){this.props=Object.extend({blocked:[],classes:["calendar","prev","next","month","year","invalid","valid","inactive","active","hover"],days:["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],direction:0,draggable:true,months:["January","February","March","April","May","June","July","August","September","October","November","December"],navigation:1,offset:0,pad:1,titleFormat:"D M jS Y"},B||{});
if(D==null||D.length==0){return false}this.props.offset%=7;this.calendar=new Element("div",{styles:{left:"-1000px",opacity:0,position:"absolute",top:"-1000px",zIndex:1000}}).addClass(this.props.classes[0]).injectInside(document.body);this.calendar.coord=this.calendar.getCoordinates();this.fx=this.calendar.effect("opacity",{onStart:function(){if(this.element.getStyle("opacity")==0){this.element.setStyle("display","block")
}},onComplete:function(){if(this.element.getStyle("opacity")==0){this.element.setStyle("display","none")}}});if(window.Drag&&this.props.draggable){new Drag.Move(this.calendar)}this.calendars=[];var F=0;var E=new Date();E.setDate(E.getDate()+this.props.direction.toInt());for(var A in D){var C={button:new Element("button",{type:"button"}),el:$(A),els:[],id:F++,month:E.getMonth(),visible:false,year:E.getFullYear()};
this.element(A,D[A],C);C.el.addClass(this.props.classes[0]);C.button.addClass(this.props.classes[0]).addEvent("click",function(G){this.toggle(G)}.pass(C,this)).injectAfter(C.el);C.val=this.evaluate(C);C.bounds=this.bounds(C);this.options(C);this.calendars.push(C)}},blocked:function(C){var A=[];var D=new Date(C.year,C.month,1).getDay();
var B=new Date(C.year,C.month+1,0).getDate();this.props.blocked.each(function(G){var F=G.split(" ");for(var H=0;H<3;H++){if(!F[H]){F[H]="*"}F[H]=F[H].contains(",")?F[H].split(","):new Array(F[H])}if(F[2].contains(C.year+"")||F[2].contains("*")){if(F[1].contains(C.month+1+"")||F[1].contains("*")){F[0].each(function(I){if(I>0){A.push(I.toInt())
}});if(F[3]){F[3]=F[3].contains(",")?F[3].split(","):new Array(F[3]);for(var H=0;H<B;H++){var E=(H+D)%7;if(F[3].contains(E+"")){A.push(H+1)}}}}}},this);return A},bounds:function(A){var B=new Date(1000,0,1);var F=new Date(2999,11,31);var D=new Date().getDate()+this.props.direction.toInt();if(this.props.direction>0){B=new Date();
B.setDate(D+this.props.pad*A.id)}if(this.props.direction<0){F=new Date();F.setDate(D-this.props.pad*(this.calendars.length-A.id-1))}var K,C,M;A.els.each(function(N){if(N.getTag()=="select"){if(N.format.test("(y|Y)")){K=[];N.getChildren().each(function(P){var O=this.unformat(P.value,N.format);if(!K.contains(O[0])){K.push(O[0])
}},this);K.sort(this.sort);if(K[0]>B.getFullYear()){d=new Date(K[0],B.getMonth()+1,0);if(B.getDate()>d.getDate()){B.setDate(d.getDate())}B.setYear(K[0])}if(K.getLast()<F.getFullYear()){d=new Date(K.getLast(),F.getMonth()+1,0);if(F.getDate()>d.getDate()){F.setDate(d.getDate())}F.setYear(K.getLast())}}if(N.format.test("(F|m|M|n)")){C=[];
N.getChildren().each(function(P){var O=this.unformat(P.value,N.format);if($type(O[0])!="number"||O[0]==A.year){if(!C.contains(O[1])){C.push(O[1])}}},this);C.sort(this.sort);if(B.getFullYear()==A.year&&C[0]>B.getMonth()){d=new Date(B.getFullYear(),C[0]+1,0);if(B.getDate()>d.getDate()){B.setDate(d.getDate())
}B.setMonth(C[0])}if(F.getFullYear()==A.year&&C.getLast()<F.getMonth()){d=new Date(B.getFullYear(),C.getLast()+1,0);if(F.getDate()>d.getDate()){F.setDate(d.getDate())}F.setMonth(C.getLast())}}if(N.format.test("(d|j)")&&!N.format.test("^(d|j)$")){M=[];N.getChildren().each(function(P){var O=this.unformat(P.value,N.format);
if(O[0]==A.year&&O[1]==A.month){if(!M.contains(O[2])){M.push(O[2])}}},this)}}},this);var J=1;var L=new Date(A.year,A.month+1,0).getDate();var E={month:true,year:true};var I={month:true,year:true};if(A.year==B.getFullYear()){E.year=false}if(A.year==F.getFullYear()){I.year=false}if(A.year==B.getFullYear()&&A.month==B.getMonth()){if(this.props.navigation==1){E.month=false
}J=B.getDate()}if(A.year==F.getFullYear()&&A.month==F.getMonth()){if(this.props.navigation==1){I.month=false}L=F.getDate()}var H=this.blocked(A);if($type(M)=="array"){M=M.filter(function(N){if(N>=J&&N<=L&&!H.contains(N)){return N}})}else{M=[];for(var G=J;G<=L;G++){if(!H.contains(G)){M.push(G)}}}M.sort(this.sort);
return{days:M,months:C,years:K,prev:E,next:I,start:B,end:F}},caption:function(F){var A=new Element("caption");var D=new Element("a").addClass(this.props.classes[1]).appendText("\x3c");var C=new Element("a").addClass(this.props.classes[2]).appendText("\x3e");if(this.props.navigation==2){var E=new Element("span").addClass(this.props.classes[3]).injectInside(A);
if(F.bounds.prev.month){D.clone().addEvent("click",function(G){this.navigate(G,"m",-1)}.pass(F,this)).injectInside(E)}E.adopt(new Element("span").appendText(this.props.months[F.month]));if(F.bounds.next.month){C.clone().addEvent("click",function(G){this.navigate(G,"m",1)}.pass(F,this)).injectInside(E)
}var B=new Element("span").addClass(this.props.classes[4]).injectInside(A);if(F.bounds.prev.year){D.clone().addEvent("click",function(G){this.navigate(G,"y",-1)}.pass(F,this)).injectInside(B)}B.adopt(new Element("span").appendText(F.year));if(F.bounds.next.year){C.clone().addEvent("click",function(G){this.navigate(G,"y",1)
}.pass(F,this)).injectInside(B)}}else{if(F.bounds.prev.month&&this.props.navigation){D.clone().addEvent("click",function(G){this.navigate(G,"m",-1)}.pass(F,this)).injectInside(A)}A.adopt(new Element("span").addClass(this.props.classes[3]).appendText(this.props.months[F.month]));A.adopt(new Element("span").addClass(this.props.classes[4]).appendText(F.year));
if(F.bounds.next.month&&this.props.navigation){C.clone().addEvent("click",function(G){this.navigate(G,"m",1)}.pass(F,this)).injectInside(A)}}return A},changed:function(A){A.val=this.evaluate(A);A.bounds=this.bounds(A);this.options(A);if(A.val){if(A.val.getDate()<A.bounds.days[0]){A.val.setDate(A.bounds.days[0])
}if(A.val.getDate()>A.bounds.days.getLast()){A.val.setDate(A.bounds.days.getLast())}A.els.each(function(B){B.value=this.format(A.val,B.format)},this)}if(!A.val){return }this.check(A);if(A.visible){this.display(A)}},clicked:function(A){this.options(A);A.els.each(function(B){B.value=this.format(A.val,B.format)
},this)},check:function(A){if(!A.val){return }this.calendars.each(function(D,B){if(D.val){var E=false;if(B<A.id){var C=new Date(Date.parse(A.val));C.setDate(C.getDate()-(this.props.pad*(A.id-B)));if(C<D.val){E=true}}if(B>A.id){var C=new Date(Date.parse(A.val));C.setDate(C.getDate()+(this.props.pad*(B-A.id)));
if(C>D.val){E=true}}if(E){if(D.bounds.start>C){C=D.bounds.start}if(D.bounds.end<C){C=D.bounds.end}D.val=C;D.month=C.getMonth();D.year=C.getFullYear();D.bounds=this.bounds(D);this.clicked(D);if(D.visible){this.display(D)}}}},this)},display:function(C){this.calendar.empty();this.calendar.className=this.props.classes[0]+" "+this.props.months[C.month].toLowerCase();
var B=new Element("div").injectInside(this.calendar);var Q=new Element("table").injectInside(B).adopt(this.caption(C));Q.setProperty("cellspacing",0);Q.setProperty("cellpadding",0);var L=new Element("thead").injectInside(Q);var M=new Element("tr").injectInside(L);for(var I=0;I<=6;I++){var D=this.props.days[(I+this.props.offset)%7];
M.adopt(new Element("th",{title:D}).appendText(D.substr(0,1)))}var J=new Element("tbody").injectInside(Q);var M=new Element("tr").injectInside(J);var H=new Date(C.year,C.month,1).getDay()-this.props.offset;var O=new Date(C.year,C.month+1,0).getDate();var K=new Date(C.year,C.month,0).getDate();var G=this.value(C);
var E=[];this.calendars.each(function(R){if(R!=C&&R.val){if(C.year==R.val.getFullYear()&&C.month==R.val.getMonth()){E.push(R.val.getDate())}}},this);var A=C.bounds.days;for(var I=1;I<43;I++){if((I-1)%7==0){M=new Element("tr").injectInside(J)}var F=new Element("td").injectInside(M);var N=I-H;var P="";
if(N===G){P=this.props.classes[8]}else{if(E.contains(N)){P=this.props.classes[7]}else{if(A.contains(N)){P=this.props.classes[6]}else{if(N>=1&&N<=O){P=this.props.classes[5]}}}}F.addClass(P);if(A.contains(N)){F.setProperty("title",this.format(new Date(C.year,C.month,N),this.props.titleFormat));F.addEvents({click:function(T,R,S){S.val=(this.value(S)==R)?null:new Date(S.year,S.month,R);
this.clicked(S);if(!S.val){S.val=this.evaluate(S)}if(S.val){this.check(S);this.toggle(S)}else{T.addClass(this.props.classes[6]);T.removeClass(this.props.classes[8])}}.pass([F,N,C],this),mouseover:function(S,R){S.addClass(R)}.pass([F,this.props.classes[9]]),mouseout:function(S,R){S.removeClass(R)}.pass([F,this.props.classes[9]])})
}if(N<1){N=K+N}else{if(N>O){N=N-O}}F.appendText(N)}},element:function(B,C,D){if($type(C)=="object"){for(var A in C){this.element(A,C[A],D)}return }B=$(B);B.format=C;if(B.getTag()=="select"){B.addEvent("change",function(E){this.changed(E)}.pass(D,this))}D.els.push(B)},evaluate:function(C){var A=[null,null,null];
C.els.each(function(F){var E=this.unformat(F.value,F.format);E.each(function(H,G){if($type(H)=="number"){A[G]=H}})},this);if($type(A[0])=="number"){C.year=A[0]}if($type(A[1])=="number"){C.month=A[1]}var D=null;if(A.every(function(E){return $type(E)=="number"})){var B=new Date(A[0],A[1]+1,0).getDate();
if(A[2]>B){A[2]=B}D=new Date(A[0],A[1],A[2])}return(C.val==D)?null:D},format:function(A,E){var D="";if(A){var G=A.getDate();var J=this.props.days[A.getDay()];var B=A.getMonth()+1;var F=this.props.months[A.getMonth()];var I=A.getFullYear()+"";for(var C=0;C<E.length;C++){var H=E.charAt(C);switch(H){case"y":I=I.substr(2);
case"Y":D+=I;break;case"m":if(B<10){B="0"+B}case"n":D+=B;break;case"M":F=F.substr(0,3);case"F":D+=F;break;case"d":if(G<10){G="0"+G}case"j":D+=G;break;case"D":J=J.substr(0,3);case"l":D+=J;break;case"S":if(G%10==1&&G!="11"){D+="st"}else{if(G%10==2&&G!="12"){D+="nd"}else{if(G%10==3&&G!="13"){D+="rd"}else{D+="th"
}}}break;default:D+=H}}}return D},navigate:function(C,B,D){switch(B){case"m":if($type(C.bounds.months)=="array"){var A=C.bounds.months.indexOf(C.month)+D;if(A<0||A==C.bounds.months.length){if(this.props.navigation==1){this.navigate(C,"y",D)}A=(A<0)?C.bounds.months.length-1:0}C.month=C.bounds.months[A]
}else{var A=C.month+D;if(A<0||A==12){if(this.props.navigation==1){this.navigate(C,"y",D)}A=(A<0)?11:0}C.month=A}break;case"y":if($type(C.bounds.years)=="array"){var A=C.bounds.years.indexOf(C.year)+D;C.year=C.bounds.years[A]}else{C.year+=D}break}C.bounds=this.bounds(C);if($type(C.bounds.months)=="array"){var A=C.bounds.months.indexOf(C.month);
if(A<0){C.month=C.bounds.months[0]}}this.display(C)},options:function(A){A.els.each(function(B){if(B.getTag()=="select"&&B.format.test("^(d|j)$")){var C=this.value(A);if(!C){C=B.value.toInt()}B.empty();A.bounds.days.each(function(D){var E=new Element("option",{selected:(C==D),value:((B.format=="d"&&D<10)?"0"+D:D)}).appendText(D).injectInside(B)
},this)}},this)},sort:function(B,A){return B-A},toggle:function(B){document.removeEvent("mousedown",this.fn);B.button.blur();B.val=this.evaluate(B);if(B.visible){B.visible=false;B.button.removeClass(this.props.classes[8]);this.fx.start(1,0)}else{this.fn=function(G,F){var G=new Event(G);var E=G.target;
var D=false;while(E!=document.body&&E.nodeType==1){if(E==this.calendar){D=true}this.calendars.each(function(H){if(H.button==E||H.els.contains(E)){D=true}});if(D){G.stop();return false}else{E=E.parentNode}}this.toggle(F)}.create({"arguments":B,bind:this,event:true});document.addEvent("mousedown",this.fn);
this.calendars.each(function(D){if(D==B){D.visible=true;D.button.addClass(this.props.classes[8])}else{D.visible=false;D.button.removeClass(this.props.classes[8])}},this);var C=B.button.getCoordinates();var A=window.getSize().scrollSize;if(C.right+this.calendar.coord.width>A.x){C.right-=(C.right+this.calendar.coord.width-A.x)
}if(C.top+this.calendar.coord.height>A.y){C.top-=(C.top+this.calendar.coord.height-A.y)}this.calendar.setStyles({left:C.right+"px",top:C.top+"px"});this.display(B);this.fx.start(0,1)}},unformat:function(B,G){G=G.escapeRegExp();var I={d:"([0-9]{2})",j:"([0-9]{1,2})",D:"("+this.props.days.map(function(J){return J.substr(0,3)
}).join("|")+")",l:"("+this.props.days.join("|")+")",S:"(st|nd|rd|th)",F:"("+this.props.months.join("|")+")",m:"([0-9]{2})",M:"("+this.props.months.map(function(J){return J.substr(0,3)}).join("|")+")",n:"([0-9]{1,2})",Y:"([0-9]{4})",y:"([0-9]{2})"};var E=[];var F="";for(var C=0;C<G.length;C++){var H=G.charAt(C);
if(I[H]){E.push(H);F+=I[H]}else{F+=H}}var D=B.match("^"+F+"$");var A=new Array(3);if(D){D=D.slice(1);E.each(function(K,J){J=D[J];switch(K){case"y":J="19"+J;case"Y":A[0]=J.toInt();break;case"F":J=J.substr(0,3);case"M":J=this.props.months.map(function(L){return L.substr(0,3)}).indexOf(J)+1;case"m":case"n":A[1]=J.toInt()-1;
break;case"d":case"j":A[2]=J.toInt();break}},this)}return A},value:function(B){var A=null;if(B.val){if(B.year==B.val.getFullYear()&&B.month==B.val.getMonth()){A=B.val.getDate()}}return A}});