home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / phpMyAdmin / js / tbl_change.js < prev    next >
Encoding:
JavaScript  |  2008-06-23  |  10.9 KB  |  346 lines

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