


/*
 *	Used by: register.php
 *
 *	makes the number of hardware rows drawn on screen match the form variable: numHardwareRowsExposed
 */
function syncHardwareRows()
{
	var max = parseInt( document.registerForm.maxHardwareRows.value );
	var numExposed = parseInt( document.registerForm.numHardwareRowsExposed.value );
	var count = 0;
	
	for( count = 0; count < max; count++ )
	{
		if( count < numExposed )
		{
			document.getElementById( ( "hardwareRow" + count ) ).style.display = "table-row";
		}
		else
		{
			document.getElementById( ( "hardwareRow" + count ) ).style.display = "none";		
		}
	}
}


/*
 *	Used by: register.php
 *
 *	increments form variable: numHardwareRowsExposed and then syncs
 */
function addHardwareRow()
{
	var numExposed = parseInt( document.registerForm.numHardwareRowsExposed.value );
	var max = parseInt( document.registerForm.maxHardwareRows.value );

	if( numExposed < max )
	{
		numExposed++;
		document.registerForm.numHardwareRowsExposed.value = numExposed;
		syncHardwareRows();
	}
	else
	{
		alert( "You may only add up to ten rows." );
	}	
}


/*
 *	Used by: register.php
 *
 *	decrements form variable: numHardwareRowsExposed and then syncs
 */
function removeHardwareRow()
{
	var numExposed = parseInt( document.registerForm.numHardwareRowsExposed.value );
	var max = parseInt( document.registerForm.maxHardwareRows.value );

	if( numExposed > 1 )
	{
		numExposed--;
		document.registerForm.numHardwareRowsExposed.value = numExposed;
		syncHardwareRows();
	}
	else
	{
		alert( "You must keep at least one row." );
	}
}


/*
 *	Used by: register.php
 *
 *	makes the number of interface rows drawn on screen match the form variable: numInterfaceRowsExposed
 */
function syncInterfaceRows()
{
	var max = parseInt( document.registerForm.maxInterfaceRows.value );
	var numExposed = parseInt( document.registerForm.numInterfaceRowsExposed.value );
	var count = 0;
	
	for( count = 0; count < max; count++ )
	{
		if( count < numExposed )
		{
			document.getElementById( ( "interfaceRow" + count ) ).style.display = "table-row";
		}
		else
		{
			document.getElementById( ( "interfaceRow" + count ) ).style.display = "none";		
		}
	}
}


/*
 *	Used by: register.php
 *
 *	increments form variable: numInterfaceRowsExposed and then syncs
 */
function addInterfaceRow()
{
	var numExposed = parseInt( document.registerForm.numInterfaceRowsExposed.value );
	var max = parseInt( document.registerForm.maxInterfaceRows.value );

	if( numExposed < max )
	{
		numExposed++;
		document.registerForm.numInterfaceRowsExposed.value = numExposed;
		syncInterfaceRows();
	}
	else
	{
		alert( "You may only add up to ten rows." );
	}	
}


/*
 *	Used by: register.php
 *
 *	decrements form variable: numInterfaceRowsExposed and then syncs
 */
function removeInterfaceRow()
{
	var numExposed = parseInt( document.registerForm.numInterfaceRowsExposed.value );
	var max = parseInt( document.registerForm.maxInterfaceRows.value );

	if( numExposed > 1 )
	{
		numExposed--;
		document.registerForm.numInterfaceRowsExposed.value = numExposed;
		syncInterfaceRows();
	}
	else
	{
		document.registerForm.ownInterface.checked = false;
		syncInterfaceTable()
	}
	
}


/*
 *	Used by: register.php
 *
 *	toggles display of interface table when checkbox changed
 */
function syncInterfaceTable()
{
	if( document.registerForm.ownInterface.checked )
	{
		document.getElementById( "interfaceSection" ).style.display = "block";
	}
	else
	{
		document.getElementById( "interfaceSection" ).style.display = "none";	
	}
	
	syncInterfaceRows()
}


/*
 *	Used by: register.php
 *
 *	copies email address to user name field
 */
