[2704] | 1 | // initialize controls |
---|
| 2 | // buttonImageName: Directory and name of calendar picture |
---|
| 3 | // day, month, year: selectors of visible date controls |
---|
| 4 | // linked_date: selector of hidden linked dates control |
---|
| 5 | // checked_on_change: selector of control to change "checked" attribut |
---|
| 6 | // min_linked_date: selector of hidden linked date control witch give min value |
---|
| 7 | // max_linked_date: selector of hidden linked date control witch give max value |
---|
| 8 | function pwg_common_initialization_datepicker(buttonImageName, day, month, year, linked_date, checked_on_change, min_linked_date, max_linked_date) |
---|
| 9 | { |
---|
| 10 | // return formated date with control values |
---|
| 11 | function pwg_get_fmt_from_ctrls() |
---|
| 12 | { |
---|
| 13 | return $(year).val() + "-" + $(month).val() + "-" + $(day).val(); |
---|
| 14 | } |
---|
| 15 | |
---|
| 16 | // return if linked_date is valid date |
---|
| 17 | function is_valid_linked_value(linked_date_name) |
---|
| 18 | { |
---|
| 19 | array_date = $(linked_date_name).val().split('-'); |
---|
| 20 | return ( |
---|
| 21 | (array_date.length == 3) && |
---|
| 22 | (array_date[0] != "") && |
---|
| 23 | (array_date[1] != "") && (array_date[1] != "0") && |
---|
| 24 | (array_date[2] != "") && (array_date[2] != "0") |
---|
| 25 | ) |
---|
| 26 | } |
---|
| 27 | |
---|
| 28 | // Action on change date value |
---|
| 29 | function pwg_on_date_change() |
---|
| 30 | { |
---|
| 31 | pwg_check_date(); |
---|
| 32 | if (checked_on_change != null) |
---|
| 33 | { |
---|
| 34 | $(checked_on_change).attr("checked", "true"); |
---|
| 35 | } |
---|
| 36 | } |
---|
| 37 | |
---|
| 38 | // In order to desable element of list |
---|
| 39 | function pwg_disabled_selection() |
---|
| 40 | { |
---|
| 41 | array_date = $(linked_date).val().split('-'); |
---|
| 42 | y = array_date[0]; |
---|
| 43 | m = array_date[1]; |
---|
| 44 | |
---|
| 45 | // Init list |
---|
| 46 | $(day + " option").attr("disabled", ""); |
---|
| 47 | $(month + " option").attr("disabled", ""); |
---|
| 48 | |
---|
| 49 | var daysInMonth = 32 - new Date(y, m - 1, 32).getDate(); |
---|
| 50 | $(day + " option:gt(" + (daysInMonth) +")").attr("disabled", "disabled"); |
---|
| 51 | |
---|
| 52 | if ((min_linked_date != null) && (is_valid_linked_value(min_linked_date) == true)) |
---|
| 53 | { |
---|
| 54 | date_cmp = min_linked_date; |
---|
| 55 | op_cmp = "lt"; |
---|
| 56 | } |
---|
| 57 | else if ((max_linked_date != null) && (is_valid_linked_value(max_linked_date) == true)) |
---|
| 58 | { |
---|
| 59 | date_cmp = max_linked_date; |
---|
| 60 | op_cmp = "gt"; |
---|
| 61 | } |
---|
| 62 | else |
---|
| 63 | { |
---|
| 64 | date_cmp = null; |
---|
| 65 | op_cmp = null; |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | if (op_cmp != null) |
---|
| 69 | { |
---|
| 70 | array_date = $(date_cmp).val().split('-'); |
---|
| 71 | y_cmp = array_date[0]; |
---|
| 72 | m_cmp = array_date[1]; |
---|
| 73 | d_cmp = array_date[2]; |
---|
| 74 | |
---|
| 75 | if (y == y_cmp) |
---|
| 76 | { |
---|
| 77 | $(month + " option:" + op_cmp + "(" + (m_cmp) +")").attr("disabled", "disabled"); |
---|
| 78 | if (op_cmp == "lt") |
---|
| 79 | { |
---|
| 80 | $(month + " option:eq(" + (0) +")").attr("disabled", ""); |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | if (m == m_cmp) |
---|
| 84 | { |
---|
| 85 | $(day + " option:" + op_cmp + "(" + (d_cmp) +")").attr("disabled", "disabled"); |
---|
| 86 | if (op_cmp == "lt") |
---|
| 87 | { |
---|
| 88 | $(day + " option:eq(" + (0) +")").attr("disabled", ""); |
---|
| 89 | } |
---|
| 90 | } |
---|
| 91 | } |
---|
| 92 | } |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | // Prevent selection of invalid dates through the select controls |
---|
| 96 | function pwg_check_date() |
---|
| 97 | { |
---|
| 98 | last_date = $(linked_date).val(); |
---|
| 99 | |
---|
| 100 | $(linked_date).val(pwg_get_fmt_from_ctrls()); |
---|
| 101 | |
---|
| 102 | if ((min_linked_date != null) && (is_valid_linked_value(min_linked_date))) |
---|
| 103 | { |
---|
| 104 | cancel = ($(min_linked_date).datepicker("getDate") > $(linked_date).datepicker("getDate")); |
---|
| 105 | } |
---|
| 106 | else if ((max_linked_date != null) && (is_valid_linked_value(max_linked_date))) |
---|
| 107 | { |
---|
| 108 | cancel = ($(max_linked_date).datepicker("getDate") < $(linked_date).datepicker("getDate")); |
---|
| 109 | } |
---|
| 110 | else |
---|
| 111 | { |
---|
| 112 | cancel = false; |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | if (cancel) |
---|
| 116 | { |
---|
| 117 | array_date = last_date.split('-'); |
---|
| 118 | $(year).val(array_date[0]); |
---|
| 119 | $(month).val(array_date[1]); |
---|
| 120 | $(day).val(array_date[2]); |
---|
| 121 | // check again |
---|
| 122 | pwg_check_date(); |
---|
| 123 | } |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | jQuery().ready(function(){ |
---|
| 127 | // Init hidden value |
---|
| 128 | $(linked_date).val(pwg_get_fmt_from_ctrls()); |
---|
| 129 | |
---|
| 130 | // Init Datepicker |
---|
| 131 | jQuery(linked_date).datepicker({ |
---|
| 132 | dateFormat:'yy-m-d', |
---|
| 133 | beforeShow: |
---|
| 134 | // Prepare to show a date picker linked to three select controls |
---|
| 135 | function readLinked(input) { |
---|
| 136 | if (min_linked_date != null) |
---|
| 137 | { |
---|
| 138 | return {minDate: $(min_linked_date).datepicker("getDate")}; |
---|
| 139 | } |
---|
| 140 | else if (max_linked_date != null) |
---|
| 141 | { |
---|
| 142 | return {maxDate: $(max_linked_date).datepicker("getDate")}; |
---|
| 143 | } |
---|
| 144 | else |
---|
| 145 | { |
---|
| 146 | return {}; |
---|
| 147 | } |
---|
| 148 | }, |
---|
| 149 | onSelect: |
---|
| 150 | // Update three select controls to match a date picker selection |
---|
| 151 | function updateLinked(date) { |
---|
| 152 | if (date.length == 0) |
---|
| 153 | { |
---|
| 154 | $(year).val(""); |
---|
| 155 | $(month).val("0"); |
---|
| 156 | $(day).val("0"); |
---|
| 157 | } |
---|
| 158 | else |
---|
| 159 | { |
---|
| 160 | array_date = date.split('-'); |
---|
| 161 | $(year).val(array_date[0]); |
---|
| 162 | $(month).val(array_date[1]); |
---|
| 163 | $(day).val(array_date[2]); |
---|
| 164 | } |
---|
| 165 | pwg_on_date_change(); |
---|
| 166 | }, |
---|
| 167 | showOn: "both", |
---|
| 168 | buttonImage: buttonImageName, |
---|
| 169 | buttonImageOnly: true, |
---|
| 170 | buttonText: "" |
---|
| 171 | }); |
---|
| 172 | |
---|
| 173 | // Check showed controls |
---|
| 174 | jQuery(day + ", " + month + ", " + year).change( |
---|
| 175 | function () |
---|
| 176 | { |
---|
| 177 | pwg_on_date_change(); |
---|
| 178 | }); |
---|
| 179 | |
---|
| 180 | // Check showed controls |
---|
| 181 | jQuery(day + ", " + month + ", " + year).focus( |
---|
| 182 | function () |
---|
| 183 | { |
---|
| 184 | pwg_disabled_selection(); |
---|
| 185 | }); |
---|
| 186 | |
---|
| 187 | // In order to init linked input |
---|
| 188 | pwg_check_date(); |
---|
| 189 | }); |
---|
| 190 | |
---|
| 191 | } |
---|