Rev Author Line No. Line
250 kaklik 1 /* $Id: tbl_change.js,v 1.1 2005/11/23 19:10:30 nijel Exp $ */
2  
3  
4 /**
5 * Modify from controls when the "NULL" checkbox is selected
6 *
7 * @param string the MySQL field type
8 * @param string the urlencoded field name
9 * @param string the md5 hashed field name
10 *
11 * @return boolean always true
12 */
13 function nullify(theType, urlField, md5Field, multi_edit)
14 {
15 var rowForm = document.forms['insertForm'];
16  
17 if (typeof(rowForm.elements['funcs' + multi_edit + '[' + urlField + ']']) != 'undefined') {
18 rowForm.elements['funcs' + multi_edit + '[' + urlField + ']'].selectedIndex = -1;
19 }
20  
21 // "SET" field , "ENUM" field with more than 20 characters
22 // or foreign key field
23 if (theType == 1 || theType == 3 || theType == 4) {
24 rowForm.elements['field_' + md5Field + multi_edit + '[]'].selectedIndex = -1;
25 }
26 // Other "ENUM" field
27 else if (theType == 2) {
28 var elts = rowForm.elements['field_' + md5Field + multi_edit + '[]'];
29 // when there is just one option in ENUM:
30 if (elts.checked) {
31 elts.checked = false;
32 } else {
33 var elts_cnt = elts.length;
34 for (var i = 0; i < elts_cnt; i++ ) {
35 elts[i].checked = false;
36 } // end for
37  
38 } // end if
39 }
40 // Other field types
41 else /*if (theType == 5)*/ {
42 rowForm.elements['fields' + multi_edit + '[' + urlField + ']'].value = '';
43 } // end if... else if... else
44  
45 return true;
46 } // end of the 'nullify()' function
47  
48  
49 /**
50 * Unchecks the "NULL" control when a function has been selected or a value
51 * entered
52 *
53 * @param string the urlencoded field name
54 *
55 * @return boolean always true
56 */
57 function unNullify(urlField, multi_edit)
58 {
59 var rowForm = document.forms['insertForm'];
60  
61 if (typeof(rowForm.elements['fields_null[multi_edit][' + multi_edit + '][' + urlField + ']']) != 'undefined') {
62 rowForm.elements['fields_null[multi_edit][' + multi_edit + '][' + urlField + ']'].checked = false
63 } // end if
64  
65 if (typeof(rowForm.elements['insert_ignore_' + multi_edit]) != 'undefined') {
66 rowForm.elements['insert_ignore_' + multi_edit].checked = false
67 } // end if
68  
69 return true;
70 } // end of the 'unNullify()' function
71  
72 var day;
73 var month;
74 var year;
75 var hour;
76 var minute;
77 var second;
78 var clock_set = 0;
79  
80 /**
81 * Opens calendar window.
82 *
83 * @param string calendar.php parameters
84 * @param string form name
85 * @param string field name
86 * @param string edit type - date/timestamp
87 */
88 function openCalendar(params, form, field, type) {
89 window.open("./calendar.php?" + params, "calendar", "width=400,height=200,status=yes");
90 dateField = eval("document." + form + "." + field);
91 dateType = type;
92 }
93  
94 /**
95 * Formats number to two digits.
96 *
97 * @param int number to format.
98 * @param string type of number
99 */
100 function formatNum2(i, valtype) {
101 f = (i < 10 ? '0' : '') + i;
102 if (valtype && valtype != '') {
103 switch(valtype) {
104 case 'month':
105 f = (f > 12 ? 12 : f);
106 break;
107  
108 case 'day':
109 f = (f > 31 ? 31 : f);
110 break;
111  
112 case 'hour':
113 f = (f > 24 ? 24 : f);
114 break;
115  
116 default:
117 case 'second':
118 case 'minute':
119 f = (f > 59 ? 59 : f);
120 break;
121 }
122 }
123  
124 return f;
125 }
126  
127 /**
128 * Formats number to two digits.
129 *
130 * @param int number to format.
131 * @param int default value
132 * @param string type of number
133 */
134 function formatNum2d(i, default_v, valtype) {
135 i = parseInt(i, 10);
136 if (isNaN(i)) return default_v;
137 return formatNum2(i, valtype)
138 }
139  
140 /**
141 * Formats number to four digits.
142 *
143 * @param int number to format.
144 */
145 function formatNum4(i) {
146 i = parseInt(i, 10)
147 return (i < 1000 ? i < 100 ? i < 10 ? '000' : '00' : '0' : '') + i;
148 }
149  
150 /**
151 * Initializes calendar window.
152 */
153 function initCalendar() {
154 if (!year && !month && !day) {
155 /* Called for first time */
156 if (window.opener.dateField.value) {
157 value = window.opener.dateField.value;
158 if (window.opener.dateType == 'datetime' || window.opener.dateType == 'date') {
159 if (window.opener.dateType == 'datetime') {
160 parts = value.split(' ');
161 value = parts[0];
162  
163 if (parts[1]) {
164 time = parts[1].split(':');
165 hour = parseInt(time[0],10);
166 minute = parseInt(time[1],10);
167 second = parseInt(time[2],10);
168 }
169 }
170 date = value.split("-");
171 day = parseInt(date[2],10);
172 month = parseInt(date[1],10) - 1;
173 year = parseInt(date[0],10);
174 } else {
175 year = parseInt(value.substr(0,4),10);
176 month = parseInt(value.substr(4,2),10) - 1;
177 day = parseInt(value.substr(6,2),10);
178 hour = parseInt(value.substr(8,2),10);
179 minute = parseInt(value.substr(10,2),10);
180 second = parseInt(value.substr(12,2),10);
181 }
182 }
183 if (isNaN(year) || isNaN(month) || isNaN(day) || day == 0) {
184 dt = new Date();
185 year = dt.getFullYear();
186 month = dt.getMonth();
187 day = dt.getDate();
188 }
189 if (isNaN(hour) || isNaN(minute) || isNaN(second)) {
190 dt = new Date();
191 hour = dt.getHours();
192 minute = dt.getMinutes();
193 second = dt.getSeconds();
194 }
195 } else {
196 /* Moving in calendar */
197 if (month > 11) {
198 month = 0;
199 year++;
200 }
201 if (month < 0) {
202 month = 11;
203 year--;
204 }
205 }
206  
207 if (document.getElementById) {
208 cnt = document.getElementById("calendar_data");
209 } else if (document.all) {
210 cnt = document.all["calendar_data"];
211 }
212  
213 cnt.innerHTML = "";
214  
215 str = ""
216  
217 //heading table
218 str += '<table class="calendar"><tr><th width="50%">';
219 str += '<form method="NONE" onsubmit="return 0">';
220 str += '<a href="javascript:month--; initCalendar();">&laquo;</a> ';
221 str += '<select id="select_month" name="monthsel" onchange="month = parseInt(document.getElementById(\'select_month\').value); initCalendar();">';
222 for (i =0; i < 12; i++) {
223 if (i == month) selected = ' selected="selected"';
224 else selected = '';
225 str += '<option value="' + i + '" ' + selected + '>' + month_names[i] + '</option>';
226 }
227 str += '</select>';
228 str += ' <a href="javascript:month++; initCalendar();">&raquo;</a>';
229 str += '</form>';
230 str += '</th><th width="50%">';
231 str += '<form method="NONE" onsubmit="return 0">';
232 str += '<a href="javascript:year--; initCalendar();">&laquo;</a> ';
233 str += '<select id="select_year" name="yearsel" onchange="year = parseInt(document.getElementById(\'select_year\').value); initCalendar();">';
234 for (i = year - 25; i < year + 25; i++) {
235 if (i == year) selected = ' selected="selected"';
236 else selected = '';
237 str += '<option value="' + i + '" ' + selected + '>' + i + '</option>';
238 }
239 str += '</select>';
240 str += ' <a href="javascript:year++; initCalendar();">&raquo;</a>';
241 str += '</form>';
242 str += '</th></tr></table>';
243  
244 str += '<table class="calendar"><tr>';
245 for (i = 0; i < 7; i++) {
246 str += "<th>" + day_names[i] + "</th>";
247 }
248 str += "</tr>";
249  
250 var firstDay = new Date(year, month, 1).getDay();
251 var lastDay = new Date(year, month + 1, 0).getDate();
252  
253 str += "<tr>";
254  
255 dayInWeek = 0;
256 for (i = 0; i < firstDay; i++) {
257 str += "<td>&nbsp;</td>";
258 dayInWeek++;
259 }
260 for (i = 1; i <= lastDay; i++) {
261 if (dayInWeek == 7) {
262 str += "</tr><tr>";
263 dayInWeek = 0;
264 }
265  
266 dispmonth = 1 + month;
267  
268 if (window.opener.dateType == 'datetime' || window.opener.dateType == 'date') {
269 actVal = "" + formatNum4(year) + "-" + formatNum2(dispmonth, 'month') + "-" + formatNum2(i, 'day');
270 } else {
271 actVal = "" + formatNum4(year) + formatNum2(dispmonth, 'month') + formatNum2(i, 'day');
272 }
273 if (i == day) {
274 style = ' class="selected"';
275 current_date = actVal;
276 } else {
277 style = '';
278 }
279 str += "<td" + style + "><a href=\"javascript:returnDate('" + actVal + "');\">" + i + "</a></td>"
280 dayInWeek++;
281 }
282 for (i = dayInWeek; i < 7; i++) {
283 str += "<td>&nbsp;</td>";
284 }
285  
286 str += "</tr></table>";
287  
288 cnt.innerHTML = str;
289  
290 // Should we handle time also?
291 if (window.opener.dateType != 'date' && !clock_set) {
292  
293 if (document.getElementById) {
294 cnt = document.getElementById("clock_data");
295 } else if (document.all) {
296 cnt = document.all["clock_data"];
297 }
298  
299 str = '';
300 init_hour = hour;
301 init_minute = minute;
302 init_second = second;
303 str += '<form method="NONE" class="clock" onsubmit="returnDate(\'' + current_date + '\')">';
304 str += '<input id="hour" type="text" size="2" maxlength="2" onblur="this.value=formatNum2d(this.value, init_hour, \'hour\'); init_hour = this.value;" value="' + formatNum2(hour, 'hour') + '" />:';
305 str += '<input id="minute" type="text" size="2" maxlength="2" onblur="this.value=formatNum2d(this.value, init_minute, \'minute\'); init_minute = this.value;" value="' + formatNum2(minute, 'minute') + '" />:';
306 str += '<input id="second" type="text" size="2" maxlength="2" onblur="this.value=formatNum2d(this.value, init_second, \'second\'); init_second = this.value;" value="' + formatNum2(second, 'second') + '" />';
307 str += '<br />';
308 str += '<input type="submit" value="' + submit_text + '"/>';
309 str += '</form>';
310  
311 cnt.innerHTML = str;
312 clock_set = 1;
313 }
314  
315 }
316  
317 /**
318 * Returns date from calendar.
319 *
320 * @param string date text
321 */
322 function returnDate(d) {
323 txt = d;
324 if (window.opener.dateType != 'date') {
325 // need to get time
326 h = parseInt(document.getElementById('hour').value,10);
327 m = parseInt(document.getElementById('minute').value,10);
328 s = parseInt(document.getElementById('second').value,10);
329 if (window.opener.dateType == 'datetime') {
330 txt += ' ' + formatNum2(h, 'hour') + ':' + formatNum2(m, 'minute') + ':' + formatNum2(s, 'second');
331 } else {
332 // timestamp
333 txt += formatNum2(h, 'hour') + formatNum2(m, 'minute') + formatNum2(s, 'second');
334 }
335 }
336  
337 window.opener.dateField.value = txt;
338 window.close();
339 }