//Validation Script to validate, alert and focus on error
//validates that there is information (characters or blank characters) present  (Required Fields)
//validates phone no. lengths, valid characters
//validates the email format and if valid characters are provided for single input fields
//validates if at least one radio button is checked


function validate()
//Validation function for Customer Inquiry Form
{
	   
 
//SUBJECT:   Subject field is required; test that a subject is choosen
if(document.form1.subject.value=="")
	{ 
	  alert("A Subject is required to submit the form. Please select a Subject.");
	  document.form1.subject.focus();
		return false;
	}

 
//NAME:   Name field Required - testing for input value
if(document.form1.name.value=="")
 	{ alert("Your Name is required to submit the form. Please provide your Name.");
	  document.form1.name.focus();
		return false; 
	}


//PHONE:   Phone is not required unless selected in contact method.  If phone # is provided, check to ensure correct # of characters provided
//1st Test is if phone # was provided
if(!(document.form1.phone.value==""))    // if phone # is provided, then go to 2nd test
	{ 
	
	//2nd Test - verify if correct number of characters entered  aaa-bbb-cccc  (10 numbers and 2 dashes)
	theField = document.form1.phone.value;
	theLength = theField.length;
	
	if(!(theLength == 12))  
		{
		alert("Length of the Phone # you entered is not valid.  \n\n Please include both area code \& number, \n and use hypens as noted: aaa-ccc-xxxx.");
		document.form1.phone.focus();
			return false; 
		}

	//3rd Test - Test for valid characters:  0-9 and dashes by matching illegal characters  
	var illegalChars= /[\(\)\<\>\,\;\:\.\\\/\"\[\][A-Za-z}]/;  
 
	if (document.form1.phone.value.match(illegalChars))
		{
		alert("Phone # must contain only numeric characters and dashes. \n\n You entered invalid characters, please correct. \n\n format:  aaa-ccc-xxxx");      
		document.form1.phone.focus();              
            return false;
        }	                  
    }


//EMAIL:   Email is not required unless selected in contact method.  
//If email is provided, verify valid format \& then test for illegal characters

//1st Test - if email address was provided
if(!(document.form1.email.value==""))
    {
	//Second test to validate the format 
	var emailFilter=/^.+@.+\..{2,3}$/;

	if(!(emailFilter.test(document.form1.email.value)))
		{ 
		alert("Please provide a valid Email Address format.");
		document.form1.email.focus(); 
				return false;
		}

	//Third test if valid characters are provided
	var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/;  

	if (document.form1.email.value.match(illegalChars))
	   {
		alert("You have invalid characters in your email address.");      
		document.form1.email.focus();              
                return false;
        }	
	}

//METHOD:    Contact Method required - 
//First test for input that one radio button was checked (true is checked; false is not checked)
//radio button name=method in this example and [ ] is the button position in group
if((document.form1.method[0].checked==false) && (document.form1.method[1].checked==false) && (document.form1.method[2].checked==false))
	{ 
	alert("Please select how you want us to contact you.");
	document.form1.method[0].focus();
		return false; 
	}

//2nd Test is for email as method choosen. If checked, then verify email field for email address.  Email is first position [0]
if((document.form1.method[0].checked==true) && (document.form1.email.value==""))
	{ 
	alert("If Preferred Contact Method is via E-mail, Please Provide Your E-mail Address \n in correct format: aaa@bbbb.nnn");
	document.form1.email.focus();
		return false; 
	}
//email validation tests would activate once data found in email field



//3rd Test is for phone as method choosen. If checked, then check phone field for phone #.  Phone is second position [1]
if((document.form1.method[1].checked==true) && (document.form1.phone.value==""))
	{ 
	alert("If Preferred Contact Method is via Telephone, Please Provide Your Phone Number \n in correct format:  aaa-bbb-cccc");
	document.form1.phone.focus();
		return false; 
	}

//4th Test is for either choice of method
if((document.form1.method[2].checked==true) && (document.form1.email.value==""))
	{ 
	alert("If we can Contact you via Either E-mail or Telephone, \n Please Provide Your E-mail Address.");
	document.form1.email.focus();
		return false; 
	}
else
	{ 
	if((document.form1.method[2].checked==true) && (document.form1.phone.value==""))
		{ 
		alert("If we can Contact you via Either E-mail or Telephone, \n Please Provide Your Phone #.");
		document.form1.phone.focus();
			return false; 
		}
	}


//Return confirmation to user that data entered correctly and now being submitted for processing
//alert("Your data entry was completed successfully. Thank you. \n Someone will contact you shortly"); 

}

