// JavaScript Document
function checkForm() {
	/*	First, simple form validation	*/
	var notice = $('formError');
	
	if($F('first_name') == '' || $F('last_name') == '' || $F('email') == '') {
		notice.update('Please fill in at least your first name, last name and email address.').setStyle({ display:'block' });
		return false;
	}


	/*	check for valid email	*/
	if(!(is_valid_email($F('email')))) {
		notice.update('Please provide a valid email address so we may confirm your donation.').setStyle({ display:'block' });
		return false;
	}

/*	Lodge number is numeric	*/
if($F('lodgeNumber') != '') {
	if($F('lodgeNumber') != parseInt($F('lodgeNumber'))) {
		notice.update('Please enter only a numeric lodge number.').setStyle({ display:'block' });
		return false;
	}
}

/*	Member number is numeric */
if($F('memberNumber') != '') {
	if($F('memberNumber') != parseInt($F('memberNumber'))) {
		notice.update('Please enter only a numeric member number.').setStyle({ display:'block' });
		return false;
	}
}

/*	If zip <= 5, make sure it's numeric. Otherwise allow (for +4)	*/
	if($F('zip') != '' && $F('zip').length <= 5 && $F('zip') != parseInt($F('zip'))) {
		notice.update('Please enter only a numeric zipcode.').setStyle({ display:'block' });
		return false;
	}

/*	Check if a phone is provided. If so, make sure it's numeric.	*/
if($F('night_phone_a') != '' && $F('night_phone_b') != '' && $F('night_phone_c') != '') {
		if($F('night_phone_a') != parseInt($F('night_phone_a')) || $F('night_phone_b') != parseInt($F('night_phone_b'))) {
			notice.update('Please enter only a numeric phone number.').setStyle({ display:'block' });
			return false;
		}
}

/*	Validate the donation amount	*/
	try {
		var typeValue = Form.getInputs('donationForm','radio','amountSelector').find(function(radio) { return radio.checked; }).value;
		$('amount').value = typeValue;		
	} catch (e) {
		if($F('customAmount') != '') {
			$('amount').value = $F('customAmount');
		} else {
		notice.update('Please select or enter an amount to donate.').setStyle({ display:'block' });
		return false;
		}
	}

/*	Concatenate the lodge and member number; get them ready for sending	*/
	var lodgeNumber = $F('lodgeNumber');
	var memberNumber = $F('memberNumber');

	$('custom').value = '<LodgeNumber>' + lodgeNumber + '</LodgeNumber><MemberNumber>' + memberNumber + '</MemberNumber>';
}

function is_valid_email (email)
{
	return /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email);
}

function clearRadios() {
	$$('.amountSelector').each(function(e){e.checked=false;});
}

function clearTextbox(which) {
	which.value = '';
}