var emailRe = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.(\w{2}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))$/
var phoneRe = /^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,5})|(\(?\d{2,6}\)?))(-| )?(\d{3,4})(-| )?(\d{4})(( x| ext)\d{1,5}){0,1}$/

var isValid;

function setMessageField ( name, isValid )
{
	var messageField = document.getElementById(name);
	messageField.style.display = isValid ? "none" : "inline";
}

function isEmpty ( val )
{
	return val == "";
}

function isValidEmail ( val )
{
	return emailRe.test( val );
}

function isValidPhone ( val )
{
	return !isNaN(val) && val.length == 10;  //phoneRe.test( val );
}

function isInteger ( val )
{
	return !isNaN( parseInt( val ) )
}

function isFloat ( val )
{
	return !isNaN( parseFloat( val ) )
}

function validateRequiredField ( formFieldName, messageFieldName )
{
	var passed = !isEmpty( ( document.ylf[formFieldName].value ).replace("-1", "") );
	setMessageField( messageFieldName, passed );
	if ( !passed ) isValid = false;
}

function validateEmailField ( formFieldName, messageFieldName )
{
	var val = document.ylf[formFieldName].value;
	if ( !isEmpty( val ) )
	{
		var passed = isValidEmail( val );
		setMessageField( messageFieldName, passed );
		if ( !passed ) isValid = false;
	}
}

function validatePhoneField ( formFieldName, messageFieldName )
{
	var val = document.ylf[formFieldName].value;
	if ( !isEmpty( val ) )
	{
		val = val.replace(/[^\d]/g,'');
		var passed = isValidPhone( val );
		setMessageField( messageFieldName, passed );
		if ( !passed ) isValid = false;
	}
}

function validateNumericField ( formFieldName, messageFieldName )
{
	var val = document.ylf[formFieldName].value;
	if ( !isEmpty( val ) )
	{
		var passed = isInteger( val );
		setMessageField( messageFieldName, passed );
		if ( !passed ) isValid = false;
	}
}

function validateMoneyField ( formFieldName, messageFieldName )
{
	var val = document.ylf[formFieldName].value;
	if ( !isEmpty( val ) )
	{
		var passed = isFloat( val );
		setMessageField( messageFieldName, passed );
		if ( !passed ) isValid = false;
	}
}

function validateFieldLength ( formFieldName, messageFieldName, length )
{
	if ( !isEmpty( document.ylf[formFieldName].value ) )
	{
		var passed = document.ylf[formFieldName].value.length == length;
		setMessageField( messageFieldName, passed );
		if ( !passed ) isValid = false;
	}
}

function validateFieldMaxLength ( formFieldName, messageFieldName, length )
{
	if ( !isEmpty( document.ylf[formFieldName].value ) )
	{
		var passed = document.ylf[formFieldName].value.length <= length;
		setMessageField( messageFieldName, passed );
		if ( !passed ) isValid = false;
	}
}

// Phone Input Formatter
function formatPhone( phoneField )
{
	var num = phoneField.value.replace(/[^\d]/g,'');
    phoneField.value = "(" + num.substring(0,3) + ") " + num.substring(3, 6) + "-" + num.substring(6);
}



function submitForm()
{
    isValid = true;

    // Perform Validation

    // Validate First Name
    validateRequiredField( "FirstName", "firstNameValidationMessage" );

    // Validate Last Name
    validateRequiredField( "LastName", "lastNameValidationMessage" );

// Validate StreetAddress
validateRequiredField( "StreetAddress", "addressValidationMessage" );

// Validate City
validateRequiredField( "City", "cityValidationMessage" );

// Validate State
validateRequiredField( "State", "stateValidationMessage" );

// Validate Email
validateRequiredField( "EmailAddress", "emailFormatValidationMessage" );
validateEmailField( "EmailAddress", "emailFormatValidationMessage" );

    // Validate Phone
    validateRequiredField( "Phone1", "phoneValidationMessage" );
    validatePhoneField( "Phone1", "phoneFormatValidationMessage" );

// Validate Zip Code
validateRequiredField( "PostalCode", "zipFormatValidationMessage" );
validateNumericField( "PostalCode", "zipFormatValidationMessage" );

	 // Validate Comments
    validateFieldMaxLength( "Comments", "commentsValidationMessage", 240 );

    // Submit Form
    if ( isValid )
    {
        document.ylf.Phone1.value = document.ylf.Phone1.value.replace(/[^\d]/g,'');
        document.forms["ylf"].submit();
    }
}




