var validASCIIsymbols =  '!%’*+-.=@^_`~';
function trim(str) {
return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}
function validateEmailChars(email) {

	email = trim(email);
	for(var i=0;i<email.length;i++)
	{
	    var emailChar = email.charAt(i);
	    if
	    (
	    (emailChar >= 'a' && emailChar <= 'z')  ||
	    (emailChar >= 'A' && emailChar <= 'Z')  ||
	    (emailChar >= '0' && emailChar <= '9')  ||
	    (validASCIIsymbols.indexOf(emailChar) != -1)
	    )
	    {
	    }
	    else  {
	       return false;
	    }
	}
	return true;
}

function validateEmailFormat(email){

var atSymbol = 0;
for(var a = 0; a < email.length; a++){
	if(email.charAt(a) == '@'){
		atSymbol++;
	}
}

// if more than 1 @ symbol exists
if(atSymbol > 1){
	// then error
	return false;
}

// if 1 @ symbol was found, and it is not the 1st character in string
if(atSymbol == 1 && email.charAt(0) != '@'){
//look for period at 2nd character after @ symbol
	var period = email.indexOf('.',email.indexOf('@')+2);

	var twoPeriods = (email.charAt((period+1)) == '.') ? true : false;

	//if period was not found OR 2 periods together OR field contains less than 5 characters OR period is in last position
	if(period == -1 || twoPeriods || email.length < period + 2 || email.charAt(email.length-1)=='.'){
		// then cancel and don't submit form
		return false;
	}
}
// no @ symbol exists or it is in position 0 (the first character of the field)
else{
	// then cancel and don't submit form
	return false;
}

//Check the length of name of email ...shouldnt exceed 64 chars and domain+ext shouldnt exceed 255 , as per new policy +  do not allow "?"
if (email.indexOf('?') != -1   ||  email.substring(0,email.indexOf('@')).length>64 || email.substring(email.indexOf('@')+1,email.length).length>255) 
{ 
	return false;
}

//all tests passed, submit form
return true;
}

