<!--
/*
  Found the PHP code at  http://www.linuxjournal.com/article/9585
  Then adapted it to javascript
  
  Added a check for at least one dot in the middle of domain part.
  (So "me@museum" will not be considered a valid e-mail address.)
  Added a check for leading or trailing dot in domain part.
  Both these checks are designed to weed out simple typos before
  calling the PHP script that checks the validity of DNS record.

  Found javascript code at  http://forums.devarticles.com/javascript-development-22/dns-lookup-using-javascript-74973.html
  (The part starting with serverData.)

*/
function validateEmail(s_email)
{
	var i_atIndex;
	var s_domain;
	var s_local;
	var i_domainLen;
	var i_localLen;
	var b_isValid = true;
	s_twoDotsPattern = /\.\./;
	s_dotInTheMiddlePattern = /^[^\.](.)+[\.](.)+[^\.]$/;
	s_domainAllowedPattern = /^[A-Za-z0-9.-]+$/;
	s_localAllowedPattern = /^(\\.|[A-Za-z0-9\!\=\/\$\*\+\?\^\{\}\|\.#%&`_'~-])+$/;
	s_quotedAllowedPattern = /^"(\\"|[^"])+"$/;

// Trim leading and trailing whitespaces
	s_email = s_email.replace(/^\s+|\s+$/g, '');
	i_atIndex = s_email.lastIndexOf('@');
	if (i_atIndex == -1)
	{
		b_isValid = false;
//		alert("Didn't find any @s.");
//		return (b_isValid);
	}
	else
	{
		s_domain = s_email.substr(i_atIndex + 1);
		s_local = s_email.substr(0, i_atIndex);
// Remove sets of even number of backslashes
		s_local = s_local.replace(/\\\\/g, '');
		i_domainLen = s_domain.length;
		i_localLen = s_local.length;
		if (i_localLen < 1 || i_localLen > 64)
		{
// Local part length exceeded
			b_isValid = false;
//			alert("Local part length exceeded.");
//			return (b_isValid);
		}
		else if (i_domainLen < 1 || i_domainLen > 255)
		{
// Domain part length exceeded
			b_isValid = false;
//			alert("Domain part length exceeded.");
//			return (b_isValid);
		}
		else if (s_local.charAt(0) == '.' || s_local.charAt(i_localLen - 1) == '.')
		{
// Local part starts or ends with '.'
			b_isValid = false;
//			alert("Local part cannot start or end with a dot.");
//			return (b_isValid);
		}
		else if (s_domain.charAt(0) == '.' || s_domain.charAt(i_domainLen - 1) == '.')
		{
// Domain part starts or ends with '.'
			b_isValid = false;
//			alert("Domain part cannot start or end with a dot.");
//			return (b_isValid);
		}
		else if (s_twoDotsPattern.test(s_local))
		{
// Local part has two consecutive dots
			b_isValid = false;
//			alert("Found two consecutive dots in local part.");
//			return (b_isValid);
		}
		else if (s_twoDotsPattern.test(s_domain))
		{
// Domain part has two consecutive dots
			b_isValid = false;
//			alert("Found two consecutive dots in domain part.");
//			return (b_isValid);
		}
		else if (!s_dotInTheMiddlePattern.test(s_domain))
		{
// Did not find any dots in domain part
			b_isValid = false;
//			alert("Domain part contains no dots.");
//			return (b_isValid);
		}
		else if (!s_domainAllowedPattern.test(s_domain))
		{
// Invalid character in domain part
			b_isValid = false;
//			alert("Invalid character in domain part.");
//			return (b_isValid);
		}
		else if (!s_localAllowedPattern.test(s_local))
		{
// Character not valid in local part unless local part is quoted
			if (!s_quotedAllowedPattern.test(s_local))
			{
				b_isValid = false;
//				alert("Invalid character in local part.");
//				return (b_isValid);
			}
			else
			{
//				Add code if needed
			}
		}
		else
		{
//			alert("Looks like we made it through ...");
//			Add code if needed
		}

		if (b_isValid)
		{
			var serverData = false;
			
			if (window.XMLHttpRequest)
			{
				serverData = new XMLHttpRequest();
			}
			else if (window.ActiveXObject)
			{
				serverData = new ActiveXObject("Microsoft.XMLHTTP");
			}

			var lookupResults = -1;
			
			if (serverData)
			{
				serverData.open('GET', '/includes/functions/dnscheck.php?s_domain=' + s_domain);
				serverData.onreadystatechange = function()
				{
					if (serverData.readyState == 4)
					{
						if (serverData.status == 200)
						{
							lookupResults = serverData.responseText;

							if (lookupResults == -1)
							{
								alert('Ups, something went wrong...');
								b_isValid = false;
							}
							else if (lookupResults == 0)
							{
								b_isValid = false;
							}
							else
							{
//								Add code if needed
							}
						}
					}
				}
				serverData.send(null);
			}
			else
			{
				b_isValid = false;
				alert('Failed To Create XMLHttpRequest Object');
			}
		}
	}
	return (b_isValid);
} // function validateEmail(s_email)
-->