function copyEmailAddress()
{
	if( document.registerForm.userName.value == "" )
	{
		document.registerForm.userName.value = document.registerForm.emailAddress.value;
	}
}


/*
 *	Used by: register.php
 *
 *	validates registration form user data entered
 *
 *	special validation: 
 *		password/confirm
 *		bio length
 *		least one product chosen
 *		at least one computer hardware row entered
 *		every hardware row filled in + int/float
 *		if external hardware, all rows filled in + int/float
 *		NDA == yes
 *		ints in int fields
 */
function validateRegisterForm()
{
	var fieldNames = 
		new Array(
			"emailAddress",
			"firstName",
			"lastName",
			"addressLineOne",
			"city",
			"state",
			"zipCode",
			"country",
			"phoneNumber",
			"background",
			"audioHoursPerMonth",
			"betaHoursPerMonth",
			"userType",
			"preferredOs"
		);
	
	var fieldLabels = 
		new Array(
			" -Email Address\n",
			" -First Name\n",
			" -Last Name\n",
			" -Address Line One\n",
			" -City\n",
			" -State\n",
			" -Zip Code\n",
			" -Country\n",
			" -Phone Number\n",
			" -Background/Biography\n",
			" -Hours spent on audio per month\n",
			" -Hours willing to spend previewing Advanced Releases for BIAS per month\n",
			" -User Type\n",
			" -Preferred Platform\n"
		);
	
	var max = fieldNames.length;
	var fails = 0;
	var errorMessage = "The following field(s) are required:\n\n";
	var returnValue = false;
	
	for( var count = 0; count < max; count++ )
	{
		if( document.registerForm[fieldNames[count]].value == "" )
		{
			fails++;
			errorMessage += fieldLabels[count];
		}
	}
	
	if( fails == 0 )
	{		
		var bio = document.registerForm.background.value;

		if( bio.length > 20 )
		{
			if( 
				document.registerForm.peakList.checked ||
				document.registerForm.sspList.checked ||
				document.registerForm.ssList.checked ||
				document.registerForm.mpsList.checked
			)
			{
				if( validateRegisterFormHardwareProfile() )
				{
					if( validateRegisterFormExternalHardwareProfile() )
					{
						if( validateInt( document.registerForm.audioHoursPerMonth.value ) )
						{
							if( validateInt( document.registerForm.betaHoursPerMonth.value ) )
							{
								returnValue = true;
							}
							else
							{
								alert( "You must enter an integer in the field \"Hours willing to spend Beta Testing for BIAS per month\"." );
								returnValue = false;
							}
						}
						else
						{
							alert( "You must enter an integer in the field \"Hours spent on audio per month\"." );
							returnValue = false;
						}
					}
					else
					{
						returnValue = false;
					}
				}
				else
				{
					returnValue = false;
				}
			}
			else
			{
				alert( "You must choose at least one product under the section \"Which products would you like to receive Advanced Releases for?\"." );
				returnValue = false;
			}
		}
		else
		{
			alert( "You must enter at least 20 characters in the field \"Biography/Background\"." );
			returnValue = false;
		}

	}
	else
	{
		alert( errorMessage )
		returnValue = false;
	}
	
	return returnValue;
}
 

/*
 *	Used by: register.php
 *
 *	validates registration form hardware profile data
 */
