
  function removeAttach(sID) {
    // sID will be in the form 'file-n' where n is an integer greater than -1.
    var oDiv = document.getElementById('div' + sID);
    var oAttach = document.getElementById('attach' + sID);
    var oCell = document.getElementById('attachcell');
    var oDestination = document.getElementById('attachments');
    // If we have all the necessary elements, remove both the file[] input element
    // and the display row from the table. Otherwise we have an error.
    if (oDiv && oAttach && oCell && oDestination) {
      if (oDestination.removeChild(oAttach)) {
        oCell.removeChild(oDiv);
      }   
      else {
        alert('Failed to remove ' + sID);
      } 
    }
  }
  
  function getIndexFromString(sID) {
    // sID is in the form 'attachfile-n' where n is an integer greater than -1.
    var iPos = sID.indexOf('-');
    return parseInt(sID.substr(iPos + 1));  
  }
  
  function createFileInput(iID) {
    // Create a new form element for the next file and name it with the new index.
    var oFileInput = document.createElement('input');
    oFileInput.setAttribute('type', 'file');
    oFileInput.setAttribute('name', 'file[]');
    oFileInput.setAttribute('id', 'attachfile-' + iID);
    oFileInput.setAttribute('size', '30');
    oFileInput.onchange = function(){changeAttach(this.id);};
    return oFileInput;
  }
  
  function createDisplayDiv(iID, sFile) {
    var oDiv = document.createElement('div');
    oDiv.setAttribute('id', 'divfile-' + iID);
    // Strip the path, otherwise IE displays the entire path and Firefox 
    // displays just the filename
    var iPos = sFile.lastIndexOf("\\");
    if (iPos > -1) sFile = sFile.substr(iPos + 1); 
    oDiv.innerHTML = '<a href="#" onclick="javascript:removeAttach(\'file-' + 
      iID + '\'); return false;" style="font-size: 0.8em">REMOVE</a> &nbsp;' +  
      sFile;  
    return oDiv;  
  }
  
  function changeAttach(sID) {
    var oCurrent = document.getElementById(sID);
    var oParent = document.getElementById('attachselect');
    var oDestination = document.getElementById('attachments');
    var oCell = document.getElementById('attachcell');
    // Only continue if we have all these elements
    if (oCurrent && oParent && oDestination && oCell) {
      // Get the current file index.
      var iCurrentID = getIndexFromString(sID);
      // Create a new file input for the next file and name it with the new index.
      var oNew = createFileInput(iCurrentID + 1);
      // Move the current file input control into our hidden div ...
      if (oDestination.appendChild(oCurrent)) {
        // ... and replace it with a new file input element.
        oParent.appendChild(oNew);
        // Now create a new div for the display cell,including the REMOVE link.
        var oDiv = createDisplayDiv(iCurrentID, oCurrent.value);
        oCell.appendChild(oDiv);
      }
      else {
        alert('Failed to move ' + sID);
      }    
    }
  }