function checkForm() {
	$('input, textarea').each(function() {
		$(this).val($.trim($(this).val()));
	});
	var missing = '';
	if ($('#rb_quantity').val() < 1 && $('#vb_quantity').val() < 1 && $('#mpm_quantity').val() < 1) missing += 'Quantity' + "\n";
	if (!$('#first_name').val()) missing += 'First Name' + "\n";
	if (!$('#last_name').val()) missing += 'Last Name' + "\n";
	if (!$('#company').val()) missing += 'Company' + "\n";
	if (!$('#address1').val() && !$('#address2').val() && !$('#address3').val()) missing += 'Address' + "\n";
	if (!$('#city').val()) missing += 'City' + "\n";
	if (!$('#country_select').val()) {
		missing += 'Country' + "\n";
	}
	else {
		switch ($('#country_select').val()) {
			case '2':
				if (!$('#state_select_2').val()) missing += 'State/Territory' + "\n";
				break;
			case '9':
				if (!$('#state_select_9').val()) missing += 'Province/Territory' + "\n";
				break;
			case '52':
				if (!$('#state_select_52').val()) missing += 'State/Possession' + "\n";
				break;
		}
	}
	if (!$('#postal_code').val()) missing += 'Postal Code' + "\n";
	if (!$('#email').val()) {
		missing += 'Email Address' + "\n";
	}
	else if (!$('#email').val().validEmail()) {
		missing += 'Valid Email Address' + "\n";
	}
	/*
	if (!$('#recaptcha_response_field').val()) missing += 'CAPTCHA Text' + "\n";
	*/
	if (missing) {
		alert('The following required fields are missing.' + "\n\n" + missing);
		return false;
	}
	return true;
} // end function checkForm

function displayCorrectSelect() {
	var countries_with_states = new Array(2, 9, 52);
	var country_id = $('#country_select').val();
	for (i = 0; i < countries_with_states.length; i++) {
		state_select_id = '#state_select_' + countries_with_states[i];
		state_tr_id =  '#state_tr_' + countries_with_states[i];
		if (country_id != countries_with_states[i]) {
			$(state_select_id).get(0).selectedIndex = 0;
			$(state_select_id).removeAttr('name');
			$(state_tr_id).hide();
		}
		else {
			$(state_select_id).attr('name', 'state_id');
			$(state_tr_id).show();
		}
	}
} // end function displayCorrectSelect

$(document).ready(function() {
	displayCorrectSelect();

	$('#country_select').change(function() {
		displayCorrectSelect();
	});

	$('#reset_button').click(function() {
		$('#subscription_form')[0].reset();
		displayCorrectSelect();
	});

	$('#subscription_form').submit(function(e) {
		if (checkForm()) {
			return true;
		}
		else {
			e.preventDefault();
			return false;
		}
	});

});
