function valid(f) 
{
	if (!/^\d*$/.test(f.value))
		f.value = f.value.replace(/[^\d]/g,"");
}

function validateForm()
{
	var form = document.forms["microwireForm"];

	var fname = form["FName"].value;
	if (fname==null || fname=="")
	{
		alert("Please enter your first name");
		form["FName"].focus();
		return false;
	}
	var lname = document.forms["microwireForm"]["LName"].value;
	if (lname==null || lname=="")
	{
		alert("Please enter your last name");
		form["LName"].focus();
		return false;
	}
	var email = form["Email"].value;
	var atpos=email.indexOf("@");
	var dotpos=email.lastIndexOf(".");
	if (atpos<1 || dotpos<atpos+2 || dotpos+2>=email.length)
	{
		alert("Please enter a valid email address");
		form["Email"].focus();
	    return false;
	}

	var numRows = 0;
	for( i = 0; i < form.NumRows.length; i++ )
		if( form.NumRows[i].checked == true )
			numRows = form.NumRows[i].value;
	if ( numRows < 1 || numRows > 8)
	{
		alert("Invalid number of rows!");
		return false;
	}

	// Enable fields so values are passed via POST
	for (i = 1; i <= numRows; i++)
	{
			var curRow = form.elements["Row" + i + "[]"]
			for (j = 0; j < curRow.length; j++)			
				curRow[j].disabled = false;			
	}


	return true;
}

function validateRowEntry(array, chan)
{
	if ( chan > array.length )
		return false;

	var val = array[chan].value;
	var newVal = val;
	var isValid = false;
	if ( isNaN(val) ) // Test non-numeric input
	{
		if ( (val == "S") || (val == "s") )
		{
			if (chan > 0)  // First row cannot be same as previous
			{
				var prevVal = array[chan - 1].value;
				if ( !isNaN( prevVal ) )
				{
					isValid = true;
					newVal = parseFloat(prevVal).toFixed(1);
				}				
			}
		}
		else if ( (val == "O") || (val == "o") )
		{
			isValid = true;
			newVal = "O";
		}
	}
	else if ( val == 0 )
	{		
		isValid = true;
		newVal = "O";
	}
	else if ( (val > 0) && (val <= 15) )
	{	
		isValid = true;
		newVal = parseFloat(val).toFixed(1);
	}

	if ( isValid )
	{
		array[chan].value = newVal;
		if (newVal >= 5.0)  // Display PEG message for tip lengths > 5 mm
		{

			var element = document.getElementById("pegNote");
			if( element.style.display == "none" ) 
				element.style.display = "block";

		}
	}
	else
	{
		alert("Invalid row entry: " + val);
		array[chan].value = "2.0";
		array[chan].focus();
	}
	return isValid;
}

function toggleSameLength(obj, array)
{
	var isChecked = obj.checked;
	if ( isChecked )
	{
		var val = parseFloat(array[0].value).toFixed(1);
		for (i = 1; i < array.length; i++)
		{
			array[i].value = val;
			array[i].disabled = true;
		}	
	}
	else
		for (i = 0; i < array.length; i++)
			array[i].disabled = false;
}

function toggleUseRowForAll(form)
{
	var isChecked = form.elements["useSameRowConfig"].checked;
	var numRows = 0;
	for( i = 0; i < form.NumRows.length; i++ )
		if( form.NumRows[i].checked == true )
			numRows = form.NumRows[i].value;
	if (numRows == 0)
		return false;

	if (numRows == 1)
	{
		form.elements["useSameRowConfig"].disabled = true;
		return true;
	}
	else
		form.elements["useSameRowConfig"].disabled = false;

	var row1 = form.elements["Row1[]"];
	if (row1.length != 8)
		return false;

	if (isChecked)
	{
		for (i = 2; i <= numRows; i++)
		{
			var curRow = form.elements["Row" + i + "[]"]
			var curRowSameLength = form.elements["sameLength" + i];
			curRowSameLength.disabled = true;
			for (j = 0; j < curRow.length; j++)
			{
				curRow[j].value = row1[j].value;
				curRow[j].disabled = true;
			}
		}
	}
	else
	{
		for (i = 2; i <= numRows; i++)
		{
			var curRow = form.elements["Row" + i + "[]"]
			var curRowSameLength = form.elements["sameLength" + i];
			curRowSameLength.disabled = false;
			for (j = 0; j < curRow.length; j++)
			{
				curRow[j].disabled = false;
				if (curRowSameLength.checked)
					break;
			}
		}
	}
}

function resetOmneticsForm()
{
	document.microwireForm.ElectrodeSpacing[1].checked = true;
	document.microwireForm.RowSeparation[0].checked = true;
	document.microwireForm.TipAngle[0].checked = true;
	document.microwireForm.Ground.checked = false;
	document.microwireForm.Reference.checked = false;
	document.microwireForm.WireDiameter[0].checked = true;
	document.microwireForm.Quantity.value = 1;

	var lands = document.microwireForm.elements["lands[]"];
	for (i = 0; i < 6; i++)
		lands[i].checked = false;		

	document.microwireForm.useSameRowConfig.checked = true;

	document.microwireForm.sameLength1.checked = true;
	document.microwireForm.sameLength2.checked = true;
	document.microwireForm.sameLength3.checked = true;
	document.microwireForm.sameLength4.checked = true;

	var row1 = document.microwireForm.elements["Row1[]"];
	row1[0].value = "2.0";
	var row2 = document.microwireForm.elements["Row2[]"];
	row2[0].value = "2.0";
	var row3 = document.microwireForm.elements["Row3[]"];
	row3[0].value = "2.0";
	var row4 = document.microwireForm.elements["Row4[]"];
	row4[0].value = "2.0";

	toggleSameLength(document.microwireForm.sameLength1, row1);
	toggleSameLength(document.microwireForm.sameLength2, row2);
	toggleSameLength(document.microwireForm.sameLength2, row3);
	toggleSameLength(document.microwireForm.sameLength2, row4);
	
	var pegNote = document.getElementById('pegNote');	
	pegNote.style.display = "none";

	toggleUseRowForAll(document.microwireForm);

	document.microwireForm.NumRows[1].checked = true;
	document.microwireForm.NumRows.value = 2;
	toggleElements(document.microwireForm, ['Layer1', 'Layer2', 'Layer3', 'Layer4'], 2);
}

