// CalendarDateSelect version 1.16.3 - a prototype based date picker
// Questions, comments, bugs? - see the project page: http://code.google.com/p/calendardateselect
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 %>)."),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(a){$(a).remove()})},build:function(a,b,c,d){var e=Element.buildAndAppend(b,c,d);return a.appendChild(e),e}}),Element.buildAndAppend=function(a,b,c){var d=$(document.createElement(a));return $H(b).each(function(a){d[a.key]=a.value}),c&&d.setStyle(c),d},nil=null,Date.one_day=864e5,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 b=parseInt(a,10);return a<10&&(b="0"+b),b},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(a){var b,c;return c=Date.months[this.getMonth()]+" "+this.getDate()+", "+this.getFullYear(),a&&(b=this.getHours(),c+=" "+this.getAMPMHour()+":"+this.getPaddedMinutes()+" "+this.getAMPM()),c},Date.parseFormattedString=function(a){return new Date(a)},Math.floor_to_interval=function(a,b){return Math.floor(a/b)*b},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(a,b,c,d){this.element=$(a).build("select",c,d),this.populate(b)},populate:function(a){this.element.purgeChildren();var b=this;$A(a).each(function(a){typeof a!="object"&&(a=[a,a]),b.element.build("option",{value:a[1],innerHTML:a[0]})})},setValue:function(a){var b=this.element,c=!1;return $R(0,b.options.length-1).each(function(d){b.options[d].value==a.toString()&&(b.selectedIndex=d,c=!0)}),c},getValue:function(){return $F(this.element)}},CalendarDateSelect=Class.create(),CalendarDateSelect.prototype={initialize:function(a,b){this.target_element=$(a);if(!this.target_element)return alert("Target element "+a+" not found!"),!1;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:!1,popup:nil,time:!1,buttons:!0,clear_button:!0,year_range:10,close_on_click:nil,minute_interval:5,popup_by:this.target_element,month_year:"dropdowns",onchange:function(a){return function(){if(a.dispatchEvent){var b=document.createEvent("HTMLEvents");b.initEvent("change",!0,!0),a.dispatchEvent(b)}else{var b=document.createEventObject();b.type="onChange",a.fireEvent("onChange",b)}}}(this.target_element),valid_date_check:nil}).merge(b||{}),this.use_time=this.options.get("time"),this.parseDate(),this.callback("before_show"),this.initCalendarDiv(),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 a=!1,b=this.calendar_div.cumulativeOffset(),c=b[0],d=b[1],e=this.calendar_div.getDimensions(),f=e.height,g=e.width,h=window.f_scrollTop(),i=window.f_height(),j=$(this.options.get("popup_by")).cumulativeOffset(),k=j[1],l=j[0],m=$(this.options.get("popup_by")).getDimensions().height,n=k+m;n+f>h+i&&n-f>h&&(a=!0);var o=l.toString()+"px",p=(a?k-f:k+m).toString()+"px";this.calendar_div.style.left=o,this.calendar_div.style.top=p,this.calendar_div.setStyle({visibility:""}),navigator.appName=="Microsoft Internet Explorer"&&(this.iframe=$(document.body).build("iframe",{src:"javascript:false",className:"ie6_blocker"},{left:o,top:p,height:f.toString()+"px",width:g.toString()+"px",border:"0px"}))},initCalendarDiv:function(){if(this.options.get("embedded"))var parent=this.target_element.parentNode,style={};else var parent=document.body,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(){return this.close(),!1}.bindAsEventListener(this),className:"close"}),this.next_month_button=a.build("a",{innerHTML:"&gt;",href:"#",onclick:function(){return this.navMonth(this.date.getMonth()+1),!1}.bindAsEventListener(this),className:"next"}),this.prev_month_button=a.build("a",{innerHTML:"&lt;",href:"#",onclick:function(){return this.navMonth(this.date.getMonth()-1),!1}.bindAsEventListener(this),className:"prev"}),this.options.get("month_year")=="dropdowns"?(this.month_select=new SelectBox(a,$R(0,11).map(function(a){return[Date.months[a],a]}),{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()):this.month_year_label=a.build("span")},initCalendarGrid:function(){var a=this.body_div;this.calendar_day_grid=[];var b=a.build("table",{cellPadding:"0px",cellSpacing:"0px",width:"100%"}),c=b.build("thead").build("tr");Date.weekdays.each(function(a){c.build("th",{innerHTML:a})});var d=b.build("tbody"),e=0,f;for(var g=0;g<42;g++)f=(g+Date.first_day_of_week)%7,g%7==0&&(days_row=d.build("tr",{className:"row_"+e++})),(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,!0)},className:f==0||f==6?" weekend":""},{cursor:"pointer"})).build("div"),this.calendar_day_grid[g]},initButtonsDiv:function(){var a=this.buttons_div;if(this.options.get("time")){var c=$A(this.options.get("time")=="mixed"?[[" - ",""]]:[]);a.build("span",{innerHTML:"@",className:"at_sign"});var d=new Date;this.hour_select=new SelectBox(a,c.concat($R(0,23).map(function(a){return d.setHours(a),$A([d.getAMPMHour()+" "+d.getAMPM(),a])})),{calendar_date_select:this,onchange:function(){this.calendar_date_select.updateSelectedDate({hour:this.value})},className:"hour"}),a.build("span",{innerHTML:":",className:"seperator"});var e=this;this.minute_select=new SelectBox(a,c.concat($R(0,59).select(function(a){return a%e.options.get("minute_interval")==0}).map(function(a){return $A([Date.padded2(a),a])})),{calendar_date_select:this,onchange:function(){this.calendar_date_select.updateSelectedDate({minute:this.value})},className:"minute"})}else this.options.get("buttons")||a.remove();if(this.options.get("buttons")){a.build("span",{innerHTML:"&#160;"});if(this.options.get("time")=="mixed"||!this.options.get("time"))b=a.build("a",{innerHTML:_translations.Today,href:"#",onclick:function(){return this.today(!1),!1}.bindAsEventListener(this)});this.options.get("time")=="mixed"&&a.build("span",{innerHTML:"&#160;|&#160;",className:"button_seperator"}),this.options.get("time")&&(b=a.build("a",{innerHTML:_translations.Now,href:"#",onclick:function(){return this.today(!0),!1}.bindAsEventListener(this)})),!this.options.get("embedded")&&!this.closeOnClick()&&(a.build("span",{innerHTML:"&#160;|&#160;",className:"button_seperator"}),a.build("a",{innerHTML:_translations.OK,href:"#",onclick:function(){return this.close(),!1}.bindAsEventListener(this)})),this.options.get("clear_button")&&(a.build("span",{innerHTML:"&#160;|&#160;",className:"button_seperator"}),a.build("a",{innerHTML:_translations.Clear,href:"#",onclick:function(){return this.clearDate(),this.options.get("embedded")||this.close(),!1}.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 a=this.beginning_date.getDay();a<3&&(a+=7),this.beginning_date.setDate(1-a+Date.first_day_of_week);var b=new Date(this.beginning_date),c=(new Date).stripTime(),d=this.date.getMonth();vdc=this.options.get("valid_date_check");for(var e=0;e<42;e++)day=b.getDate(),month=b.getMonth(),cell=this.calendar_day_grid[e],Element.remove(cell.childNodes[0]),div=cell.build("div",{innerHTML:day}),month!=d&&(div.className="other"),cell.day=day,cell.month=month,cell.year=b.getFullYear(),vdc&&(vdc(b.stripTime())?cell.removeClassName("disabled"):cell.addClassName("disabled")),b.setDate(day+1);this.today_cell&&this.today_cell.removeClassName("today"),$R(0,41).include(days_until=this.beginning_date.stripTime().daysDistance(c))&&(this.today_cell=this.calendar_day_grid[days_until],this.today_cell.addClassName("today"))},refreshMonthYear:function(){var a=this.date.getMonth(),b=this.date.getFullYear();if(this.options.get("month_year")=="dropdowns"){this.month_select.setValue(a,!1);var c=this.year_select.element;this.flexibleYearRange()&&(!this.year_select.setValue(b,!1)||c.selectedIndex<=1||c.selectedIndex>=c.options.length-2)&&this.populateYearRange(),this.year_select.setValue(b)}else this.month_year_label.update().insert(Date.months[a]+" "+b.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){return this.flexibleYearRange()?!0:this.yearRange().include(a)},dayHover:function(a){var b=new Date(this.selected_date);b.setFullYear(a.year,a.month,a.day),this.updateFooter(b.toFormattedString(this.use_time))},dayHoverOut:function(a){this.updateFooter()},clearSelectedClass:function(){this.selected_cell&&this.selected_cell.removeClassName("selected")},setSelectedClass:function(){if(!this.selection_made)return;this.clearSelectedClass(),$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(),b=this.options.get("default_time");this.selection_made=a!=""||b,this.date=a==""?NaN:Date.parseFormattedString(this.options.get("date")||a),isNaN(this.date)&&!b?this.date=new Date:isNaN(this.date)&&b&&(this.date=Object.prototype.toString.apply(b)==="[object Function]"?b():b),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)?!0:!1,this.date.setDate(1)},updateFooter:function(a){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!1;var a=this.target_element.value;this.target_element.value="",this.clearSelectedClass(),this.updateFooter("&#160;"),a!=this.target_element.value&&this.callback("onchange")},updateSelectedDate:function(a,b){var c=$H(a);if((this.target_element.disabled||this.target_element.readOnly)&&this.options.get("popup")!="force")return!1;if(c.get("day")){var d=this.selected_date,e=this.options.get("valid_date_check");d.setFullYear(c.get("year"),c.get("month"),c.get("day"));if(e&&!e(d.stripTime()))return!1;this.selected_date=d,this.selection_made=!0}isNaN(c.get("hour"))||this.selected_date.setHours(c.get("hour")),isNaN(c.get("minute"))||this.selected_date.setMinutes(Math.floor_to_interval(c.get("minute"),this.options.get("minute_interval"))),c.get("hour")===""||c.get("minute")===""?this.setUseTime(!1):(!isNaN(c.get("hour"))||!isNaN(c.get("minute")))&&this.setUseTime(!0),this.updateFooter(),this.setSelectedClass(),this.selection_made&&this.updateValue(),this.closeOnClick()&&this.close(),b&&!this.options.get("embedded")&&(new Date-this.last_click_at<333&&this.close(),this.last_click_at=new Date)},closeOnClick:function(){return this.options.get("embedded")?!1:this.options.get("close_on_click")===nil?this.options.get("time")?!1:!0:this.options.get("close_on_click")},navMonth:function(a){return(target_date=new Date(this.date)).setMonth(a),this.navTo(target_date)},navYear:function(a){return(target_date=new Date(this.date)).setYear(a),this.navTo(target_date)},navTo:function(a){return this.validYear(a.getFullYear())?(this.date=a,this.date.setDate(1),this.refresh(),this.callback("after_navigate",this.date),!0):!1},setUseTime:function(a){this.use_time=this.options.get("time")&&(this.options.get("time")=="mixed"?a:!0);if(this.use_time&&this.selected_date){var b=Math.floor_to_interval(this.selected_date.getMinutes(),this.options.get("minute_interval")),c=this.selected_date.getHours();this.hour_select.setValue(c),this.minute_select.setValue(b)}else 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(),a!=this.target_element.value&&this.callback("onchange")},today:function(a){var b=new Date;this.date=new Date;var c=$H({day:b.getDate(),month:b.getMonth(),year:b.getFullYear(),hour:b.getHours(),minute:b.getMinutes()});a||(c=c.merge({hour:"",minute:""})),this.updateSelectedDate(c,!0),this.refresh()},close:function(){if(this.closed)return!1;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=!0,this.iframe&&this.iframe.remove(),this.target_element.type!="hidden"&&!this.target_element.disabled&&this.target_element.focus(),this.callback("after_close")},closeIfClickedOut:function(a){$(Event.element(a)).descendantOf(this.calendar_div)||this.close()},keyPress:function(a){a.keyCode==Event.KEY_ESC&&this.close()},callback:function(a,b){this.options.get(a)&&this.options.get(a).bind(this.target_element)(b)}}
