(function($){$.widget("ui.datetime_selector",{options:{version:"1.1",type:"datetime",date:null,time:null,ampm:null,disabled:"false",passThruId:null,mode:"future_date",showlabel:true,value:null,required:false,show_now_option:false},_create:function(){var self=this;var options=self.options;var element=$(self.element[0]);var type=options.type;self.tableObj;self.dateObj;self.timeObj;self.ampmObj;self.jsTimeObj;self.mode=self.options.mode;self.tableObj=$('<table class="dt-table" cellspacing="0" cellpadding="0"><tr class="dt-row"></tr></table>');
self.nowSelector=null;if(self.options.show_now_option){self.nowSelector=$('<div style="padding:3px;"><select id="mode" name="mode" style="width: 100px; min-width: 100px;"><option value="now" '+(self.mode=="now"?"selected":"")+'>Now</option><option value="future_date" '+(self.mode=="future_date"?"selected":"")+">Future Date</option></select></div>");element.append(self.nowSelector);self.nowSelector.find("#mode").bind("change",function(){var selected=$(this).val();if(selected=="now"){self.tableObj.hide();
self.mode="now"}else{self.tableObj.show();self.mode="future_date"}})}if(type=="datetime"||type=="date"){self.dateObj=$('<td class="dt-date-cell"><nobr>'+(options.showlabel?"Date:":"")+' <input type="text" '+(options.disabled=="false"?"":'disabled="true"')+' class="date" value="'+(options.date==null?"":options.date)+'" size="10" /></nobr></td>');self.dateObj.find(".date").datepicker({changeYear:true});self.dateObj.find(".date").bind("change",function(){self.update()});self.tableObj.find(".dt-row").append(self.dateObj)}if(type==
"datetime"||type=="time"){self.timeObj=$('<td class="dt-time-cell"><nobr>Time: <input type="text" '+(options.disabled=="false"?"":'disabled="true"')+' class="time" value="'+(options.time==null?"":options.time)+'" size="5" /></nobr></td>');self.ampmObj=$('<select class="ampm-selector" style="min-width: 0; width: 50px;" size="1" '+(options.disabled=="false"?"":'disabled="true"')+'><option value="AM" '+(options.ampm=="AM"?'selected="selected"':"")+'>AM</option><option value="PM" '+(options.ampm=="PM"?
'selected="selected"':"")+">PM</option></select>");self.timeObj.find(".time").bind("change",function(){self.update()});self.ampmObj.bind("change",function(){self.update()});self.timeObj.append(self.ampmObj);self.tableObj.find(".dt-row").append(self.timeObj)}self.update();element.append(self.tableObj);element.append('<div style="clear: both;"></div>');if(self.options.show_now_option)self.nowSelector.find("#mode").trigger("change")},update:function(){var self=this;var options=self.options;var type=
options.type;var dateItems;var timeItems;var hoursTxt;var minutes;var year;var month;var day;var hours;self.options.value=null;if(type=="datetime"||type=="date")if(self.dateObj.find(".date").val()!=null&&self.dateObj.find(".date").val()!=""&&self.dateObj.find(".date").val()!=undefined){dateItems=self.dateObj.find(".date").val().split("/");month=parseInt(dateItems[0],10);day=parseInt(dateItems[1],10);year=parseInt(dateItems[2],10)}if(type=="datetime")if(self.timeObj.find(".time").val()!=null&&self.timeObj.find(".time").val()!=
""&&self.timeObj.find(".time").val()!=undefined){timeItems=self.timeObj.find(".time").val().split(":");hours=parseInt(timeItems[0],10);hoursTxt="";minutes=timeItems[1];if(self.ampmObj.val()=="AM"){if(hours==12)hours=0}else if(hours<12)hours=hours+12;if(hours<10)hoursTxt="0"+hours;else hoursTxt=hours;hours=parseInt(hoursTxt,10);minutes=parseInt(timeItems[1],10)}if(isNaN(year)||isNaN(month)||isNaN(day)||isNaN(hours)||isNaN(minutes))self.jsTimeObj=null;else self.jsTimeObj=new Date(year,month-1,day,hours,
minutes,0,0);if(dateItems!==undefined)if(dateItems.length>1)if(type=="datetime"){self._setOption("date",dateItems[2]+"-"+dateItems[0]+"-"+dateItems[1]);self._setOption("time",hoursTxt+":"+minutes+":00");self._setOption("value",dateItems[2]+"-"+dateItems[0]+"-"+dateItems[1]+" "+hoursTxt+":"+minutes+":00")}else if(type=="date"){self._setOption("date",dateItems[2]+"-"+dateItems[0]+"-"+dateItems[1]);self._setOption("value",dateItems[2]+"-"+dateItems[0]+"-"+dateItems[1]+" 01:00:00")}else if(type=="time"){self._setOption("time",
hoursTxt+":"+minutes+":00");self._setOption("value",hoursTxt+":"+minutes+":00")}if(options.passThruId!=null&&options.value!==undefined)$(options.passThruId).val(options.value);else $(options.passThruId).val("")},getDateTimeObject:function(){var self=this;if(self.options.type=="datetime")return{mode:self.mode,viewDate:self.dateObj.find(".date").val(),viewTime:self.timeObj.find(".time").val(),viewAmPm:self.ampmObj.val()};else return{mode:self.mode,viewDate:self.dateObj.find(".date").val()}},validate:function(){var self=
this;if(self.options.show_now_option)if(self.element.find("#mode").val()=="now")return true;if(!self.options.required){var dateval=self.dateObj.find(".date").val();var timeval=self.timeObj.find(".time").val();if(self.options.type=="datetime"&&(dateval==""||dateval==null||dateval==undefined)&&(timeval==""||timeval==null||timeval==undefined))return true;else if(self.options.type=="date"&&(dateval==""||dateval==null||dateval==undefined))return true}return self._validateDate()&self._validateTime()},_validateDate:function(){var self=
this;var valid=true;var dateItems=self.dateObj.find(".date").val().split("/");if(dateItems.length!=3){valid=false;self._setDateError("Please enter a valid date")}else{var month=parseInt(dateItems[0],10);var date=parseInt(dateItems[1],10);var year=parseInt(dateItems[2],10);if(isNaN(month)||isNaN(date)||isNaN(year)){self._setDateError("Please enter a valid date");valid=false}else if(month<1||month>12){self._setDateError("Please enter a valid date");valid=false}else if(month<1||month>31){self._setDateError("Please enter a valid date");
valid=false}else if(date==31&&(month==4||month==6||month==9||month==11)){self._setDateError("Please enter a valid date");valid=false}else if(date>=30&&month==2){self._setDateError("Please enter a valid date");valid=false}else if(dateItems[2].length!=4){self._setDateError("Please enter a valid date");valid=false}else if(month==2&&date==29&&!(year%4==0&&(year%100!=0||year%400==0))){self._setDateError("Please enter a valid date");valid=false}}return valid},_validateTime:function(){var self=this;var valid=
true;if(self.options.type=="datetime"){timeItems=self.timeObj.find(".time").val().split(":");if(timeItems.length!=2){self._setTimeError("Please enter a valid time");valid=false}var hours=parseInt(timeItems[0],10);var minutes=parseInt(timeItems[1],10);if(isNaN(hours)){self._setTimeError("Please enter a valid time");valid=false}else if(hours<1||hours>12){self._setTimeError("Please enter a valid time");valid=false}else if(isNaN(hours)){self._setTimeError("Please enter a valid time");valid=false}else if(minutes<
0||minutes>59){self._setTimeError("Please enter a valid time");valid=false}}return valid},_setDateError:function(message){var self=this;var dateInput=self.dateObj.find(".date");if(!dateInput.hasClass("error")){dateInput.addClass("error");dateInput.after('<div class="error-msg">'+message+"</div>");var removeError=function $removeError$(){dateInput.removeClass("error");self.dateObj.find(".error-msg").remove();dateInput.unbind("focus",removeError)};dateInput.bind("focus",removeError)}},_setTimeError:function(message){var self=
this;var timeInput=self.timeObj.find(".time");if(!timeInput.hasClass("error")){timeInput.addClass("error");self.timeObj.find(".ampm-selector").after('<div class="error-msg">'+message+"</div>");var removeError=function $removeError$(){if(self._validateTime()){timeInput.removeClass("error");self.timeObj.find(".error-msg").remove();timeInput.unbind("blur",removeError)}};timeInput.bind("blur",removeError);if(!self.options.required){var removeErrorDate=function $removeErrorDate$(){timeInput.removeClass("error");
self.timeObj.find(".error-msg").remove();self.dateObj.find(".date").unbind("focus",removeErrorDate)};self.dateObj.find(".date").bind("focus",removeErrorDate)}}},getDateGmt:function(){var self=this;if(self.jsTimeObj!=null&&self.jsTimeObj!=undefined)return self.jsTimeObj.getTime();else return null},getValue:function(){if(self.mode!="now")return this.options.value;else return"now"}})})(jQuery);
