window.onload=function() {
  document.getElementById( 'counter' ).value = 1;  // reset vacc file counter
}

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() {

  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( 'vaccFileInputsCell' );

  // get a value from the hidden counter element, increment it, and re-store
  var hiddenCounter = document.getElementById( 'counter' ).value;
  hiddenCounter++;
  document.getElementById( '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', 'vaccfile'+hiddenCounter );
  newFileInput.setAttribute( 'id'  , 'vacc'+hiddenCounter );
  // add the new file input element to the table cell
  parentElement.appendChild( newFileInput );

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

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

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

function checkFormInputs() {

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

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

    // get the contents of this vaccine file input box
    var inputBox = document.getElementById( 'vacc'+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( "Antigen file input " + counter + " is blank.  Please fill in all file input boxes you create." );
      return false;
    }

    // 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 + "' for antigen file " + counter + " contains potentially harmful characters. Please use only letters, numbers, underscores, or dashes." )
      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( "Antigen file input 1 was left blank.  Please fill in all file input boxes you create." );
      return false;
  }

  if( !jobNumber && !pastedVaccineSet && vaccineFilesCounter<1 ) {
    alert( "You must provide a input antigen set." );
    return false;
  }
  if( !pastedBackgroundSet && !viralProteinsFile ) {
    alert( "You must provide a test viral protein set." );
    return false;
  }


/* check for  epitope_length */

  if ( checkNum(document.poscover_form.epitope_length, 5, 12, "Y" ) == false )
  {
      document.poscover_form.epitope_length.select();
      document.poscover_form.epitope_length.focus();
      return (false);
  }
 
 

/* check for offByMax */

  if ( checkNum(document.poscover_form.antigen_counts, 0, 10, "N" ) == false )
  {
      document.poscover_form.antigen_counts.select();
      document.poscover_form.antigen_counts.focus();
      return (false);
  }
 
}

function checkNum(obj, minVal, maxVal, Numeric)
{
	//alert("object= " + obj.name);
	var checkStr = obj.value;
	var allValid = true;
	var validGroups = true;
	var validChar;

	if ( Numeric == "Y" )
	{
		validChar = "0123456789"; 
	}
	else {
		validChar = ", -0123456789"; 
	}


	if ( checkStr == "" )
	{
		alert("Please Enter a(an) " + obj.name);
		return (false);
	}

	for (i = 0;  i < checkStr.length;  i++)
	{
		var ch = checkStr.charAt(i);

		for (j = 0;  j < validChar.length;  j++)
		{
			if (ch == validChar.charAt(j))
				break;
		}
		if (j == validChar.length)
		{
			allValid = false;
			break;
		}
	}
	
	if (!allValid)
	{
		if (minVal == "NA" || maxVal == "NA" ) 
		{
			alert("\"" + ch + "\" is not a valid " + obj.name + ".\nPlease enter a number.");
		} 
		else 
		{
			alert("\"" + ch + "\" is not a valid " + obj.name + ".\nPlease enter a number between " + minVal + " and " + maxVal + ".");
		}
		obj.select();
		obj.focus();
		return (false);
	}

	if (minVal != "NA" && maxVal != "NA" ) 
	{
		var prsVal = parseInt(checkStr);

		if (( prsVal > maxVal || prsVal < minVal ))
		{
			alert("Please enter a(an) " + obj.name + " between " + minVal + " to " + maxVal + "." );
			return (false);
		}
    }

	return (true);
}




