//////////////////////////////////////////////////////////////////////
// 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 (!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();
}
