function checkemail(str){
var testresults;						
var filter=/^(['_a-z0-9-+]+)(\.['_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,5})$/i
if (filter.test(str))
	testresults=true
else				
	testresults=false				
return (testresults)			
}

function trim(s) {
	while (s.substring(0,1) == ' ') {
		s = s.substring(1,s.length);
	}
	while (s.substring(s.length-1,s.length) == ' ') {
		s = s.substring(0,s.length-1);
	}
	return s;
}		
			
function ValidateForm(){                	
//verify the sender form fields

//Grab the sender's email address, trim off any spaces
var emailtest = trim(document.referral_form.refemail.value)

//Check to make sure the sender's email address has a valid formatting
if (checkemail(emailtest)==false)
{
	//It doesn't conform to the usual %@%.% standard
	alert ("Please check your email address, it doesn't appear to be valid");
	document.referral_form.refemail.focus();
	//return false so the form doesn't get submitted
	return false;
}

//Email address is valid, so now time to check to make sure they've entered a name
//Grab the sender's first name, remove spaces
var name = trim(document.referral_form.reffirstname.value)

//Check to make sure it exists after removing spaces
if (name.length < 1)
{
	//It doesn't, most likely someone entered a bunch of spaces
	alert("Please enter your first name");
	document.referral_form.reffirstname.focus();
	//return false so the form doesn't get submitted
	return false;
}

//Grab the sender's last name, remove spaces
name = trim(document.referral_form.reflastname.value)

//Check to make sure it exists after removing spaces
if (name.length < 1)
{
	//It doesn't, most likely someone entered a bunch of spaces
	alert("Please enter your last name");
	document.referral_form.reflastname.focus();
	//return false so the form doesn't get submitted
	return false;
}

//Great, the sender is all filled out, now to move onto the referees
//Grab the 1st referree's email address, trim off any spaces
emailtest = trim(document.referral_form.email1.value)

if (checkemail(emailtest)==false)
{
	//No email address filled in, alert the user
	alert("Please enter an email address for the first box");
	document.referral_form.email1.focus();
	return false;
}

//Grab the first referree's first name, remove spaces
name = trim(document.referral_form.fname1.value)

//Check to make sure it exists after removing spaces
if (name.length < 1)
{
	//It doesn't, most likely someone entered a bunch of spaces
	alert("Please enter the first name of the person you are referring");
	document.referral_form.fname1.focus();
	//return false so the form doesn't get submitted
	return false;
}

//Grab the first referee's last name, remove spaces
name = trim(document.referral_form.lname1.value)

//Check to make sure it exists after removing spaces
if (name.length < 1)
{
	//It doesn't, most likely someone entered a bunch of spaces
	alert("Please enter your last name of the person you are referring");
	document.referral_form.lname1.focus();
	//return false so the form doesn't get submitted
	return false;
}

document.referral_form.field1.value = "true";

//Now to check further fields for data
if (document.referral_form.email2.value.length > 0)
{
	emailtest = trim(document.referral_form.email2.value)
		
 	if (checkemail(emailtest)==false)
    {
    	//No email address filled in, alert the user
    	alert("Please enter an email address for the second box");
    	document.referral_form.email2.focus();
    	return false;
    }
  
  //Grab the second referree's first name, remove spaces
  name = trim(document.referral_form.fname2.value)
  
  //Check to make sure it exists after removing spaces
  if (name.length < 1)
  {
  	//It doesn't, most likely someone entered a bunch of spaces
  	alert("Please enter the first name of the person you are referring");
  	document.referral_form.fname2.focus();
  	//return false so the form doesn't get submitted
  	return false;
  }
  
  //Grab the second referee's last name, remove spaces
  name = trim(document.referral_form.lname2.value)
  
  //Check to make sure it exists after removing spaces
  if (name.length < 1)
  {
  	//It doesn't, most likely someone entered a bunch of spaces
  	alert("Please enter your last name of the person you are referring");
  	document.referral_form.lname2.focus();
  	//return false so the form doesn't get submitted
  	return false;
  }
  
  document.referral_form.field2.value = "true";
}	

//Now to check further fields for data
if (document.referral_form.email3.value.length > 0)
{
	emailtest = trim(document.referral_form.email3.value)
		
 	if (checkemail(emailtest)==false)
    {
    	//No email address filled in, alert the user
    	alert("Please enter an email address for the third box");
    	document.referral_form.email3.focus();
    	return false;
    }
  
  //Grab the third referree's first name, remove spaces
  name = trim(document.referral_form.fname3.value)
  
  //Check to make sure it exists after removing spaces
  if (name.length < 1)
  {
  	//It doesn't, most likely someone entered a bunch of spaces
  	alert("Please enter the first name of the person you are referring");
  	document.referral_form.fname3.focus();
  	//return false so the form doesn't get submitted
  	return false;
  }
  
  //Grab the third referee's last name, remove spaces
  name = trim(document.referral_form.lname3.value)
  
  //Check to make sure it exists after removing spaces
  if (name.length < 1)
  {
  	//It doesn't, most likely someone entered a bunch of spaces
  	alert("Please enter your last name of the person you are referring");
  	document.referral_form.lname3.focus();
  	//return false so the form doesn't get submitted
  	return false;
  }
  
  document.referral_form.field3.value = "true";
}

//Now to check further fields for data
if (document.referral_form.email4.value.length > 0)
{
	emailtest = trim(document.referral_form.email4.value)
		
 	if (checkemail(emailtest)==false)
    {
    	//No email address filled in, alert the user
    	alert("Please enter an email address for the fourth box");
    	document.referral_form.email4.focus();
    	return false;
    }
  
  //Grab the fourth referree's first name, remove spaces
  name = trim(document.referral_form.fname4.value)
  
  //Check to make sure it exists after removing spaces
  if (name.length < 1)
  {
  	//It doesn't, most likely someone entered a bunch of spaces
  	alert("Please enter the first name of the person you are referring");
  	document.referral_form.fname4.focus();
  	//return false so the form doesn't get submitted
  	return false;
  }
  
  //Grab the fourth referee's last name, remove spaces
  name = trim(document.referral_form.lname4.value)
  
  //Check to make sure it exists after removing spaces
  if (name.length < 1)
  {
  	//It doesn't, most likely someone entered a bunch of spaces
  	alert("Please enter your last name of the person you are referring");
  	document.referral_form.lname4.focus();
  	//return false so the form doesn't get submitted
  	return false;
  }
  
  document.referral_form.field4.value = "true"; 
}		

//Now to check further fields for data
if (document.referral_form.email5.value.length > 0)
{
	emailtest = trim(document.referral_form.email5.value)
		
 	if (checkemail(emailtest)==false)
    {
    	//No email address filled in, alert the user
    	alert("Please enter an email address for the fifth box");
    	document.referral_form.email5.focus();
    	return false;
    }
  
  //Grab the first referree's first name, remove spaces
  name = trim(document.referral_form.fname5.value)
  
  //Check to make sure it exists after removing spaces
  if (name.length < 1)
  {
  	//It doesn't, most likely someone entered a bunch of spaces
  	alert("Please enter the first name of the person you are referring");
  	document.referral_form.fname5.focus();
  	//return false so the form doesn't get submitted
  	return false;
  }
  
  //Grab the first referee's last name, remove spaces
  name = trim(document.referral_form.lname5.value)
  
  //Check to make sure it exists after removing spaces
  if (name.length < 1)
  {
  	//It doesn't, most likely someone entered a bunch of spaces
  	alert("Please enter your last name of the person you are referring");
  	document.referral_form.lname5.focus();
  	//return false so the form doesn't get submitted
  	return false;
  }
  
  document.referral_form.field5.value = "true";  
}

return true;
}