var $j = jQuery.noConflict();

$j(function(){

	// run this before submitting the donate form
	function checkAmounts() {
		// set variables
		var numberRegex = /^[+-]?\d+(\.\d+)?([eE][+-]?\d+)?$/;
		var amount;
		// if 'Other amount' is checked
		if( $j('#otherAmount').is(':checked') ) {
			// number in the 'Other amount' field
			if(numberRegex.test($j('.amountTextBox').val())) {
				amount = $j('.amountTextBox').val();
			}
			// no number in 'Other amount' field
			else {
				alert ( 'Please enter a number in the "Other amount" field' );
				return false;
			}
		}
		// if 'Other amount' is not checked
		else {
			amount = $j('#paypalForm input[type="radio"]:checked').val();
		}
		// if repeat gift every month is checked
		if( $j('#repeatMonthly').is(':checked') ) {
			$j('#cmd').val('_xclick-subscriptions');
			$j('#a3').val(amount);
		} 
		// if not
		else {
			$j('#cmd').val('_donations');
		}
		$j('#amount').val(amount);
		
		//alert ( $j('#paypalForm').serialize() );
		$j('#paypalForm').submit();
		//return false;
	}
	// /function checkAmounts (which runs before submitting the donate form)
	
	$j('#paypalDonateButton').click(checkAmounts);
	
	$j('.amountTextBox').focus(function(){
		$j('#otherAmount').attr('checked','checked');
	});
	
});
