if(typeof Prototype=="undefined"){alert("CalendarDateSelect Error: Prototype could not be found. Please make sure that your application's layout includes prototype.js (.g. <%= javascript_include_tag :defaults %>) *before* it includes calendar_date_select.js (.g. <%= calendar_date_select_includes %>).")}if(Prototype.Version<"1.6"){alert("Prototype 1.6.0 is required.  If using earlier version of prototype, please use calendar_date_select version 1.8.3")}Element.addMethods({purgeChildren:function(a){$A(a.childNodes).each(function(c){$(c).remove()})},build:function(c,e,a,d){var f=Element.buildAndAppend(e,a,d);c.appendChild(f);return f}});Element.buildAndAppend=function(d,a,c){var f=$(document.createElement(d));$H(a).each(function(e){f[e.key]=e.value});if(c){f.setStyle(c)}return f};nil=null;Date.one_day=24*60*60*1000;Date.weekdays=$w("S M T W T F S");Date.first_day_of_week=0;Date.months=$w("January February March April May June July August September October November December");Date.padded2=function(a){var c=parseInt(a,10);if(a<10){c="0"+c}return c};Date.prototype.getPaddedMinutes=function(){return Date.padded2(this.getMinutes())};Date.prototype.getAMPMHour=function(){var a=this.getHours();return(a==0)?12:(a>12?a-12:a)};Date.prototype.getAMPM=function(){return(this.getHours()<12)?"AM":"PM"};Date.prototype.stripTime=function(){return new Date(this.getFullYear(),this.getMonth(),this.getDate())};Date.prototype.daysDistance=function(a){return Math.round((a-this)/Date.one_day)};Date.prototype.toFormattedString=function(c){var a,d;d=Date.months[this.getMonth()]+" "+this.getDate()+", "+this.getFullYear();if(c){a=this.getHours();d+=" "+this.getAMPMHour()+":"+this.getPaddedMinutes()+" "+this.getAMPM()}return d};Date.parseFormattedString=function(a){return new Date(a)};Math.floor_to_interval=function(c,a){return Math.floor(c/a)*a};window.f_height=function(){return([window.innerHeight?window.innerHeight:null,document.documentElement?document.documentElement.clientHeight:null,document.body?document.body.clientHeight:null].select(function(a){return a>0}).first()||0)};window.f_scrollTop=function(){return([window.pageYOffset?window.pageYOffset:null,document.documentElement?document.documentElement.scrollTop:null,document.body?document.body.scrollTop:null].select(function(a){return a>0}).first()||0)};_translations={OK:"OK",Now:"Now",Today:"Today",Clear:"Clear"};SelectBox=Class.create();SelectBox.prototype={initialize:function(e,c,d,a){this.element=$(e).build("select",d,a);this.populate(c)},populate:function(a){this.element.purgeChildren();var c=this;$A(a).each(function(d){if(typeof(d)!="object"){d=[d,d]}c.element.build("option",{value:d[1],innerHTML:d[0]})})},setValue:function(c){var d=this.element;var a=false;$R(0,d.options.length-1).each(function(e){if(d.options[e].value==c.toString()){d.selectedIndex=e;a=true}});return a},getValue:function(){return $F(this.element)}};CalendarDateSelect=Class.create();CalendarDateSelect.prototype={initialize:function(c,a){this.target_element=$(c);if(!this.target_element){alert("Target element "+c+" not found!");return false}if(this.target_element.tagName!="INPUT"){this.target_element=this.target_element.down("INPUT")}this.target_element.calendar_date_select=this;this.last_click_at=0;this.options=$H({embedded:false,popup:nil,time:false,buttons:true,clear_button:true,year_range:10,close_on_click:nil,minute_interval:5,popup_by:this.target_element,month_year:"dropdowns",onchange:this.target_element.onchange,valid_date_check:nil}).merge(a||{});this.use_time=this.options.get("time");this.parseDate();this.callback("before_show");this.initCalendarDiv();if(!this.options.get("embedded")){this.positionCalendarDiv();Event.observe(document,"mousedown",this.closeIfClickedOut_handler=this.closeIfClickedOut.bindAsEventListener(this));Event.observe(document,"keypress",this.keyPress_handler=this.keyPress.bindAsEventListener(this))}this.callback("after_show")},positionCalendarDiv:function(){var n=false;var j=this.calendar_div.cumulativeOffset(),l=j[0],i=j[1],d=this.calendar_div.getDimensions(),o=d.height,c=d.width;var m=window.f_scrollTop(),e=window.f_height();var p=$(this.options.get("popup_by")).cumulativeOffset(),g=p[1],q=p[0],h=$(this.options.get("popup_by")).getDimensions().height,k=g+h;if(((k+o)>(m+e))&&(k-o>m)){n=true}var a=q.toString()+"px",f=(n?(g-o):(g+h)).toString()+"px";this.calendar_div.style.left=a;this.calendar_div.style.top=f;this.calendar_div.setStyle({visibility:""});if(navigator.appName=="Microsoft Internet Explorer"){this.iframe=$(document.body).build("iframe",{src:"javascript:false",className:"ie6_blocker"},{left:a,top:f,height:o.toString()+"px",width:c.toString()+"px",border:"0px"})}},initCalendarDiv:function(){if(this.options.get("embedded")){var parent=this.target_element.parentNode;var style={}}else{var parent=document.body;var style={position:"absolute",visibility:"hidden",left:0,top:0}}this.calendar_div=$(parent).build("div",{className:"calendar_date_select"},style);var that=this;$w("top header body buttons footer bottom").each(function(name){eval("var "+name+"_div = that."+name+"_div = that.calendar_div.build('div', { className: 'cds_"+name+"' }, { clear: 'left'} ); ")});this.initHeaderDiv();this.initButtonsDiv();this.initCalendarGrid();this.updateFooter("&#160;");this.refresh();this.setUseTime(this.use_time)},initHeaderDiv:function(){var a=this.header_div;this.close_button=a.build("a",{innerHTML:"x",href:"#",onclick:function(){this.close();return false}.bindAsEventListener(this),className:"close"});this.next_month_button=a.build("a",{innerHTML:"&gt;",href:"#",onclick:function(){this.navMonth(this.date.getMonth()+1);return false}.bindAsEventListener(this),className:"next"});this.prev_month_button=a.build("a",{innerHTML:"&lt;",href:"#",onclick:function(){this.navMonth(this.date.getMonth()-1);return false}.bindAsEventListener(this),className:"prev"});if(this.options.get("month_year")=="dropdowns"){this.month_select=new SelectBox(a,$R(0,11).map(function(c){return[Date.months[c],c]}),{className:"month",onchange:function(){this.navMonth(this.month_select.getValue())}.bindAsEventListener(this)});this.year_select=new SelectBox(a,[],{className:"year",onchange:function(){this.navYear(this.year_select.getValue())}.bindAsEventListener(this)});this.populateYearRange()}else{this.month_year_label=a.build("span")}},initCalendarGrid:function(){var a=this.body_div;this.calendar_day_grid=[];var h=a.build("table",{cellPadding:"0px",cellSpacing:"0px",width:"100%"});var c=h.build("thead").build("tr");Date.weekdays.each(function(i){c.build("th",{innerHTML:i})});var f=h.build("tbody");var d=0,e;for(var g=0;g<42;g++){e=(g+Date.first_day_of_week)%7;if(g%7==0){days_row=f.build("tr",{className:"row_"+d++})}(this.calendar_day_grid[g]=days_row.build("td",{calendar_date_select:this,onmouseover:function(){this.calendar_date_select.dayHover(this)},onmouseout:function(){this.calendar_date_select.dayHoverOut(this)},onclick:function(){this.calendar_date_select.updateSelectedDate(this,true)},className:(e==0)||(e==6)?" weekend":""},{cursor:"pointer"})).build("div");this.calendar_day_grid[g]}},initButtonsDiv:function(){var e=this.buttons_div;if(this.options.get("time")){var c=$A(this.options.get("time")=="mixed"?[[" - ",""]]:[]);e.build("span",{innerHTML:"@",className:"at_sign"});var a=new Date();this.hour_select=new SelectBox(e,c.concat($R(0,23).map(function(f){a.setHours(f);return $A([a.getAMPMHour()+" "+a.getAMPM(),f])})),{calendar_date_select:this,onchange:function(){this.calendar_date_select.updateSelectedDate({hour:this.value})},className:"hour"});e.build("span",{innerHTML:":",className:"seperator"});var d=this;this.minute_select=new SelectBox(e,c.concat($R(0,59).select(function(f){return(f%d.options.get("minute_interval")==0)}).map(function(f){return $A([Date.padded2(f),f])})),{calendar_date_select:this,onchange:function(){this.calendar_date_select.updateSelectedDate({minute:this.value})},className:"minute"})}else{if(!this.options.get("buttons")){e.remove()}}if(this.options.get("buttons")){e.build("span",{innerHTML:"&#160;"});if(this.options.get("time")=="mixed"||!this.options.get("time")){b=e.build("a",{innerHTML:_translations.Today,href:"#",onclick:function(){this.today(false);return false}.bindAsEventListener(this)})}if(this.options.get("time")=="mixed"){e.build("span",{innerHTML:"&#160;|&#160;",className:"button_seperator"})}if(this.options.get("time")){b=e.build("a",{innerHTML:_translations.Now,href:"#",onclick:function(){this.today(true);return false}.bindAsEventListener(this)})}if(!this.options.get("embedded")&&!this.closeOnClick()){e.build("span",{innerHTML:"&#160;|&#160;",className:"button_seperator"});e.build("a",{innerHTML:_translations.OK,href:"#",onclick:function(){this.close();return false}.bindAsEventListener(this)})}if(this.options.get("clear_button")){e.build("span",{innerHTML:"&#160;|&#160;",className:"button_seperator"});e.build("a",{innerHTML:_translations.Clear,href:"#",onclick:function(){this.clearDate();if(!this.options.get("embedded")){this.close()}return false}.bindAsEventListener(this)})}}},refresh:function(){this.refreshMonthYear();this.refreshCalendarGrid();this.setSelectedClass();this.updateFooter()},refreshCalendarGrid:function(){this.beginning_date=new Date(this.date).stripTime();this.beginning_date.setDate(1);this.beginning_date.setHours(12);var c=this.beginning_date.getDay();if(c<3){c+=7}this.beginning_date.setDate(1-c+Date.first_day_of_week);var e=new Date(this.beginning_date);var a=new Date().stripTime();var d=this.date.getMonth();vdc=this.options.get("valid_date_check");for(var f=0;f<42;f++){day=e.getDate();month=e.getMonth();cell=this.calendar_day_grid[f];Element.remove(cell.childNodes[0]);div=cell.build("div",{innerHTML:day});if(month!=d){div.className="other"}cell.day=day;cell.month=month;cell.year=e.getFullYear();if(vdc){if(vdc(e.stripTime())){cell.removeClassName("disabled")}else{cell.addClassName("disabled")}}e.setDate(day+1)}if(this.today_cell){this.today_cell.removeClassName("today")}if($R(0,41).include(days_until=this.beginning_date.stripTime().daysDistance(a))){this.today_cell=this.calendar_day_grid[days_until];this.today_cell.addClassName("today")}},refreshMonthYear:function(){var a=this.date.getMonth();var d=this.date.getFullYear();if(this.options.get("month_year")=="dropdowns"){this.month_select.setValue(a,false);var c=this.year_select.element;if(this.flexibleYearRange()&&(!(this.year_select.setValue(d,false))||c.selectedIndex<=1||c.selectedIndex>=c.options.length-2)){this.populateYearRange()}this.year_select.setValue(d)}else{this.month_year_label.update(Date.months[a]+" "+d.toString())}},populateYearRange:function(){this.year_select.populate(this.yearRange().toArray())},yearRange:function(){if(!this.flexibleYearRange()){return $R(this.options.get("year_range")[0],this.options.get("year_range")[1])}var a=this.date.getFullYear();return $R(a-this.options.get("year_range"),a+this.options.get("year_range"))},flexibleYearRange:function(){return(typeof(this.options.get("year_range"))=="number")},validYear:function(a){if(this.flexibleYearRange()){return true}else{return this.yearRange().include(a)}},dayHover:function(a){var c=new Date(this.selected_date);c.setYear(a.year);c.setMonth(a.month);c.setDate(a.day);this.updateFooter(c.toFormattedString(this.use_time))},dayHoverOut:function(a){this.updateFooter()},clearSelectedClass:function(){if(this.selected_cell){this.selected_cell.removeClassName("selected")}},setSelectedClass:function(){if(!this.selection_made){return}this.clearSelectedClass();if($R(0,42).include(days_until=this.beginning_date.stripTime().daysDistance(this.selected_date.stripTime()))){this.selected_cell=this.calendar_day_grid[days_until];this.selected_cell.addClassName("selected")}},reparse:function(){this.parseDate();this.refresh()},dateString:function(){return(this.selection_made)?this.selected_date.toFormattedString(this.use_time):"&#160;"},parseDate:function(){var a=$F(this.target_element).strip();this.selection_made=(a!="");this.date=a==""?NaN:Date.parseFormattedString(this.options.get("date")||a);if(isNaN(this.date)){this.date=new Date()}if(!this.validYear(this.date.getFullYear())){this.date.setYear((this.date.getFullYear()<this.yearRange().start)?this.yearRange().start:this.yearRange().end)}this.selected_date=new Date(this.date);this.use_time=/[0-9]:[0-9]{2}/.exec(a)?true:false;this.date.setDate(1)},updateFooter:function(a){if(!a){a=this.dateString()}this.footer_div.purgeChildren();this.footer_div.build("span",{innerHTML:a})},clearDate:function(){if((this.target_element.disabled||this.target_element.readOnly)&&this.options.get("popup")!="force"){return false}var a=this.target_element.value;this.target_element.value="";this.clearSelectedClass();this.updateFooter("&#160;");if(a!=this.target_element.value){this.callback("onchange")}},updateSelectedDate:function(d,c){var g=$H(d);if((this.target_element.disabled||this.target_element.readOnly)&&this.options.get("popup")!="force"){return false}if(g.get("day")){var f=this.selected_date,e=this.options.get("valid_date_check");for(var a=0;a<=3;a++){f.setDate(g.get("day"))}f.setYear(g.get("year"));f.setMonth(g.get("month"));if(e&&!e(f.stripTime())){return false}this.selected_date=f;this.selection_made=true}if(!isNaN(g.get("hour"))){this.selected_date.setHours(g.get("hour"))}if(!isNaN(g.get("minute"))){this.selected_date.setMinutes(Math.floor_to_interval(g.get("minute"),this.options.get("minute_interval")))}if(g.get("hour")===""||g.get("minute")===""){this.setUseTime(false)}else{if(!isNaN(g.get("hour"))||!isNaN(g.get("minute"))){this.setUseTime(true)}}this.updateFooter();this.setSelectedClass();if(this.selection_made){this.updateValue()}if(this.closeOnClick()){this.close()}if(c&&!this.options.get("embedded")){if((new Date()-this.last_click_at)<333){this.close()}this.last_click_at=new Date()}},closeOnClick:function(){if(this.options.get("embedded")){return false}if(this.options.get("close_on_click")===nil){return(this.options.get("time"))?false:true}else{return(this.options.get("close_on_click"))}},navMonth:function(a){(target_date=new Date(this.date)).setMonth(a);return(this.navTo(target_date))},navYear:function(a){(target_date=new Date(this.date)).setYear(a);return(this.navTo(target_date))},navTo:function(a){if(!this.validYear(a.getFullYear())){return false}this.date=a;this.date.setDate(1);this.refresh();this.callback("after_navigate",this.date);return true},setUseTime:function(c){this.use_time=this.options.get("time")&&(this.options.get("time")=="mixed"?c:true);if(this.use_time&&this.selected_date){var d=Math.floor_to_interval(this.selected_date.getMinutes(),this.options.get("minute_interval"));var a=this.selected_date.getHours();this.hour_select.setValue(a);this.minute_select.setValue(d)}else{if(this.options.get("time")=="mixed"){this.hour_select.setValue("");this.minute_select.setValue("")}}},updateValue:function(){var a=this.target_element.value;this.target_element.value=this.dateString();if(a!=this.target_element.value){this.callback("onchange")}},today:function(a){var e=new Date();this.date=new Date();var c=$H({day:e.getDate(),month:e.getMonth(),year:e.getFullYear(),hour:e.getHours(),minute:e.getMinutes()});if(!a){c=c.merge({hour:"",minute:""})}this.updateSelectedDate(c,true);this.refresh()},close:function(){if(this.closed){return false}this.callback("before_close");this.target_element.calendar_date_select=nil;Event.stopObserving(document,"mousedown",this.closeIfClickedOut_handler);Event.stopObserving(document,"keypress",this.keyPress_handler);this.calendar_div.remove();this.closed=true;if(this.iframe){this.iframe.remove()}if(this.target_element.type!="hidden"&&!this.target_element.disabled){this.target_element.focus()}this.callback("after_close")},closeIfClickedOut:function(a){if(!$(Event.element(a)).descendantOf(this.calendar_div)){this.close()}},keyPress:function(a){if(a.keyCode==Event.KEY_ESC){this.close()}},callback:function(a,c){if(this.options.get(a)){this.options.get(a).bind(this.target_element)(c)}}};
