/*============================================================
								TUBESET MANAGEMENT SCRIPT
==============================================================
	NAMING CONVENTIONS AND VARIABLE DESCRIPTION
	at = Assign Tube (Creating Tubeset Methods)
	as = Assign Sample (Creating Tubeset Methods)
	to = Tube Order (Ordering Tubeset Methods)
	select = Select tag element (presentation list)
	list = Hidden tag element (hidden csv list read into java)
============================================================*/

//Globals variables
var potList;
var potSelect;

/*------------------------------------------------
FUNCTION RICE_MOVE TUBE:
Moves a tube from the main tube list to the selected
pot select and pot list. The reorders the pot select
and pot list

Input:
potRef(Pot Reference) - name of the corresonding pot
formName - name of the form for the selected element
------------------------------------------------*/
function rice_moveTube(potRef, formName)
{
	//Retrieve the tube number
	tubeNum = document.forms[formName].tubeList.value;
	
	if ((tubeNum == "") || (tubeNum == null))
	{
		alert('Please select a tube to be moved into the pot list')
	}
	else
	{		
		//Create string for element id for pot listing
		potSelect = 'potSelect' + potRef;
		potList = 'potList' + potRef;
		
		//Retrieve the location of the selected option
		indexNum = document.forms[formName].tubeList.selectedIndex
		
		//Setup new option to update the pot list
		var newOption = new Option(tubeNum, tubeNum);
		optionCount = document.forms[formName].elements[potSelect].length
		document.forms[formName].elements[potSelect].options[optionCount] = newOption;
		
		//Remove the selected tube from the main tube list
		document.forms[formName].tubeList.options[indexNum] = null;
			
		//-----Sort the pot list when adding an tube in------
		//Create temporay array with the pot list contents
		var tempArray = new Array(document.forms[formName].elements[potSelect].length);
		
		//Loop through all available options in pot select element
		for (j=0; j < tempArray.length; j++)
		{
			tempArray[j] = document.forms[formName].elements[potSelect].options[j].value;			
		}
	
		//Sort the array using the sort method
		tempArray.sort(sortMethod);
	
	
		//Get the contents of the pot text list then clear text list
		var potListContents = document.forms[formName].elements[potList].value;
		document.forms[formName].elements[potList].value = "";
		
		//Output the sorted list in the pot select list and pot list
		for (q=0; q < tempArray.length; q++)
		{
			document.forms[formName].elements[potSelect].options[q] = new Option(tempArray[q], tempArray[q]);
			document.forms[formName].elements[potList].value = document.forms[formName].elements[potList].value + tempArray[q] + ","
		}
		
		//Create temporay array with the tube list contents
		tempArray = new Array(document.forms[formName].tubeList.length);
	}
	
	
	//Set the focus and selected index in the main tube list to the first element
	document.forms[formName].tubeList.focus();
	document.forms[formName].tubeList.selectedIndex = 0;
}

/*------------------------------------------------
FUNCTION AT_MOVE TUBE:
Moves a tube from the main tube list to the selected
pot select and pot list. The reorders the pot select
and pot list

Input:
potRef(Pot Reference) - name of the corresonding pot
formName - name of the form for the selected element
------------------------------------------------*/
function at_moveTube(potRef, formName)
{
	//Retrieve the tube number
	tubeNum = document.forms[formName].tubeList.value;
	
	if ((tubeNum == "") || (tubeNum == null))
	{
		alert('Please select a tube to be moved into the pot list')
	}
	else
	{		
		//Create string for element id for pot listing
		potSelect = 'potSelect' + potRef;
		potList = 'potList' + potRef;
		
		//Retrieve the location of the selected option
		indexNum = document.forms[formName].tubeList.selectedIndex
		
		//Setup new option to update the pot list
		var newOption = new Option(tubeNum, tubeNum);
		optionCount = document.forms[formName].elements[potSelect].length
		document.forms[formName].elements[potSelect].options[optionCount] = newOption;
		
		//Remove the selected tube from the main tube list
		document.forms[formName].tubeList.options[indexNum] = null;
			
		//-----Sort the pot list when adding an tube in------
		//Create temporay array with the pot list contents
		var tempArray = new Array(document.forms[formName].elements[potSelect].length);
		
		//Loop through all available options in pot select element
		for (j=0; j < tempArray.length; j++)
		{
			tempArray[j] = document.forms[formName].elements[potSelect].options[j].value;			
		}
	
		//Sort the array using the sort method
		tempArray.sort(sortMethod);
	
	
		//Get the contents of the pot text list then clear text list
		var potListContents = document.forms[formName].elements[potList].value;
		document.forms[formName].elements[potList].value = "";
		
		//Output the sorted list in the pot select list and pot list
		for (q=0; q < tempArray.length; q++)
		{
			document.forms[formName].elements[potSelect].options[q] = new Option(tempArray[q], tempArray[q]);
			document.forms[formName].elements[potList].value = document.forms[formName].elements[potList].value + tempArray[q] + ","
		}
		
		//Create temporay array with the tube list contents
		tempArray = new Array(document.forms[formName].tubeList.length);
		
		//Loop through all available options in pot select element
		for (j=0; j < tempArray.length; j++)
		{
			tempArray[j] = document.forms[formName].tubeList.options[j].value;			
		}
		document.forms[formName].sampleList.value = '';
		//Output the sorted list in the pot select list and pot list
		for (q=0; q < tempArray.length; q++)
		{
			document.forms[formName].sampleList.value = document.forms[formName].sampleList.value + tempArray[q] + ","
		}
	}
	
	
	//Set the focus and selected index in the main tube list to the first element
	document.forms[formName].tubeList.focus();
	document.forms[formName].tubeList.selectedIndex = 0;
}


