//////////////////////////////////////////////////////////////////////
// This file contains JavaScript functions which are unique to the
// associated web page.
//////////////////////////////////////////////////////////////////////

// holds error text
var errorText;

//////////////////////////////////////////////////////////////////
// Validate
//////////////////////////////////////////////////////////////////
function validateForm(inForm)
{ 
	var valid = true;
	errorText = '';
	var validEmail = false;

	if (isBlank(inForm.senderName.value))
	{
		errorText += "Please enter your name (so the recipients recognize the email)\n";
	}

	if (isValidEmail($('aaa').value))
	{
		validEmail = true;
	}
	if (isValidEmail($('bbb').value))
	{
		validEmail = true;
	}
	if (isValidEmail($('ccc').value))
	{
		validEmail = true;
	}
	if (isValidEmail($('ddd').value))
	{
		validEmail = true;
	}
	if (isValidEmail($('eee').value))
	{
		validEmail = true;
	}
	if (isValidEmail($('fff').value))
	{
		validEmail = true;
	}
	if (isValidEmail($('ggg').value))
	{
		validEmail = true;
	}
	
	// if we have at least 1 valid email, make sure all valid emails have a matching name
	if (validEmail)
	{
		// some forms don't contains first names, skip this if we have none
		if ($('firstNames1'))
		{
			var missingFirstName = false;
			
			if ( (isValidEmail($('aaa').value)) && (isBlank($('firstNames1').value)) )
			{
				errorText += "Please enter a name for each email address you entered.\n";
				missingFirstName = true;
			}
			if ( (!missingFirstName) && (isValidEmail($('bbb').value)) && (isBlank($('firstNames2').value)) )
			{
				errorText += "Please enter a name for each email address you entered.\n";
				missingFirstName = true;
			}
			if ( (!missingFirstName) && (isValidEmail($('ccc').value)) && (isBlank($('firstNames3').value)) )
			{
				errorText += "Please enter a name for each email address you entered.\n";
				missingFirstName = true;
			}
			if ( (!missingFirstName) && (isValidEmail($('ddd').value)) && (isBlank($('firstNames4').value)) )
			{
				errorText += "Please enter a name for each email address you entered.\n";
				missingFirstName = true;
			}
			if ( (!missingFirstName) && (isValidEmail($('eee').value)) && (isBlank($('firstNames5').value)) )
			{
				errorText += "Please enter a name for each email address you entered.\n";
				missingFirstName = true;
			}
			if ( (!missingFirstName) && (isValidEmail($('fff').value)) && (isBlank($('firstNames6').value)) )
			{
				errorText += "Please enter a name for each email address you entered.\n";
				missingFirstName = true;
			}
			if ( (!missingFirstName) && (isValidEmail($('ggg').value)) && (isBlank($('firstNames7').value)) )
			{
				errorText += "Please enter a name for each email address you entered.\n";
				missingFirstName = true;
			}
		}
	}
	
	if (!validEmail)
	{
		errorText += "Please enter at least one valid email address.\n";
	}
	
	if (errorText != '')
	{
		valid = false;
	}

	return valid;
}

//////////////////////////////////////////////////////////////////
// Validate
//////////////////////////////////////////////////////////////////
function validateForm_OLD2(inForm)
{ 
	var valid = true;
	errorText = '';
	var validEmail = false;
	var numberOfEmailAddresses = 7;

	if (isBlank(inForm.senderName.value))
	{
		errorText += "Please enter your name (so the recipients recognize the email)\n";
	}

	for (var index=1; index <= numberOfEmailAddresses; ++index)
	{
		if (isValidEmail($('sserddaLiame'+index).value))
		{
			validEmail = true;
			break;
		}
	}
	
	// if we have at least 1 valid email, make sure all valid emails have a matching name
	if (validEmail)
	{
		// some forms don't contains first names, skip this if we have none
		if ($('firstNames1'))
		{
			for (var index=1; index <= numberOfEmailAddresses; ++index)
			{
				if ( (isValidEmail($('sserddaLiame'+index).value)) && (isBlank($('firstNames'+index).value)) )
				{
					errorText += "Please enter a name for each email address you entered.\n";
					break;
				}
			}			
		}
	}
	
	if (!validEmail)
	{
		errorText += "Please enter at least one valid email address.\n";
	}
	
	if (errorText != '')
	{
		valid = false;
	}

	return valid;
}

//////////////////////////////////////////////////////////////////
// Validate
//////////////////////////////////////////////////////////////////
function validateForm_OLD(inForm)
{ 
	var valid = true;
	errorText = '';
	var validEmail = false;

	if (isBlank(inForm.senderName.value))
	{
		errorText += "Please enter your name (so the recipients recognize the email)\n";
	}

	if (!inForm.emailAddresses.length)
	{
		if (isValidEmail(inForm.emailAddresses.value))
		{
			validEmail = true;
		}
	}
	
	if (inForm.emailAddresses.length)
	{
		for (var index=0,ln=inForm.emailAddresses.length; index < ln; ++index)
		{
			if (isValidEmail(inForm.emailAddresses[index].value))
			{
				validEmail = true;
				break;
			}
		}
		
		// if we have at least 1 valid email, make sure all valid emails have a matching name
		if (validEmail)
		{
			if (inForm.firstNames)
			{
				for (var index=0,ln=inForm.firstNames.length; index < ln; ++index)
				{
					if ( (isValidEmail(inForm.emailAddresses[index].value)) && (isBlank(inForm.firstNames[index].value)) )
					{
						errorText += "Please enter a name for each email address you entered.\n";
						break;
					}
				}			
			}
		}
	}

	if (!validEmail)
	{
		errorText += "Please enter at least one valid email address.\n";
	}
	
	if (errorText != '')
	{
		valid = false;
	}

	return valid;
}

//////////////////////////////////////////////////////////////////
// sendEmail
//////////////////////////////////////////////////////////////////
function sendEmail(inForm)
{ 
	// if the form is NOT invalid
	if ( !validateForm(inForm) )
	{
		// slap them around...
		alert(errorText);
		return false;
	}

	$('idSend').value = "Sending...";
	$('idSend').disabled = true;
	
	inForm.submit();
}

