window.onload=function() {
  // (re)set some counters
  document.getElementById( 'vacCounter' ).value = 1;
  document.getElementById( 'virCounter' ).value = 1;
  // show javascript-handled multiple file upload controls
  document.getElementById('addOrRemoveVacFiles').style.display  = 'block';
  document.getElementById('addOrRemoveVirFiles').style.display  = 'block';
}

function resetclicked() {
  var old_URL = self.location.href;
  var end_of_base_URL = old_URL.indexOf('?');
  var new_URL = old_URL.substring(0, end_of_base_URL);
  self.location.href = new_URL;
  document.getElementById( 'counter' ).value = 1;  // reset vacc file counter
}

function addFileUploadElement( fileType ) {

  DEBUG = false;

  if (DEBUG) { alert( "in function addFileUploadElement" ); }

  // get the element (a table cell) which will have new sub-elements added into it
  var parentElement = document.getElementById( fileType + 'FileInputsCell' );

  // get a value from the hidden counter element, increment it, and re-store
  var hiddenCounter = document.getElementById( fileType + 'Counter' ).value;
  hiddenCounter++;
  document.getElementById( fileType + 'Counter' ).value = hiddenCounter;
  if (DEBUG) { alert( "using " + hiddenCounter + " to create the new file input element" ); }

  // create the new file input element, with attributes
  var newFileInput = document.createElement( 'input' );
  newFileInput.setAttribute( 'type', "file");
  newFileInput.setAttribute( 'size', 50);
  newFileInput.setAttribute( 'name', fileType + 'file' + hiddenCounter );
  newFileInput.setAttribute( 'id'  , fileType + hiddenCounter );

  // add the new file input element to the table cell
  parentElement.appendChild( newFileInput );

  // show the 'or fewer [-]' div
  var fewerDivStyle = document.getElementById( fileType + 'Fewer' ).style
  fewerDivStyle.display = 'inline';
}

function removeFileUploadElement( fileType ) {
  var id = document.getElementById( fileType + 'Counter' ).value;
  if ( id <= 1 ) { return false; }
  var parentElement   = document.getElementById( fileType + 'FileInputsCell' );
  var targetFileInput = document.getElementById( fileType + id );
  parentElement.removeChild( targetFileInput );
  id--;
  document.getElementById( fileType + 'Counter' ).value = id;

  // if id is now 1, hide the 'or fewer [-]' div
  if ( id == 1 ) {
    var fewerDivStyle = document.getElementById( fileType + 'Fewer' ).style
    fewerDivStyle.display = 'none';
  }
}

function checkFormInputs() {

/* get all 'Input' element contents */
  var pastedVaccineSet = document.epicover_form.vaccine.value;
  // vaccine files dealt with separately below
  var pastedBackgroundSet = document.epicover_form.virals.value;
  var jobNumber = document.epicover_form.jobNumber.value;

  // iterate through vaccine file input boxes until you run out of them
  var counter = 1;              // tracks which file box we are looking at
  var vaccineFilesCounter = 0;  // counts how many vaccine file boxes were filled
  while ( document.getElementById( 'vac'+counter ) ) {

    // get the contents of this vaccine file input box
    var inputBox = document.getElementById( 'vac'+counter );
    var filePath = inputBox.value;

    if ( filePath ) { vaccineFilesCounter++; }

  /* Do some checks on each vaccine file input while we are looping */
    // check to make sure this input box is not blank (after the first one?)
    if ( !filePath && !pastedVaccineSet && counter>1 ) {
      alert( "Vaccine file input " + counter + " is blank.  Please fill in all file input boxes you create." );
      return false;
    }

    if ( !checkName( filePath ) ) { return false; }

    counter++;
  }

  // something here doesn't work right - sometimes fires when box 1 is full and box 2 is empty
  if( vaccineFilesCounter>0 && counter-1 != vaccineFilesCounter ) {
      alert( "Vaccine file input 1 was left blank.  Please fill in all file input boxes you create." );
      return false;
  }

  // iterate through background sequence file input boxes until you run out of them
  counter = 1;
  var backgroundFilesCounter = 0;  // counts how many background sequence file boxes were filled
  while ( document.getElementById( 'vir'+counter ) ) {

    // get the contents of this background sequence file input box
    inputBox = document.getElementById( 'vir'+counter );
    filePath = inputBox.value;

    if ( filePath ) { backgroundFilesCounter++; }

  /* Do some checks on each background sequence file input while we are looping */
    // check to make sure this input box is not blank (after the first one?)
    if ( !filePath && !pastedBackgroundSet && counter>1 ) {
      alert( "Background sequences file input " + counter + " is blank.  Please fill in all file input boxes you create." );
      return false;
    }

    if ( !checkName( filePath ) ) { return false; }

    counter++;
  }

  if( backgroundFilesCounter>0 && counter-1 != backgroundFilesCounter ) {
      alert( "Background sequences file input 1 was left blank.  Please fill in all file input boxes you create." );
      return false;
  }

  // get the subgroups file path and check that, too
  var subgroupsFilePath = document.epicover_form.groupsfile.value;
  if ( !checkName( subgroupsFilePath ) ) { return false; }

//  alert( "pastedVaccineSet: " + ((pastedVaccineSet) ? 'true' : 'false') + "\nvaccineFilesCounter = " + vaccineFilesCounter + "\npastedBackgroundSet: " + ((pastedBackgroundSet) ? 'true' : 'false') + "\nbackgroundFilesCounter = " + backgroundFilesCounter );

/* check for consistency now that all inputs are known */
  /* If a jobNumber is provided, no other input is required.
   * If not, there must be some input in the vaccine area,
   * AND some input in the background sequences area, for
   * processing to be meaningful. */
  if( !jobNumber
         &&
         ! (
             ( pastedVaccineSet || vaccineFilesCounter>0 )
             &&
             ( pastedBackgroundSet || backgroundFilesCounter>0 )
           )
    )
  {
    alert( "You must provide both a vaccine set and a background viral protein set." );
    return false;
  }

}

function checkName ( filePath ) {

    // need to parse the file path, and only look at the file name
    var fileDelimRegExp = /[\/\\:]/;
    var fileTokens = filePath.split( fileDelimRegExp );  // split the path
    var fileName = fileTokens[fileTokens.length-1];      // file name is final token

    // now check the name for bad characters
    var badName = /[^A-Za-z0-9._\-]/.test(fileName);
    if ( badName ) {
      alert( "The name '" + fileName + "' contains potentially harmful characters. Please use only letters, numbers, underscores, or dashes." )
      return false;
    }

    return true;
}