/*------------------------------------------------
FUNCTION AT_REMOVE TUBE:
This fucntion will remove the tube from the pot
select and pot list then return it back to the main
tube list. Then reorders the main tube list, pot
select, and pot list.
	
Input:
potRef(Pot Reference) - name of the corresonding pot
formName - name of the form for the selected element
------------------------------------------------*/
function at_removeTube(potRef, formName)
{
	//Create string for element id for pot listing
	potSelect = potRef;
	potList = 'potList' + potRef.substring(9, potRef.length)
	
	//Retrieve the tube number
	tubeNum = document.forms[formName].elements[potSelect].value;
	
	if ((tubeNum == "") || (tubeNum == null))
	{
		alert('Please select a tube to be removed from the pot list')
	}
	else
	{
	
	//Retrieve the location of the selected option
	indexNum = document.forms[formName].elements[potSelect].selectedIndex
	
	//Setup new option to update the main tube list
	var newOption = new Option(tubeNum, tubeNum);
	optionCount = document.forms[formName].tubeList.length
	document.forms[formName].tubeList.options[optionCount] = newOption;
	
	//Remove the selected tube from the main tube list
	document.forms[formName].elements[potSelect].options[indexNum] = null;
	
	//-----Sort the main list when adding an element back in------
	//Create temporay array with the main tube list contents
	var tempArray = new Array(document.forms[formName].tubeList.length);
	for (j=0; j < tempArray.length; j++)
	{
		tempArray[j] = document.forms[formName].tubeList[j].value;
		
	}
	
	//Sort the array using the sort method
	tempArray.sort(sortMethod);
	document.forms[formName].sampleList.value = '';	
	
	//Output the sorted list in the main tube list
	for (q=0; q < tempArray.length; q++)
	{
		document.forms[formName].tubeList[q] = new Option(tempArray[q], tempArray[q]);
		document.forms[formName].sampleList.value = document.forms[formName].sampleList.value + tempArray[q] + ","			
	}
	
	//Reset the pot select to update the pot list contents
	var tempListArray = new Array(document.forms[formName].elements[potSelect].length);
	for (j=0; j < tempListArray.length; j++)
	{
		tempListArray[j] = document.forms[formName].elements[potSelect].options[j].value;			
	}
	
	//Get the contents of the pot text list then clear text list
	var potListContents = document.forms[formName].elements[potList].value;
	document.forms[formName].elements[potList].value = "";
	
	//Output the pot select and pot list with the new array
	for (j=0; j < tempListArray.length; j++)
	{
		document.forms[formName].elements[potSelect].options[j] = new Option(tempListArray[j], tempListArray[j]);
		document.forms[formName].elements[potList].value = document.forms[formName].elements[potList].value + tempListArray[j] + ","
	}
	
	//Set the focus and selected index in the pot select list to the first element
	document.forms[formName].elements[potSelect].focus();
	document.forms[formName].elements[potSelect].selectedIndex = 0;
	
	}
}


