$(document).ready(function(){
	// Validates that the passwords match
	$("#signupForm").validate({
		rules: {
			password: "required",
			password_again: {
				equalTo: "#password"
			}
		}
	});

	// Submits the form
	$('#signupForm').submit(function(){
		// Disables the submit button
		$('input[type=submit]',  this).attr('disabled', 'disabled');
	
		// Validates the form
		if ($("#signupForm").valid() == true) {
			// Posts the form to the signup processing page
			$.post(
				"/register/process",
				$("#signupForm").serialize(),
				function (data) {
					if (data.type == 'success') {
						// Swaps the form for the PayPal redirect form and submits it
						//$("#viewport").html(data.redirectForm);
						//$('#paypalRedirectForm').submit();
						document.location.href = '/';
					}
					else {
						// Displays the error to the user
						alert(data.message);
					}
				},
				"json"
			);
		}
		
		// Enables the submit button
		$('input[type=submit]',  this).attr('disabled', '');

		// Keeps the form from posting to itself
		return false;
	});
});
