var errorText_email = "Please enter a valid email address.";

function Form_Subscribe() {
	
    var emailValid = ValidateEmail();

    if (emailValid == false) {
        return false;
    }
	else
		document.frmSubscribe.submit();

    return false;
}

function ValidateEmail() {
    var email = document.getElementById('subscribe-form').value;
    if ((email.indexOf("@") > 1) && //  must contain @, and it must not be the first character
          (email.lastIndexOf(".") > email.indexOf("@")) &&  // last dot must be after the @
          (email.indexOf("@") != email.length) &&  // @ must not be the last character
          (email.indexOf("..") < 0) && // two periods in a row is not valid
          (email.indexOf(".") != email.length) &&  // . must not be the last character
          (AllValidEmailChars(email))) // all characters must be valid
    {
        document.getElementById('ErrEmailSubscribe').style.display = "none";
        document.getElementById('ErrEmailSubscribe').innerHTML = "";
        return true;
    }
    else {
        document.getElementById('ErrEmailSubscribe').style.display = "block";
        document.getElementById('ErrEmailSubscribe').innerHTML = errorText_email;
        return false;
    }
}

function AllValidEmailChars(email) {
    var isValid = true;
    var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
    for (var i = 0; i < email.length; i++) {
        var letter = email.charAt(i).toLowerCase();
        if (validchars.indexOf(letter) != -1) {
            continue;
        }
        else {
            isValid = false;
            break;
        }
    }
    return isValid;
}