/*------------------------------------------------
FUNCTION RICE_REMOVE TUBE:
This fucntion will remove the tube from the pot
select and pot list then return it back to the main
tube list. Then reorders the main tube list, pot
select, and pot list.
	
Input:
potRef(Pot Reference) - name of the corresonding pot
formName - name of the form for the selected element
------------------------------------------------*/
function rice_removeTube(potRef, formName)
{
	//Create string for element id for pot listing
	potSelect = potRef;
	potList = 'potList' + potRef.substring(9, potRef.length)
	
	//Retrieve the tube number
	tubeNum = document.forms[formName].elements[potSelect].value;
	
	if ((tubeNum == "") || (tubeNum == null))
	{
		alert('Please select a tube to be removed from the pot list')
	}
	else
	{
	
	//Retrieve the location of the selected option
	indexNum = document.forms[formName].elements[potSelect].selectedIndex
	
	//Setup new option to update the main tube list
	var newOption = new Option(tubeNum, tubeNum);
	optionCount = document.forms[formName].tubeList.length
	document.forms[formName].tubeList.options[optionCount] = newOption;
	
	//Remove the selected tube from the main tube list
	document.forms[formName].elements[potSelect].options[indexNum] = null;
	
	//-----Sort the main list when adding an element back in------
	//Create temporay array with the main tube list contents
	var tempArray = new Array(document.forms[formName].tubeList.length);
	for (j=0; j < tempArray.length; j++)
	{
		tempArray[j] = document.forms[formName].tubeList[j].value;
		
	}
	
	//Sort the array using the sort method
	tempArray.sort(sortMethod);
	//document.forms[formName].sampleList.value = '';	
	
	//Output the sorted list in the main tube list
	//for (q=0; q < tempArray.length; q++)
	//{
	//	document.forms[formName].tubeList[q] = new Option(tempArray[q], tempArray[q]);
	//	document.forms[formName].sampleList.value = document.forms[formName].sampleList.value + tempArray[q] + ","			
	//}
	
	//Reset the pot select to update the pot list contents
	var tempListArray = new Array(document.forms[formName].elements[potSelect].length);
	for (j=0; j < tempListArray.length; j++)
	{
		tempListArray[j] = document.forms[formName].elements[potSelect].options[j].value;			
	}
	
	//Get the contents of the pot text list then clear text list
	var potListContents = document.forms[formName].elements[potList].value;
	document.forms[formName].elements[potList].value = "";
	
	//Output the pot select and pot list with the new array
	for (j=0; j < tempListArray.length; j++)
	{
		document.forms[formName].elements[potSelect].options[j] = new Option(tempListArray[j], tempListArray[j]);
		document.forms[formName].elements[potList].value = document.forms[formName].elements[potList].value + tempListArray[j] + ","
	}
	
	//Set the focus and selected index in the pot select list to the first element
	document.forms[formName].elements[potSelect].focus();
	document.forms[formName].elements[potSelect].selectedIndex = 0;
	
	}
}


/*------------------------------------------------
FUNCTION SORT METHOD:
This function defines how an array is sorted. This
function uses the numerical bubble sort method.

Input:
a - sample of array 1
b - sample of array 2
------------------------------------------------*/
function sortMethod(a,b)
{
	return(a-b);
}


/*------------------------------------------------
FUNCTION TO_PLACETUBE:
This function will place the tube into the last
position in the tube order list and tube order select,
then remove the tube from the main tube list.
	
Input:
formName - name of the form for the selected element
------------------------------------------------*/
function to_placeTube(formName)
{
	//Move to order column
	//Retrieve the tube number
		tubeNum = document.forms[formName].tubeList.value;
		
		if ((tubeNum == "") || (tubeNum == null))
		{
			alert('Please select a tube to be moved into the tube order list')
		}
		else
		{		
			//Retrieve the location of the selected option
			indexNum = document.forms[formName].tubeList.selectedIndex
			
			//Setup new option to update the pot list
			var newOption = new Option(tubeNum, tubeNum);
			optionCount = document.forms[formName].tubeOrder.length
			document.forms[formName].tubeOrder.options[optionCount] = newOption;
			
			//Append the tube number to the hidden field
			document.forms[formName].tubeOrderList.value = document.forms[formName].tubeOrderList.value + tubeNum + ",";
			
			//Remove the selected tube from the main tube list
			document.forms[formName].tubeList.options[indexNum] = null;
			document.forms[formName].tubeOrder.selectedIndex = document.forms[formName].tubeOrder.length-1;
		}
}

