(function(n,t,i){"use strict";function f(t,i){return this.element=t,this.$element=n(t),this.config=n.extend({},u,i),this.internals={objectRefs:{}},this.init(),this}var r="dateDropdowns",u={defaultDate:null,defaultDateFormat:"yyyy-mm-dd",displayFormat:"dmy",submitFormat:"yyyy-mm-dd",minAge:0,maxAge:120,minYear:null,maxYear:null,submitFieldName:"date",wrapperClass:"date-dropdowns",dropdownClass:null,daySuffixes:!1,monthSuffixes:!0,monthFormat:"long",required:!1,dayLabel:"Day",monthLabel:"Month",yearLabel:"Year",monthLongValues:["January","February","March","April","May","June","July","August","September","October","November","December"],monthShortValues:["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],initialDayMonthYearValues:["Day","Month","Year"],daySuffixValues:["st","nd","rd","th"]};n.extend(f.prototype,{init:function(){this.checkForDuplicateElement();this.setInternalVariables();this.setupMarkup();this.buildDropdowns();this.attachDropdowns();this.bindChangeEvent();this.config.defaultDate&&this.populateDefaultDate()},checkForDuplicateElement:function(){return n('input[name="'+this.config.submitFieldName+'"]').length?(n.error("Duplicate element found"),!1):!0},setInternalVariables:function(){var n=new Date;this.internals.currentDay=n.getDate();this.internals.currentMonth=n.getMonth()+1;this.internals.currentYear=n.getFullYear()},setupMarkup:function(){var i,t,r,f;return this.element.tagName.toLowerCase()==="input"?(this.config.defaultDate||(this.config.defaultDate=this.element.value),t=this.$element.attr("type","hidden").wrap('<div class="'+this.config.wrapperClass+'"><\/div>'),r=this.config.submitFieldName!==u.submitFieldName,f=this.element.hasAttribute("name"),f||r?r&&this.$element.attr("name",this.config.submitFieldName):this.$element.attr("name",u.submitFieldName),i=this.$element.parent()):(t=n("<input/>",{type:"hidden",name:this.config.submitFieldName}),this.$element.append(t).addClass(this.config.wrapperClass),i=this.$element),this.internals.objectRefs.pluginWrapper=i,this.internals.objectRefs.hiddenField=t,!0},buildDropdowns:function(){var n,t,i;return f.message={day:this.config.initialDayMonthYearValues[0],month:this.config.initialDayMonthYearValues[1],year:this.config.initialDayMonthYearValues[2]},n=this.buildDayDropdown(),this.internals.objectRefs.dayDropdown=n,t=this.buildMonthDropdown(),this.internals.objectRefs.monthDropdown=t,i=this.buildYearDropdown(),this.internals.objectRefs.yearDropdown=i,!0},attachDropdowns:function(){var n=this.internals.objectRefs.pluginWrapper,t=this.internals.objectRefs.dayDropdown,i=this.internals.objectRefs.monthDropdown,r=this.internals.objectRefs.yearDropdown;switch(this.config.displayFormat){case"mdy":n.append(i,t,r);break;case"ymd":n.append(r,i,t);break;case"dmy":default:n.append(t,i,r)}return!0},bindChangeEvent:function(){var i=this.internals.objectRefs.dayDropdown,r=this.internals.objectRefs.monthDropdown,u=this.internals.objectRefs.yearDropdown,t=this,n=this.internals.objectRefs;n.pluginWrapper.on("change","select",function(){var f=i.val(),e=r.val(),o=u.val(),s,h;if(s=t.checkDate(f,e,o),s)return n.dayDropdown.addClass("invalid"),!1;"00"!==n.dayDropdown.val()&&n.dayDropdown.removeClass("invalid");n.hiddenField.val("");s||f*e*o==0||(h=t.formatSubmitDate(f,e,o),n.hiddenField.val(h));n.hiddenField.change()})},populateDefaultDate:function(){var u=this.config.defaultDate,n=[],t="",i="",r="";switch(this.config.defaultDateFormat){case"yyyy-mm-dd":default:n=u.split("-");t=n[2];i=n[1];r=n[0];break;case"dd/mm/yyyy":n=u.split("/");t=n[0];i=n[1];r=n[2];break;case"mm/dd/yyyy":n=u.split("/");t=n[1];i=n[0];r=n[2];break;case"unix":n=new Date;n.setTime(u*1e3);t=n.getDate()+"";i=n.getMonth()+1+"";r=n.getFullYear();t.length<2&&(t="0"+t);i.length<2&&(i="0"+i)}return this.internals.objectRefs.dayDropdown.val(t),this.internals.objectRefs.monthDropdown.val(i),this.internals.objectRefs.yearDropdown.val(r),this.internals.objectRefs.hiddenField.val(u),!0===this.checkDate(t,i,r)&&this.internals.objectRefs.dayDropdown.addClass("invalid"),!0},buildBaseDropdown:function(t){var i=t;return this.config.dropdownClass&&(i+=" "+this.config.dropdownClass),n("<select><\/select>",{"class":i,name:this.config.submitFieldName+"_["+t+"]",required:this.config.required})},buildDayDropdown:function(){var u,f=this.buildBaseDropdown("day"),n=i.createElement("option"),t,r;for(n.setAttribute("value",""),n.appendChild(i.createTextNode(this.config.dayLabel)),f.append(n),t=1;t<10;t++)u=this.config.daySuffixes?t+this.getSuffix(t):"0"+t,n=i.createElement("option"),n.setAttribute("value","0"+t),n.appendChild(i.createTextNode(u)),f.append(n);for(r=10;r<=31;r++)u=r,this.config.daySuffixes&&(u=r+this.getSuffix(r)),n=i.createElement("option"),n.setAttribute("value",r),n.appendChild(i.createTextNode(u)),f.append(n);return f},buildMonthDropdown:function(){var u=this.buildBaseDropdown("month"),t=i.createElement("option"),n,r;for(t.setAttribute("value",""),t.appendChild(i.createTextNode(this.config.monthLabel)),u.append(t),n=1;n<=12;n++){switch(this.config.monthFormat){case"short":r=this.config.monthShortValues[n-1];break;case"long":r=this.config.monthLongValues[n-1];break;case"numeric":r=n;this.config.monthSuffixes&&(r+=this.getSuffix(n))}n<10&&(n="0"+n);t=i.createElement("option");t.setAttribute("value",n);t.appendChild(i.createTextNode(r));u.append(t)}return u},buildYearDropdown:function(){var r=this.config.minYear,u=this.config.maxYear,f=this.buildBaseDropdown("year"),n=i.createElement("option"),t;for(n.setAttribute("value",""),n.appendChild(i.createTextNode(this.config.yearLabel)),f.append(n),r||(r=this.internals.currentYear-(this.config.maxAge+1)),u||(u=this.internals.currentYear-this.config.minAge),t=u;t>=r;t--)n=i.createElement("option"),n.setAttribute("value",t),n.appendChild(i.createTextNode(t)),f.append(n);return f},getSuffix:function(n){var t="",r=this.config.daySuffixValues[0],u=this.config.daySuffixValues[1],f=this.config.daySuffixValues[2],i=this.config.daySuffixValues[3];switch(n%10){case 1:t=n%100==11?i:r;break;case 2:t=n%100==12?i:u;break;case 3:t=n%100==13?i:f;break;default:t="th"}return t},checkDate:function(n,t,i){var r,u,f;return t!=="00"&&(u=new Date(i,t,0).getDate(),f=parseInt(n,10),r=this.updateDayOptions(u,f),r&&this.internals.objectRefs.hiddenField.val("")),r},updateDayOptions:function(n,t){var r=parseInt(this.internals.objectRefs.dayDropdown.children(":last").val(),10),f="",e="",o=!1,u;if(r>n){while(r>n)this.internals.objectRefs.dayDropdown.children(":last").remove(),r--;t>n&&(o=!0)}else if(r<n)while(r<n)f=++r,e=f,this.config.daySuffixes&&(e+=this.getSuffix(r)),u=i.createElement("option"),u.setAttribute("value",f),u.appendChild(i.createTextNode(e)),this.internals.objectRefs.dayDropdown.append(u);return o},formatSubmitDate:function(n,t,i){var u,r;switch(this.config.submitFormat){case"unix":r=new Date;r.setDate(n);r.setMonth(t-1);r.setYear(i);u=Math.round(r.getTime()/1e3);break;default:u=this.config.submitFormat.replace("dd",n).replace("mm",t).replace("yyyy",i)}return u},destroy:function(){var i=this.config.wrapperClass,n,t;this.$element.hasClass(i)?this.$element.empty():(n=this.$element.parent(),t=n.find("select"),this.$element.unwrap(),t.remove())}});n.fn[r]=function(t){return this.each(function(){if(typeof t=="string"){var u=Array.prototype.slice.call(arguments,1),i=n.data(this,"plugin_"+r);if(typeof i=="undefined")return n.error("Please initialize the plugin before calling this method."),!1;i[t].apply(i,u)}else n.data(this,"plugin_"+r)||n.data(this,"plugin_"+r,new f(this,t))}),this}})(jQuery,window,document)