<script type="text/javascript">
var g_visibleFileUploads;

function multiFileUpload_init(p_item) {
  var l_counter = 1;
  while ($x(p_item + '_' + l_counter)) {
    if  ($x(p_item + '_' + (l_counter + 1))) {
      $x(p_item + '_' + (l_counter + 1)).parentNode.parentNode.style.display = "none";
    }
    $x(p_item + '_' + l_counter).parentNode.innerHTML =
      $x(p_item + '_' + l_counter).parentNode.innerHTML +
      '<span id="' + p_item + '_' + l_counter + '_HIDE" style="display: none;"> ' +
      '&nbsp;' +
      '<a class="removeFileUpload" href="javascript:multiFileUpload_hide(\'' + p_item + '\', ' + l_counter + ')">' +
      'Remove' +
      '</a>' +
      '</span>' +
      '<div id="' + p_item + '_' + l_counter + '_UNHIDE"> ' +
      '<a class="addFileUpload" href="javascript:multiFileUpload_unhide(\'' + p_item + '\')">' +
      'Upload another file' +
      '</div>';
    l_counter = l_counter + 1;
  }
  g_visibleFileUploads = 1;
}

function multiFileUpload_unhide(p_item) {
  var l_next_item = -1;
  var l_counter = 1;
  while ($x(p_item + '_' + l_counter) && l_next_item == -1) {
    if ($x(p_item + '_' + l_counter).parentNode.parentNode.style.display == "none") {
      l_next_item = l_counter;
    }
    l_counter = l_counter + 1;
  } 
  if (l_next_item != -1) {
    $x(p_item + '_' + l_next_item).parentNode.parentNode.style.display = "";
    g_visibleFileUploads = g_visibleFileUploads + 1;
    multiFileUpload_adjustUploadAnotherFile(p_item);
    multiFileUpload_adjustRemoveFile(p_item);
  } else {
    alert("Cannot upload more files");
  }
}

function multiFileUpload_hide(p_item, p_counter) {
  $x(p_item + '_' + p_counter).parentNode.parentNode.style.display = "none";
  document.getElementById(p_item + '_' + p_counter).parentNode.innerHTML = document.getElementById(p_item + '_' + p_counter).parentNode.innerHTML;
  g_visibleFileUploads = g_visibleFileUploads - 1;
  multiFileUpload_adjustUploadAnotherFile(p_item);
  multiFileUpload_adjustRemoveFile(p_item);
}

function multiFileUpload_adjustRemoveFile(p_item) {
  var l_counter;
  if (g_visibleFileUploads == 1) {
    l_counter = 1;
    while ($x(p_item + '_' + l_counter)) {
      $x(p_item + '_' + l_counter + '_HIDE').style.display = "none";
      l_counter = l_counter + 1;
    } 
  } else {
    l_counter = 1;
    while ($x(p_item + '_' + l_counter)) {
      document.getElementById(p_item + '_' + l_counter + '_HIDE').style.display = "";
      l_counter = l_counter + 1;
    } 
  } 
}

function multiFileUpload_adjustUploadAnotherFile(p_item) {
  var l_counter = 1;
  var l_last_item;
  var l_visible_items = 0;
  while ($x(p_item + '_' + l_counter)) {
    $x(p_item + '_' + l_counter + '_UNHIDE').style.display = "none";
    if ($x(p_item + '_' + l_counter).parentNode.parentNode.style.display == "") {
      l_last_item = l_counter;
      l_visible_items = l_visible_items + 1;
    }
    l_counter = l_counter + 1;
  }
  if ((l_counter - 1) > l_visible_items) {
    $x(p_item + '_' + l_last_item + '_UNHIDE').style.display = "";
  }
}
</script>