/*------------------------------------------------
FUNCTION RICE_TO_PLACETUBE:
This function will place the tube into the last
position in the tube order list and tube order select,
then remove the tube from the main tube list.
	
Input:
formName - name of the form for the selected element
------------------------------------------------*/
function rice_to_placeTube(formName)
{
	//Move to order column
	//Retrieve the tube number
		tubeNum = document.forms[formName].tubeList.value;
		
		if ((tubeNum == "") || (tubeNum == null))
		{
			alert('Please select a tube to be moved into the tube order list')
		}
		else
		{		
			//Retrieve the location of the selected option
			indexNum = document.forms[formName].tubeList.selectedIndex
			
			//Setup new option to update the pot list
			var newOption = new Option(tubeNum, tubeNum);
			optionCount = document.forms[formName].tubeOrder.length
			document.forms[formName].tubeOrder.options[optionCount] = newOption;
			
			//Append the tube number to the hidden field
			document.forms[formName].tubeOrderList.value = document.forms[formName].tubeOrderList.value + tubeNum + ",";
						
			//tubeNum value follows AT's format, <tube>-<pot>.
			//we don't want to show the pot name, only tube.
			for(i=0; i < document.forms[formName].tubeOrder.length; i++){
			  var txtValue = document.forms[formName].tubeOrder.options[i].value;
			  var txtDisplay = txtValue.substring(0, txtValue.indexOf("-"));
			  document.forms[formName].tubeOrder.options[i].text = txtDisplay;
			}
						
			//Remove the selected tube from the main tube list
			document.forms[formName].tubeList.options[indexNum] = null;
			document.forms[formName].tubeOrder.selectedIndex = document.forms[formName].tubeOrder.length-1;
		}
}


/*------------------------------------------------
FUNCTION TO_REMOVETUBE:
This function will remove the selected tube from
the tube order list and tube select list. 
	
Input:
potRef(Pot Reference) - name of the corresonding pot
formName - name of the form for the selected element
------------------------------------------------*/
function to_removeTube(formName)
{
	//Move to order column
	//Retrieve the tube number
		tubeNum = document.forms[formName].tubeOrder.value;
		var indexNum;
		
		if ((tubeNum == "") || (tubeNum == null))
		{
			alert('Please select a tube to be moved into the tube list')
		}
		else
		{		
			//Retrieve the location of the selected option
			indexNum = document.forms[formName].tubeOrder.selectedIndex
			
			//Setup new option to update the main tube list
			var newOption = new Option(tubeNum, tubeNum);
			optionCount = document.forms[formName].tubeList.length
			document.forms[formName].tubeList.options[optionCount] = newOption;
			
			//Remove the selected tube from the main tube list
			document.forms[formName].tubeOrder.options[indexNum] = null;
		}
		
		//-----Sort the main list when adding an element back in------
		//Create temporay array with the main tube list contents
		var tempArray = new Array(document.forms[formName].tubeList.length);
		for (j=0; j < tempArray.length; j++)
		{
			tempArray[j] = document.forms[formName].tubeList[j].value;
		}
		
		//Sort the array using the sort method
		tempArray.sort(sortMethod);
		
		for (q=0; q < tempArray.length; q++)
		{
			document.forms[formName].tubeList[q] = new Option(tempArray[q], tempArray[q]);
		}
		
		//Create temporay array with the ordered contents to refresh order list
		var tempOrderArray = new Array(document.forms[formName].tubeOrder.length);
		for (j=0; j < tempOrderArray.length; j++)
		{
			tempOrderArray[j] = document.forms[formName].tubeOrder[j].value;
		}
		
		document.forms[formName].tubeOrderList.value = ""
		
		for (q=0; q < tempOrderArray.length; q++)
		{
			document.forms[formName].tubeOrderList.value = document.forms[formName].tubeOrderList.value + tempOrderArray[q] + ",";			
		}
}


