// Check to see if a form fields is null
function checkNull(formField, errorSpan, errorMsg, noErrorText)
{
    var formFieldError = formField + 'Error';
	
    if (formField.value == "")
    {
        document.getElementById(errorSpan).className = 'formError';
        document.getElementById(errorSpan).innerHTML = errorMsg;
        formField.focus();
        return true;
    }
    else
    {
        document.getElementById(errorSpan).className = '';
        document.getElementById(errorSpan).innerHTML = noErrorText;
        return;
    }
}

// Check to see if an email address is valid
function checkEmail(formField, errorSpan, errorMsg, noErrorText)
{
    var emailPat = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
    var matchArray = formField.value.match(emailPat);

    if (matchArray == null) {
        document.getElementById(errorSpan).className = 'formError';
        document.getElementById(errorSpan).innerHTML = errorMsg;
        formField.focus();
        return true;
    }
    else
    {
        document.getElementById(errorSpan).className = '';
        document.getElementById(errorSpan).innerHTML = noErrorText;
        return;
    }
}

// Check to see if a radio button has been selected
function checkRadio(formField, errorSpan, errorMsg, noErrorText)
{
	var radioLength = formField.length;
    var radioSelected = false;

    for(var i = 0; i < radioLength; i++)
    {
		if(formField[i].checked)
        {
			radioSelected = true;
		}
	}

    if (radioSelected == false)
    {
        document.getElementById(errorSpan).className = 'formError';
        document.getElementById(errorSpan).innerHTML = errorMsg;
        return true;
    }
    else
    {
        document.getElementById(errorSpan).className = '';
        document.getElementById(errorSpan).innerHTML = noErrorText;
        return;
    }
}

// Check the form for errors
function errorCheck(formName)
{
    var foundError = false;
    checkArray = new Array();

    checkArray[1] = checkRadio(formName.reason, 'reasonError', 'Please choose from the options below so we may handle your communication most efficiently <span class="formrequired">*</span>:', 'Please choose from the options below so we may handle your communication most efficiently <span class="formrequired">*</span>:');
    checkArray[2] = checkEmail(formName.email, 'emailError', '&nbsp;&nbsp;Please a valid email address.', '');
    checkArray[3] = checkNull(formName.lastname, 'lastnameError', '&nbsp;&nbsp;Please enter your last name.', '');
    checkArray[4] = checkNull(formName.firstname, 'firstnameError', '&nbsp;&nbsp;Please enter your first name.', '');

    // Give focus to the first field that was found to have an error
    for (x = 0; x < checkArray.length; x++)
    {
        if (checkArray[x])
        {
            foundError = true;
        }
    }

    // Submit the form is no errors were found
    if (foundError == false)
    {
        formName.submit();
    }
}
