function VerifyEmail(field, type)
{
	var pattern = /^[\w.-]+@[\w+-]+\.\w+/;
	if (pattern.test(field.value)) {
		return true;
	}
	else {
		alert('The ' + type + ' email address appears to be invalid.\n\n' +
				'I need a valid email address to complete the order.');
		return false;
	}
}


function VerifyRequired(field, desc)
{
	if (field.value != '') {
		return true;
	}
	else {
		alert('You must provide ' + desc + '.');
		return false;
	}
}


function VerifyUSZipCode(zipcode)
{
	var pattern = /^\d{5}((-)?(\d{4})?)$/;
	if (zipcode == '') {
		alert('A zip code is required for US destinations.');
		return false;
	}
	else {
		var zclen = zipcode.length;
		if (pattern.test(zipcode) &&
				(zclen == 5 || zclen == 9 || zclen == 10)) {
			return true;
		}
		else {
			alert('That is not a valid US zip code.');
			return false;
		}
	}
}


function VerifyCanadianPostCode(postcode)
{
	var pattern = /^[A-Za-z]\d[A-Za-z]([ -])?\d[A-Za-z]\d$/;
	if (postcode == '') {
		alert('A postal code is required for Canadian destinations.');
		return false;
	}
	else {
		if (pattern.test(postcode)) {
			return true;
		}
		else {
			alert('That is not a valid Canadian postal code.');
			return false;
		}
	}
}


function VerifyPostCode(postcode, country)
{
	if (country.value == 'United States of America') {
		return VerifyUSZipCode(postcode.value);
	}
	else if (country.value == 'Canada') {
		return VerifyCanadianPostCode(postcode.value);
	}
	else {
		return true;
	}
}


function VerifyCountry(country)
{
	if (country.value == 'NONE GIVEN') {
		alert('You must select a valid country.');
		return false;
	}
	else {
		return true;
	}
}


// If payment method is Paypal and user gave a separate Paypal address,
// return true if it verifies.  Returns true if either precondition is
// false: either we're not using Paypal, so the address can be any old
// junk, or we are and user is content to use their contact address for
// Paypal, which is verified separately. 
function VerifyPaypalEmail(form)
{
	if ((findSelectedValue(form.payment) == 'Paypal') &&
				form.paypal_email.value.length) {
			// User selected Paypal and gave a separate email address for
			// that, so verify it, too.
			return VerifyEmail(form.paypal_email, 'Paypal');
	}
	else {
		return true;
	}
}


function VerifyRequireds(form)
{
	if (VerifyEmail(form.required_email, 'contact')) {
		var country = form.country.options[form.country.selectedIndex];
		return VerifyRequired(form.required_name, 'your name') &&
				VerifyRequired(form.required_email, 'an email address') &&
				VerifyRequired(country, 'a country') &&
				VerifyCountry(country) &&
				VerifyRequired(form.postcode, 'a postal code, or "n/a"') &&
				VerifyPostCode(form.postcode, country) &&
				VerifyPaypalEmail(form);
	}

	return false;
}



