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