$(document).ready(function() {
	switch($('input[id=validate]').val()) {
		case 'register':

			// assign actions to the back and next buttons
			$('input.submit_button[value=Next], input.submit_button[value=Register]').click(function() {
				form.register.goNext(this);
			});
			$('input.submit_button[value=Back]').click(function() {
				form.register.goPrev(this);
			});
			
			break;
	}
});

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'));
				});
			}
		}
	},
	register: {
		validate: function(which) {
		
			// clear the errors
			form.errors.clear();
			
			// define the fieldset
			var fieldset = $(which).parent().closest('fieldset').attr('id');
			
			// run each validator according to section
			switch(fieldset) {
				case 'fs_contact':
					form.validate.empty($('#user_firstname, #user_lastname, #user_address_line1, #user_city, #user_state, #user_zipcode'));
					form.validate.zipcode($('#user_zipcode'));
					break;
				case 'fs_phone':
					form.validate.empty($('#user_phone_daytime'));
					form.validate.phone($('#user_phone_daytime, #user_phone_evening, #user_phone_fax'));
					break;
				case 'fs_email':
					form.validate.empty($('#user_email, #user_email_verify'));
					form.validate.email($('#user_email, #user_email_verify'));
					form.validate.compare($('#user_email'), $('#user_email_verify'));
					form.validate.checked($(':radio[name=user[contact_type]]'));
					break;
				case 'fs_profile':
					form.validate.checked($(':radio[name=user[is_accredited]]'));
					form.validate.checked($(':radio[name=user[is_experienced]]'));
					break;
			}
			
			// return the status
			return form.isValid;

		},
		goFirst: function(which) {
			alert(which);
		},
		goNext: function(which) {
			if (this.validate(which)) {
				$(which).parent().closest('fieldset').slideUp('fast', function() {
					$(this).next().slideDown('fast');
				});
			}
		},
		goPrev: function(which) {
			$(which).parent().closest('fieldset').slideUp('fast', function() {
				$(this).prev().slideDown('fast');
			});
		}
	}
};