function check_form ()
{
  valid = true; // assume valid

//alert(document.contact.subject.value)

  if (document.contact.tocontact.selectedIndex == false)
     { valid = false; alert('The Department and Person To Contact Must be Filled In!');
      document.contact.tocontact.focus();return false; }

  if (document.contact.fname.value.length == 0)
     { valid = false; alert('First Name Must be Filled In!');
      document.contact.fname.focus();return false; }
  if (document.contact.lname.value.length == 0)
     { valid = false; alert('Last Name Must be Filled In!');
      document.contact.lname.focus();return false; }
  
  if (document.contact.address1.value.length > 0 ||
      document.contact.address2.value.length > 0 ||
      document.contact.city.value.length > 0 ||
      document.contact.state.selectedIndex == true ||
      document.contact.otherstate.value.length > 0 ||
      document.contact.zip.value.length > 0 ){

			if (document.contact.address1.value.length == 0)
				 { valid = false; alert('Address line 1 Must be Filled In!');
					document.contact.address1.focus(); return false;}

			if (document.contact.city.value.length == 0)
				 { valid = false; alert('City Must be Filled In!');
					document.contact.city.focus(); return false;}

			if (document.contact.state.selectedIndex == false &&
				  document.contact.otherstate.value.length == 0)
				 { valid = false; alert('State or Other State Must be Filled In!');
					document.contact.state.focus();return false; }

			if (document.contact.zip.value.length == 0)
				 { valid = false; alert('Zip or Postal code Must be Filled In.');
					document.contact.zip.focus(); return false;}
	}

	if (document.contact.country.selectedIndex == false )
		 { valid = false; alert('Country Must be Filled In!');
			document.contact.country.focus();return false; }

  if (document.contact.hphone.value.length == 0 &&
      document.contact.wphone.value.length == 0 &&
      document.contact.cphone.value.length == 0  &&
      document.contact.email.value.length == 0)
		 { valid = false; alert('Home phone or Work phone or Cell phone or E-mail Must be Filled In!');
			document.contact.hphone.focus(); return false; }

	if (document.contact.request.value.length == 0)
		 { valid = false; alert('Request Must be Filled In!');
			document.contact.request.focus(); return false;}

  if (document.contact.email.value.length > 0)
	  {valid = VerifyEmailaddress1();}

  if (valid)
        {
      //alert("Please be patient while your form is processed. \n" + 	"DO NOT CLICK SUBMIT AGAIN, \n" +  "It can result in a duplicate information." + 			"\n\n Click OK to Continue.");

			x=document.contact.tocontact.selectedIndex;
			document.contact.recipient.value = church_dept_list[x-1][3];
			document.contact.recipient_name.value = church_dept_list[x-1][2];
			var dept = church_dept_list[x-1][0];

			document.contact.subject.value = "TO: " + dept + " FROM: " + document.contact.fname.value + " " + document.contact.lname.value;
			
//			if (document.contact.email.value.length > 0)
//				{document.contact.from_name.value = document.contact.lname.value + ", " + document.contact.fname.value;
//				  document.contact.from_email.value = document.contact.email.value;}

			document.contact.textrestrict.value = "";
			if (document.contact.restrict.checked){document.contact.textrestrict.value = "Restrict Permission to this Message";}

			document.contact.textpriority.value = "";
			if (document.contact.priority[0].checked){document.contact.textpriority.value = "High Importance";}
			if (document.contact.priority[1].checked){document.contact.textpriority.value = "Low Importance";}

      document.contact.submit();
      return true;

        }
   document.contact.email.focus();
   return false;
}


function VerifyEmailaddress1()
{
  var Reason  = "Your Email address1 appears incorrect.  \n\nPlease check. \n\nReason:"
  var Success = "Email address1 entered Correctly!"
  var checkStr = document.contact.email.value;
  var ix = (checkStr.length - 4)
  var RC = true;
  var x = AtSignValid = DoublePeriod = PeriodValid = SpaceValid = ExtValid = RL = 0;

  for (i = 0;  i < checkStr.length;  i++)
  {
    if (checkStr.charAt(i) == '@')
      AtSignValid++;
    else if (checkStr.charAt(i) == '.')
    {
      if (x == (i-1))
        DoublePeriod++;
      else
      {
        x = i;
        PeriodValid++;
      }
    }
    else if (checkStr.charAt(i) == ' ')
      SpaceValid ++;
  }
  if (checkStr.indexOf(".com", ix) > -1)
    ExtValid++;
  else if (checkStr.indexOf(".edu", ix) > -1)
    ExtValid++;
  else if (checkStr.indexOf(".biz", ix) > -1)
    ExtValid++;
  else if (checkStr.indexOf(".net", ix) > -1)
    ExtValid++;
  else if (checkStr.indexOf(".org", ix) > -1)
    ExtValid++;
  else if (checkStr.indexOf(".gov", ix) > -1)
    ExtValid++;
  else if (checkStr.indexOf(".mil", ix) > -1)
    ExtValid++;
  else if (checkStr.indexOf(".cc", ix) > -1)
    ExtValid++;
  RL = Reason.length;
  if (AtSignValid != 1)
    Reason += "\nOnly one '@' allowed, " + AtSignValid + " found.";
  if (PeriodValid == 0)
    Reason += "\naddress1 must contain at least one period.";
  if (SpaceValid > 0)
    Reason += "\nNo Spaces allowed. address1 contains " + SpaceValid + " space";
  if (SpaceValid > 1)
    Reason += "s.";
  if (checkStr.length > 120)
    Reason += "\nPlease limit the Email address1 to 120 characters.";

  if (RL != Reason.length)
  {
 alert(Reason);
 return false;
  }
  return true;
}
