kaniosrn-j
5/12/2017 - 3:32 PM

Copy data into rows below

Copy data into rows below

function copy_row(from) {
  var cont = confirm("This will copy ALL the data from the current row to\nEVERY row below it. This cannot be undone.\n\nClick 'OK' to go ahead.\nClick 'Cancel' to stop.");
  if (cont == 1) {
    //document.getElementById('inprogress').style.display='block';
    var row = document.getElementById(from);
    var values = [];
    var text_values = [];
    var selectmenu = [];
    var selectmenutext = [];

    for (var y = 0; y < row.getElementsByTagName("input").length; y++) {
      switch (row.getElementsByTagName("input")[y].type) {
        case 'text':
          text_values[y] = row.getElementsByTagName("input")[y].value;
          values[y] = row.getElementsByTagName("input")[y].value;
          break;
        case 'radio':
          text_values[y] = row.getElementsByTagName("input")[y].value;
          values[y] = row.getElementsByTagName("input")[y].checked;
          break;
        case 'checkbox':
          text_values[y] = row.getElementsByTagName("input")[y].value;
          values[y] = row.getElementsByTagName("input")[y].checked;
          break;
      }
    }
    //Get the select option (if there is one).
    for (var y = 0; y < row.getElementsByTagName("select").length; y++) {
      selectmenu[y] = row.getElementsByTagName("select")[y].selectedIndex;
      selectmenutext[y] = row.getElementsByTagName("select")[y].value;
    }


    //assuming there will be 5 rows (0-5) fill in the values for any row below
    //the current one, using the array from above.
    counter = 6;
    for (x = 0; x < counter; x++) {
      // if the current row number is in the cookie...
      // ...and the current row number is greater than 'from'
      // if((getCookie("rownumbers").indexOf(x)!=-1) && (x>from))
      if (x > from) {
        var row = document.getElementById(x);
        for (var y = 0; y < row.getElementsByTagName("input").length; y++) {
          switch (row.getElementsByTagName("input")[y].type) {
            case 'text':
              row.getElementsByTagName("input")[y].value = values[y];
              data = row.getElementsByTagName("input")[y].id + "=" + encodeURIComponent(values[y]) + "&page_number=" + page_number;
              if (data != "") {
                post_data(data);
              }
              //getfield(row.getElementsByTagName("input")[y]);
              break;
            case 'radio':
              row.getElementsByTagName("input")[y].checked = values[y];
              if (row.getElementsByTagName("input")[y].checked) {
                data = row.getElementsByTagName("input")[y].id + "=" + encodeURIComponent(text_values[y]) + "&page_number=" + page_number;

                if (values[y] == false) {
                  data = row.getElementsByTagName("input")[y].id + "=" + '_del_' + "&page_number=" + page_number;
                }

              }
              if (data != "") {
                post_data(data);
              }
              break;
            case 'checkbox':
              row.getElementsByTagName("input")[y].checked = values[y];
              data = row.getElementsByTagName("input")[y].id + "=" + encodeURIComponent(text_values[y]) + "&page_number=" + page_number;
              if (values[y] == false) {
                data = row.getElementsByTagName("input")[y].id + "=" + '_del_' + "&page_number=" + page_number;
              }

              if (data != "") {
                post_data(data);
              }
              //getchkfield(row.getElementsByTagName("input")[y]);
              break;
          }

        }
        //Set the select option (if there is one).
        for (var y = 0; y < row.getElementsByTagName("select").length; y++) {
          row.getElementsByTagName("select")[y].selectedIndex = selectmenu[y];
          data = row.getElementsByTagName("select")[y].id + "=" + encodeURIComponent(selectmenutext[y]) + "&page_number=" + page_number;
          if (data != "") {
            post_data(data);
          }

        }
      }
    }
  }
}