/*------------------------------------------------
FUNCTION TO_ORDERTUBES:
This function allows a tube to moved within the 
tube order list. The function will upfate the
tube order list and the tube order select.
	
Input:
direction - direction to be moved
formName - name of the form
------------------------------------------------*/
function to_orderTubes(direction, formName)
{
	//Get the value selected in the tube order list
	tubeNum = document.forms[formName].tubeOrder.value;
	
	//Verify a value was selected
	if ((tubeNum == "") || (tubeNum == null))
	{
		alert('Please select a tube to be moved in the tube order list')
	}
	else
	{
		//Create temporay array with the tube order select contents
		var tempArray = new Array(document.forms[formName].tubeOrder.length);
		for (j=0; j < tempArray.length; j++)
		{
			tempArray[j] = document.forms[formName].tubeOrder.options[j].value;
		}
		
		//Get the current loction of the selected tube
		selectLoc = document.forms[formName].tubeOrder.selectedIndex;
		
		//Move Up (do not process if selectLoc is at zero)
		if ((direction == 'up') && !(selectLoc == 0))
		{
			//Store selected tube value and above tube value
			tempValue1 = tempArray[selectLoc];
			tempValue2 = tempArray[selectLoc - 1];
			
			//Swap the values
			tempArray[selectLoc] = tempValue2;
			tempArray[selectLoc-1] = tempValue1
			
			//Reset the tube order list
			document.forms[formName].tubeOrderList.value = ""
			
			//Output the reordered list in the ordered tube select and tube list
			for (q=0; q < tempArray.length; q++)
			{
				document.forms[formName].tubeOrder.options[q] = new Option(tempArray[q], tempArray[q]);
				document.forms[formName].tubeOrderList.value = document.forms[formName].tubeOrderList.value + tempArray[q] + ",";
			}
			
			//Set the focus to the selected tube
			document.forms[formName].tubeOrder.focus();
			document.forms[formName].tubeOrder.selectedIndex = selectLoc - 1;
			
		}
		
		//Move Down (do not move down if the selected element is the last in list
		else if ((direction == 'down') && !(selectLoc == tempArray.length-1))
		{
			//Store selected tube value and above tube value
			tempValue1 = tempArray[selectLoc];
			tempValue2 = tempArray[selectLoc + 1];
			
			//Swap the values
			tempArray[selectLoc] = tempValue2;
			tempArray[selectLoc + 1] = tempValue1
			
			//Reset the tube order list
			document.forms[formName].tubeOrderList.value = ""
			
			//Output the reordered list in the ordered tube select and tube list
			for (q=0; q < tempArray.length; q++)
			{
				document.forms[formName].tubeOrder.options[q] = new Option(tempArray[q], tempArray[q]);
				document.forms[formName].tubeOrderList.value = document.forms[formName].tubeOrderList.value + tempArray[q] + ",";
			}
			
			//Set the focus to the selected tube
			document.forms[formName].tubeOrder.focus();
			document.forms[formName].tubeOrder.selectedIndex = selectLoc + 1;
		}	
	}
}


/*------------------------------------------------
FUNCTION TO_KEEPORDER:
Function to automate keeping tubes in numerical
order for 	
Input:
formName - name of the form for the selected element
------------------------------------------------*/
function to_keepOrder(formName)
{
	document.forms[formName].tubeOrder.length = 0;
	document.forms[formName].tubeList.length= 0;
	document.forms[formName].tubeOrderList.value = ""
	
	for (q=0; q < tubeListSize; q++)
	{
		document.forms[formName].tubeOrder.options[q] = new Option(q+1, q+1);
		document.forms[formName].tubeOrderList.value = document.forms[formName].tubeOrderList.value + (q+1) + ",";
	}
}


/*------------------------------------------------
FUNCTION VALIDATETUBECOUNT:
This function will validate that all tubes have
been properly assigned to a list. Used in both
assigning tubes to pots and assigning tube order.
	
Input:
formName - name of the form for the selected element
------------------------------------------------*/
function validateTubeCount(formName)
{
	var validCount = true;
	
	//Get the tube count in the main tube list
	var tubeCount = document.forms[formName].tubeList.length
	
	//If there are any tubes in the tube list alert
	if (tubeCount > 0)
	{
		alert('All tubes must be assigned');
		validCount = false;
	}
	
	if (validCount)
	{
		//All tubes are assigned
		document.forms[formName].submit();		
	}
	else
	{
		document.forms[formName].tubeList.focus();
	}
	
}


