/* Copyright (c) 1995-2008 Choice Hotels International, Inc. All Rights Reserved. */
Chcom={};Chcom.menu={};Chcom.menu.DateMenu=function(config){Chcom.menu.DateMenu.superclass.constructor.call(this,config);this.plain=true;this.dateItem=new Chcom.menu.DateItem(this,config);this.add(this.dateItem);this.relayEvents(this.dateItem,["select"]);this.on('beforeshow',function(){if(this.dateItem.picker){this.dateItem.picker.hideMonthPicker(true)}},this)};Ext.extend(Chcom.menu.DateMenu,Ext.menu.Menu,{cls:'extjs x-date-menu',beforeDestroy:function(){this.dateItem.destroy()}});Chcom.menu.DateItem=function(parentDateMenu,config){Chcom.menu.DateItem.superclass.constructor.call(this,new Chcom.DatePicker(parentDateMenu,config),config);this.picker=this.component;this.addEvents('select');this.picker.on("render",function(picker){picker.getEl().swallowEvent("click");picker.container.addClass("x-menu-date-item")});this.picker.on("select",this.onSelect,this)};Ext.extend(Chcom.menu.DateItem,Ext.menu.Adapter,{onSelect:function(picker,date){this.fireEvent("select",this,date,picker);Chcom.menu.DateItem.superclass.handleClick.call(this)}});Chcom.DatePicker=function(parentDateMenu,config){Chcom.DatePicker.superclass.constructor.call(this,config);this.parentDateMenu=parentDateMenu};Ext.extend(Chcom.DatePicker,Ext.DatePicker,{lang:"en",allowMonthPicker:true,minDate:new Date().add(Date.YEAR,-30),maxDate:new Date().add(Date.YEAR,30),mode:"standalone",imageId:"",fromMonthSelectId:"",fromDaySelectId:"",fromYearSelectId:"",toMonthSelectId:"",toDaySelectId:"",toYearSelectId:"",message:"",initComponent:function(){Chcom.DatePicker.superclass.initComponent.call(this);Ext.get(this.imageId).on("click",function(){var date=new Date();var year=date.getFullYear();if((this.mode=="from")||(this.mode=="standalone")){var month=Ext.get(this.fromMonthSelectId).dom.selectedIndex-1;var day=Ext.get(this.fromDaySelectId).dom.selectedIndex;if((month>=0)&&(day>0)){if(this.fromYearSelectId!=""){if(Ext.get(this.fromYearSelectId).dom.selectedIndex>0){year=Ext.get(this.fromYearSelectId).dom.value}}else {if((month<date.getMonth())||((month==date.getMonth())&&(day<date.getDate()))){year++}}date=new Date(year,month,day)}if(!date.between(this.minDate.clearTime(false),this.maxDate.clearTime(false))){date=new Date()}}else if(this.mode=="to"){if(this.fromYearSelectId==""){var today=new Date();var fromMonth=Ext.get(this.fromMonthSelectId).dom.selectedIndex-1;var fromDay=Ext.get(this.fromDaySelectId).dom.selectedIndex;var fromYear=today.getFullYear();if((fromMonth>=0)&&(fromDay>0)){if((fromMonth<today.getMonth())||((fromMonth==today.getMonth())&&(fromDay<today.getDate()))){fromYear++}this.minDate=new Date(fromYear,fromMonth,fromDay).add(Date.DAY,1)}}var month=Ext.get(this.toMonthSelectId).dom.selectedIndex-1;var day=Ext.get(this.toDaySelectId).dom.selectedIndex;if((month>=0)&&(day>0)){if(this.toYearSelectId!=""){if(Ext.get(this.toYearSelectId).dom.selectedIndex>0){year=Ext.get(this.toYearSelectId).dom.value}}else {if((month<date.getMonth())||((month==date.getMonth())&&(day<date.getDate()))){year++}date=new Date(year,month,day)}}if(!date.between(this.minDate.clearTime(false),this.maxDate.clearTime(false))){date=new Date()}}this.setValue(date);this.parentDateMenu.show(this.imageId)},this);if((this.mode=="from")||(this.mode=="to")){this.on("select",function(datePicker,date){datePicker.adjustFromToDateSelects(date.getMonth()+1,date.getDate(),date.getFullYear())})}else if(this.mode=="standalone"){this.on("select",function(datePicker,date){datePicker.adjustStandaloneDateSelects(date.getMonth()+1,date.getDate(),date.getFullYear())})}},onRender:function(container,position){var m=['<table cellspacing="0"><tr>','<td><table cellspacing="0">','<tr><td class="x-date-left"><a href="#" title="',this.prevText,'">&#160;</a></td><td class="x-date-middle" align="center"></td><td class="x-date-right"></td></tr>','<tr><td colspan="3"><table class="x-date-inner" cellspacing="0"><thead><tr>'];var dn=this.dayNames;for(var i=0;i<7;i++){var d=this.startDay+i;if(d>6){d=d-7}m.push('<th><span class="x-chcom-date-date">',dn[d].substr(0,1),'</span></th>')}m[m.length]="</tr></thead><tbody><tr>";for(var i=0;i<42;i++){if(i%7==0&&i!=0){m[m.length]="</tr><tr>"}m[m.length]='<td><a href="#" hidefocus="on" class="x-date-date x-chcom-date-date" tabIndex="1"><em><span></span></em></a></td>'}m[m.length]='</tr></tbody></table></td></tr></table></td>';m.push('<td class="x-chcom-2nd-month-td"><table cellspacing="0">','<tr><td class="x-date-left"></td><td class="x-date-middle" align="center"></td><td class="x-date-right"><a href="#" title="',this.nextText,'">&#160;</a></td></tr>','<tr><td colspan="3"><table class="x-date-inner" cellspacing="0"><thead><tr>');var dn=this.dayNames;for(var i=0;i<7;i++){var d=this.startDay+i;if(d>6){d=d-7}m.push('<th><span class="x-chcom-date-date">',dn[d].substr(0,1),'</span></th>')}m[m.length]="</tr></thead><tbody><tr>";for(var i=0;i<42;i++){if(i%7==0&&i!=0){m[m.length]="</tr><tr>"}m[m.length]='<td><a href="#" hidefocus="on" class="x-date-date x-chcom-date-date" tabIndex="1"><em><span></span></em></a></td>'}m[m.length]='</tr></tbody></table></td></tr></table></td></tr>';if(this.message.length>0){m.push('<tr><td colspan="2"><table cellspacing="0" width="351px"><tr><td class="x-chcom-date-bottom">',this.message,'</td></tr></table></td></tr>')}m[m.length]='</table><div class="x-date-mp"></div>';var el=document.createElement("div");el.className="x-date-picker";el.innerHTML=m.join("");container.dom.insertBefore(el,position);this.el=Ext.get(el);this.eventEl=Ext.get(el.firstChild);new Ext.util.ClickRepeater(this.el.child("td.x-date-left a"),{handler:this.showPrevMonth,scope:this,preventDefault:true,stopDefault:true});new Ext.util.ClickRepeater(this.el.child("td.x-date-right a"),{handler:this.showNextMonth,scope:this,preventDefault:true,stopDefault:true});this.eventEl.on("mousewheel",this.handleMouseWheel,this);this.monthPicker=this.el.down('div.x-date-mp');this.monthPicker.enableDisplayMode('block');var kn=new Ext.KeyNav(this.eventEl,{"left":function(e){var tmp=e.ctrlKey?this.showPrevMonth():this.update(this.activeDate.add("d",-1))},"right":function(e){var tmp=e.ctrlKey?this.showNextMonth():this.update(this.activeDate.add("d",1))},"up":function(e){var tmp=e.ctrlKey?this.showNextYear():this.update(this.activeDate.add("d",-7))},"down":function(e){var tmp=e.ctrlKey?this.showPrevYear():this.update(this.activeDate.add("d",7))},"pageUp":function(e){this.showPrevMonth()},"pageDown":function(e){this.showNextMonth()},"enter":function(e){e.stopPropagation();return true},scope:this});this.eventEl.on("click",this.handleDateClick,this,{delegate:"a.x-date-date"});this.eventEl.addKeyListener(Ext.EventObject.SPACE,this.selectToday,this);this.el.unselectable();this.cells=this.el.select("table.x-date-inner tbody td");this.textNodes=this.el.query("table.x-date-inner tbody span");this.mbtn1=new Ext.Button({text:"&#160;",tooltip:this.monthYearText,renderTo:this.el.query("td.x-date-middle")[0]});if(this.allowMonthPicker){this.mbtn1.on('click',this.showMonthPicker,this);this.mbtn1.el.child(this.mbtn1.menuClassTarget).addClass("x-btn-with-menu")}else {this.mbtn1.el.child(this.mbtn1.menuClassTarget).addClass("x-chcom-btn-nohand")}this.mbtn2=new Ext.Button({text:"&#160;",tooltip:this.monthYearText,renderTo:this.el.query("td.x-date-middle")[1]});if(this.allowMonthPicker){this.mbtn2.on('click',this.showMonthPicker,this);this.mbtn2.el.child(this.mbtn2.menuClassTarget).addClass("x-btn-with-menu")}else {this.mbtn2.el.child(this.mbtn2.menuClassTarget).addClass("x-chcom-btn-nohand")}if(Ext.isIE){this.el.repaint()}this.update(this.value)},update:function(date){var vd=(this.activeDate)?this.activeDate:null;this.activeDate=date;if(vd&&this.el){var t=date.getTime();if(vd.getMonth()==date.getMonth()&&vd.getFullYear()==date.getFullYear()){this.cells.removeClass("x-date-selected");this.cells.each(function(c){if(c.dom.firstChild.dateValue==t){c.addClass("x-date-selected");setTimeout(function(){try{c.dom.firstChild.focus()}catch(e){}},50);return false}});if(this.mode!="to"){return }}}var setCellClass=function(cal,cell){cell.title="";var t=d.getTime();cell.firstChild.dateValue=t;if(t==today){cell.className+=" x-date-today";cell.title=cal.todayText}if(t==sel){cell.className+=" x-date-selected";setTimeout(function(){try{cell.firstChild.focus()}catch(e){}},50)}if(t<min){cell.className=" x-date-disabled";cell.title=cal.minText;return }if(t>max){cell.className=" x-date-disabled";cell.title=cal.maxText;return }if(ddays){if(ddays.indexOf(d.getDay())!=-1){cell.title=ddaysText;cell.className=" x-date-disabled"}}if(ddMatch&&format){var fvalue=d.dateFormat(format);if(ddMatch.test(fvalue)){cell.title=ddText.replace("%0",fvalue);cell.className=" x-date-disabled"}}};var today=new Date().clearTime().getTime();var min=this.minDate?this.minDate.clearTime(false):Number.NEGATIVE_INFINITY;var max=this.maxDate?this.maxDate.clearTime(false):Number.POSITIVE_INFINITY;var ddMatch=this.disabledDatesRE;var ddText=this.disabledDatesText;var ddays=this.disabledDays?this.disabledDays.join(""):false;var ddaysText=this.disabledDaysText;var format=this.format;var day=86400000;var cells=this.cells.elements;var textEls=this.textNodes;var i=0;var days=date.getDaysInMonth();var firstOfMonth=date.getFirstDateOfMonth();var startingPos=firstOfMonth.getDay()-this.startDay;if(startingPos<=this.startDay){startingPos+=7}var pm=date.add(Date.MONTH,-1);var prevStart=pm.getDaysInMonth()-startingPos;days+=startingPos;var d=(new Date(pm.getFullYear(),pm.getMonth(),prevStart)).clearTime();var sel=date.clearTime().getTime();i=0;for(;i<startingPos;i++){textEls[i].innerHTML=( ++prevStart);d.setDate(d.getDate()+1);cells[i].className="x-date-prevday";setCellClass(this,cells[i])}for(;i<days;i++){var intDay=i-startingPos+1;textEls[i].innerHTML=(intDay);d.setDate(d.getDate()+1);cells[i].className="x-date-active";setCellClass(this,cells[i])}var extraDays=0;for(;i<42;i++){textEls[i].innerHTML=( ++extraDays);d.setDate(d.getDate()+1);cells[i].className="x-chcom-date-hidden";setCellClass(this,cells[i])}this.mbtn1.setText(this.monthNames[date.getMonth()]+" "+date.getFullYear());date=date.add(Date.MONTH,1);days=date.getDaysInMonth();firstOfMonth=date.getFirstDateOfMonth();startingPos=firstOfMonth.getDay()-this.startDay;if(startingPos<=this.startDay){startingPos+=7}pm=date.add(Date.MONTH,-1);prevStart=pm.getDaysInMonth()-startingPos;startingPos+=42;days+=startingPos;d=(new Date(pm.getFullYear(),pm.getMonth(),prevStart)).clearTime();i=42;for(;i<startingPos;i++){textEls[i].innerHTML=( ++prevStart);d.setDate(d.getDate()+1);cells[i].className="x-chcom-date-hidden";setCellClass(this,cells[i])}for(;i<days;i++){var intDay=i-startingPos+1;textEls[i].innerHTML=(intDay);d.setDate(d.getDate()+1);cells[i].className="x-date-active";setCellClass(this,cells[i])}var extraDays=0;for(;i<84;i++){textEls[i].innerHTML=( ++extraDays);d.setDate(d.getDate()+1);cells[i].className="x-date-nextday";setCellClass(this,cells[i])}this.mbtn2.setText(this.monthNames[date.getMonth()]+" "+date.getFullYear());if(!this.internalRender){date=date.add(Date.MONTH,-1);var main=this.el.dom.firstChild;var w=main.offsetWidth;this.el.setWidth(w+this.el.getBorderWidth("lr"));Ext.fly(main).setWidth(w);this.internalRender=true;if(Ext.isOpera&&!this.secondPass){main.rows[0].cells[1].style.width=(w-(main.rows[0].cells[0].offsetWidth+(main.rows[0].cells[2])?main.rows[0].cells[2].offsetWidth:0))+"px";this.secondPass=true;this.update.defer(10,this,[date])}}},beforeDestroy:function(){this.mbtn1.destroy();this.mbtn2.destroy()},adjustFromToDateSelects:function(monthIndex,dayIndex,year){var today=new Date();if(this.mode=="from"){Ext.get(this.fromMonthSelectId).dom.selectedIndex=monthIndex;Ext.get(this.fromDaySelectId).dom.selectedIndex=dayIndex;if(this.fromYearSelectId.length>0){Ext.get(this.fromYearSelectId).dom.selectedIndex=(year-this.minDate.getFullYear()+1)}var fromMonth=monthIndex-1;var fromDay=dayIndex;var fromDate=new Date(year,fromMonth,fromDay);var toMonth=Ext.get(this.toMonthSelectId).dom.selectedIndex-1;var toDay=Ext.get(this.toDaySelectId).dom.selectedIndex;var toYear=year;var toDate=fromDate.add(Date.DAY,1);if(this.toYearSelectId.length>0){var toYearSelect=Ext.get(this.toYearSelectId);var toYearSelectIdx=toYearSelect.dom.selectedIndex;if(toYearSelectIdx>0){toYear=toYearSelect.dom.options[toYearSelectIdx].value}}else {if((toMonth>=0)&&(toDay>0)){if((toMonth<fromMonth)||((toMonth==fromMonth)&&(toDay<fromDay))){toYear++}}}if((toMonth>=0)&&(toDay>0)){toDate=new Date(toYear,toMonth,toDay)}if((!toDate.between(this.minDate.clearTime(false),this.maxDate.clearTime(false)))||(toDate<=fromDate)){toDate=fromDate.add(Date.DAY,1)}Ext.get(this.toMonthSelectId).dom.selectedIndex=toDate.getMonth()+1;Ext.get(this.toDaySelectId).dom.selectedIndex=toDate.getDate();if(this.toYearSelectId.length>0){Ext.get(this.toYearSelectId).dom.selectedIndex=(toDate.getFullYear()-this.minDate.getFullYear()+1)}}else if(this.mode=="to"){Ext.get(this.toMonthSelectId).dom.selectedIndex=monthIndex;Ext.get(this.toDaySelectId).dom.selectedIndex=dayIndex;if(this.toYearSelectId.length>0){Ext.get(this.toYearSelectId).dom.selectedIndex=(year-this.minDate.getFullYear()+1)}var toMonth=monthIndex-1;var toDay=dayIndex;var toDate=new Date(year,toMonth,toDay);var fromMonth=Ext.get(this.fromMonthSelectId).dom.selectedIndex-1;var fromDay=Ext.get(this.fromDaySelectId).dom.selectedIndex;var fromYear=year;var fromDate=toDate.add(Date.DAY,-1);if(this.fromYearSelectId.length>0){var fromYearSelect=Ext.get(this.fromYearSelectId);var fromYearSelectIdx=fromYearSelect.dom.selectedIndex;if(fromYearSelectIdx>0){fromYear=fromYearSelect.dom.options[fromYearSelectIdx].value}}else {if((fromMonth>=0)&&(fromDay>0)){if((fromMonth>toMonth)||((fromMonth==toMonth)&&(fromDay>=toDay))){fromYear++}}}if((fromMonth>=0)&&(fromDay>0)){fromDate=new Date(fromYear,fromMonth,fromDay)}Ext.get(this.fromMonthSelectId).dom.selectedIndex=fromDate.getMonth()+1;Ext.get(this.fromDaySelectId).dom.selectedIndex=fromDate.getDate();if(this.fromYearSelectId.length>0){Ext.get(this.fromYearSelectId).dom.selectedIndex=(fromDate.getFullYear()-this.minDate.getFullYear()+1)}}Chcom.menu.updateDaySelect(this.fromMonthSelectId,this.fromDaySelectId,this.fromYearSelectId);Chcom.menu.updateDaySelect(this.toMonthSelectId,this.toDaySelectId,this.toYearSelectId)},adjustStandaloneDateSelects:function(monthIndex,dayIndex,year){var today=new Date();Ext.get(this.fromMonthSelectId).dom.selectedIndex=monthIndex;Ext.get(this.fromDaySelectId).dom.selectedIndex=dayIndex;if(this.fromYearSelectId.length>0){Ext.get(this.fromYearSelectId).dom.selectedIndex=(year-this.minDate.getFullYear()+1)}Chcom.menu.updateDaySelect(this.fromMonthSelectId,this.fromDaySelectId,this.fromYearSelectId)}});Ext.reg('chcomdatepicker',Chcom.DatePicker);Chcom.menu.updateDaySelect=function(monthId,dayId,yearId){var month=Ext.get(monthId);var day=Ext.get(dayId);var year=Ext.get(yearId);if(!month||!day){return }var monthNum=month.dom.selectedIndex-1;if(monthNum<1){monthNum=0}var today=new Date();var yearNum=today.getFullYear();if(year){yearIdx=year.dom.selectedIndex;if(yearIdx>0){yearNum=year.dom.options[yearIdx].value}}else {if(monthNum<today.getMonth()){yearNum++}}var date=new Date(yearNum,monthNum,1);numDays=date.getDaysInMonth();var len=day.dom.options.length;if((len-1)<numDays){for(var i=len;i<(numDays+1);i++){var option=new Option(i,i);if(document.all){day.dom.add(option)}else {day.dom.add(option,null)}}}else if((len-1)>numDays){for(var i=(len-1);i>numDays;i--){day.dom.remove(i)}}};