/*
Designed, maintained, and copyrighted (c) by:
     Capitalism's Gravediggers (r)
     www.Capitalisms-Gravediggers.com
     www.CapitalismsGravediggers.com
*/
//-----------------------------------------------------------------------------
// Validate the contact form data before sending to server.
function validateContactForm(form)
{
	// Clear the clear field.
	form.contact_clear.value = '';
	
	// Form data to check.
	var cName = form.contact_name.value;
	var cEmail = form.contact_email.value;
	var cSubject = form.contact_email.value;
	var cComments = form.contact_comments.value;
	
	form.contact_validated.value = 'no';	// Form has not validated.
	
	var eMsg = '';
	if (isWhitespace(cName)) eMsg = eMsg + 'Please enter your name.\n';
	if (isWhitespace(cEmail)) eMsg = eMsg + 'Please enter your e-mail address.\n';
	if (isWhitespace(cComments)) eMsg = eMsg + 'Please enter your comments.\n';

	if (eMsg.length != 0)
		{
			alert(eMsg);
			return false;
		}
				
	var eMailAddress = cEmail.value;
	// invalidEmail and validEmail are JavaScript regular expressions, not strings.
	// var invalidEmail = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/;
	var validEmail = /^.+\@[^-.][a-zA-Z0-9\-\.]+[^-.]\.(?!([0-9]*$))[a-zA-Z0-9]{2,6}$|\[[0-9]{1,3}(\.[0-9]{1,3}){3}\]$/;
	
	if (! validEmail.test(cEmail))
		{
			alert('The e-mail address you specified is not valid.');
			return false;
		}
		
	// percent2many is a JavaScript regular expression, not a string.
	var percentMany = /([^%]*%){25,}/;
	
	// If too many percent signs (actual garbage messages seen), don't send.
	if (percentMany.test(cComments))
		{
			alert('Invalid comments.');
			return false;
		}
		
	form.contact_validated.value = 'yes';
	
	return true;
	
}	// validateContactForm

//-----------------------------------------------------------------------------
// Return true if a string is empty.
function isEmpty(string2check)
{
	return ((string2check.length == 0) || (string2check == null));
}

//-----------------------------------------------------------------------------
// Return true if a stringg is whitespace or empty.
function isWhitespace(string2check)
{
	var whitespace = " \t\n\r";	// whitespace characters	
  
  // Is string empty?
  if (isEmpty(string2check)) return true;
	
  // Search through string for non-whitespace.
  for (var i = 0; i < string2check.length; i++)
		if (whitespace.indexOf(string2check.charAt(i)) == -1) return false;
  
  // All characters are whitespace.
  return true;
}