/*------------------------------------------------
FUNCTION RESETTUBES:
Manually clears all input elements in the form	
Input:
formName - name of the form for the selected element
------------------------------------------------*/
function resetTubes(formName)
{
	//Reset the tube order select and tube order list
	for (i=0; i < document.forms[formName].length; i++)
	{
		document.forms[formName].elements[i].length = '';
	}
	
	//Reset the main tube list to contain all tubes
	for (i=0; i < tubeListSize; i++)
	{
		document.forms[formName].tubeList.options[i] = new Option(i+1, i+1);
	}
	
	document.forms[formName].reset();	
	
}


/*------------------------------------------------
FUNCTION RESETSAMPLES:
Manually clears all input elements in the form	
Input:
formName - name of the form for the selected element
------------------------------------------------*/
function resetSamples(formName, samples)
{
	
	//Reset the tube order select and tube order list
	for (i=0; i < document.forms[formName].length; i++)
	{
		document.forms[formName].elements[i].length = '';
		
		alert('element name: ' + document.forms[formName].elements[i].name);
		
		if (document.forms[formName].elements[i].name == txtPotList) {
			document.forms[formName].elements[i].value='';
		}
		
		if (document.forms[formName].elements[i].name == txtTubeorderList) {
			document.forms[formName].elements[i].value='';
		}
	}
	
	
	//Reset the main tube list to contain all tubes
	for (i=0; i < tubeListSize; i++)
	{
		document.forms[formName].tubeList.options[i] = new Option(samples[i],samples[i]);
	}
	
	document.forms[formName].reset();	
	
}


/*------------------------------------------------
FUNCTION SHOWPREVIEW:
This function displays a preview of the selected
tray type.
	
Input:
value - corresponding id of the tray type
------------------------------------------------*/
function showPreview(value)
	{
	
		for (i=0; i < jsTrayIDs.length; i++)
		{
			if (jsTrayIDs[i] == value)
			{
				//show
				document.getElementById(jsTrayIDs[i]).style.display = '';
			}
			else
			{
				//hide
				document.getElementById(jsTrayIDs[i]).style.display = 'none';
			}
		}
		
	}


/*------------------------------------------------
FUNCTION RICEVALIDATETUBESETINFO:
Function validates the information submitted
about the tubeset and submits the form if all input
required input is valid.

Called when creating a new tubeset

Input:
formName - name of the form for the selected element
------------------------------------------------*/
function riceValidateTubesetInfo(formName)
{
	validInput = true;
	
	//Function to Create Form Field Object
	//Purpose: Maintain consistency in referencing names and titles of fields
	function objFormField(name, title)
	{
		//Name of the form field (internal use)
		this.name = name;
		//Title of the form field (user output)	
		this.title = title;
	}

	tubesetName = new objFormField(txtTubesetName, 'tubeset name');
	tubeCount = new objFormField(txtTubeCount, 'tube count');
	//sampleCount = new objFormField(txtSampleCount, 'sample count');
	//blankCount = new objFormField(txtBlankCount, 'blank count');
	//extCalCount = new objFormField(txtExtCalCount, 'external calibration count');
	//stdRefCount = new objFormField(txtStdRefCount, 'standard reference count');	
	
	//Check if tray name is null or empty string
	if ((inputExists(document.forms[formName].elements[tubesetName.name].value, tubesetName.name, tubesetName.title, formName)))
	{
		for (i=0; i < jsTubeNames.length; i++)
		{
			if (jsTubeNames[i] == document.forms[formName].elements[tubesetName.name].value)
			{
				alert("Tubeset name already exists, please insert another");
				document.forms[formName].elements[tubesetName.name].focus();
				validInput = false;
			}
		}
	} else {
		validInput = false;
	}
	
	//Check if number of samples is not null, an integer, and greater than zero
	if (validInput) {
	if (inputExists(document.forms[formName].elements[tubeCount.name].value, tubeCount.name, tubeCount.title, formName)) {
		if (isInteger(document.forms[formName].elements[tubeCount.name].value, tubeCount.name, tubeCount.title, formName)) {
		} else {
			validInput = false;
	}} else {
		validInput = false;
	}}
	
	//Check if number of non-samples is not null, an integer, and greater than zero
	if (validInput) {
	  var sNonSampleCount = document.getElementById('nonSampleCount').value;	  
	  var sSampleCount = document.getElementById('tubeCount').value;
	  
	  if(sNonSampleCount != null && sSampleCount != null){
	    if (!isIntegerValue(sNonSampleCount)){ 
	      validInput = false;
	      alert('Number of Non-Sample Tubes should be a number.');
	    }
	    
	    if (!isIntegerValue(sSampleCount)){ 
	      validInput = false;
	      alert('Number of Tubes should be a number.');
	    }
	    
	    if(validInput){
	      if(parseInt(sSampleCount) <= parseInt(sNonSampleCount)){
	        validInput = false;
	        alert('Number of Tubes should be greater than Non-Sample.');
	      }
	    }
	  }//end != null
	}//end validInput
	
	//http://www.codetoad.com/javascript/isnumeric.asp
	function isIntegerValue(sText){
	  var ValidChars = "0123456789";
	  var IsNumber=true;
	  var Char;
	 
	  for (i = 0; i < sText.length && IsNumber == true; i++){ 
	    Char = sText.charAt(i); 
	    if (ValidChars.indexOf(Char) == -1){
	      IsNumber = false;
	    }
	  }
	  return IsNumber;
	}

	
	if (validInput)
	{
		//All fields valid
		document.forms[formName].submit();
	}
	
		
}


