 function isBlank(str){
        var len = str.length
        var i
        for (i=0;i<len;++i){
                if (str.charAt(i) != " ") return false;
                }
        return true
        }

 function trim(inputString) {
    // Removes leading and trailing spaces from the passed string. Also removes
    // consecutive spaces and replaces it with one space. If something besides
    // a string is passed in (null, custom object, etc.) then return the input.
    if (typeof inputString != "string") { return inputString; }
    var retValue = inputString;
    var ch = retValue.substring(0, 1);
    while (ch == " ") { // Check for spaces at the beginning of the string
        retValue = retValue.substring(1, retValue.length);
        ch = retValue.substring(0, 1);
    }
    ch = retValue.substring(retValue.length-1, retValue.length);
    while (ch == " ") { // Check for spaces at the end of the string
        retValue = retValue.substring(0, retValue.length-1);
        ch = retValue.substring(retValue.length-1, retValue.length);
    }
    while (retValue.indexOf("  ") != -1) {
        // Note that there are two spaces in the string -
        //look for multiple spaces within the string
        retValue = retValue.substring(0, retValue.indexOf("  ")) +
            retValue.substring(retValue.indexOf("  ")+1, retValue.length);
        // Again, there are two spaces in each of the strings
            }
    return retValue; // Return the trimmed string back to the user
}
function emailCheck(emailStr) {
                var emailPat=/^(.+)@(.+)$/
                var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
                var validChars="\[^\\s" + specialChars + "\]"
                var quotedUser="(\"[^\"]*\")"
                var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
                var atom=validChars + '+'
                var word="(" + atom + "|" + quotedUser + ")"
                var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
                var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
                var matchArray=emailStr.match(emailPat)
                if (matchArray==null) {
                                alert("Email address seems incorrect (check @ and .'s)")
                                return false
                }
                var user=matchArray[1]
                var domain=matchArray[2]
                

                if (user.match(userPat)==null) {
                    alert(" The username in the email doesn't seem to be valid")
                    return false
                }
                var IPArray=domain.match(ipDomainPat)
                if (IPArray!=null) {
                    // this is an IP address
                          for (var i=1;i<=4;i++) {
                            if (IPArray[i]>255) {
                                //alert(" Destination IP address is invalid ")
                                alert(" Please check the email address ")
                                return false
                            }
                    }
                    return true
                }
                

                // Domain is symbolic name
                var domainArray=domain.match(domainPat)
                if (domainArray==null) {
                        alert(" The domain name in the email address doesn't seem to be valid ")
                    return false
                }
                

                /* domain name seems valid, but now make sure that it ends in a
                   three-letter word (like com, edu, gov) or a two-letter word,
                   representing country (uk, nl), and that there's a hostname preceding 
                   the domain or country. */
                

                /* Now we need to break up the domain to get a count of how many atoms
                   it consists of. */
                var atomPat=new RegExp(atom,"g")
                var domArr=domain.match(atomPat)
                var len=domArr.length
                if (domArr[domArr.length-1].length<2 || 
                    domArr[domArr.length-1].length>3) {
                   // the address must end in a two letter or three letter word.
                   alert(" The email must end in a three-letter domain, or two letter country")
                   return false
                }
                

                // Make sure there's a host name preceding the domain.
                if (len<2) {
                   var errStr=" Email is missing a hostname!"
                   alert(errStr)
                   return false
		}
                return true;
	}
	
function chkNewsLetterForm(){

	if (isBlank(trim(document.newsForm.nameNL.value))) {

	    alert("Please enter your Name");

	    document.newsForm.nameNL.focus();

	    return false;

	 }

	 if (isBlank(trim(document.newsForm.emailNL.value))) {

	    alert("Please enter your Email");

	    document.newsForm.emailNL.focus();

	    return false;

	 }
	 if (!emailCheck(trim(document.newsForm.emailNL.value))) {

	  	document.newsForm.emailNL.focus();

		 return false;

	  }
	 document.newsForm.submit();
}
