var joinFormDidSubmit = function(e){
	var amount = $('#amount').val();
	if(amount.length > 0){
		amount = parseFloat(amount);
		if(amount == NaN){
			amount = 0;
		}
	}
	
	$('#paypal_amount').val(amount);
	
	$.ajax({
		url: this.action
		, global: false
		, type: 'POST'
		, data: $('#join_form').serialize() + '&send_html_fragment=true'
		, dataType: 'html'
		, success: function(data, textStatus){
			var result = data.split(',');
			if(result.length > 0 && result[0] == 'ok'){
				$('#user_message').html('<p>Thank you for joining the campaign.</p>')
				if(amount > 0){
					$('#user_message').append('<p>Please wait while we redirect you to PayPal.</p>');
					$('#paypal_form').submit();
				}else{
					window.location = result[1];
				}
			}else{
				$('#user_message').html(data);
			}
		}
		, error: function(req, textStatus, errorThrown){
			$('#user_message').html(errorThrown);
		}
	});
	
	return false;
}

var didFocus = function(e){
	if(this.value == this.defaultValue){
		this.value = '';
	}
}
$(document).ready(function(){
	$('#join_form').submit(joinFormDidSubmit);
	$('#will_make_calls').focus();
	$('input.filledin').focus(didFocus);
});
