var Qs_Form_Element_Date = { timeoutHandle : null, eventTimeout : 1000, message: new qs.Message({ 'eng': { }, 'uk': { } }), options: {}, elementIds: {}, init: function (id, displayId, options) { var element = document.getElementById(displayId); if (!element) { alert('Qs_Form_Element_Date. Can not find element with id = ' + displayId); return false; } if (!element.tagName || element.tagName != 'INPUT') { alert('Qs_Form_Element_Date. Element (id = ' + displayId + ') is not "input"'); return false; } if (element.type != 'text') { alert('Qs_Form_Element_Date. Element (id = ' + displayId + ') type is not "text"'); return false; } Qs_Form_Element_Date.setDisplayId(id, displayId); options.onChangeMonthYear = Qs_Form_Element_Date.onChangeMonthYear; $(element).datepicker(options); var dateFormat = Qs_Form_Element_Date.getElementOption(id, 'dateFormat'); var strFormat = dateFormat.replace(/y/g, 'yy'); $(element).attr('maxlength', strFormat.length); $(element).tooltip({ title: 'Date is in wrong format. Example: ' + $.datepicker.formatDate(dateFormat, new Date()), trigger: 'manual' }); $(element).keyup(Qs_Form_Element_Date.onKeyUp); $(element).change(Qs_Form_Element_Date.onChange); return true; }, getElementOptions: function (id) { var displayId = Qs_Form_Element_Date.getDisplayId(id); return $('#' + displayId).datepicker('option'); }, getElementOption: function (id, name) { var displayId = Qs_Form_Element_Date.getDisplayId(id); return $('#' + displayId).datepicker('option', name); }, setElementOption: function (id, name, value) { var displayId = Qs_Form_Element_Date.getDisplayId(id); $('#' + displayId).datepicker('option', name, value); }, setElementOptions: function (id, options) { for (var option in options) { Qs_Form_Element_Date.setOption(id, option, options[option]); } }, callMethod: function (id, method, value) { var displayId = Qs_Form_Element_Date.getDisplayId(id); return $('#' + displayId).datepicker(method, value); }, onKeyUp: function () { Qs_Form_Element_Date.textChanged(this); }, onChange: function () { Qs_Form_Element_Date.textChanged(this); }, textChanged: function (element) { clearTimeout(Qs_Form_Element_Date.timeoutHandle); Qs_Form_Element_Date.timeoutHandle = setTimeout( function(){Qs_Form_Element_Date.dateChanged(element);}, Qs_Form_Element_Date.eventTimeout ); }, dateChanged: function (element) { Qs_Form_Element_Date.normalizeDate(element); var id = $(element).attr('id'); var displayId = Qs_Form_Element_Date.getDisplayId(id); var value = $('#' + displayId).val(); try { if (Qs_Form_Element_Date.isValidDate(value)) { $(element).removeClass('error'); var formatted = $.datepicker.parseDate(Qs_Form_Element_Date.getElementOption(id, 'dateFormat'), value); Qs_Form_Element_Date.callMethod(id, 'setDate', formatted); Qs_Form_Element_Date.hideTip(element); } else { if (value != '') { $(element).addClass('error'); Qs_Form_Element_Date.showTip(element); } $('#' + id).val(value); } } catch (e) { $('#' + id).val(value); } }, showTip: function (element) { $(element).tooltip('show'); }, hideTip: function (element) { $(element).tooltip('hide'); }, setDisplayId: function (id, displayId) { Qs_Form_Element_Date.elementIds[id] = displayId; }, getDisplayId: function (id) { if (typeof Qs_Form_Element_Date.elementIds[id] != 'undefined') { return Qs_Form_Element_Date.elementIds[id]; } else { for (var key in Qs_Form_Element_Date.elementIds) { if (Qs_Form_Element_Date.elementIds[key] == id) { return id; } } } alert('Qs_Form_Element_Date. Can not find "displayId" for element with id = ' + id); return false; }, normalizeDate: function (element) { var id = Qs_Form_Element_Date.getDisplayId($(element).attr('id')); var format = Qs_Form_Element_Date.getElementOption(id, 'dateFormat').replace(/y/g, 'yy'); var delimiters = format.replace(/[mdy]/gi, ''); var delimiter = delimiters.charAt(0); for (var i = 0; i < element.value.length; i++) { if (format.charAt(i) == delimiter && element.value.charAt(i) != delimiter) { element.value = element.value.substring(0, i) + delimiter + element.value.substring(i); } } if (element.value.length > format.length) { element.value = element.value.substring(0, format.length); } }, isValidDate: function (txtDate) { var objDate; var mSeconds; if (txtDate.length != 10) { return false; } var day = txtDate.substring(3,5) - 0; var month = txtDate.substring(0,2) - 1; var year = txtDate.substring(6,10) - 0; if (txtDate.substring(2,3) != '/' || txtDate.substring(5,6) != '/' || year < 999 || year > 3000) { return false; } mSeconds = (new Date(year, month, day)).getTime(); objDate = new Date(); objDate.setTime(mSeconds); if (objDate.getFullYear() != year) return false; if (objDate.getMonth() != month) return false; if (objDate.getDate() != day) return false; return true; }, onChangeMonthYear: function (year, month, inst) { var day = null; if (1 <= inst.currentDay) { var daysInMonths = new Date(year, month, 0).getDate(); day = (inst.currentDay <= daysInMonths) ? inst.currentDay : daysInMonths; } else { day = new Date().getDate(); } var date = new Date(year, month - 1, day); var id = inst.input.attr('id'); $('#' + id).datepicker('setDate', date); } };