﻿//This function is used to check if email address is valid
function ChkEmail(theAddress) 
{
  var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.@";
  var checkStr = TrimSpace(theAddress);
  var allValid = true;
  
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
  }
	if (theAddress == "")
	{	alert("Email is a required field!");
		return false;
	}
	
	if (theAddress.indexOf("@") == -1) 
	{	alert("Sorry, the email address you entered does not contain an '@' symbol.");
		return false;
	}
	
	if (theAddress.indexOf(".") == -1) 
	{	alert("Sorry, the email address you entered does not contain a '.' symbol.");
		return false;
	}	
	
	if (theAddress.indexOf("@.") != -1) 
	{
		alert("Sorry, the email address you entered is missing a valid character after the '@' sign and before the first period.");
		return false;
	}

	if (theAddress.indexOf("@") == 0) 
	{
		alert("Sorry, the email address you entered is missing a valid character before the '@' sign.");
		return false;
	}	
	if (!allValid)
	{
		alert("Sorry, the email address you entered contains invalid characters.");
		return false;
	}
	else
	{  
	return true;
	} 
}

function TrimSpace(str) {
	return( (""+str).replace(/^\s*([\s\S]*\S+)\s*$|^\s*$/,'$1') );
}
