
function checkExists( fieldName, theFormElement, focus ) {
  if( theFormElement ) {
    var trimmedElement = theFormElement.value.replace(/^\\s+|\\s+$/, '');
    if( trimmedElement == "" ) {
        if( focus ) {
          theFormElement.focus( );
        }
        alert( "Please enter a value for the \" " + fieldName + " \" field." );
        return false;
    }
    return true;
  } else {
    // The form element does not exist
	alert( "Please enter a value for the \" " + fieldName + " \" field." );
	
    return false;
  }
}

function checkMinLength( iLength, fieldName, theFormElement, focus ) {
    var checkString = theFormElement.value;
    if( checkString.length < iLength ) {
        if( focus ) {
          theFormElement.focus( );
        }
        alert( "Please enter at least " + iLength + " characters in the \" " + fieldName + " \" field.");
        return false;
    } else
        return true;
}

function checkEmailAddress( fieldName, theFormElement ) {

    var emailStr = theFormElement.value;
    var emailPat = new RegExp( "^(.+)@(.+)$" );
    var specialChars = "\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
    var validChars = "\[^\\s" + specialChars + "\]";
    var quotedUser = "(\"[^\"]*\")";
    var ipDomainPat = new RegExp( "^\\[(\\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 ) {
        theFormElement.focus( );
        alert( "Value in field \"" + fieldName + "\" is an invalid email address.\nPlease check for \"@\"." );
        return false;
    }
    var user = matchArray[1];
    var domain = matchArray[2];
    if( user.match( userPat ) == null ) {
        theFormElement.focus( );
        alert( "Value in field \" " + fieldName + " \" is an invalid email address." );
        return false;
    }
    // W3C says, when an ip adress is part of the email address instead of the host name, 
    // it has to surrounded with [].
    var IPArray = domain.match( ipDomainPat );
    if( IPArray != null ) {
        for (var i = 1; i <= 4; i ++ ) {
            if( IPArray[i] > 255 ) {
                theFormElement.focus( );
                alert( "Value in field \" " + fieldName + " \" is an invalid email address.\nDestination IP address is invalid." );
                return false;
            }
        }
        return true;
    }
    var domainArray = domain.match( domainPat );
    if( domainArray == null ) {
        theFormElement.focus( );
        alert( "Value in field \" " + fieldName + " \" is an invalid email address.\nThe domain name is invalid." );
        return false;
    }
    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 > 7 ) {
        theFormElement.focus( );
        alert( "Value in field \" " + fieldName + " \" is an invalid email address.\nThe address must end with a domain that is between two and seven letters in length." );
        return false;
    }
    if( len < 2 ) {
        theFormElement.focus( );
        alert( "Value in field \" " + fieldName + " \" is an invalid email address.\nThis address is missing a hostname!" );
        return false;
    }
    return true;
}

function validateContactForm(contactForm) {

	if (!checkExists("Name", contactForm.name, true)) return false;
	if (!checkExists("Company", contactForm.company, true)) return false;
	if (!checkExists("Email Address", contactForm.email, true)) return false;
	if (!checkEmailAddress("Email Address", contactForm.email)) return false;
	if (!checkMinLength(10, "Telephone Number", contactForm.telephone, true)) return false;
	if (!checkExists("Message", contactForm.message, true)) return false;
	
}