$(function() {
	// setup scrolling anchor links
	$('a#phamLink, a#richmanLink, a#jonesLink').click(function(e) {
		$.scrollTo($(this).attr("href"), 700);
		e.preventDefault();
	});
	// hide the form on page load
	$('form#signupForm').hide();
	// show the form when the appropriate link is clicked
	$('p#forAnInvite a').click(function(e) {
		$($(this).attr("href")).toggle();
		$('input#first_name').focus();
		e.preventDefault();
	});
	// handle the optional field
	$('input.optional').focus(function() {
		if ($(this).val() == this.defaultValue) {
			$(this).val('');
		}
	});	
	// handle external links
	$('a[rel=external]').click(function(e) {
		open(this.href);
		e.preventDefault();
	});
	// process the form
	$('form#signupForm').submit(function(e) {
		var errors = new Array();
		$(':input').each(function() {
			var field = $(this);
			// clean up the mess
			field.removeClass("showError");
			$('span#errors').remove();
			// check the fields
			if (field.hasClass("requiredText")) {
				if (!isEmpty(field)) {
					errors.push(field.attr("id").replace("_", ' '));
					$(field).addClass("showError");
				}
			}
			if (field.hasClass("requiredEmail")) {
				if (!isEmail(field)) {
					errors.push(field.attr("id").replace("_", ' '));
					$(field).addClass("showError");
				}
			}
		});
		if (errors.length > 0) {
			$("td#action").prepend($('<span id="errors" />').html('The following are required: ' + errors.join(", ")));
			e.preventDefault();
		};
	});
});

function isEmpty (field) {
	if (!field.val()) {
		return false;
	} else {
		return true;
	}
}

function isEmail(field) {
	if (!field.val().match(/^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/)) {
		return false;
	} else {
		return true;
	}
}