/*------------------------------------------------
FUNCTION VALIDATETUBESETINFO:
Function validates the information submitted
about the tubeset and submits the form if all input
required input is valid.

Called when creating a new tubeset

Input:
formName - name of the form for the selected element
------------------------------------------------*/
function validateTubesetInfo(formName)
{
	validInput = true;
	
	//Function to Create Form Field Object
	//Purpose: Maintain consistency in referencing names and titles of fields
	function objFormField(name, title)
	{
		//Name of the form field (internal use)
		this.name = name;
		//Title of the form field (user output)	
		this.title = title;
	}

	tubesetName = new objFormField(txtTubesetName, 'tubeset name');
	tubeCount = new objFormField(txtTubeCount, 'tube count');
	//sampleCount = new objFormField(txtSampleCount, 'sample count');
	//blankCount = new objFormField(txtBlankCount, 'blank count');
	//extCalCount = new objFormField(txtExtCalCount, 'external calibration count');
	//stdRefCount = new objFormField(txtStdRefCount, 'standard reference count');	
	
	//Check if tray name is null or empty string
	if ((inputExists(document.forms[formName].elements[tubesetName.name].value, tubesetName.name, tubesetName.title, formName)))
	{
		for (i=0; i < jsTubeNames.length; i++)
		{
			if (jsTubeNames[i] == document.forms[formName].elements[tubesetName.name].value)
			{
				alert("Tubeset name already exists, please insert another");
				document.forms[formName].elements[tubesetName.name].focus();
				validInput = false;
			}
		}
	} else {
		validInput = false;
	}
	
	//Check if number of samples is not null, an integer, and greater than zero
	if (validInput) {
	if (inputExists(document.forms[formName].elements[tubeCount.name].value, tubeCount.name, tubeCount.title, formName)) {
		if (isInteger(document.forms[formName].elements[tubeCount.name].value, tubeCount.name, tubeCount.title, formName)) {
		} else {
			validInput = false;
	}} else {
		validInput = false;
	}}
	
	//Check if number of samples is not null, an integer, and greater than zero
	//if (validInput) {
	//if (inputExists(document.forms[formName].elements[sampleCount.name].value, sampleCount.name, sampleCount.title, formName)) {
	//	if (isInteger(document.forms[formName].elements[sampleCount.name].value, sampleCount.name, sampleCount.title, formName)) {
	//	} else {
	//		validInput = false;
	//}} else {
	//	validInput = false;
	//}}
	
	//Check if number of blanks cases is not null, and an integer
	//if (validInput) {
	//if (inputExists(document.forms[formName].elements[blankCount.name].value,blankCount.name, blankCount.title, formName)) {
	//	if (isInteger(document.forms[formName].elements[blankCount.name].value,blankCount.name, blankCount.title, formName)) {
	//		//Input is valid
	//	} else {
	//		validInput = false;
	//}} else {
	//	validInput = false;
	//}}
	
	//Check if number of standard reference cases is not null, and an integer
	//if (validInput) {
	//if (inputExists(document.forms[formName].elements[stdRefCount.name].value,stdRefCount.name, stdRefCount.title, formName)) {
	//	if (isInteger(document.forms[formName].elements[stdRefCount.name].value,stdRefCount.name, stdRefCount.title, formName)) {
	//		//Input is valid
	//	} else {
	//		validInput = false;
	//}} else {
	//	validInput = false;
	//}}
	
	//Check if number of external calibration cases is not null, and an integer
	//if (validInput) {
	//if (inputExists(document.forms[formName].elements[extCalCount.name].value,extCalCount.name, extCalCount.title, formName)) {
	//	if (isInteger(document.forms[formName].elements[extCalCount.name].value,extCalCount.name, extCalCount.title, formName)) {
	//		//Input is valid
	//	} else {
	//		validInput = false;
	//}} else {
	//	validInput = false;
	//}}
	//
	if (validInput)
	{
		//All fields valid
		document.forms[formName].submit();
	}
	
		
}


