Event.observe(window, 'load', function(event)
{
	Event.observe('fpi_ShowMailingAddressFlag', 'change', function()
	{
		$('fpi_MailingAddress')[$('fpi_ShowMailingAddressFlag').checked ? 'show' : 'hide']();
		if (!$('fpi_MailingAddress').checked)
		{
			$('fpi_Mailing_Address1').value = '';
			$('fpi_Mailing_Address2').value = '';
			$('fpi_Mailing_Address3').value = '';
			$('fpi_Mailing_City').value = '';
			$('fpi_Mailing_StateCode').value = '';
			$('fpi_Mailing_PostalCode').value = '';
			$('fpi_Mailing_CountryCode').value = 'US';
		}
	});

	Event.observe('fpi_CountryCode', 'change', function()
	{
		var f = $('fpi_RegistrationForm');

		if ($F(f.CountryCode) != 'US' && $F(f.CountryCode) != 'CA')
		{
			alert("Please use the address lines to enter your city, region, and postal code information to meet addressing requirements for your country.");
			$('fpi_City').disable();
			$('fpi_StateCode').disable();
			$('fpi_PostalCode').disable();
			$('fpi_City').value = '';
			$('fpi_StateCode').value = '';
			$('fpi_PostalCode').value = '';
			f.Address2.focus();
		}
		else
		{
			$('fpi_City').enable();
			$('fpi_StateCode').enable();
			$('fpi_PostalCode').enable();
		}
	});
});

function fpi_validateRegistration()
{
	var f = $('fpi_RegistrationForm');

	if ($F(f.ParishName).blank())
	{
		alert("Please enter a parish name.");
		f.ParishName.focus();
		return false;
	}

	if ($F(f.ContactFirstName).blank())
	{
		alert("Please enter a contact first name.");
		f.ContactFirstName.focus();
		return false;
	}

	if ($F(f.ContactLastName).blank())
	{
		alert("Please enter a contact last name.");
		f.ContactLastName.focus();
		return false;
	}

	if ($F(f.Phone1).blank())
	{
		alert("Please enter a church phone number.");
		f.Phone1.focus();
		return false;
	}

	if ($F(f.Phone1).length < 10)
	{
		alert("Please enter a full 10 digit church phone number (please include the country code for international phone numbers).");
		f.Phone1.focus();
		return false;
	}

	if ($F(f.Phone2).blank())
	{
		alert("Please enter a contact phone number.");
		f.Phone2.focus();
		return false;
	}

	if ($F(f.Phone2).length < 10)
	{
		alert("Please enter a full 10 digit contact phone number (or longer for international phone numbers).");
		f.Phone2.focus();
		return false;
	}

	if (!isEmail($F(f.EmailAddress1)))
	{
		alert("Please enter a valid email address.");
		f.EmailAddress1.focus();
		return false;
	}

	if ($F(f.Address1).blank())
	{
		alert("Please enter a mailing address.");
		f.Address1.focus();
		return false;
	}

	var fullAddress = ($F(f.Address1) + $F(f.Address2) + $F(f.Address3)).toUpperCase();
	/*if (fullAddress.indexOf('P.O.') >= 0 || fullAddress.indexOf('PO BOX') >= 0 || fullAddress.indexOf('POST OFFICE') >= 0)
	{
		alert("P.O. Boxes are not allowed.\n\nPlease enter a different address.");
		f.Address1.value = '';
		f.Address2.value = '';
		f.Address3.value = '';
		f.Address1.focus();
		return false;
	}*/

	if ($F(f.CountryCode).blank())
	{
		alert("Please enter a country.");
		f.CountryCode.focus();
		return false;
	}

	if ($F(f.CountryCode) != 'US' && $F(f.CountryCode) != 'CA')
	{
		if ($F(f.Address2).blank() && $F(f.Address3).blank())
		{
			alert("Please use the address lines to enter your city, region, and postal code information to meet addressing requirements for your country.");
			f.Address2.focus();
			return false;
		}
	}
	else
	{
		if ($F(f.City).blank())
		{
			alert("Please enter a city.");
			f.City.focus();
			return false;
		}

		if ($F(f.StateCode).blank())
		{
			alert("Please enter a state.");
			f.StateCode.focus();
			return false;
		}

		if ($F(f.PostalCode).blank())
		{
			alert("Please enter a zip/postal code.");
			f.PostalCode.focus();
			return false;
		}
	}

	if ($F(f.CountryCode) == 'US' && $F(f.StateCode) == "MN" && $F(f.TaxExemptNumber).blank())
	{
		if (!confirm("If your parish has tax excemption status, please enter something for a tax excemption number. If you do not know your tax excemption number, you may enter \"EXCEMPT\" so that you are not charged sales tax.\n\nClick \"Cancel\" to enter something for a tax excemption number; Click \"OK\" to proceed understanding that you will be charged Minnesota sales tax of 6.5%."))
		{
			f.TaxExemptNumber.focus();
			return false;
		}
	}

	return true;
}

function fpi_submitRegistration()
{
	if (!fpi_validateRegistration()) return;
	
	$('fpi_RegistrationForm').submit();
}
