var form = {
	isValid: true,
	errors: {
		add: function(field, text) {
			form.isValid = false;
			$('label[for='+field+']').addClass('error').attr('title', text.replace('%%field%%', $('label[for='+field+']').html().replace('*', ''))).focus();
		},
		remove: function(field) {
			$('label[for='+field+']').removeClass('error');
		},
		clear: function() {
			$('label').removeClass('error');
			form.isValid = true;
		}
	},
	validate: {
		empty: function(fields) {
			$(fields).each(function() {
				if ($.trim($(this).val()).length == 0) {
					form.errors.add($(this).attr('id'), '%%field%% cannot be blank.');
				} else {
					form.errors.remove($(this).attr('id'));
				}
			});
		},
		zipcode: function(fields) {
			$(fields).each(function() {
				var pattern = /^\d{5}$|^\d{5}-\d{4}$/;
				if ($.trim($(this).val()).length > 0) {
					if (!$(this).val().match(pattern)) {
						form.errors.add($(this).attr('id'), 'Please provide a valid US Zip Code.')
					} else {
						form.errors.remove($(this).attr('id'));
					}
				}
			});
		},
		phone: function(fields) {
			$(fields).each(function() {
				var pattern = /^(1\s*[-\/\.]?)?(\((\d{3})\)|(\d{3}))\s*[-\/\.]?\s*(\d{3})\s*[-\/\.]?\s*(\d{4})\s*(([xX]|[eE][xX][tT])\.?\s*(\d+))*$/;
				if ($.trim($(this).val()).length > 0) {
					if (!$(this).val().match(pattern)) {
						form.errors.add($(this).attr('id'), 'Please provide a valid phone number.');
					} else {
						form.errors.remove($(this).attr('id'));
					}
				}
			});
		},
		email: function(fields) {
			$(fields).each(function() {
				var pattern = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
				if ($.trim($(this).val()).length > 0) {
					if (!$(this).val().match(pattern)) {
						form.errors.add($(this).attr('id'), 'Please provide a valid e-mail address.');
					} else {
						form.errors.remove($(this).attr('id'));
					}
				}
			});
		},
		compare: function(fields, comparisons) {
			$(fields).each(function(i) {
				if ($.trim($(this).val()).length > 0) {
					if ($(this).val() != $(comparisons[i]).val()) {
						form.errors.add($(this).attr('id'), 'Your e-mail addresses do not match.');
						form.errors.add($(comparisons[i]).attr('id'), 'Your e-mail addresses do not match.');
					} else {
						form.errors.remove($(this).attr('id'));
						form.errors.remove($(comparisons[i]).attr('id'));
					}
				}
			});
		},
		checked: function(group) {
			if ($(group).filter(':checked').length == 0) {
				$(group).each(function() {
					form.errors.add($(this).attr('id'), 'You must select an option.');
				});
			} else {
				$(group).each(function() {
					form.errors.remove($(this).attr('id'));
				});
			}
		}
	}
};