/************************************************************************************
/*  Name:       validate.js
/*  Purpose:    Form validation javascript functions.
/*
/*  Change Log
/*      2/1/06  Cassidy Rauch       Created script file
/***********************************************************************************/

/*******************************************************************************
/* Name:    isValidEmailAddress
/* Purpose: Check if passed in string is a syntactically valid email address.
/******************************************************************************/
function isValidEmailAddress(email)
{    
    var emailReg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;   
    return (emailReg.test(email));     
}

/********************************************************************************************
/* Name:    isValidPhone
/* Purpose: Check if passed in string is a syntactically valid phone in format (555)555-5555
/********************************************************************************************/
function isValidPhone(phone)
{    
    var phoneReg = /^\(\d\d\d\)\d\d\d-\d\d\d\d$/;     
    return phone.match(phoneReg);  
}

/*******************************************************************************
/* Name:    validateContact
/* Purpose: Validate contact form input.
/******************************************************************************/
function validateContact()
{
    var formContact = document.contact;
	
    if (formContact.name.value == "")
    {
        alert("Name is required.");
        formContact.name.focus();
        return false;
    }        
    else if (formContact.phone1.value == "" || (formContact.phone1.value).length < 3 )
    {
        alert("The area code must be 3 digits.");
        formContact.phone1.focus();
        return false;
    }
    else if (formContact.phone2.value == "" || (formContact.phone2.value).length < 3 )
    {
        alert("The middle three digits of the phone number are missing.");
        formContact.phone2.focus();
        return false;
    }
    else if (formContact.phone3.value == "" || (formContact.phone3.value).length < 4 )
    {
        alert("The last four digits of the phone number are missing.");
        formContact.phone3.focus();
        return false;
    }
    else if (!isValidEmailAddress(formContact.email.value))
    {
        alert("Email address is not valid.");
        formContact.email.focus();
        return false;
    }    
    else
    {
        formContact.submit();
        return true;	
    } 
}