function resetZIFForm()
{
	
	document.microwireForm.ElectrodeType[0].checked = true;
	document.microwireForm.ElectrodeSpacing[0].checked = true;
	document.microwireForm.RowSeparation[0].checked = true;
	document.microwireForm.TipAngle[0].checked = true;
	document.microwireForm.WireDiameter[1].checked = true;
	document.microwireForm.GroundWires[0].checked = true;
	document.microwireForm.Quantity.value = 1;	

	document.microwireForm.sameLength1.checked = true;
	document.microwireForm.sameLength2.checked = true;
	document.microwireForm.sameLength3.checked = true;
	document.microwireForm.sameLength4.checked = true;
	document.microwireForm.sameLength5.checked = true;
	document.microwireForm.sameLength6.checked = true;
	document.microwireForm.sameLength7.checked = true;
	document.microwireForm.sameLength8.checked = true;
	
	var row1 = document.microwireForm.elements["Row1[]"];
	row1[0].value = "2.0";
	var row2 = document.microwireForm.elements["Row2[]"];
	row2[0].value = "2.0";
	var row3 = document.microwireForm.elements["Row3[]"];
	row3[0].value = "2.0";
	var row4 = document.microwireForm.elements["Row4[]"];
	row4[0].value = "2.0";
	var row5 = document.microwireForm.elements["Row5[]"];
	row5[0].value = "2.0";
	var row6 = document.microwireForm.elements["Row6[]"];
	row6[0].value = "2.0";
	var row7 = document.microwireForm.elements["Row7[]"];
	row7[0].value = "2.0";
	var row8 = document.microwireForm.elements["Row8[]"];
	row8[0].value = "2.0";

	toggleSameLength(document.microwireForm.sameLength1, row1);
	toggleSameLength(document.microwireForm.sameLength2, row2);
	toggleSameLength(document.microwireForm.sameLength1, row3);
	toggleSameLength(document.microwireForm.sameLength2, row4);
	toggleSameLength(document.microwireForm.sameLength1, row5);
	toggleSameLength(document.microwireForm.sameLength2, row6);
	toggleSameLength(document.microwireForm.sameLength1, row7);
	toggleSameLength(document.microwireForm.sameLength2, row8);

	document.microwireForm.useSameRowConfig.checked = true;
	toggleUseRowForAll(document.microwireForm);

	document.microwireForm.NumRows[0].checked = true;
	document.microwireForm.NumRows.value = 2;
	toggleElements(document.microwireForm, ['Layer1', 'Layer2', 'Layer3', 'Layer4', 'Layer5', 'Layer6', 'Layer7', 'Layer8'], 2);

	var pegNote = document.getElementById('pegNote');	
	pegNote.style.display = "none";

}

function toggleElements(obj, array, numOn)
{
	var elements = new Array();
	for (i = 0; i < array.length; i++ )
	{
		elements[i] = document.getElementById(array[i]);
		if( i == 0 && !elements[0] ) 
			return true; 
		(i < numOn) ? elements[i].style.display="block" : elements[i].style.display="none";
	}
	return true;
}

function toggleWires(obj, a, b)
{
  var one=document.getElementById(a);
  var two=document.getElementById(b);
  if((obj.checked) && (obj.value == "Plain Shroud") && (!one.checked) && (!two.checked)){
	  one.click();
  }
  if((obj.checked) && (obj.value == "Footed Shroud") && (!one.checked) && (!two.checked)){
	  one.click();
  }
  if((obj.checked) && (obj.value == "No Shroud") && (!one.checked) && (two.checked)){
	  two.click();
  }
  if((obj.checked) && (obj.value == "No Shroud") && (one.checked) && (!two.checked)){
	  one.click();
  }

  if((obj.checked) && (obj.value == "No Shroud") && (one.checked) && (two.checked)){
	  one.click();
	  two.click();
  }
  return true;
}

function toggleFlexRibbon(obj) 
{
	if(((obj.checked) && (obj.value == "Flex Ribbon")) || (document.microwireForm.ElectrodeType[1].checked))
		document.microwireForm.ElectrodeSpacing[0].checked = true;
	return true;
}

function toggleSpacing(obj) {
	if(document.microwireForm.NumRows[2].checked)
	{
		document.microwireForm.ElectrodeSpacing[1].checked = true;
		document.microwireForm.ElectrodeType[0].checked = true;
	}
}

function toggleFlexSite() 
{
	if((document.microwireForm.NumRows[1].checked) && (document.microwireForm.ElectrodeType[1].checked))
	 	document.getElementById('Flex').style.display="block";
	else
		document.getElementById('Flex').style.display="none";
}