function validateRegisterFormHardwareProfile()
{
	var fieldNames =
		new Array(
			"make",
			"model",
			"procs",
			"cpuSpeed",
			"memoryTotal",
			"hddTotal",
			"platform",
			"osVersion"
		);
	
	var fieldLabels = 
		new Array(
			" -Make",
			" -Model",
			" -# CPU",
			" -CPU Speed",
			" -Total RAM",
			" -Total Disk Space",
			" -Platform",
			" -OS Version"
		);

	var maxRows = document.registerForm.numHardwareRowsExposed.value;
	var max = fieldNames.length;
	var fails = 0;
	var errorMessage = "The following field(s) are required in the section \"Computer Hardware Profile\":\n\n";
	var returnValue = false;
	
	for( var countRows = 0; countRows < maxRows; countRows++ )
	{
		for( var count = 0; count < max; count++ )
		{
			if( document.registerForm[(fieldNames[count] + countRows)].value == "" )
			{
				fails++;
				errorMessage += ( fieldLabels[count] + " on Row #" + ( countRows + 1 ) + "\n" );
			}			
		}

		if( ! validateInt( document.registerForm[("procs" + countRows)].value ) )
		{
			fails++;
			errorMessage += ( " -# CPU on Row #" + ( countRows + 1 ) + " must be an integer.\n" );
		}

		if( ! validateFloat( document.registerForm[("cpuSpeed" + countRows)].value ) )
		{
			fails++;
			errorMessage += ( " -CPU Speed on Row #" + ( countRows + 1 ) + " must be a decimal number.\n" );
		}
		
		if( ! validateFloat( document.registerForm[("memoryTotal" + countRows)].value ) )
		{
			fails++;
			errorMessage += ( " -Total RAM on Row #" + ( countRows + 1 ) + " must be an decmial number.\n" );
		}
		
		if( ! validateInt( document.registerForm[("hddTotal" + countRows)].value ) )
		{
			fails++;
			errorMessage += ( " -Total Disk Space on Row #" + ( countRows + 1 ) + " must be an integer.\n" );
		}
	}
	
	if( fails == 0 )
	{
		returnValue = true;
	}
	else
	{
		alert( errorMessage );
		returnValue = false;
	}
	
	return returnValue;
}


/*
 *	Used by: register.php
 *
 *	validates registration form hardware profile data
 */
function validateRegisterFormExternalHardwareProfile()
{
	var fieldNames = 
		new Array(
			"interfaceMake",
			"interfaceModel",
			"interfaceType"
		);
	
	var fieldLabels = 
		new Array(
			" -Make",
			" -Model",
			" -Interface"
		);
	
	var maxRows = document.registerForm.numInterfaceRowsExposed.value;
	var max = fieldNames.length;
	var fails = 0;
	var errorMessage = "The following field(s) are required in the section \"External Audio Hardware Profile\":\n\n";
	var returnValue = false;
	
	if( ! document.registerForm.ownInterface.checked )
	{
		returnValue = true;
	}
	else
	{
		for( var countRows = 0; countRows < maxRows; countRows++ )
		{
			for( var count = 0; count < max; count++ )
			{
				if( document.registerForm[(fieldNames[count] + countRows)].value == "" )			
				{
					fails++;
					errorMessage += ( fieldLabels[count] + " on Row #" + ( countRows + 1 ) + "\n" );
				}
			}
		}
	}
	
	if( fails > 0 )
	{
		returnValue = false;
		alert( errorMessage );
	}
	else
	{
		returnValue = true;
	}
	
	return returnValue;
}



/* see whether or not given is integer */
function validateInt( theInt )
{
	var returnValue = false;
	var fails = 0;
	var intAsString = new String( theInt );
	var max = intAsString.length;
	
	for( var count = 0; count < max; count++ )
	{
		if( 
			! (
				( intAsString.charAt( count ) >= 0 ) &&
				( intAsString.charAt( count ) <= 9 )
			)
		)
		{
			fails++;
		}
	}
	
	if( fails != 0 )
	{
		returnValue = false;
	}
	else
	{
		returnValue = true;
	}
	
	return returnValue;
}
 

function validateFloat( theFloat )
{
	var returnValue = false;
	var fails = 0;
	var floatAsString = new String( theFloat );
	var max = floatAsString.length;
	
	for( var count = 0; count < max; count++ )
	{
		if( 
			! (
				(
					( floatAsString.charAt( count ) >= 0 ) &&
					( floatAsString.charAt( count ) <= 9 )
				) ||
				( floatAsString.charAt( count ) == "." )
			)
		)
		{
			fails++;
		}
	}
	
	if( fails != 0 )
	{
		returnValue = false;
	}
	else
	{
		returnValue = true;
	}
	
	return returnValue;
}





