/* ----- FORM VALIDATION ----- */
function validate()
{	
    // PRESET OK = TRUE
    ok = true;
    
    //TEST THE VALUE OF FIRST NAME IN THE FORM
    // When testing form values, two options exist
    // Format 1:  document.form_name.field_name.value (required field have a name.)
    // format 2:  document.getElementById("FIELD_ID_NAME").value (requires an ID in the field)
    
    //BASIC VALIDATION TESTING EMPTY VALUES
    if(document.beeForm.Name.value == "")
    {				
        alert('Please make sure you fill in your Name before submitting the form.');
        return false;				
    }	
    if(document.beeForm.Email.value == "")
    {				
        alert('Please make sure you fill in your Email Address before submitting the form.');
        return false;				
    }	
    if(document.beeForm.Phone.value == "")
    {				
        alert('Please make sure you fill in your Phone Number before submitting the form.');
        return false;				
    }
	if(document.beeForm.City.value == "")
    {				
        alert('Please make sure you fill in your City before submitting the form.');
        return false;				
    }
    if ( document.beeForm.Contact.selectedIndex == 0 )
    {
        alert ( "Please choose a preferred contact type before submitting the form." );
        return false;
    }
	//END BASIC VALIDATION
    

    //TESTING EMAIL VALUE
    // ASSIGNING THE VALUE OF EMAIL		
    var email_str = document.beeForm.Email.value;	
    //TEST TO SEE IF THE VALUE CONTAINS AN '@" SYMBOL	
    if(! email_str.match("@"))
    {				
        alert('Email Format Error: Please make sure you fill in your email address.');
        return false;				
    }
    //TEST TO SEE IF THE VALUE CONTAINS A PERIOD
    if(! email_str.match("."))
    {				
        alert('Email Format Error: Please make sure you fill in your email address.');
        return false;				
    }
    
    
    // MAKE CALL TO THE PHONE VALIDATION FUNCTION
    var fld = document.beeForm.Phone.value;
    return validatePhone(fld);	
                         
        //TEST TO SEE IF THE VALUE OF OK EQUALS 'TRUE'	OR 'FALSE'			 
        if(ok)
        {
            return ok;
        }
        else
        {
            return false;
        }       
}

/* ----- PHONE NUMBER FUNCTION ----- */
function validatePhone(fld)
{       
//STRIP OUT SLASHES, DASHES, and SPACES IN THE PHONE NUMBER
// USING REGULAR EXPRESSIONS
var stripped = fld.replace(/[\(\)\.\-\ ]/g, ''); 
// CHECK TO SEE IF THE REMAINING VALUE IS EMPTY
if (fld.value == "")
{
    //SEND ALERT TO THE SCREEN AND PAUSE
    alert("You didn't enter a phone number.\n");			    
    return  false;			
} 		
    // IF THE VALUE IS NOT EMPTY TEST TO SEE IF THE VALUE IS "NOT A NUMBER" NaN
    // PARSEInt Flips the value in an integer
else if (isNaN(parseInt(stripped))) {
   alert("The phone number contains illegal characters.\n");        
   return  false;
} 		
    // IS THE REMAINING VALUE AT LEAST 10 CHARACTERS IN LENGTH
else if (!(stripped.length == 10)) {
    alert("The phone number is the wrong length. Make sure you included an area code.\n");      
    return  false;  
}			
}