/*------------------------------------------------
FUNCTION VALIDATETUBESETINFO:
Function validates the information submitted
about the tubeset and submits the form if all input
required input is valid.

Called when creating a new tubeset

Input:
formName - name of the form for the selected element
------------------------------------------------*/
function validateEditTubeset(formName)
{
		validInput = true;
	
	//Function to Create Form Field Object
	//Purpose: Maintain consistency in referencing names and titles of fields
	function objFormField(name, title)
	{
		//Name of the form field (internal use)
		this.name = name;
		//Title of the form field (user output)	
		this.title = title;
	}

	tubesetName = new objFormField(txtTubesetName, 'tubeset name');
	sampleCount = new objFormField(txtSampleCount, 'sample count');
	blankCount = new objFormField(txtBlankCount, 'blank count');
	extCalCount = new objFormField(txtExtCalCount, 'external calibration count');
	stdRefCount = new objFormField(txtStdRefCount, 'standard reference count');
	
	//Check if tray name is null or empty string
	if ((inputExists(document.forms[formName].elements[tubesetName.name].value, tubesetName.name, tubesetName.title, formName))) {
		for (i=0; i < jsTubeNames.length; i++)
		{
			if(orgTubeName != jsTubeNames[i])
			{
				if (jsTubeNames[i] == document.forms[formName].elements[tubesetName.name].value)
				{
					alert("Tubeset name already exists, please insert another");
					document.forms[formName].elements[tubesetName.name].focus();
					validInput = false;
				}
			}
		}
	} else {
		validInput = false;
	}
	
	//Check if number of samples is not null, an integer, and greater than zero
	if (validInput) {
	if (inputExists(document.forms[formName].elements[sampleCount.name].value, sampleCount.name, sampleCount.title, formName)) {
		if (isInteger(document.forms[formName].elements[sampleCount.name].value, sampleCount.name, sampleCount.title, formName)) {
		} else {
			validInput = false;
	}} else {
		validInput = false;
	}}
	
	//Check if number of blanks cases is not null, and an integer
	if (validInput) {
	if (inputExists(document.forms[formName].elements[blankCount.name].value,blankCount.name, blankCount.title, formName)) {
		if (isInteger(document.forms[formName].elements[blankCount.name].value,blankCount.name, blankCount.title, formName)) {
			//Input is valid
		} else {
			validInput = false;
	}} else {
		validInput = false;
	}}
	
	//Check if number of standard reference cases is not null, and an integer
	if (validInput) {
	if (inputExists(document.forms[formName].elements[stdRefCount.name].value,stdRefCount.name, stdRefCount.title, formName)) {
		if (isInteger(document.forms[formName].elements[stdRefCount.name].value,stdRefCount.name, stdRefCount.title, formName)) {
			//Input is valid
		} else {
			validInput = false;
	}} else {
		validInput = false;
	}}
	
	//Check if number of external calibration cases is not null, and an integer
	if (validInput) {
	if (inputExists(document.forms[formName].elements[extCalCount.name].value,extCalCount.name, extCalCount.title, formName)) {
		if (isInteger(document.forms[formName].elements[extCalCount.name].value,extCalCount.name, extCalCount.title, formName)) {
			//Input is valid
		} else {
			validInput = false;
	}} else {
		validInput = false;
	}}
	
		if (validInput)
	{
		//All fields valid
		document.forms[formName].submit();
	}	
}

function riceValidateTubeCount(formName){
  var validCount = true;
	
  //Get the tube count in the main tube list
  var tubeCount = document.forms[formName].tubeList.length;
	
  //If there are any tubes in the tube list alert
  if (tubeCount > 0){
    alert('All non-sample tubes must be assigned');
    validCount = false;
  }
	
  if (validCount){
    //All tubes are assigned
    document.forms[formName].submit();		
  }else{
    document.forms[formName].tubeList.focus();
  }
}