// A utility function that returns true if a string contains only
// whitespace characters.
function isblank(s)
{
    for(var i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
    }
    return true;
}

/*
  From http://scripts.franciscocharrua.com/validate-email-address.php
*/
function validateString(string, return_invalid_chars)
{
	 valid_chars = '1234567890-_.^~abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
	 invalid_chars = '';
	 
	 if(string == null || string == '')
		return(true);
	 
	 //For every character on the string.   
	 for(index = 0; index < string.length; index++)
		{
		char = string.substr(index, 1);                        
		
		//Is it a valid character?
		if(valid_chars.indexOf(char) == -1)
		  {
		  //If not, is it already on the list of invalid characters?
		  if(invalid_chars.indexOf(char) == -1)
			{
			//If it's not, add it.
			if(invalid_chars == '')
			   invalid_chars += char;
			else
			   invalid_chars += ', ' + char;
			}
		  }
		}                     
		
	 //If the string does not contain invalid characters, the function will return true.
	 //If it does, it will either return false or a list of the invalid characters used
	 //in the string, depending on the value of the second parameter.
	 if(return_invalid_chars == true && invalid_chars != '')
	   {
	   last_comma = invalid_chars.lastIndexOf(',');
	   
	   if(last_comma != -1)
		  invalid_chars = invalid_chars.substr(0, $last_comma) + 
		  ' and ' + invalid_chars.substr(last_comma + 1, invalid_chars.length);
				  
	   return(invalid_chars);
	   }
	 else
	   return(invalid_chars == ''); 
}


// Validates e-mail address
function isValidEmail (email_address)
{
	 //Assumes that valid email addresses consist of user_name@domain.tld
	 at = email_address.indexOf('@');
	 dot = email_address.indexOf('.');
	 
	 if(at == -1 || 
		dot == -1 || 
		dot <= at + 1 ||
		dot == 0 || 
		dot == email_address.length - 1)
		return(false);
		
	 user_name = email_address.substr(0, at);
	 domain_name = email_address.substr(at + 1, email_address.length);                  
	 
	 if (validateString(user_name) === false || 
		validateString(domain_name) === false)
		return(false);                     
	 
	 return(true);
}

// Validates newsletter form for mandatory fields
function validateNewsletter(f)
{
  var isValid = true;
  var msg = '';

  if ((f.fullname.value == null) || (isblank(f.fullname.value)))
  {
	  isValid = false;
	  msg += ' - Name\n';
  }

  if ((f.email1.value == null) || (isblank(f.email1.value)))
  {
	  isValid = false;
	  msg += ' - E-mail Address\n';
  }

  if ((f.email2.value == null) || (isblank(f.email2.value)))
  {
	  isValid = false;
	  msg += ' - Confirm E-mail Address\n';
  }

  if ((f.company.value == null) || (isblank(f.company.value)))
  {
	  isValid = false;
	  msg += ' - Company/Organization\n';
  }

  if ((f.job.value == null) || (isblank(f.job.value)))
  {
	  isValid = false;
	  msg += ' - Job Title\n';
  }

  if (isValid == false)
  {
      msg = 'Please make sure the following mandatory fields are entered:\n' + msg;
  }

  // Validate e-mail address
  if (!isValidEmail(f.email1.value))
  {
	  isValid = false;
	  msg += '\nE-mail Address does not appear to be valid\n';
  }

  if ((f.email1.value != null) && !isblank(f.email1.value) && 
	  (f.email2.value != null) && !isblank(f.email2.value) &&
	  (f.email1.value != f.email2.value))
  {
	  isValid = false;
	  msg += '\nE-mail Address fields do not match\n';
  }

  if (isValid == false)
  {
	  alert(msg);
  }
  return isValid;

}
